:root {
  --bg: #111113;
  --panel: #17171a;
  --line: #2a2a30;
  --line-strong: #3a3a42;
  --text: #ecebe8;
  --muted: #8e8b86;
  --human: #f0a33a;
  --ai: #43c6b8;
  --grid: rgba(255, 255, 255, 0.06);
  --ivory: #f1ede4;
  --ivory-shade: #ddd6c9;
  --ebony: #1d1d22;
  --error: #f08a7a;
  color-scheme: dark;
  font-family: ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}

* { box-sizing: border-box; }

html, body { margin: 0; background: var(--bg); color: var(--text); }

body {
  min-height: 100vh;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
}

main {
  max-width: 1180px;
  margin: 0 auto;
  padding: max(20px, env(safe-area-inset-top)) max(16px, env(safe-area-inset-right)) 40px max(16px, env(safe-area-inset-left));
  display: grid;
  gap: 16px;
}

.top { display: flex; flex-wrap: wrap; align-items: flex-end; justify-content: space-between; gap: 12px 24px; }
.brand h1 {
  margin: 0;
  font-family: "Iowan Old Style", "Palatino Linotype", Palatino, Georgia, serif;
  font-style: italic;
  font-weight: 500;
  font-size: 36px;
  letter-spacing: -0.01em;
  line-height: 1;
}
.brand p { margin: 8px 0 0; color: var(--muted); font-size: 14px; max-width: 46ch; }

.status {
  display: inline-flex;
  align-items: center;
  gap: 9px;
  padding: 7px 13px;
  border: 1px solid var(--line);
  border-radius: 999px;
  font-size: 13px;
  white-space: nowrap;
}
.status .dot { width: 8px; height: 8px; border-radius: 50%; background: var(--muted); transition: background 160ms; }
.status[data-state="listening"] .dot { background: var(--human); }
.status[data-state="waiting"] .dot,
.status[data-state="thinking"] .dot { background: var(--ai); animation: blink 0.9s ease-in-out infinite; }
.status[data-state="playing"] .dot { background: var(--ai); box-shadow: 0 0 0 4px rgba(67, 198, 184, 0.18); }
@keyframes blink { 50% { opacity: 0.3; } }

.stage {
  position: relative;
  height: clamp(150px, 30vh, 320px);
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 12px;
  overflow: hidden;
}
#roll { display: block; width: 100%; height: 100%; }
.legend { position: absolute; top: 10px; left: 12px; display: flex; gap: 14px; font-size: 12px; color: var(--muted); }
.sw { display: inline-block; width: 10px; height: 10px; margin-right: 6px; border-radius: 3px; vertical-align: -1px; }
.sw.human { background: var(--human); }
.sw.ai { background: var(--ai); }
.empty {
  position: absolute;
  inset: 0;
  margin: 0;
  display: grid;
  place-items: center;
  padding: 0 24px;
  text-align: center;
  color: var(--muted);
  font-size: 15px;
  line-height: 1.55;
  pointer-events: none;
}
.empty[hidden] { display: none; }
.countdown { position: absolute; left: 0; bottom: 0; width: 0; height: 2px; background: var(--ai); }
.countdown.run { width: 100%; transition: width var(--delay, 1s) linear; }

.piano-area { display: grid; gap: 8px; }
.piano-nav { display: flex; align-items: center; justify-content: space-between; gap: 10px; font-size: 12px; color: var(--muted); }
.piano-nav[hidden] { display: none; }
.range { font-variant-numeric: tabular-nums; }

.piano {
  position: relative;
  display: flex;
  height: clamp(140px, 24vh, 210px);
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
  touch-action: none;
}
.key { position: relative; transition: background-color 90ms; }
.key.white {
  flex: none;
  height: 100%;
  background: color-mix(in oklab, var(--ai) calc(var(--p, 0) * 48%), var(--ivory));
  border-right: 1px solid #b4ad9f;
  border-radius: 0 0 6px 6px;
  box-shadow: inset 0 -6px 0 rgba(0, 0, 0, 0.06);
}
.key.white:first-child { border-radius: 6px 0 6px 6px; }
.key.black {
  position: absolute;
  top: 0;
  z-index: 2;
  height: 61%;
  background: color-mix(in oklab, var(--ai) calc(var(--p, 0) * 70%), var(--ebony));
  border-radius: 0 0 5px 5px;
  box-shadow: 0 3px 6px rgba(0, 0, 0, 0.45);
}
.key.is-human { background: var(--human); box-shadow: none; }
.key.is-ai { background: var(--ai); box-shadow: none; }
.hint, .name {
  position: absolute;
  left: 0;
  right: 0;
  text-align: center;
  pointer-events: none;
}
.hint { bottom: 8px; font-size: 11px; font-weight: 600; color: #6e695f; }
.black .hint { bottom: 6px; color: #a9a59c; }
.name { bottom: 26px; font-size: 10px; color: #9b9589; }
.is-human .hint, .is-ai .hint, .is-human .name, .is-ai .name { color: rgba(0, 0, 0, 0.55); }

button, select {
  font: inherit;
  font-size: 14px;
  color: var(--text);
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 8px;
}
button { padding: 9px 16px; cursor: pointer; min-height: 40px; }
button:hover:not(:disabled) { border-color: var(--line-strong); }
button:disabled { opacity: 0.35; cursor: default; }
button.primary { background: var(--text); color: var(--bg); border-color: var(--text); font-weight: 600; }
button.ghost { min-width: 44px; padding: 6px 12px; }
select { width: 100%; padding: 9px 10px; }

.actions { display: flex; flex-wrap: wrap; align-items: center; gap: 10px; }
.error { margin: 0 0 0 6px; color: var(--error); font-size: 13px; }

.controls {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(190px, 1fr));
  gap: 14px 24px;
  align-items: end;
}
.controls label { display: grid; gap: 8px; font-size: 13px; color: var(--muted); }
.lbl { display: flex; justify-content: space-between; gap: 8px; }
.lbl output { color: var(--text); font-variant-numeric: tabular-nums; }
input[type="range"] { width: 100%; accent-color: var(--ai); margin: 0; height: 24px; }

.stats {
  display: flex;
  flex-wrap: wrap;
  gap: 10px 32px;
  padding-top: 14px;
  border-top: 1px solid var(--line);
  font-size: 13px;
}
.stats div { display: grid; gap: 3px; }
.stats span { color: var(--muted); font-size: 12px; }
.stats b { font-weight: 500; font-variant-numeric: tabular-nums; }

.help { color: var(--muted); font-size: 13px; line-height: 1.55; }
.help p { margin: 0 0 6px; max-width: 80ch; }
kbd {
  font: inherit;
  font-size: 12px;
  padding: 1px 6px;
  border: 1px solid var(--line-strong);
  border-radius: 4px;
  color: var(--text);
}
body.aria-mode .controls label:has(#memory),
body.aria-mode .controls label:has(#creativity) { opacity: 0.35; pointer-events: none; }
.aria-only { display: none; }
body.aria-mode .aria-only { display: block; }
.touch-only { display: none; }
body.touch .touch-only { display: block; }
body.touch .desk-only { display: none; }

@media (max-width: 640px) {
  .brand h1 { font-size: 30px; }
  .brand p { font-size: 13px; }
  .piano { height: clamp(170px, 30vh, 240px); }
  .controls { grid-template-columns: 1fr 1fr; }
  .stats { gap: 10px 22px; }
}

@media (max-height: 500px) and (orientation: landscape) {
  main { gap: 10px; padding-top: 10px; }
  .brand p, .help, .stats { display: none; }
  .stage { height: 26vh; }
  .piano { height: 46vh; }
}
