/* ============================================================
   ThemeLab chassis — structure only. Every visual decision must
   route through a token. If you find yourself writing a raw color
   or font name in this file, it belongs in a theme instead.

   Token contract (every theme must define all of these):
   --bg --bg-image --surface --surface-2 --text --text-muted
   --accent --on-accent --border --grid-line
   --radius --radius-sm --shadow --backdrop-filter
   --font-display --font-body --font-mono
   --label-case --label-tracking
   --chart-1 --chart-2 --chart-3 --chart-4
   --ok --warn --bad --ok-bg --warn-bg --bad-bg
   ============================================================ */

*, *::before, *::after { box-sizing: border-box; }
html, body { margin: 0; }
img, svg { display: block; max-width: 100%; }
button, input, select { font: inherit; color: inherit; }

body {
  background-color: var(--bg);
  background-image: var(--bg-image);
  background-attachment: fixed;
  color: var(--text);
  font-family: var(--font-body);
  font-size: 15px;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
}

:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

/* Panel-level surfaces frost whatever is behind them when a theme
   sets --backdrop-filter (translucent --surface + --bg-image). */
.sidebar, .tile, .card, .chart-tip, .modal, .toast, .lab {
  -webkit-backdrop-filter: var(--backdrop-filter);
  backdrop-filter: var(--backdrop-filter);
}

/* ---------- layout ---------- */

.app {
  display: grid;
  grid-template-columns: 248px 1fr;
  min-height: 100vh;
}

.sidebar {
  display: flex;
  flex-direction: column;
  gap: 28px;
  padding: 22px 18px;
  border-right: 1px solid var(--border);
  background: var(--surface);
  position: sticky;
  top: 0;
  align-self: start;
  height: 100vh;   /* fill the viewport even when nav content is short */
  max-height: 100vh;
  overflow-y: auto;
}

.main {
  padding: 26px 30px 48px;
  max-width: 1160px;
  width: 100%;
}

/* ---------- sidebar ---------- */

.logo { display: flex; align-items: center; gap: 10px; }
.logo-mark {
  width: 14px; height: 14px;
  background: var(--accent);
  border-radius: var(--radius-sm);
}
.logo-word {
  font-family: var(--font-display);
  font-weight: 700;
  font-size: 17px;
  letter-spacing: var(--label-tracking);
  text-transform: var(--label-case);
}

.nav { display: flex; flex-direction: column; gap: 2px; }
.nav-item {
  display: flex; align-items: center; gap: 10px;
  padding: 8px 10px;
  color: var(--text-muted);
  text-decoration: none;
  border-radius: var(--radius-sm);
}
.nav-item:hover { color: var(--text); background: var(--surface-2); }
.nav-item.is-active { color: var(--text); background: var(--surface-2); }
.nav-dot {
  width: 6px; height: 6px; border-radius: 50%;
  background: currentColor; opacity: .45;
}
.nav-item.is-active .nav-dot { background: var(--accent); opacity: 1; }

/* ---------- theme rail (the point of this app) ---------- */

.theme-rail {
  display: flex; flex-direction: column; gap: 10px;
}

/* ---------- floating lab palette ---------- */

