:root { --red: #E0182D; --ink: #0B0B10; --panel: rgba(14,12,20,.9); --line: rgba(255,255,255,.14);
  --sl: env(safe-area-inset-left, 0px); --sr: env(safe-area-inset-right, 0px); --sb: env(safe-area-inset-bottom, 0px); }
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; height: 100%; background: var(--ink); color: #fff; overflow: hidden; overscroll-behavior: none;
  font-family: system-ui, -apple-system, "Hiragino Sans", "Noto Sans JP", sans-serif;
  -webkit-user-select: none; user-select: none; -webkit-touch-callout: none; touch-action: none; }
canvas#game { position: fixed; inset: 0; display: block; }
#safe-probe { position: fixed; left: var(--sl); right: var(--sr); top: 0; height: 0; pointer-events: none; }
#safe-probe-b { position: fixed; bottom: 0; left: 0; width: 0; height: var(--sb); pointer-events: none; }
#control-bar { display: none; }
.accel small.port { display: none; }
.hidden { display: none !important; }
button { font: inherit; color: inherit; touch-action: manipulation; }

/* ---------- レース中の操作 ---------- */
#race-ui { position: fixed; inset: 0; }
#steer-zone { position: absolute; inset: 0; }
#steer-zone .hint { position: absolute; top: 58%; font-weight: 900; font-size: 13px; color: rgba(255,255,255,.35); display: none; }
#steer-zone .hint.l { left: calc(26% + var(--sl)); } #steer-zone .hint.r { right: calc(26% + var(--sr)); }
body.touch-mode #steer-zone .hint { display: block; }
.ctl { position: absolute; border-radius: 50%; border: 3px solid rgba(255,255,255,.55); font-weight: 900; letter-spacing: .04em;
  display: flex; flex-direction: column; align-items: center; justify-content: center; touch-action: none;
  box-shadow: 0 6px 18px rgba(0,0,0,.45), inset 0 -6px 0 rgba(0,0,0,.25); transition: transform .05s; }
