:root {
  --bg: #0f172a; --panel: #1e293b; --panel2: #334155; --line: #475569;
  --text: #f1f5f9; --sub: #94a3b8; --accent: #3b82f6; --ok: #16a34a; --ng: #b91c1c;
}
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body {
  margin: 0; height: 100%; overflow: hidden; background: var(--bg); color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Hiragino Sans", "Hiragino Kaku Gothic ProN", Meiryo, sans-serif;
  -webkit-user-select: none; user-select: none; -webkit-touch-callout: none;
  overscroll-behavior: none; touch-action: manipulation; -webkit-text-size-adjust: 100%;
}
body {
  display: flex; flex-direction: column; position: fixed; inset: 0;
  padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
}
/* 横向き: ツールバーを上。縦向き: 下（コートを最大化、親指で届く） */
#toolbar { order: 0; }
#stage { order: 1; }
@media (orientation: portrait) { #toolbar { order: 2; border-top: 1px solid var(--line); border-bottom: 0; } }
#toolbar {
  display: flex; flex-direction: column; gap: 3px; padding: 3px 6px; background: var(--panel);
  border-bottom: 1px solid var(--line); flex: 0 0 auto;
}
.row { display: flex; align-items: center; gap: 8px; min-width: 0; }
.group { display: flex; align-items: center; gap: 6px; }
.group.steps { flex: 1 1 auto; min-width: 0; }
button {
  font: inherit; font-size: 15px; font-weight: 600; min-height: 44px; min-width: 44px; padding: 0 12px;
  border: 1px solid var(--line); border-radius: 10px; background: var(--panel2); color: var(--text);
  touch-action: manipulation; white-space: nowrap; cursor: pointer;
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
}
button:active { filter: brightness(1.25); }
button.on { background: var(--accent); border-color: #bfdbfe; color: #fff; box-shadow: inset 0 -3px 0 rgba(255,255,255,.55); }
button.primary { background: var(--ok); border-color: #86efac; color: #fff; }
button.danger { background: var(--ng); border-color: #fca5a5; }
button:disabled { opacity: .35; }
button.icon { padding: 0; }
button svg { width: 22px; height: 22px; flex: 0 0 auto; stroke: currentColor; fill: none; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
i[data-icon] { display: inline-flex; }
.seg { gap: 0; }
.seg button { border-radius: 0; margin-left: -1px; }
.seg button:first-child { border-radius: 10px 0 0 10px; margin-left: 0; }
.seg button:last-child { border-radius: 0 10px 10px 0; }
#stepList {
  display: flex; gap: 6px; flex: 1 1 auto; min-width: 60px; overflow-x: auto; overflow-y: hidden;
  touch-action: pan-x; -webkit-overflow-scrolling: touch; padding: 0 2px; scrollbar-width: none;
}
#stepList::-webkit-scrollbar { display: none; }
#stepList button { flex: 0 0 auto; padding: 0 10px; min-width: 44px; font-size: 16px; }
#addStepBtn .short { display: none; }
#stage { flex: 1 1 auto; min-height: 0; position: relative; background: var(--bg); }
#board { position: absolute; inset: 0; width: 100%; height: 100%; touch-action: none; display: block; }
.colors {
  position: absolute; left: 8px; top: 8px; display: flex; gap: 8px; padding: 6px 8px;
  background: rgba(15,23,42,.82); border: 1px solid var(--line); border-radius: 26px; z-index: 5;
}
.colors[hidden] { display: none; }
.colors button { width: 44px; height: 44px; min-height: 44px; padding: 0; border-radius: 50%; border: 3px solid #0f172a; }
.colors button.on { border-color: #fff; box-shadow: 0 0 0 3px var(--accent); }
.colors button.black { border-color: #e5e7eb; box-shadow: 0 0 0 1px #0f172a; }
.colors button.black.on { border-color: #fff; box-shadow: 0 0 0 3px var(--accent); }
#toast {
  position: fixed; left: 50%; bottom: calc(24px + env(safe-area-inset-bottom)); transform: translateX(-50%);
  background: rgba(15,23,42,.96); border: 1px solid var(--sub); padding: 10px 18px; border-radius: 10px; z-index: 60;
  pointer-events: none; max-width: 90vw; text-align: center;
}
.overlay {
  position: fixed; left: 0; right: 0; top: var(--vvt, 0px); height: var(--vvh, 100%); background: rgba(0,0,0,.55); z-index: 20;
  display: flex; align-items: center; justify-content: center;
  padding: max(16px, env(safe-area-inset-top)) max(16px, env(safe-area-inset-right)) max(16px, env(safe-area-inset-bottom)) max(16px, env(safe-area-inset-left));
}
.overlay.top { z-index: 25; }
.overlay[hidden], #toast[hidden], #guide[hidden] { display: none; }
.dialog {
  background: var(--panel); border: 1px solid var(--line); border-radius: 14px; padding: 18px;
  width: min(560px, 100%); max-height: 100%; display: flex; flex-direction: column; gap: 12px;
}
.dialog h2 { margin: 0; font-size: 18px; }
.dialog .body { overflow-y: auto; -webkit-overflow-scrolling: touch; touch-action: pan-y; }
.dialog .actions { display: flex; gap: 8px; justify-content: flex-end; flex-wrap: wrap; }
.dialog input[type=text], .dialog textarea {
  width: 100%; min-height: 46px; font-size: 16px; padding: 0 10px; border-radius: 8px;
  border: 1px solid var(--sub); background: var(--bg); color: var(--text); -webkit-user-select: text; user-select: text;
}
.dialog textarea { padding: 8px 10px; height: 140px; font-family: monospace; font-size: 16px; }
.dialog img.preview { max-width: 100%; max-height: 50vh; display: block; margin: 0 auto; -webkit-touch-callout: default; -webkit-user-select: auto; }
.msg { color: var(--sub); font-size: 13px; margin: 6px 0; }
.menu h3 { margin: 12px 0 6px; font-size: 13px; color: var(--sub); font-weight: 600; }
.menu h3:first-child { margin-top: 0; }
.menu .grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 8px; }
.planrow { display: flex; align-items: center; gap: 10px; padding: 8px 0; border-bottom: 1px solid var(--panel2); }
.planrow canvas { width: 64px; height: 64px; border-radius: 6px; flex: 0 0 auto; background: var(--bg); }
.planrow .info { flex: 1; min-width: 0; }
.planrow .name { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.planrow .meta { font-size: 12px; color: var(--sub); }
.empty { color: var(--sub); padding: 12px 0; }
/* 選手登録 */
.roster h3 { margin: 14px 0 4px; font-size: 14px; }
.roster .modes { display: flex; gap: 8px; }
.roster .modes button { flex: 1 1 0; }
.roster .warn { color: #fbbf24; font-size: 14px; }
.roster .rhead, .roster .rrow { display: grid; grid-template-columns: 14px 52px minmax(0, 1fr) 68px; gap: 6px; align-items: center; }
.roster .rhead { font-size: 11px; color: var(--sub); }
.roster .rrow { margin: 6px 0; }
.roster .dot { width: 14px; height: 14px; border-radius: 50%; border: 1px solid #fff; }
.roster .rrow input { padding: 0 6px; }
.roster input.num, .roster input.mark { text-align: center; }
/* ガイド */
#guide { position: fixed; inset: 0; z-index: 50; }
#guideSpot { position: fixed; border-radius: 12px; box-shadow: 0 0 0 9999px rgba(0,0,0,.72), 0 0 0 3px #fbbf24; pointer-events: none; transition: all .2s; }
#guideBubble {
  position: fixed; width: min(340px, calc(100vw - 24px)); background: #fff; color: #111827; border-radius: 14px;
  padding: 14px 16px; display: flex; flex-direction: column; gap: 8px; box-shadow: 0 6px 24px rgba(0,0,0,.5);
}
#guideCount { font-size: 12px; color: #6b7280; }
#guideText { font-size: 17px; font-weight: 600; line-height: 1.5; }
#guideBubble .gbtns { display: flex; gap: 8px; justify-content: flex-end; }
#guideBubble button:not(.primary) { background: #e5e7eb; color: #111827; border-color: #9ca3af; }
@media (max-width: 800px) { button { font-size: 14px; padding: 0 9px; } .row { gap: 5px; } }

/* 縦向きで幅が狭いときだけ、「ステップ追加」を「＋ステップ」に縮める（aria-label は「ステップ追加」のまま） */
@media (orientation: portrait) and (max-width: 640px) {
  #addStepBtn .long, #addStepBtn i { display: none; }
  #addStepBtn .short { display: inline; }
  #stepList button { padding: 0 7px; font-size: 15px; }
}
/* さらに狭い縦向き（スマホ幅）では、ステップ行を 2 段に分けて重ならないようにする */
@media (orientation: portrait) and (max-width: 520px) {
  #rowSteps { flex-wrap: wrap; }
  #rowSteps .group.steps { flex: 1 1 100%; }
  #addStepBtn { flex: 0 0 auto; }
}
