/* Theme tokens -- same values as ..\Reference\static\css\style.css so the two sibling apps
   look consistent. --good/--bad/--warn are new here (Reference has no conditional-formatting
   concept): they back the BRD's ±7%-of-TimeGone Red/Blue/Green rule (project.md §5, Phase 5). */
:root,
html[data-theme="dark"] {
  --bg: #0f1320;
  --panel: #161b2e;
  --panel2: #1d2338;
  --input-bg: #0f1320;
  --text: #e7e9f3;
  --muted: #9aa3c0;
  --accent: #5fb3ff;
  --accent-rgb: 95, 179, 255;
  --accent-text: #0b1020;
  --border: #2b3155;
  --good: #58d68d;   /* Blue rule in the BRD is "ahead of pace" -- rendered as green-blue here */
  --bad: #ff6b81;    /* Red rule: behind pace */
  --warn: #ffcf6b;   /* Green rule (on pace) uses --good; --warn reserved for estimated/placeholder badges */
}

html[data-theme="light"] {
  --bg: #f4f6fb;
  --panel: #ffffff;
  --panel2: #eef1f8;
  --input-bg: #ffffff;
  --text: #1b2030;
  --muted: #5d6680;
  --accent: #1f6fd6;
  --accent-rgb: 31, 111, 214;
  --accent-text: #ffffff;
  --border: #d6dce8;
  --good: #1f9d55;
  --bad: #d6314a;
  --warn: #b8860b;
}

* { box-sizing: border-box; }

/* Fixed-viewport app shell -- html/body/#app-root/.view-panel never scroll themselves; only
   the CURRENT tab's own table (below) does. Two earlier approaches were tried and rejected:
   (1) letting the whole page scroll naturally, sticky thead relative to the document -- an
   ancestor's `overflow-x: auto` (needed for wide tables) silently made THAT ancestor the
   sticky positioning context instead (CSS computes `overflow-y` as `auto` the instant
   `overflow-x` isn't `visible`), breaking stickiness outright; (2) making #app-root the one
   scroll container -- this mostly worked, but tables tall enough to need scrolling showed a
   genuine, reproducible Chromium/Firefox rendering glitch (not a layout bug -- geometry was
   correct) where the row just above the sticky point visibly painted OVER the header instead
   of being covered by it. Giving each table its OWN tightly-scoped scroll box (in
   .table-card, further down) avoids both: the sticky ancestor is unambiguous and the box is
   small/simple enough that the glitch doesn't reproduce. Everything else in a tab (KPI strip,
   charts) keeps its natural height so the table is the only thing that ever needs to scroll --
   that's what keeps this down to exactly one visible scrollbar. */
html, body { height: 100%; }
body {
  margin: 0; display: flex; flex-direction: column; overflow: hidden;
  font-family: -apple-system, "Segoe UI", Roboto, Helvetica, Arial, sans-serif, "Noto Sans Thai";
  background: var(--bg);
  color: var(--text);
  line-height: 1.5;
}

.app-header {
  background: var(--panel);
  border-bottom: 1px solid var(--border);
  padding: 16px 24px;
  flex: 0 0 auto;
}
.app-header-row { display: flex; justify-content: space-between; align-items: center; flex-wrap: wrap; gap: 12px; }
.app-header h1 { margin: 0; font-size: 20px; }
.tagline { margin: 2px 0 0; color: var(--muted); font-size: 13px; }
.header-actions { display: flex; align-items: center; gap: 12px; }
.status-banner { font-size: 13px; color: var(--muted); }
.status-banner.error { color: var(--bad); }
.theme-toggle-btn {
  background: var(--panel2); border: 1px solid var(--border); color: var(--text);
  border-radius: 8px; padding: 6px 10px; cursor: pointer; font-size: 15px;
}
.theme-toggle-btn:hover { border-color: var(--accent); }

.logout-btn {
  background: var(--panel2); border: 1px solid var(--border); color: var(--text);
  border-radius: 8px; padding: 6px 12px; cursor: pointer; font-size: 13px;
  text-decoration: none; display: inline-flex; align-items: center;
}
.logout-btn:hover { border-color: var(--accent); color: var(--accent); }

#app-root {
  flex: 1 1 auto; min-height: 0; overflow: hidden;
  display: flex; flex-direction: column;
  padding: 20px 24px 16px; max-width: 1400px; margin: 0 auto; width: 100%;
}

.tab-bar { flex: 0 0 auto; display: flex; gap: 6px; border-bottom: 1px solid var(--border); margin-bottom: 16px; flex-wrap: wrap; }
.tab {
  background: none; border: none; color: var(--muted); padding: 10px 16px; cursor: pointer;
  font-size: 14px; border-bottom: 2px solid transparent; margin-bottom: -1px;
}
.tab:hover { color: var(--text); }
.tab.active { color: var(--accent); border-bottom-color: var(--accent); font-weight: 600; }

