:root {
  color-scheme: light;
  --page: #f9f9f7;
  --surface: #fcfcfb;
  --surface-2: #f2f1ee;
  --ink: #0b0b0b;
  --ink-soft: #52514e;
  --muted: #898781;
  --line: #e1e0d9;
  --line-strong: #c3c2b7;
  --ring: rgba(11, 11, 11, 0.10);

  --accent: #2a78d6;
  --accent-strong: #184f95;
  --accent-soft: #eaf2fc;
  --aqua: #1baf7a;

  --good: #0ca30c;
  --good-soft: #e4f7e4;
  --warning: #b8790a;
  --warning-soft: #fdf1dc;
  --critical: #d03b3b;
  --critical-soft: #fbe6e6;

  --div-neg: #256abf;
  --div-pos: #d03b3b;
  --div-mid: #f0efec;

  --shadow: 0 1px 2px rgba(16, 24, 32, .04), 0 8px 24px rgba(16, 24, 32, .06);
  --radius: 12px;
  --radius-sm: 8px;

  --chart-ink: #52514e;
  --chart-grid: #d8d6cd;

  --nav-bg: #0f1520;
  --nav-line: #232b3a;
  --nav-ink: #e7eaf0;
  --nav-muted: #8b96a8;
  --nav-active-bg: #f9f9f7;
  --nav-active-ink: #0f1520;
}

:root[data-theme="dark"] {
  color-scheme: dark;
  --page: #0d0d0d;
  --surface: #17181a;
  --surface-2: #1f2023;
  --ink: #ffffff;
  --ink-soft: #c3c2b7;
  --muted: #8f8d87;
  --line: #2c2c2a;
  --line-strong: #383835;
  --ring: rgba(255, 255, 255, 0.10);

  --accent: #3987e5;
  --accent-strong: #86b6ef;
  --accent-soft: #17263b;
  --aqua: #199e70;

  --good: #35c235;
  --good-soft: #103410;
  --warning: #fab219;
  --warning-soft: #3a2c0c;
  --critical: #e66767;
  --critical-soft: #3a1717;

  --div-neg: #3987e5;
  --div-pos: #e66767;
  --div-mid: #383835;

  --shadow: 0 1px 2px rgba(0, 0, 0, .3), 0 8px 28px rgba(0, 0, 0, .35);

  --chart-ink: #d6d4cc;
  --chart-grid: #3d3d3a;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-height: 100vh;
  background: var(--page);
  color: var(--ink);
  font: 14px/1.5 "Segoe UI", system-ui, -apple-system, Arial, sans-serif;
  transition: background-color .15s ease, color .15s ease;
}

button,
input,
select {
  font: inherit;
  color: inherit;
}

button {
  cursor: pointer;
}

svg {
  display: block;
}

/* ---------- loading overlay ---------- */

.loading-overlay {
  position: fixed;
  inset: 0;
  z-index: 999;
  display: grid;
  place-items: center;
  gap: 14px;
  background: var(--page);
  color: var(--muted);
  transition: opacity .25s ease;
}

.loading-overlay.hide {
  opacity: 0;
  pointer-events: none;
}

.spinner {
  width: 34px;
  height: 34px;
  border-radius: 50%;
  border: 3px solid var(--line);
  border-top-color: var(--accent);
  animation: spin .8s linear infinite;
}

@keyframes spin {
  to { transform: rotate(360deg); }
}

/* ---------- shell ---------- */

.shell {
  display: grid;
  grid-template-columns: 272px minmax(0, 1fr);
  min-height: 100vh;
}

.sidebar {
  background: var(--nav-bg);
  color: var(--nav-ink);
  padding: 22px;
  display: flex;
  flex-direction: column;
  gap: 24px;
  position: sticky;
  top: 0;
  height: 100vh;
  border-right: 1px solid var(--nav-line);
}

.brand {
  display: flex;
  justify-content: center;
  padding: 6px 0 12px;
  border-bottom: 1px solid var(--nav-line);
}

.brand-logo {
  width: 100%;
  max-width: 172px;
  height: auto;
  display: block;
  filter: drop-shadow(0 6px 14px rgba(0, 0, 0, .35));
}

h2, h3, p {
  margin: 0;
}

