:root {
  font-family: Inter, "PingFang SC", "Microsoft YaHei", system-ui, sans-serif;
  color: #4b4667;
  background: #ebe6f3;
  font-synthesis: none;
  --page-gutter: 16px;
}
* { box-sizing: border-box; }
body { margin: 0; overscroll-behavior-y: none; }
button, a, canvas { -webkit-tap-highlight-color: transparent; }
button {
  min-width: 44px;
  min-height: 44px;
  font: inherit;
  color: inherit;
  cursor: pointer;
  touch-action: manipulation;
  border: 0;
}
button:disabled { cursor: default; opacity: .4; }
button:focus-visible, a:focus-visible, canvas:focus-visible {
  outline: 3px solid #f1a666;
  outline-offset: 2px;
}
#game {
  position: relative;
  display: flex;
  flex-direction: column;
  width: min(100%, 520px);
  min-height: 100vh;
  min-height: 100dvh;
  margin: auto;
  padding: env(safe-area-inset-top) var(--page-gutter) max(10px, env(safe-area-inset-bottom));
  background: linear-gradient(145deg, #f8f5fc, #eee8f5 65%);
}
/* Let short screens scroll instead of clipping the controls or shrinking the cars. */
.topbar { min-height: 64px; flex-shrink: 0; display: flex; gap: 6px; align-items: center; }
.icon, .language {
  width: 44px;
  min-width: 44px;
  height: 44px;
  flex-shrink: 0;
  background: #ffffffba;
  border-radius: 15px;
  display: grid;
  place-items: center;
  text-decoration: none;
  font-size: 23px;
  box-shadow: 0 3px 0 #dad3e647;
}
.language { font-size: 13px; font-weight: 700; }
.brand { flex: 1; min-width: 0; display: flex; gap: 4px; flex-direction: column; }
.wordmark { font-size: 10px; line-height: 1.3; letter-spacing: 1.2px; color: #786487; font-weight: 800; }
.brand strong { font-size: 17px; line-height: 1.15; font-weight: 800; letter-spacing: -.45px; }
.dashboard {
  padding: 4px 12px 12px;
  border: 1px solid #fff;
  border-radius: 23px;
  background: #fffffff0;
  box-shadow: 0 7px 23px #7861940a;
}
.chapterline { display: flex; align-items: center; justify-content: space-between; font-size: 12px; color: #786487; gap: 8px; min-height: 44px; }
.chapterline > span { min-width: 0; line-height: 1.4; }
.textbutton { flex-shrink: 0; background: transparent; padding: 0 4px; font-size: 12px; font-weight: 700; white-space: nowrap; color: #78608e; }
.stats { display: flex; gap: 10px; }
.stats > div { flex: 1; min-width: 0; }
.stats > div + div { border-left: 1px solid #eee9f2; padding-left: 10px; }
.stats small { font-size: 12px; line-height: 1.4; color: #786487; display: block; margin-bottom: 4px; }
.stats strong { display: block; font-variant-numeric: tabular-nums; font-size: 25px; font-weight: 800; line-height: 1.15; white-space: nowrap; }
.stats em { font-style: normal; font-size: 12px; color: #786487; font-weight: 600; }
.mission { min-height: 40px; flex-shrink: 0; display: flex; gap: 8px; align-items: center; justify-content: center; text-align: center; font-size: 13px; color: #786487; line-height: 1.45; padding: 6px 0; }
.mission i { height: 6px; width: 6px; border-radius: 50%; background: #ef9488; flex-shrink: 0; box-shadow: 0 0 0 4px #ef948819; }
#stage {
  position: relative;
  flex: none;
  height: clamp(240px, calc(100vh - 454px - env(safe-area-inset-top) - max(10px, env(safe-area-inset-bottom))), 600px);
  height: clamp(240px, calc(100dvh - 454px - env(safe-area-inset-top) - max(10px, env(safe-area-inset-bottom))), 600px);
  margin: 0 calc(-1 * var(--page-gutter));
  overflow: hidden;
}
#board { display: block; width: 100%; height: 100%; touch-action: none; user-select: none; }
.stage-tag { pointer-events: none; position: absolute; bottom: 3px; left: 8px; right: 8px; text-align: center; font-size: 12px; line-height: 1.4; color: #786487; }
.stage-tag > span:first-child { font-size: 14px; margin-right: 4px; }
.board-district { position: absolute; left: 0; right: 0; text-align: center; pointer-events: none; font: 600 12px/18px system-ui, sans-serif; color: #786487; }
.controls { flex-shrink: 0; min-width: 0; padding-top: 8px; }
.selection { display: flex; flex-direction: column; gap: 3px; font-size: 13px; line-height: 1.4; margin-bottom: 8px; }
.selection strong { font-weight: 700; }
#cost-rule { font-size: 12px; line-height: 1.4; color: #786487; }
#status { min-height: 2.8em; font-size: 12px; color: #786487; line-height: 1.4; }
.drive-controls { display: flex; gap: 8px; }
.drive-controls button { flex: 0 0 76px; min-height: 48px; min-width: 76px; padding: 8px 10px; border-radius: 16px; background: #fffaf4; box-shadow: 0 4px 0 #d9cfe3; font-size: 20px; }
.drive-controls small { font-size: 12px; vertical-align: middle; }
.drive-controls .exit-button { flex: 1; min-width: 44px; background: #806aa4; color: white; box-shadow: 0 4px 0 #8875aa; font-size: 14px; font-weight: 750; line-height: 1.3; }
.tool-controls { display: flex; gap: 4px; margin-top: 9px; }
.tool-controls button { flex: 1; min-width: 44px; min-height: 48px; border-radius: 12px; background: transparent; color: #786487; font-size: 19px; line-height: 1.25; padding: 4px 2px; }
.tool-controls span { display: inline-block; font-size: 12px; margin-left: 3px; vertical-align: middle; }
.tool-controls button:active, .drive-controls button:active { transform: translateY(2px); }
.save-status { text-align: center; font-size: 12px; color: #786487; margin: 5px 0 0; line-height: 1.4; min-height: 17px; }
.save-status.warning { color: #a05047; }
#overlay { position: fixed; inset: 0; z-index: 4; background: #3e33568f; backdrop-filter: blur(6px); display: flex; align-items: center; justify-content: center; padding: max(12px, env(safe-area-inset-top)) max(12px, env(safe-area-inset-right)) max(12px, env(safe-area-inset-bottom)) max(12px, env(safe-area-inset-left)); }
#overlay[hidden], .loading[hidden] { display: none; }
.modal { width: 100%; max-width: 480px; max-height: 100%; overflow-y: auto; overscroll-behavior: contain; background: #faf7fd; border: 1px solid white; border-radius: 24px; padding: 22px 16px; box-shadow: 0 20px 70px #36294236; text-align: center; }
.modal h2 { font-size: 25px; letter-spacing: -.6px; margin: 12px 0; }
.modal p { font-size: 13px; color: #786487; line-height: 1.6; margin: 12px 0 18px; }
.modal .eyebrow { font-size: 12px; line-height: 1.4; color: #786487; letter-spacing: 1px; }
.modal .wide { display: block; width: 100%; min-height: 48px; background: #806aa4; border-radius: 15px; color: #fff; font-size: 14px; font-weight: 700; margin-top: 11px; padding: 9px; }
.modal .secondary { background: #eee7f6; color: #786487; }
.modal .stars { font-size: 34px; letter-spacing: 7px; color: #c98b24; margin: 18px 0; }
.result-row { display: flex; gap: 7px; padding: 17px 0; border-top: 1px solid #e9e0f1; border-bottom: 1px solid #e9e0f1; }
.result-row div { flex: 1; min-width: 0; }
.result-row strong { font-size: 23px; display: block; }
.result-row span { font-size: 12px; color: #786487; display: block; margin-top: 6px; }
.level-tabs { display: grid; grid-template-columns: repeat(auto-fit, minmax(76px, 1fr)); gap: 6px; margin: 17px 0 10px; }
.level-tabs button { min-height: 44px; min-width: 44px; padding: 8px 4px; background: #ece5f2; border-radius: 11px; font-size: 12px; line-height: 1.3; }
.level-tabs .active { background: #806aa4; color: white; }
.levelgrid { display: grid; grid-template-columns: repeat(5, minmax(44px, 1fr)); gap: 6px; padding: 10px 0 15px; }
.levelgrid button { min-height: 60px; min-width: 44px; padding: 6px 2px; border-radius: 13px; background: #eee8f4; font-size: 16px; }
.levelgrid .current { box-shadow: inset 0 0 0 2px #806aa4; }
.levelgrid small { display: block; font-size: 12px; color: #805f29; margin-top: 4px; overflow-wrap: anywhere; }
.modal .rules { text-align: left; padding: 13px 15px; background: #eee8f5; border-radius: 15px; font-size: 13px; line-height: 1.8; color: #786487; }
.modal .notice { background: #fff0d6; padding: 10px; border-radius: 13px; color: #8c6636; }
.loading { position: fixed; inset: 0; background: #f1edf8; z-index: 6; display: flex; flex-direction: column; justify-content: center; align-items: center; gap: 13px; padding: 20px; text-align: center; }
.loading strong { font-size: 17px; }
.loading span { font-size: 13px; color: #786487; }
.loading-car { font-size: 54px; color: #fa786e; }
.loading button { padding: 14px 23px; border-radius: 13px; background: #806aa4; color: white; }
body.night #game { background: linear-gradient(#303751, #242b4b); color: #f1eaf9; }
body.night .dashboard, body.night .icon, body.night .language { background: #424965; border-color: #65708a; }
body.night .chapterline, body.night .mission, body.night .textbutton, body.night .wordmark, body.night .stage-tag, body.night #cost-rule { color: #c5b6d9; }
body.night .stats > div + div { border-color: #626880; }
body.night .stats small, body.night .stats em, body.night .save-status { color: #c5b6d9; }
body.night .selection #status, body.night .tool-controls button { color: #c5b6d9; }
body.night .save-status.warning { color: #f3b4ab; }
body.night .drive-controls button { background: #515775; color: #f3edf8; box-shadow: 0 4px 0 #202840; }
body.night .drive-controls .exit-button { background: #ac97d1; color: #242b4b; }
body.night .modal { color: #4b4667; }
@media (max-width: 350px) {
  :root { --page-gutter: 10px; }
  .topbar { gap: 4px; }
  .brand strong { font-size: 14px; }
  .wordmark { font-size: 10px; letter-spacing: .25px; }
  .dashboard { padding-left: 8px; padding-right: 8px; }
  .stats { gap: 6px; }
  .stats > div + div { padding-left: 6px; }
  .stats strong { font-size: 22px; }
  .tool-controls span { margin-left: 0; }
}
@media (max-height: 740px) {
  .topbar { min-height: 56px; }
  .dashboard { padding-bottom: 8px; }
  .stats strong { font-size: 22px; }
  .mission { min-height: 34px; }
  .controls { padding-top: 5px; }
  .selection { margin-bottom: 6px; }
  .tool-controls { margin-top: 7px; }
  .modal { padding-top: 16px; padding-bottom: 16px; }
}
/* Keep steering beside the visible board before variable-length status text. */
@media (max-width: 559px) and (max-height: 740px) {
  .controls { display: flex; flex-direction: column; }
  .drive-controls { order: -1; }
  .selection { margin-top: 10px; margin-bottom: 0; }
  #cost-rule { order: -1; }
}
/* Keep the board and its controls beside each other on phone landscape screens. */
@media (min-width: 560px) and (orientation: landscape) {
  #game {
    width: min(100%, 1040px);
    display: grid;
    grid-template-columns: minmax(0, 1.15fr) minmax(268px, 1fr);
    grid-template-areas: "topbar topbar" "stage mission" "stage controls" "dashboard controls";
    grid-template-rows: auto auto minmax(0, 1fr) auto;
    column-gap: 16px;
    align-content: start;
    padding-left: max(12px, env(safe-area-inset-left));
    padding-right: max(12px, env(safe-area-inset-right));
  }
  .topbar { grid-area: topbar; min-height: 56px; }
  .dashboard { grid-area: dashboard; }
  .mission { grid-area: mission; min-height: 32px; align-self: start; }
  #stage {
    grid-area: stage;
    height: clamp(190px, calc(100vh - 162px - env(safe-area-inset-top) - max(10px, env(safe-area-inset-bottom))), 500px);
    height: clamp(190px, calc(100dvh - 162px - env(safe-area-inset-top) - max(10px, env(safe-area-inset-bottom))), 500px);
    margin: 0;
    align-self: start;
    border-radius: 20px;
  }
  .controls { grid-area: controls; align-self: start; }
  .stats { gap: 6px; }
  .stats > div + div { padding-left: 6px; }
}
@media (prefers-reduced-motion: reduce) {
  * { scroll-behavior: auto !important; }
}
