:root {
  color-scheme: dark;
  --ink: #f8fbff;
  --muted: #c9d3f5;
  --night: #090d27;
  --blue: #506bff;
  --yellow: #ffd34e;
  --pink: #ff6eb4;
  --mint: #72efc5;
  --orange: #ff8a4c;
  --card: rgba(24, 33, 80, .82);
  --line: rgba(255, 255, 255, .14);
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

html, body { height: 100%; margin: 0; }

body {
  color: var(--ink);
  font-family: ui-rounded, "SF Pro Rounded", "Nunito", system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  background:
    radial-gradient(circle at 18% 12%, rgba(80, 107, 255, .32), transparent 30rem),
    radial-gradient(circle at 85% 80%, rgba(255, 110, 180, .16), transparent 26rem),
    linear-gradient(150deg, #090d27 0%, #111b46 58%, #132458 100%);
  background-attachment: fixed;
}

button, input { font: inherit; color: inherit; }
button { cursor: pointer; }
:focus-visible { outline: 3px solid var(--mint); outline-offset: 2px; }

h1, h2, p { margin: 0; }
h2 { font-size: .78rem; font-weight: 900; letter-spacing: .08em; text-transform: uppercase; color: var(--muted); }

.app {
  display: grid;
  grid-template-columns: 300px minmax(0, 1fr) 360px;
  grid-template-rows: auto minmax(0, 1fr);
  grid-template-areas: "top top top" "controls stage data";
  height: 100vh;
  height: 100dvh;
  gap: 12px;
  padding: 12px;
}

/* ---- top bar ---- */
.topbar { grid-area: top; display: flex; align-items: center; gap: 16px; }
.home {
  flex: none;
  padding: .5rem .85rem;
  font-size: .85rem;
  font-weight: 900;
  text-decoration: none;
  color: var(--ink);
  background: rgba(9, 13, 39, .62);
  border: 1px solid var(--line);
  border-radius: 999px;
}
.home:hover { background: rgba(80, 107, 255, .35); }
.heading { flex: 1; min-width: 0; }
.heading h1 { font-size: 1.45rem; font-weight: 950; letter-spacing: -.02em; }
.heading p { color: var(--muted); font-size: .85rem; font-weight: 700; }
.language-switcher { display: inline-flex; padding: 4px; background: rgba(9, 13, 39, .62); border: 1px solid var(--line); border-radius: 999px; }
.language-switcher button { min-width: 44px; padding: .45rem .6rem; font-size: .78rem; font-weight: 950; background: transparent; border: 0; border-radius: 999px; color: var(--muted); }
.language-switcher button[aria-pressed="true"] { color: #111737; background: var(--yellow); }

/* ---- side panels ---- */
.panel { display: flex; flex-direction: column; gap: 12px; min-height: 0; overflow-y: auto; scrollbar-width: thin; }
.controls { grid-area: controls; }
.data { grid-area: data; }
.card { display: grid; gap: 10px; padding: 14px; background: var(--card); border: 1px solid var(--line); border-radius: 18px; }
.hint { color: var(--muted); font-size: .8rem; line-height: 1.4; }

.segmented { display: grid; grid-template-columns: 1fr 1fr; padding: 4px; background: rgba(9, 13, 39, .6); border-radius: 12px; }
.segmented button { padding: .55rem; font-weight: 900; background: transparent; border: 0; border-radius: 9px; color: var(--muted); }
.segmented button[aria-pressed="true"] { color: #111737; background: var(--yellow); }

.slider { display: grid; gap: 6px; font-size: .88rem; font-weight: 800; }
.slider-head { display: flex; justify-content: space-between; gap: 8px; }
.slider output { color: var(--yellow); font-variant-numeric: tabular-nums; white-space: nowrap; }
input[type="range"] { width: 100%; accent-color: var(--yellow); }

.link-button { justify-self: start; padding: 0; font-size: .82rem; font-weight: 900; color: var(--mint); background: none; border: 0; text-decoration: underline; text-underline-offset: 3px; }
.best { padding: .5rem .7rem; font-size: .84rem; font-weight: 800; color: #111737; background: var(--mint); border-radius: 10px; }

.toggle { display: flex; align-items: center; gap: 8px; font-size: .9rem; font-weight: 800; }
.toggle input { width: 18px; height: 18px; accent-color: var(--yellow); }

.challenge p { font-size: .9rem; line-height: 1.45; }
.challenge-head, .challenge-actions { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.challenge .count { font-size: .75rem; font-weight: 800; color: var(--muted); }
.challenge .answer { padding: .6rem .75rem; background: rgba(114, 239, 197, .12); border-left: 3px solid var(--mint); border-radius: 8px; }
.icon-button { width: 34px; height: 34px; font-size: 1rem; font-weight: 900; background: rgba(9, 13, 39, .6); border: 1px solid var(--line); border-radius: 50%; }
.icon-button:hover { background: rgba(80, 107, 255, .4); }

/* ---- 3D stage ---- */
.stage {
  grid-area: stage;
  display: flex;
  flex-direction: column;
  min-height: 0;
  overflow: hidden;
  background: radial-gradient(circle at 50% 40%, rgba(80, 107, 255, .18), rgba(9, 13, 39, .5) 70%);
  border: 1px solid var(--line);
  border-radius: 22px;
}
.viewport { position: relative; flex: 1; min-height: 0; overflow: hidden; }
.stage canvas { display: block; cursor: grab; touch-action: none; }
.stage canvas:active { cursor: grabbing; }

.part-labels { position: absolute; inset: 0; pointer-events: none; }
.part-label {
  position: absolute;
  left: 0;
  top: 0;
  padding: .2rem .5rem;
  font-size: .72rem;
  font-weight: 900;
  white-space: nowrap;
  color: var(--ink);
  background: rgba(9, 13, 39, .72);
  border: 1px solid var(--line);
  border-radius: 999px;
  pointer-events: auto;
}
.part-label:hover, .part-label.active { color: #111737; background: var(--yellow); }

.counters {
  position: absolute;
  top: 12px;
  left: 12px;
  display: flex;
  align-items: center;
  gap: 6px;
  flex-wrap: wrap;
  max-width: calc(100% - 24px);
}
.counters div { display: grid; padding: .35rem .65rem; background: rgba(9, 13, 39, .72); border: 1px solid var(--line); border-radius: 12px; }
.counters b { font-size: 1.1rem; font-variant-numeric: tabular-nums; color: var(--yellow); }
.counters span { font-size: .66rem; font-weight: 800; color: var(--muted); }
.counters .link-button { margin-left: 4px; }

.part-card {
  position: absolute;
  top: 12px;
  right: 12px;
  width: min(280px, calc(100% - 24px));
  display: grid;
  gap: 6px;
  padding: 14px 16px;
  background: rgba(9, 13, 39, .9);
  border: 1px solid var(--yellow);
  border-radius: 16px;
  z-index: 2;
}
.part-card h2 { color: var(--yellow); font-size: .95rem; letter-spacing: 0; text-transform: none; }
.part-card p { font-size: .88rem; line-height: 1.45; }
.part-card .close { position: absolute; top: 6px; right: 8px; font-size: 1.3rem; background: none; border: 0; color: var(--muted); }

.hud {
  display: grid;
  gap: 8px;
  padding: 12px 14px 14px;
  background: rgba(9, 13, 39, .6);
  border-top: 1px solid var(--line);
}
.strokes { display: grid; grid-template-columns: repeat(4, 1fr); gap: 4px; }
.stroke { position: relative; overflow: hidden; padding: .4rem 0; text-align: center; font-size: .8rem; font-weight: 900; color: var(--muted); background: rgba(255, 255, 255, .06); border-radius: 8px; }
.stroke i { position: absolute; inset: 0 auto 0 0; width: 0; opacity: .35; }
.stroke span { position: relative; }
.stroke.active { color: var(--ink); }
.stroke[data-stroke="intake"] i { background: #5f8cff; }
.stroke[data-stroke="compression"] i { background: #c88cff; }
.stroke[data-stroke="power"] i { background: var(--orange); }
.stroke[data-stroke="exhaust"] i { background: #a0a0aa; }
.stroke.active[data-stroke="intake"] { box-shadow: inset 0 0 0 2px #5f8cff; }
.stroke.active[data-stroke="compression"] { box-shadow: inset 0 0 0 2px #c88cff; }
.stroke.active[data-stroke="power"] { box-shadow: inset 0 0 0 2px var(--orange); }
.stroke.active[data-stroke="exhaust"] { box-shadow: inset 0 0 0 2px #a0a0aa; }
.stroke-text { min-height: 2.9em; font-size: .9rem; line-height: 1.45; }
.playback { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.play { min-width: 92px; padding: .5rem 1rem; font-weight: 950; color: #111737; background: var(--yellow); border: 0; border-radius: 999px; }
.speeds { display: inline-flex; margin-left: auto; padding: 3px; background: rgba(255, 255, 255, .06); border-radius: 999px; }
.speeds button { padding: .3rem .55rem; font-size: .78rem; font-weight: 900; background: none; border: 0; border-radius: 999px; color: var(--muted); }
.speeds button[aria-pressed="true"] { color: #111737; background: var(--mint); }
.slowdown { font-size: .74rem; color: var(--muted); }
.sound { padding: .4rem .8rem; font-size: .78rem; font-weight: 900; color: var(--muted); background: rgba(255, 255, 255, .06); border: 1px solid var(--line); border-radius: 999px; }
.sound[aria-pressed="true"] { color: #111737; background: var(--pink); border-color: var(--pink); }

/* ---- data panel ---- */
.tabs { display: grid; grid-template-columns: repeat(3, 1fr); gap: 4px; }
.tabs button { padding: .45rem .3rem; font-size: .76rem; font-weight: 900; background: rgba(255, 255, 255, .05); border: 1px solid transparent; border-radius: 10px; color: var(--muted); }
.tabs button[aria-selected="true"] { color: var(--ink); border-color: var(--yellow); }
.graph { width: 100%; height: 220px; touch-action: none; cursor: ew-resize; }
.graph.pv { cursor: default; }
.legend { display: flex; align-items: center; gap: 6px; font-size: .75rem; font-weight: 800; color: var(--muted); }
.legend span:nth-child(3) { margin-left: 10px; }
.swatch { display: inline-block; width: 14px; height: 4px; border-radius: 2px; }
.swatch.firing, .swatch.torque { background: var(--yellow); }
.swatch.power { background: var(--pink); }
.swatch.motored { background: repeating-linear-gradient(90deg, var(--muted) 0 4px, transparent 4px 7px); }

.verdict { padding: .6rem .75rem; font-size: .86rem; font-weight: 800; line-height: 1.4; border-radius: 10px; background: rgba(255, 255, 255, .06); }
.verdict[data-tone="near"] { color: #111737; background: var(--mint); }
.verdict[data-tone="knock"] { color: #fff; background: #c8325c; }
.verdict[data-tone="late"] { color: #111737; background: var(--orange); }

.readouts { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; margin: 0; }
.readouts div { padding: .5rem .6rem; background: rgba(9, 13, 39, .5); border-radius: 10px; }
.readouts dt { font-size: .7rem; font-weight: 800; color: var(--muted); }
.readouts dd { margin: 2px 0 0; font-size: 1.05rem; font-weight: 950; font-variant-numeric: tabular-nums; }

.energy-bar { display: flex; height: 18px; overflow: hidden; border-radius: 9px; background: rgba(255, 255, 255, .06); }
.energy-bar i { transition: width .3s; }
#barWork, .swatch.work { background: var(--yellow); }
#barFriction, .swatch.friction { background: #c4b5fd; }
#barCoolant, .swatch.coolant { background: #5fb8ff; }
#barExhaust, .swatch.exhaust { background: var(--orange); }
.energy-legend { display: grid; gap: 4px; margin: 0; padding: 0; list-style: none; font-size: .82rem; font-weight: 700; }
.energy-legend li { display: flex; align-items: center; gap: 8px; }
.energy-legend .swatch { width: 12px; height: 12px; border-radius: 3px; }
.energy-legend b { margin-left: auto; font-variant-numeric: tabular-nums; }

/* ---- narrower screens: stack everything and let the page scroll ---- */
@media (max-width: 1100px) {
  .app { grid-template-columns: 280px minmax(0, 1fr); grid-template-areas: "top top" "controls stage" "data data"; grid-template-rows: auto minmax(560px, 1fr) auto; height: auto; }
  .data { display: grid; grid-template-columns: repeat(auto-fit, minmax(300px, 1fr)); align-items: start; overflow: visible; }
}

@media (max-width: 760px) {
  .app { grid-template-columns: minmax(0, 1fr); grid-template-areas: "top" "stage" "controls" "data"; grid-template-rows: auto auto auto auto; padding: 10px 16px 24px; }
  .stage { height: 88vh; min-height: 600px; }
  .controls, .data { overflow: visible; }
  .heading h1 { font-size: 1.15rem; }
  .heading p { display: none; }
  .counters div { padding: .25rem .5rem; }
  .counters b { font-size: .95rem; }
  .part-card { top: auto; bottom: 12px; }
  .hud { padding: 10px; }
  .part-card { bottom: 8px; }
  .stroke { font-size: .7rem; }
  .stroke-text { font-size: .82rem; }
  .speeds { margin-left: 0; }
}
