* {
  box-sizing: border-box;
}

:root {
  color-scheme: dark;
  --bg: #141a1e;
  --panel: #272a2b;
  --ink: #f5eddb;
  --muted: #b9b29f;
  --quiet: #837d6d;
  --chop: #ff7d42;
  --flip: #85cc66;
  --plate: #54abeb;
  --serve: #ffc74f;
  --perfect: #ffe07a;
  --good: #85cc66;
  --miss: #ff6a5a;
  --heart: #ff6a5a;
  --board-panel: #10161a;
  --board-edge: rgba(245, 237, 219, 0.14);
  --root-edge: rgba(245, 237, 219, 0.2);
  --secondary-btn: #2f3334;
  --primary-btn: #ffc74f;
  --primary-label: #141a1e;
  --disabled: #1b2024;
  --disabled-label: #5a5648;
  --shadow: rgba(0, 0, 0, 0.5);
  --deep-shadow: rgba(0, 0, 0, 0.65);
}

html,
body {
  min-height: 100%;
}

body {
  margin: 0;
  overflow-x: hidden;
  background:
    radial-gradient(120% 80% at 50% -10%, rgba(255, 199, 79, 0.08) 0%, transparent 55%),
    linear-gradient(180deg, #1a2227 0%, #141a1e 55%, #0c1013 100%);
  color: var(--ink);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Hiragino Sans", "Hiragino Kaku Gothic ProN", "Noto Sans CJK JP", "Microsoft YaHei", sans-serif;
}

button,
a {
  -webkit-tap-highlight-color: transparent;
}

.play-shell {
  width: min(1120px, calc(100% - 32px));
  min-height: 100svh;
  margin: 0 auto;
  padding: max(18px, env(safe-area-inset-top)) 0 max(24px, env(safe-area-inset-bottom));
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 10px 0 22px;
}

.brand {
  display: inline-flex;
  align-items: center;
  gap: 10px;
  color: var(--ink);
  font-size: 24px;
  font-weight: 900;
  text-decoration: none;
}

.brand::before {
  width: 18px;
  height: 18px;
  border-radius: 5px;
  background:
    linear-gradient(135deg, var(--chop) 0 25%, var(--flip) 25% 50%, var(--plate) 50% 75%, var(--serve) 75% 100%);
  box-shadow: 0 0 18px rgba(255, 199, 79, 0.32);
  content: "";
}

.status-pills {
  display: flex;
  flex-wrap: wrap;
  justify-content: flex-end;
  gap: 8px;
}

.status-pills span {
  min-height: 38px;
  display: inline-flex;
  align-items: center;
  border: 1px solid var(--board-edge);
  border-radius: 8px;
  padding: 8px 12px;
  color: var(--muted);
  background: var(--panel);
  box-shadow: inset 0 1px 0 rgba(255, 244, 209, 0.05);
  font-size: 13px;
  font-weight: 900;
}

.game-layout {
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(280px, 360px);
  align-items: stretch;
  gap: clamp(18px, 4vw, 40px);
}

.stage-panel,
.hud {
  border: 1px solid var(--root-edge);
  border-radius: 8px;
  background: var(--panel);
  box-shadow: 0 28px 90px var(--deep-shadow);
}

.stage-panel {
  position: relative;
  display: grid;
  min-height: min(78svh, 800px);
  place-items: center;
  overflow: hidden;
  padding: clamp(14px, 3vw, 24px);
}

#gameCanvas {
  position: relative;
  z-index: 1;
  display: block;
  height: min(74svh, 760px);
  max-height: 760px;
  width: auto;
  max-width: 100%;
  aspect-ratio: 2 / 3;
  border: 1px solid var(--board-edge);
  border-radius: 8px;
  background: var(--board-panel);
  box-shadow: 0 24px 70px rgba(0, 0, 0, 0.42), 0 0 0 1px rgba(255, 199, 79, 0.08);
  touch-action: manipulation;
}

.hud-overlay {
  position: absolute;
  z-index: 2;
  top: clamp(18px, 3vw, 30px);
  left: 50%;
  width: min(86%, 420px);
  transform: translateX(-50%);
  pointer-events: none;
}

.hud-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  margin-bottom: 8px;
}

.recipe-title {
  color: var(--ink);
  font-size: 15px;
  font-weight: 900;
  text-shadow: 0 2px 8px rgba(0, 0, 0, 0.6);
}

.hearts {
  display: inline-flex;
  gap: 4px;
}

.heart {
  width: 15px;
  height: 15px;
  background: var(--heart);
  box-shadow: 0 1px 4px rgba(0, 0, 0, 0.5);
  clip-path: path("M7.5 13.5 1.6 7.6a3.8 3.8 0 0 1 5.9-4.7 3.8 3.8 0 0 1 5.9 4.7z");
}

.heart.lost {
  background: rgba(245, 237, 219, 0.18);
  box-shadow: none;
}

.progress-bar {
  height: 7px;
  border-radius: 999px;
  background: rgba(245, 237, 219, 0.12);
  overflow: hidden;
}

.progress-fill {
  display: block;
  width: 0%;
  height: 100%;
  border-radius: 999px;
  background: linear-gradient(90deg, var(--serve), var(--chop));
  transition: width 120ms linear;
}

.judge-popup {
  position: absolute;
  z-index: 3;
  top: 38%;
  left: 50%;
  font-size: clamp(30px, 8vw, 54px);
  font-weight: 900;
  letter-spacing: 0.02em;
  text-shadow: 0 4px 18px rgba(0, 0, 0, 0.7);
  opacity: 0;
  pointer-events: none;
  transform: translate(-50%, 0) scale(0.7);
}

.judge-popup.show {
  animation: pop 560ms ease-out;
}

@keyframes pop {
  0% { opacity: 0; transform: translate(-50%, 6px) scale(0.7); }
  22% { opacity: 1; transform: translate(-50%, -8px) scale(1.12); }
  70% { opacity: 1; transform: translate(-50%, -10px) scale(1); }
  100% { opacity: 0; transform: translate(-50%, -22px) scale(0.96); }
}

.mute-btn {
  position: absolute;
  z-index: 4;
  right: clamp(14px, 3vw, 22px);
  bottom: clamp(14px, 3vw, 22px);
  width: 44px;
  height: 44px;
  display: grid;
  place-items: center;
  padding: 0;
  border: 1px solid var(--board-edge);
  border-radius: 999px;
  color: var(--ink);
  background: rgba(16, 22, 26, 0.82);
  cursor: pointer;
}

.mute-btn svg {
  width: 24px;
  height: 24px;
  fill: currentColor;
}

.mute-btn .icon-muted {
  display: none;
}

.mute-btn[data-muted="true"] .icon-sound {
  display: none;
}

.mute-btn[data-muted="true"] .icon-muted {
  display: block;
  color: var(--quiet);
}

.overlay {
  position: absolute;
  z-index: 5;
  inset: 0;
  display: grid;
  place-items: center;
  padding: clamp(14px, 4vw, 28px);
  background: rgba(10, 14, 17, 0.86);
  backdrop-filter: blur(3px);
}

.overlay[hidden] {
  display: none;
}

.overlay-card {
  width: min(100%, 380px);
  max-height: 100%;
  overflow-y: auto;
  border: 1px solid var(--root-edge);
  border-radius: 12px;
  padding: clamp(18px, 4vw, 26px);
  background: var(--panel);
  box-shadow: 0 28px 70px var(--deep-shadow);
  text-align: center;
}

.eyebrow {
  margin: 0 0 8px;
  color: var(--serve);
  font-size: 12px;
  font-weight: 900;
  text-transform: uppercase;
  letter-spacing: 0.08em;
}

.overlay-title {
  margin: 0;
  color: var(--ink);
  font-size: clamp(30px, 7vw, 44px);
  line-height: 1;
}

.overlay-text {
  margin: 12px 0 0;
  color: var(--muted);
  font-size: 15px;
  line-height: 1.5;
}

.overlay-stats {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 10px;
  margin: 18px 0 4px;
}

.overlay-stats[hidden] {
  display: none;
}

.stat {
  border: 1px solid var(--board-edge);
  border-radius: 8px;
  padding: 10px 6px;
  background: var(--board-panel);
}

.stat-num {
  display: block;
  font-size: 24px;
  font-weight: 900;
  color: var(--serve);
}

.stat-cap {
  display: block;
  margin-top: 4px;
  font-size: 11px;
  font-weight: 900;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--quiet);
}

.overlay-actions {
  display: flex;
  flex-direction: column;
  gap: 10px;
  margin: 18px 0 0;
}

button {
  min-height: 48px;
  border: 1px solid var(--board-edge);
  border-radius: 8px;
  color: var(--ink);
  background: var(--secondary-btn);
  font: inherit;
  font-weight: 900;
  cursor: pointer;
  transition: transform 150ms ease, border-color 150ms ease, background-color 150ms ease, color 150ms ease, box-shadow 150ms ease;
}

.action-button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 0 14px;
  font-size: 15px;
  box-shadow: 0 5px 0 rgba(0, 0, 0, 0.32), 0 12px 24px rgba(0, 0, 0, 0.22);
}

.action-button[hidden] {
  display: none;
}

.action-button.primary:not(:disabled) {
  border-color: var(--root-edge);
  color: var(--primary-label);
  background: var(--primary-btn);
}

button:hover:not(:disabled),
button:focus-visible {
  border-color: rgba(245, 237, 219, 0.8);
  transform: translateY(-1px);
}

.action-button.primary:hover:not(:disabled),
.action-button.primary:focus-visible {
  color: var(--primary-label);
}

button:disabled {
  border-color: rgba(46, 52, 56, 0.8);
  color: var(--disabled-label);
  background: var(--disabled);
  box-shadow: 0 3px 0 rgba(0, 0, 0, 0.25);
  cursor: not-allowed;
}

button:active:not(:disabled) {
  transform: translateY(2px);
}

