/* Tagih — styles
 *
 * Design rules (house law):
 * - Elements are flat, single-colour fills. No borders, no shadows, no
 *   inset highlights anywhere. The only lines allowed are the separators
 *   between rows inside a panel.
 * - Amber is a fill colour only (primary actions, brand mark), never a
 *   line, edge, or frame.
 * - The header floats over the content on a gradient scrim; real spacer
 *   elements (not padding) hold the space it occupies.
 * - Light and dark themes share the same surface variables.
 */

:root {
  --bg: #f4f1ea;
  --surface: #fffefa;
  --surface-2: #eae4d3;
  --surface-3: #ded5bf;
  --text: #211d14;
  --muted: #6e6757;
  --accent: #f0a202;
  --on-accent: #2a1e00;
  --danger: #b3261e;
  --divider: rgb(33 29 20 / 0.09);
  --chip-draft-bg: #e4ddcb;   --chip-draft-fg: #57503c;
  --chip-sent-bg: #cfdff5;    --chip-sent-fg: #17406e;
  --chip-paid-bg: #cdeeda;    --chip-paid-fg: #14532d;
  --chip-overdue-bg: #f8d3cc; --chip-overdue-fg: #7c1d12;
  --radius: 16px;
  color-scheme: light;
}

:root[data-theme="dark"] {
  --bg: #171410;
  --surface: #23201a;
  --surface-2: #322d24;
  --surface-3: #443d2f;
  --text: #f3eee0;
  --muted: #aaa18e;
  --accent: #f0a202;
  --on-accent: #2a1e00;
  --danger: #ff9d94;
  --divider: rgb(243 238 224 / 0.10);
  --chip-draft-bg: #3b362a;   --chip-draft-fg: #ddd6c2;
  --chip-sent-bg: #1e3a5c;    --chip-sent-fg: #bcd6f8;
  --chip-paid-bg: #1e4530;    --chip-paid-fg: #b9ecca;
  --chip-overdue-bg: #5c271e; --chip-overdue-fg: #ffcabd;
  color-scheme: dark;
}

* { box-sizing: border-box; }

html { -webkit-text-size-adjust: 100%; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: ui-rounded, "Avenir Next", "Segoe UI", system-ui, -apple-system, sans-serif;
  font-size: 1rem;
  line-height: 1.5;
}

.visually-hidden {
  position: absolute;
  clip-path: inset(50%);
  overflow: hidden;
  width: 1px;
  height: 1px;
  margin: -1px;
  padding: 0;
  border: 0;
  white-space: nowrap;
}

:focus-visible {
  outline: 3px solid var(--text);
  outline-offset: 2px;
  border-radius: 6px;
}

/* ---------- floating header ---------- */

.scrim-top {
  position: fixed;
  inset: 0 0 auto 0;
  /* Exactly the header zone: the fade must finish before page content
     starts, so it never washes over buttons or cards below it. */
  height: 4.8rem;
  background: linear-gradient(to bottom, var(--bg) 55%, transparent);
  pointer-events: none;
  z-index: 10;
}

.app-header {
  position: fixed;
  inset: 0 0 auto 0;
  z-index: 11;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  max-width: 62rem;
  margin-inline: auto;
  padding: 0.9rem 1.25rem 0;
}

.brand {
  margin: 0;
  font-size: 1.45rem;
  font-weight: 800;
  letter-spacing: -0.02em;
  display: flex;
  align-items: center;
  gap: 0.55rem;
}

.brand::before {
  content: "";
  width: 0.8rem;
  height: 0.8rem;
  border-radius: 50%;
  background: var(--accent);
}

.header-spacer { height: 4.6rem; }

.theme-picker select {
  font: inherit;
  font-size: 0.9rem;
  color: var(--text);
  background: var(--surface);
  border: 0;
  border-radius: 999px;
  padding: 0.55rem 0.9rem;
  min-height: 2.75rem;
  cursor: pointer;
}

/* ---------- layout ---------- */

main {
  max-width: 62rem;
  margin-inline: auto;
  padding: 0 1.25rem 4rem;
}

h1 {
  font-size: 1.9rem;
  letter-spacing: -0.02em;
  margin: 0.4rem 0 1.1rem;
}

h1:focus { outline: none; }

/* ---------- buttons ---------- */

.btn {
  font: inherit;
  font-weight: 650;
  appearance: none;
  -webkit-appearance: none;
  border: 0;
  border-radius: 999px;
  padding: 0.7rem 1.3rem;
  min-height: 3rem;
  cursor: pointer;
  background: var(--surface-2);
  color: var(--text);
  display: inline-flex;
  align-items: center;
  gap: 0.45rem;
  text-decoration: none;
}

.btn:hover { background: var(--surface-3); }

