:root {
  --navy: #13284f; --ink: #1d2a44; --sky: #3fa9f5; --pink: #ff5c8a; --yellow: #ffd23f; --green: #2fbf7f; --paper: #ffffff; --soft: #eef5fd;
  --disp: 'Dela Gothic One', 'Zen Kaku Gothic New', sans-serif; --body: 'Zen Kaku Gothic New', 'Hiragino Sans', sans-serif;
  --sat: env(safe-area-inset-top, 0px); --sab: env(safe-area-inset-bottom, 0px); --sal: env(safe-area-inset-left, 0px); --sar: env(safe-area-inset-right, 0px);
}
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; height: 100%; overflow: clip; background: #58a6f0; font-family: var(--body); color: var(--ink); touch-action: none; user-select: none; -webkit-user-select: none; }
button { font-family: var(--body); cursor: pointer; touch-action: manipulation; }
#cv { position: fixed; inset: 0; width: 100%; height: 100%; display: block; }
[hidden] { display: none !important; }

/* ---------- HUD ---------- */
#hud { position: fixed; inset: 0; pointer-events: none; }
#hud button, #hud #steer { pointer-events: auto; }
#speed { position: absolute; left: 50%; bottom: calc(14px + var(--sab)); translate: -50% 0; display: grid; grid-template-columns: auto auto; align-items: end; column-gap: 6px; color: #fff; text-shadow: 0 2px 0 var(--navy), 0 0 6px rgba(19, 40, 79, .6); }
#speed b { font-family: var(--disp); font-weight: 400; font-size: 46px; line-height: 1; min-width: 3ch; text-align: right; font-variant-numeric: tabular-nums; }
#speed span { font-weight: 900; font-size: 15px; padding-bottom: 6px; }
#spdbar, #drift { grid-column: 1 / 3; display: block; height: 7px; border-radius: 4px; background: rgba(19, 40, 79, .55); margin-top: 5px; overflow: hidden; width: 150px; }
#spdbar u, #drift u { display: block; height: 100%; width: 0%; background: #fff; border-radius: 4px; }
#drift { height: 5px; margin-top: 3px; opacity: 0; transition: opacity .2s; }
#drift.on { opacity: 1; }
#drift u { background: var(--sky); }
#drift.l1 u { background: #61e0ff; } #drift.l2 u { background: var(--pink); }
#mini { position: absolute; top: calc(10px + var(--sat)); right: calc(10px + var(--sar)); width: 138px; height: 138px; border-radius: 50%; border: 3px solid #fff; box-shadow: 0 3px 0 rgba(19, 40, 79, .35); background: #7cc36a; }
#raceinfo { position: absolute; top: calc(10px + var(--sat)); left: calc(12px + var(--sal)); color: #fff; text-shadow: 0 2px 0 var(--navy), 0 0 6px rgba(19, 40, 79, .6); font-weight: 900; }
#rpos { font-family: var(--disp); font-size: 40px; line-height: 1; }
#rpos small { font-size: 18px; margin-left: 2px; }
#rlap { font-size: 18px; margin-top: 4px; }
#rtime, #rbest { font-size: 15px; font-variant-numeric: tabular-nums; }
#toast { position: absolute; top: calc(66px + var(--sat)); left: 50%; translate: -50% 0; background: rgba(255, 255, 255, .94); color: var(--navy); font-weight: 900; padding: 8px 18px; border-radius: 999px; font-size: 16px; opacity: 0; transition: opacity .3s; white-space: nowrap; box-shadow: 0 3px 0 rgba(19, 40, 79, .25); }
#toast.on { opacity: 1; }
#center { position: absolute; top: 34%; left: 50%; translate: -50% -50%; font-family: var(--disp); font-size: 84px; color: #fff; text-shadow: 0 4px 0 var(--navy), 0 0 14px rgba(19, 40, 79, .6); white-space: nowrap; text-align: center; }
#center.small { font-family: var(--body); font-weight: 900; font-size: 26px; background: var(--pink); padding: 8px 20px; border-radius: 999px; text-shadow: none; }
#topbtns { position: absolute; top: calc(10px + var(--sat)); left: 50%; translate: -50% 0; display: flex; gap: 8px; }
.ico { width: 44px; height: 44px; border-radius: 50%; border: 0; background: rgba(255, 255, 255, .92); color: var(--navy); display: grid; place-items: center; box-shadow: 0 3px 0 rgba(19, 40, 79, .3); }
.ico svg { width: 22px; height: 22px; fill: currentColor; }
#b-ctx { position: absolute; left: 50%; bottom: calc(100px + var(--sab)); translate: -50% 0; pointer-events: auto; border: 0; background: var(--yellow); color: var(--navy); font-weight: 900; font-size: 17px; padding: 12px 26px; border-radius: 999px; box-shadow: 0 4px 0 #b38f00; min-height: 48px; }