.sidebar .field span {
  color: var(--nav-muted);
}

.field {
  display: grid;
  gap: 7px;
  min-width: 0;
}

.field span {
  font-size: 11px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: .04em;
}

select,
input {
  width: 100%;
  min-width: 0;
  height: 40px;
  box-sizing: border-box;
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  padding: 0 11px;
  background: var(--surface);
  color: var(--ink);
  overflow: hidden;
  text-overflow: ellipsis;
  outline-offset: 2px;
}

select:focus-visible,
input:focus-visible,
button:focus-visible {
  outline: 2px solid var(--accent);
}

.sidebar select {
  border-color: var(--nav-line);
  background: #161d2b;
  color: var(--nav-ink);
}

.tabs {
  display: grid;
  gap: 6px;
}

#manualAccessFields {
  display: grid;
  gap: 24px;
}

.session-info .session-name {
  min-height: 40px;
  border: 1px solid var(--nav-line);
  border-radius: var(--radius-sm);
  padding: 9px 11px;
  background: #161d2b;
  color: var(--nav-ink);
  font-weight: 700;
  font-size: 13px;
  display: flex;
  align-items: center;
}

.logout-link {
  background: none;
  border: none;
  color: var(--nav-muted);
  font-size: 11.5px;
  font-weight: 600;
  text-decoration: underline;
  padding: 4px 2px 0;
  justify-self: start;
}

.logout-link:hover {
  color: var(--nav-ink);
}

.tab {
  display: flex;
  align-items: center;
  gap: 10px;
  border: 1px solid transparent;
  background: transparent;
  color: var(--nav-muted);
  border-radius: var(--radius-sm);
  padding: 10px 12px;
  text-align: left;
  font-weight: 600;
  transition: background-color .12s ease, color .12s ease;
}

.tab svg {
  width: 17px;
  height: 17px;
  flex: none;
}

.tab:hover {
  background: #161d2b;
  color: var(--nav-ink);
}

.tab.active {
  background: var(--nav-active-bg);
  color: var(--nav-active-ink);
}

.meta {
  margin-top: auto;
  display: grid;
  gap: 8px;
  font-size: 12px;
  color: var(--nav-muted);
  border-top: 1px solid var(--nav-line);
  padding-top: 14px;
}

.theme-toggle {
  display: flex;
  align-items: center;
  gap: 9px;
  background: #161d2b;
  border: 1px solid var(--nav-line);
  color: var(--nav-ink);
  border-radius: var(--radius-sm);
  padding: 9px 11px;
  font-weight: 600;
  font-size: 12px;
  margin-bottom: 2px;
}

.notify-actions {
  display: grid;
  gap: 8px;
  margin-bottom: 2px;
}

.notify-actions .primary,
.notify-actions .theme-toggle {
  width: 100%;
  font-size: 12px;
  margin-bottom: 0;
}

.notify-actions .primary svg,
.notify-actions .theme-toggle svg {
  width: 15px;
  height: 15px;
}

.refresh-panel {
  display: grid;
  gap: 8px;
  margin-bottom: 2px;
  padding-top: 6px;
  border-top: 1px solid var(--nav-line);
}

.refresh-panel-label {
  font-size: 11px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: .04em;
  color: var(--nav-muted);
}

.file-drop {
  display: grid;
  gap: 4px;
  font-size: 11.5px;
  color: var(--nav-muted);
}

.file-drop input[type="file"] {
  border: 1px solid var(--nav-line);
  border-radius: var(--radius-sm);
  background: #161d2b;
  color: var(--nav-ink);
  padding: 6px 7px;
  font-size: 11px;
  max-width: 100%;
}

.refresh-panel .primary {
  width: 100%;
  font-size: 12px;
  margin-bottom: 0;
}

.refresh-status {
  font-size: 11.5px;
  color: var(--nav-muted);
  line-height: 1.4;
  word-break: break-word;
}

.theme-toggle svg {
  width: 16px;
  height: 16px;
}

.theme-toggle .icon-moon { display: none; }
:root[data-theme="dark"] .theme-toggle .icon-sun { display: none; }
:root[data-theme="dark"] .theme-toggle .icon-moon { display: block; }

.content {
  padding: 24px;
  display: grid;
  gap: 18px;
  align-content: start;
}