.btn-primary { background: var(--accent); color: var(--on-accent); }
.btn-primary:hover { background: var(--accent); filter: brightness(1.07); }

.btn-quiet { background: transparent; color: var(--muted); }
.btn-quiet:hover { background: var(--surface-2); color: var(--text); }

.btn-danger { background: transparent; color: var(--danger); }
.btn-danger:hover { background: var(--chip-overdue-bg); }

.btn-back { background: var(--surface); padding-inline: 1rem 1.2rem; }
.btn-back:hover { background: var(--surface-2); }

.btn-small { min-height: 2.6rem; padding: 0.45rem 1rem; font-size: 0.92rem; }

/* ---------- summary cards ---------- */

.summary-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 0.9rem;
  margin-bottom: 1.6rem;
}

.summary-card {
  background: var(--surface);
  border-radius: var(--radius);
  padding: 1.1rem 1.2rem;
}

.summary-card .card-label {
  margin: 0 0 0.35rem;
  color: var(--muted);
  font-size: 0.9rem;
  font-weight: 600;
}

.summary-card .card-amount {
  margin: 0;
  font-size: 1.35rem;
  font-weight: 800;
  letter-spacing: -0.01em;
  overflow-wrap: anywhere;
}

.summary-card .card-sub {
  margin: 0.15rem 0 0;
  color: var(--muted);
  font-size: 0.9rem;
}

/* ---------- search and filter ---------- */

#filter-form {
  display: flex;
  flex-wrap: wrap;
  gap: 0.9rem 1.4rem;
  align-items: end;
  margin-bottom: 1.1rem;
}

.field label {
  display: block;
  font-weight: 650;
  margin-bottom: 0.35rem;
}

.field input,
.field select,
.field textarea {
  font: inherit;
  font-size: max(1rem, 1em);
  color: var(--text);
  background: var(--surface);
  border: 0;
  border-radius: 12px;
  padding: 0.75rem 0.95rem;
  min-height: 3rem;
  width: 100%;
}

.search-field { flex: 1 1 15rem; }

.status-filter { border: 0; margin: 0; padding: 0; }
.status-filter legend { font-weight: 650; margin-bottom: 0.35rem; padding: 0; }

.chip-row { display: flex; flex-wrap: wrap; gap: 0.45rem; }

.filter-chip { position: relative; cursor: pointer; }
.filter-chip input {
  position: absolute;
  inset: 0;
  opacity: 0;
  margin: 0;
  cursor: pointer;
}
.filter-chip span {
  display: inline-flex;
  align-items: center;
  min-height: 2.6rem;
  padding: 0.45rem 1rem;
  border-radius: 999px;
  background: var(--surface);
  color: var(--muted);
  font-weight: 600;
}
.filter-chip input:checked + span { background: var(--text); color: var(--bg); }
.filter-chip input:focus-visible + span { outline: 3px solid var(--text); outline-offset: 2px; }

/* ---------- invoice list ---------- */

.panel {
  background: var(--surface);
  border-radius: var(--radius);
  padding: 0.4rem 1.2rem;
}

.invoice-list { list-style: none; margin: 0; padding: 0; }

.invoice-row {
  display: flex;
  align-items: center;
  gap: 0.9rem;
  padding: 0.95rem 0;
}

.invoice-row + .invoice-row { border-top: 1px solid var(--divider); }

.row-open {
  flex: 1 1 auto;
  min-width: 0;
  display: flex;
  align-items: center;
  gap: 0.9rem;
  background: none;
  border: 0;
  padding: 0.2rem 0;
  font: inherit;
  color: inherit;
  text-align: start;
  cursor: pointer;
}

