:root {
  --paper: #f8f7f2;
  --paper-dark: #efede5;
  --paper-light: #fffefa;
  --ink: #24231f;
  --muted: #68645b;
  --line: #cbc6b9;
  --red: #c40000;
  --red-dark: #9d0000;
  --ochre: #9b6d13;
  --blue: #3865b5;
  --teal: #15858e;
  --purple: #8754a1;
  --hud: rgba(255, 254, 250, 0.94);
  --toolbar-h: 82px;
  --timebar-h: 74px;
  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  --serif: Georgia, "Times New Roman", serif;
}

:root[data-theme="dark"] {
  --paper: #171815;
  --paper-dark: #20211d;
  --paper-light: #1b1c19;
  --ink: #ece8de;
  --muted: #aaa59a;
  --line: #45463f;
  --red: #f07168;
  --red-dark: #f28a82;
  --ochre: #d3aa57;
  --blue: #7194df;
  --teal: #50b9be;
  --purple: #b68ace;
  --hud: rgba(27, 28, 25, 0.94);
}

*, *::before, *::after { box-sizing: border-box; }

html, body {
  height: 100%;
  margin: 0;
  background: var(--paper);
  color: var(--ink);
  font: 14px/1.45 var(--font);
  overflow: hidden;
  -webkit-font-smoothing: antialiased;
}

button, input, select { font: inherit; color: inherit; }
button {
  min-height: 34px;
  padding: 6px 11px;
  border: 1px solid var(--line);
  border-radius: 0;
  background: var(--paper-light);
  cursor: pointer;
}
button:hover:not(:disabled) { border-color: var(--red); background: var(--paper-dark); }
button:focus-visible, input:focus-visible, select:focus-visible {
  outline: 2px solid var(--red);
  outline-offset: 2px;
}
button[aria-pressed="true"] { background: var(--red-dark); border-color: var(--red); color: #fffefa; }
button:disabled { cursor: default; opacity: 0.42; }
a { color: var(--red-dark); text-underline-offset: 3px; }
var, .math { font-family: var(--serif); }
sup, sub { line-height: 0; }

#app {
  display: grid;
  grid-template-rows: var(--toolbar-h) minmax(0, 1fr) auto;
  width: 100vw;
  height: 100vh;
}

#toolbar {
  z-index: 30;
  display: flex;
  align-items: center;
  gap: 12px;
  min-width: 0;
  padding: 8px 18px;
  background: var(--paper-light);
  border-bottom: 1px solid var(--line);
}