/* タッチの操作 */
#touch { display: none; }
body.touch #touch { display: block; }
#steer { position: absolute; left: calc(18px + var(--sal)); bottom: calc(18px + var(--sab)); width: 170px; height: 96px; border-radius: 48px; background: rgba(255, 255, 255, .28); border: 3px solid rgba(255, 255, 255, .75); touch-action: none; }
#steer i { position: absolute; left: 50%; top: 50%; width: 70px; height: 70px; margin: -35px 0 0 -35px; border-radius: 50%; background: rgba(255, 255, 255, .95); box-shadow: 0 3px 0 rgba(19, 40, 79, .3); }
#steer::before, #steer::after { content: ''; position: absolute; top: 50%; width: 0; height: 0; border: 9px solid transparent; margin-top: -9px; }
#steer::before { left: 8px; border-right-color: rgba(255, 255, 255, .9); }
#steer::after { right: 8px; border-left-color: rgba(255, 255, 255, .9); }
#touch button { position: absolute; border: 0; border-radius: 50%; font-weight: 900; color: var(--navy); touch-action: none; box-shadow: 0 4px 0 rgba(19, 40, 79, .35); }
#touch button.on { translate: 0 3px; box-shadow: 0 1px 0 rgba(19, 40, 79, .35); }
#b-acc { right: calc(18px + var(--sar)); bottom: calc(18px + var(--sab)); width: 96px; height: 96px; background: #7ee2a8; font-size: 15px; }
#b-brk { right: calc(124px + var(--sar)); bottom: calc(14px + var(--sab)); width: 70px; height: 70px; background: #ffffff; font-size: 13px; }
#b-drift { right: calc(30px + var(--sar)); bottom: calc(126px + var(--sab)); width: 76px; height: 76px; background: #8fd3ff; font-size: 13px; }
@media (max-aspect-ratio: 1/1) {
  #speed { left: calc(20px + var(--sal)); translate: none; bottom: calc(128px + var(--sab)); }
  #speed b { font-size: 38px; }
  #spdbar, #drift { width: 130px; }
  body:not(.touch) #speed { bottom: calc(16px + var(--sab)); }
  #b-ctx { bottom: calc(232px + var(--sab)); }
  .setrow { grid-template-columns: 1fr; gap: 4px; }
  #mini { width: 112px; height: 112px; top: calc(62px + var(--sat)); }
  #toast { top: calc(130px + var(--sat)); }
  #raceinfo { top: calc(62px + var(--sat)); }
}

