:root {
  --ink: #eaf3ff;
  --muted: #a9bfdc;
  --card: rgba(9, 24, 48, 0.82);
  --line: rgba(160, 200, 255, 0.22);
  --accent: #ffd43b;
  --accent-2: #4dabf7;
  --danger: #ff8a7a;
  --ok: #69db7c;
  --radius: 16px;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  overflow: hidden;
  background: #07142b;
  color: var(--ink);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-font-smoothing: antialiased;
}

#scene {
  position: fixed;
  inset: 0;
  width: 100vw;
  height: 100vh;
  display: block;
  touch-action: none;
}

.hud {
  position: fixed;
  inset: 0;
  pointer-events: none;
  padding: 16px;
  display: grid;
  grid-template-rows: auto auto 1fr auto;
  gap: 10px;
}

.hud button, .hud input, .hud .panel { pointer-events: auto; }

.top {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 12px 18px;
  padding: 12px 16px;
  border-radius: var(--radius);
  background: var(--card);
  border: 1px solid var(--line);
  backdrop-filter: blur(8px);
  position: relative;
  overflow: hidden;
}

.titles { display: flex; align-items: center; gap: 12px; min-width: 0; }
.titles h1 { margin: 0; font-size: clamp(1rem, 2.2vw, 1.35rem); font-weight: 700; white-space: nowrap; }

