/* ── tokens ── */
:root {
  --blue: #1e57a4;
  --blue-lt: #e8f0fb;
  --blue-md: #d0e2f8;
  --red:  #c0392b;
  --bg:   #eef0f5;
  --surface: #ffffff;
  --ink:  #12182a;
  --ink2: #3d4662;
  --muted:#8892aa;
  --line: #e4e7ef;
  --paper:#fefefe;
  --radius: 14px;
  --sh:   0 1px 2px rgba(18,24,42,.05), 0 4px 12px rgba(18,24,42,.05), 0 12px 32px rgba(18,24,42,.05);
  --sh-lg:0 4px 8px rgba(18,24,42,.06), 0 16px 48px rgba(18,24,42,.14), 0 32px 80px rgba(18,24,42,.08);
}

/* ── reset ── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
body {
  font-family: 'Plus Jakarta Sans', system-ui, -apple-system, sans-serif;
  background:
    radial-gradient(1100px 520px at 85% -8%, rgba(30,87,164,.10) 0%, transparent 62%),
    radial-gradient(900px 480px at -8% 108%, rgba(192,57,43,.05) 0%, transparent 55%),
    var(--bg);
  background-attachment: fixed;
  color: var(--ink);
  height: 100vh;
  overflow: hidden;
  display: flex;
  flex-direction: column;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-rendering: optimizeLegibility;
  accent-color: var(--blue);
}

/* ── scrollbars ── */
::-webkit-scrollbar { width: 5px; height: 5px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: #cdd4e4; border-radius: 99px; }
::-webkit-scrollbar-thumb:hover { background: var(--muted); }
::selection { background: var(--blue-md); }

/* ──────────────────── HEADER ──────────────────── */
.app-header {
  background: linear-gradient(180deg, #1a2138 0%, var(--ink) 100%);
  border-bottom: 1px solid rgba(255,255,255,.06);
  box-shadow: 0 1px 0 rgba(18,24,42,.2), 0 4px 20px rgba(18,24,42,.18);
  position: sticky;
  top: 0;
  z-index: 100;
  height: 54px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 1.25rem;
  flex-shrink: 0;
  gap: 1rem;
}
.brand {
  display: flex;
  align-items: center;
  gap: .65rem;
}
.brand-mark {
  width: 30px;
  height: 30px;
  border-radius: 7px;
  overflow: hidden;
  flex-shrink: 0;
  background: #fff;
  box-shadow: 0 0 0 1px rgba(255,255,255,.14), 0 2px 8px rgba(0,0,0,.35);
}
.brand-mark img { width: 100%; height: 100%; object-fit: cover; display: block; }
.brand-name {
  font-size: .95rem;
  font-weight: 800;
  color: #fff;
  letter-spacing: -.01em;
}
.brand-pipe {
  width: 1px;
  height: 14px;
  background: rgba(255,255,255,.2);
  flex-shrink: 0;
}
.brand-sub {
  font-size: .72rem;
  color: rgba(255,255,255,.45);
  font-weight: 500;
}
.hdr-btn {
  display: inline-flex;
  align-items: center;
  gap: .4rem;
  background: rgba(255,255,255,.08);
  border: 1px solid rgba(255,255,255,.12);
  color: rgba(255,255,255,.75);
  padding: .38rem .75rem;
  border-radius: 7px;
  font-size: .76rem;
  font-weight: 600;
  cursor: pointer;
  font-family: inherit;
  transition: background .15s, color .15s;
}
.hdr-btn:hover { background: rgba(255,255,255,.15); color: #fff; }
.hdr-btn .ico { width: 13px; height: 13px; flex-shrink: 0; }

/* ──────────────────── WORKSPACE ──────────────────── */
.workspace {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 1.25rem;
  padding: 1.25rem;
  flex: 1;
  min-height: 0;
  align-items: stretch;
  max-width: 1480px;
  width: 100%;
  margin: 0 auto;
}

/* ──────────────────── SHARED CARD ──────────────────── */
.panel {
  background: var(--surface);
  border-radius: var(--radius);
  box-shadow: var(--sh);
  border: 1px solid var(--line);
  overflow: hidden;
  display: flex;
  flex-direction: column;
  min-height: 0;
  animation: rise .45s cubic-bezier(.22,.8,.36,1) both;
}
.preview-panel { animation-delay: .08s; }
@keyframes rise { from { opacity: 0; transform: translateY(10px); } to { opacity: 1; transform: none; } }
@media (prefers-reduced-motion: reduce) { *, *::before, *::after { animation: none !important; transition: none !important; } }

/* ──────────────────── EDITOR PANEL ──────────────────── */
.editor-cap {
  background: linear-gradient(135deg, var(--blue) 0%, #2869c4 100%);
  padding: 1rem 1.25rem;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: .75rem;
  flex-shrink: 0;
}
.editor-cap-title {
  font-size: .72rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: .1em;
  color: rgba(255,255,255,.7);
}
.type-pill {
  background: rgba(255,255,255,.18);
  border: 1px solid rgba(255,255,255,.28);
  color: #fff;
  padding: .22rem .65rem;
  border-radius: 99px;
  font-size: .68rem;
  font-weight: 700;
  letter-spacing: .04em;
  white-space: nowrap;
}

.editor-body {
  padding: 1.1rem 1.25rem;
  overflow-y: auto;
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 0;
}

/* ── form sections ── */
.fsec { margin-bottom: 1rem; }
#dynamicFieldsContainer { display: grid; grid-template-columns: 1fr 1fr; gap: .65rem; margin-bottom: .75rem; }
#dynamicFieldsContainer:empty { display: none; }
#dynamicFieldsContainer .fgrp { margin-bottom: 0; }
.fsec:last-child { margin-bottom: .5rem; }
.fsec-label {
  font-size: .64rem;
  font-weight: 800;
  text-transform: uppercase;
  letter-spacing: .1em;
  color: var(--muted);
  display: flex;
  align-items: center;
  gap: .5rem;
  margin-bottom: .8rem;
}
.fsec-label::after {
  content: '';
  flex: 1;
  height: 1px;
  background: var(--line);
}

.fgrp { margin-bottom: .75rem; }
.fgrp:last-child { margin-bottom: 0; }
.fgrp label {
  display: block;
  font-size: .78rem;
  font-weight: 600;
  color: var(--ink2);
  margin-bottom: .35rem;
}
.frow { display: grid; grid-template-columns: 1fr 1fr; gap: .65rem; }

/* inputs */
select, input[type=date], input[type=email], input[type=text], input[type=password] {
  width: 100%;
  border: 1.5px solid var(--line);
  border-radius: 8px;
  padding: .7rem .9rem;
  font-size: .9rem;
  font-family: inherit;
  background: var(--surface);
  color: var(--ink);
  transition: border-color .15s, box-shadow .15s;
  appearance: none;
}
select {
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 24 24' fill='none' stroke='%238892aa' stroke-width='2.5'%3E%3Cpolyline points='6 9 12 15 18 9'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right .75rem center;
  padding-right: 2rem;
}
select:hover, input:hover { border-color: #c9d2e6; }
select:focus, input:focus { border-color: var(--blue); box-shadow: 0 0 0 3px var(--blue-lt); outline: none; }

.rich-input {
  border: 1.5px solid var(--line);
  border-radius: 8px;
  padding: .7rem .9rem;
  min-height: 3rem;
  font-family: inherit;
  font-size: .9rem;
  line-height: 1.5;
  outline: none;
  word-break: break-word;
  background: var(--surface);
  color: var(--ink);
  transition: border-color .15s, box-shadow .15s;
}
.rich-input:hover { border-color: #c9d2e6; }
.rich-input:focus { border-color: var(--blue); box-shadow: 0 0 0 3px var(--blue-lt); }
.rich-input[style*="min-height:80px"], .rich-input[style*="min-height: 80px"] { min-height: 80px; }

/* ── tip ── */
.tip {
  background: var(--blue-lt);
  border-left: 3px solid var(--blue);
  border-radius: 4px 8px 8px 4px;
  padding: .55rem .8rem;
  font-size: .73rem;
  color: var(--ink2);
  line-height: 1.55;
  margin-top: .25rem;
}
.tip b { color: var(--blue); }

/* ── action bar ── */
.action-bar {
  padding: .9rem 1.25rem;
  border-top: 1px solid var(--line);
  background: #f7f8fb;
  display: flex;
  flex-direction: column;
  gap: .55rem;
  flex-shrink: 0;
}
.arow { display: flex; gap: .5rem; }
.send-row { display: flex; gap: .5rem; flex: 1; }
.send-row input { flex: 1; min-width: 0; }

/* buttons */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: .4rem;
  padding: .58rem 1rem;
  border-radius: 8px;
  font-size: .8rem;
  font-weight: 600;
  font-family: inherit;
  cursor: pointer;
  transition: all .15s;
  border: 1.5px solid transparent;
  white-space: nowrap;
  flex-shrink: 0;
}
.btn .ico { width: 14px; height: 14px; flex-shrink: 0; }
.btn-primary {
  background: linear-gradient(180deg, #2a66ba 0%, var(--blue) 100%);
  color: #fff;
  border-color: var(--blue);
  box-shadow: 0 1px 2px rgba(18,24,42,.18), 0 4px 12px rgba(30,87,164,.28), inset 0 1px 0 rgba(255,255,255,.16);
}
.btn-primary:hover {
  background: linear-gradient(180deg, #2f70ca 0%, #2160b3 100%);
  transform: translateY(-1px);
  box-shadow: 0 2px 4px rgba(18,24,42,.18), 0 8px 20px rgba(30,87,164,.34), inset 0 1px 0 rgba(255,255,255,.16);
}
.btn-primary:active { transform: translateY(0); box-shadow: 0 1px 2px rgba(18,24,42,.2); }
.btn-secondary { background: var(--surface); color: var(--ink2); border-color: var(--line); box-shadow: 0 1px 2px rgba(18,24,42,.05); }
.btn-secondary:hover { background: var(--blue-lt); color: var(--blue); border-color: var(--blue-md); transform: translateY(-1px); box-shadow: 0 4px 12px rgba(30,87,164,.14); }
.btn-secondary:active { transform: translateY(0); }
.btn-full { flex: 1; }

/* ──────────────────── PREVIEW PANEL ──────────────────── */
.preview-cap {
  padding: .8rem 1.25rem;
  border-bottom: 1px solid var(--line);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  background: var(--surface);
  flex-shrink: 0;
}
.preview-cap-title {
  font-size: .72rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: .1em;
  color: var(--muted);
}
.live-badge {
  display: flex;
  align-items: center;
  gap: .35rem;
  background: var(--blue-lt);
  color: var(--blue);
  padding: .22rem .7rem;
  border-radius: 99px;
  font-size: .7rem;
  font-weight: 700;
  letter-spacing: .04em;
}
.live-dot {
  width: 6px; height: 6px;
  border-radius: 50%;
  background: #22c55e;
  box-shadow: 0 0 0 3px rgba(34,197,94,.18);
  flex-shrink: 0;
  animation: blink 2s ease-in-out infinite;
}
@keyframes blink { 0%,100%{opacity:1} 50%{opacity:.3} }

/* paper — fixed 700×990 A4 (matches print clone), scaled by JS to fit pane */
.letter-wrap {
  padding: 1rem;
  display: flex;
  justify-content: center;
  align-items: center;
  flex: 1;
  min-height: 0;
  overflow: hidden;
}
.letter-paper {
  background: var(--paper);
  border-radius: 3px;
  border: 1px solid #dde2ee;
  box-shadow: var(--sh-lg), 0 0 0 1px rgba(18,24,42,.03);
  width: 700px;
  height: 990px;
  overflow: hidden;
  position: relative;
  flex-shrink: 0;
  display: flex;
  flex-direction: column;
  transform-origin: center;
  transition: box-shadow .25s ease;
}
.letter-paper:hover {
  box-shadow: var(--sh-lg), 0 0 0 1px rgba(18,24,42,.04), 0 24px 64px rgba(18,24,42,.16);
}
.letter-stripe {
  height: 4px;
  flex-shrink: 0;
  background: linear-gradient(90deg, var(--blue) 0%, var(--blue) 62%, var(--red) 62%);
}
.letter-inner {
  flex: 1;
  overflow-y: auto;
  padding: 2.4rem 4.2rem 2.6rem;
  min-height: 0;
  display: flex;
  flex-direction: column;
}

/* letterhead — ultra-minimal Scandinavian: 2-col grid, identity dominant */
.letterhead {
  margin-bottom: 1.4rem;
  white-space: normal;
}
.lh-row {
  display: grid;
  grid-template-columns: 1fr auto;
  align-items: center;
  column-gap: 1.5rem;
  margin-bottom: .85rem;
}
.lh-ident {
  display: flex;
  align-items: center;
  gap: .85rem;
}
.lh-mark {
  width: 42px;
  height: 42px;
  border-radius: 9px;
  display: block;
  flex-shrink: 0;
}
.lh-name {
  font-family: 'Inter', 'Plus Jakarta Sans', sans-serif;
  font-size: 1.45rem;
  font-weight: 400;
  letter-spacing: -.02em;
  color: var(--ink);
}
.lh-contact {
  font-family: 'Inter', 'Plus Jakarta Sans', sans-serif;
  font-size: .68rem;
  color: var(--muted);
  letter-spacing: .02em;
  line-height: 1.8;
  text-align: right;
  font-style: normal;
}
.lh-contact a { color: var(--muted); text-decoration: none; }
.lh-contact b { color: var(--ink); font-weight: 700; }
.lh-rule {
  height: 1px;
  background: var(--line);
  border: none;
  margin: 0;
  width: 100%;
  display: block;
}

/* letter body */
.letter-content-display {
  font-family: 'Cormorant Garamond', Georgia, 'Times New Roman', serif;
  line-height: 1.58;
  /* .735rem × print zoom 1.1339 = 13.33px = exactly 10pt on paper */
  font-size: .735rem;
  color: #1a1e30;
  white-space: normal;
  flex: 1 0 auto;
  display: flex;
  flex-direction: column;
}
.letter-content-display strong { font-weight: 700; }

/* letter footer */
.letter-footer {
  margin-top: auto;
  padding-top: .85rem;
  border-top: 1px solid var(--line);
  font-family: 'Plus Jakarta Sans', sans-serif;
  font-size: .55rem;
  color: var(--muted);
  text-align: center;
  letter-spacing: .02em;
  line-height: 1.5;
}
.letter-footer b { color: var(--ink); font-weight: 700; }
.letter-sysnote {
  margin-bottom: .55rem;
  font-family: 'Plus Jakarta Sans', sans-serif;
  font-size: .58rem;
  font-style: italic;
  color: var(--muted);
  text-align: center;
  line-height: 1.5;
}

/* ──────────────────── MODAL ──────────────────── */
.modal {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(10,14,30,.55);
  align-items: center;
  justify-content: center;
  z-index: 200;
  padding: 1.5rem;
  backdrop-filter: blur(4px);
}
.modal-box {
  background: var(--surface);
  max-width: 540px;
  width: 100%;
  border-radius: 16px;
  box-shadow: var(--sh-lg);
  max-height: 90vh;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
}
.modal-hdr {
  padding: 1.2rem 1.5rem;
  border-bottom: 1px solid var(--line);
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-shrink: 0;
}
.modal-hdr h3 { font-size: .95rem; font-weight: 700; }
.modal-close {
  background: none; border: none; cursor: pointer; color: var(--muted);
  font-size: 1.35rem; line-height: 1; padding: 0; display: flex;
  align-items: center; transition: color .15s;
}
.modal-close:hover { color: var(--ink); }
.modal-content { padding: 1.5rem; flex: 1; }
.modal-desc { font-size: .78rem; color: var(--muted); line-height: 1.6; margin-bottom: 1.25rem; }
.modal-sec {
  font-size: .64rem; font-weight: 800; text-transform: uppercase;
  letter-spacing: .1em; color: var(--muted);
  margin: 1.2rem 0 .75rem;
  padding-top: 1rem;
  border-top: 1px solid var(--line);
}
.modal-sec:first-child { margin-top: 0; padding-top: 0; border-top: none; }
.modal-ftr {
  padding: 1rem 1.5rem;
  border-top: 1px solid var(--line);
  background: #f7f8fb;
  display: flex;
  align-items: center;
  gap: 1rem;
  flex-shrink: 0;
}
.modal-ftr p { font-size: .74rem; color: var(--muted); flex: 1; line-height: 1.5; }

/* ──────────────────── RESPONSIVE ──────────────────── */
@media (max-width: 820px) {
  body { height: auto; min-height: 100vh; overflow: auto; }
  .workspace { grid-template-columns: 1fr; }
  .editor-body { max-height: none; }
  .letter-wrap { overflow: visible; }
}
@media (max-width: 580px) {
  .workspace { padding: .75rem; gap: .75rem; }
  .letter-inner { padding: 2rem 1.75rem 1.5rem; }
  .arow { flex-wrap: wrap; }
  .brand-sub, .brand-pipe { display: none; }
}
