:root {
  color-scheme: light;
  --surface-0: #eaf0f8;
  --surface-1: #ffffff;
  --surface-2: #e2eaf5;
  --border: #d0dcea;
  --border-strong: #a9bdd4;
  --text-primary: #0c1a2b;
  --text-secondary: #42546b;
  --text-muted: #6a7d94;
  --accent: #1e6fd9;
  --accent-ink: #fff;
  --grid: #dbe4f0;

  /* Shared column geometry. Every report table (cost pivot, cost type, revenue)
     uses the same three widths, so the column for a given month sits at the same
     x-position in all of them. Change them here, not per table. */
  --col-label: 330px;
  --col-total: 190px;
  --col-month-min: 175px;

  --series-1: #2a78d6;
  --series-2: #eb6834;
  --series-3: #1baf7a;
  --series-4: #eda100;
  --series-5: #e87ba4;
  --series-6: #008300;
  --series-7: #4a3aa7;
  --series-8: #e34948;
  --series-other: #91a2b8;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --surface-0: #0a1120;
    --surface-1: #111c2e;
    --surface-2: #1a2739;
    --border: #26344a;
    --border-strong: #3b4d66;
    --text-primary: #eef4fc;
    --text-secondary: #b2c2d7;
    --text-muted: #8496ad;
    --accent: #4f95f2;
    --accent-ink: #06121f;
    --grid: #1f2c40;

    --series-1: #3987e5;
    --series-2: #d95926;
    --series-3: #199e70;
    --series-4: #c98500;
    --series-5: #d55181;
    --series-6: #008300;
    --series-7: #9085e9;
    --series-8: #e66767;
    --series-other: #74869e;
  }
}

:root[data-theme="dark"] {
  color-scheme: dark;
  --surface-0: #0a1120;
  --surface-1: #111c2e;
  --surface-2: #1a2739;
  --border: #26344a;
  --border-strong: #3b4d66;
  --text-primary: #eef4fc;
  --text-secondary: #b2c2d7;
  --text-muted: #8496ad;
  --accent: #4f95f2;
  --accent-ink: #06121f;
  --grid: #1f2c40;

  --series-1: #3987e5;
  --series-2: #d95926;
  --series-3: #199e70;
  --series-4: #c98500;
  --series-5: #d55181;
  --series-6: #008300;
  --series-7: #9085e9;
  --series-8: #e66767;
  --series-other: #74869e;
}

* { box-sizing: border-box; }

/* Author rules like `.loading { display: flex }` outrank the UA's
   `[hidden] { display: none }`, which left the busy indicator permanently on
   screen. Enforce it once here rather than per component. */
[hidden] { display: none !important; }

body {
  margin: 0;
  padding: 0 20px 64px;
  background: var(--surface-0);
  color: var(--text-primary);
  font: 15px/1.5 "IBM Plex Sans Thai", "Noto Sans Thai", "Sarabun", system-ui, -apple-system, "Segoe UI", sans-serif;
}

.topbar {
  display: flex; align-items: flex-start; justify-content: space-between; gap: 16px;
  padding: 22px 0 14px;
}
h1 { margin: 0; font-size: 21px; letter-spacing: -0.01em; }
h2 { margin: 0; font-size: 15px; font-weight: 600; }
h3 { margin: 0; font-size: 16px; }
.sub, .panel-sub, .hint { margin: 4px 0 0; color: var(--text-secondary); font-size: 13px; }
.hint { color: var(--text-muted); }
.muted { color: var(--text-muted); font-weight: 400; }

/* ---------------- filters ---------------- */

.filters {
  display: flex; flex-wrap: wrap; align-items: flex-end; gap: 12px;
  padding: 14px; margin-bottom: 10px;
  background: var(--surface-1);
  border: 1px solid var(--border); border-radius: 0;
}
.field { display: flex; flex-direction: column; gap: 5px; min-width: 150px; }
.field.grow { flex: 1 1 220px; }
.filters > .field.grow,
.advanced-filters > .field.grow { max-width: 420px; }
.advanced-filters > .advanced { flex: 1 1 100%; margin-bottom: 0; }
.field > span { font-size: 12px; color: var(--text-secondary); }
.actions { display: flex; gap: 8px; margin-left: auto; }

input, select, textarea, button {
  font: inherit; color: var(--text-primary);
  background: var(--surface-1);
  border: 1px solid var(--border-strong); border-radius: 0;
  padding: 7px 9px;
}
input:focus-visible, select:focus-visible, textarea:focus-visible, button:focus-visible {
  outline: 2px solid var(--accent); outline-offset: 1px;
}
textarea { width: 100%; font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 12px; resize: vertical; }

