:root {
  --bg: #f6f7f9;
  --surface: #ffffff;
  --surface-2: #fbfbfc;
  --border: #e3e6ea;
  --text: #1a1d21;
  --text-dim: #646b74;
  --text-faint: #8b929b;
  --accent: #2f6f4f;
  --accent-soft: #e8f2ec;
  --sales: #2f6f4f;
  --sales-soft: #e8f2ec;
  --purchase: #8a5a2b;
  --purchase-soft: #f6eee3;
  --danger: #a8342c;
  --danger-soft: #fbeceb;
  --radius: 10px;
  --shadow: 0 1px 2px rgba(16, 20, 26, .06), 0 4px 14px rgba(16, 20, 26, .05);
  --mono: ui-monospace, SFMono-Regular, "SF Mono", Menlo, Consolas, monospace;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #14171a;
    --surface: #1c2024;
    --surface-2: #21262b;
    --border: #2f353b;
    --text: #e8eaed;
    --text-dim: #a2abb5;
    --text-faint: #79828c;
    --accent: #6fbf90;
    --accent-soft: #1e2f26;
    --sales: #6fbf90;
    --sales-soft: #1e2f26;
    --purchase: #d3a06a;
    --purchase-soft: #2d251a;
    --danger: #e0796f;
    --danger-soft: #2e1e1c;
    --shadow: 0 1px 2px rgba(0, 0, 0, .3), 0 4px 14px rgba(0, 0, 0, .25);
  }
}

:root[data-theme="dark"] {
  --bg: #14171a; --surface: #1c2024; --surface-2: #21262b; --border: #2f353b;
  --text: #e8eaed; --text-dim: #a2abb5; --text-faint: #79828c;
  --accent: #6fbf90; --accent-soft: #1e2f26;
  --sales: #6fbf90; --sales-soft: #1e2f26;
  --purchase: #d3a06a; --purchase-soft: #2d251a;
  --danger: #e0796f; --danger-soft: #2e1e1c;
  --shadow: 0 1px 2px rgba(0,0,0,.3), 0 4px 14px rgba(0,0,0,.25);
}
:root[data-theme="light"] {
  --bg: #f6f7f9; --surface: #ffffff; --surface-2: #fbfbfc; --border: #e3e6ea;
  --text: #1a1d21; --text-dim: #646b74; --text-faint: #8b929b;
  --accent: #2f6f4f; --accent-soft: #e8f2ec;
  --sales: #2f6f4f; --sales-soft: #e8f2ec;
  --purchase: #8a5a2b; --purchase-soft: #f6eee3;
  --danger: #a8342c; --danger-soft: #fbeceb;
  --shadow: 0 1px 2px rgba(16,20,26,.06), 0 4px 14px rgba(16,20,26,.05);
}

* { box-sizing: border-box; }

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;
}

a { color: inherit; text-decoration: none; }

/* ------------------------------------------------------------ top bar */
.topbar {
  position: sticky; top: 0; z-index: 20;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}
.topbar-inner {
  max-width: 1140px; margin: 0 auto; padding: 0 20px;
  display: flex; align-items: center; gap: 18px; min-height: 60px;
  flex-wrap: wrap;
}
.brand { display: flex; align-items: baseline; gap: 9px; margin-right: auto; }
.brand-name { font-weight: 650; letter-spacing: -.01em; font-size: 16px; }
.brand-sub {
  font-size: 11px; text-transform: uppercase; letter-spacing: .09em;
  color: var(--text-faint); font-weight: 600;
}
.nav { display: flex; gap: 4px; }
.nav a {
  padding: 7px 13px; border-radius: 7px; font-size: 14px; font-weight: 500;
  color: var(--text-dim); white-space: nowrap;
}
.nav a:hover { background: var(--surface-2); color: var(--text); }
.nav a.active { background: var(--accent-soft); color: var(--accent); font-weight: 600; }

.signout {
  border: 1px solid var(--border); background: var(--surface);
  color: var(--text-dim); padding: 7px 14px; border-radius: 7px;
  font: inherit; font-size: 14px; cursor: pointer;
}
.signout:hover { border-color: var(--danger); color: var(--danger); background: var(--danger-soft); }

/* ------------------------------------------------------------- layout */
.wrap { max-width: 1140px; margin: 0 auto; padding: 28px 20px 64px; }

