/* ============================================================
   slipbook — warm paper & ledger. ledger-green ink,
   stamp vermilion, perforated receipt tear-edge signature.
   ============================================================ */

:root {
  --paper:    #FBF7EF;   /* warm off-white paper base */
  --paper2:   #F4EEE1;   /* slightly deeper card paper */
  --paper3:   #EFE7D6;   /* inset / table zebra */
  --ink:      #1B1A17;   /* charcoal ink */
  --ink-soft: #6B6558;   /* muted ink */
  --ink-faint:#9A9284;   /* hints, placeholders */
  --rule:     #E4DAC7;   /* ledger ruling / hairlines */
  --rule2:    #D6C9AF;   /* stronger rule */
  --brand:    #1F4E46;   /* deep ledger-green — the one spot color */
  --brand-dk: #163A34;
  --brand-dim:rgba(31,78,70,0.09);
  --stamp:    #B4322A;   /* stamp vermilion */
  --stamp-dim:rgba(180,50,42,0.10);

  --sans: system-ui, -apple-system, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  --mono: ui-monospace, "SF Mono", Menlo, Consolas, "Liberation Mono", monospace;

  --radius: 12px;
  --radius-sm: 8px;
  --shadow: 0 22px 48px -30px rgba(50,40,20,0.5);
  --shadow-doc: 0 26px 60px -32px rgba(40,32,14,0.42);
  --wrap: 1180px;
  --ease: cubic-bezier(0.22, 0.61, 0.36, 1);
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }

body {
  margin: 0;
  background: var(--paper);
  color: var(--ink);
  font-family: var(--sans);
  font-size: 16px;
  line-height: 1.55;
  letter-spacing: -0.003em;
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
}

/* faint ledger ruling across the whole page background */
body::before {
  content: "";
  position: fixed;
  inset: 0;
  z-index: -1;
  pointer-events: none;
  background-image:
    linear-gradient(180deg, var(--rule) 1px, transparent 1px),
    radial-gradient(1100px 560px at 88% -10%, var(--brand-dim), transparent 62%);
  background-size: 100% 34px, 100% 100%;
  opacity: 0.5;
}

h1, h2, h3 { margin: 0; font-weight: 800; letter-spacing: -0.025em; line-height: 1.08; }
p { margin: 0; }

.mono { font-family: var(--mono); font-variant-numeric: tabular-nums; }

.skip-link {
  position: absolute; left: 12px; top: -60px;
  background: var(--brand); color: var(--paper);
  padding: 10px 16px; border-radius: 10px; font-weight: 700;
  z-index: 60; transition: top 0.18s var(--ease); text-decoration: none;
}
.skip-link:focus { top: 12px; }

/* shared focus ring */
a:focus-visible,
button:focus-visible,
input:focus-visible,
select:focus-visible,
textarea:focus-visible,
.seg2__opt:focus-within,
.toggle:focus-within {
  outline: 3px solid var(--brand);
  outline-offset: 2px;
  border-radius: 6px;
}

.eyebrow {
  font-size: 0.72rem;
  text-transform: uppercase;
  letter-spacing: 0.18em;
  color: var(--ink-soft);
  font-weight: 700;
}

/* ============================================================
   HERO
   ============================================================ */
