:root {
  --bg: #0e141a;
  --panel: #1a2330;
  --panel-2: #22303f;
  --ink: #e6edf5;
  --ink-dim: #b0c4d6;
  --ink-mute: #8a9db2;
  --accent: #7dd3fc;
  --accent-ink: #082133;
  --r-over: rgba(239, 68, 68, 0.22);
  --r-helm: rgba(120, 196, 172, 0.16);
  --r-floor: rgba(155, 120, 200, 0.22);
  --line: rgba(230, 237, 245, 0.16);
  --line-strong: rgba(230, 237, 245, 0.75);
  --card: rgba(255, 255, 255, 0.03);
  --radius: 14px;
  --font: ui-sans-serif, -apple-system, BlinkMacSystemFont, "Inter", "Segoe UI", system-ui, sans-serif;
  --mono: ui-monospace, "SF Mono", Menlo, monospace;
}

html.light {
  --bg: #f5f5f0;
  --panel: #ffffff;
  --panel-2: #eeeee8;
  --ink: #1a1a1a;
  --ink-dim: #4a4a4a;
  --ink-mute: #6b6b6b;
  --accent: #0077b6;
  --accent-ink: #ffffff;
  --r-over: rgba(220, 50, 50, 0.18);
  --r-helm: rgba(40, 160, 130, 0.14);
  --r-floor: rgba(120, 80, 180, 0.18);
  --line: rgba(0, 0, 0, 0.15);
  --line-strong: rgba(0, 0, 0, 0.65);
  --card: rgba(0, 0, 0, 0.04);
}
html.light body {
  background: var(--bg);
}
html.light .btn:hover { background: rgba(0, 0, 0, 0.06); }
html.light .chips button:hover { background: rgba(0, 0, 0, 0.06); }
html.light .diagram-wrap { box-shadow: 0 1px 3px rgba(0, 0, 0, 0.08); }
html.light .tour { background: rgba(255, 255, 255, 0.85); }
html.light .tour-card { box-shadow: 0 10px 40px rgba(0, 0, 0, 0.15); }
html.light .tour-close:hover { background: rgba(0, 0, 0, 0.06); }
html.light .tour-dots span { background: rgba(0, 0, 0, 0.15); }
html.light #plot .puck { stroke: #333; filter: drop-shadow(0 2px 6px rgba(0, 119, 182, 0.35)); }
html.light .btn-primary { background: var(--accent); }
html.light .btn-primary:hover { background: #005f8f; }
html.light .audio-status { background: rgba(239, 68, 68, 0.1); color: #b91c1c; border-color: rgba(239, 68, 68, 0.3); }

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
body {
  font-family: var(--font);
  background: radial-gradient(1200px 600px at 20% -10%, #16222e, var(--bg) 60%);
  color: var(--ink);
  min-height: 100vh;
  min-height: 100dvh;
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
}

.app {
  max-width: 1400px;
  margin: 0 auto;
  padding: 18px;
  padding-bottom: max(18px, env(safe-area-inset-bottom));
}

.top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 14px;
}
.top h1 {
  margin: 0;
  font-size: 17px;
  font-weight: 600;
  letter-spacing: 0.005em;
}
.top h1::after {
  content: "a Schelleng workshop";
  color: var(--ink-mute);
  margin-left: 10px;
  font-weight: 400;
  font-size: 13px;
  font-style: italic;
}
.top-actions { display: flex; gap: 8px; flex-shrink: 0; }

.btn {
  appearance: none;
  border: 1px solid var(--line);
  background: var(--card);
  color: var(--ink);
  padding: 9px 14px;
  min-height: 44px;
  border-radius: 10px;
  font: inherit;
  font-size: 13px;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  transition: background .15s, border-color .15s, color .15s;
}
.btn:hover { background: rgba(255, 255, 255, 0.05); }
.btn:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.btn:disabled { opacity: 0.4; cursor: default; }
.btn-audio[aria-pressed="true"] { border-color: var(--accent); color: var(--accent); }
.btn-primary { background: var(--accent); color: var(--accent-ink); border-color: transparent; font-weight: 600; }
.btn-primary:hover { background: #9fdfff; }
.btn-ghost { background: transparent; }

.grid {
  display: grid;
  gap: 14px;
  grid-template-columns: 1fr;
}
@media (min-width: 920px) {
  .grid { grid-template-columns: minmax(0, 1fr) 340px; align-items: start; }
}

.diagram-wrap {
  margin: 0;
  background: var(--panel);
  border-radius: var(--radius);
  padding: 8px;
  aspect-ratio: 1 / 1;
  position: relative;
  overflow: hidden;
  box-shadow: 0 1px 0 rgba(255, 255, 255, 0.03) inset;
}
#plot {
  width: 100%;
  height: 100%;
  display: block;
  touch-action: none;
  user-select: none;
}
.diagram-hint {
  position: absolute;
  bottom: 14px;
  right: 16px;
  font-size: 11px;
  color: var(--ink-mute);
  pointer-events: none;
  transition: opacity .5s;
  font-style: italic;
}
.diagram-hint.faded { opacity: 0; }

.sidebar {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.readout {
  background: var(--panel);
  border-radius: var(--radius);
  padding: 14px 16px;
}
.region-name {
  font-size: 22px;
  font-weight: 600;
  letter-spacing: -0.01em;
  font-variant: small-caps;
}
.coupling {
  margin-top: 6px;
  font-size: 13px;
  line-height: 1.45;
  color: var(--ink-dim);
  min-height: 3.6em;
}
.params {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
  margin: 12px 0 0;
  padding: 0;
}
.params > div {
  background: var(--panel-2);
  border-radius: 8px;
  padding: 8px 10px;
  display: flex;
  flex-direction: column;
  gap: 2px;
}
.params dt { font-size: 11px; color: var(--ink-mute); font-style: italic; margin: 0; }
.params dd {
  margin: 0;
  font-size: 13px;
  font-family: var(--mono);
  display: flex;
  flex-direction: column;
  gap: 1px;
}
.params dd em {
  color: var(--ink-dim);
  font-size: 10px;
  font-style: normal;
  font-family: var(--font);
}

.speed {
  background: var(--panel);
  border-radius: var(--radius);
  padding: 14px 16px;
}
.speed label {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  font-size: 13px;
  color: var(--ink-dim);
  margin-bottom: 8px;
  gap: 8px;
}
.speed label em { color: var(--ink-mute); font-style: italic; font-size: 11px; }
.speed label output { font-weight: 600; color: var(--ink); font-family: var(--mono); }
.speed input[type=range] { width: 100%; accent-color: var(--accent); }
.speed .ticks {
  display: flex;
  justify-content: space-between;
  font-size: 10px;
  color: var(--ink-mute);
  margin-top: 4px;
}

.presets {
  background: var(--panel);
  border-radius: var(--radius);
  padding: 14px 16px;
}
.presets-label {
  font-size: 12px;
  color: var(--ink-mute);
  margin-bottom: 10px;
  font-style: italic;
}
.chips {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}
.chips button {
  appearance: none;
  border: 1px solid var(--line);
  background: transparent;
  color: var(--ink);
  padding: 8px 12px;
  min-height: 40px;
  border-radius: 999px;
  font: inherit;
  font-size: 13px;
  cursor: pointer;
  transition: background .15s, border-color .15s, color .15s, transform .15s;
}
.chips button:hover { background: rgba(255, 255, 255, 0.05); }
.chips button:active { transform: scale(0.97); }
.chips button.active {
  background: var(--accent);
  color: var(--accent-ink);
  border-color: transparent;
  font-weight: 600;
}

.audio-status {
  position: fixed;
  top: 12px;
  left: 50%;
  transform: translateX(-50%);
  background: rgba(239, 68, 68, 0.15);
  color: #fca5a5;
  border: 1px solid rgba(239, 68, 68, 0.35);
  padding: 6px 12px;
  border-radius: 999px;
  font-size: 12px;
  z-index: 40;
}
.hidden { display: none !important; }

/* --- Schelleng plot --- */
#plot .axis { stroke: var(--line); stroke-width: 1; fill: none; }
#plot .grid { stroke: var(--line); stroke-width: 1; stroke-dasharray: 3 4; opacity: 0.65; }
#plot .axis-label { fill: var(--ink-dim); font-family: var(--font); font-size: 12px; }
#plot .tick-label { fill: var(--ink-mute); font-family: var(--mono); font-size: 11px; }
#plot .region-over { fill: var(--r-over); }
#plot .region-helm { fill: var(--r-helm); }
#plot .region-floor { fill: var(--r-floor); }
#plot .region-label {
  fill: var(--ink);
  font-family: var(--font);
  font-size: 12px;
  font-weight: 500;
  opacity: 0.78;
  text-anchor: middle;
  pointer-events: none;
  font-variant: small-caps;
  letter-spacing: 0.03em;
}
#plot .boundary { fill: none; stroke: var(--line-strong); stroke-width: 2.0; }
#plot .boundary-max { }
#plot .boundary-min { stroke-dasharray: 5 4; }
#plot .boundary-label {
  fill: var(--ink-dim);
  font-family: var(--mono);
  font-size: 11px;
  font-style: italic;
  pointer-events: none;
}
#plot .puck {
  fill: var(--accent);
  stroke: #fff;
  stroke-width: 2;
  filter: drop-shadow(0 2px 6px rgba(125, 211, 252, 0.45));
  transition: r 0.15s;
}
#plot .puck-hit { fill: transparent; }
#plot .v-tick { stroke: var(--accent); stroke-width: 2; opacity: 0.6; }
#plot .v-tick-label { fill: var(--accent); font-family: var(--mono); font-size: 9px; }

