:root {
  --paper: #f4f0e8;
  --canvas: #faf7f1;
  --ink: #1b1915;
  --muted: #6e695f;
  --line: #e4ddd1;
  --surface: #fffdf8;
  --amber: #c9a227;
  --amber-ink: #5c4708;
  --amber-soft: #f4e7bc;
  --green: #2c6b4a;
  --green-soft: #dceadf;
  --rose: #9a3b3b;
  --rose-soft: #f3d9d7;
  --ink-soft: #ece8df;
  --shadow: 0 18px 50px rgba(40, 32, 18, 0.07);
  --radius: 18px;
}

* { box-sizing: border-box; }
html, body { margin: 0; min-height: 100%; }
body {
  font-family: "Instrument Sans", sans-serif;
  background: var(--paper);
  color: var(--ink);
  font-size: 15px;
  line-height: 1.5;
}

h1, h2, h3 {
  font-family: "Instrument Sans", sans-serif;
  font-weight: 600;
  letter-spacing: -0.02em;
  margin: 0;
}
h1 { font-size: clamp(1.75rem, 2.5vw, 2.1rem); line-height: 1.2; }
h2 { font-size: 1.15rem; }
h3 { font-size: 1.2rem; }

a { color: inherit; }
button, input, select { font: inherit; }

