:root {
  --bg: #2b3a55;
  --ink: #fcfcfc;
  --red: #e84a5f;
  --gold: #ffd84a;
  --paper: #fbf8ef;
  --pen: #2b3a55;
}

* { box-sizing: border-box; }

/* iPhone のダブルタップ拡大を止める（viewport の user-scalable=no は iOS では無視される） */
html, body, * { touch-action: manipulation; }

html, body {
  margin: 0;
  height: 100%;
  overflow: hidden;
  background: var(--bg);
  color: var(--ink);
  font-family: "M PLUS 1p", "Hiragino Kaku Gothic ProN", sans-serif;
  font-weight: 500;
  -webkit-text-size-adjust: 100%;
  overscroll-behavior: none;
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
}
.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip: rect(0 0 0 0);
}
[hidden] { display: none !important; }

/* 画面いっぱいに 3D を描く */
.screen-wrap {
  position: fixed;
  inset: 0;
  background: #9fd0f5;
  line-height: 0;
}
#screen, #hud {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  display: block;
}
#hud { pointer-events: none; }
/* 長押しの早送りで、文字の選択やメニューが出ないように */
.screen-wrap { -webkit-touch-callout: none; -webkit-user-select: none; user-select: none; }
.loading {
  position: absolute;
  inset: 0;
  margin: 0;
  display: grid;
  place-items: center;
  line-height: 1;
  color: #2b3a55;
  font-size: 14px;
  pointer-events: none;
}
body.ready .loading { display: none; }