button { cursor: pointer; background: var(--accent); color: var(--accent-ink); border-color: transparent; font-weight: 600; padding: 8px 16px; }
button.ghost { background: var(--surface-1); color: var(--text-primary); border-color: var(--border-strong); font-weight: 500; }
button:disabled { opacity: .45; cursor: not-allowed; }

.check { display: flex; align-items: center; gap: 7px; font-size: 13px; color: var(--text-secondary); padding-bottom: 8px; white-space: nowrap; }
.check input { width: 15px; height: 15px; accent-color: var(--accent); padding: 0; }

.itemfilters {
  flex: 1 1 100%;
  display: flex; flex-wrap: wrap; align-items: flex-start; gap: 10px 16px;
  margin: 2px 0 0; padding: 10px 14px 12px;
  border: 1px solid var(--border); border-radius: 0; background: var(--surface-2);
}
.itemfilters legend { padding: 0 6px; font-size: 12px; color: var(--text-secondary); }
.itemfilters[hidden] { display: none; }
.itemfilters .field { flex: 1 1 300px; min-width: 260px; }
.itemfilters .hint { flex: 1 1 100%; margin: 0; }
.itemfilters select[multiple] { padding: 4px; height: 92px; }
.itemfilters select[multiple] option { padding: 2px 5px; border-radius: 0; }

.advanced-filters {
  flex: 1 1 100%;
  display: flex; flex-wrap: wrap; align-items: flex-end; gap: 12px;
  margin-top: 4px; padding-top: 14px;
  border-top: 1px solid var(--border);
}
#advancedBtn .caret { display: inline-block; width: auto; margin-left: 4px; }

.advanced { margin-bottom: 16px; font-size: 13px; color: var(--text-secondary); }
.advanced summary { cursor: pointer; padding: 4px 0; }
.advanced[open] { padding-bottom: 8px; }

/* ---------------- panels & kpis ---------------- */

.panel {
  background: var(--surface-1); border: 1px solid var(--border);
  border-radius: 0; padding: 16px; margin-bottom: 16px;
}
.panel-head { margin-bottom: 14px; }

.kpis { display: grid; grid-template-columns: repeat(auto-fit, minmax(180px, 1fr)); gap: 12px; margin-bottom: 16px; }
.kpi {
  background: var(--surface-1); border: 1px solid var(--border);
  border-radius: 0; padding: 14px 16px;
}
.kpi-label { font-size: 12px; color: var(--text-secondary); }
.kpi-value { font-size: 24px; font-weight: 650; letter-spacing: -0.02em; margin-top: 4px; font-variant-numeric: tabular-nums; }
.kpi-note { font-size: 12px; color: var(--text-muted); margin-top: 2px; }

/* ---------------- tables ---------------- */

.table-tools { display: flex; gap: 12px; align-items: center; margin-bottom: 10px; flex-wrap: wrap; }
.table-tools input[type="search"] { min-width: 240px; }
.table-tools .check { padding-bottom: 0; }
/* The revenue tools carry selects rather than only checkboxes; their labels sit
   above the control, so align on the controls' baseline instead of the centre
   and let the CSV button sit at the far end. */
.table-tools .field { min-width: 130px; }
.table-tools:has(.field) { align-items: flex-end; }
.table-tools > button { margin-left: auto; }
#revenueNote { margin-top: 10px; font-size: 12px; }
.table-scroll { overflow: auto; max-height: 68vh; border: 1px solid var(--border); border-radius: 0; }

/* Cluster view is short enough to show whole — drop the inner scroller so the
   page scrolls instead. The header still sticks (to the viewport now); the
   footer must not, or it would hover over the rows for the table's full height.
   overflow-x stays auto for wide month ranges, and with no max-height the
   vertical scrollbar simply never has cause to appear. */
.table-scroll.fit { max-height: none; }
.table-scroll.fit tfoot { position: static; }
.detail-scroll { max-height: 58vh; }

table { border-collapse: separate; border-spacing: 0; width: 100%; font-size: 13px; }
th, td { padding: 7px 10px; text-align: right; white-space: nowrap; border-bottom: 1px solid var(--border); }
th { position: sticky; top: 0; z-index: 2; background: var(--surface-2); font-weight: 600; color: var(--text-secondary); }
th:first-child, td:first-child { text-align: left; position: sticky; left: 0; z-index: 1; background: var(--surface-1); max-width: 320px; overflow: hidden; text-overflow: ellipsis; }

