  :root {
    --ink: #1f2328;
    --muted: #5b6370;
    --line: #d5d9e0;
    --soft: #f3f5f8;
    --bg: #eef1f5;
    --paper: #ffffff;
    --accent: #1d4ed8;
    --accent-soft: #e8eefc;
    --danger: #b42318;
    --ok: #067647;
    --radius: 8px;
  }
  * { box-sizing: border-box; }
  body {
    margin: 0; background: var(--bg); color: var(--ink);
    font: 14px/1.45 "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  }

  /* ---------- App bar ---------- */
  .appbar {
    position: sticky; top: 0; z-index: 10;
    display: flex; align-items: center; justify-content: space-between; gap: 12px; flex-wrap: wrap;
    padding: 12px 20px; background: var(--paper); border-bottom: 1px solid var(--line);
  }
  .appbar h1 { margin: 0; font-size: 17px; }
  .appbar .actions { display: flex; gap: 8px; flex-wrap: wrap; }
  .btn {
    font: inherit; font-size: 13px; padding: 7px 14px; border-radius: 6px; cursor: pointer;
    border: 1px solid var(--line); background: var(--paper); color: var(--ink);
  }
  .btn:hover { background: var(--soft); }
  .btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
  .btn.primary:hover { filter: brightness(1.08); }
  .btn.link { border: 0; background: none; color: var(--accent); padding: 4px 0; }
  .btn.icon { padding: 6px 9px; color: var(--danger); }

  /* ---------- Layout ---------- */
  .layout {
    display: grid; grid-template-columns: minmax(380px, 520px) 1fr; gap: 20px;
    padding: 20px; max-width: 1500px; margin: 0 auto;
  }
  .preview-pane { position: sticky; top: 76px; align-self: start; max-height: calc(100vh - 96px); overflow: auto; }

  /* ---------- Form cards ---------- */
  .card {
    background: var(--paper); border: 1px solid var(--line); border-radius: var(--radius);
    margin-bottom: 14px; overflow: hidden;
  }
  .card > summary {
    list-style: none; cursor: pointer; padding: 12px 16px; display: flex; align-items: center; gap: 10px;
    font-weight: 600; user-select: none;
  }
  .card > summary::-webkit-details-marker { display: none; }
  .card > summary .step {
    width: 24px; height: 24px; border-radius: 50%; display: grid; place-items: center;
    background: var(--accent-soft); color: var(--accent); font-size: 12px; flex: none;
  }
  .card > summary .hint { margin-left: auto; font-weight: 400; color: var(--muted); font-size: 12px; }
  .card > summary::after { content: "▾"; color: var(--muted); transition: transform .15s; }
  .card:not([open]) > summary::after { transform: rotate(-90deg); }
  .sub-head { margin-top: 16px; font-size: 11px; font-weight: 700; text-transform: uppercase; letter-spacing: .6px; color: var(--muted); }
  .card .body { padding: 4px 16px 16px; border-top: 1px solid var(--line); }

  .row { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; margin-top: 12px; }
  .row.three { grid-template-columns: 1fr 1fr 1fr; }
  .row.one { grid-template-columns: 1fr; }
  .field { display: flex; flex-direction: column; gap: 4px; min-width: 0; }
  .field label { font-size: 12px; font-weight: 600; color: var(--muted); }
  .field label .req { color: var(--danger); }
  input, select, textarea {
    font: inherit; font-size: 14px; color: var(--ink); width: 100%;
    padding: 8px 10px; border: 1px solid var(--line); border-radius: 6px; background: var(--paper);
  }
  textarea { resize: vertical; min-height: 60px; }
  input:focus, select:focus, textarea:focus { outline: 2px solid var(--accent); outline-offset: -1px; border-color: var(--accent); }
  input.invalid { border-color: var(--danger); }
  .msg { font-size: 11px; min-height: 14px; }
  .msg.err { color: var(--danger); }
  .msg.ok { color: var(--ok); }
  .check { display: flex; align-items: center; gap: 8px; margin-top: 12px; font-size: 13px; }
  .check input { width: auto; }
  .pill {
    display: inline-block; margin-top: 12px; padding: 6px 10px; border-radius: 6px;
    background: var(--accent-soft); color: var(--accent); font-size: 12px; font-weight: 600;
  }

  /* Items editor */
  .item {
    border: 1px solid var(--line); border-radius: 6px; padding: 10px 12px 12px; margin-top: 12px; background: var(--soft);
  }
  .item-head { display: flex; justify-content: space-between; align-items: center; }
  .item-head b { font-size: 12px; color: var(--muted); text-transform: uppercase; letter-spacing: .5px; }
  .item .row { margin-top: 8px; }
  .item .row.four { grid-template-columns: 1fr 1fr 1fr 1fr; }
  .item-total { margin-top: 8px; text-align: right; font-size: 12px; color: var(--muted); }
  .item-total b { color: var(--ink); }
  .add-item { margin-top: 12px; width: 100%; border-style: dashed; color: var(--accent); }

  /* ---------- Invoice preview ---------- */
  .inv {
    background: var(--paper); border: 1px solid var(--line); padding: 28px;
    box-shadow: 0 2px 12px rgba(0,0,0,.06); font-size: 14.5px; min-width: 640px;
  }
  .inv .top { display: flex; justify-content: space-between; gap: 20px; border-bottom: 2px solid var(--ink); padding-bottom: 14px; }
  .inv .brand h2 { margin: 0 0 4px; font-size: 22px; }
  .inv .brand p { margin: 0; color: var(--muted); white-space: pre-line; }
  .inv .brand .gst { color: var(--ink); font-weight: 600; margin-top: 4px; }
  .inv .title { text-align: right; }
  .inv .title h3 { margin: 0; font-size: 20px; letter-spacing: 2px; color: var(--accent); }
  .inv .title small { font-size: 10px; color: var(--muted); text-transform: uppercase; }
  .inv .grid { display: grid; grid-template-columns: 1fr 1fr; border: 1px solid var(--line); border-top: 0; }
  .inv .grid > div { padding: 10px 12px; }
  .inv .grid > div + div { border-left: 1px solid var(--line); }
  .inv h4 { margin: 0 0 4px; font-size: 10.5px; text-transform: uppercase; letter-spacing: .8px; color: var(--muted); }
  .inv .grid p { margin: 0; white-space: pre-line; }
  .inv .kv { display: grid; grid-template-columns: 120px 1fr; row-gap: 2px; }
  .inv .kv span:nth-child(odd) { color: var(--muted); }
  .inv .meta { margin-top: 14px; border-top: 1px solid var(--line); }
  .inv table { width: 100%; border-collapse: collapse; }
  .inv .items { margin-top: 14px; }
  .inv .items th, .inv .items td, .inv .hsn th, .inv .hsn td { border: 1px solid var(--line); padding: 6px 7px; vertical-align: top; }
  .inv .items th, .inv .hsn th { background: var(--soft); font-size: 10.5px; text-transform: uppercase; text-align: left; }
  .inv .items tfoot td { font-weight: 600; background: var(--soft); }
  .num { text-align: right; white-space: nowrap; font-variant-numeric: tabular-nums; }
  .center { text-align: center; }
  .inv.intra .igst-col { display: none; }
  .inv.inter .cgst-col, .inv.inter .sgst-col { display: none; }
  .inv .bottom { display: flex; gap: 14px; margin-top: 12px; align-items: flex-start; }
  .inv .bottom > div:first-child { flex: 1.3; min-width: 0; }
  .inv .bottom > div:last-child { flex: 1; min-width: 0; }
  .inv .box { border: 1px solid var(--line); padding: 9px 12px; }
  .inv .box + .box { margin-top: 8px; }
  .inv .words { font-weight: 600; margin: 0; }
  .inv .totals td { padding: 5px 8px; border-bottom: 1px solid var(--line); }
  .inv .totals .grand td { font-size: 14px; font-weight: 700; border-top: 2px solid var(--ink); border-bottom: 2px solid var(--ink); }
  .inv ol { margin: 0; padding-left: 18px; color: var(--muted); }
  /* Items: description takes all spare width, other columns shrink to content */
  .inv .items th, .inv .items td { width: 1%; white-space: nowrap; }
  .inv .items td:not(.desc), .inv .items th:not(.desc) { padding-left: 5px; padding-right: 5px; }
  .inv .amt-note { margin-top: 14px; text-align: right; font-size: 10px; color: var(--muted); }
  .inv .amt-note + .items { margin-top: 3px; }
  .inv .items tr, .inv .box, .inv .totals, .inv .sign-box, .inv .decl { break-inside: avoid; page-break-inside: avoid; }
  .inv .items .desc { width: auto; white-space: normal; overflow-wrap: anywhere; }
  .inv .items .desc span { display: block; }
  .inv .items .d-main { font-weight: 600; }
  .inv .items .d-addl { color: var(--muted); font-size: 11px; white-space: pre-line; margin-top: 2px; }
  .inv .items .d-disc { color: var(--muted); font-size: 11px; font-style: italic; }

  /* HSN tax summary: small, under bank details */
  .inv .hsn-box { padding: 8px 10px; }
  .inv .hsn-box { break-inside: auto; page-break-inside: auto; }

  /* Reference numbers (P.O. / DC) */
  .inv .ref-dt { color: var(--muted); font-size: 11px; white-space: nowrap; }

  /* Bank details: compact */
  .inv .bank-box { padding: 7px 10px; }
  .inv .bank-box .kv { grid-template-columns: 90px 1fr; row-gap: 0; font-size: 10.5px; line-height: 1.4; }

  /* Terms: compact */
  .inv .terms-box { margin-top: 10px; padding: 7px 10px; }
  .inv .terms-box ol { font-size: 10px; line-height: 1.4; padding-left: 15px; }
  .inv .hsn { font-size: 9.5px; margin-top: 4px; }
  .inv .hsn th, .inv .hsn td { padding: 2px 5px; line-height: 1.25; }
  .inv .hsn tfoot { display: table-row-group; } /* print the total once, not on every page */
  .inv .hsn tr { break-inside: avoid; }
  .inv .hsn th { font-size: 9px; }
  .inv .hsn tfoot td { font-weight: 600; background: var(--soft); }
  .inv .decl { margin-top: 8px; color: var(--muted); font-size: 10.5px; line-height: 1.4; }
  .inv .sign-box { margin-top: 10px; border: 1px solid var(--line); padding: 8px 10px; text-align: right; font-size: 11.5px; }
  .inv .sign-box .space { height: 44px; }
  .inv .foot { margin-top: 10px; text-align: center; color: var(--muted); font-size: 10.5px; }
  .empty { color: #a0a7b2; font-style: italic; }

  /* ---------- Address lines in preview ---------- */
  .inv .addr span { display: block; }
  .inv .brand .addr { white-space: normal; }

  /* ---------- Preview stage (A4 width, scaled to fit) ---------- */
  .stage { width: 100%; overflow: hidden; }
  .form-pane, .preview-pane { min-width: 0; }
  .inv { width: 794px; min-width: 0; transform-origin: top left; }

  /* ---------- Edit / Preview tabs (hidden on desktop) ---------- */
  .tabs { display: none; }

  /* ===== DESKTOP  ≥ 1200px : form and preview side by side ===== */
  @media (min-width: 1200px) {
    .layout { grid-template-columns: minmax(420px, 540px) 1fr; }
  }

  /* ===== TABLET  768px – 1199px : tabs, roomy two-column form ===== */
  @media (max-width: 1199px) {
    .layout { grid-template-columns: minmax(0, 1fr); max-width: 900px; padding: 16px 24px; }
    .preview-pane { position: static; max-height: none; overflow: visible; }
    .tabs {
      display: flex; gap: 6px; position: sticky; top: 57px; z-index: 9;
      max-width: 900px; margin: 0 auto; padding: 10px 24px; background: var(--bg);
    }
    .tabs button { flex: 1; padding: 9px; font-weight: 600; }
    .tabs button.active { background: var(--accent); color: #fff; border-color: var(--accent); }
    body[data-tab="form"] .preview-pane { display: none; }
    body[data-tab="preview"] .form-pane { display: none; }
    .inv { box-shadow: 0 2px 12px rgba(0,0,0,.08); }
  }

  /* ===== MOBILE  < 768px : single column, larger touch targets ===== */
  @media (max-width: 767px) {
    body { font-size: 15px; }
    .appbar { padding: 10px 16px; }
    .appbar h1 { font-size: 16px; }
    .appbar .actions { width: 100%; }
    .appbar .actions .btn { flex: 1; padding: 9px 4px; font-size: 13px; white-space: nowrap; }
    .tabs { top: 101px; padding: 8px 16px; }
    .layout { padding: 8px 16px 24px; }
    .card > summary { padding: 12px 14px; }
    .card > summary .hint { display: none; }
    .card > summary::after { margin-left: auto; }
    .card .body { padding: 2px 14px 14px; }
    .row, .row.three, .item .row.four { grid-template-columns: 1fr; gap: 10px; }
    .row.addr { grid-template-columns: 1fr 1fr; }
    .row.addr .field:first-child { grid-column: 1 / -1; }
    .item .row.four { grid-template-columns: 1fr 1fr; }
    input, select, textarea { font-size: 16px; padding: 10px 12px; } /* 16px stops iOS zoom */
    .btn.icon { padding: 8px 12px; }
  }

  /* ---------- Print: invoice only ---------- */
  @media print {
    @page { size: A4; margin: 10mm; }
    html, body { overflow: visible !important; background: #fff; }
    header:not(.appbar), .appbar, .tabs, .form-pane, footer { display: none !important; }
    .layout, .container-fluid, .row { display: block; padding: 0; margin: 0; overflow: visible; }
    .preview-pane { display: block !important; position: static; max-height: none; overflow: visible; }
    .stage { height: auto !important; overflow: visible; }
    .inv { border: 0; box-shadow: none; padding: 0; width: auto; transform: none !important; font-size: 11.5px; }
    .inv .brand h2 { font-size: 17px; }
    .inv .title h3 { font-size: 16px; }
    .inv th, .inv tfoot td, .inv .hsn tfoot td { -webkit-print-color-adjust: exact; print-color-adjust: exact; }
  }

/* ---------- Compact invoice spacing (fits a typical invoice on one A4 page) ---------- */
.inv { line-height: 1.4; }
.inv .top { padding-bottom: 10px; }
.inv .brand h2 { font-size: 22px; margin-bottom: 2px; }
.inv .grid > div { padding: 7px 10px; }
.inv .kv { row-gap: 0; }
.inv .amt-note { margin-top: 8px; }
.inv .items th, .inv .items td { padding-top: 4px; padding-bottom: 4px; }
.inv .box { padding: 7px 10px; }
.inv .totals td { padding: 4px 8px; }
.inv .terms-box, .inv .sign-box { margin-top: 8px; }
.inv .decl { margin-top: 6px; }
.inv .sign-box .space { height: 40px; }
.inv .foot { margin-top: 8px; }

/* ---------- Company logo ---------- */
.logo-field { display: flex; gap: 14px; align-items: center; margin-top: 12px; }
.logo-thumb {
  width: 72px; height: 72px; flex: none; border: 1px dashed var(--line); border-radius: 6px;
  display: grid; place-items: center; background: var(--soft); overflow: hidden;
}
.logo-thumb span { font-size: 11px; color: var(--muted); text-transform: uppercase; letter-spacing: .5px; }
.logo-thumb img { max-width: 100%; max-height: 100%; object-fit: contain; }
.logo-actions { display: flex; flex-direction: column; gap: 6px; min-width: 0; }
.field-label { font-size: 12px; font-weight: 600; color: var(--muted); }
.logo-btns { display: flex; gap: 8px; }

/* Logo in the invoice: top-left, beside the business name */
.inv .brand-wrap { display: flex; gap: 14px; align-items: flex-start; min-width: 0; }
.inv .logo-slot {
  width: 90px; height: 90px; flex: none; display: grid; place-items: center; overflow: hidden;
}
.inv .logo-slot img { max-width: 100%; max-height: 100%; object-fit: contain; }
.inv .logo-slot.empty-logo {
  border: 1px dashed var(--line); color: #a0a7b2; font-size: 11px; letter-spacing: 1px;
}
@media print {
  .inv .logo-slot.empty-logo { display: none; } /* no logo uploaded: no empty box on paper */
}