.page-head {
  display: flex; align-items: flex-end; gap: 16px;
  flex-wrap: wrap; margin-bottom: 22px;
}
.page-head h1 { margin: 0; font-size: 23px; font-weight: 650; letter-spacing: -.02em; }
.page-head p { margin: 3px 0 0; color: var(--text-dim); font-size: 14px; }
.page-head .spacer { margin-left: auto; }

/* --------------------------------------------------------- year filter */
.yearfilter { display: flex; gap: 5px; align-items: center; flex-wrap: wrap; }
.yearfilter a {
  padding: 5px 11px; border-radius: 999px; font-size: 13px; font-weight: 500;
  border: 1px solid var(--border); color: var(--text-dim); background: var(--surface);
}
.yearfilter a.active { background: var(--accent); border-color: var(--accent); color: #fff; }
@media (prefers-color-scheme: dark) {
  .yearfilter a.active { color: #14171a; }
}
:root[data-theme="dark"] .yearfilter a.active { color: #14171a; }
:root[data-theme="light"] .yearfilter a.active { color: #fff; }

/* --------------------------------------------------------------- cards */
.stats { display: grid; gap: 14px; grid-template-columns: repeat(4, 1fr); margin-bottom: 26px; }
.stat {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 16px 18px; box-shadow: var(--shadow);
}
.stat-label {
  font-size: 11px; text-transform: uppercase; letter-spacing: .07em;
  color: var(--text-faint); font-weight: 650; margin-bottom: 7px;
}
.stat-value { font-size: 24px; font-weight: 640; letter-spacing: -.02em; font-variant-numeric: tabular-nums; }
.stat-value .cur { font-size: 15px; color: var(--text-faint); font-weight: 500; margin-right: 2px; }
.stat-meta { font-size: 12.5px; color: var(--text-dim); margin-top: 5px; }
.stat.is-sales .stat-value { color: var(--sales); }
.stat.is-purchase .stat-value { color: var(--purchase); }
.stat.is-neg .stat-value { color: var(--danger); }

.panel {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); box-shadow: var(--shadow); overflow: hidden;
  margin-bottom: 22px;
}
.panel-head {
  padding: 14px 18px; border-bottom: 1px solid var(--border);
  display: flex; align-items: center; gap: 12px; background: var(--surface-2);
}
.panel-head h2 { margin: 0; font-size: 14.5px; font-weight: 650; }
.panel-head .count { font-size: 12.5px; color: var(--text-faint); }
.panel-head .spacer { margin-left: auto; }
.panel-body { padding: 18px; }

/* -------------------------------------------------------------- tables */
.table-scroll { overflow-x: auto; -webkit-overflow-scrolling: touch; }
table.data { width: 100%; border-collapse: collapse; font-size: 14px; }
table.data th {
  text-align: left; font-size: 11px; text-transform: uppercase;
  letter-spacing: .06em; color: var(--text-faint); font-weight: 650;
  padding: 10px 14px; border-bottom: 1px solid var(--border); white-space: nowrap;
}
table.data td { padding: 12px 14px; border-bottom: 1px solid var(--border); vertical-align: middle; }
table.data tbody tr:last-child td { border-bottom: none; }
table.data tbody tr:hover { background: var(--surface-2); }
table.data tfoot td {
  padding: 12px 14px; font-weight: 650; border-top: 2px solid var(--border);
  background: var(--surface-2);
}
.num { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
.docref { font-weight: 600; }
.docref .sub { display: block; font-size: 12px; color: var(--text-faint); font-weight: 400; margin-top: 1px; }
.party { font-weight: 500; }

/* --------------------------------------------------------------- pills */
.pill {
  display: inline-block; padding: 2.5px 9px; border-radius: 999px;
  font-size: 11.5px; font-weight: 600; letter-spacing: .01em; white-space: nowrap;
}
.pill-posted { background: var(--sales-soft); color: var(--sales); }
.pill-draft { background: var(--surface-2); color: var(--text-dim); border: 1px solid var(--border); }
.pill-paid { background: var(--sales-soft); color: var(--sales); }
.pill-unpaid { background: var(--danger-soft); color: var(--danger); }
.pill-neutral { background: var(--surface-2); color: var(--text-dim); border: 1px solid var(--border); }
.pill-credit { background: var(--purchase-soft); color: var(--purchase); }

/* --------------------------------------------------------------- empty */
.empty { text-align: center; padding: 46px 20px; color: var(--text-dim); }
.empty .big { font-size: 15px; font-weight: 600; color: var(--text); margin-bottom: 5px; }
.empty p { margin: 0; font-size: 14px; }

/* ---------------------------------------------------------------- note */
.note {
  display: flex; gap: 11px; align-items: flex-start;
  background: var(--accent-soft); border: 1px solid transparent;
  border-radius: var(--radius); padding: 13px 15px; margin-bottom: 22px;
  font-size: 13.5px; color: var(--text);
}
.note strong { font-weight: 650; }
.note .icon { flex: none; font-size: 15px; line-height: 1.35; }

/* -------------------------------------------------------------- detail */
.doc-grid { display: grid; grid-template-columns: 2fr 1fr; gap: 22px; align-items: start; }
.kv { display: grid; grid-template-columns: auto 1fr; gap: 9px 18px; font-size: 14px; }
.kv dt { color: var(--text-faint); font-size: 12.5px; padding-top: 1px; }
.kv dd { margin: 0; font-weight: 500; }
.addr { font-style: normal; line-height: 1.65; font-size: 14px; }
.addr .nm { font-weight: 620; }
.vat { font-family: var(--mono); font-size: 13px; color: var(--text-dim); }

.attach {
  display: flex; align-items: center; gap: 11px; padding: 11px 13px;
  border: 1px solid var(--border); border-radius: 8px; margin-bottom: 8px;
  background: var(--surface-2);
}
.attach:hover { border-color: var(--accent); }
.attach .ic { font-size: 18px; }
.attach .nm { font-weight: 550; font-size: 13.5px; word-break: break-all; }
.attach .sz { font-size: 12px; color: var(--text-faint); }

.back { display: inline-flex; align-items: center; gap: 6px; color: var(--text-dim); font-size: 14px; margin-bottom: 14px; }
.back:hover { color: var(--accent); }

.totals { margin-left: auto; width: min(320px, 100%); }
.totals .row { display: flex; justify-content: space-between; padding: 7px 0; font-size: 14px; }
.totals .row.grand {
  border-top: 2px solid var(--border); margin-top: 6px; padding-top: 11px;
  font-size: 17px; font-weight: 680;
}
.totals .row span:last-child { font-variant-numeric: tabular-nums; }

/* --------------------------------------------------------------- login */
.login-wrap {
  min-height: 100vh; display: flex; align-items: center; justify-content: center;
  padding: 24px;
}
.login-card {
  width: 100%; max-width: 380px; background: var(--surface);
  border: 1px solid var(--border); border-radius: 14px; padding: 34px 30px;
  box-shadow: var(--shadow);
}
.login-card h1 { margin: 0 0 4px; font-size: 21px; font-weight: 660; letter-spacing: -.02em; }
.login-card .lead { margin: 0 0 24px; color: var(--text-dim); font-size: 14px; }
.field { margin-bottom: 15px; }
.field label {
  display: block; font-size: 12px; font-weight: 620; color: var(--text-dim);
  margin-bottom: 6px; text-transform: uppercase; letter-spacing: .05em;
}
.field input {
  width: 100%; padding: 11px 13px; font: inherit; font-size: 15px;
  border: 1px solid var(--border); border-radius: 8px;
  background: var(--surface-2); color: var(--text);
}
.field input:focus {
  outline: none; border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-soft);
}
.btn {
  width: 100%; padding: 11px 16px; font: inherit; font-size: 15px; font-weight: 600;
  color: #fff; background: var(--accent); border: none; border-radius: 8px;
  cursor: pointer; margin-top: 6px;
}
.btn:hover { filter: brightness(1.07); }
.alert {
  background: var(--danger-soft); color: var(--danger); padding: 10px 13px;
  border-radius: 8px; font-size: 13.5px; margin-bottom: 16px; font-weight: 500;
}
.login-foot { margin-top: 20px; text-align: center; font-size: 12px; color: var(--text-faint); }

/* -------------------------------------------------------------- footer */
.foot {
  max-width: 1140px; margin: 0 auto; padding: 0 20px 40px;
  color: var(--text-faint); font-size: 12.5px; text-align: center;
}

/* --------------------------------------------------------- responsive */
@media (max-width: 900px) {
  .stats { grid-template-columns: repeat(2, 1fr); }
  .doc-grid { grid-template-columns: 1fr; }
}

@media (max-width: 640px) {
  .topbar-inner { min-height: 54px; padding: 8px 15px; gap: 10px 12px; }
  /* Row 1: brand on the left, sign out pinned top-right.
     Row 2: the nav, full width underneath. */
  .brand { order: 1; flex: 1 1 auto; min-width: 0; margin-right: 0; }
  .signout { order: 2; flex: none; margin-left: auto; }
  .nav { order: 3; width: 100%; }
  .nav a { flex: 1; text-align: center; padding: 8px 6px; }
  .wrap { padding: 20px 15px 48px; }
  .stats { grid-template-columns: 1fr 1fr; gap: 10px; }
  .stat { padding: 13px 14px; }
  .stat-value { font-size: 20px; }
  .page-head h1 { font-size: 20px; }

  /* Tables become stacked cards - no horizontal scrolling on a phone */
  table.data, table.data tbody, table.data tr, table.data td { display: block; width: 100%; }
  table.data thead, table.data tfoot { display: none; }
  table.data tr {
    border: 1px solid var(--border); border-radius: 9px;
    margin-bottom: 10px; padding: 4px 0; background: var(--surface);
  }
  table.data td { border: none; padding: 6px 14px; display: flex; justify-content: space-between; gap: 14px; }
  table.data td::before {
    content: attr(data-label); font-size: 11.5px; text-transform: uppercase;
    letter-spacing: .05em; color: var(--text-faint); font-weight: 650;
    flex: none; padding-top: 2px;
  }
  table.data td.num { text-align: right; }
  .mobile-total {
    display: flex; justify-content: space-between; padding: 13px 15px;
    background: var(--surface-2); border-top: 1px solid var(--border);
    font-weight: 660; font-size: 15px;
  }
}

@media (min-width: 641px) { .mobile-total { display: none; } }

/* ---------------------------------------------------------------- chart */
/* Series colours: validated categorical slots 1-3 (blue / orange / aqua).
   Light aqua sits at 2.82:1 on white, so direct labels + legend are required. */
.viz {
  --series-sales:    #2a78d6;
  --series-purchase: #eb6834;
  --series-result:   #1baf7a;
  --viz-grid:        #e1e0d9;
  --viz-axis:        #c3c2b7;
  --viz-muted:       #898781;
}
@media (prefers-color-scheme: dark) {
  :root:where(:not([data-theme="light"])) .viz {
    --series-sales:    #3987e5;
    --series-purchase: #d95926;
    --series-result:   #199e70;
    --viz-grid:        #2c2c2a;
    --viz-axis:        #383835;
    --viz-muted:       #898781;
  }
}
:root[data-theme="dark"] .viz {
  --series-sales:    #3987e5;
  --series-purchase: #d95926;
  --series-result:   #199e70;
  --viz-grid:        #2c2c2a;
  --viz-axis:        #383835;
}

.viz svg { display: block; width: 100%; height: auto; }
.viz .grid line { stroke: var(--viz-grid); stroke-width: 1; }
.viz .zero { stroke: var(--viz-axis); stroke-width: 1.5; }
.viz .axis-text { fill: var(--viz-muted); font-size: 11px; font-variant-numeric: tabular-nums; }
.viz .serie { fill: none; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.viz .dot { stroke: var(--surface); stroke-width: 2; }   /* 2px surface ring */
/* A label sitting on the zero baseline would read as struck through, so each one
   is drawn twice: a surface-coloured halo first, then the label over it. Done with
   two elements rather than `paint-order`, which some renderers ignore - and when
   they ignore it the stroke covers the text entirely. */
.viz .endlabel-halo {
  font-size: 11.5px; font-weight: 650;
  fill: none; stroke: var(--surface); stroke-width: 3.5px; stroke-linejoin: round;
}
.viz .endlabel { font-size: 11.5px; font-weight: 650; }

.legend { display: flex; gap: 16px; flex-wrap: wrap; padding: 0 18px 16px; }
.legend .item { display: inline-flex; align-items: center; gap: 7px; font-size: 13px; color: var(--text-dim); }
.legend .swatch { width: 11px; height: 11px; border-radius: 3px; flex: none; }

.viz-note { padding: 0 18px 16px; font-size: 12.5px; color: var(--text-faint); }

@media (max-width: 640px) {
  .viz .axis-text { font-size: 10px; }
  .viz .endlabel { display: none; }   /* too cramped; legend carries identity */
  .legend { padding: 0 15px 14px; gap: 12px; }
}

@media print {
  .topbar, .signout, .yearfilter, .back, .note { display: none !important; }
  body { background: #fff; }
  .panel, .stat { box-shadow: none; border-color: #ccc; }
}
