/* ============================================================
   Invoice Generator — app #001
   Palette is token-driven so the whole UI themes from one block.
   ============================================================ */

:root {
  --bg:            #f4f5f7;
  --surface:       #ffffff;
  --surface-2:     #fafbfc;
  --border:        #e2e5ea;
  --border-strong: #cfd4dc;
  --text:          #1c2024;
  --text-muted:    #6b7480;
  --text-faint:    #98a1ad;
  --accent:        #2f6fed;
  --accent-hover:  #2559c8;
  --accent-soft:   #eaf0fe;
  --danger:        #d4483b;
  --shadow:        0 1px 2px rgba(16, 24, 40, .06), 0 4px 12px rgba(16, 24, 40, .06);
  --radius:        10px;
  --radius-sm:     7px;
}

:root[data-theme="dark"] {
  --bg:            #14171b;
  --surface:       #1c2026;
  --surface-2:     #22272e;
  --border:        #2e353e;
  --border-strong: #3d4650;
  --text:          #e7eaee;
  --text-muted:    #99a2ae;
  --text-faint:    #6f7885;
  --accent:        #5b8dfa;
  --accent-hover:  #7ba3fb;
  --accent-soft:   #1e2942;
  --danger:        #f0705f;
  --shadow:        0 1px 2px rgba(0, 0, 0, .3), 0 4px 14px rgba(0, 0, 0, .3);
}

*, *::before, *::after { box-sizing: border-box; }

/* Several components below set an explicit `display`, which would otherwise
   beat the UA stylesheet's `[hidden] { display: none }`. */
[hidden] { display: none !important; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.55 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto,
        "Helvetica Neue", Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
}

/* ─────────────────────────  Top bar  ───────────────────────── */

.topbar {
  position: sticky;
  top: 0;
  z-index: 20;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 12px 20px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}

.topbar__brand {
  display: flex;
  align-items: center;
  gap: 9px;
  font-weight: 650;
  letter-spacing: -.01em;
}

.topbar__mark { font-size: 20px; }

.topbar__actions { display: flex; gap: 8px; }

/* ─────────────────────────  Buttons  ───────────────────────── */

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 8px 14px;
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-sm);
  background: var(--surface);
  color: var(--text);
  font: inherit;
  font-weight: 550;
  font-size: 14px;
  cursor: pointer;
  transition: background .13s, border-color .13s, color .13s;
}

.btn:hover { background: var(--surface-2); border-color: var(--text-faint); }

.btn:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

.btn--primary {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}

.btn--primary:hover { background: var(--accent-hover); border-color: var(--accent-hover); }

.btn--sm { padding: 6px 11px; font-size: 13px; }

.btn--block { width: 100%; }

.btn--ghost { background: transparent; }

/* ─────────────────────────  Layout  ───────────────────────── */

.layout {
  display: grid;
  grid-template-columns: minmax(340px, 420px) 1fr;
  gap: 24px;
  align-items: start;
  max-width: 1500px;
  margin: 0 auto;
  padding: 24px 20px 60px;
}

@media (max-width: 1000px) {
  .layout { grid-template-columns: 1fr; }
}

/* ─────────────────────────  Editor  ───────────────────────── */

.editor {
  display: flex;
  flex-direction: column;
  gap: 14px;
  position: sticky;
  top: 76px;
  max-height: calc(100vh - 100px);
  overflow-y: auto;
  padding-right: 4px;
}

@media (max-width: 1000px) {
  .editor { position: static; max-height: none; overflow: visible; }
}

.card {
  /* The editor is a scrolling flex column — without this, cards compress to
     fit the container instead of overflowing it. */
  flex: 0 0 auto;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  overflow: hidden;
}