.mini {
  font: inherit;
  font-size: 16px;
  font-weight: 800;
  color: var(--ink);
  background: rgba(20, 24, 40, 0.55);
  border: 1px solid rgba(255, 255, 255, 0.5);
  border-radius: 8px;
  padding: 5px 10px;
  cursor: pointer;
}
.corner {
  position: fixed;
  top: calc(8px + env(safe-area-inset-top));
  right: calc(8px + env(safe-area-inset-right));
  z-index: 20;
}
.mini.on { background: #e84a5f; border-color: #fff; color: #fff; }
body[data-mode="edit"] .corner { color: var(--pen); background: rgba(255, 255, 255, 0.7); border-color: #c8d0dc; }

/* ボタン */
.btn {
  font: inherit;
  font-weight: 800;
  font-size: 15px;
  padding: 11px 8px;
  border: 2px solid rgba(255, 255, 255, 0.9);
  border-radius: 12px;
  cursor: pointer;
  color: #fff;
  background: #4c5468;
}
.btn:active { transform: translateY(2px); }
.btn:disabled { opacity: 0.4; cursor: default; transform: none; }
.btn-main { background: var(--red); box-shadow: 0 3px 0 #a8283a; }
.btn-sub2 { background: #4c5468; }
.btn-x { background: #000; }
.btn-line { background: #06c755; }
.btn-share { background: #0058f8; }
.btn-fav { background: #d89a00; box-shadow: 0 3px 0 #9a6c00; }

/* ---------- タイトル ---------- */
.menu {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  padding: 14px 16px calc(10px + env(safe-area-inset-bottom));
  text-align: center;
  background: linear-gradient(rgba(43, 58, 85, 0), rgba(43, 58, 85, 0.88) 35%);
  z-index: 3;
  line-height: 1.6;
}
.menu-btns { display: flex; flex-direction: column; align-items: center; gap: 10px; margin-bottom: 10px; }
.menu-btns .btn { width: min(320px, 86vw); }
.menu-btns .btn-main { font-size: 20px; padding: 14px; }
.menu p { margin: 0 0 2px; font-size: 12px; color: #e0e8f4; }
.menu .how { font-weight: 800; }
.menu .note { font-size: 11px; color: #b8c4d8; }
.menu .ver {
  position: absolute;
  right: calc(12px + env(safe-area-inset-right));
  bottom: calc(10px + env(safe-area-inset-bottom));
  font-size: 11px;
  color: #b8c4d8;
}
.menu a { color: #fff; }

/* ---------- カード（届いたコース・結果） ---------- */
.card {
  position: fixed;
  left: 50%;
  transform: translateX(-50%);
  width: min(460px, calc(100% - 20px));
  background: rgba(20, 26, 44, 0.86);
  border: 2px solid rgba(255, 255, 255, 0.25);
  border-radius: 16px;
  padding: 14px 14px 12px;
  z-index: 8;
  text-align: center;
}
.intro { top: 50%; transform: translate(-50%, -50%); display: flex; flex-direction: column; gap: 10px; }
.intro .kicker { margin: 0; color: var(--gold); font-weight: 800; font-size: 13px; }
.intro h2 { margin: 0; font-family: "Dela Gothic One", sans-serif; font-weight: 400; font-size: 24px; }
.intro .meta { margin: 0; font-size: 12px; color: #c8d0e0; }
.result {
  bottom: calc(10px + env(safe-area-inset-bottom));
  max-height: calc(100% - 20px - env(safe-area-inset-top) - env(safe-area-inset-bottom));
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 7px;
}
.result p { margin: 0; }
.result .stamp {
  font-family: "Dela Gothic One", sans-serif;
  font-size: 40px;
  line-height: 1.1;
  transform: rotate(-4deg);
  text-shadow: 0 3px 0 #000, 0 0 12px rgba(0, 0, 0, 0.5);
}
.result .head { font-weight: 800; font-size: 16px; }
.result .stars { font-size: 13px; color: #c8d0e0; }
.result .stars b { color: var(--gold); font-weight: 800; letter-spacing: 1px; }
.result .grid { display: grid; grid-template-columns: repeat(4, 1fr); gap: 4px; }
.result .grid div { background: rgba(255, 255, 255, 0.07); border-radius: 8px; padding: 4px 2px; display: flex; flex-direction: column; }
.result .grid span { font-size: 10px; color: #a8b4c8; }
.result .grid b { font-size: 14px; }
.result .crowd { font-size: 12px; color: #e0e8f4; }
.result .share-lead { font-size: 11px; color: var(--gold); }
.share-row { display: flex; gap: 6px; }
.share-row .btn { flex: 1; padding: 9px 4px; font-size: 14px; }
.share-row .narrow { flex: 0.6; }
.follow {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 14px;
  font-size: 11px;
  color: #a8b4c8;
}
.follow a { color: #d8e0f0; display: flex; padding: 2px; }
.follow svg { width: 16px; height: 16px; fill: currentColor; }

/* ---------- お気に入り ---------- */
.result .fav-note { font-size: 11px; color: var(--gold); min-height: 0; }
.result .fav-note:empty { display: none; }
.favs {
  top: calc(56px + env(safe-area-inset-top));
  bottom: calc(10px + env(safe-area-inset-bottom));
  display: flex;
  flex-direction: column;
  gap: 8px;
  text-align: left;
}
.favs h2 { margin: 0; font-family: "Dela Gothic One", sans-serif; font-weight: 400; font-size: 22px; color: var(--gold); }
.favs h2 small { font-family: inherit; font-size: 12px; color: #a8b4c8; }
.fav-list { flex: 1; min-height: 0; overflow-y: auto; display: flex; flex-direction: column; gap: 8px; }
.fav-empty { margin: 0; font-size: 13px; color: #c8d0e0; }
.fav-item { background: rgba(255, 255, 255, 0.07); border-radius: 12px; padding: 8px 10px; }
.fav-name { font-weight: 800; font-size: 16px; word-break: break-all; }
.fav-meta { font-size: 11px; color: #a8b4c8; margin: 1px 0 6px; }
.fav-btns { display: flex; gap: 5px; }
.fav-btns .btn { flex: 1; padding: 7px 2px; font-size: 13px; }
.fav-btns .btn-main { flex: 1.8; }

/* ---------- 走っている間 ---------- */
/* 走っている間の操作：画面の下の帯（0.1.33 で右上の縦並びから移した）。上の段が再生バー、下の段がボタン */
.ride-ui {
  position: fixed;
  left: 50%;
  transform: translateX(-50%);
  bottom: calc(8px + env(safe-area-inset-bottom));
  width: min(560px, calc(100% - 16px - env(safe-area-inset-left) - env(safe-area-inset-right)));
  display: flex;
  flex-direction: column;
  gap: 4px;
  padding: 6px 8px;
  background: rgba(20, 24, 40, 0.6);
  border-radius: 14px;
  z-index: 6;
}
.pl-row { display: flex; gap: 6px; }
.pl-row .mini { flex: 1 1 0; min-width: 0; padding: 7px 2px; font-size: 13px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.pl-row #btn-play { flex: 0 0 44px; font-size: 16px; }
/* 文字の長さに合わせて幅を分ける（「🎥 追いかけ」がスマホで「追い…」に切れていた。0.1.34） */
.pl-row #btn-cam { flex-grow: 1.5; }
.pl-row #btn-speed { flex-grow: 1.1; }
.pl-row #btn-bar { flex-grow: 0.7; }
.pl-row #btn-skip { flex-grow: 1.2; }
.pl-row .mini.on { background: #e84a5f; }
.ride-ui[data-build] #btn-play, .ride-ui[data-build] #btn-bar, .ride-ui[data-build] .pl-bar { display: none; }
.ride-ui[data-bar="off"] .pl-bar { display: none; }
.pl-bar { display: flex; align-items: center; gap: 8px; padding: 14px 2px 0; }
.pl-time { font-size: 12px; font-weight: 800; color: #fff; min-width: 34px; text-align: center; font-variant-numeric: tabular-nums; }
.pl-track { position: relative; flex: 1; height: 26px; touch-action: none; cursor: pointer; }
.pl-track::before { content: ""; position: absolute; left: 0; right: 0; top: 50%; height: 6px; transform: translateY(-50%); background: rgba(255, 255, 255, 0.3); border-radius: 3px; }
.pl-fill { position: absolute; left: 0; top: 50%; height: 6px; transform: translateY(-50%); background: #e84a5f; border-radius: 3px; width: 0; pointer-events: none; }
.pl-knob { position: absolute; top: 50%; width: 18px; height: 18px; margin-left: -9px; transform: translateY(-50%); background: #fff; border: 3px solid #e84a5f; border-radius: 50%; left: 0; pointer-events: none; box-shadow: 0 1px 4px rgba(0, 0, 0, 0.4); }
/* 事故などの印：タップでその少し手前へ */
.pl-marks span { position: absolute; bottom: 62%; transform: translateX(-50%); font-size: 14px; line-height: 1; cursor: pointer; padding: 2px; filter: drop-shadow(0 1px 1px rgba(0, 0, 0, 0.6)); }

/* ---------- 設計（方眼紙） ---------- */
.editor {
  position: fixed;
  inset: 0;
  z-index: 10;
  background: var(--paper);
  color: var(--pen);
  display: flex;
  flex-direction: column;
  padding: calc(8px + env(safe-area-inset-top)) calc(10px + env(safe-area-inset-right)) calc(8px + env(safe-area-inset-bottom)) calc(10px + env(safe-area-inset-left));
  gap: 6px;
}
.ed-head { padding-right: 52px; }
.ed-head h2 { margin: 0; font-family: "Dela Gothic One", sans-serif; font-weight: 400; font-size: 20px; color: var(--red); }
.ed-titlerow { display: flex; flex-wrap: wrap; align-items: center; gap: 4px 12px; }
/* 手順の印（① 道 → ② 高さ → ③ しかけ → ④ 乗り物）。いまの段階を赤く、済んだ段階を薄い赤にする */
.ed-steps { display: flex; margin: 0; padding: 0; list-style: none; counter-reset: st; font-size: 12px; font-weight: 800; }
.ed-steps li { counter-increment: st; display: flex; align-items: center; gap: 3px; padding: 2px 8px 2px 3px; color: #8a98ae; background: #e8edf3; border-radius: 999px; }
.ed-steps li + li { margin-left: 14px; position: relative; }
.ed-steps li + li::after { content: ""; position: absolute; left: -12px; top: 50%; width: 10px; border-top: 2px solid #c8d4e0; }
.ed-steps li::before { content: counter(st); width: 17px; height: 17px; display: grid; place-items: center; border-radius: 50%; background: #fff; font-size: 11px; }
.ed-steps li.done { color: var(--red); background: #ffe8ec; }
.ed-steps li.on { color: #fff; background: var(--red); }
.ed-steps li.on::before { color: var(--red); }
/* 段階が切りかわったときに、見出しと図をふわっと出し直す */
.editor.ed-enter .ed-head h2, .editor.ed-enter .ed-canvas-wrap, .editor.ed-enter .ed-opts { animation: ed-in 0.35s ease-out; }
@keyframes ed-in { from { opacity: 0; transform: translateX(24px); } to { opacity: 1; transform: none; } }
@media (prefers-reduced-motion: reduce) { .editor.ed-enter .ed-head h2, .editor.ed-enter .ed-canvas-wrap, .editor.ed-enter .ed-opts { animation: none; } }
.ed-info { margin: 2px 0 0; font-size: 12px; font-weight: 800; line-height: 1.5; }
.ed-info .over { color: #d02020; }
.ed-canvas-wrap {
  flex: 1;
  min-height: 160px;
  position: relative;
  border: 2px solid #c8d4e0;
  border-radius: 10px;
  overflow: hidden;
  background: var(--paper);
}
.editor[data-step="4"] .ed-canvas-wrap { display: none; }
/* 元に戻す・やり直す（図の右上の小さなボタン） */
.ed-undo { position: absolute; top: 6px; right: 6px; display: flex; gap: 4px; z-index: 2; }
.ed-undo button { width: 34px; height: 30px; padding: 0; font: inherit; font-size: 18px; font-weight: 800; line-height: 1; color: var(--pen); background: rgba(255, 255, 255, 0.9); border: 2px solid #c8d4e0; border-radius: 8px; cursor: pointer; }
.ed-undo button:disabled { opacity: 0.3; cursor: default; }
#ed-canvas { position: absolute; inset: 0; touch-action: none; cursor: crosshair; }
/* おまかせの結果のカード（図の上・まん中。0.1.35。前は図の下の小さな一行だけで、押しても何も変わらないように見えた） */
.ed-toast { position: absolute; left: 50%; top: 44px; transform: translateX(-50%); width: max-content; max-width: calc(100% - 24px); box-sizing: border-box; display: none; z-index: 3; padding: 10px 16px; text-align: center; color: var(--pen); background: #fff; border: 3px solid #3ab05a; border-radius: 14px; box-shadow: 0 6px 18px rgba(0, 0, 0, 0.25); cursor: pointer; }
.ed-toast.show { display: block; animation: toast-in 0.3s cubic-bezier(0.2, 1.4, 0.4, 1); }
@keyframes toast-in { from { opacity: 0; transform: translate(-50%, -12px) scale(0.85); } to { opacity: 1; transform: translateX(-50%); } }
@media (prefers-reduced-motion: reduce) { .ed-toast.show { animation: none; } }
.ed-toast b { display: block; font-size: 17px; font-weight: 800; color: #2a8a46; }
.ed-toast small { display: block; margin-top: 4px; font-size: 12px; line-height: 1.5; color: #4a5a74; }
.ed-toast.warn { border-color: #f0a020; background: #fff8e6; }
.ed-toast.warn b { color: #c05a00; }
.ed-toast.busy { border-color: #3a8ae8; }
.ed-toast.busy b { color: #2a6ad0; }
.ed-toast .spin { display: inline-block; width: 15px; height: 15px; margin-right: 8px; vertical-align: -2px; border: 3px solid #c8daf4; border-top-color: #3a8ae8; border-radius: 50%; animation: spin 0.8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
/* 考え中は図を暗くする */
.ed-canvas-wrap.ed-busy #ed-canvas { opacity: 0.4; transition: opacity 0.15s; }
/* 図の凡例 */
.ed-legend { margin: 0; display: flex; flex-wrap: wrap; gap: 2px 12px; font-size: 11px; font-weight: 800; color: #5a6a84; }
.ed-legend:empty, .editor[data-step="4"] .ed-legend { display: none; }
.ed-legend span { display: inline-flex; align-items: center; gap: 4px; white-space: nowrap; }
.lg { display: inline-block; width: 18px; height: 0; border-top: 3px solid #2b3a55; border-radius: 2px; }
.lg.red { border-top: 2px dashed #e84a5f; }
.lg.faint { border-top-color: rgba(43, 58, 85, 0.3); }
.lg.blue { border-top: 2px solid #8ac4f8; }
.lg.box { width: 12px; height: 10px; border: 0; background: rgba(60, 60, 60, 0.18); }
.lg.orange { width: 18px; height: 7px; border: 0; border-radius: 4px; background: rgba(255, 150, 40, 0.5); }
.lg.green { width: 14px; height: 6px; border: 0; background: rgba(80, 190, 90, 0.6); }
.ed-hint { margin: 0; font-size: 12px; color: #5a6a84; min-height: 1.5em; }
.ed-tools { display: flex; gap: 6px; flex-wrap: wrap; }
/* ③ のしかけのボタンは大きく、目立つ帯にのせる（② との違いが分かるように） */
.editor[data-step="3"] .ed-tools { gap: 8px; padding: 8px; background: #fff3d6; border: 2px solid #f8d27a; border-radius: 12px; }
.editor[data-step="3"] .ed-tools .chip { font-size: 15px; padding: 8px 16px; }
.editor[data-step="3"] .ed-tools .chip i { width: 14px; height: 14px; }
.editor[data-step="3"] #ed-canvas { cursor: pointer; }
.ed-tools:empty, .ed-opts:empty { display: none; }
.ed-opts { display: flex; flex-direction: column; gap: 6px; overflow-y: auto; min-height: 0; flex: 1; }
/* ④ 乗り物の設定：項目ごとのカード */
.ed-opts { gap: 10px; }
.opt { display: flex; flex-wrap: wrap; align-items: center; gap: 6px; background: #fff; border: 2px solid #e2e8f0; border-radius: 14px; padding: 10px; }
.opt .lab { flex-basis: 100%; font-weight: 800; font-size: 16px; color: var(--pen); }
.opt .chip { flex: 1 1 0; justify-content: center; flex-direction: column; gap: 0; padding: 8px 4px; font-size: 14px; min-width: 0; }
.opt .note { flex-basis: 100%; font-size: 12px; color: #6a7a94; }
.chip {
  font: inherit;
  font-size: 13px;
  font-weight: 800;
  color: var(--pen);
  background: #fff;
  border: 2px solid #c8d4e0;
  border-radius: 999px;
  padding: 5px 10px;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  gap: 4px;
}
.chip small { font-size: 10px; color: #7a8aa4; font-weight: 500; }
.chip.on { border-color: var(--red); background: #ffe8ec; }
.chip i { width: 12px; height: 12px; border-radius: 50%; display: inline-block; }
/* ④ の下の「見た目を変える」（装飾の入口。4 項目より下に置き、色の帯で目立たせる） */
.look-btn { flex: none; width: 100%; font: inherit; font-size: 15px; font-weight: 800; color: #fff; background: linear-gradient(90deg, #e84a5f, #ff9a30, #f0b800, #4ac060, #3a8ae8, #9a5ad8); border: 0; border-radius: 14px; padding: 10px 12px; box-shadow: 0 3px 0 rgba(43, 58, 85, 0.35); text-shadow: 0 1px 2px rgba(0, 0, 0, 0.45); cursor: pointer; display: flex; align-items: center; justify-content: center; gap: 8px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.look-btn:active { transform: translateY(2px); box-shadow: 0 1px 0 rgba(43, 58, 85, 0.35); }
.look-btn small { font-size: 11px; font-weight: 800; opacity: 0.95; }
.look-btn b { font-size: 12px; background: rgba(255, 255, 255, 0.92); color: var(--pen); text-shadow: none; border-radius: 999px; padding: 2px 8px; overflow: hidden; text-overflow: ellipsis; }
.look-btn::after { content: "▶"; font-size: 11px; }
/* 見た目の小窓 */
.look-sheet { position: fixed; inset: 0; z-index: 20; background: rgba(20, 26, 44, 0.45); display: flex; align-items: flex-end; justify-content: center; padding: 10px calc(10px + env(safe-area-inset-right)) calc(10px + env(safe-area-inset-bottom)) calc(10px + env(safe-area-inset-left)); }
.look-sheet[hidden] { display: none; }
.look-box { width: min(460px, 100%); max-height: 100%; overflow-y: auto; background: var(--paper); color: var(--pen); border-radius: 16px; padding: 12px; display: flex; flex-direction: column; gap: 8px; box-shadow: 0 6px 24px rgba(0, 0, 0, 0.35); animation: look-up 0.22s ease-out; }
@keyframes look-up { from { transform: translateY(30px); opacity: 0; } to { transform: none; opacity: 1; } }
@media (prefers-reduced-motion: reduce) { .look-box { animation: none; } }
.look-box h3 { margin: 0; font-family: "Dela Gothic One", sans-serif; font-weight: 400; font-size: 18px; color: var(--red); }
.look-note { margin: 0; font-size: 11px; color: #6a7a94; }
.look-rows { display: flex; flex-direction: column; gap: 6px; }
.look-opt { padding: 6px 8px; gap: 4px; }
.look-opt .lab { font-size: 14px; }
.look-opt .chip { flex: 1 1 0; justify-content: center; flex-direction: column; gap: 2px; padding: 5px 0; font-size: 12px; letter-spacing: -0.05em; min-width: 0; white-space: nowrap; border-radius: 12px; }
.look-opt .chip .sw { width: 18px; height: 18px; border-radius: 50%; border: 2px solid rgba(0, 0, 0, 0.12); }
.look-opt .chip .ic { font-size: 18px; line-height: 1.1; }
.look-box .btn { padding: 10px; font-size: 15px; }
.ed-foot { display: flex; gap: 6px; }
/* 文字は折り返さない（幅の狭いスマホで「◀ タイトル」が 2 行になっていた。1.0.1）。幅は文字の長さに合わせて分ける */
.ed-foot .btn { flex: 1 1 auto; min-width: 0; padding: 11px 6px; font-size: 14px; border-color: transparent; white-space: nowrap; }
.ed-foot .btn-sub2 { background: #e4e9f0; color: var(--pen); }
.ed-foot .btn-main { flex: 3 1 auto; }

/* セーフエリア（ノッチ・角丸）を JS で読むための見えない箱 */
#safe-probe {
  position: fixed;
  left: 0;
  top: 0;
  visibility: hidden;
  pointer-events: none;
  padding: env(safe-area-inset-top, 0px) env(safe-area-inset-right, 0px) 0 env(safe-area-inset-left, 0px);
}

/* 背の低い画面（iPhone SE など）でも、④ の 4 項目がスクロールなしで見えるように詰める */
@media (max-height: 760px) {
  .editor[data-step="4"] .ed-hint { display: none; }
  .ed-opts { gap: 6px; }
  .opt { padding: 6px 8px; gap: 4px; border-radius: 10px; }
  .opt .lab { font-size: 14px; }
  .opt .chip { padding: 4px 2px; font-size: 13px; }
  .opt .chip small { font-size: 9px; }
  .opt .note { font-size: 11px; }
  .look-btn { font-size: 14px; padding: 7px 10px; }
}
