:root {
  --bg: #0e0e10;
  --bg-elev: #18181b;
  --fg: #f5f5f5;
  --fg-muted: #a1a1aa;
  --accent: #8b5cf6;
  --ok: #22c55e;
  --warn: #f59e0b;
  --over: #ef4444;
  --border: #27272a;
  --sans: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  --mono: ui-monospace, SFMono-Regular, "SF Mono", Menlo, Consolas, monospace;
}

@media (prefers-color-scheme: light) {
  :root {
    --bg: #fafafa;
    --bg-elev: #ffffff;
    --fg: #0e0e10;
    --fg-muted: #52525b;
    --accent: #7c3aed;
    --ok: #16a34a;
    --warn: #d97706;
    --over: #dc2626;
    --border: #e4e4e7;
  }
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--fg);
  font-family: var(--sans);
  overflow: hidden;
}

/* --- stage --------------------------------------------------------------- */

#stage {
  position: fixed;
  inset: 0;
  display: flex;
  flex-direction: column;
  justify-content: flex-start;
  align-items: center;
  padding: 4rem 4rem 5rem;
  overflow-y: auto;
  transition: background 300ms ease;
}

#stage > * {
  max-width: 1000px;
  width: 100%;
}

#stage[data-layout="title"] {
  justify-content: center;
  align-items: center;
  text-align: center;
}

#stage[data-layout="title"] > * {
  text-align: center;
}

#stage[data-layout="figure"] {
  justify-content: flex-start;
}

#stage[data-layout="figure"] img,
#stage[data-layout="figure"] figure img {
  max-height: 65vh;
  max-width: 100%;
  object-fit: contain;
  display: block;
  margin: 0 auto;
}

#stage h1 {
  font-size: clamp(2rem, 5vw, 3.5rem);
  font-weight: 700;
  margin: 0 0 1rem;
  line-height: 1.15;
}

#stage h2 {
  font-size: clamp(1.5rem, 3.5vw, 2.25rem);
  font-weight: 600;
  margin: 0 0 0.75rem;
  line-height: 1.2;
}

#stage h3 {
  font-size: clamp(1.25rem, 2.5vw, 1.75rem);
  font-weight: 600;
  margin: 1.5rem 0 0.5rem;
}

#stage p, #stage li {
  font-size: clamp(1.125rem, 2vw, 1.5rem);
  line-height: 1.6;
  margin: 0 0 1rem;
}

#stage ul, #stage ol { padding-left: 1.5rem; }

#stage code {
  font-family: var(--mono);
  background: var(--bg-elev);
  padding: 0.1em 0.3em;
  border-radius: 3px;
  font-size: 0.95em;
}

#stage pre {
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 1rem 1.25rem;
  overflow-x: auto;
  font-size: 1rem;
  line-height: 1.5;
}

#stage pre code { background: transparent; padding: 0; font-size: 1rem; }

#stage blockquote {
  border-left: 3px solid var(--accent);
  padding-left: 1rem;
  margin-left: 0;
  color: var(--fg-muted);
}

#stage figure { margin: 0; }
#stage figcaption {
  font-size: 1rem;
  color: var(--fg-muted);
  text-align: center;
  margin-top: 0.5rem;
}

#stage table {
  border-collapse: collapse;
  width: 100%;
  margin: 0 0 1rem;
  font-size: clamp(1rem, 1.6vw, 1.25rem);
  line-height: 1.5;
}

#stage thead th {
  text-align: left;
  font-weight: 600;
  padding: 0.6rem 0.9rem;
  border-bottom: 2px solid var(--accent);
  color: var(--fg);
}

#stage tbody td {
  padding: 0.55rem 0.9rem;
  border-bottom: 1px solid var(--border);
  vertical-align: top;
}

#stage tbody tr:last-child td { border-bottom: none; }

#stage tbody tr:hover {
  background: color-mix(in srgb, var(--accent) 6%, transparent);
}

#stage svg { display: block; }

#loading {
  font-family: var(--mono);
  color: var(--fg-muted);
}

.error-card {
  background: var(--bg-elev);
  border: 1px solid var(--over);
  border-radius: 8px;
  padding: 2rem;
  max-width: 600px;
}

/* --- HUD ----------------------------------------------------------------- */

#hud {
  position: fixed;
  inset: 0;
  pointer-events: none;
  display: grid;
  grid-template-columns: auto 1fr auto;
  grid-template-rows: auto 1fr auto;
  padding: 1rem;
  z-index: 10;
  font-family: var(--mono);
}

#hud > * { pointer-events: auto; }

#hud-left {
  grid-column: 1;
  grid-row: 1;
  font-size: 0.875rem;
  color: var(--fg-muted);
  padding: 0.5rem 0.75rem;
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: 6px;
}

#hud-center {
  grid-column: 3;
  grid-row: 1;
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

#hud-right {
  grid-column: 3;
  grid-row: 3;
  display: flex;
  gap: 0.5rem;
}

.timer {
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 0.5rem 0.75rem;
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  min-width: 90px;
  transition: color 200ms ease, background 300ms ease;
}