.row-main { min-width: 0; }
.row-client {
  display: block;
  font-weight: 700;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.row-meta { display: block; color: var(--muted); font-size: 0.9rem; }

.row-side { text-align: end; flex: none; }
.row-amount { display: block; font-weight: 800; }
.row-due { display: block; color: var(--muted); font-size: 0.88rem; }
.row-due.is-overdue { color: var(--danger); font-weight: 650; }

.row-action { flex: none; }

.chip {
  display: inline-flex;
  align-items: center;
  border-radius: 999px;
  padding: 0.28rem 0.75rem;
  font-size: 0.83rem;
  font-weight: 700;
  flex: none;
}
.chip-draft   { background: var(--chip-draft-bg);   color: var(--chip-draft-fg); }
.chip-sent    { background: var(--chip-sent-bg);    color: var(--chip-sent-fg); }
.chip-paid    { background: var(--chip-paid-bg);    color: var(--chip-paid-fg); }
.chip-overdue { background: var(--chip-overdue-bg); color: var(--chip-overdue-fg); }

.empty-state { color: var(--muted); padding: 1.6rem 0.2rem; text-align: center; }

.dashboard-footer { margin-top: 1.4rem; text-align: end; }

/* ---------- detail view ---------- */

.view-nav { margin: 0.2rem 0 0.9rem; }

.doc-card {
  background: var(--surface);
  border-radius: var(--radius);
  padding: 1.5rem 1.6rem;
}

.doc-head {
  display: flex;
  flex-wrap: wrap;
  align-items: start;
  justify-content: space-between;
  gap: 0.8rem;
  margin-bottom: 1.2rem;
}

.doc-head .doc-number { margin: 0; font-size: 1.3rem; font-weight: 800; }
.doc-head .doc-client { margin: 0.15rem 0 0; color: var(--muted); }

.doc-meta {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(9.5rem, 1fr));
  gap: 0.9rem 1.4rem;
  margin: 0 0 1.4rem;
}
.doc-meta dt { color: var(--muted); font-size: 0.88rem; font-weight: 600; }
.doc-meta dd { margin: 0.1rem 0 0; font-weight: 650; }

.items-table { width: 100%; border-collapse: collapse; }
.items-table th, .items-table td { padding: 0.6rem 0.4rem; text-align: start; vertical-align: top; }
.items-table thead th { color: var(--muted); font-size: 0.88rem; font-weight: 650; }
.items-table tbody tr + tr td { border-top: 1px solid var(--divider); }
.items-table .num { text-align: end; white-space: nowrap; }

.totals {
  margin: 1.2rem 0 0 auto;
  max-width: 21rem;
  width: 100%;
}
.totals .t-row {
  display: flex;
  justify-content: space-between;
  gap: 1rem;
  padding: 0.35rem 0.4rem;
}
.totals .t-row + .t-row { border-top: 1px solid var(--divider); }
.totals .t-grand { font-size: 1.12rem; font-weight: 800; }
.totals dt { color: var(--muted); }
.totals .t-grand dt { color: var(--text); }
.totals dd { margin: 0; font-weight: 650; white-space: nowrap; }

.doc-notes { margin-top: 1.4rem; }
.doc-notes h2 { font-size: 1rem; margin: 0 0 0.3rem; }
.doc-notes p { margin: 0; color: var(--muted); white-space: pre-wrap; }

.doc-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 0.6rem;
  margin-top: 1.5rem;
}
.doc-actions .spacer { flex: 1; }

/* ---------- small screens ---------- */

@media (max-width: 44rem) {
  .summary-grid { grid-template-columns: 1fr; }
  .invoice-row { flex-wrap: wrap; }
  .row-action { margin-inline-start: auto; }
  .doc-card { padding: 1.2rem 1.1rem; }
  main { padding-inline: 0.9rem; }
  /* Keep the items table inside the card: tighter cells, numbers may wrap. */
  .items-table { font-size: 0.92rem; }
  .items-table th, .items-table td { padding: 0.5rem 0.25rem; }
  .items-table th { white-space: nowrap; }
  .items-table td.num { white-space: normal; overflow-wrap: anywhere; }
  .field-row { grid-template-columns: repeat(auto-fit, minmax(11.5rem, 1fr)); }
}

/* ---------- print document (screen) ---------- */

#print-doc { display: none; }

/* ---------- title row ---------- */

.title-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  flex-wrap: wrap;
}
.title-row h1 { margin-bottom: 1.1rem; }

/* ---------- editor form ---------- */

.editor-form { max-width: 48rem; }

.form-panel {
  background: var(--surface);
  border: 0;
  border-radius: var(--radius);
  padding: 1.3rem 1.4rem 1.5rem;
  margin: 0 0 1.1rem;
  min-width: 0;
}

/* Inputs inside a panel take the deeper fill so they stay visible against
   the panel surface (flat colour only, no borders). Item rows sit on the
   page background instead, so their inputs keep the panel colour. */
.form-panel input,
.form-panel select,
.form-panel textarea { background: var(--surface-2); }
.form-panel .item-row input { background: var(--surface); }

.form-panel legend {
  font-size: 1.05rem;
  font-weight: 750;
  padding: 0;
  float: left;
  width: 100%;
  margin-bottom: 0.9rem;
}
/* Clear the floated legend for the panel content. */
.form-panel legend + * { clear: both; }

.form-panel .field { margin-bottom: 1rem; }
.form-panel .field:last-child { margin-bottom: 0; }

.field-row {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(10rem, 1fr));
  gap: 1rem;
}

.field .hint { color: var(--muted); font-size: 0.88rem; margin: 0.3rem 0 0; }

.field-error {
  color: var(--danger);
  font-weight: 600;
  font-size: 0.92rem;
  margin: 0.35rem 0 0;
}