/* ---------- 画面 ---------- */
.screen { position: fixed; inset: 0; display: grid; place-items: center; padding: calc(12px + var(--sat)) calc(12px + var(--sar)) calc(12px + var(--sab)) calc(12px + var(--sal)); overflow: auto; }
.screen.dim { background: rgba(19, 40, 79, .45); }
#title { place-items: center start; padding-left: calc(5vw + var(--sal)); }
.tbox { width: min(440px, 100%); }
#title h1 { font-family: var(--disp); font-weight: 400; white-space: nowrap; font-size: clamp(30px, 7.4vw, 60px); line-height: 1.05; margin: 0; color: #fff; text-shadow: 0 4px 0 var(--navy), 0 0 16px rgba(19, 40, 79, .5); letter-spacing: .02em; }
#title .sub { color: #fff; font-weight: 900; margin: 10px 0 18px; text-shadow: 0 2px 0 var(--navy); font-size: 15px; line-height: 1.6; }
.menu { display: grid; gap: 10px; }
.big { display: grid; text-align: left; border: 0; border-radius: 18px; padding: 14px 18px; box-shadow: 0 5px 0 rgba(19, 40, 79, .35); color: var(--navy); min-height: 64px; }
.big b { font-family: var(--disp); font-weight: 400; font-size: 24px; }
.big small { font-weight: 700; font-size: 13px; margin-top: 2px; }
.big.c1 { background: #ffffff; } .big.c2 { background: var(--yellow); } .big.c3 { background: #9fe3c1; }
.big:active { translate: 0 3px; box-shadow: 0 2px 0 rgba(19, 40, 79, .35); }
.row { display: flex; gap: 10px; flex-wrap: wrap; margin-top: 12px; }
.pill { border: 0; border-radius: 999px; background: #fff; color: var(--navy); font-weight: 900; font-size: 16px; padding: 12px 22px; min-height: 48px; box-shadow: 0 4px 0 rgba(19, 40, 79, .3); }
.pill.strong { background: var(--sky); color: #fff; }
.pill:active { translate: 0 2px; box-shadow: 0 2px 0 rgba(19, 40, 79, .3); }
.keys { color: #fff; font-size: 12px; font-weight: 700; text-shadow: 0 1px 0 var(--navy); line-height: 1.7; margin-top: 16px; }
body.touch .keys { display: none; }

.sbox { width: min(980px, 100%); max-height: 100%; background: rgba(255, 255, 255, .96); border-radius: 22px; padding: 14px; display: grid; gap: 12px; box-shadow: 0 6px 0 rgba(19, 40, 79, .3); }
header { display: flex; align-items: center; gap: 12px; }
header h2 { margin: 0; font-family: var(--disp); font-weight: 400; font-size: 26px; color: var(--navy); }
.back { background: var(--soft); box-shadow: none; }
#garage { place-items: stretch; padding: 0; }
.gbox { position: relative; width: 100%; height: 100%; display: grid; grid-template-columns: minmax(0, 300px) 1fr; grid-template-rows: auto 1fr auto; gap: 10px; padding: calc(12px + var(--sat)) calc(12px + var(--sar)) calc(12px + var(--sab)) calc(12px + var(--sal)); }
.gbox header { grid-column: 1 / 3; }
.gbox header .back { background: #fff; box-shadow: 0 3px 0 rgba(19, 40, 79, .25); }
.gside { grid-column: 1; grid-row: 2; align-self: start; display: grid; gap: 10px; background: rgba(255, 255, 255, .95); border-radius: 18px; padding: 14px; box-shadow: 0 4px 0 rgba(19, 40, 79, .2); }
.gside h3 { margin: 0; font-family: var(--disp); font-weight: 400; font-size: 24px; color: var(--navy); }
.glist { grid-column: 1 / 3; grid-row: 3; display: flex; gap: 8px; overflow-x: auto; overflow-y: clip; padding-bottom: 4px; scroll-snap-type: x proximity; }
.card { flex: 0 0 150px; scroll-snap-align: start; border: 3px solid transparent; background: rgba(255, 255, 255, .95); border-radius: 14px; padding: 10px; text-align: left; color: var(--navy); display: grid; gap: 2px; min-height: 64px; box-shadow: 0 3px 0 rgba(19, 40, 79, .2); }
.card b { font-weight: 900; font-size: 16px; }
.card small { font-weight: 700; font-size: 13px; color: #33476b; }
.card.sel { border-color: var(--sky); }
.card .tag { justify-self: start; font-size: 11px; font-weight: 900; background: var(--green); color: #fff; padding: 1px 8px; border-radius: 999px; }
#gstats { display: grid; gap: 6px; }
.stat { display: grid; grid-template-columns: 4.5em 1fr auto; align-items: center; gap: 8px; font-weight: 900; font-size: 13px; }
.stat i { height: 9px; border-radius: 5px; background: #d3e2f3; overflow: hidden; }
.stat i u { display: block; height: 100%; background: var(--sky); border-radius: 5px; }
#gcolors { display: flex; flex-wrap: wrap; gap: 8px; }
#gcolors button { width: 34px; height: 34px; border-radius: 50%; border: 3px solid #fff; box-shadow: 0 0 0 2px #c9d6e6; }
#gcolors button.sel { box-shadow: 0 0 0 3px var(--navy); }
.tabs { display: flex; gap: 8px; }
.tabs button { border: 0; border-radius: 999px; padding: 10px 20px; font-weight: 900; font-size: 16px; background: var(--soft); color: var(--navy); min-height: 44px; }
.tabs button.sel { background: var(--navy); color: #fff; }
.clist { display: grid; grid-template-columns: repeat(auto-fill, minmax(210px, 1fr)); gap: 10px; overflow: auto; align-content: start; }
.course { border: 0; border-radius: 16px; background: var(--soft); padding: 10px; text-align: left; color: var(--navy); display: grid; gap: 4px; }
.course canvas { width: 100%; aspect-ratio: 16/9; border-radius: 10px; background: #dfeefb; }
.course b { font-weight: 900; font-size: 16px; }
.course small { font-weight: 700; font-size: 13px; color: #33476b; }
.course .medal { font-weight: 900; font-size: 12px; }
.help { margin: 0; font-size: 13px; font-weight: 700; color: #33476b; }
.pbox, .rbox { background: rgba(255, 255, 255, .97); border-radius: 22px; padding: 18px; display: grid; gap: 10px; min-width: min(320px, 100%); box-shadow: 0 6px 0 rgba(19, 40, 79, .3); }
.pbox h2, .rbox h2 { margin: 0 0 4px; font-family: var(--disp); font-weight: 400; color: var(--navy); font-size: 26px; text-align: center; }
.rbox h2 { font-size: 56px; }
#r-course { margin: 0; text-align: center; font-weight: 900; }
#r-medal { margin: 0; text-align: center; font-weight: 900; font-size: 18px; }
#r-times { display: grid; grid-template-columns: auto auto; gap: 4px 16px; margin: 0; font-weight: 700; font-variant-numeric: tabular-nums; justify-content: center; }
#r-times dt { color: #33476b; } #r-times dd { margin: 0; font-weight: 900; }
.mbox { display: grid; gap: 10px; justify-items: center; }
#bigmap { width: min(86vmin, 680px); height: min(86vmin, 680px); border-radius: 18px; border: 4px solid #fff; background: #3d9ad6; touch-action: none; }
.setrow { display: grid; grid-template-columns: 7em 1fr; align-items: center; gap: 10px; font-weight: 900; }
.seg { display: flex; gap: 6px; }
.seg button { flex: 1; border: 0; border-radius: 999px; padding: 10px; font-weight: 900; background: var(--soft); color: var(--navy); min-height: 44px; }
.seg button.sel { background: var(--navy); color: #fff; }

#loading { position: fixed; inset: 0; display: grid; place-items: center; background: #58a6f0; z-index: 10; transition: opacity .4s; }
#loading div { display: grid; gap: 10px; justify-items: center; color: #fff; text-shadow: 0 3px 0 var(--navy); }
#loading b { font-family: var(--disp); font-weight: 400; font-size: 44px; }
#loading span { font-weight: 900; }
#loading i { width: 200px; height: 8px; background: rgba(19, 40, 79, .4); border-radius: 4px; overflow: hidden; }
#loading u { display: block; height: 100%; width: 0; background: #fff; transition: width .2s; }
#loading.off { opacity: 0; pointer-events: none; }

@media (max-width: 760px) {
  .gbox { grid-template-columns: 1fr; }
  .gbox header { grid-column: 1; }
  .gside { grid-column: 1; grid-row: 3; padding: 10px; gap: 6px; }
  .gside h3 { font-size: 19px; }
  .glist { grid-column: 1; grid-row: 4; }
  .gbox { grid-template-rows: auto 1fr auto auto; }
  #title { place-items: center; padding-left: calc(12px + var(--sal)); }
  .clist { grid-template-columns: repeat(auto-fill, minmax(160px, 1fr)); }
}
@media (max-height: 480px) {
  .tbox { width: min(380px, 100%); }
  #title h1 { font-size: 34px; }
  #title .sub { margin: 4px 0 8px; font-size: 13px; }
  .big { min-height: 48px; padding: 8px 14px; } .big b { font-size: 19px; } .big small { font-size: 12px; }
  .menu { gap: 6px; }
  .gside { padding: 10px; gap: 6px; } .gside h3 { font-size: 19px; }
  .card { min-height: 52px; padding: 6px 10px; }
}