.filter-row { flex: 0 0 auto; display: flex; align-items: center; flex-wrap: wrap; gap: 10px; margin-bottom: 20px; position: relative; z-index: 5; }
.filter-chips-group { display: flex; flex-wrap: wrap; gap: 10px; flex: 1 1 auto; }

/* Searchable multi-select dropdown -- replaces an earlier one-chip-per-value design that
   didn't scale to fields like Product (Family), which has 70+ distinct values in real data. */
.filter-dropdown { position: relative; }
.filter-dropdown-btn {
  background: var(--panel); border: 1px solid var(--border); color: var(--text);
  border-radius: 8px; padding: 7px 14px; font-size: 13px; cursor: pointer; min-width: 150px;
  text-align: left;
}
.filter-dropdown-btn:hover { border-color: var(--accent); }
.filter-dropdown-panel {
  position: absolute; top: calc(100% + 4px); left: 0; z-index: 20; width: 260px;
  background: var(--panel); border: 1px solid var(--border); border-radius: 8px;
  box-shadow: 0 8px 24px rgba(0,0,0,0.25); padding: 8px;
}
.filter-dropdown-search {
  width: 100%; box-sizing: border-box; background: var(--input-bg, var(--bg)); color: var(--text);
  border: 1px solid var(--border); border-radius: 6px; padding: 6px 8px; font-size: 13px;
  margin-bottom: 6px;
}
.filter-dropdown-list { max-height: 240px; overflow-y: auto; display: flex; flex-direction: column; gap: 2px; }
.filter-dropdown-option {
  display: flex; align-items: center; gap: 8px; font-size: 13px; padding: 4px 6px;
  border-radius: 5px; cursor: pointer;
}
.filter-dropdown-option:hover { background: var(--panel2); }
.filter-dropdown-empty { font-size: 12.5px; color: var(--muted); padding: 6px; }

.view-panel {
  flex: 1 1 auto; min-height: 0; display: flex; flex-direction: column; overflow: hidden;
}

/* Tab name is already shown by the active .tab button in the nav above -- no separate
   heading here. The KPI-strip info icon lives at the right end of the filter row (a normal
   flex item, pushed there via margin-left: auto) rather than its own row, to save vertical
   space -- it only applies to the "MTD by Accounts" tab, but the filter row is shared chrome
   present on every tab regardless, same as the measure toggle in the header. */
.kpi-info-corner { display: flex; align-items: center; gap: 6px; margin-left: auto; }
.kpi-info-corner .tooltip-text { left: auto; right: 0; }
.section-label { font-size: 11.5px; color: var(--muted); text-transform: uppercase; letter-spacing: .04em; }

/* Qty(piece)/Value(THB) toggle -- same pill-button look as the tab bar, in the header since
   it applies globally across all 4 tabs (project.md's Phase 5 pptx re-read). */
.measure-toggle { display: flex; border-radius: 8px; overflow: hidden; border: 1px solid var(--border); }
.measure-btn {
  background: var(--panel2); border: none; color: var(--muted); padding: 7px 14px;
  font-size: 13px; cursor: pointer;
}
.measure-btn:hover { color: var(--text); }
.measure-btn.active { background: var(--accent); color: var(--accent-text); font-weight: 600; }

/* Hover/focus tooltip -- CSS-only, no JS needed. Used for the KPI-strip's "why don't these
   numbers move when I filter by Brand?" explanation (project.md §9/§12) so it's available
   without taking up permanent vertical space. */
.info-icon {
  position: relative; display: inline-flex; align-items: center; justify-content: center;
  width: 16px; height: 16px; border-radius: 50%; border: 1px solid var(--muted);
  color: var(--muted); font-size: 11px; cursor: help;
}
.info-icon .tooltip-text {
  display: none; position: absolute; top: 22px; left: 0; z-index: 30; width: 320px;
  background: var(--panel2); border: 1px solid var(--border); border-radius: 8px;
  padding: 10px 12px; font-size: 12px; color: var(--text); font-weight: normal;
  box-shadow: 0 8px 24px rgba(0,0,0,0.25);
}
.info-icon .tooltip-text code { background: none; padding: 0; color: inherit; }
.info-icon:hover .tooltip-text, .info-icon:focus .tooltip-text { display: block; }

/* .view-body is the ONE scroll container per tab -- KPI strip/charts/table all scroll
   together as one unit (see #app-root's comment above for why the document/#app-root itself
   never scrolls). An earlier version tried making ONLY .table-card scroll (everything above
   it fixed-height) so the header+charts would always stay put -- broke down the moment a tab
   had enough content above the table (2 charts + KPI strip) that there was barely any height
   left over for the table itself (it got squeezed to ~20px). Scrolling the whole tab body is
   simpler and doesn't have that failure mode. */
