/* でんしゃ しゅっぱつ — 見た目。ライト=昼・ダーク=夜。外部フォントは使わない(何も送らないため) */
:root {
  --bg: #fff6e9;
  --ink: #2b2433;
  --sub: #6b6177;
  --card: #ffffff;
  --card-sh: #ecd9b8;
  --gold: #ffb800;
  --focus: #2f7cf6;
  --off: #d9cfc0;
  --tk: #8a6a4f;
  --tl: #c9ced6;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #1c1a27;
    --ink: #f3eefc;
    --sub: #b4abc4;
    --card: #2c2840;
    --card-sh: #14121c;
    --focus: #ffb800;
    --off: #4a4560;
    --tk: #b08b6b;
    --tl: #6b7079;
  }
  /* 電車の絵は 明るい台の上に(黒い車体や車輪が 夜の色に とけないように) */
  .trains .pic, .preview svg { background: #e9e4f4; border-radius: 12px; padding: 3px 6px; }
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; background: var(--bg); color: var(--ink); }
body {
  font-family: "Hiragino Maru Gothic ProN", "Hiragino Maru Gothic Pro", "BIZ UDPGothic", "Yu Gothic UI", Meiryo, system-ui, sans-serif;
  -webkit-user-select: none; user-select: none;
  -webkit-tap-highlight-color: transparent;
  -webkit-touch-callout: none;
  touch-action: manipulation;
  overscroll-behavior: none;
  overflow: hidden;
}
#app { position: fixed; inset: 0; display: flex; flex-direction: column; }
#safe {
  position: fixed; inset: 0; visibility: hidden; pointer-events: none;
  padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
}
[hidden] { display: none !important; }
button { font: inherit; color: inherit; border: 0; background: none; padding: 0; margin: 0; cursor: pointer; touch-action: manipulation; }
button:focus-visible { outline: 4px solid var(--focus); outline-offset: 3px; }
.screen { flex: 1; min-height: 0; display: flex; flex-direction: column; align-items: center; }

/* ---- ボタン ---- */
.big {
  background: var(--card);
  border-radius: 22px;
  box-shadow: 0 6px 0 var(--card-sh);
  transition: transform .06s;
}
.big:active { transform: translateY(4px); box-shadow: 0 2px 0 var(--card-sh); }
.back {
  display: inline-flex; align-items: center; gap: 2px;
  min-height: 56px; min-width: 56px; padding: 0 16px 0 10px;
  border-radius: 16px; background: var(--card); box-shadow: 0 4px 0 var(--card-sh);
  font-weight: 800; font-size: 18px;
}
.back span { font-size: 32px; line-height: 1; margin-top: -5px; }
.snd { width: 56px; height: 56px; flex-shrink: 0; border-radius: 16px; background: var(--card); box-shadow: 0 4px 0 var(--card-sh); display: grid; place-items: center; }
.snd svg { width: 34px; height: 34px; }
.spk { fill: var(--ink); }
.wave, .slash { fill: none; stroke: var(--ink); stroke-width: 3.5; stroke-linecap: round; }
.snd.off .wave, .snd:not(.off) .slash { display: none; }
.back:active, .snd:active, .num:active, .card:active { transform: translateY(3px); }