.toolbar {
  display: grid;
  grid-template-columns: repeat(4, minmax(140px, 1fr)) minmax(220px, 1.4fr) auto;
  gap: 12px;
  align-items: end;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 14px;
  box-shadow: var(--shadow);
}

.primary {
  height: 40px;
  box-sizing: border-box;
  border: 1px solid transparent;
  border-radius: var(--radius-sm);
  padding: 0 16px;
  color: #fff;
  background: var(--accent);
  font-weight: 700;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  white-space: nowrap;
  transition: background-color .12s ease, transform .12s ease;
}

.primary svg {
  width: 15px;
  height: 15px;
}

.primary:hover {
  background: var(--accent-strong);
}

.primary:active {
  transform: scale(.98);
}

/* ---------- KPI cards ---------- */

.kpis {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 12px;
}

.kpi-card {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 16px;
  box-shadow: var(--shadow);
  display: flex;
  gap: 12px;
  min-width: 0;
}

.kpi-icon {
  width: 38px;
  height: 38px;
  border-radius: 9px;
  display: grid;
  place-items: center;
  flex: none;
  background: var(--accent-soft);
  color: var(--accent);
}

.kpi-icon svg {
  width: 19px;
  height: 19px;
}

.kpi-icon.budget { background: color-mix(in srgb, var(--aqua) 16%, transparent); color: var(--aqua); }
.kpi-icon.variance { background: var(--critical-soft); color: var(--critical); }
.kpi-icon.pct { background: var(--warning-soft); color: var(--warning); }

.kpi-body {
  display: grid;
  gap: 4px;
  min-width: 0;
  flex: 1;
}

.kpi-body > span {
  color: var(--muted);
  font-size: 11px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: .04em;
}

.kpi-body strong {
  font-size: 22px;
  font-weight: 700;
  letter-spacing: -.01em;
  line-height: 1.1;
}

.kpi-foot {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  margin-top: 2px;
}

.delta {
  font-size: 12px;
  font-weight: 700;
  white-space: nowrap;
}

.delta.up { color: var(--critical); }
.delta.down { color: var(--good); }
.delta.flat { color: var(--muted); }

.sparkline {
  width: 68px;
  height: 22px;
  flex: none;
}

.sparkline .spark-line {
  fill: none;
  stroke: var(--accent);
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.sparkline .spark-area {
  fill: var(--accent);
  opacity: .10;
  stroke: none;
}

/* ---------- panels / charts ---------- */

.panel {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 16px;
  box-shadow: var(--shadow);
  display: grid;
  gap: 12px;
}

.panel-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  flex-wrap: wrap;
}

.panel-head h3 {
  font-size: 14px;
  font-weight: 700;
}

.hint {
  color: var(--muted);
  font-size: 12px;
}

.legend {
  display: flex;
  gap: 14px;
  flex-wrap: wrap;
}

.legend-item {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 12px;
  font-weight: 600;
  color: var(--ink-soft);
}

.legend-swatch {
  width: 12px;
  height: 3px;
  border-radius: 2px;
  flex: none;
}

.legend-swatch.dashed {
  background: repeating-linear-gradient(to right, var(--muted) 0 4px, transparent 4px 7px);
}

.chart-wrap {
  overflow-x: auto;
}

.chart-wrap svg {
  width: 100%;
  height: auto;
  overflow: visible;
}

.chart-axis,
.chart-grid-line {
  font-size: 11px;
}

.chart-grid-line {
  stroke: var(--chart-grid);
  stroke-width: 1;
}

.chart-baseline {
  stroke: var(--chart-grid);
  stroke-width: 1.4;
}

.chart-axis {
  fill: var(--chart-ink);
  font-weight: 600;
}