.view-body[hidden] { display: none; }
.view-body:not([hidden]) {
  flex: 1 1 auto; min-height: 0; overflow-y: auto; overflow-x: hidden;
  /* Hide the scrollbar itself (still fully scrollable via wheel/drag/keyboard) -- the
     browser's default scrollbar rendered light-on-dark and, on tab 3's wide matrix table,
     visually overlapped the rightmost column instead of sitting in reserved space. */
  scrollbar-width: none; /* Firefox */
}
.view-body:not([hidden])::-webkit-scrollbar { display: none; } /* Chromium/Safari/Edge */

.kpi-strip { display: flex; flex-wrap: wrap; gap: 12px; margin-bottom: 16px; }
.kpi-tile {
  flex: 1 1 160px; background: var(--panel); border: 1px solid var(--border); border-radius: 10px;
  padding: 12px 16px;
}
.kpi-tile .kpi-label { font-size: 12px; color: var(--muted); margin-bottom: 4px; }
.kpi-tile .kpi-value { font-size: 22px; font-weight: 600; }
.kpi-tile .kpi-note { font-size: 11px; color: var(--warn); margin-top: 2px; }

.chart-row { display: flex; gap: 16px; flex-wrap: wrap; margin-bottom: 20px; }
.chart-card {
  flex: 1 1 420px; background: var(--panel); border: 1px solid var(--border); border-radius: 10px;
  padding: 14px 16px;
}
/* Tab 1's 3-across chart row (Treemap | MTD vs Forecast by Account | MTD Sales by Channel,
   pptx mockup) -- always exactly 3 charts, never a dynamic list, so force them onto one row
   (`nowrap`) and let them shrink evenly (`flex-basis: 0`, `min-width: 0` so a chart's rendered
   canvas width doesn't refuse to shrink) instead of the wrap-then-grow behavior `.chart-row`
   uses elsewhere -- a flex-basis guess (e.g. 320px) wrapped to 2-per-row well before the
   container was actually full. */
.chart-row-3 { flex-wrap: nowrap; }
.chart-row-3 .chart-card { flex: 1 1 0; min-width: 0; }
.chart-card h3 { margin: 0 0 8px; font-size: 14px; color: var(--muted); font-weight: 600; }
.chart-container { width: 100%; height: 320px; }

.table-card {
  background: var(--panel); border: 1px solid var(--border); border-radius: 10px;
  padding: 14px 16px;
}
.table-card h3 { margin: 0 0 8px; font-size: 14px; color: var(--muted); font-weight: 600; }

/* `border-collapse: separate` + `border-spacing: 0` (NOT `collapse`) -- found 2026-07:
   `border-collapse: collapse` combined with a `position: sticky` thead is a known
   Chrome/Firefox rendering bug where the row that's just scrolled past bleeds through and
   renders ABOVE the sticky header instead of being covered by it. `separate` + zero spacing
   looks visually identical to `collapse` (bordered cells, no visible gaps) without the bug. */
table.data-table { width: 100%; border-collapse: separate; border-spacing: 0; font-size: 13px; white-space: nowrap; table-layout: fixed; }
table.data-table th, table.data-table td {
  /* Only right/bottom borders (+ left/top added back just on the first column/header row
     below) -- with `border-collapse: separate`, giving every cell a full 4-sided border
     would double up the shared edge between adjacent cells into a visibly thicker line. */
  border-right: 1px solid var(--border); border-bottom: 1px solid var(--border);
  padding: 5px 9px; text-align: right;
  overflow: hidden; text-overflow: ellipsis;
}
table.data-table th:first-child, table.data-table td:first-child { border-left: 1px solid var(--border); }
table.data-table thead th { border-top: 1px solid var(--border); }
table.data-table th:first-child, table.data-table td:first-child,
table.data-table th.text-col, table.data-table td.text-col { text-align: left; }
table.data-table thead th {
  background: var(--panel2); color: var(--muted); position: sticky; top: 0; z-index: 2;
  /* `position: sticky` also serves as the positioning context for the absolutely-positioned
     .col-resize-handle child -- no extra `position: relative` needed/wanted here. */
}
table.data-table tbody tr:nth-child(even) td { background: rgba(255,255,255,0.02); }
table.data-table tr.total-row td { font-weight: 700; background: var(--panel2); }

