:root {
  --bg: #0d1117;
  --bg-raised: #151b24;
  --bg-inset: #0a0e14;
  --border: #232b36;
  --border-strong: #313b48;
  --text: #e6edf3;
  --text-dim: #9aa7b4;
  --text-faint: #6b7785;
  --accent: #ff9d2f;
  --accent-dim: #b36b16;
  --good: #3fb950;
  --bad: #f85149;
  --info: #58a6ff;
  --radius: 10px;
  --radius-sm: 6px;
  --shadow: 0 8px 24px rgba(0, 0, 0, .4);
  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  --mono: ui-monospace, SFMono-Regular, "SF Mono", Menlo, Consolas, monospace;
}

* { box-sizing: border-box; }

/* Several components below set `display`, which would otherwise beat the
   browser's `[hidden] { display: none }` and leave invisible elements
   intercepting clicks. */
[hidden] { display: none !important; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  font-size: 15px;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
}

h1, h2, h3 { margin: 0; font-weight: 600; letter-spacing: -.01em; }
h1 { font-size: 1.05rem; }
h2 { font-size: .95rem; }
h3 { font-size: .92rem; }
p { margin: 0; }
code { font-family: var(--mono); font-size: .85em; background: var(--bg-inset); padding: .1em .35em; border-radius: 4px; }
.mono { font-family: var(--mono); }
.muted { color: var(--text-faint); font-weight: 400; }
.hint { color: var(--text-faint); font-size: .78rem; margin-top: .5rem; }

/* ---------- top bar ---------- */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  padding: .7rem 1.25rem;
  background: var(--bg-raised);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 50;
  flex-wrap: wrap;
}
.brand { display: flex; align-items: center; gap: .75rem; min-width: 0; }
.brand-mark { font-size: 1.6rem; line-height: 1; }
.brand-sub {
  color: var(--text-dim);
  font-size: .8rem;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 60vw;
}
.topbar-actions { display: flex; align-items: center; gap: .6rem; }
.run-picker { max-width: 260px; }

/* ---------- controls ---------- */
button, select, input[type=range], input[type=file] { font-family: inherit; font-size: .85rem; }