.trend-line {
  fill: none;
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.trend-line.actual {
  stroke: var(--accent);
}

.trend-line.budget {
  stroke: var(--ink-soft);
  stroke-dasharray: 5 4;
}

.trend-area {
  fill: var(--accent);
  opacity: .08;
  stroke: none;
}

.trend-dot {
  fill: var(--surface);
  stroke-width: 2;
  r: 4;
}

.trend-dot.actual { stroke: var(--accent); }
.trend-dot.budget { stroke: var(--ink-soft); }

.trend-hit {
  fill: transparent;
}

.trend-crosshair {
  stroke: var(--chart-ink);
  stroke-width: 1;
  opacity: .5;
  pointer-events: none;
}

/* ---------- heatmap ---------- */

.heatmap-wrap {
  overflow-x: auto;
}

.heatmap {
  border-collapse: separate;
  border-spacing: 2px;
  width: 100%;
}

.heatmap th {
  font-size: 11px;
  font-weight: 700;
  color: var(--chart-ink);
  text-transform: uppercase;
  padding: 4px 6px;
  text-align: center;
  white-space: nowrap;
}

.heatmap th.row-label,
.heatmap td.row-label {
  text-align: left;
  color: var(--ink-soft);
  font-weight: 600;
  font-size: 12px;
  padding: 6px 10px 6px 2px;
  white-space: nowrap;
  text-transform: none;
}

.heat-cell {
  min-width: 46px;
  height: 30px;
  border-radius: 5px;
  cursor: default;
}

.heatmap-legend {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 11px;
  color: var(--muted);
  margin-top: 4px;
}

.heatmap-legend .ramp {
  width: 140px;
  height: 8px;
  border-radius: 4px;
  background: linear-gradient(to right, var(--div-neg), var(--div-mid), var(--div-pos));
}

/* ---------- tooltip ---------- */

.tooltip {
  position: fixed;
  z-index: 1000;
  background: var(--ink);
  color: var(--page);
  padding: 8px 10px;
  border-radius: 7px;
  font-size: 12px;
  line-height: 1.4;
  pointer-events: none;
  box-shadow: var(--shadow);
  max-width: 240px;
}

:root[data-theme="dark"] .tooltip {
  background: #f2f1ee;
  color: #0d0d0d;
}

.tooltip strong {
  display: block;
  margin-bottom: 2px;
}

/* ---------- dashboard view ---------- */

.view {
  display: none;
  gap: 14px;
}

.view.active {
  display: grid;
}

.section-head {
  display: flex;
  justify-content: space-between;
  gap: 14px;
  align-items: center;
}

.section-head h2 {
  font-size: 19px;
  letter-spacing: -.01em;
  font-weight: 700;
}

.section-head p {
  color: var(--muted);
  font-size: 12.5px;
  margin-top: 3px;
}

.compact h2 {
  font-size: 15px;
}

#rowCount {
  color: var(--muted);
  font-weight: 700;
  font-size: 12.5px;
}

.summary-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(260px, 1fr));
  gap: 12px;
}

.summary-card {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 14px;
  display: grid;
  gap: 12px;
  box-shadow: var(--shadow);
  text-align: left;
  transition: transform .12s ease, box-shadow .12s ease, border-color .12s ease;
}

button.summary-card {
  cursor: pointer;
}

button.summary-card:hover {
  transform: translateY(-2px);
  border-color: var(--line-strong);
  box-shadow: 0 4px 10px rgba(16, 24, 32, .08), 0 14px 30px rgba(16, 24, 32, .10);
}

.summary-card header {
  display: flex;
  justify-content: space-between;
  gap: 10px;
}

.summary-card header > div {
  min-width: 0;
}