.app {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

.topbar {
  display: flex;
  align-items: center;
  gap: 1.5rem;
  padding: 0.85rem 1.5rem;
  border-bottom: 1px solid var(--line);
  background: rgba(250, 247, 241, 0.86);
  backdrop-filter: blur(16px);
  position: sticky;
  top: 0;
  z-index: 10;
}

.brand { display: flex; align-items: center; gap: 0.6rem; min-width: 8rem; }
.brand-name { font-weight: 600; letter-spacing: -0.02em; }
.mark {
  width: 14px;
  height: 14px;
  border-radius: 4px;
  background: linear-gradient(140deg, #f0cf5a, #c9a227 55%, #8d7012);
  box-shadow: 0 0 0 3px rgba(201, 162, 39, 0.16);
  display: inline-block;
}
.mark.lg { width: 22px; height: 22px; }

.tabs {
  display: flex;
  gap: 0.2rem;
  padding: 0.2rem;
  background: #efe9dc;
  border-radius: 999px;
}
.tab {
  text-decoration: none;
  padding: 0.4rem 0.95rem;
  border-radius: 999px;
  color: var(--muted);
  font-weight: 500;
  font-size: 0.92rem;
}
.tab.is-active { background: var(--surface); color: var(--ink); box-shadow: 0 1px 2px rgba(40, 32, 18, 0.06); }

.topbar-end { margin-left: auto; display: flex; align-items: center; gap: 0.75rem; }

.canvas { flex: 1; padding: 2rem 1.5rem 3.5rem; }
.page { max-width: 1080px; margin: 0 auto; }
.page-head {
  display: flex;
  justify-content: space-between;
  align-items: flex-end;
  gap: 1rem;
  margin-bottom: 1.75rem;
}
.page-head.tight { margin-bottom: 1.25rem; }
.eyebrow {
  margin: 0 0 0.35rem;
  text-transform: uppercase;
  letter-spacing: 0.12em;
  font-size: 0.72rem;
  font-weight: 600;
  color: var(--muted);
}
.lede, .subtle { color: var(--muted); margin: 0.4rem 0 0; }
.hint { color: var(--muted); font-size: 0.92rem; }

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border: 0;
  border-radius: 999px;
  padding: 0.7rem 1.15rem;
  cursor: pointer;
  text-decoration: none;
  font-weight: 600;
  transition: transform 0.15s ease, background 0.15s ease, opacity 0.15s ease;
}
.btn:hover { transform: translateY(-1px); }
.btn:disabled { opacity: 0.45; cursor: not-allowed; transform: none; }
.btn-primary { background: var(--ink); color: #f8f3e8; }
.btn-ghost { background: transparent; border: 1px solid var(--line); color: var(--ink); }
.btn-sm { padding: 0.45rem 0.85rem; font-size: 0.88rem; }

.stat-row { display: grid; grid-template-columns: repeat(3, 1fr); gap: 0.85rem; margin-bottom: 1.25rem; }
.stat {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 1.1rem 1.2rem;
}
.stat-label { color: var(--muted); font-size: 0.82rem; }
.stat strong { display: block; font-size: 1.75rem; font-family: "Instrument Sans", sans-serif; font-weight: 600; margin-top: 0.2rem; }

.panel {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 1.15rem 1.2rem 0.4rem;
}
.panel-head {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 1rem;
  padding-bottom: 0.85rem;
  margin-bottom: 0.2rem;
}

.row-list { list-style: none; margin: 0; padding: 0; }
.row-item {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 0.75rem;
  align-items: center;
  border-top: 1px solid var(--line);
}
.row-item:first-child { border-top: 0; }
.row {
  display: grid;
  grid-template-columns: 1fr auto auto;
  gap: 1rem;
  align-items: center;
  padding: 0.95rem 0.2rem;
  border-top: 0;
  text-decoration: none;
  min-width: 0;
}
.contact-row {
  grid-template-columns: auto 1fr auto auto;
}
.row-pdf { white-space: nowrap; }
.row-main { display: flex; flex-direction: column; gap: 0.15rem; min-width: 0; }
.row-title { font-weight: 600; }
.row-meta, .row-date { color: var(--muted); font-size: 0.88rem; }
.row:hover .row-title { color: var(--amber-ink); }
.pdf-frame {
  width: 100%;
  min-height: 70vh;
  border: 0;
  background: #f3f0ea;
}

.pill {
  display: inline-flex;
  align-items: center;
  border-radius: 999px;
  padding: 0.2rem 0.6rem;
  font-size: 0.75rem;
  font-weight: 600;
  letter-spacing: 0.01em;
}
.pill.lg { padding: 0.4rem 0.8rem; font-size: 0.82rem; }
.pill-amber { background: var(--amber-soft); color: var(--amber-ink); }
.pill-green { background: var(--green-soft); color: var(--green); }
.pill-ink { background: var(--ink-soft); color: var(--ink); }
.pill-rose { background: var(--rose-soft); color: var(--rose); }
.pill-muted { background: #efeae0; color: var(--muted); }

.empty { text-align: center; padding: 3rem 1rem 3.4rem; color: var(--muted); }
.empty.tall { padding: 6rem 1rem; }
.empty h3 { color: var(--ink); margin-bottom: 0.4rem; }
.empty .btn { margin-top: 1rem; }

.text-link { color: var(--muted); text-decoration: none; font-size: 0.9rem; font-weight: 500; }
.text-link:hover { color: var(--ink); }

.avatar {
  width: 34px;
  height: 34px;
  border-radius: 50%;
  background: #ece3cc;
  color: var(--amber-ink);
  display: grid;
  place-items: center;
  font-size: 0.72rem;
  font-weight: 700;
}
.avatar.sm { width: 36px; height: 36px; }

.filters { display: flex; gap: 0.45rem; margin-bottom: 1rem; }
.chip {
  text-decoration: none;
  padding: 0.35rem 0.75rem;
  border-radius: 999px;
  border: 1px solid var(--line);
  color: var(--muted);
  font-size: 0.88rem;
}
.chip.is-active { background: var(--ink); color: #f8f3e8; border-color: var(--ink); }

.steps {
  display: flex;
  gap: 0.75rem;
  list-style: none;
  padding: 0;
  margin: 0 0 1.4rem;
  color: var(--muted);
  font-size: 0.9rem;
}
.steps li { display: flex; align-items: center; gap: 0.4rem; }
.steps span {
  width: 22px;
  height: 22px;
  border-radius: 50%;
  display: grid;
  place-items: center;
  background: #efe9dc;
  font-size: 0.75rem;
  font-weight: 700;
}
.steps .is-active { color: var(--ink); font-weight: 600; }
.steps .is-active span { background: var(--ink); color: #fff; }
.steps .is-done span { background: var(--amber); color: #1b1915; }

.send-grid {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 280px;
  gap: 1.25rem;
  align-items: start;
}
.wizard {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 1.25rem 1.25rem 1rem;
  min-height: 420px;
  display: flex;
  flex-direction: column;
}
.step-pane { display: none; animation: rise 0.18s ease; }
.step-pane.is-active { display: block; }
.wizard-bar { display: flex; align-items: center; margin-top: auto; padding-top: 1.1rem; }
.spacer { flex: 1; }

.field { display: flex; flex-direction: column; gap: 0.4rem; margin-bottom: 1rem; }
.field > span { font-size: 0.8rem; font-weight: 600; color: var(--muted); }
.field input, .field select {
  border: 1px solid var(--line);
  background: #fff;
  border-radius: 12px;
  padding: 0.75rem 0.85rem;
  outline: none;
}
.field input:focus, .field select:focus { border-color: #c9b27a; box-shadow: 0 0 0 4px rgba(201, 162, 39, 0.14); }

.choice-list { list-style: none; margin: 0; padding: 0; max-height: 320px; overflow: auto; }
.choice {
  width: 100%;
  display: flex;
  gap: 0.8rem;
  align-items: center;
  text-align: left;
  background: transparent;
  border: 0;
  border-radius: 12px;
  padding: 0.7rem 0.45rem;
  cursor: pointer;
}
.choice:hover, .choice.is-selected { background: #f6f0e2; }
.choice-copy { display: flex; flex-direction: column; }
.choice-copy em { color: var(--muted); font-style: normal; font-size: 0.86rem; }

.template-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0.8rem;
}
.template-card {
  text-align: left;
  border: 1px solid var(--line);
  background: #fff;
  border-radius: 16px;
  padding: 0.95rem;
  cursor: pointer;
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
}
.template-card em, .template-card .meta { font-style: normal; color: var(--muted); font-size: 0.84rem; }
.template-card.is-selected { border-color: var(--amber); box-shadow: 0 0 0 3px rgba(201, 162, 39, 0.16); }
.doc-thumb {
  height: 64px;
  border-radius: 10px;
  background:
    linear-gradient(#fff, #fff) padding-box,
    repeating-linear-gradient(180deg, #efe7d4 0 8px, transparent 8px 16px);
  border: 1px solid var(--line);
  margin-bottom: 0.45rem;
}

.field-table { display: grid; gap: 0.65rem; }
.field-row {
  display: grid;
  grid-template-columns: 140px 1fr auto;
  gap: 0.7rem;
  align-items: center;
}
.field-row label { color: var(--muted); font-size: 0.88rem; }
.field-row input {
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 0.6rem 0.7rem;
}

.recipient {
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 0.85rem 1rem;
  background: #fff;
}
.recipient strong { display: block; }
.recipient span { color: var(--muted); font-size: 0.88rem; }

.summary {
  background: #f7f1e4;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 1.15rem 1.2rem;
  position: sticky;
  top: 5.2rem;
}
.summary dl { margin: 1rem 0 0; }
.summary dl > div { padding: 0.7rem 0; border-top: 1px solid #eadfcb; }
.summary dt { color: var(--muted); font-size: 0.75rem; text-transform: uppercase; letter-spacing: 0.08em; }
.summary dd { margin: 0.2rem 0 0; font-weight: 600; }

.detail-grid { display: grid; grid-template-columns: 1.2fr 0.8fr; gap: 1rem; }
.timeline { list-style: none; margin: 0; padding: 0 0 1rem; }
.timeline li { display: flex; gap: 0.8rem; padding: 0.7rem 0; }
.timeline .dot {
  width: 10px; height: 10px; border-radius: 50%;
  background: var(--amber); margin-top: 0.4rem; flex: none;
}
.timeline em { display: block; color: var(--muted); font-style: normal; font-size: 0.86rem; }
.plain-list { list-style: none; margin: 0; padding: 0 0 1rem; }
.plain-list li { padding: 0.75rem 0; border-top: 1px solid var(--line); display: flex; flex-direction: column; }
.plain-list em { color: var(--muted); font-style: normal; font-size: 0.84rem; }

.stack { display: grid; gap: 1rem; }
.connect-form { display: grid; grid-template-columns: 1fr 160px auto; gap: 0.8rem; align-items: end; }
.connected-line { color: var(--muted); margin: 0 0 0.5rem; }
.ds-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 0.75rem;
  align-items: flex-end;
  margin-top: 1rem;
}
.ds-reconnect {
  display: flex;
  flex-wrap: wrap;
  gap: 0.75rem;
  align-items: flex-end;
  flex: 1;
  min-width: 240px;
  margin: 0;
}
.ds-reconnect .field {
  margin: 0;
  min-width: 160px;
  max-width: 220px;
}
.ds-actions #disconnectBtn {
  margin-left: auto;
}

.prepare-overlay {
  position: fixed;
  inset: 0;
  z-index: 60;
  background: var(--paper);
  display: flex;
  flex-direction: column;
}
.prepare-overlay[hidden] { display: none !important; }
.prepare-bar {
  display: flex;
  align-items: center;
  gap: 0.85rem;
  padding: 0.75rem 1rem;
  border-bottom: 1px solid var(--line);
  background: #fff;
}
.prepare-bar strong { font-size: 0.95rem; }
.prepare-bar span { color: var(--muted); font-size: 0.85rem; }
.prepare-frame {
  flex: 1;
  width: 100%;
  border: 0;
  background: #f3f0ea;
}
.toggle-list { list-style: none; margin: 0; padding: 0 0 0.8rem; }
.toggle-list li {
  display: flex;
  justify-content: space-between;
  gap: 1rem;
  align-items: center;
  padding: 0.85rem 0;
  border-top: 1px solid var(--line);
}
.toggle-list span { display: block; color: var(--muted); font-size: 0.88rem; }

.switch { position: relative; width: 40px; height: 24px; flex: none; }
.switch input { opacity: 0; width: 0; height: 0; }
.switch span {
  position: absolute; inset: 0;
  background: #ddd4c4;
  border-radius: 999px;
  transition: 0.15s ease;
}
.switch span:before {
  content: "";
  position: absolute;
  width: 18px; height: 18px;
  left: 3px; top: 3px;
  background: #fff;
  border-radius: 50%;
  transition: 0.15s ease;
}
.switch input:checked + span { background: var(--ink); }
.switch input:checked + span:before { transform: translateX(16px); }

.map-table { display: grid; gap: 0.55rem; }
.map-head, .map-row {
  display: grid;
  grid-template-columns: 1fr 1fr 80px;
  gap: 0.7rem;
  align-items: center;
}
.map-head-cfg, .map-row-cfg {
  grid-template-columns: 56px minmax(140px, 1.2fr) 1fr 80px;
}
.map-head { color: var(--muted); font-size: 0.75rem; letter-spacing: 0.06em; text-transform: uppercase; }
.map-row input {
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 0.6rem 0.7rem;
}
.map-source strong { display: block; font-size: 0.92rem; }
.map-source em { display: block; color: var(--muted); font-size: 0.75rem; font-style: normal; margin-top: 0.15rem; }
.map-table { max-height: 480px; overflow: auto; padding-right: 0.25rem; }

.pager {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
  margin-top: 0.85rem;
}
.pager-meta {
  color: var(--muted);
  font-size: 0.82rem;
  text-align: center;
  flex: 1;
}
.envelope-toolbar {
  display: flex;
  gap: 0.75rem;
  align-items: flex-end;
  margin: 0 0 1rem;
  flex-wrap: wrap;
}
.envelope-search { flex: 1; min-width: 220px; margin: 0; }
.envelope-pager {
  margin-top: 0;
  padding: 0.85rem 0.2rem 0.2rem;
  border-top: 1px solid var(--line);
}

.toast {
  position: fixed;
  bottom: 1.2rem;
  right: 1.2rem;
  background: var(--ink);
  color: #f8f3e8;
  padding: 0.75rem 1rem;
  border-radius: 12px;
  font-size: 0.9rem;
  z-index: 40;
}
.boot {
  display: none;
  position: fixed;
  inset: 0;
  background: var(--paper);
  place-items: center;
  z-index: 50;
  pointer-events: none;
}
.boot.is-on {
  display: grid;
  pointer-events: auto;
}
.boot-card { text-align: center; color: var(--muted); }
.boot-card p { margin-top: 0.7rem; }

@keyframes rise {
  from { opacity: 0; transform: translateY(6px); }
  to { opacity: 1; transform: none; }
}

@media (max-width: 860px) {
  .topbar { flex-wrap: wrap; }
  .send-grid, .detail-grid, .stat-row, .template-grid, .connect-form, .field-row, .map-head, .map-row, .ds-actions, .ds-reconnect {
    grid-template-columns: 1fr;
  }
  .ds-actions #disconnectBtn { margin-left: 0; width: 100%; }
  .row { grid-template-columns: 1fr auto; }
  .contact-row { grid-template-columns: auto 1fr; }
  .contact-row .row-date { display: none; }
  .row-item { grid-template-columns: 1fr; }
  .row-pdf { margin: 0 0 0.75rem 0.2rem; justify-self: start; }
  .row-date { display: none; }
  .summary { position: static; }
}
