/* ---------- Palette (light default, dark via media query or [data-theme]) ---------- */
:root {
  color-scheme: light;
  --surface-1:      #fcfcfb;
  --page-plane:     #f9f9f7;
  --text-primary:   #0b0b0b;
  --text-secondary: #52514e;
  --text-muted:     #898781;
  --gridline:       #e1e0d9;
  --baseline:       #c3c2b7;
  --border:         rgba(11,11,11,0.10);
  --success-text:   #006300;

  --series-1: #2a78d6; /* blue   - PSA */
  --series-2: #eb6834; /* orange - BGS */
  --series-3: #1baf7a; /* aqua   - CGC */
  --series-4: #eda100; /* yellow - SGC */
  --series-5: #e87ba4; /* magenta- Other */

  --status-good: #0ca30c;
  --status-warning: #fab219;
  --status-serious: #ec835a;
  --status-critical: #d03b3b;

  --radius: 10px;
  --radius-sm: 6px;
}

@media (prefers-color-scheme: dark) {
  :root:where(:not([data-theme="light"])) {
    color-scheme: dark;
    --surface-1:      #1a1a19;
    --page-plane:     #0d0d0d;
    --text-primary:   #ffffff;
    --text-secondary: #c3c2b7;
    --text-muted:     #898781;
    --gridline:       #2c2c2a;
    --baseline:       #383835;
    --border:         rgba(255,255,255,0.10);
    --success-text:   #0ca30c;

    --series-1: #3987e5;
    --series-2: #d95926;
    --series-3: #199e70;
    --series-4: #c98500;
    --series-5: #d55181;

    --status-warning: #fab219;
    --status-serious: #ec835a;
    --status-critical: #e66767;
  }
}

:root[data-theme="dark"] {
  color-scheme: dark;
  --surface-1:      #1a1a19;
  --page-plane:     #0d0d0d;
  --text-primary:   #ffffff;
  --text-secondary: #c3c2b7;
  --text-muted:     #898781;
  --gridline:       #2c2c2a;
  --baseline:       #383835;
  --border:         rgba(255,255,255,0.10);
  --success-text:   #0ca30c;

  --series-1: #3987e5;
  --series-2: #d95926;
  --series-3: #199e70;
  --series-4: #c98500;
  --series-5: #d55181;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--page-plane);
  color: var(--text-primary);
  font-family: system-ui, -apple-system, "Segoe UI", sans-serif;
}

.app { max-width: 1550px; margin: 0 auto; padding: 20px 24px 60px; }

/* ---------- Topbar ---------- */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  flex-wrap: wrap;
  padding: 8px 0 20px;
}
.topbar-left { display: flex; align-items: center; gap: 16px; flex-wrap: wrap; }
.topbar-title { display: flex; align-items: center; gap: 12px; }
.topbar-emoji { font-size: 34px; line-height: 1; }
.topbar h1 { margin: 0; font-size: 22px; font-weight: 650; }
.subtitle { margin: 2px 0 0; color: var(--text-secondary); font-size: 13px; }
.topbar-actions { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }
.backlink { color: var(--text-secondary); font-size: 13px; text-decoration: none; margin-right: 4px; }
.backlink:hover { color: var(--text-primary); }