.card__title {
  padding: 13px 16px;
  font-weight: 620;
  font-size: 14px;
  cursor: pointer;
  list-style: none;
  user-select: none;
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.card__title::-webkit-details-marker { display: none; }

.card__title::after {
  content: "▾";
  color: var(--text-faint);
  font-size: 12px;
  transition: transform .18s;
}

.card[open] .card__title::after { transform: rotate(180deg); }

.card__body {
  display: flex;
  flex-direction: column;
  gap: 13px;
  padding: 4px 16px 17px;
  border-top: 1px solid var(--border);
  padding-top: 15px;
}

.field { display: flex; flex-direction: column; gap: 5px; }

.field > span {
  font-size: 12.5px;
  font-weight: 560;
  color: var(--text-muted);
}

.field-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
}

input, textarea, select {
  width: 100%;
  padding: 8px 11px;
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-sm);
  background: var(--surface-2);
  color: var(--text);
  font: inherit;
  font-size: 14px;
}

input:focus, textarea:focus, select:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-soft);
}

textarea { resize: vertical; min-height: 62px; }

select {
  appearance: none;
  background-image: linear-gradient(45deg, transparent 50%, var(--text-faint) 50%),
                    linear-gradient(135deg, var(--text-faint) 50%, transparent 50%);
  background-position: calc(100% - 16px) 52%, calc(100% - 11px) 52%;
  background-size: 5px 5px, 5px 5px;
  background-repeat: no-repeat;
  padding-right: 32px;
}

.hint { margin: 0; font-size: 12px; color: var(--text-faint); }

/* Logo uploader */

.logo-field { display: flex; gap: 14px; align-items: flex-start; }

.logo-preview {
  flex: 0 0 84px;
  height: 84px;
  display: grid;
  place-items: center;
  border: 1px dashed var(--border-strong);
  border-radius: var(--radius-sm);
  background: var(--surface-2);
  overflow: hidden;
}

.logo-preview[data-empty="true"] span {
  font-size: 11.5px;
  color: var(--text-faint);
}

.logo-preview img {
  max-width: 100%;
  max-height: 100%;
  object-fit: contain;
}

.logo-field__controls {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 7px;
}

/* Line-item editor rows */

.items { display: flex; flex-direction: column; gap: 10px; }

.item-row {
  display: grid;
  grid-template-columns: 1fr 62px 82px 28px;
  gap: 8px;
  align-items: center;
}

.item-row input { padding: 7px 9px; font-size: 13.5px; }

.item-row__remove {
  width: 28px;
  height: 28px;
  padding: 0;
  display: grid;
  place-items: center;
  border: 1px solid transparent;
  border-radius: var(--radius-sm);
  background: transparent;
  color: var(--text-faint);
  font-size: 17px;
  line-height: 1;
  cursor: pointer;
}

.item-row__remove:hover { color: var(--danger); background: var(--surface-2); }

.items-head {
  display: grid;
  grid-template-columns: 1fr 62px 82px 28px;
  gap: 8px;
  font-size: 11.5px;
  font-weight: 600;
  color: var(--text-faint);
  text-transform: uppercase;
  letter-spacing: .04em;
}

.privacy-note {
  flex: 0 0 auto;
  margin: 2px 0 0;
  padding: 11px 14px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  font-size: 12.5px;
  line-height: 1.5;
  color: var(--text-muted);
}

/* ─────────────────────────  Invoice preview  ───────────────────────── */

.preview-pane { min-width: 0; }

.invoice {
  --ink:       #1a1d21;
  --ink-soft:  #5b6470;
  --rule:      #dfe3e8;
  --brand:     #2f6fed;

  max-width: 820px;
  margin: 0 auto;
  padding: 52px 56px 60px;
  background: #fff;
  color: var(--ink);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  font-size: 14px;
}

@media (max-width: 620px) {
  .invoice { padding: 30px 24px 36px; }
}

.invoice__head {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 32px;
  flex-wrap: wrap;
}

.invoice__identity { display: flex; flex-direction: column; gap: 14px; max-width: 55%; }

.invoice__logo { max-width: 190px; max-height: 84px; object-fit: contain; }

.invoice__from-name {
  margin: 0 0 3px;
  font-size: 17px;
  font-weight: 680;
  letter-spacing: -.01em;
}

.invoice__from-address,
.invoice__to-address {
  margin: 0;
  color: var(--ink-soft);
  white-space: pre-line;
  line-height: 1.5;
}