.btn {
  border: 1px solid var(--border-strong);
  background: var(--bg-inset);
  color: var(--text);
  padding: .45rem .9rem;
  border-radius: var(--radius-sm);
  cursor: pointer;
  transition: background .12s, border-color .12s, color .12s;
  white-space: nowrap;
}
.btn:hover { background: #1c2330; border-color: #45525f; }
.btn-primary { background: var(--accent); border-color: var(--accent); color: #1a1206; font-weight: 600; }
.btn-primary:hover { background: #ffad52; border-color: #ffad52; }
.btn-ghost { background: transparent; }
.btn-danger { border-color: #6e2b28; color: #ff8a80; }
.btn-danger:hover { background: #2a1513; border-color: var(--bad); }
.btn-block { width: 100%; margin-top: auto; }
.icon-btn {
  background: none; border: none; color: var(--text-dim);
  cursor: pointer; font-size: 1rem; padding: .2rem .4rem; border-radius: 4px;
}
.icon-btn:hover { color: var(--text); background: var(--bg-inset); }
.link-btn {
  background: none; border: none; color: var(--info);
  cursor: pointer; padding: 0; font-size: .78rem; text-decoration: none;
}
.link-btn:hover { text-decoration: underline; }

select {
  background: var(--bg-inset);
  color: var(--text);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-sm);
  padding: .4rem .55rem;
  width: 100%;
}
select[multiple] { padding: .25rem; }
/* Without explicit colours the browser paints options with system light
   defaults, which reads as a white block inside the dark sidebar. */
select option { background: var(--bg-inset); color: var(--text); }
select[multiple] option { padding: .22rem .4rem; border-radius: 4px; }
select[multiple] option:checked {
  background: var(--accent) linear-gradient(0deg, var(--accent), var(--accent));
  color: #1a1206;
  font-weight: 600;
}
select:focus-visible, button:focus-visible, input:focus-visible {
  outline: 2px solid var(--accent); outline-offset: 1px;
}
input[type=range] { width: 100%; accent-color: var(--accent); }

/* ---------- landing ---------- */
.landing {
  max-width: 1000px;
  margin: 3rem auto;
  padding: 0 1.25rem;
  display: grid;
  grid-template-columns: 1.4fr 1fr;
  gap: 1.5rem;
  align-items: start;
}
.dropzone {
  border: 2px dashed var(--border-strong);
  border-radius: var(--radius);
  background: var(--bg-raised);
  transition: border-color .15s, background .15s;
}
.dropzone.is-over { border-color: var(--accent); background: #1a1509; }
.dropzone-inner { padding: 3rem 2rem; text-align: center; }
.dropzone-icon { font-size: 2.5rem; display: block; margin-bottom: .75rem; }
.dropzone-inner h2 { margin-bottom: .4rem; font-size: 1.1rem; }
.dropzone-inner p { color: var(--text-dim); font-size: .85rem; margin-bottom: 1.25rem; }
.landing-notes {
  background: var(--bg-raised);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1.25rem;
}
.landing-notes h3 { margin-bottom: .75rem; }
.landing-notes ul { margin: 0; padding-left: 1.1rem; color: var(--text-dim); font-size: .84rem; }
.landing-notes li { margin-bottom: .55rem; }
.landing-notes strong { color: var(--text); }

/* ---------- upload overlay ---------- */
.upload-status {
  position: fixed; inset: 0; z-index: 200;
  background: rgba(5, 8, 12, .8);
  display: flex; align-items: center; justify-content: center;
}
.upload-card {
  background: var(--bg-raised);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 2rem 3rem;
  text-align: center;
  box-shadow: var(--shadow);
}
.spinner {
  width: 32px; height: 32px; margin: 0 auto 1rem;
  border: 3px solid var(--border-strong);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin .8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }
@media (prefers-reduced-motion: reduce) { .spinner { animation-duration: 3s; } }

/* ---------- app shell ---------- */
.app { display: grid; grid-template-columns: 260px 1fr; align-items: start; }

.sidebar {
  background: var(--bg-raised);
  border-right: 1px solid var(--border);
  padding: 1rem;
  display: flex;
  flex-direction: column;
  gap: 1.15rem;
  position: sticky;
  top: 57px;
  height: calc(100vh - 57px);
  overflow-y: auto;
}
.sidebar-head { display: flex; align-items: center; justify-content: space-between; }
.sidebar-head .icon-btn { display: none; }
.filter-group { display: flex; flex-direction: column; gap: .4rem; }
.filter-label {
  font-size: .76rem; text-transform: uppercase; letter-spacing: .06em;
  color: var(--text-dim); font-weight: 600;
  display: flex; justify-content: space-between; align-items: baseline;
}
.filter-actions { display: flex; gap: .7rem; }
.checkbox-list { display: flex; flex-direction: column; gap: .3rem; }
.checkbox-list label {
  display: flex; align-items: center; gap: .45rem;
  font-size: .83rem; color: var(--text-dim); cursor: pointer;
}
.checkbox-list input { accent-color: var(--accent); }
.checkbox-list .count { margin-left: auto; color: var(--text-faint); font-size: .75rem; font-family: var(--mono); }
.filter-summary {
  font-size: .78rem; color: var(--text-dim);
  background: var(--bg-inset); border: 1px solid var(--border);
  border-radius: var(--radius-sm); padding: .55rem .65rem;
}
.sidebar-toggle { display: none; margin-bottom: .75rem; }

.content { padding: 1.25rem; min-width: 0; }

/* ---------- tabs ---------- */
.tabs {
  display: flex; gap: .25rem; margin-bottom: 1.1rem;
  border-bottom: 1px solid var(--border);
  overflow-x: auto; scrollbar-width: thin;
}
.tab {
  background: none; border: none; border-bottom: 2px solid transparent;
  color: var(--text-dim); padding: .55rem .85rem; cursor: pointer;
  white-space: nowrap; font-weight: 500;
}
.tab:hover { color: var(--text); }
.tab.is-active { color: var(--accent); border-bottom-color: var(--accent); }

.panel { display: none; }
.panel.is-active { display: block; }

/* ---------- cards ---------- */
.card {
  background: var(--bg-raised);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1rem 1.1rem;
  margin-bottom: 1.1rem;
}
.card-head {
  display: flex; align-items: flex-start; justify-content: space-between;
  gap: 1rem; flex-wrap: wrap; margin-bottom: .5rem;
}
.card-sub { color: var(--text-faint); font-size: .8rem; margin-top: .15rem; }
.card-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(330px, 1fr)); gap: 1.1rem; }
.card-grid .card { margin-bottom: 0; }
.inline-controls { display: flex; align-items: center; gap: .75rem; flex-wrap: wrap; }
.inline-controls select { width: auto; }
.inline-check {
  display: flex; align-items: center; gap: .4rem;
  font-size: .8rem; color: var(--text-dim); white-space: nowrap;
}
.inline-check input[type=range] { width: 100px; }

/* ---------- KPI tiles ---------- */
.kpi-grid {
  display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
  gap: .8rem; margin-bottom: 1.1rem;
}
.kpi {
  background: var(--bg-raised); border: 1px solid var(--border);
  border-radius: var(--radius); padding: .85rem 1rem;
}
.kpi-label {
  font-size: .72rem; text-transform: uppercase; letter-spacing: .06em;
  color: var(--text-faint); font-weight: 600;
}
.kpi-value { font-size: 1.5rem; font-weight: 600; margin-top: .2rem; font-variant-numeric: tabular-nums; }
.kpi-unit { font-size: .8rem; color: var(--text-dim); font-weight: 400; margin-left: .15rem; }
.kpi-note { font-size: .75rem; color: var(--text-faint); margin-top: .1rem; }
.kpi-good .kpi-value { color: var(--good); }
.kpi-accent .kpi-value { color: var(--accent); }

/* ---------- key/value tables ---------- */
.kv { display: grid; grid-template-columns: auto 1fr; gap: .35rem .9rem; font-size: .83rem; }
.kv dt, .kv .k { color: var(--text-faint); white-space: nowrap; }
.kv dd, .kv .v { margin: 0; font-family: var(--mono); font-size: .8rem; word-break: break-word; }
.kv .empty { grid-column: 1 / -1; color: var(--text-faint); font-style: italic; }

/* ---------- plots ---------- */
.plot { width: 100%; height: 300px; }
.plot-sm { height: 230px; }
.plot-tall { height: 540px; }

/* ---------- episode replay ---------- */
.replay-grid { display: grid; grid-template-columns: 1fr 260px; gap: 1rem; align-items: start; }
.replay-side { display: flex; flex-direction: column; gap: 1rem; }
.episode-select { width: auto; min-width: 180px; }
.scrubber { display: flex; flex-direction: column; gap: .5rem; }
.scrubber #step-readout { font-size: .78rem; color: var(--text-dim); }

/* ---------- data table ---------- */
.table-wrap { overflow-x: auto; max-height: 620px; overflow-y: auto; }
.data-table { width: 100%; border-collapse: collapse; font-size: .82rem; }
.data-table th, .data-table td {
  padding: .4rem .6rem; text-align: right; white-space: nowrap;
  border-bottom: 1px solid var(--border);
}
.data-table th:first-child, .data-table td:first-child { text-align: left; }
.data-table thead th {
  position: sticky; top: 0; background: var(--bg-raised);
  color: var(--text-dim); font-weight: 600; cursor: pointer;
  font-size: .75rem; text-transform: uppercase; letter-spacing: .04em;
  border-bottom: 1px solid var(--border-strong);
}
.data-table thead th:hover { color: var(--text); }
.data-table tbody tr:hover { background: #1b222c; }
.data-table td { font-variant-numeric: tabular-nums; font-family: var(--mono); }
.data-table td:first-child { font-family: var(--font); }
.pill {
  display: inline-block; padding: .1rem .5rem; border-radius: 999px;
  font-size: .72rem; font-family: var(--font);
}
.pill-good { background: rgba(63, 185, 80, .15); color: var(--good); }
.pill-bad { background: rgba(248, 81, 73, .13); color: var(--bad); }
.pill-neutral { background: rgba(154, 167, 180, .13); color: var(--text-dim); }

/* ---------- toast ---------- */
.toast {
  position: fixed; bottom: 1.25rem; left: 50%; transform: translateX(-50%);
  background: var(--bg-raised); border: 1px solid var(--border-strong);
  border-left: 3px solid var(--accent);
  padding: .7rem 1.1rem; border-radius: var(--radius-sm);
  box-shadow: var(--shadow); z-index: 300; font-size: .85rem; max-width: 90vw;
}
.toast.is-error { border-left-color: var(--bad); }

/* ---------- responsive ---------- */
@media (max-width: 1100px) {
  .replay-grid { grid-template-columns: 1fr; }
}

@media (max-width: 900px) {
  .landing { grid-template-columns: 1fr; margin: 1.5rem auto; }
  .app { grid-template-columns: 1fr; }
  .sidebar {
    position: fixed; top: 0; left: 0; bottom: 0; width: 280px; height: 100vh;
    z-index: 150; box-shadow: var(--shadow);
    transform: translateX(-100%); transition: transform .2s ease;
  }
  .sidebar.is-open { transform: translateX(0); }
  .sidebar-head .icon-btn { display: block; }
  .sidebar-toggle { display: inline-block; }
  .plot-tall { height: 420px; }
}

@media (max-width: 560px) {
  body { font-size: 14px; }
  .topbar { padding: .6rem .8rem; }
  .brand-sub { max-width: 100%; white-space: normal; }
  .content { padding: .8rem; }
  .dropzone-inner { padding: 2rem 1rem; }
  .kpi-grid { grid-template-columns: repeat(auto-fit, minmax(130px, 1fr)); }
  .kpi-value { font-size: 1.25rem; }
  .plot { height: 260px; }
  .plot-tall { height: 360px; }
  .card-grid { grid-template-columns: 1fr; }
}