/* The 3-sub-column-per-account matrix (project.md's Phase 5 revision) can have ~28 columns
   (8 accounts x FC/MTD/%MTD + Brand/Segment + Total x3) -- `table-layout: fixed` +
   `white-space: nowrap` (the shared rule above, fine for every other table's ~7-9 columns)
   squeezes that many columns down to unreadable "M...". `.table-scroll-wrap` deliberately has
   no `overflow-x` of its own (see #app-root's comment above -- an ancestor's `overflow-x: auto`
   silently forces `overflow-y: auto` too, which would make it the sticky thead's positioning
   context instead of `.view-body` and reintroduce that already-fixed bug), so instead of
   relying on horizontal scroll, let the matrix size columns by content and wrap text that
   doesn't fit -- it never exceeds the container's own width this way.
   */
table.matrix-table { table-layout: auto; white-space: normal; font-size: 12px; }
table.matrix-table th, table.matrix-table td { padding: 4px 6px; }
table.matrix-table th.text-col:first-child { min-width: 110px; }

table.data-table th.sortable { cursor: pointer; user-select: none; }
table.data-table th.sortable:hover { color: var(--text); }
table.data-table th .sort-indicator { font-size: 10px; margin-left: 4px; color: var(--accent); }

.col-resize-handle {
  position: absolute; top: 0; right: -3px; width: 6px; height: 100%; cursor: col-resize;
  z-index: 3;
}
.col-resize-handle:hover, .col-resize-handle.resizing { background: var(--accent); opacity: 0.5; }

/* BRD's ±7%-of-TimeGone conditional-formatting rule (project.md §5/§6 Phase 5) -- only ever
   applied to a real per-account %Achievement figure (from Pending's own summary), never to a
   fabricated brand/segment-level number. Font color only (2026-07-06 feedback) -- no cell
   background wash. */
td.cf-red { color: var(--bad); font-weight: 600; }
td.cf-blue { color: var(--accent); font-weight: 600; }
td.cf-green { color: var(--good); font-weight: 600; }

/* Same BRD Red/Blue/Green rule, applied to a KPI tile's value text instead of a table cell --
   no background wash here (a whole tile tinted the same as a table cell reads as too heavy). */
.kpi-value.cf-red { color: var(--bad); }
.kpi-value.cf-blue { color: var(--accent); }
.kpi-value.cf-green { color: var(--good); }

.estimated-badge {
  display: inline-block; font-size: 10.5px; color: var(--warn); border: 1px solid var(--warn);
  border-radius: 4px; padding: 0 4px; margin-left: 6px; vertical-align: middle;
}

/* Collapsible tree drill-down table (renderDrillDownTable() in table.js) -- Brand/Segment
   group rows are clickable, with a rotating ▸/▾ arrow indicating expand state. */
table.data-table tr.group-row { cursor: pointer; }
table.data-table tr.group-row td { font-weight: 600; background: var(--panel2); }
table.data-table tr.group-row:hover td { background: color-mix(in srgb, var(--accent) 10%, var(--panel2)); }
.tree-toggle { display: inline-block; width: 12px; margin-right: 4px; transition: transform 0.15s; }
tr.group-row.expanded .tree-toggle { transform: rotate(90deg); }

.tree-expand-all-btn {
  background: var(--panel); border: 1px solid var(--border); color: var(--text);
  border-radius: 6px; padding: 2px 8px; font-size: 11px; cursor: pointer; margin-right: 8px;
  text-transform: none; font-weight: normal;
}
.tree-expand-all-btn:hover { border-color: var(--accent); color: var(--accent); }

/* Drag-to-reorder hierarchy control (renderHierarchyOrderControl() in table.js) -- sits above
   the tree table, separate from the filter chips (see that function's docstring for why). */
.hierarchy-order-row { display: flex; align-items: center; gap: 8px; margin-bottom: 10px; }
.hierarchy-order-label { font-size: 12px; color: var(--muted); }
.hierarchy-tag {
  background: var(--panel); border: 1px solid var(--border); color: var(--text);
  border-radius: 14px; padding: 4px 12px; font-size: 12.5px; cursor: grab; user-select: none;
}
.hierarchy-tag:hover { border-color: var(--accent); }
.hierarchy-tag.dragging { opacity: 0.4; cursor: grabbing; }

.empty-state, .error-state { color: var(--muted); padding: 24px; text-align: center; font-size: 13.5px; }
.error-state { color: var(--bad); }

/* Loading spinner -- same pattern as ..\Reference\static\css\style.css's
   .loading-spinner-wrap, theme-aware via --border/--accent. */
.loading-spinner-wrap { display: flex; flex-direction: column; align-items: center; gap: 10px; padding: 40px 0; color: var(--muted); font-size: 13px; }
.loading-spinner {
  width: 28px; height: 28px; border-radius: 50%; border: 3px solid var(--border);
  border-top-color: var(--accent); animation: spin 0.8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }
