:root {
  color-scheme: dark;
  --bg: #07080d;
  --panel: rgba(16, 19, 28, 0.9);
  --line: rgba(255, 255, 255, 0.1);
  --line-strong: rgba(255, 255, 255, 0.16);
  --text: #f6f8fb;
  --muted: #c2cad8;
  --soft: #e6ebf4;
  --cyan: #8eecf8;
  --cyan-ink: #062028;
  --violet: #d4c4fe;
  --green: #3ee0a2;
  --green-ink: #05281a;
  --danger: #fb7185;
  --danger-ink: #ffe4e8;
  --sans: ui-sans-serif, system-ui, -apple-system, "Segoe UI", sans-serif;
  --mono: ui-monospace, "SFMono-Regular", "Cascadia Code", Menlo, Consolas, monospace;
  --ease-out: cubic-bezier(0.23, 1, 0.32, 1);
  --ease-in-out: cubic-bezier(0.77, 0, 0.175, 1);
  --shadow: 0 12px 32px rgba(0, 0, 0, 0.32);
  --edge: inset 0 1px 0 rgba(255, 255, 255, 0.07);
}

* { box-sizing: border-box; }

html, body, #root {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: var(--sans);
  font-size: 16px;
  line-height: 1.5;
}

html { -webkit-tap-highlight-color: transparent; }

html, body { overscroll-behavior: none; }

body { overflow: hidden; }

.app, .top, .dock, .controls, .fields, .modes, .about, .brand, .readout { min-width: 0; max-width: 100%; }

button, input { font: inherit; color: inherit; }

button {
  touch-action: manipulation;
  -webkit-user-select: none;
  user-select: none;
}

:focus { outline: none; }

:focus-visible {
  outline: 2px solid var(--cyan);
  outline-offset: 2px;
}

.app {
  height: 100dvh;
  display: grid;
  grid-template-rows: auto minmax(0, 1fr) auto;
}

.top {
  display: flex;
  gap: 16px;
  align-items: stretch;
  padding: 16px 20px 0;
  z-index: 2;
}

.brand, .about, .dock {
  background: var(--panel);
  border: 1px solid var(--line);
  -webkit-backdrop-filter: blur(18px) saturate(1.4);
  backdrop-filter: blur(18px) saturate(1.4);
}

.brand, .about {
  border-radius: 16px;
  box-shadow: var(--shadow), var(--edge);
}

.brand {
  padding: 14px 16px 15px;
  min-width: 232px;
  display: flex;
  flex-direction: column;
  justify-content: center;
}

.brand h1 {
  margin: 0;
  font-size: 1.25rem;
  font-weight: 600;
  letter-spacing: -0.03em;
  line-height: 1.15;
  color: var(--text);
}

.brand p {
  margin: 5px 0 0;
  color: var(--muted);
  font-size: 0.8125rem;
  line-height: 1.4;
  letter-spacing: 0;
}

.about {
  margin-left: auto;
  max-width: 460px;
  padding: 14px 16px 15px 18px;
}

.about h2 {
  margin: 0 0 6px;
  font-size: 0.95rem;
  font-weight: 600;
  letter-spacing: -0.02em;
  line-height: 1.2;
  text-transform: none;
  color: var(--text);
}

.about h2::before {
  content: "";
  display: block;
  width: 28px;
  height: 3px;
  margin-bottom: 10px;
  border-radius: 99px;
  background: linear-gradient(90deg, var(--cyan), var(--violet));
}

.about p {
  margin: 0;
  color: var(--soft);
  font-size: 0.875rem;
  line-height: 1.5;
  letter-spacing: 0;
  text-wrap: pretty;
}

.stage { position: relative; min-height: 0; }

.stage canvas { display: block; width: 100%; height: 100%; }

.scene-fallback, .stage-note {
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  display: grid;
  place-items: center;
  padding: 24px;
  text-align: center;
  font-size: 0.95rem;
  line-height: 1.45;
}

.scene-fallback {
  color: var(--muted);
  background: rgba(7, 8, 13, 0.35);
}

.scene-fallback:not([role="alert"]) {
  animation: status-fade 1.1s var(--ease-in-out) infinite;
}

.stage-note {
  color: var(--danger-ink);
  background: rgba(7, 8, 13, 0.62);
}

.typing-overlay {
  position: absolute;
  left: 50%;
  top: 42%;
  transform: translate(-50%, -50%);
  text-align: center;
  pointer-events: none;
  text-shadow: 0 8px 30px rgba(0, 0, 0, 0.65);
}

.typing-i {
  font-family: var(--mono);
  font-size: clamp(2.4rem, 8vw, 5rem);
  font-weight: 700;
  letter-spacing: -0.04em;
  line-height: 1;
  color: var(--cyan);
  font-variant-numeric: tabular-nums;
}

.typing-cond {
  margin-top: 8px;
  font-family: var(--mono);
  color: var(--muted);
  font-variant-numeric: tabular-nums;
}

.dock {
  margin: 0 20px 16px;
  padding: 16px 16px 12px;
  border-radius: 20px;
  display: flex;
  flex-direction: column;
  gap: 14px;
  z-index: 2;
  box-shadow: 0 18px 48px rgba(0, 0, 0, 0.4), var(--edge);
}

.readout {
  display: flex;
  flex-wrap: wrap;
  gap: 6px 16px;
  align-items: baseline;
  justify-content: space-between;
}

.formula {
  margin: 0;
  font-family: var(--mono);
  font-size: 1rem;
  font-weight: 600;
  letter-spacing: -0.02em;
  line-height: 1.35;
  overflow-wrap: anywhere;
  font-variant-numeric: tabular-nums;
}

.count {
  margin: 0;
  color: var(--muted);
  font-size: 0.8125rem;
  font-family: var(--mono);
  letter-spacing: 0;
  font-variant-numeric: tabular-nums;
}

.track {
  height: 6px;
  border-radius: 99px;
  background: rgba(255, 255, 255, 0.1);
  overflow: hidden;
}

.track span {
  display: block;
  height: 100%;
  border-radius: inherit;
  background: linear-gradient(90deg, var(--cyan), var(--violet));
}

.controls {
  display: flex;
  flex-wrap: wrap;
  gap: 16px;
  align-items: end;
}

.transport { display: flex; gap: 8px; align-items: center; }

.icon-btn {
  width: 48px;
  height: 48px;
  border-radius: 999px;
  border: 1px solid var(--line-strong);
  background: rgba(255, 255, 255, 0.04);
  display: grid;
  place-items: center;
  cursor: pointer;
  transition: transform 160ms var(--ease-out), background-color 160ms var(--ease-out), border-color 160ms var(--ease-out), color 160ms var(--ease-out);
}

.icon-btn.play {
  background: var(--green);
  color: var(--green-ink);
  border-color: transparent;
}

.icon-btn.play svg { fill: currentColor; }

.icon-btn.play.is-on {
  background: #3c1824;
  color: var(--danger);
  border-color: rgba(251, 113, 133, 0.45);
}

.icon-btn:disabled { opacity: 0.38; cursor: not-allowed; }

.icon-btn:active:not(:disabled) { transform: scale(0.97); }

.fields {
  display: grid;
  grid-template-columns: repeat(3, 5.75rem) minmax(10rem, 1fr);
  gap: 12px 14px;
  flex: 1;
  align-items: end;
}

.field { display: flex; flex-direction: column; gap: 6px; min-width: 0; }

.field span, .delay-label {
  font-family: var(--sans);
  font-size: 0.6875rem;
  font-weight: 600;
  letter-spacing: 0.08em;
  line-height: 1.2;
  color: var(--muted);
}

.field input[type="number"] {
  width: 100%;
  background: rgba(0, 0, 0, 0.35);
  border: 1px solid var(--line-strong);
  border-radius: 10px;
  padding: 8px 6px;
  text-align: center;
  font-family: var(--mono);
  font-size: 0.95rem;
  font-variant-numeric: tabular-nums;
  transition: border-color 160ms var(--ease-out), background-color 160ms var(--ease-out);
}

.field input[type="range"] {
  width: 100%;
  height: 28px;
  margin: 0;
  accent-color: var(--cyan);
  cursor: pointer;
}

.problem {
  margin: 0;
  color: var(--danger-ink);
  font-size: 0.875rem;
  line-height: 1.4;
  background: rgba(251, 113, 133, 0.12);
  border: 1px solid rgba(251, 113, 133, 0.4);
  border-radius: 10px;
  padding: 8px 12px;
}

.modes {
  display: flex;
  gap: 8px;
  overflow-x: auto;
  overscroll-behavior-x: contain;
  padding: 2px;
  scroll-snap-type: x proximity;
  scroll-padding-inline: 8px;
  scrollbar-width: thin;
  scrollbar-color: rgba(255, 255, 255, 0.28) transparent;
}