/* ---------- Buttons ---------- */
.btn {
  font: inherit;
  font-size: 13.5px;
  font-weight: 600;
  padding: 9px 14px;
  border-radius: var(--radius-sm);
  border: 1px solid transparent;
  cursor: pointer;
  transition: filter .12s ease, background .12s ease;
}
a.btn { display: inline-block; text-decoration: none; text-align: center; }
.btn:hover { filter: brightness(0.96); }
:root[data-theme="dark"] .btn:hover,
@media (prefers-color-scheme: dark) { .btn:hover { filter: brightness(1.12); } }
.btn-primary { background: var(--series-1); color: #fff; }
.btn-secondary { background: var(--surface-1); color: var(--text-primary); border-color: var(--border); }
.btn-ghost { background: transparent; color: var(--text-secondary); border-color: transparent; }
.btn-danger { color: var(--status-critical); border-color: var(--status-critical); }
.btn-ghost:hover { background: var(--surface-1); }
.btn:disabled { opacity: 0.45; cursor: not-allowed; }

/* ---------- Stat tiles ---------- */
.stat-row {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 12px;
  margin-bottom: 16px;
}
.stat-tile {
  background: var(--surface-1);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px 18px;
  display: flex;
  flex-direction: column;
  gap: 4px;
}
.stat-label { font-size: 12.5px; color: var(--text-secondary); }
.stat-value { font-size: 26px; font-weight: 650; letter-spacing: -0.01em; }
.stat-delta { font-size: 13px; font-weight: 600; }
.stat-delta.positive { color: var(--success-text); }
.stat-delta.negative { color: var(--status-critical); }

/* ---------- Charts ---------- */
.chart-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
  margin-bottom: 20px;
}
.chart-card {
  background: var(--surface-1);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px 18px 10px;
}
.chart-card-wide { grid-column: 1 / -1; }
.chart-card h2 { margin: 0 0 10px; font-size: 14px; font-weight: 650; }
.chart-body { position: relative; min-height: 40px; }
.chart-empty { color: var(--text-muted); font-size: 13px; margin: 4px 0 8px; }

.bar-row { display: flex; align-items: center; gap: 8px; height: 30px; }
.bar-row .bar-label {
  width: 110px; flex: 0 0 110px;
  font-size: 12.5px; color: var(--text-secondary);
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.bar-row .bar-track { position: relative; flex: 1; height: 24px; }
.bar-row .bar-fill {
  position: absolute; left: 0; top: 0; height: 24px; border-radius: 4px;
  transition: width .25s ease;
}
.bar-row .bar-value {
  position: absolute; top: 50%; transform: translateY(-50%);
  font-size: 12px; font-weight: 600; color: var(--text-primary);
  white-space: nowrap;
}
.chart-gridline-wrap { position: relative; margin-left: 118px; }

.legend { display: flex; flex-wrap: wrap; gap: 12px; margin-top: 10px; }
.legend-item { display: flex; align-items: center; gap: 6px; font-size: 12px; color: var(--text-secondary); }
.legend-swatch { width: 10px; height: 10px; border-radius: 2px; flex: 0 0 auto; }

.trend-tooltip {
  position: absolute; pointer-events: none; background: var(--surface-1);
  border: 1px solid var(--border); border-radius: var(--radius-sm);
  padding: 6px 10px; font-size: 12px; box-shadow: 0 4px 14px rgba(0,0,0,0.12);
  transform: translate(-50%, -110%); white-space: nowrap; z-index: 5;
}

/* ---------- Table ---------- */
.table-section {
  background: var(--surface-1);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px 18px 6px;
}
.table-toolbar { display: flex; gap: 8px; flex-wrap: wrap; margin-bottom: 12px; }
.input {
  font: inherit; font-size: 13.5px;
  padding: 8px 10px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
  background: var(--page-plane);
  color: var(--text-primary);
}
#searchInput { flex: 1; min-width: 180px; }

.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; font-size: 13px; }
thead th {
  text-align: left; font-size: 11.5px; text-transform: uppercase; letter-spacing: .03em;
  color: var(--text-muted); font-weight: 600; padding: 8px 10px; border-bottom: 1px solid var(--gridline);
  white-space: nowrap;
}
th.num, td.num { text-align: right; font-variant-numeric: tabular-nums; }
th.col-center, td.col-center { text-align: center; }
tbody td { padding: 10px; border-bottom: 1px solid var(--gridline); white-space: nowrap; }
tbody tr:hover { background: var(--page-plane); }
tbody tr:last-child td { border-bottom: none; }
.cell-primary { font-weight: 600; }
.cell-secondary { color: var(--text-secondary); }
.gain-positive { color: var(--success-text); font-weight: 600; }
.gain-negative { color: var(--status-critical); font-weight: 600; }
.row-actions { display: flex; gap: 6px; justify-content: flex-end; }
.icon-btn {
  background: transparent; border: none; cursor: pointer; font-size: 14px;
  padding: 4px 6px; border-radius: 6px; color: var(--text-secondary);
  display: inline-flex; align-items: center; justify-content: center;
}
.icon-btn:hover { background: var(--page-plane); color: var(--text-primary); }
.save-btn { color: var(--success-text); }
.save-btn:hover { color: var(--success-text); filter: brightness(1.15); }
.del-btn { color: var(--status-critical); }
.del-btn:hover { color: var(--status-critical); filter: brightness(1.15); }