.lab {
  position: fixed;
  bottom: 20px;
  right: 20px;
  z-index: 40;
  width: 250px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow), 0 12px 32px rgb(0 0 0 / .22);
  display: flex;
  flex-direction: column;
  max-height: calc(100vh - 40px);
}
.lab-head {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 12px;
  cursor: grab;
  user-select: none;
  touch-action: none;
  border-bottom: 1px solid var(--border);
}
.lab.is-dragging .lab-head { cursor: grabbing; }
.lab-mark {
  width: 12px; height: 12px; flex: none;
  background: var(--accent);
  border-radius: var(--radius-sm);
}
.lab-title {
  flex: 1;
  font-family: var(--font-display);
  font-weight: 700;
  font-size: 13.5px;
  letter-spacing: var(--label-tracking);
  text-transform: var(--label-case);
}
.lab-collapse {
  background: none;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  color: var(--text-muted);
  cursor: pointer;
  width: 24px; height: 24px;
  line-height: 1;
  font-size: 15px;
  display: grid; place-items: center;
}
.lab-collapse:hover { color: var(--text); background: var(--surface-2); }
.lab-body {
  padding: 12px;
  display: flex;
  flex-direction: column;
  gap: 12px;
  overflow-y: auto;
}
.lab.is-collapsed { width: auto; }
.lab.is-collapsed .lab-head { border-bottom: 0; }
.lab.is-collapsed .lab-body { display: none; }
.rail-title {
  margin: 0;
  font-family: var(--font-display);
  font-size: 12px;
  color: var(--text-muted);
  letter-spacing: var(--label-tracking);
  text-transform: var(--label-case);
}
.theme-list { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 6px; }
.theme-item {
  display: flex; align-items: center; gap: 8px;
  padding: 8px 10px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--surface);
  cursor: pointer;
  width: 100%;
  text-align: left;
}
.theme-item:hover { background: var(--surface-2); }
.theme-item.is-current { border-color: var(--accent); background: var(--surface-2); }
.theme-chips { display: flex; gap: 3px; flex: none; }
.theme-chip { width: 10px; height: 18px; border-radius: 2px; border: 1px solid rgba(0,0,0,.15); }
.theme-name { flex: 1; font-size: 13px; }
.theme-star {
  background: none; border: 0; cursor: pointer;
  color: var(--text-muted); font-size: 14px; padding: 2px 4px;
  border-radius: var(--radius-sm);
}
.theme-star.is-default { color: var(--accent); }
.rail-actions { display: flex; gap: 8px; }
.rail-hint { margin: 0; font-size: 11.5px; color: var(--text-muted); }
.rail-hint code { font-family: var(--font-mono); font-size: 11px; }

/* ---------- topbar ---------- */

.topbar {
  display: flex; align-items: flex-end; justify-content: space-between;
  gap: 16px; margin-bottom: 24px;
}
.page-title {
  margin: 0;
  font-family: var(--font-display);
  font-size: 26px;
  font-weight: 700;
  letter-spacing: var(--label-tracking);
  text-transform: var(--label-case);
}
.page-sub { margin: 2px 0 0; color: var(--text-muted); font-size: 13px; }
.topbar-actions { display: flex; gap: 10px; }

/* ---------- tiles ---------- */

.tiles {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 14px;
  margin-bottom: 20px;
}
.tile {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 14px 16px 12px;
  display: flex; flex-direction: column; gap: 6px;
}
.tile-label {
  font-size: 11.5px; color: var(--text-muted);
  letter-spacing: var(--label-tracking);
  text-transform: var(--label-case);
  display: flex; align-items: center; gap: 6px;
}
.tile-swatch { width: 8px; height: 8px; border-radius: 2px; flex: none; }
.tile-value {
  font-family: var(--font-display);
  font-size: 24px; font-weight: 700; line-height: 1.1;
  font-variant-numeric: tabular-nums;
}
.tile-delta { font-size: 12px; color: var(--text-muted); }
.tile-delta.up { color: var(--ok); }
.tile-delta.down { color: var(--bad); }
.tile-spark { margin-top: 2px; }
.tile-spark svg { width: 100%; height: 28px; overflow: visible; }

/* ---------- cards & grids ---------- */

.grid-main {
  display: grid;
  grid-template-columns: 3fr 2fr;
  gap: 14px;
  margin-bottom: 20px;
}
.grid-lower {
  display: grid;
  grid-template-columns: 3fr 2fr;
  gap: 14px;
}
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 16px;
  min-width: 0;
}
.card-head {
  display: flex; align-items: baseline; justify-content: space-between;
  gap: 10px; margin-bottom: 12px;
}
.card-title {
  margin: 0;
  font-family: var(--font-display);
  font-size: 15px; font-weight: 700;
  letter-spacing: var(--label-tracking);
  text-transform: var(--label-case);
}
.card-meta { font-size: 12px; color: var(--text-muted); }

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

