/* ============================================================
   Tokens / Base
   ============================================================ */

:root {
  color-scheme: light;
  --road-aspect: 1338 / 753;
  --ink: #26313c;
  --muted: #66727d;
  --panel: #ffffff;
  --line: #d6dde2;
  --accent: #e84855;
  --accent-dark: #bd2834;
  --incorrect-blue: #2378d4;
  --sky: #bfeaf8;
  --grass: #75bd38;
  --shadow: 0 18px 44px rgba(33, 54, 73, 0.18);
}

* {
  box-sizing: border-box;
  -webkit-tap-highlight-color: transparent;
}

html,
body {
  height: 100%;
  overflow: hidden;
  overscroll-behavior: none;
  touch-action: none;
  -webkit-touch-callout: none;
  -webkit-user-select: none;
  user-select: none;
}

body {
  margin: 0;
  background-color: #d8f5ff;
  background-image: url("assets/background.png?v=31");
  background-position: center center;
  background-repeat: no-repeat;
  background-size: cover;
  color: var(--ink);
  font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

button {
  border: 0;
  font: inherit;
  -webkit-user-select: none;
  user-select: none;
  touch-action: manipulation;
}

[hidden] {
  display: none !important;
}

/* ============================================================
   App Layout
   ============================================================ */

.app-shell {
  width: min(1180px, calc(100vw - 28px));
  height: 100vh;
  height: 100dvh;
  margin: 0 auto;
  padding: 14px 0 16px;
  display: grid;
  grid-template-rows: auto minmax(0, 1fr);
  gap: 10px;
  overflow: hidden;
}

.topbar {
  display: flex;
  align-items: end;
  justify-content: space-between;
  gap: 18px;
  padding-top: clamp(6px, 1.1dvh, 14px);
}

.app-title {
  margin: 0;
  line-height: 0;
}

.app-title-image {
  display: block;
  width: clamp(150px, 20vw, 230px);
  height: auto;
  user-select: none;
  pointer-events: none;
}

.play-hint {
  position: absolute;
  left: 50%;
  bottom: clamp(8px, 2.2%, 18px);
  z-index: 4;
  width: max-content;
  max-width: calc(100% - 26px);
  margin: 0;
  padding: 8px 14px;
  border: 2px solid rgba(226, 157, 113, 0.36);
  border-radius: 8px;
  background: rgba(255, 245, 214, 0.94);
  color: #d85f5f;
  font-size: clamp(0.95rem, 2.1vw, 1.25rem);
  line-height: 1.25;
  font-weight: 900;
  text-align: center;
  box-shadow: 0 8px 18px rgba(139, 86, 45, 0.14);
  transform: translateX(-50%);
  pointer-events: none;
}

.play-hint-mobile {
  display: none;
}

/* ============================================================
   Stage / Road / Cars
   ============================================================ */

.stage-wrap {
  align-self: center;
  min-height: 0;
  display: grid;
  place-items: center;
}

.stage-frame {
  position: relative;
  width: min(100%, calc((100dvh - 194px) * var(--road-aspect)));
  aspect-ratio: var(--road-aspect);
}

.stage {
  position: relative;
  width: 100%;
  height: 100%;
  overflow: hidden;
  border-radius: 8px;
  background: var(--sky);
  box-shadow: var(--shadow);
  contain: layout paint style;
  isolation: isolate;
}

.stage:not(.is-playing) .play-hint {
  display: none;
}

.road {
  display: block;
  width: 100%;
  height: 100%;
  object-fit: cover;
  user-select: none;
  pointer-events: none;
}

.car-layer {
  position: absolute;
  inset: 0;
  z-index: 2;
  overflow: hidden;
  contain: strict;
}

.car {
  position: absolute;
  left: 0;
  top: 0;
  object-fit: contain;
  filter: none;
  will-change: transform;
  transform-origin: center center;
  pointer-events: none;
}

/* ============================================================
   Debug Panel
   ============================================================ */

.debug-panel {
  position: fixed;
  left: 8px;
  bottom: 8px;
  z-index: 20;
  max-width: min(360px, calc(100vw - 16px));
  padding: 8px 10px;
  border-radius: 8px;
  background: rgba(20, 29, 38, 0.86);
  color: #fff;
  font: 700 11px/1.35 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  white-space: pre-wrap;
  pointer-events: none;
}

/* ============================================================
   Menu / Panels
   ============================================================ */

.title-panel,
.howto-panel,
.target-panel,
.result-panel {
  position: absolute;
  inset: 0;
  z-index: 10;
  display: grid;
  place-items: center;
  background: rgba(255, 255, 255, 0.16);
}

.title-panel,
.howto-panel,
.target-panel {
  background: rgba(236, 249, 255, 0.72);
  backdrop-filter: none;
}

.howto-panel {
  position: fixed;
  inset: 0;
  z-index: 40;
  min-height: 100dvh;
  padding: max(18px, env(safe-area-inset-top)) 16px max(18px, env(safe-area-inset-bottom));
  background: rgba(236, 249, 255, 0.94);
}

.howto-panel .menu-panel {
  width: min(720px, calc(100vw - 32px));
  max-height: calc(100dvh - 36px);
  overflow: auto;
  gap: 16px;
}

.title-panel[hidden],
.howto-panel[hidden],
.target-panel[hidden],
.result-panel {
  display: grid;
}

.title-panel[hidden],
.howto-panel[hidden],
.target-panel[hidden],
.result-panel[hidden] {
  display: none;
}

.menu-panel {
  width: min(360px, calc(100% - 34px));
  padding: 18px;
  display: grid;
  gap: 12px;
  border: 1px solid rgba(38, 49, 60, 0.12);
  border-radius: 8px;
  background: rgba(255, 255, 255, 0.9);
  box-shadow: var(--shadow);
}

.title-panel .menu-panel {
  width: min(640px, calc(100% - 34px));
}

.target-card {
  width: min(720px, calc(100% - 34px));
  padding: clamp(14px, 2.4vw, 22px);
  display: grid;
  gap: clamp(12px, 2vw, 22px);
  justify-items: center;
  border: 1px solid rgba(38, 49, 60, 0.12);
  border-radius: 8px;
  background: rgba(255, 255, 255, 0.9);
  box-shadow: var(--shadow);
}

.menu-title,
.target-title {
  margin: 0;
  color: var(--ink);
  font-size: clamp(1.35rem, 4vw, 2.15rem);
  line-height: 1.05;
  font-weight: 950;
  text-align: center;
  letter-spacing: 0;
}

.menu-title {
  line-height: 0;
}

.menu-title-image {
  display: block;
  width: min(100%, 360px);
  height: auto;
  margin: 0 auto;
  user-select: none;
  pointer-events: none;
}

.target-title {
  max-width: 100%;
  white-space: nowrap;
  font-size: clamp(1.35rem, 5.2vw, 3rem);
}

.menu-buttons {
  display: grid;
  gap: 10px;
}

.difficulty-buttons {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 10px;
}

.menu-button {
  min-height: 50px;
  padding: 0 12px;
  border-radius: 8px;
  background: var(--ink);
  color: #fff;
  font-size: 1.1rem;
  font-weight: 950;
  cursor: pointer;
}

.difficulty-buttons .menu-button {
  min-height: 76px;
  font-size: clamp(1rem, 2.2vw, 1.3rem);
  line-height: 1.08;
}

.difficulty-easy {
  background: #24a85a;
  box-shadow: 0 6px 0 #14733a, 0 12px 18px rgba(20, 115, 58, 0.2);
}

.difficulty-normal {
  background: #2378d4;
  box-shadow: 0 6px 0 #15509a, 0 12px 18px rgba(21, 80, 154, 0.2);
}

.difficulty-hard {
  background: #e84855;
  box-shadow: 0 6px 0 #bd2834, 0 12px 18px rgba(189, 40, 52, 0.2);
}

.difficulty-buttons .menu-button:active {
  transform: translateY(4px);
  box-shadow: 0 2px 0 rgba(20, 29, 38, 0.28), 0 6px 10px rgba(20, 29, 38, 0.16);
}

.menu-button.secondary {
  background: #f5f8fa;
  color: var(--ink);
  border: 1px solid var(--line);
}

.menu-link {
  min-height: 28px;
  padding: 2px 4px;
  background: transparent;
  color: var(--ink);
  font-weight: 850;
  text-decoration: underline;
  cursor: pointer;
  justify-self: center;
}

.howto-text {
  margin: 0;
  display: grid;
  gap: 8px;
  color: var(--muted);
  font-size: 1rem;
  line-height: 1.6;
  font-weight: 750;
}

.howto-text p {
  margin: 0;
}

.target-image {
  width: min(360px, 48vw);
  max-height: min(230px, 28vh);
  object-fit: contain;
  filter: none;
}

/* ============================================================
   Result Screen
   ============================================================ */

.result-panel {
  align-content: center;
  gap: 14px;
  background: rgba(246, 251, 255, 0.82);
  backdrop-filter: none;
}

.result-heading {
  margin: 0;
  color: var(--ink);
  font-size: clamp(1.55rem, 4vw, 2.9rem);
  line-height: 1;
  font-weight: 950;
  letter-spacing: 0;
}

.result-label {
  margin: 0;
  color: var(--accent-dark);
  font-size: clamp(2.4rem, 7vw, 5.2rem);
  line-height: 1;
  font-weight: 950;
  letter-spacing: 0;
  visibility: visible;
}

.result-label.is-hidden {
  visibility: hidden;
}

.result-label.is-incorrect {
  color: var(--incorrect-blue);
}

.result-action.is-reserved {
  visibility: hidden;
  pointer-events: none;
}

.score-row {
  width: min(480px, calc(100% - 36px));
  min-height: 82px;
  padding: 12px 22px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  border: 1px solid rgba(38, 49, 60, 0.15);
  border-radius: 8px;
  background: rgba(255, 255, 255, 0.9);
  font-weight: 950;
}

.score-row span {
  color: var(--muted);
  font-size: clamp(1.35rem, 3vw, 2.2rem);
}

.score-row strong {
  font-size: clamp(2.8rem, 7vw, 5rem);
  line-height: 0.9;
}

/* ============================================================
   Buttons
   ============================================================ */

.primary-button {
  min-height: 52px;
  min-width: 180px;
  padding: 0 28px;
  border-radius: 8px;
  background: var(--accent);
  color: #fff;
  font-size: 1.15rem;
  font-weight: 950;
  letter-spacing: 0;
  box-shadow: 0 10px 0 var(--accent-dark), 0 20px 28px rgba(150, 39, 48, 0.28);
  cursor: pointer;
  transform: translateY(0);
  transition: transform 120ms ease, box-shadow 120ms ease, opacity 120ms ease;
}

.primary-button:active {
  transform: translateY(7px);
  box-shadow: 0 3px 0 var(--accent-dark), 0 10px 18px rgba(150, 39, 48, 0.2);
}

/* ============================================================
   Target HUD
   ============================================================ */

.target-strip {
  position: absolute;
  right: 0;
  bottom: calc(100% + clamp(6px, 1.1vw, 10px));
  z-index: 12;
  min-height: 56px;
  padding: 8px 10px;
  display: flex;
  align-items: center;
  justify-content: end;
  gap: 8px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: rgba(255, 255, 255, 0.9);
  box-shadow: 0 8px 20px rgba(33, 54, 73, 0.14);
  pointer-events: none;
}

.target-strip[hidden] {
  display: none;
}

.target-strip-label {
  min-width: 0;
  color: var(--muted);
  font-weight: 850;
  overflow-wrap: anywhere;
}

.target-strip-image {
  width: clamp(54px, 8vw, 84px);
  height: clamp(36px, 5.4vw, 54px);
  object-fit: contain;
  filter: none;
}

.hidden-count {
  min-width: clamp(48px, 7vw, 72px);
  min-height: clamp(36px, 5vw, 50px);
  display: inline-grid;
  place-items: center;
  border-radius: 6px;
  background: var(--ink);
  color: #fff;
  font-size: clamp(1.35rem, 3vw, 2.2rem);
  font-weight: 950;
}

.stage.is-playing .title-panel,
.stage.is-playing .target-panel {
  display: none;
}

.stage.is-finished .title-panel,
.stage.is-finished .target-panel {
  display: none;
}

/* ============================================================
   Responsive: Mobile
   ============================================================ */

@media (max-width: 720px) {
  .app-shell {
    width: min(100vw - 18px, 560px);
    padding: 10px 0 12px;
    gap: 8px;
  }

  .topbar {
    align-items: stretch;
    flex-direction: column;
    gap: 4px;
  }

  .app-title-image {
    width: clamp(132px, 34vw, 180px);
  }

  .play-hint {
    padding: 6px 10px;
    font-size: clamp(0.82rem, 3.7vw, 1rem);
    line-height: 1.2;
  }

  .play-hint-stage {
    display: none !important;
  }

  .stage-wrap {
    gap: 8px;
  }

  .stage-wrap.is-playing .play-hint-mobile {
    position: static;
    left: auto;
    bottom: auto;
    z-index: 1;
    display: block;
    width: min(100%, calc(100vw - 18px));
    max-width: 560px;
    margin: 0 auto;
    padding: 5px 6px;
    font-size: var(--mobile-hint-font-size, 12px);
    line-height: 1.15;
    overflow: hidden;
    white-space: nowrap;
    transform: none;
  }

  .stage-frame {
    width: min(100%, calc((100dvh - 152px) * var(--road-aspect)));
    transform: translateY(-42px);
  }

  .title-panel:not([hidden]) {
    position: fixed;
    inset: 0;
    z-index: 35;
    display: grid;
    place-items: center;
    padding: max(18px, env(safe-area-inset-top)) 14px max(18px, env(safe-area-inset-bottom));
    background: rgba(236, 249, 255, 0.9);
  }

  .title-panel .menu-panel {
    width: min(420px, calc(100vw - 28px));
    max-height: calc(100dvh - 36px);
    overflow: auto;
    -webkit-overflow-scrolling: touch;
  }

  .howto-panel:not([hidden]) {
    position: fixed;
    inset: 0;
    z-index: 40;
    display: grid;
    place-items: center;
    padding: max(18px, env(safe-area-inset-top)) 14px max(18px, env(safe-area-inset-bottom));
    background: rgba(236, 249, 255, 0.94);
  }

  .howto-panel .menu-panel {
    width: min(360px, calc(100vw - 28px));
    max-height: calc(100dvh - 36px);
    overflow: auto;
    -webkit-overflow-scrolling: touch;
  }

  .menu-panel,
  .target-card {
    width: min(320px, calc(100% - 22px));
    padding: 10px;
    gap: 7px;
  }

  .title-panel .menu-panel {
    width: min(420px, calc(100vw - 28px));
  }

  .menu-title,
  .target-title {
    font-size: clamp(1.05rem, 5.8vw, 1.45rem);
    line-height: 1.08;
  }

  .target-title {
    font-size: clamp(0.9rem, 4.8vw, 1.2rem);
  }

  .menu-buttons {
    gap: 6px;
  }

  .difficulty-buttons {
    gap: 6px;
  }

  .menu-button {
    min-height: 34px;
    font-size: 0.95rem;
  }

  .difficulty-buttons .menu-button {
    min-height: 54px;
    padding: 0 5px;
    font-size: clamp(0.8rem, 3.5vw, 0.98rem);
  }

  .menu-title-image {
    width: min(100%, 180px);
  }

  .howto-text {
    font-size: 0.86rem;
    line-height: 1.45;
    gap: 5px;
  }

  .target-image {
    width: clamp(70px, 24vw, 96px);
  }

  .primary-button {
    min-width: 142px;
    min-height: 42px;
    padding: 0 18px;
    font-size: 1rem;
  }

  .target-strip {
    min-height: 42px;
    padding: 6px 8px;
    gap: 8px;
  }

  .target-strip-image {
    width: 48px;
    height: 32px;
  }

  .hidden-count {
    min-width: 42px;
    min-height: 30px;
    font-size: 1.25rem;
  }

  .result-panel {
    gap: 4px;
  }

  .result-label {
    font-size: clamp(1.45rem, 8.5vw, 2.15rem);
    line-height: 0.95;
  }

  .result-heading {
    font-size: clamp(1.05rem, 5.8vw, 1.45rem);
  }

  .score-row {
    width: min(320px, calc(100% - 22px));
    min-height: 34px;
    padding: 2px 10px;
  }

  .score-row span {
    font-size: clamp(0.86rem, 4.5vw, 1.12rem);
  }

  .score-row strong {
    font-size: clamp(1.65rem, 8.5vw, 2.25rem);
  }

  .result-panel .primary-button {
    min-height: 34px;
    box-shadow: 0 3px 0 var(--accent-dark), 0 7px 10px rgba(150, 39, 48, 0.18);
  }

  .menu-link {
    min-height: 20px;
    font-size: 0.86rem;
  }
}

/* ============================================================
   Responsive: Short Height
   ============================================================ */

@media (max-height: 620px) {
  .app-shell {
    padding: 8px 0;
    gap: 8px;
  }

  .app-title-image {
    width: 142px;
  }

  .topbar {
    padding-top: 4px;
  }

  .menu-button {
    min-height: 42px;
  }

  .difficulty-buttons .menu-button {
    min-height: 58px;
  }

  .stage-frame {
    width: min(100%, calc((100dvh - 166px) * var(--road-aspect)));
  }

  .primary-button {
    min-height: 46px;
  }

  .target-strip {
    min-height: 42px;
  }

  .score-row {
    min-height: 62px;
    padding: 8px 16px;
  }
}

/* ============================================================
   Responsive: Mobile Short Height
   ============================================================ */

@media (max-width: 720px) and (max-height: 720px) {
  .app-shell {
    padding: 7px 0 8px;
    gap: 6px;
  }

  .app-title-image {
    width: clamp(118px, 32vw, 148px);
  }

  .menu-title-image {
    width: min(100%, 140px);
  }

  .play-hint {
    font-size: clamp(0.76rem, 3.4vw, 0.9rem);
  }

  .stage-frame {
    transform: translateY(-46px);
  }

  .menu-panel,
  .target-card {
    padding: 8px;
    gap: 6px;
  }

  .menu-title,
  .target-title {
    font-size: clamp(0.98rem, 5.4vw, 1.25rem);
  }

  .target-title {
    font-size: clamp(0.84rem, 4.5vw, 1.05rem);
  }

  .target-image {
    width: clamp(62px, 22vw, 84px);
  }

  .menu-button {
    min-height: 32px;
    font-size: 0.9rem;
  }

  .difficulty-buttons .menu-button {
    min-height: 48px;
    font-size: clamp(0.76rem, 3.4vw, 0.9rem);
  }

  .primary-button {
    min-height: 38px;
  }

  .score-row {
    min-height: 32px;
    padding: 2px 9px;
  }

  .result-label {
    font-size: clamp(1.35rem, 8vw, 1.95rem);
  }

  .result-heading {
    font-size: clamp(0.98rem, 5.4vw, 1.25rem);
  }

  .result-panel .primary-button {
    min-height: 32px;
    box-shadow: 0 2px 0 var(--accent-dark), 0 5px 8px rgba(150, 39, 48, 0.16);
  }

  .target-strip-label {
    font-size: 0.78rem;
  }

  .target-strip-image {
    width: 42px;
    height: 28px;
  }

  .hidden-count {
    min-width: 36px;
    min-height: 28px;
    font-size: 1.08rem;
  }
}

/* ============================================================
   Responsive: Small Devices
   ============================================================ */

@media (max-width: 360px) and (max-height: 640px) {
  .app-shell {
    width: calc(100vw - 14px);
    padding: 6px 0 8px;
  }

  .app-title-image {
    width: 112px;
  }

  .menu-title-image {
    width: min(100%, 118px);
  }

  .menu-panel,
  .target-card {
    width: min(300px, calc(100% - 16px));
    padding: 6px;
    gap: 5px;
  }

  .title-panel .menu-panel {
    width: min(340px, calc(100vw - 20px));
  }

  .menu-title,
  .target-title {
    font-size: 0.95rem;
  }

  .target-title {
    font-size: 0.78rem;
  }

  .menu-buttons {
    gap: 5px;
  }

  .difficulty-buttons {
    gap: 4px;
  }

  .menu-button {
    min-height: 28px;
    font-size: 0.82rem;
  }

  .difficulty-buttons .menu-button {
    min-height: 50px;
    padding: 0 3px;
    font-size: clamp(0.68rem, 3.45vw, 0.78rem);
  }

  .howto-text {
    font-size: 0.76rem;
    line-height: 1.3;
    gap: 4px;
  }

  .target-image {
    width: clamp(54px, 20vw, 66px);
  }

  .primary-button {
    min-height: 34px;
    font-size: 0.92rem;
  }

  .result-panel {
    gap: 5px;
  }

  .result-label {
    font-size: 1.5rem;
  }

  .result-heading {
    font-size: 0.95rem;
  }

  .score-row {
    min-height: 30px;
  }

  .stage-frame {
    transform: translateY(-50px);
  }
}