.hero {
  position: relative;
  overflow: hidden;
  background:
    linear-gradient(180deg, #FCFAF4 0%, var(--paper) 100%);
  border-bottom: 1px solid var(--rule2);
}
/* the perforated top edge motif on the hero, echoing a receipt */
.hero__rules {
  position: absolute;
  left: 0; right: 0; bottom: 0;
  height: 3px;
  background:
    repeating-linear-gradient(90deg, var(--rule2) 0 10px, transparent 10px 20px);
  opacity: 0.9;
}
.hero__inner {
  position: relative;
  z-index: 1;
  max-width: var(--wrap);
  margin: 0 auto;
  padding: clamp(26px, 4vw, 46px) clamp(20px, 5vw, 44px) clamp(30px, 4vw, 44px);
}

.brandline { display: flex; align-items: center; gap: 12px; color: var(--brand); }
.brandmark { width: 30px; height: 30px; display: block; }
.wordmark {
  font-size: 1.42rem; font-weight: 800; letter-spacing: -0.045em;
  color: var(--brand); line-height: 1;
}

.eyebrow { margin-top: 22px; }
.hero__title {
  font-size: clamp(2.1rem, 5.6vw, 3.6rem);
  margin: 12px 0 0;
  max-width: 15ch;
  color: var(--ink);
}
.hero__accent { color: var(--brand); }
.hero__lede {
  margin-top: 16px;
  max-width: 58ch;
  color: var(--ink-soft);
  font-size: clamp(0.98rem, 1.3vw, 1.1rem);
}
.hero__cta {
  margin-top: 26px;
  display: flex; align-items: center; gap: 18px; flex-wrap: wrap;
}
.hero__note { font-size: 0.85rem; color: var(--ink-soft); }

/* ============================================================
   BUTTONS
   ============================================================ */
.btn {
  font-family: var(--sans);
  font-weight: 700;
  font-size: 0.95rem;
  letter-spacing: -0.01em;
  border-radius: 10px;
  cursor: pointer;
  border: 1px solid transparent;
  padding: 12px 22px;
  text-decoration: none;
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  transition: transform 0.12s var(--ease), background 0.16s var(--ease), border-color 0.16s var(--ease), box-shadow 0.16s var(--ease);
}
.btn:active { transform: translateY(1px); }
.btn--primary {
  background: var(--brand);
  color: var(--paper);
  box-shadow: 0 14px 30px -14px rgba(31,78,70,0.7);
}
.btn--primary:hover { background: var(--brand-dk); }
.btn--ghost {
  background: var(--paper);
  color: var(--ink);
  border-color: var(--rule2);
  padding: 10px 18px;
}
.btn--ghost:hover { border-color: var(--brand); color: var(--brand); }
.btn--sm { padding: 8px 14px; font-size: 0.85rem; }
.btn--file {
  background: var(--paper);
  color: var(--ink);
  border-color: var(--rule2);
  padding: 10px 18px;
  position: relative;
}
.btn--file:hover { border-color: var(--brand); color: var(--brand); }
.btn--add {
  background: transparent;
  color: var(--brand);
  border: 1.5px dashed var(--rule2);
  width: 100%;
  padding: 12px;
  margin-top: 12px;
}
.btn--add span { font-size: 1.15rem; line-height: 0; font-weight: 700; }
.btn--add:hover { border-color: var(--brand); background: var(--brand-dim); }

/* ============================================================
   ACTION BAR (sticky)
   ============================================================ */
.actionbar {
  position: sticky;
  top: 0;
  z-index: 30;
  background: rgba(251,247,239,0.9);
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
  border-bottom: 1px solid var(--rule2);
}
.actionbar__inner {
  max-width: var(--wrap);
  margin: 0 auto;
  padding: 12px clamp(20px, 5vw, 44px);
  display: flex; align-items: center; justify-content: space-between;
  gap: 16px; flex-wrap: wrap;
}
.actionbar__label {
  font-size: 0.72rem; text-transform: uppercase; letter-spacing: 0.16em;
  font-weight: 700; color: var(--ink-soft);
}
.actionbar__buttons { display: flex; gap: 10px; flex-wrap: wrap; }

/* ============================================================
   WORKSPACE LAYOUT
   ============================================================ */
.workspace {
  max-width: var(--wrap);
  margin: 0 auto;
  padding: clamp(22px, 3.5vw, 40px) clamp(20px, 5vw, 44px) clamp(50px, 7vw, 80px);
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
  gap: 26px;
  align-items: start;
}

/* ============================================================
   EDITOR
   ============================================================ */
.editor { display: flex; flex-direction: column; gap: 16px; min-width: 0; }

.panel {
  border: 1px solid var(--rule2);
  background: var(--paper2);
  border-radius: var(--radius);
  padding: 18px 18px 20px;
  margin: 0;
  min-width: 0;
}
.panel__legend {
  font-size: 0.7rem;
  text-transform: uppercase;
  letter-spacing: 0.15em;
  font-weight: 800;
  color: var(--brand);
  padding: 0 6px;
  margin-left: -6px;
}
.panel__hint {
  margin-top: 14px;
  font-size: 0.8rem;
  color: var(--ink-soft);
  line-height: 1.45;
}

.grid { display: grid; gap: 12px; margin-top: 12px; }
.grid--2 { grid-template-columns: 1fr 1fr; }
.grid--3 { grid-template-columns: repeat(3, minmax(0, 1fr)); }

.field { display: flex; flex-direction: column; gap: 6px; min-width: 0; }
.field--full { grid-column: 1 / -1; }
.field__label {
  font-size: 0.76rem; font-weight: 700; color: var(--ink);
  letter-spacing: 0.005em;
}
.field__opt { color: var(--ink-faint); font-weight: 500; text-transform: none; letter-spacing: 0; }

input[type=text],
input[type=tel],
input[type=email],
input[type=number],
input[type=date],
select,
textarea {
  font-family: var(--sans);
  font-size: 0.95rem;
  color: var(--ink);
  background: var(--paper);
  border: 1px solid var(--rule2);
  border-radius: var(--radius-sm);
  padding: 10px 12px;
  width: 100%;
  min-width: 0;
}
input.mono { font-family: var(--mono); }
textarea { resize: vertical; line-height: 1.45; }
input::placeholder, textarea::placeholder { color: var(--ink-faint); }
input:hover, select:hover, textarea:hover { border-color: var(--rule2); }
input:focus, select:focus, textarea:focus { border-color: var(--brand); }
select {
  appearance: none; -webkit-appearance: none;
  background-image:
    linear-gradient(45deg, transparent 50%, var(--ink-soft) 50%),
    linear-gradient(135deg, var(--ink-soft) 50%, transparent 50%);
  background-position: right 15px center, right 10px center;
  background-size: 5px 5px, 5px 5px;
  background-repeat: no-repeat;
  padding-right: 32px;
}
input[type=number] { -moz-appearance: textfield; }
input[type=number]::-webkit-outer-spin-button,
input[type=number]::-webkit-inner-spin-button { -webkit-appearance: none; margin: 0; }

/* logo row */
.logo-row {
  display: flex; align-items: center; gap: 16px;
  margin-top: 16px;
}
.logo-preview {
  flex: none;
  width: 74px; height: 74px;
  border: 1.5px dashed var(--rule2);
  border-radius: var(--radius-sm);
  display: grid; place-items: center;
  background: var(--paper);
  overflow: hidden;
}
.logo-preview img { width: 100%; height: 100%; object-fit: contain; }
.logo-preview__hint { font-size: 0.68rem; color: var(--ink-faint); }
.logo-controls { display: flex; flex-direction: column; gap: 8px; min-width: 0; }
.logo-buttons { display: flex; gap: 8px; flex-wrap: wrap; }

/* PAID toggle */
.toggle { position: relative; display: inline-flex; margin-top: 14px; }
.toggle input { position: absolute; opacity: 0; inset: 0; cursor: pointer; margin: 0; }
.toggle span {
  display: inline-flex; align-items: center; gap: 12px;
  font-size: 0.9rem; font-weight: 500; color: var(--ink-soft);
  cursor: pointer; padding: 4px 2px; line-height: 1.35;
}
.toggle span b { color: var(--stamp); font-weight: 800; letter-spacing: 0.02em; }
.toggle span::before {
  content: "";
  flex: none;
  width: 42px; height: 24px; border-radius: 999px;
  background: var(--paper3);
  border: 1px solid var(--rule2);
  position: relative;
  transition: background 0.18s var(--ease), border-color 0.18s var(--ease);
}
.toggle span::after {
  content: "";
  position: absolute; left: 4px;
  width: 18px; height: 18px; border-radius: 50%;
  background: var(--ink-faint);
  transition: transform 0.18s var(--ease), background 0.18s var(--ease);
}
.toggle input:checked + span { color: var(--ink); }
.toggle input:checked + span::before { background: var(--stamp-dim); border-color: var(--stamp); }
.toggle input:checked + span::after { transform: translateX(18px); background: var(--stamp); }

/* discount composite control */
.discount { display: flex; gap: 8px; }
.discount input { flex: 1; }
.seg2 {
  display: inline-flex; flex: none;
  background: var(--paper);
  border: 1px solid var(--rule2);
  border-radius: var(--radius-sm);
  padding: 3px;
}
.seg2__opt { position: relative; }
.seg2__opt input { position: absolute; opacity: 0; inset: 0; cursor: pointer; margin: 0; }
.seg2__opt span {
  display: grid; place-items: center;
  width: 34px; height: 100%;
  min-height: 32px;
  border-radius: 6px;
  font-weight: 700; font-size: 0.9rem;
  color: var(--ink-soft);
  cursor: pointer;
  font-family: var(--mono);
  transition: background 0.14s var(--ease), color 0.14s var(--ease);
}
.seg2__opt input:checked + span { background: var(--brand); color: var(--paper); }

/* items table (editor) */
.panel--items { padding-bottom: 18px; }
.items-head {
  display: grid;
  grid-template-columns: minmax(0,1fr) 58px 92px 92px 30px;
  gap: 8px;
  margin-top: 14px;
  padding: 0 2px 6px;
  border-bottom: 1px solid var(--rule);
  font-size: 0.68rem; text-transform: uppercase; letter-spacing: 0.08em;
  font-weight: 700; color: var(--ink-soft);
}
.items-head__qty, .items-head__price, .items-head__total { text-align: right; }
.item-rows { display: flex; flex-direction: column; }
.item-row {
  display: grid;
  grid-template-columns: minmax(0,1fr) 58px 92px 92px 30px;
  gap: 8px;
  align-items: center;
  padding: 8px 2px;
  border-bottom: 1px solid var(--rule);
}
.item-row input { padding: 8px 10px; font-size: 0.9rem; }
.item-row .r-qty, .item-row .r-price { text-align: right; font-family: var(--mono); }
.item-row .r-total {
  text-align: right; font-family: var(--mono); font-variant-numeric: tabular-nums;
  font-size: 0.88rem; color: var(--ink); font-weight: 600;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.item-row__x {
  border: none; background: transparent; cursor: pointer;
  width: 28px; height: 28px; border-radius: 6px;
  color: var(--ink-faint); font-size: 1.15rem; line-height: 1;
  display: grid; place-items: center;
  transition: background 0.14s var(--ease), color 0.14s var(--ease);
}
.item-row__x:hover { background: var(--stamp-dim); color: var(--stamp); }

/* ============================================================
   LIVE PREVIEW / INVOICE DOCUMENT (the printed thing)
   ============================================================ */
.preview-wrap { position: sticky; top: 76px; min-width: 0; }
.preview-wrap__hint {
  font-size: 0.7rem; text-transform: uppercase; letter-spacing: 0.14em;
  font-weight: 700; color: var(--ink-soft);
  margin-bottom: 10px; padding-left: 4px;
}

.invoice {
  position: relative;
  background: #FFFFFF;
  color: var(--ink);
  border: 1px solid var(--rule2);
  box-shadow: var(--shadow-doc);
  padding: 34px 34px 30px;
  /* the signature: subtle carbon-copy ledger ruling behind the doc */
  background-image: linear-gradient(180deg, rgba(31,78,70,0.028) 1px, transparent 1px);
  background-size: 100% 30px;
  overflow: hidden;
}
/* perforated tear edges — the slipbook signature */
.invoice__perf {
  position: absolute; left: 0; right: 0; height: 12px;
  background:
    radial-gradient(circle at 8px 6px, var(--paper) 5px, transparent 5.5px);
  background-size: 16px 12px;
  background-position: 0 0;
}
.invoice__perf--top { top: -6px; }
.invoice__perf--bottom { bottom: -6px; }

.doc-head {
  display: flex; justify-content: space-between; align-items: flex-start;
  gap: 20px;
  padding-bottom: 20px;
  border-bottom: 2px solid var(--ink);
}
.doc-head__biz { display: flex; gap: 14px; align-items: flex-start; min-width: 0; }
.doc-head__logo {
  width: 56px; height: 56px; object-fit: contain;
  border-radius: 8px; flex: none;
}
.doc-head__biztext { min-width: 0; }
.doc-head__name {
  font-size: 1.25rem; font-weight: 800; letter-spacing: -0.02em;
  color: var(--ink); word-break: break-word;
}
.doc-head__meta {
  margin-top: 5px;
  font-size: 0.8rem; color: var(--ink-soft); line-height: 1.5;
  white-space: pre-line; word-break: break-word;
}
.doc-head__title { text-align: right; flex: none; }
.doc-title {
  font-size: 1.5rem; font-weight: 800; letter-spacing: 0.12em;
  color: var(--brand);
}
.doc-meta {
  margin: 10px 0 0;
  display: flex; flex-direction: column; gap: 3px;
  font-size: 0.82rem;
}
.doc-meta div { display: flex; justify-content: flex-end; gap: 10px; }
.doc-meta dt { color: var(--ink-soft); margin: 0; }
.doc-meta dd { margin: 0; color: var(--ink); font-weight: 600; }

.doc-billto {
  margin-top: 20px;
}
.doc-billto__label {
  font-size: 0.68rem; text-transform: uppercase; letter-spacing: 0.12em;
  font-weight: 700; color: var(--ink-soft); margin-bottom: 4px;
}
.doc-billto__name { font-weight: 700; font-size: 0.98rem; color: var(--ink); word-break: break-word; }
.doc-billto__contact { font-size: 0.84rem; color: var(--ink-soft); white-space: pre-line; word-break: break-word; margin-top: 2px; }

.doc-table {
  width: 100%;
  border-collapse: collapse;
  margin-top: 22px;
  font-size: 0.86rem;
}
.doc-table th {
  text-align: left;
  font-size: 0.66rem; text-transform: uppercase; letter-spacing: 0.08em;
  font-weight: 700; color: var(--ink-soft);
  padding: 0 8px 8px; border-bottom: 1px solid var(--rule2);
}
.doc-table th.doc-table__num { text-align: right; }
.doc-table td {
  padding: 9px 8px;
  border-bottom: 1px solid var(--rule);
  vertical-align: top;
}
.doc-table__desc { word-break: break-word; }
.doc-table td.dt-num {
  text-align: right; font-family: var(--mono); font-variant-numeric: tabular-nums;
  white-space: nowrap;
}
.doc-table__empty td {
  color: var(--ink-faint); font-style: italic; text-align: center; padding: 18px 8px;
}

.doc-foot {
  display: flex; justify-content: space-between; gap: 30px;
  margin-top: 22px; flex-wrap: wrap;
}
.doc-notes { flex: 1 1 200px; min-width: 0; }
.doc-notes__label {
  font-size: 0.66rem; text-transform: uppercase; letter-spacing: 0.1em;
  font-weight: 700; color: var(--ink-soft); margin-bottom: 4px;
}
.doc-notes__body { font-size: 0.82rem; color: var(--ink-soft); white-space: pre-line; word-break: break-word; }

.doc-totals { flex: 0 0 auto; min-width: 220px; margin-left: auto; }
.doc-totals__row {
  display: flex; justify-content: space-between; gap: 24px;
  padding: 6px 0;
  font-size: 0.88rem; color: var(--ink-soft);
}
.doc-totals__row span:last-child { color: var(--ink); font-weight: 600; }
.doc-totals__row--grand {
  margin-top: 4px; padding-top: 12px;
  border-top: 2px solid var(--ink);
  font-size: 1.05rem; font-weight: 800; color: var(--ink);
}
.doc-totals__row--grand span:last-child { color: var(--brand); font-weight: 800; }

.doc-thanks {
  margin-top: 24px; padding-top: 16px;
  border-top: 1px dashed var(--rule2);
  text-align: center;
  font-size: 0.84rem; color: var(--ink-soft);
}

/* PAID stamp */
.stamp {
  position: absolute;
  top: 42%; right: 40px;
  transform: rotate(-14deg);
  font-family: var(--sans);
  font-size: 2.6rem; font-weight: 900; letter-spacing: 0.06em;
  color: var(--stamp);
  border: 5px solid var(--stamp);
  border-radius: 10px;
  padding: 6px 22px;
  opacity: 0.82;
  pointer-events: none;
  box-shadow: inset 0 0 0 2px rgba(180,50,42,0.25);
  mix-blend-mode: multiply;
}

/* ============================================================
   FOOTER
   ============================================================ */
.foot {
  border-top: 1px solid var(--rule2);
  background: var(--paper2);
}
.foot__inner {
  max-width: var(--wrap);
  margin: 0 auto;
  padding: 28px clamp(20px, 5vw, 44px) 40px;
}
.foot__disclaimer {
  font-size: 0.85rem; color: var(--ink-soft); max-width: 80ch; line-height: 1.55;
}
.foot__disclaimer strong { color: var(--ink); }
.foot__meta {
  margin-top: 16px;
  display: flex; flex-wrap: wrap; gap: 8px 10px;
  font-size: 0.78rem; color: var(--ink-soft);
}
.foot__dot { color: var(--rule2); }

/* ============================================================
   RESPONSIVE
   ============================================================ */
@media (max-width: 940px) {
  .workspace { grid-template-columns: 1fr; }
  .preview-wrap { position: static; }
  .preview-wrap__hint { margin-top: 8px; }
}
@media (max-width: 560px) {
  .grid--2, .grid--3 { grid-template-columns: 1fr; }
  .actionbar__buttons { width: 100%; }
  .actionbar__buttons .btn { flex: 1; }
  .items-head { grid-template-columns: minmax(0,1fr) 46px 76px 30px; }
  .items-head__total { display: none; }
  .item-row { grid-template-columns: minmax(0,1fr) 46px 76px 30px; }
  .item-row .r-total { display: none; }
  .invoice { padding: 24px 20px; }
  .doc-head { flex-direction: column; }
  .doc-head__title { text-align: left; }
  .doc-meta div { justify-content: flex-start; }
  .stamp { right: 24px; font-size: 2.1rem; }
}

/* ============================================================
   REDUCED MOTION
   ============================================================ */
@media (prefers-reduced-motion: reduce) {
  * { animation-duration: 0.001ms !important; animation-iteration-count: 1 !important; transition-duration: 0.001ms !important; }
}

/* ============================================================
   PRINT — only the invoice document, clean on A4/Letter
   ============================================================ */
@media print {
  body { background: #fff; color: #000; }
  body::before { display: none; }
  .hero, .actionbar, .editor, .foot,
  .skip-link, .preview-wrap__hint { display: none !important; }

  main, .workspace { display: block; margin: 0; padding: 0; max-width: none; }
  .preview-wrap { position: static; top: auto; }

  .invoice {
    border: none;
    box-shadow: none;
    background: #fff;
    background-image: none;
    padding: 0;
    color: #000;
  }
  .invoice__perf { display: none !important; }

  .doc-head { border-bottom: 2px solid #000; }
  .doc-head__name, .doc-meta dd, .doc-billto__name,
  .doc-table td, .doc-totals__row span:last-child { color: #000; }
  .doc-title { color: #000; }
  .doc-head__meta, .doc-billto__contact, .doc-meta dt,
  .doc-table th, .doc-notes__body, .doc-totals__row,
  .doc-thanks, .doc-billto__label, .doc-notes__label { color: #222; }
  .doc-totals__row--grand { border-top: 2px solid #000; color: #000; }
  .doc-totals__row--grand span:last-child { color: #000; }
  .doc-table th, .doc-table td { border-color: #ccc; }
  .doc-table th { border-bottom: 1px solid #000; }

  .stamp {
    color: #000 !important;
    border-color: #000 !important;
    box-shadow: none;
    opacity: 1;
    mix-blend-mode: normal;
    -webkit-print-color-adjust: exact;
    print-color-adjust: exact;
  }

  .doc-table { break-inside: auto; }
  .doc-table tr { break-inside: avoid; }

  @page { margin: 16mm; size: auto; }
}