.mode {
  scroll-snap-align: start;
  flex: 0 0 auto;
  min-width: 76px;
  min-height: 62px;
  border-radius: 14px;
  border: 1px solid transparent;
  background: rgba(255, 255, 255, 0.035);
  color: var(--muted);
  padding: 8px 6px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 6px;
  cursor: pointer;
  transition: transform 160ms var(--ease-out), background-color 160ms var(--ease-out), color 160ms var(--ease-out), border-color 160ms var(--ease-out);
}

.mode span {
  font-size: 0.65rem;
  font-weight: 600;
  letter-spacing: 0.05em;
  line-height: 1.1;
  text-transform: uppercase;
}

.mode.is-on {
  color: var(--cyan-ink);
  background: var(--cyan);
  border-color: transparent;
}

.mode:active { transform: scale(0.97); }

.hint-row {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  align-items: center;
}

.hint, .linkish {
  color: var(--muted);
  font-size: 0.75rem;
  line-height: 1.4;
}

.hint { letter-spacing: 0; }

.linkish {
  background: rgba(255, 255, 255, 0.04);
  border: 1px solid var(--line);
  border-radius: 999px;
  padding: 6px 12px;
  cursor: pointer;
  text-decoration: none;
  transition: transform 160ms var(--ease-out), background-color 160ms var(--ease-out), color 160ms var(--ease-out);
}

.linkish:active { transform: scale(0.97); }

@keyframes rise {
  from { opacity: 0; transform: translateY(8px); }
  to { opacity: 1; transform: none; }
}

@keyframes status-fade {
  50% { opacity: 0.55; }
}

.brand { animation: rise 240ms var(--ease-out) both; }
.about { animation: rise 240ms var(--ease-out) 50ms both; }
.dock { animation: rise 240ms var(--ease-out) 90ms both; }

@media (hover: hover) and (pointer: fine) {
  .icon-btn:hover:not(:disabled):not(.play) { background: rgba(255, 255, 255, 0.09); }
  .icon-btn.play:hover:not(:disabled) { background: #67ebbb; }
  .icon-btn.play.is-on:hover:not(:disabled) { background: #54202d; }
  .mode:hover:not(.is-on) {
    background: rgba(255, 255, 255, 0.07);
    color: var(--text);
  }
  .linkish:hover {
    color: var(--text);
    background: rgba(255, 255, 255, 0.08);
  }
}

@media (min-width: 1100px) {
  .modes { overflow-x: visible; }
  .mode { flex: 1 1 0; min-width: 0; }
}

@media (pointer: coarse) {
  .field input[type="number"] { font-size: 16px; }
}

@media (max-width: 800px) {
  body { overflow-x: hidden; overflow-y: auto; }
  .app { height: auto; min-height: 100dvh; }
  .top {
    flex-direction: column;
    gap: 10px;
    padding: max(12px, env(safe-area-inset-top, 0px)) 12px 0;
  }
  .about { margin-left: 0; max-width: none; }
  .stage { height: 39dvh; min-height: 210px; }
  .hint { display: none; }
  .hint-row { justify-content: flex-end; }
  .brand { min-width: 0; padding: 12px 14px; }
  .brand h1 { font-size: 1.125rem; }
  .about { padding: 12px 14px; }
  .about h2::before { margin-bottom: 8px; }
  .fields { grid-template-columns: repeat(3, minmax(0, 1fr)); }
  .field.grow { grid-column: 1 / -1; }
  .dock {
    margin: 0 12px max(10px, env(safe-area-inset-bottom, 0px));
    padding: 12px 12px 8px;
    gap: 10px;
  }
  .mode { min-width: 5.6rem; min-height: 52px; }
}

@media (max-width: 420px) {
  .formula { font-size: 0.8125rem; }
}

@media (prefers-reduced-motion: reduce) {
  .brand, .about, .dock, .scene-fallback:not([role="alert"]) { animation: none; }
  .icon-btn, .mode, .linkish, .field input[type="number"] {
    transition: background-color 160ms ease, color 160ms ease, border-color 160ms ease, opacity 160ms ease;
  }
  .icon-btn:active, .mode:active, .linkish:active { transform: none; }
}

@media (prefers-reduced-transparency: reduce) {
  .brand, .about, .dock {
    background: #12151c;
    -webkit-backdrop-filter: none;
    backdrop-filter: none;
  }
}

@media (prefers-contrast: more) {
  .brand, .about, .dock {
    background: #0c0e14;
    border-color: #fff;
    -webkit-backdrop-filter: none;
    backdrop-filter: none;
  }
  .mode:not(.is-on) { border-color: rgba(255, 255, 255, 0.55); }
  .field input[type="number"] { border-color: #fff; }
}