.level-select {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 8px;
  margin-top: 18px;
}

.level-select[hidden] {
  display: none;
}

.level-chip {
  position: relative;
  min-height: 52px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  padding: 6px 4px;
  border: 1px solid var(--board-edge);
  border-radius: 8px;
  background: var(--board-panel);
  color: var(--muted);
  font-size: 11px;
  font-weight: 900;
  line-height: 1.1;
  cursor: pointer;
}

.level-chip .chip-num {
  font-size: 15px;
  color: var(--ink);
}

.level-chip.cleared {
  border-color: rgba(133, 204, 102, 0.6);
}

.level-chip.cleared .chip-num {
  color: var(--flip);
}

.level-chip.current {
  outline: 2px solid var(--serve);
  outline-offset: 2px;
}

.hud {
  display: flex;
  min-height: 100%;
  flex-direction: column;
  justify-content: space-between;
  gap: 22px;
  padding: clamp(18px, 3vw, 26px);
}

.hud-copy h1 {
  margin: 0;
  color: var(--ink);
  font-size: clamp(28px, 4vw, 42px);
  line-height: 0.98;
}

.hud-copy .eyebrow {
  color: var(--serve);
}

.hud-copy p {
  margin: 12px 0 0;
  color: var(--muted);
  font-size: 15px;
  line-height: 1.5;
}

.lane-pad {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 10px;
  border: 1px solid var(--board-edge);
  border-radius: 8px;
  padding: 12px;
  background: var(--board-panel);
}

.lane-btn {
  position: relative;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  min-height: 92px;
  padding: 10px 4px;
  border: 1px solid var(--board-edge);
  border-top-width: 3px;
  border-radius: 10px;
  color: var(--ink);
  background: var(--secondary-btn);
  box-shadow: 0 5px 0 rgba(0, 0, 0, 0.3), 0 12px 22px rgba(0, 0, 0, 0.22);
}

.lane-btn[data-lane="0"] { border-top-color: var(--chop); }
.lane-btn[data-lane="1"] { border-top-color: var(--flip); }
.lane-btn[data-lane="2"] { border-top-color: var(--plate); }
.lane-btn[data-lane="3"] { border-top-color: var(--serve); }

.lane-icon {
  display: grid;
  place-items: center;
  width: 30px;
  height: 30px;
}

.lane-icon svg {
  width: 100%;
  height: 100%;
  fill: currentColor;
}

.lane-btn[data-lane="0"] .lane-icon { color: var(--chop); }
.lane-btn[data-lane="1"] .lane-icon { color: var(--flip); }
.lane-btn[data-lane="2"] .lane-icon { color: var(--plate); }
.lane-btn[data-lane="3"] .lane-icon { color: var(--serve); }

.lane-name {
  font-size: 13px;
  font-weight: 900;
}

.lane-key {
  font-size: 10px;
  font-weight: 900;
  color: var(--quiet);
  border: 1px solid var(--board-edge);
  border-radius: 4px;
  padding: 1px 5px;
}

.lane-btn.flash {
  background: var(--secondary-btn);
  animation: laneflash 220ms ease-out;
}

.lane-btn[data-lane="0"].flash { box-shadow: 0 0 0 2px var(--chop), 0 0 22px rgba(255, 125, 66, 0.6); }
.lane-btn[data-lane="1"].flash { box-shadow: 0 0 0 2px var(--flip), 0 0 22px rgba(133, 204, 102, 0.6); }
.lane-btn[data-lane="2"].flash { box-shadow: 0 0 0 2px var(--plate), 0 0 22px rgba(84, 171, 235, 0.6); }
.lane-btn[data-lane="3"].flash { box-shadow: 0 0 0 2px var(--serve), 0 0 22px rgba(255, 199, 79, 0.6); }

@keyframes laneflash {
  0% { transform: translateY(2px); }
  100% { transform: translateY(0); }
}

.lane-btn:active {
  transform: translateY(2px);
}

.control-note {
  margin: 0;
  color: var(--quiet);
  font-size: 13px;
}

@media (max-width: 900px) {
  .play-shell {
    width: calc(100% - 24px);
    max-width: 560px;
  }

  .game-layout {
    grid-template-columns: 1fr;
  }

  .stage-panel {
    min-height: auto;
  }

  #gameCanvas {
    height: auto;
    width: min(100%, 62svh);
    max-height: none;
  }
}

@media (max-width: 560px) {
  html,
  body {
    width: 100%;
    max-width: 100%;
    overflow-x: hidden;
  }

  .play-shell {
    width: 100%;
    max-width: 100%;
    padding-right: 10px;
    padding-left: 10px;
  }

  .topbar {
    align-items: flex-start;
    flex-direction: column;
  }

  .status-pills {
    justify-content: flex-start;
  }

  #gameCanvas {
    width: 100%;
  }

  .game-layout,
  .stage-panel,
  .hud,
  .lane-pad {
    width: 100%;
    max-width: 100%;
    min-width: 0;
  }

  .lane-btn {
    min-height: 80px;
  }
}

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