/* The three report tables share one column geometry so that a month's column
   lands at the same x-position in each of them. `table-layout: fixed` makes the
   <colgroup> widths authoritative instead of letting content decide: the label
   and total columns are pinned, and the month columns divide what is left,
   equally, in every table.

   --months is set from JS per table. Below the min-width the columns would
   squeeze the figures, so the table stops shrinking there and .table-scroll
   takes over horizontally (see syncTableScroll — the panels scroll together,
   or the alignment would only hold at scrollLeft 0). */
.report-table {
  table-layout: fixed;
  min-width: calc(
    var(--col-label) + var(--col-total)
    + var(--months, 3) * var(--col-month-min)
    + var(--budget-cols, 0) * var(--col-total)
  );
}
/* The <col> widths do the constraining now, so drop the auto-layout guard that
   would otherwise fight them. */
.report-table td:first-child { max-width: none; }
.report-table col.col-label { width: var(--col-label); }
.report-table col.col-total,
.report-table col.col-budget { width: var(--col-total); }
th:first-child { z-index: 3; background: var(--surface-2); }
tbody tr:hover td { background: var(--surface-2); }
td.num { font-variant-numeric: tabular-nums; }
td.zero { color: var(--text-muted); }
td.clickable { cursor: pointer; }
td.clickable:hover { text-decoration: underline; text-underline-offset: 2px; }
td.expandable { cursor: pointer; user-select: none; }
td.expandable:hover { color: var(--accent); }
.caret {
  display: inline-block; width: 1.1em; color: var(--text-muted);
  font-size: 11px; vertical-align: 1px;
}
td.expandable:hover .caret { color: var(--accent); }
.child-count { margin-left: 8px; font-size: 11px; color: var(--text-muted); font-variant-numeric: tabular-nums; }

/* ---------------- budget column ---------------- */

th.budget-col, td.budget-col {
  border-left: 2px solid var(--border-strong);
  background: var(--surface-2);
}
tbody tr:hover td.budget-col { background: var(--surface-1); }

/* Closed months keep showing their budget but can no longer be edited. */
th.budget-locked, td.budget-locked { color: var(--text-muted); }
td.budget-locked .budget-val { opacity: .75; }
.budget-val { font-variant-numeric: tabular-nums; }
.budget-unset { color: var(--text-muted); font-weight: 500; }
.budget-input {
  width: 130px; padding: 3px 6px; font-size: 13px; text-align: right;
  font-variant-numeric: tabular-nums;
}

/* Usage bands. The badge always shows the number too, so the colour is a
   second signal rather than the only one. */