.chart-slot { position: relative; height: 220px; }
.chart-slot svg { width: 100%; height: 100%; overflow: visible; }

.chart-grid-line { stroke: var(--grid-line); stroke-width: 1; }
.chart-axis-text {
  fill: var(--text-muted);
  font-family: var(--font-body);
  font-size: 11px;
}
.chart-line { fill: none; stroke: var(--chart-1); stroke-width: 2; stroke-linejoin: round; stroke-linecap: round; }
.chart-area { fill: var(--chart-1); opacity: .12; }
.chart-bar { fill: var(--chart-1); }
.chart-bar-hit { fill: transparent; cursor: default; }
.spark-line { fill: none; stroke-width: 1.5; stroke-linejoin: round; stroke-linecap: round; }

.crosshair { stroke: var(--text-muted); stroke-width: 1; stroke-dasharray: 3 3; }
.crosshair-dot { fill: var(--chart-1); stroke: var(--surface); stroke-width: 2; }

.chart-tip {
  position: absolute;
  pointer-events: none;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  box-shadow: var(--shadow);
  padding: 6px 9px;
  font-size: 12px;
  white-space: nowrap;
  z-index: 5;
  transform: translate(-50%, calc(-100% - 10px));
}
.chart-tip .tip-label { color: var(--text-muted); }
.chart-tip .tip-value { font-weight: 600; font-variant-numeric: tabular-nums; }

/* ---------- table ---------- */

.table-wrap { overflow-x: auto; }
.table { width: 100%; border-collapse: collapse; font-size: 13.5px; }
.table th {
  text-align: left; font-weight: 600;
  font-size: 11.5px; color: var(--text-muted);
  letter-spacing: var(--label-tracking);
  text-transform: var(--label-case);
  padding: 6px 10px;
  border-bottom: 1px solid var(--border);
}
.table td { padding: 9px 10px; border-bottom: 1px solid var(--border); }
.table tr:last-child td { border-bottom: 0; }
.table .t-muted { color: var(--text-muted); }

/* ---------- controls ---------- */

.btn {
  display: inline-flex; align-items: center; gap: 6px;
  padding: 8px 14px;
  border-radius: var(--radius-sm);
  border: 1px solid transparent;
  cursor: pointer;
  font-weight: 600; font-size: 13.5px;
  font-family: var(--font-body);
  letter-spacing: var(--label-tracking);
  text-transform: var(--label-case);
}
.btn:disabled { opacity: .45; cursor: not-allowed; }
.btn-primary { background: var(--accent); color: var(--on-accent); }
.btn-primary:not(:disabled):hover { filter: brightness(1.08); }
.btn-ghost { background: transparent; color: var(--text); border-color: var(--border); }
.btn-ghost:hover { background: var(--surface-2); }
.btn-danger { background: transparent; color: var(--bad); border-color: var(--bad); }
.btn-danger:hover { background: var(--bad-bg); }
.btn-sm { padding: 5px 10px; font-size: 12px; }

.badge {
  display: inline-flex; align-items: center; gap: 5px;
  padding: 3px 9px;
  border-radius: 999px;
  font-size: 12px; font-weight: 600;
  background: var(--surface-2);
  border: 1px solid var(--border);
  color: var(--text-muted);
}
.badge::before { content: ""; width: 6px; height: 6px; border-radius: 50%; background: currentColor; }
.badge-ok { color: var(--ok); background: var(--ok-bg); border-color: transparent; }
.badge-warn { color: var(--warn); background: var(--warn-bg); border-color: transparent; }
.badge-bad { color: var(--bad); background: var(--bad-bg); border-color: transparent; }