.ctl.on { transform: scale(.93); filter: brightness(1.25); }
.accel { right: calc(18px + var(--sr)); bottom: calc(16px + var(--sb)); width: 128px; height: 128px; font-size: 20px;
  background: radial-gradient(circle at 40% 35%, #FF4D5E, #B40F20); }
.accel small { font-size: 9px; font-weight: 700; opacity: .85; margin-top: 3px; letter-spacing: 0; }
.brake { left: calc(18px + var(--sl)); bottom: calc(16px + var(--sb)); width: 96px; height: 96px; font-size: 15px;
  background: radial-gradient(circle at 40% 35%, #555A66, #23252C); }
.item { left: calc(30px + var(--sl)); bottom: calc(126px + var(--sb)); width: 84px; height: 84px; font-size: 11px;
  background: radial-gradient(circle at 40% 35%, #3A3450, #17141F); border-color: #FFE45C; }
.item canvas { width: 54px; height: 54px; margin-top: -4px; }
.item span { margin-top: -4px; }
.pause { position: absolute; top: 10px; right: calc(12px + var(--sr)); width: 40px; height: 36px; border-radius: 10px;
  background: rgba(10,8,14,.62); border: 1px solid var(--line); font-weight: 900; }
@media (max-height: 380px) {
  .accel { width: 112px; height: 112px; } .brake { width: 84px; height: 84px; }
  .item { width: 74px; height: 74px; bottom: calc(108px + var(--sb)); left: calc(24px + var(--sl)); }
  .item canvas { width: 46px; height: 46px; }
}

/* ---------- 画面共通 ---------- */
.screen { position: fixed; inset: 0; background: radial-gradient(ellipse at 30% 20%, #3A0B14, #0B0B10 70%); overflow: hidden;
  padding: 12px calc(14px + var(--sr)) 12px calc(14px + var(--sl)); }
.bgcanvas { position: absolute; inset: 0; width: 100%; height: 100%; }
.screen header { display: flex; align-items: center; justify-content: space-between; gap: 10px; margin-bottom: 8px; }
.screen h2 { margin: 0; font-size: 20px; font-style: italic; letter-spacing: .04em; }
.btn { background: #2A2633; border: 1px solid rgba(255,255,255,.25); border-radius: 12px; padding: 10px 18px; font-weight: 800; font-size: 15px; min-height: 44px; }
.btn.primary { background: linear-gradient(180deg, #FF3345, #C20F22); border-color: #FF8090; }
.btn.big { font-size: 20px; padding: 12px 34px; }
.btn.small { padding: 6px 12px; font-size: 13px; min-height: 36px; }
.btn.gyro-btn { background: linear-gradient(180deg, #24C7DD, #127C8C); border-color: #8EF2FF; }
.btn.gyro-btn.done { background: #1E3B40; border-color: #3FA; }
.btn:active { filter: brightness(1.2); transform: translateY(1px); }
.row { display: flex; gap: 10px; justify-content: center; flex-wrap: wrap; margin-top: 10px; }
.row.col { flex-direction: column; align-items: stretch; }
.note { font-size: 12px; opacity: .8; margin: 6px 0 0; line-height: 1.5; }

/* タイトル */
.title-box { position: relative; z-index: 1; height: 100%; display: flex; flex-direction: column; align-items: center; justify-content: flex-start; padding-top: 4vh; text-align: center; }
.logo { display: flex; align-items: baseline; gap: 12px; transform: skewX(-10deg); filter: drop-shadow(0 6px 0 rgba(0,0,0,.5)); }
.logo .aox { font-size: clamp(38px, 11vh, 72px); font-weight: 900; color: #fff; background: var(--red); padding: 0 14px; border-radius: 8px; }
.logo .kart { font-size: clamp(30px, 9vh, 58px); font-weight: 900; font-style: italic; color: #fff; -webkit-text-stroke: 2px var(--red); }
.sub { margin: 8px 0 0; font-weight: 700; opacity: .9; }

/* キャラ選択 */
#scr-select { display: flex; gap: 12px; }
.sel-left { width: 34%; min-width: 220px; display: flex; gap: 6px; background: var(--panel); border: 1px solid var(--line); border-radius: 16px; padding: 8px; }
#sel-preview { height: 100%; max-height: calc(100vh - 40px); width: auto; aspect-ratio: 4 / 5; flex: 0 1 auto; min-width: 0; max-width: 55%; object-fit: contain; }
.sel-info { flex: 1; display: flex; flex-direction: column; justify-content: center; min-width: 0; }
.sel-name { font-size: 19px; font-weight: 900; font-style: italic; }
.sel-type { font-size: 12px; opacity: .85; margin-bottom: 8px; }
.stats { display: grid; gap: 5px; }
.stat { font-size: 11px; display: grid; grid-template-columns: 52px 1fr; align-items: center; gap: 6px; }
.stat .bar { height: 8px; background: #2A2633; border-radius: 4px; overflow: hidden; }
.stat .bar i { display: block; height: 100%; background: linear-gradient(90deg, #FF3345, #FFE45C); }
.sel-right { flex: 1; display: flex; flex-direction: column; min-width: 0; }
.char-grid { flex: 1; display: grid; grid-template-columns: repeat(6, 1fr); grid-auto-rows: 1fr; gap: 6px; overflow-y: auto; touch-action: pan-y; }
.card { position: relative; background: #1A1722; border: 2px solid rgba(255,255,255,.12); border-radius: 12px; padding: 2px; display: flex; flex-direction: column; align-items: center; justify-content: flex-end; overflow: hidden; min-height: 0; }
.card canvas { width: 100%; flex: 1; min-height: 0; object-fit: contain; }
.card b { font-size: 12px; font-style: italic; white-space: nowrap; }
.card .tag { position: absolute; top: 3px; left: 3px; font-size: 8px; font-weight: 900; padding: 1px 5px; border-radius: 6px; background: var(--red); }
.card .stripe { position: absolute; left: 0; right: 0; bottom: 0; height: 4px; }
.card.sel { border-color: #FFE45C; box-shadow: 0 0 0 2px #FFE45C inset, 0 0 16px rgba(255,228,92,.5); }

/* コース選択 */
#scr-course { display: flex; flex-direction: column; }
.course-grid { flex: 1; display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; min-height: 0; }
.course { background: var(--panel); border: 2px solid var(--line); border-radius: 16px; padding: 10px; display: flex; flex-direction: column; gap: 6px; text-align: left; min-height: 0; }
.course canvas { width: 100%; flex: 1; min-height: 60px; border-radius: 10px; object-fit: cover; }
.course .en { font-size: 11px; font-weight: 900; color: #FF8090; letter-spacing: .08em; }
.course .nm { font-size: 17px; font-weight: 900; }
.course .ds { font-size: 11px; opacity: .85; line-height: 1.4; }
.course .lv { font-size: 12px; color: #FFE45C; }

/* 結果 */
.result-panel { position: relative; z-index: 1; margin-left: auto; width: 54%; height: 100%; background: var(--panel); border: 1px solid var(--line); border-radius: 16px; padding: 10px 14px; overflow-y: auto; touch-action: pan-y; }
.res-you { font-size: 15px; font-weight: 800; margin: 2px 0 6px; }
.res-list { list-style: none; padding: 0; margin: 0; display: grid; grid-template-columns: 1fr 1fr; gap: 3px 10px; }
.res-list li { display: grid; grid-template-columns: 26px 8px 1fr auto; align-items: center; gap: 6px; font-size: 12px; padding: 3px 6px; border-radius: 8px; background: rgba(255,255,255,.05); }
.res-list li.me { background: rgba(224,24,45,.45); font-weight: 900; }
.res-list .pl { font-weight: 900; font-style: italic; }
.res-list .sw { width: 8px; height: 16px; border-radius: 3px; }
.res-list .tm { font-variant-numeric: tabular-nums; opacity: .9; }

/* モーダル */
.modal { position: fixed; inset: 0; background: rgba(0,0,0,.6); display: flex; align-items: center; justify-content: center; z-index: 10; }
.modal-box { background: #15121C; border: 1px solid var(--line); border-radius: 16px; padding: 14px 18px; width: min(560px, 92vw); max-height: 92vh; overflow-y: auto; touch-action: pan-y; }
.modal-box h2 { margin: 0 0 8px; font-size: 18px; }
.set-row { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; margin: 8px 0; font-size: 14px; }
.set-row > label:first-child { min-width: 120px; font-weight: 700; }
.seg { display: flex; border: 1px solid var(--line); border-radius: 10px; overflow: hidden; }
.seg button { background: #221E2A; border: 0; padding: 9px 12px; font-weight: 800; font-size: 13px; }
.seg button.on { background: var(--red); }
input[type=range] { flex: 1; min-width: 160px; accent-color: var(--red); touch-action: pan-x; }
input[type=checkbox] { width: 20px; height: 20px; vertical-align: middle; accent-color: var(--red); }
.meter { position: relative; width: 180px; height: 14px; background: #2A2633; border-radius: 7px; }
.meter::after { content: ''; position: absolute; left: 50%; top: -3px; bottom: -3px; width: 2px; background: #fff; opacity: .5; }
#meter-dot { position: absolute; top: 1px; width: 12px; height: 12px; border-radius: 50%; background: #24C7DD; left: calc(50% - 6px); }

.rotate { display: none; position: fixed; inset: 0; z-index: 50; background: #0B0B10; align-items: center; justify-content: center; text-align: center; font-size: 48px; }
.rotate p { font-size: 16px; }


/* ================= 縦画面（片手操作）：画面下に BRAKE / ACCEL / ITEM を横一列 ================= */
@media (orientation: portrait) {
  :root { --barh: 150px; }
  #control-bar { display: block; position: absolute; left: 0; right: 0; bottom: 0; height: calc(var(--barh) + var(--sb));
    background: linear-gradient(180deg, #1A1418, #0B0B10); border-top: 3px solid var(--red); }
  #steer-zone { bottom: calc(var(--barh) + var(--sb)); }
  #steer-zone .hint { top: 72%; }
  .ctl { bottom: calc(22px + var(--sb)); }
  .brake { left: calc(16px + var(--sl)); width: 92px; height: 92px; bottom: calc(30px + var(--sb)); }
  .accel { left: calc(50% - 62px); right: auto; width: 124px; height: 124px; bottom: calc(14px + var(--sb)); }
  .accel small.land { display: none; } .accel small.port { display: block; font-size: 8px; max-width: 96px; text-align: center; line-height: 1.2; }
  .item { left: auto; right: calc(16px + var(--sr)); width: 92px; height: 92px; bottom: calc(30px + var(--sb)); }
  .item canvas { width: 56px; height: 56px; }

  /* キャラ選択：上にプレビュー、下に4列 */
  #scr-select { flex-direction: column; gap: 8px; padding-top: calc(10px + env(safe-area-inset-top, 0px)); }
  .sel-left { width: 100%; min-width: 0; height: 34%; }
  #sel-preview { max-width: 45%; }
  .char-grid { grid-template-columns: repeat(4, 1fr); }
  .sel-right header h2 { font-size: 17px; }
  /* コース選択：縦に並べる */
  #scr-course { padding-top: calc(10px + env(safe-area-inset-top, 0px)); }
  .course-grid { grid-template-columns: 1fr; grid-auto-rows: 1fr; overflow-y: auto; touch-action: pan-y; }
  .course { flex-direction: row; flex-wrap: wrap; align-content: flex-start; }
  .course canvas { flex: 0 0 42%; height: 100%; min-height: 0; max-height: 120px; }
  .course .txt { flex: 1; min-width: 0; }
  /* 結果：上に表彰台、下に順位 */
  .result-panel { position: absolute; left: 10px; right: 10px; bottom: calc(10px + var(--sb)); width: auto; height: 56%; margin: 0; }
  .res-list { grid-template-columns: 1fr; }
  /* タイトル */
  .title-box { padding-top: calc(12vh + env(safe-area-inset-top, 0px)); }
  .logo { flex-direction: column; align-items: center; gap: 4px; }
}