.usage {
  display: inline-block; margin-left: 8px;
  padding: 1px 6px; border-radius: 0;
  font-size: 11px; font-weight: 600; font-variant-numeric: tabular-nums;
}
.usage.u-low  { color: #0b5d2e; background: color-mix(in srgb, #0b5d2e 15%, transparent); }
.usage.u-mid  { color: #1e874a; background: color-mix(in srgb, #1e874a 16%, transparent); }
.usage.u-high { color: #a85700; background: color-mix(in srgb, #c77700 20%, transparent); }
.usage.u-over { color: #b3261e; background: color-mix(in srgb, #b3261e 16%, transparent); }

:root[data-theme="dark"] .usage.u-low  { color: #6dd39a; background: color-mix(in srgb, #6dd39a 16%, transparent); }
:root[data-theme="dark"] .usage.u-mid  { color: #4fbf80; background: color-mix(in srgb, #4fbf80 16%, transparent); }
:root[data-theme="dark"] .usage.u-high { color: #e8a33d; background: color-mix(in srgb, #e8a33d 18%, transparent); }
:root[data-theme="dark"] .usage.u-over { color: #f2837c; background: color-mix(in srgb, #f2837c 18%, transparent); }

/* Share of the month's total, as a badge inline after the amount. */
.pct {
  display: inline-block; margin-left: 8px;
  padding: 1px 6px; border-radius: 0;
  font-size: 11px; font-weight: 500; font-variant-numeric: tabular-nums;
  color: var(--accent);
  background: color-mix(in srgb, var(--accent) 13%, transparent);
}
tr.child-row .pct { color: var(--text-secondary); background: color-mix(in srgb, var(--text-secondary) 12%, transparent); }

tr.child-row td { background: var(--surface-2); }
tr.child-row td:first-child { background: var(--surface-2); }
tr.child-row td.num { color: var(--text-secondary); }
/* Deeper tiers recede a little further with each level. */
tr.depth-2 td.num, tr.depth-3 td.num { color: var(--text-muted); }
tr.depth-1 td:first-child { font-weight: 600; }
td.child-label { color: var(--text-secondary); }
td.child-label::before { content: '↳'; margin-right: 6px; color: var(--text-muted); }

tr.total-row td { font-weight: 650; background: var(--surface-2); border-top: 2px solid var(--border-strong); }
tr.total-row td:first-child { background: var(--surface-2); }
tfoot { position: sticky; bottom: 0; }
.count { display: block; font-size: 11px; color: var(--text-muted); font-variant-numeric: tabular-nums; }

/* ---------------- states ---------------- */

.warn {
  margin-bottom: 16px; padding: 12px 14px; border-radius: 0; font-size: 13px;
  border: 1px solid #eda100; color: var(--text-primary);
  background: color-mix(in srgb, #eda100 12%, var(--surface-1));
}
.note {
  margin-bottom: 16px; padding: 12px 14px; border-radius: 0; font-size: 13px;
  border: 1px solid var(--border-strong); color: var(--text-secondary);
  background: var(--surface-2);
}
.warn b, .note b { font-variant-numeric: tabular-nums; color: var(--text-primary); }
.warn code, .note code { font-size: 12px; }

.loading, .empty, .error { padding: 16px; border-radius: 0; border: 1px solid var(--border); background: var(--surface-1); }
/* Pinned, so it stays visible no matter how far down the table you are. */
.loading {
  position: fixed; right: 20px; bottom: 20px; z-index: 60;
  display: flex; align-items: center; gap: 10px;
  color: var(--text-primary); background: var(--surface-1);
  box-shadow: 0 6px 24px rgb(0 0 0 / .18);
  padding: 12px 16px;
}
.empty { color: var(--text-secondary); text-align: center; }
.error { border-color: #e34948; color: #b62a29; background: color-mix(in srgb, #e34948 8%, var(--surface-1)); }
:root[data-theme="dark"] .error, :root:not([data-theme="light"]) .error { color: #e66767; }

.spinner {
  width: 15px; height: 15px; border-radius: 0;
  border: 2px solid var(--border-strong); border-top-color: var(--accent);
  animation: spin .7s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }
@media (prefers-reduced-motion: reduce) { .spinner { animation-duration: 2s; } }

dialog {
  border: 1px solid var(--border-strong); border-radius: 0;
  background: var(--surface-1); color: var(--text-primary);
  padding: 18px; width: min(940px, 94vw); max-width: 94vw;
}
dialog::backdrop { background: rgb(0 0 0 / .45); }
.detail-head { display: flex; align-items: center; justify-content: space-between; gap: 16px; }
dialog { width: min(1280px, 96vw); }

/* Long Thai vendor/project names would otherwise push the cost column out of
   view; truncate them and keep the full text in the tooltip. */
#detailTable td { max-width: 165px; overflow: hidden; text-overflow: ellipsis; }
#detailTable td.wrap {
  white-space: normal; text-align: left;
  min-width: 180px; max-width: 240px; line-height: 1.35;
}
#detailTable td.num, #detailTable th:last-child { max-width: none; min-width: 92px; }
#detailTable tfoot td:first-child { position: static; max-width: none; }

/* ---------------- share line chart ---------------- */

.chart-scroll { overflow-x: auto; }
.viz-root svg { display: block; }
.viz-root text { fill: var(--text-secondary); font-size: 11px; }
.viz-root .grid-line { stroke: var(--grid); stroke-width: 1; }
.viz-root .axis-line { stroke: var(--border-strong); stroke-width: 1; }
.viz-root .crosshair { stroke: var(--border-strong); stroke-width: 1; }
.viz-root .series-line { fill: none; stroke-width: 2; stroke-linejoin: round; stroke-linecap: round; }
/* 2px surface ring keeps markers legible where lines cross */
.viz-root .marker { stroke: var(--surface-1); stroke-width: 2; }

.legend { display: flex; flex-wrap: wrap; gap: 6px 16px; margin-bottom: 12px; }
.legend-item { display: inline-flex; align-items: center; gap: 7px; font-size: 12px; color: var(--text-secondary); }
.legend-key { width: 16px; height: 2px; border-radius: 0; flex: none; }

.tip {
  position: fixed; z-index: 50; pointer-events: none;
  background: var(--surface-1); color: var(--text-primary);
  border: 1px solid var(--border-strong); border-radius: 0;
  padding: 8px 10px; font-size: 12px; line-height: 1.5;
  box-shadow: 0 6px 20px rgb(0 0 0 / .18); max-width: 320px;
}
.tip-head { font-weight: 650; margin-bottom: 4px; }
.tip-row { display: flex; align-items: center; gap: 7px; white-space: nowrap; }
.tip-key { width: 14px; height: 2px; border-radius: 0; flex: none; }
.tip-val { font-weight: 650; font-variant-numeric: tabular-nums; margin-left: auto; padding-left: 12px; }
.tip-name { color: var(--text-secondary); overflow: hidden; text-overflow: ellipsis; max-width: 200px; }