.zoo { display: flex; flex-direction: column; gap: 14px; }
.zoo-row { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
.zoo-fields { align-items: stretch; }

.field { display: flex; flex-direction: column; gap: 4px; flex: 1; min-width: 140px; }
.field-label {
  font-size: 11.5px; color: var(--text-muted);
  letter-spacing: var(--label-tracking);
  text-transform: var(--label-case);
}
.input {
  padding: 8px 10px;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  color: var(--text);
  width: 100%;
}
.input::placeholder { color: var(--text-muted); }
.input:hover { border-color: var(--text-muted); }

.switch { display: inline-flex; align-items: center; gap: 8px; cursor: pointer; font-size: 13.5px; }
.switch input { position: absolute; opacity: 0; width: 0; height: 0; }
.switch-track {
  width: 34px; height: 20px; border-radius: 999px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  position: relative;
  transition: background .15s ease;
  flex: none;
}
.switch-track::after {
  content: "";
  position: absolute; top: 2px; left: 2px;
  width: 14px; height: 14px; border-radius: 50%;
  background: var(--text-muted);
  transition: transform .15s ease, background .15s ease;
}
.switch input:checked + .switch-track { background: var(--accent); border-color: var(--accent); }
.switch input:checked + .switch-track::after { transform: translateX(14px); background: var(--on-accent); }
.switch input:focus-visible + .switch-track { outline: 2px solid var(--accent); outline-offset: 2px; }

.progress {
  flex: 1; height: 8px; min-width: 120px;
  background: var(--surface-2);
  border-radius: 999px;
  overflow: hidden;
}
.progress-fill { height: 100%; background: var(--accent); border-radius: 999px; }
.progress-label { font-size: 12px; color: var(--text-muted); }

/* ---------- modal ---------- */

.modal {
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--text);
  box-shadow: var(--shadow);
  padding: 0;
  max-width: 420px;
  width: calc(100% - 40px);
}
.modal::backdrop { background: rgb(0 0 0 / .5); }
.modal-body { padding: 20px; display: flex; flex-direction: column; gap: 10px; }
.modal-text { margin: 0; color: var(--text-muted); font-size: 14px; }
.modal-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 6px; }

/* ---------- toasts ---------- */

.toasts {
  position: fixed; bottom: 20px; right: 20px;
  display: flex; flex-direction: column; gap: 8px;
  z-index: 50;
}
.toast {
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-left: 3px solid var(--accent);
  border-radius: var(--radius-sm);
  box-shadow: var(--shadow);
  padding: 10px 14px;
  font-size: 13.5px;
  animation: toast-in .18s ease;
}
@keyframes toast-in {
  from { opacity: 0; transform: translateY(6px); }
  to   { opacity: 1; transform: none; }
}

/* ---------- skeletons / fake loading ---------- */

.skeleton {
  position: relative;
  overflow: hidden;
  background: var(--surface-2);
  border-radius: var(--radius-sm);
  color: transparent !important;
  user-select: none;
}
.skeleton::after {
  content: "";
  position: absolute; inset: 0;
  background: linear-gradient(100deg, transparent 30%, rgb(255 255 255 / .09) 50%, transparent 70%);
  animation: shimmer 1.1s infinite;
}
@keyframes shimmer {
  from { transform: translateX(-100%); }
  to   { transform: translateX(100%); }
}
.chart-slot[data-loading] { display: grid; place-items: stretch; }

/* ---------- responsive & motion ---------- */

@media (max-width: 980px) {
  .app { grid-template-columns: 1fr; }
  .sidebar { flex-direction: row; flex-wrap: wrap; align-items: flex-start; border-right: 0; border-bottom: 1px solid var(--border); position: static; height: auto; max-height: none; }
  .theme-rail { margin-top: 0; min-width: 220px; }
  .tiles { grid-template-columns: repeat(2, 1fr); }
  .grid-main, .grid-lower { grid-template-columns: 1fr; }
}
@media (max-width: 520px) {
  .tiles { grid-template-columns: 1fr; }
  .topbar { flex-direction: column; align-items: flex-start; }
}

@media (prefers-reduced-motion: reduce) {
  .skeleton::after { animation: none; }
  .toast { animation: none; }
  * { transition: none !important; }
}