.invoice__meta { text-align: right; }

.invoice__title {
  margin: 0 0 12px;
  font-size: 30px;
  font-weight: 700;
  letter-spacing: .06em;
  color: var(--brand);
}

.invoice__meta-list { margin: 0; display: grid; gap: 4px; }

.invoice__meta-list > div {
  display: grid;
  grid-template-columns: auto auto;
  gap: 14px;
  justify-content: end;
}

.invoice__meta-list dt { color: var(--ink-soft); }

.invoice__meta-list dd { margin: 0; font-weight: 580; font-variant-numeric: tabular-nums; }

.invoice__billto { margin-top: 38px; }

.invoice__label {
  margin: 0 0 6px;
  font-size: 11px;
  font-weight: 650;
  letter-spacing: .09em;
  text-transform: uppercase;
  color: var(--ink-soft);
}

.invoice__to-name { margin: 0 0 3px; font-weight: 640; font-size: 15px; }

/* Items table */

.invoice__table {
  width: 100%;
  margin-top: 34px;
  border-collapse: collapse;
}

.invoice__table th {
  padding: 0 0 9px;
  font-size: 11px;
  font-weight: 650;
  letter-spacing: .07em;
  text-transform: uppercase;
  color: var(--ink-soft);
  text-align: left;
  border-bottom: 1.5px solid var(--rule);
}

.invoice__table td {
  padding: 12px 0;
  border-bottom: 1px solid var(--rule);
  vertical-align: top;
}

.col-num { text-align: right; width: 110px; }

.invoice__table td.col-num { font-variant-numeric: tabular-nums; }

.item-desc { white-space: pre-line; }

.item-empty { color: var(--ink-soft); font-style: italic; }

/* Unfilled preview fields read as hints, not as content. */
.is-placeholder { color: #a8b0ba; }

/* Totals */

.invoice__summary { display: flex; justify-content: flex-end; margin-top: 22px; }

.totals { margin: 0; width: min(310px, 100%); display: grid; gap: 7px; }

.totals__row {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 24px;
}

.totals__row dt { color: var(--ink-soft); }

.totals__row dd { margin: 0; font-variant-numeric: tabular-nums; font-weight: 550; }

.totals__row--grand {
  margin-top: 6px;
  padding-top: 11px;
  border-top: 1.5px solid var(--rule);
  font-size: 17px;
}

.totals__row--grand dt { color: var(--ink); font-weight: 650; }
.totals__row--grand dd { font-weight: 700; }

.totals__row--due {
  margin-top: 3px;
  padding-top: 9px;
  border-top: 1px solid var(--rule);
}

.totals__row--due dt { color: var(--ink); font-weight: 620; }
.totals__row--due dd { color: var(--brand); font-weight: 700; }

/* Footer notes */

.invoice__foot {
  display: grid;
  gap: 22px;
  margin-top: 44px;
  padding-top: 24px;
  border-top: 1px solid var(--rule);
}

.invoice__note p {
  margin: 0;
  white-space: pre-line;
  color: var(--ink-soft);
  line-height: 1.55;
}

.invoice__foot:not(:has(.invoice__note:not([hidden]))) {
  margin-top: 0;
  padding-top: 0;
  border-top: 0;
}

.sitefoot {
  padding: 0 20px 40px;
  text-align: center;
  font-size: 12.5px;
  color: var(--text-faint);
}

/* ─────────────────────────  Print / PDF  ───────────────────────── */

@page { size: A4; margin: 14mm; }

@media print {
  .no-print { display: none !important; }

  body { background: #fff; }

  .layout {
    display: block;
    max-width: none;
    margin: 0;
    padding: 0;
  }

  .invoice {
    max-width: none;
    margin: 0;
    padding: 0;
    border-radius: 0;
    box-shadow: none;
    font-size: 12px;
  }

  .invoice__title { font-size: 26px; }

  /* Keep rows and the totals block from splitting across pages. */
  .invoice__table tr,
  .totals,
  .invoice__note { break-inside: avoid; }

  .invoice__table thead { display: table-header-group; }
}