/* ---- はじめの画面 ---- */
.menu { padding: calc(env(safe-area-inset-top) + 10px) 16px 15vh; gap: 1.4vh; overflow-y: auto; }
.menu-top { display: flex; align-items: center; justify-content: space-between; gap: 10px; width: min(100%, 640px); margin-top: auto; }
.logo { display: flex; align-items: center; flex-wrap: wrap; column-gap: .3em; margin: 0; font-size: clamp(26px, 7.4vw, 44px); font-weight: 900; line-height: 1.15; letter-spacing: .02em; }
.logo-mark { width: 1.25em; height: 1.25em; flex-shrink: 0; }
.pick { display: grid; gap: 10px; width: min(100%, 640px); }
.trains { grid-template-columns: repeat(2, 1fr); }
.courses { grid-template-columns: repeat(4, 1fr); gap: 6px; }
/* 両数は 5つ ならべても 1つ 64px 以上(せまい画面では 少しだけ横に広げる) */
.cars { grid-template-columns: repeat(5, 1fr); gap: 6px; width: min(calc(100% + 16px), 640px); }
.card {
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px;
  min-height: clamp(70px, 11.5vh, 108px); padding: 6px 6px 4px;
  background: var(--card); border-radius: 18px; box-shadow: 0 5px 0 var(--card-sh);
}
.card[aria-pressed="true"] { box-shadow: 0 5px 0 var(--card-sh), 0 0 0 4px var(--gold); }
.card .pic { display: block; width: min(100%, 150px); aspect-ratio: 120 / 56; }
.card .pic svg { display: block; width: 100%; height: 100%; }
.courses .card { padding: 6px 2px 4px; }
.courses .nm { font-size: 13px; letter-spacing: -.02em; }
@media (max-width: 350px) { .courses .nm { font-size: 11.5px; letter-spacing: -.04em; } }
.courses svg { display: block; width: min(100%, 92px); height: auto; aspect-ratio: 100 / 64; }
.courses .gr { fill: none; stroke: var(--sub); stroke-width: 1.5; stroke-dasharray: 3 3; opacity: .45; }
.courses .pc { fill: #ffd34d; stroke: #2b2433; stroke-width: 2.5; stroke-linejoin: round; }
.courses .pe { fill: #ff8f5e; stroke: #2b2433; stroke-width: 2.5; stroke-linejoin: round; }
.courses .tk { fill: none; stroke: var(--tk); stroke-width: 8; }
.courses .tl { fill: none; stroke: var(--tl); stroke-width: 2.5; }
.courses .st { fill: #ff8f5e; }
.courses .mt { fill: #5b9a4f; stroke: #2b2433; stroke-width: 2.5; stroke-linejoin: round; }
.courses .rv { fill: none; stroke: #6cc4ff; stroke-width: 7; stroke-linecap: round; }
.nm { font-size: 15px; font-weight: 800; line-height: 1.2; white-space: nowrap; }
.num {
  min-height: 64px; min-width: 0; border-radius: 16px; background: var(--card); box-shadow: 0 4px 0 var(--card-sh);
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 3px;
}
.num b { font-size: 24px; font-weight: 900; line-height: 1; }
.num .blk { display: flex; gap: 2px; }
.num .blk i { display: block; width: 7px; height: 6px; border-radius: 2px; background: var(--sub); }
.num[aria-pressed="true"] { background: var(--ink); color: var(--bg); box-shadow: 0 4px 0 rgba(0, 0, 0, .3); }
.num[aria-pressed="true"] .blk i { background: var(--bg); }
.preview { display: flex; justify-content: center; width: min(100%, 640px); height: clamp(28px, 6vh, 56px); }
.preview svg { display: block; height: 100%; max-width: 100%; }
.go {
  min-height: 68px; width: min(100%, 420px); margin-bottom: auto;
  font-size: clamp(24px, 6.5vw, 32px); font-weight: 900;
  background: var(--gold); color: #3a2a00; box-shadow: 0 6px 0 #c98a00;
}
.go:active { box-shadow: 0 2px 0 #c98a00; }

/* ---- あそぶ画面 ---- */
.play { position: relative; width: 100%; }
#cv { position: absolute; inset: 0; display: block; width: 100%; height: 100%; touch-action: none; }
.hud {
  position: absolute; top: calc(env(safe-area-inset-top) + 10px);
  left: calc(env(safe-area-inset-left) + 12px); right: calc(env(safe-area-inset-right) + 12px);
  display: flex; align-items: center; justify-content: space-between; gap: 8px; pointer-events: none;
}
.hud > * { pointer-events: auto; }
.hr { display: flex; align-items: center; gap: 8px; }
/* 画面の下 15% には押すものを置かない(手首が当たる) */
.ctl {
  position: absolute; bottom: 15%;
  left: calc(env(safe-area-inset-left) + 12px); right: calc(env(safe-area-inset-right) + 12px);
  display: grid; grid-template-columns: 1.7fr 1fr 1fr; gap: 10px;
}
.ctl .big {
  min-height: 78px; min-width: 0;
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px;
  font-weight: 900; font-size: clamp(14px, 3.9vw, 18px); line-height: 1.1;
}
.ctl svg { width: 34px; height: 34px; flex-shrink: 0; }
.main { background: var(--gold); color: #3a2a00; box-shadow: 0 6px 0 #c98a00; font-size: clamp(18px, 5vw, 24px) !important; }
.main:active { box-shadow: 0 2px 0 #c98a00; }
.main svg { fill: #3a2a00; }
.main .ic-stop, .main.stop .ic-go { display: none; }
.main.stop .ic-stop { display: block; }
.main.stop { background: #ff8f7a; color: #3a0d05; box-shadow: 0 6px 0 #c95a45; }
.main.stop svg { fill: #3a0d05; }
/* ゆれても 下15%の線をこえないよう、下の辺を軸にして少し持ち上げる */
.main.nudge { animation: nudge 1.4s ease-in-out 3; transform-origin: 50% 100%; }
@keyframes nudge { 0%, 60%, 100% { transform: none; } 20% { transform: translateY(-6px) scale(1.06) rotate(-1.5deg); } 40% { transform: translateY(-6px) scale(1.06) rotate(1.5deg); } }
/* つくる画面のボタン(線の絵) */
.ico { fill: none; stroke: var(--ink); stroke-width: 4.5; stroke-linecap: round; stroke-linejoin: round; }
.main .ico { stroke: #3a2a00; }
.pen { fill: #ffd34d; stroke: var(--ink); stroke-width: 3; stroke-linejoin: round; }
.snd .ico { stroke-width: 5; }
.ctl .big svg.ico-s { width: 30px; height: 30px; }
.big.dim { opacity: .42; box-shadow: 0 6px 0 var(--card-sh); }
.main.dim { background: var(--off); box-shadow: 0 6px 0 var(--card-sh); }
.big.dim:active { transform: none; }
#ectl .big { line-height: 1.15; }
.sp rect { fill: var(--off); }
.sp[data-lv="1"] .b1, .sp[data-lv="2"] .b1, .sp[data-lv="2"] .b2, .sp[data-lv="3"] rect { fill: #2f7cf6; }
.wh .hn { fill: var(--ink); }
.wh .hw { fill: none; stroke: var(--ink); stroke-width: 3.5; stroke-linecap: round; }

/* 寄る・ひく(上の右)・おく(下の4つめ)・おくものの ならび */
.zm { display: flex; gap: 8px; }
.snd.dim { opacity: .42; }
.ctl { grid-template-columns: 1.7fr 1fr 1fr 1fr; }
.put svg { width: 38px !important; height: 38px !important; }
.pal {
  position: absolute; bottom: 15%;
  left: calc(env(safe-area-inset-left) + 12px); right: calc(env(safe-area-inset-right) + 12px);
  display: grid; grid-template-columns: repeat(5, 1fr); gap: 8px;
}
.pal .big { min-height: 64px; min-width: 0; display: grid; place-items: center; }
.pal svg { width: 42px; height: 42px; }
.pal .tk[aria-pressed="true"] { box-shadow: 0 6px 0 var(--card-sh), 0 0 0 4px var(--gold); background: #fff3c4; }
@media (prefers-color-scheme: dark) { .pal .tk[aria-pressed="true"] { background: #4a4260; } }
/* たてで せまい画面: 上の右に 4つ ならぶよう 少し小さく(48px より小さくは しない) */
@media (orientation: portrait) and (max-width: 400px) {
  .hud .snd { width: 50px; height: 50px; }
  .hud .snd svg { width: 30px; height: 30px; }
  .hr, .zm { gap: 6px; }
  .pal { gap: 4px; left: calc(env(safe-area-inset-left) + 8px); right: calc(env(safe-area-inset-right) + 8px); }
  .back { padding: 0 10px 0 6px; font-size: 16px; }
}

/* よこ長の画面: ボタンは右に たてに並べる(線路を大きく) */
@media (orientation: landscape) {
  .hud { left: auto; width: 176px; }
  .ctl {
    left: auto; width: 176px; bottom: auto; top: calc(env(safe-area-inset-top) + 76px);
    grid-template-columns: 1fr; gap: 8px;
  }
  .ctl .big { min-height: clamp(64px, 11vh, 96px); flex-direction: row; gap: 8px; }
  .ctl .main { min-height: clamp(70px, 14vh, 120px); }
  /* 寄る・ひく と おく は 左の列(線路の四角は その右から) */
  .zm { position: fixed; left: calc(env(safe-area-inset-left) + 12px); top: calc(env(safe-area-inset-top) + 10px); flex-direction: column; }
  .ctl .put { position: fixed; left: calc(env(safe-area-inset-left) + 12px); top: calc(env(safe-area-inset-top) + 142px); width: 64px; min-height: 72px; flex-direction: column; gap: 0; font-size: 14px; }
  /* おくものの ならび: 右の列の 上から(3つずつ 4だん)。ひらいている あいだ 上の右の ボタンは かくす(✓で もどる) */
  .pal {
    left: auto; bottom: auto; top: calc(env(safe-area-inset-top) + 8px);
    width: 210px; grid-template-columns: repeat(3, 1fr); gap: 6px;
  }
  .putting .hud > .back, .putting .hud .hr > :not(.zm) { visibility: hidden; }
}

/* 線路をつくる遊びでは、上のボタンが1つふえるので 右のはばを広げる(よこ長のとき) */
@media (orientation: landscape) {
  .mk .hud, .mk .ctl { width: 240px; }
}

/* スマホを横にしたとき: はじめの画面は2列 */
@media (orientation: landscape) and (max-height: 600px) {
  .menu {
    display: grid; grid-template-columns: minmax(0, 1fr) minmax(344px, 1fr);
    grid-template-areas: "top top" "trains courses" "trains cars" "prev go";
    align-content: center; justify-items: stretch; column-gap: 18px; row-gap: 6px;
    padding: calc(env(safe-area-inset-top) + 8px) calc(env(safe-area-inset-right) + 16px) 15vh calc(env(safe-area-inset-left) + 16px);
  }
  .menu-top { grid-area: top; width: 100%; margin: 0; }
  .logo { font-size: 30px; }
  .trains { grid-area: trains; width: 100%; }
  .courses { grid-area: courses; width: 100%; }
  .cars { grid-area: cars; width: 100%; }
  .preview { grid-area: prev; width: 100%; height: 44px; align-self: center; }
  .go { grid-area: go; width: 100%; min-height: 64px; margin: 0; font-size: 26px; }
  .card { min-height: 64px; }
  /* 電車の絵と名前を よこに並べて 低くする(横のスマホでも 全部が 下15%より上に入る) */
  .trains .card { flex-direction: row; gap: 8px; min-height: 64px; }
  .trains .card .pic { width: min(62%, 120px); }
  .courses svg { width: min(100%, 76px); }
}

/* たてが みじかい スマホ: 絵を少し小さく */
@media (orientation: portrait) and (max-height: 700px) {
  .menu { gap: 1vh; }
  .card .pic { width: min(100%, 120px); }
  .courses svg { width: min(100%, 76px); }
  .preview { height: 30px; }
  .logo { font-size: 26px; }
}

/* たてが もっと みじかい スマホ(ブラウザの 帯が ある 小さい機種・たかさ 610px 以下): 「しゅっぱつ！」が 下15%に かからないよう つめる */
@media (orientation: portrait) and (max-height: 610px) {
  .card .pic { width: min(100%, 92px); }
  .courses svg { width: min(100%, 64px); }
  .num { min-height: 56px; }
  .preview { height: 24px; }
  .go { min-height: 60px; }
}

/* よこで ひくい 画面(たかさ 345px 以下): はじめの画面と 右の ボタンを つめて、下15%より 上に おさめる */
@media (orientation: landscape) and (max-height: 345px) {
  .menu { row-gap: 4px; }
  .go { min-height: 56px; }
  .ctl { top: calc(env(safe-area-inset-top) + 72px); gap: 6px; }
  .ctl .big { min-height: 56px; }
  .ctl .main { min-height: 62px; }
  .ctl .put { top: calc(env(safe-area-inset-top) + 136px); min-height: 64px; }

}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: .01ms !important; animation-iteration-count: 1 !important; animation-delay: 0s !important; transition: none !important; }
}