.field input[aria-invalid="true"],
.field select[aria-invalid="true"],
.field textarea[aria-invalid="true"] {
  background: var(--chip-overdue-bg);
}

.error-summary {
  background: var(--chip-overdue-bg);
  color: var(--chip-overdue-fg);
  border-radius: var(--radius);
  padding: 1.1rem 1.3rem;
  margin-bottom: 1.1rem;
}
.error-summary h2 { margin: 0 0 0.4rem; font-size: 1.02rem; }
.error-summary ul { margin: 0; padding-inline-start: 1.2rem; }
.error-summary a { color: inherit; font-weight: 650; }

.item-rows { display: flex; flex-direction: column; gap: 0.8rem; }

.item-row {
  display: grid;
  grid-template-columns: 1fr 5.2rem 8.5rem 7.5rem 3rem;
  gap: 0.6rem;
  align-items: end;
  background: var(--bg);
  border-radius: 12px;
  padding: 0.8rem;
}

.item-row .field { margin: 0; }
.item-row label { font-size: 0.88rem; }
.item-row input { background: var(--surface); }

.item-row .line-total {
  font-weight: 750;
  padding-bottom: 0.85rem;
  text-align: end;
  overflow-wrap: anywhere;
}

.item-remove {
  align-self: end;
  appearance: none;
  -webkit-appearance: none;
  width: 3rem;
  height: 3rem;
  border: 0;
  border-radius: 12px;
  background: var(--surface);
  color: var(--danger);
  font-size: 1.25rem;
  line-height: 1;
  cursor: pointer;
}
.item-remove:hover { background: var(--chip-overdue-bg); }

.add-item { margin-top: 0.9rem; }

.totals-preview {
  background: var(--surface);
  border-radius: var(--radius);
  padding: 1.1rem 1.4rem;
  margin-bottom: 1.1rem;
}
.totals-preview h2 { margin: 0 0 0.4rem; font-size: 1.05rem; }
.totals-preview .totals { margin: 0; max-width: none; }

.form-actions { display: flex; gap: 0.6rem; flex-wrap: wrap; }

@media (max-width: 44rem) {
  .item-row { grid-template-columns: 1fr 1fr; }
  .item-row .item-desc { grid-column: 1 / -1; }
  .item-row .line-total { text-align: start; padding-bottom: 0.2rem; }
}

/* ---------- confirmation dialog ---------- */

#confirm-dialog {
  border: 1px solid var(--divider);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--text);
  padding: 1.4rem;
  max-width: min(26rem, calc(100vw - 2rem));
  box-shadow: 0 1.2rem 3rem rgb(0 0 0 / 0.28);
}
#confirm-dialog::backdrop { background: rgb(0 0 0 / 0.45); }
#confirm-dialog p { margin: 0 0 1.2rem; line-height: 1.5; }
.dialog-actions { display: flex; gap: 0.6rem; justify-content: flex-end; }

/* ---------- print ---------- */

@media print {
  .scrim-top, .app-header, .header-spacer { display: none !important; }
  body { background: #fff; }

  body.printing-invoice main { display: none; }
  body.printing-invoice #print-doc {
    display: block;
    color: #000;
    font-size: 11pt;
    max-width: 44rem;
    margin: 0 auto;
  }

  #print-doc .p-brand { font-weight: 800; font-size: 13pt; margin: 0 0 1.2rem; }
  #print-doc .p-title { font-size: 20pt; margin: 0 0 0.4rem; }
  #print-doc .p-billed { margin: 0 0 1.2rem; }
  #print-doc .p-meta {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 0.6rem 1.5rem;
    margin: 0 0 1.4rem;
  }
  #print-doc .p-meta dt { color: #555; font-size: 9.5pt; }
  #print-doc .p-meta dd { margin: 0.1rem 0 0; font-weight: 650; }
  #print-doc .p-table { width: 100%; border-collapse: collapse; }
  #print-doc .p-table th, #print-doc .p-table td {
    padding: 0.45rem 0.3rem;
    text-align: start;
    border-bottom: 1px solid #ccc;
    vertical-align: top;
  }
  #print-doc .p-table thead th { color: #555; font-size: 9.5pt; }
  #print-doc .p-table .num { text-align: end; white-space: nowrap; }
  #print-doc .totals { margin-top: 1rem; }
  #print-doc .totals dt { color: #333; }
  #print-doc .p-notes { margin-top: 1.4rem; }
  #print-doc .p-notes h2 { font-size: 11pt; margin: 0 0 0.3rem; }
  #print-doc .p-notes p { margin: 0; white-space: pre-wrap; }
  #print-doc .p-footer { margin-top: 2rem; color: #777; font-size: 9pt; }
}