.brand { display: flex; align-items: center; gap: 10px; min-width: 280px; }
.brand-copy { min-width: 0; }
.back-link { display: block; width: max-content; margin-bottom: 2px; color: var(--muted); font-size: 11px; }
.brand h1 { margin: 0; font: normal 25px/1 var(--serif); white-space: nowrap; }
.subtitle {
  max-width: 430px;
  margin-top: 4px;
  overflow: hidden;
  color: var(--muted);
  font-size: 11px;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.mode-switch { display: inline-flex; margin: 0 auto; border: 1px solid var(--line); flex-shrink: 0; }
.mode-switch button { border: 0; border-right: 1px solid var(--line); background: transparent; white-space: nowrap; }
.mode-switch button:last-child { border-right: 0; }
.mode-switch button[aria-pressed="true"] { background: var(--red-dark); color: #fffefa; }
.toolbar-actions { display: flex; gap: 6px; margin-left: auto; }
.toolbar-actions button { white-space: nowrap; }
.icon { display: inline-block; min-width: 1em; margin-right: 4px; text-align: center; }

#stage {
  position: relative;
  min-height: 0;
  overflow: hidden;
  background: var(--paper-dark);
}
#main-canvas { display: block; width: 100%; height: 100%; touch-action: none; }

.hud {
  position: absolute;
  z-index: 5;
  border: 1px solid var(--line);
  background: var(--hud);
  box-shadow: none;
  pointer-events: none;
}
.legend { right: 18px; bottom: 18px; width: 248px; padding: 10px 12px; }
.legend-title { margin-bottom: 7px; font: 14px/1.2 var(--serif); }
.hidden-strip {
  height: 10px;
  border: 1px solid color-mix(in srgb, var(--line), transparent 25%);
  background: linear-gradient(90deg, #15858e 0%, #3865b5 25%, #8754a1 50%, #c18a25 75%, #15858e 100%);
}
.hidden-labels { display: flex; justify-content: space-between; margin-top: 3px; color: var(--muted); font: 10px/1 var(--serif); }
.real-key { display: flex; align-items: center; gap: 7px; margin-top: 10px; font-size: 12px; }
.real-line { width: 34px; height: 2px; background: var(--red); box-shadow: 0 0 0 2px color-mix(in srgb, var(--red), transparent 87%); }

#timebar {
  display: none;
  grid-template-columns: auto minmax(220px, 1fr) auto;
  align-items: center;
  gap: 16px;
  min-height: var(--timebar-h);
  padding: 10px 18px;
  border-top: 1px solid var(--line);
  background: var(--paper-light);
}
#timebar.visible { display: grid; }
.time-buttons { display: flex; gap: 5px; }
.time-buttons button { width: 36px; padding: 0; }
#slice-zero { width: auto; padding-inline: 10px; }
.slider-wrap { display: grid; grid-template-columns: minmax(120px, 1fr) auto; align-items: center; gap: 12px; }
.slider-wrap output { min-width: 112px; color: var(--red-dark); font: 13px/1 var(--serif); }
input[type="range"] {
  width: 100%;
  height: 6px;
  appearance: none;
  border-radius: 0;
  background: linear-gradient(90deg, var(--teal), var(--blue) 33%, var(--red) 50%, var(--ochre) 68%, var(--teal));
}
input[type="range"]::-webkit-slider-thumb {
  width: 17px;
  height: 17px;
  appearance: none;
  border: 2px solid var(--paper-light);
  border-radius: 50%;
  background: var(--red);
  box-shadow: 0 0 0 1px var(--red-dark);
}
input[type="range"]::-moz-range-thumb {
  width: 13px;
  height: 13px;
  border: 2px solid var(--paper-light);
  border-radius: 50%;
  background: var(--red);
}
.time-options { display: flex; align-items: center; gap: 10px; color: var(--muted); font-size: 12px; }
.time-options label { display: inline-flex; align-items: center; gap: 5px; white-space: nowrap; }
select { padding: 5px 7px; border: 1px solid var(--line); border-radius: 0; background: var(--paper-light); }

#toast {
  position: fixed;
  left: 50%;
  bottom: 92px;
  z-index: 100;
  padding: 8px 12px;
  border: 1px solid var(--line);
  background: var(--paper-light);
  opacity: 0;
  transform: translate(-50%, 8px);
  transition: opacity 0.16s, transform 0.16s;
  pointer-events: none;
}
#toast.show { opacity: 1; transform: translate(-50%, 0); }
.sr-only { position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px; overflow: hidden; clip: rect(0, 0, 0, 0); white-space: nowrap; border: 0; }

@media (max-width: 980px) {
  :root { --toolbar-h: 118px; }
  #toolbar { align-content: center; flex-wrap: wrap; padding: 8px 12px; }
  .brand { flex: 1 1 360px; min-width: 240px; }
  .mode-switch { order: 3; margin: 0; }
  .toolbar-actions { order: 2; }
  .mode-switch button { padding-inline: 10px; }
}

@media (max-width: 680px) {
  :root { --toolbar-h: 164px; --timebar-h: 116px; }
  .brand { flex-basis: calc(100% - 50px); min-width: 0; }
  .brand h1 { font-size: 21px; }
  .subtitle { max-width: 270px; }
  .toolbar-actions { margin-left: auto; }
  .toolbar-actions .label { display: none; }
  .toolbar-actions button { width: 34px; padding: 0; }
  .toolbar-actions .icon { margin: 0; }
  .mode-switch { width: 100%; }
  .mode-switch button { flex: 1; padding: 6px 5px; font-size: 12px; }
  .legend { right: 10px; bottom: 10px; width: 190px; }
  #timebar { grid-template-columns: auto 1fr; gap: 8px 10px; padding: 9px 10px; }
  .slider-wrap { grid-template-columns: 1fr; gap: 6px; }
  .time-options { grid-column: 1 / -1; justify-content: flex-end; }
  #toast { bottom: 128px; }
}

@media (prefers-reduced-motion: reduce) {
  * { scroll-behavior: auto !important; transition-duration: 0.01ms !important; }
}