.timer .label {
  font-size: 0.7rem;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--fg-muted);
  font-weight: 500;
}

.timer .value {
  font-size: 1.125rem;
  font-weight: 600;
  font-variant-numeric: tabular-nums;
}

.timer.ok .value { color: var(--ok); }
.timer.warn .value {
  color: var(--warn);
  animation: pulse 2s ease-in-out infinite;
}
.timer.over {
  background: color-mix(in srgb, var(--over) 18%, var(--bg-elev));
}
.timer.over .value { color: var(--over); }

.timer.paused .value::after {
  content: " ⏸";
  color: var(--fg-muted);
  animation: none;
}

@keyframes pulse {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.6; }
}

@media (prefers-reduced-motion: reduce) {
  .timer.warn .value, #progress-bar.over { animation: none !important; }
}

/* --- buttons ------------------------------------------------------------- */

#hud-right button {
  background: var(--bg-elev);
  color: var(--fg);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 0.5rem 0.75rem;
  font-family: var(--mono);
  font-size: 0.875rem;
  cursor: pointer;
  transition: border-color 150ms ease, background 150ms ease;
}

#hud-right button:hover { border-color: var(--accent); }
#hud-right button:active { background: color-mix(in srgb, var(--accent) 12%, var(--bg-elev)); }
#hud-right button:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

/* --- progress bar -------------------------------------------------------- */

#progress {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  height: 4px;
  background: var(--border);
  z-index: 9;
}

#progress-bar {
  height: 100%;
  width: 0%;
  background: var(--accent);
  transition: width 1s linear, background 200ms ease;
}

#progress-bar.over {
  background: var(--over);
  animation: pulse 2s ease-in-out infinite;
}

/* --- dialogs ------------------------------------------------------------- */

dialog {
  background: var(--bg-elev);
  color: var(--fg);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 1.5rem;
  font-family: var(--sans);
  max-width: 90vw;
  max-height: 90vh;
}

dialog::backdrop {
  background: rgba(0, 0, 0, 0.6);
  backdrop-filter: blur(2px);
}

#help h2 { margin: 0 0 1rem; font-size: 1.25rem; }
#help table { border-collapse: collapse; font-family: var(--mono); font-size: 0.95rem; }
#help td { padding: 0.4rem 1.5rem 0.4rem 0; }
#help td:first-child { color: var(--accent); white-space: nowrap; }
#help button { margin-top: 1rem; padding: 0.5rem 1rem; border: 1px solid var(--border); border-radius: 6px; background: var(--bg); color: var(--fg); cursor: pointer; }

#overview { width: 90vw; height: 90vh; }
#overview header {
  font-family: var(--mono);
  font-size: 0.75rem;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--fg-muted);
  margin-bottom: 1rem;
}
#overview-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
  gap: 1rem;
  overflow-y: auto;
  max-height: calc(90vh - 4rem);
  padding-right: 0.5rem;
}

.overview-card {
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 0;
  cursor: pointer;
  text-align: left;
  font: inherit;
  color: inherit;
  overflow: hidden;
  display: flex;
  flex-direction: column;
}

.overview-card.current { border-color: var(--accent); box-shadow: 0 0 0 1px var(--accent); }
.overview-card:hover { border-color: var(--accent); }
.overview-card:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

.overview-thumb {
  height: 140px;
  overflow: hidden;
  position: relative;
  background: var(--bg);
  border-bottom: 1px solid var(--border);
}

.overview-thumb-inner {
  transform: scale(0.25);
  transform-origin: top left;
  width: 400%;
  height: 400%;
  padding: 1rem;
  pointer-events: none;
}

.overview-thumb-inner h1 { font-size: 2rem; margin: 0 0 0.5rem; }
.overview-thumb-inner h2 { font-size: 1.5rem; margin: 0 0 0.5rem; }
.overview-thumb-inner p { font-size: 1rem; margin: 0 0 0.5rem; }

.overview-meta {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.5rem 0.75rem;
  font-size: 0.875rem;
}

.overview-num { font-family: var(--mono); color: var(--fg-muted); }
.overview-title { flex: 1; font-weight: 500; }
.overview-budget {
  font-family: var(--mono);
  font-size: 0.75rem;
  color: var(--fg-muted);
  border: 1px solid var(--border);
  padding: 0.1rem 0.4rem;
  border-radius: 4px;
}

#overview form { position: absolute; top: 1rem; right: 1rem; }
#overview form button {
  padding: 0.4rem 0.8rem;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--bg);
  color: var(--fg);
  cursor: pointer;
  font-family: var(--mono);
}

/* --- responsive ---------------------------------------------------------- */

@media (max-width: 720px) {
  #stage { padding: 3rem 1.5rem 5rem; }
  #hud-center { flex-direction: row; }
  #hud-right button { padding: 0.4rem 0.6rem; }
}

@media (max-width: 480px) {
  #overview-grid { grid-template-columns: 1fr; }
}

/* --- print --------------------------------------------------------------- */

@media print {
  #hud, #progress, dialog { display: none !important; }
  html, body { height: auto; overflow: visible; background: white; color: black; }
  #stage { position: static; padding: 1in; page-break-after: always; }
}