.summary-card h3 {
  margin: 0;
  font-size: 14.5px;
  font-weight: 700;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.code {
  color: var(--muted);
  font-size: 12px;
  font-weight: 700;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  display: block;
}

.summary-card .badge {
  flex: none;
}

.metrics {
  display: grid;
  gap: 7px;
}

.metrics > div {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 12px;
}

.metrics span {
  color: var(--muted);
  font-size: 10.5px;
  text-transform: uppercase;
  font-weight: 700;
  letter-spacing: .03em;
  flex: none;
}

.metrics strong {
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
  text-align: right;
  font-size: 13px;
  min-width: 0;
}

.badge {
  border-radius: 999px;
  padding: 4px 9px;
  font-size: 11.5px;
  font-weight: 800;
  white-space: nowrap;
  display: inline-flex;
  align-items: center;
  gap: 4px;
}

.badge.good {
  color: var(--good);
  background: var(--good-soft);
}

.badge.warn {
  color: var(--warning);
  background: var(--warning-soft);
}

.badge.bad {
  color: var(--critical);
  background: var(--critical-soft);
}

.bars {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 16px;
  display: grid;
  gap: 12px;
  box-shadow: var(--shadow);
}

.bar-row {
  display: grid;
  grid-template-columns: minmax(140px, 260px) 1fr minmax(100px, auto);
  gap: 12px;
  align-items: center;
}

.bar-row strong {
  font-size: 12.5px;
  font-weight: 600;
  color: var(--ink-soft);
}

.bar-track {
  height: 10px;
  background: var(--surface-2);
  border-radius: 999px;
  overflow: hidden;
}

.bar-fill {
  height: 100%;
  width: var(--w);
  border-radius: 0 4px 4px 0;
  background: var(--good);
}

.bar-fill.bad {
  background: var(--critical);
}

.bar-fill.actual {
  background: var(--accent);
}

.bar-fill.budget {
  background: var(--line-strong);
}

.bar-track.mini {
  height: 6px;
  min-width: 70px;
}

/* ---------- composition rows ---------- */

.comp-row {
  display: grid;
  gap: 6px;
  padding-bottom: 12px;
  border-bottom: 1px solid var(--line);
}

.comp-row:last-child {
  border-bottom: 0;
  padding-bottom: 0;
}

.comp-row > strong {
  font-size: 12.5px;
  font-weight: 700;
  color: var(--ink-soft);
}

.comp-bar-line {
  display: grid;
  grid-template-columns: 56px 1fr minmax(110px, auto);
  align-items: center;
  gap: 10px;
}

.comp-tag {
  font-size: 10.5px;
  color: var(--muted);
  text-transform: uppercase;
  font-weight: 700;
  letter-spacing: .03em;
}

.comp-bar-line .num {
  font-size: 12.5px;
  font-weight: 700;
}

.bar-row .num {
  font-variant-numeric: tabular-nums;
  font-weight: 700;
  font-size: 12.5px;
}

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

.pager {
  display: flex;
  gap: 9px;
  align-items: center;
}

.pager span {
  font-size: 12.5px;
  color: var(--ink-soft);
  font-weight: 600;
}

.pager button {
  width: 32px;
  height: 32px;
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  background: var(--surface);
  font-size: 19px;
  color: var(--ink-soft);
}

.pager button:disabled {
  opacity: .4;
  cursor: not-allowed;
}

.table-wrap {
  overflow: auto;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  max-height: 74vh;
}

.panel .table-wrap {
  border: 0;
  box-shadow: none;
  background: transparent;
  border-radius: 0;
  max-height: none;
}

table {
  width: 100%;
  border-collapse: collapse;
  min-width: 1020px;
}

.panel table {
  min-width: 760px;
}

th,
td {
  padding: 10px 12px;
  border-bottom: 1px solid var(--line);
  text-align: left;
  vertical-align: top;
}

th {
  position: sticky;
  top: 0;
  background: var(--surface-2);
  z-index: 1;
  color: var(--ink-soft);
  font-size: 11.5px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: .03em;
}

th.sortable {
  cursor: pointer;
  user-select: none;
}

th.sortable:hover {
  color: var(--ink);
}

th.sortable::after {
  content: "⇅";
  display: inline-block;
  margin-left: 4px;
  opacity: .35;
  font-size: 10px;
}

th.sortable.sorted-asc::after {
  content: "▲";
  opacity: 1;
  color: var(--accent);
}

th.sortable.sorted-desc::after {
  content: "▼";
  opacity: 1;
  color: var(--accent);
}

tbody tr:hover {
  background: var(--surface-2);
}

.num {
  text-align: right;
}

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

.note {
  min-width: 180px;
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  padding: 7px 8px;
  background: var(--surface);
}

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

  .sidebar {
    position: static;
    height: auto;
  }

  .toolbar,
  .kpis {
    grid-template-columns: 1fr 1fr;
  }

  .search,
  .primary {
    grid-column: 1 / -1;
  }
}

@media (max-width: 640px) {
  .content {
    padding: 14px;
  }

  .toolbar,
  .kpis {
    grid-template-columns: 1fr;
  }

  .section-head {
    align-items: flex-start;
    flex-direction: column;
  }

  .bar-row {
    grid-template-columns: 1fr;
    gap: 5px;
  }
}
