:root {
  --ink: #23262b;
  --player: #e0413a;
  --paper: #ffffff;
  --line: #c5c9d2;
  --shadow: 0 10px 30px rgba(40, 50, 120, .25);
}
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body {
  height: 100%; margin: 0; overflow: hidden; background: #7fc8ff;
  font-family: "Hiragino Maru Gothic ProN", "Hiragino Sans", "Noto Sans JP", system-ui, sans-serif;
  color: var(--ink);
  -webkit-user-select: none; user-select: none;
}
button { font: inherit; }

#race { position: fixed; inset: 0; width: 100%; height: 100%; display: block; touch-action: none; }

/* 上部 HUD */
#top {
  position: fixed; left: 0; right: 0; top: 0;
  padding: max(10px, env(safe-area-inset-top)) 12px 0;
  display: flex; align-items: center; gap: 10px; pointer-events: none;
}
#courses { display: flex; gap: 6px; pointer-events: auto; }
.cbtn {
  width: 40px; height: 40px; border-radius: 12px; border: 2px solid rgba(255, 255, 255, .8);
  background: rgba(255, 255, 255, .55); color: var(--ink); font-weight: 700; font-size: 18px;
  cursor: pointer; backdrop-filter: blur(4px);
}
.cbtn.active { background: var(--ink); color: #fff; border-color: var(--ink); }
#cname { flex: 1; display: flex; flex-direction: column; line-height: 1.15; min-width: 0; }
#cname-text { font-weight: 700; font-size: 16px; text-shadow: 0 1px 0 rgba(255, 255, 255, .6); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
#cbest { font-size: 12px; opacity: .8; }
#timer {
  font-variant-numeric: tabular-nums; font-weight: 800; font-size: 30px; letter-spacing: .5px;
  color: var(--ink); text-shadow: 0 1px 0 rgba(255, 255, 255, .7); min-width: 92px; text-align: right;
}

body.dark #cname-text, body.dark #cbest, body.dark #timer { color: #fff; text-shadow: 0 1px 3px rgba(0, 0, 0, .7); }
body.dark .cbtn { background: rgba(255, 255, 255, .25); color: #fff; }
body.dark .cbtn.active { background: #fff; color: var(--ink); border-color: #fff; }

/* 描画パッド */
#padwrap {
  position: fixed; left: 50%; bottom: max(10px, env(safe-area-inset-bottom)); transform: translateX(-50%);
  width: min(86vw, 440px); display: flex; flex-direction: column; align-items: center; gap: 8px;
}
#pad {
  width: 100%; aspect-ratio: 5 / 3; display: block; background: var(--paper);
  border: 3px dashed var(--line); border-radius: 20px; box-shadow: var(--shadow);
  touch-action: none; cursor: crosshair;
}
#padhint {
  position: absolute; top: 10px; left: 0; right: 0; text-align: center; pointer-events: none;
  font-weight: 700; font-size: 15px; color: #7a8290;
}
#padhint.hidden { display: none; }
#padwrap.replay #pad { border-color: #e0a83a; border-style: solid; cursor: default; }
#padbtns { display: flex; gap: 8px; min-height: 40px; }
#padbtns button, .rbtns button {
  border: 2px solid var(--ink); border-radius: 999px; padding: 8px 18px; font-weight: 700; font-size: 15px; cursor: pointer;
}
button { touch-action: manipulation; -webkit-tap-highlight-color: transparent; }
button.main { background: var(--ink); color: #fff; }
button.sub { background: #fff; color: var(--ink); }
button[hidden] { display: none !important; }

/* リザルト・シェア */
/* リザルトは画面全体を覆わず、パネルの大きさだけ置く（下のパッドはそのまま描ける）。
   全画面の pointer-events:none の箱に入れると iOS Safari で最初のタップが効かなくなる */
#result {
  position: fixed; left: 50%; transform: translateX(-50%); width: min(88vw, 360px);
  top: max(70px, calc(env(safe-area-inset-top) + 60px));
}
#sharebox {
  position: fixed; inset: 0; display: flex; align-items: center; justify-content: center;
  background: rgba(0, 0, 0, .35);
}
#result[hidden], #sharebox[hidden] { display: none; }
.panel {
  width: min(88vw, 360px); background: rgba(255, 255, 255, .96);
  border: 2px solid var(--ink); border-radius: 18px; padding: 18px 18px 16px; text-align: center; box-shadow: var(--shadow);
}
.rtitle { font-size: 26px; font-weight: 800; color: var(--player); }
.rcourse { font-size: 14px; margin-top: 4px; opacity: .8; }
.rtime { font-size: 40px; font-weight: 800; font-variant-numeric: tabular-nums; margin-top: 6px; }
.rtime .unit { font-size: 18px; margin-left: 4px; }
.rbest { font-size: 14px; min-height: 20px; margin-top: 2px; }
.rbest.record { color: var(--player); font-weight: 800; }
.rghosts { font-size: 13px; margin-top: 8px; line-height: 1.6; }
.rghosts .win { color: var(--player); }
.gdot { display: inline-block; width: 10px; height: 10px; border-radius: 50%; margin-right: 5px; vertical-align: -1px; }
#toast {
  position: fixed; left: 50%; top: 45%; transform: translateX(-50%); max-width: 86vw; z-index: 20;
  background: rgba(35, 38, 43, .92); color: #fff; font-weight: 700; font-size: 14px; padding: 10px 16px; border-radius: 12px; text-align: center;
  pointer-events: none;
}
#toast[hidden] { display: none; }
.rbtns { display: flex; flex-wrap: wrap; gap: 8px; justify-content: center; margin-top: 12px; }
#shareimg { width: 100%; border-radius: 10px; border: 1px solid var(--line); }
.note { font-size: 12px; opacity: .7; margin-top: 6px; }
#sharetext { width: 100%; height: 90px; margin-top: 8px; font-family: inherit; font-size: 13px; line-height: 1.4; border: 1px solid var(--line); border-radius: 8px; padding: 6px; resize: none; }