/* --- Tour --- */
.tour {
  position: fixed;
  inset: 0;
  background: rgba(7, 11, 16, 0.82);
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
  z-index: 50;
}
.tour.hidden { display: none; }
.tour-card {
  background: var(--panel);
  border-radius: 18px;
  padding: 20px;
  width: 100%;
  max-width: 540px;
  max-height: calc(100dvh - 32px);
  overflow: auto;
  position: relative;
  display: flex;
  flex-direction: column;
  gap: 14px;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.5);
}
.tour-close {
  position: absolute;
  top: 8px;
  right: 10px;
  appearance: none;
  border: 0;
  background: transparent;
  color: var(--ink-dim);
  font-size: 26px;
  line-height: 1;
  cursor: pointer;
  width: 36px;
  height: 36px;
  border-radius: 50%;
}
.tour-close:hover { background: rgba(255, 255, 255, 0.05); color: var(--ink); }
.tour-body { min-height: 280px; display: flex; flex-direction: column; gap: 10px; }
.tour-chapter { font-size: 11px; color: var(--ink-mute); letter-spacing: 0.1em; text-transform: uppercase; }
.tour-title { font-size: 22px; font-weight: 600; letter-spacing: -0.01em; line-height: 1.15; }
.tour-text { font-size: 14px; line-height: 1.5; color: var(--ink-dim); }
.tour-visual {
  background: var(--panel-2);
  border-radius: 12px;
  padding: 14px;
  min-height: 160px;
  display: flex;
  align-items: center;
  justify-content: center;
}
.tour-visual svg { width: 100%; height: auto; max-width: 460px; display: block; }
.tour-nav {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  margin-top: auto;
}
.tour-dots { display: flex; gap: 6px; }
.tour-dots span {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.15);
  transition: background .15s;
}
.tour-dots span.active { background: var(--accent); }

/* Tour mini-svg styles */
.mini-string { stroke: #cbd5e1; stroke-width: 2; fill: none; }
.mini-bow { stroke: #fde68a; stroke-width: 3; stroke-linecap: round; }
.mini-bow-fat { stroke-width: 6; opacity: 0.85; }
.mini-label { fill: var(--ink-dim); font: 11px var(--font); }
.mini-label-mono { fill: var(--ink-mute); font: 10px var(--mono); }
.mini-anchor { fill: #64748b; }
.mini-fmax { stroke: #fde68a; stroke-width: 2; fill: none; }
.mini-fmin { stroke: #c4b5fd; stroke-width: 2; stroke-dasharray: 4 3; fill: none; }
.mini-puck { fill: var(--accent); stroke: white; stroke-width: 1.5; }

/* Mobile tweaks */
@media (max-width: 640px) {
  .app { padding: 12px; }
  .top h1 { font-size: 15px; }
  .top h1::after { display: none; }
  .top-actions { gap: 6px; }
  .btn { padding: 8px 10px; font-size: 12px; }
  .btn .label { display: none; }
  .region-name { font-size: 19px; }
  .coupling { min-height: 4.2em; }
  .tour-title { font-size: 19px; }
}