.grader-badge {
  display: inline-flex; align-items: center; gap: 6px;
  font-size: 12px; font-weight: 600; color: var(--text-primary);
}
.grader-badge::before {
  content: ""; width: 8px; height: 8px; border-radius: 50%;
  background: var(--dot-color, var(--text-muted));
}

.table-empty { color: var(--text-muted); font-size: 13.5px; text-align: center; padding: 26px 10px; }

/* ---------- Modals ---------- */
.modal-overlay {
  display: none; position: fixed; inset: 0; background: rgba(0,0,0,0.45);
  align-items: center; justify-content: center; z-index: 50; padding: 20px;
}
.modal-overlay.open { display: flex; }
.modal {
  background: var(--surface-1); border-radius: var(--radius);
  width: 100%; max-width: 640px; max-height: 90vh; overflow-y: auto;
  border: 1px solid var(--border);
}
.modal-wide { max-width: 760px; }
.modal-header {
  display: flex; align-items: center; justify-content: space-between;
  padding: 16px 20px; border-bottom: 1px solid var(--gridline);
  position: sticky; top: 0; background: var(--surface-1); z-index: 1;
}
.modal-header h2 { margin: 0; font-size: 16px; }
.modal-close { background: transparent; border: none; font-size: 16px; cursor: pointer; color: var(--text-secondary); }
.modal-body { padding: 20px; }
.form-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; }
.field { display: flex; flex-direction: column; gap: 5px; font-size: 12.5px; color: var(--text-secondary); }
.field-wide { grid-column: 1 / -1; }
.field input, .field select {
  font: inherit; font-size: 14px; padding: 9px 10px; border-radius: var(--radius-sm);
  border: 1px solid var(--border); background: var(--page-plane); color: var(--text-primary);
}
.modal-actions {
  display: flex; align-items: center; justify-content: space-between;
  margin-top: 20px; padding-top: 16px; border-top: 1px solid var(--gridline);
}
.modal-actions-footer { padding: 16px 20px; margin: 0; border-top: 1px solid var(--gridline); }
.modal-actions-right { display: flex; gap: 8px; margin-left: auto; }

.help-text { font-size: 13px; color: var(--text-secondary); margin: 0 0 14px; }
.radio-row { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin-bottom: 16px; }
.radio-card {
  display: flex; gap: 10px; align-items: flex-start; padding: 12px;
  border: 1px solid var(--border); border-radius: var(--radius-sm); cursor: pointer;
}
.radio-card:has(input:checked) { border-color: var(--series-1); background: color-mix(in srgb, var(--series-1) 8%, var(--surface-1)); }
.radio-card input { margin-top: 3px; }
.radio-card strong { font-size: 13.5px; display: block; margin-bottom: 3px; }
.radio-card p { margin: 0; font-size: 12px; color: var(--text-secondary); }

.checkbox-row { display: flex; align-items: center; gap: 8px; font-size: 13.5px; margin-top: 10px; }

.import-summary { font-size: 14px; line-height: 1.7; background: var(--page-plane); border-radius: var(--radius-sm); padding: 12px 14px; }
.import-summary b { font-variant-numeric: tabular-nums; }

.toast {
  position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%) translateY(20px);
  background: var(--text-primary); color: var(--surface-1);
  padding: 10px 18px; border-radius: 8px; font-size: 13.5px; font-weight: 500;
  opacity: 0; pointer-events: none; transition: opacity .2s ease, transform .2s ease;
  z-index: 100;
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }

@media (max-width: 860px) {
  .stat-row { grid-template-columns: 1fr 1fr; }
  .chart-row { grid-template-columns: 1fr; }
  .form-grid, .radio-row { grid-template-columns: 1fr; }
}