.chip {
  padding: 4px 10px;
  border-radius: 999px;
  background: linear-gradient(90deg, #ffd43b, #ff9f43);
  color: #2a1a00;
  font-size: 0.78rem;
  font-weight: 700;
  white-space: nowrap;
}

.equation {
  font-family: ui-monospace, "Cascadia Mono", Consolas, monospace;
  font-size: 1.05rem;
  color: var(--accent-2);
  font-weight: 700;
}

.sp::before { content: ''; display: inline-block; width: 0.8em; height: 0.8em; margin-right: 0.3em; border-radius: 50%; vertical-align: -0.05em; box-shadow: inset -2px -2px 3px rgba(0,0,0,.3); }
.sp.a::before { background: #ff6b6b; }
.sp.b::before { background: #4dabf7; }
.sp.ab::before { background: #ffd43b; }
.sp.k { margin-left: 14px; font-family: system-ui, sans-serif; font-weight: 600; font-size: 0.85rem; color: #d0bfff; }
.sp.k::before { background: #b197fc; }
.sp.k[hidden] { display: none; }
.counters { margin-left: auto; display: flex; gap: 10px; }
.counter {
  display: flex;
  flex-direction: column;
  align-items: center;
  min-width: 92px;
  padding: 6px 10px;
  border-radius: 12px;
  background: rgba(255, 255, 255, 0.06);
  font-size: 0.72rem;
  color: var(--muted);
}
.counter b { font-size: 1.25rem; color: var(--ink); font-variant-numeric: tabular-nums; }

.progress {
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  height: 4px;
  background: rgba(255, 255, 255, 0.08);
}
.progress i {
  display: block;
  height: 100%;
  width: 0;
  background: linear-gradient(90deg, #ffd43b, #69db7c);
  transition: width 0.4s ease;
}

.goal {
  align-self: start;
  justify-self: start;
  margin: 0;
  max-width: min(560px, 92vw);
  padding: 10px 14px;
  border-radius: 12px;
  background: rgba(9, 24, 48, 0.7);
  border-left: 4px solid var(--accent);
  font-size: 0.95rem;
  line-height: 1.4;
}

.panel {
  justify-self: end;
  align-self: end;
  width: min(330px, 100%);
  display: grid;
  gap: 12px;
  padding: 14px;
  border-radius: var(--radius);
  background: var(--card);
  border: 1px solid var(--line);
  backdrop-filter: blur(8px);
}

.control { display: grid; gap: 6px; font-size: 0.9rem; }
.control label { display: flex; justify-content: space-between; align-items: baseline; }
.control output { font-variant-numeric: tabular-nums; font-weight: 700; color: var(--accent); }
.control small { color: var(--muted); font-size: 0.74rem; }
.control[hidden] { display: none; }

input[type="range"] {
  width: 100%;
  accent-color: #ff9f43;
  height: 28px;
}
input[type="range"]:disabled { opacity: 0.45; }

.conc-row { display: flex; align-items: center; justify-content: space-between; gap: 10px; }
.conc-row span { font-variant-numeric: tabular-nums; font-weight: 700; color: var(--accent-2); }

.switch {
  width: 100%;
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 10px;
  padding: 10px 12px;
  border-radius: 12px;
  border: 1px solid var(--line);
  background: rgba(255, 255, 255, 0.05);
  color: var(--ink);
  font: inherit;
  cursor: pointer;
}
.switch[aria-checked="true"] { border-color: #b197fc; background: rgba(177, 151, 252, 0.18); }
.switch b { color: var(--muted); }
.switch[aria-checked="true"] b { color: #d0bfff; }

.hist-wrap { display: grid; gap: 6px; }
.hist-title { font-size: 0.78rem; color: var(--muted); }
#hist { width: 100%; height: 112px; display: block; border-radius: 10px; background: rgba(255, 255, 255, 0.04); }
.stats { font-size: 0.8rem; color: var(--ink); font-variant-numeric: tabular-nums; }
.units { margin: 0; font-size: 0.7rem; color: var(--muted); }

.actions { justify-self: start; align-self: end; display: flex; gap: 8px; }

.btn {
  font: inherit;
  font-weight: 700;
  padding: 10px 16px;
  border-radius: 999px;
  border: 1px solid var(--line);
  color: var(--ink);
  background: rgba(255, 255, 255, 0.08);
  cursor: pointer;
  transition: transform 0.12s ease, background 0.2s ease;
}
.btn:hover { background: rgba(255, 255, 255, 0.16); }
.btn:active { transform: scale(0.97); }
.btn.primary {
  border: none;
  color: #2a1a00;
  background: linear-gradient(90deg, #ffd43b, #ff9f43);
  box-shadow: 0 8px 22px rgba(255, 159, 67, 0.35);
}
.btn.primary:hover { background: linear-gradient(90deg, #ffe066, #ffb36b); }
.btn.ghost { font-size: 0.85rem; padding: 8px 14px; }

button:focus-visible, input:focus-visible {
  outline: 3px solid #ffffff;
  outline-offset: 2px;
}

.overlay {
  position: fixed;
  inset: 0;
  display: grid;
  place-items: center;
  padding: 16px;
  background: rgba(3, 10, 24, 0.55);
  backdrop-filter: blur(3px);
  z-index: 5;
}
.overlay[hidden] { display: none; }

.card {
  width: min(520px, 100%);
  padding: 26px 26px 22px;
  border-radius: 22px;
  background: linear-gradient(160deg, rgba(20, 44, 84, 0.96), rgba(8, 20, 42, 0.96));
  border: 1px solid var(--line);
  box-shadow: 0 30px 80px rgba(0, 0, 0, 0.45);
  animation: pop-in 0.35s cubic-bezier(0.2, 1.3, 0.4, 1);
}
.card h2 { margin: 0 0 10px; font-size: clamp(1.3rem, 3vw, 1.7rem); }
.card p { margin: 0 0 14px; line-height: 1.5; color: var(--ink); }
.card .hint { color: var(--accent); font-size: 0.92rem; }
.lessons { margin: 0 0 16px; padding-left: 20px; line-height: 1.5; }
.lessons li { margin-bottom: 8px; }

@keyframes pop-in {
  from { transform: scale(0.9); opacity: 0; }
  to { transform: scale(1); opacity: 1; }
}

@media (max-width: 760px) {
  .hud { padding: 10px 10px 58px; gap: 8px; }
  .sp::before { content: ''; display: inline-block; width: 0.8em; height: 0.8em; margin-right: 0.3em; border-radius: 50%; vertical-align: -0.05em; box-shadow: inset -2px -2px 3px rgba(0,0,0,.3); }
.sp.a::before { background: #ff6b6b; }
.sp.b::before { background: #4dabf7; }
.sp.ab::before { background: #ffd43b; }
.sp.k { margin-left: 14px; font-family: system-ui, sans-serif; font-weight: 600; font-size: 0.85rem; color: #d0bfff; }
.sp.k::before { background: #b197fc; }
.sp.k[hidden] { display: none; }
.counters { margin-left: 0; width: 100%; }
  .counter { flex: 1; }
  .equation { order: 3; }
  .panel {
    justify-self: stretch;
    width: 100%;
    gap: 10px;
    padding: 12px;
    max-height: 44vh;
    overflow-y: auto;
  }
  .goal { font-size: 0.85rem; }
  .hud { grid-template-rows: auto auto 1fr auto; }
  .actions { position: fixed; right: 12px; bottom: 12px; left: auto; }
  #hist { height: 84px; }
}

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