/* ===== tokens ===== */
:root {
  --bg: #f5f6f8;
  --surface: #ffffff;
  --surface-2: #eceef2;
  --line: #d3d7e0;
  --line-strong: #9aa2b1;
  --text: #1a1d23;
  --text-dim: #626a78;
  --accent: #1c7ed6;
  --accent-soft: #d8ebfb;
  --cell: #ffffff;
  --cell-line: #8b93a3;
  --grid-dot: #c3c9d4;
  --danger: #d9480f;
  --mk0: #e8590c;
  --mk1: #1c7ed6;
  --mk2: #2f9e44;
  --shadow: 0 6px 24px rgba(16, 20, 30, .14);
  --safe-b: env(safe-area-inset-bottom, 0px);
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1216;
    --surface: #191d24;
    --surface-2: #232831;
    --line: #333a45;
    --line-strong: #5b6472;
    --text: #e8ebf0;
    --text-dim: #98a1b0;
    --accent: #4dabf7;
    --accent-soft: #16324b;
    --cell: #232831;
    --cell-line: #737d8c;
    --grid-dot: #39404c;
    --danger: #ff922b;
    --mk0: #ff922b;
    --mk1: #4dabf7;
    --mk2: #51cf66;
    --shadow: 0 6px 24px rgba(0, 0, 0, .5);
  }
}

* { box-sizing: border-box; }
html, body {
  margin: 0; padding: 0; height: 100%;
  overflow: hidden;             /* ページ自体は絶対にスクロールさせない */
  overscroll-behavior: none;
}
body { height: 100dvh; }
body {
  display: flex; flex-direction: column;
  background: var(--bg); color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Hiragino Sans", "Noto Sans JP", "Yu Gothic", system-ui, sans-serif;
  -webkit-text-size-adjust: 100%;
  -webkit-tap-highlight-color: transparent;
  user-select: none; -webkit-user-select: none;
}
button { font: inherit; color: inherit; cursor: pointer; }
svg { width: 1.25em; height: 1.25em; fill: none; stroke: currentColor; stroke-width: 1.9; stroke-linecap: round; stroke-linejoin: round; }

/* 画面内の案内文は、実際に使っている端末に合わせる（body[data-input] は app.js が付ける）。
   JS が走る前の一瞬に両方出ないよう、既定ではマウス向けを表示しておく */
#emptyHint .only-touch { display: none; }
body[data-input="touch"] #emptyHint .only-touch { display: revert; }
body[data-input="touch"] #emptyHint .only-mouse { display: none; }

/* 使い方は端末タブで切り替える。自分の端末が最初に選ばれるが、
   もう片方も一手で読めるようにしておく（同じ人が両方で使うため） */
.help .only-touch, .help .only-mouse { display: none; }
.help[data-dev="touch"] .only-touch { display: revert; }
.help[data-dev="mouse"] .only-mouse { display: revert; }

/* ===== header ===== */
/* 上部は親指が届かない領域なので、タップ対象を置かず現在のボード名だけを示す */
#header {
  display: flex; align-items: center; justify-content: center;
  padding: max(5px, env(safe-area-inset-top, 0px)) 12px 5px;
  background: var(--surface); border-bottom: 1px solid var(--line);
  flex: 0 0 auto; z-index: 5;
}
.icon-btn {
  display: inline-flex; align-items: center; justify-content: center;
  min-width: 42px; height: 42px; padding: 0 8px;
  background: transparent; border: none; border-radius: 11px;
}
.icon-btn:active { background: var(--surface-2); }
.icon-btn:disabled { opacity: .3; }
.icon-btn.small { min-width: 38px; height: 38px; font-size: 17px; background: var(--surface); box-shadow: var(--shadow); border: 1px solid var(--line); }
.board-name {
  padding: 4px 0; font-size: 15px; font-weight: 600; color: var(--text-dim);
  max-width: 80vw; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}

/* ===== grid viewport ===== */
#viewport {
  position: relative; flex: 1 1 auto; min-height: 0; overflow: hidden;
  touch-action: none; background: var(--bg);
}
#stage { position: absolute; top: 0; left: 0; transform-origin: 0 0; will-change: transform; }
#grid { display: grid; }

.cell {
  position: relative;
  width: 44px; height: 44px;
  display: flex; align-items: center; justify-content: center;
  font-size: 25px; line-height: 1; font-weight: 600;
  color: var(--text);
}
/* 空きマス: 近傍のみ薄いドットを出す */
.cell.hint::before {
  content: ""; position: absolute; inset: 50% auto auto 50%;
  width: 4px; height: 4px; margin: -2px 0 0 -2px; border-radius: 50%;
  background: var(--grid-dot);
}
.cell.on {
  background: var(--cell);
  box-shadow: inset 0 0 0 1px var(--cell-line);
}
.cell.on.mk0 { background: color-mix(in srgb, var(--mk0) 18%, var(--cell)); box-shadow: inset 0 0 0 2px var(--mk0); }
.cell.on.mk1 { background: color-mix(in srgb, var(--mk1) 18%, var(--cell)); box-shadow: inset 0 0 0 2px var(--mk1); }
.cell.on.mk2 { background: color-mix(in srgb, var(--mk2) 18%, var(--cell)); box-shadow: inset 0 0 0 2px var(--mk2); }
.cell[data-ord]::after {
  content: attr(data-ord);
  position: absolute; top: 1px; left: 2px;
  font-size: 10px; font-weight: 700; line-height: 1; opacity: .85;
}
.cell.mk0[data-ord]::after { color: var(--mk0); }
.cell.mk1[data-ord]::after { color: var(--mk1); }
.cell.mk2[data-ord]::after { color: var(--mk2); }
.cell.sel { box-shadow: inset 0 0 0 3px var(--accent); background: var(--accent-soft); z-index: 2; }
/* マスが無い場所のカーソルは破線に（マスがあるように見せない） */
.cell.sel:not(.on) {
  background: transparent; box-shadow: none;
  outline: 2px dashed var(--accent); outline-offset: -4px;
}
.cell.preview { background: var(--accent-soft); box-shadow: inset 0 0 0 1px var(--accent); }
.cell.preview-del { background: color-mix(in srgb, var(--danger) 22%, transparent); box-shadow: inset 0 0 0 1px var(--danger); }
.cell .dir {
  position: absolute; right: 2px; bottom: 0; font-size: 11px; color: var(--accent); font-weight: 700;
  pointer-events: none;
}

#zoomCtl {
  position: absolute; right: 10px; bottom: 10px;
  display: flex; flex-direction: column; gap: 8px; z-index: 3;
}
.empty-hint {
  position: absolute; inset: auto 0 50% 0; display: flex; flex-direction: column;
  align-items: center; gap: 4px; pointer-events: none; color: var(--text-dim);
  text-align: center; padding: 0 24px; transform: translateY(50%);
}
.empty-hint strong { font-size: 16px; color: var(--text); }
.empty-hint span { font-size: 13px; }
.empty-hint[hidden] { display: none; }

/* ===== 抽出結果 ===== */
.extract {
  flex: 0 0 auto; background: var(--surface); border-top: 1px solid var(--line);
  padding: 8px 10px; display: flex; flex-direction: column; gap: 6px;
  max-height: 30vh; overflow-y: auto;
}
.extract[hidden] { display: none; }
.ex-row { display: flex; align-items: center; gap: 8px; }
.ex-badge {
  flex: 0 0 auto; width: 22px; height: 22px; border-radius: 7px; color: #fff;
  display: flex; align-items: center; justify-content: center; font-size: 12px; font-weight: 700;
}
.ex-badge.g0 { background: var(--mk0); } .ex-badge.g1 { background: var(--mk1); } .ex-badge.g2 { background: var(--mk2); }
.ex-text {
  flex: 1 1 auto; font-size: 19px; font-weight: 600; letter-spacing: .12em;
  overflow-x: auto; white-space: nowrap; user-select: text; -webkit-user-select: text;
  padding: 2px 0;
}
.ex-text .blank { color: var(--text-dim); opacity: .6; }
.ex-copy {
  flex: 0 0 auto; background: var(--surface-2); border: 1px solid var(--line);
  border-radius: 9px; padding: 5px 10px; font-size: 12px;
}

/* ===== toolbar ===== */
#toolbar {
  flex: 0 0 auto; background: var(--surface); border-top: 1px solid var(--line);
  padding: 7px 8px;
  display: flex; align-items: center; gap: 6px;
}
.tools { flex: 1 1 auto; min-width: 0; display: flex; align-items: center; gap: 6px; overflow-x: auto; }
.history {
  flex: 0 0 auto; display: flex; align-items: center; gap: 2px;
  padding-left: 6px; border-left: 1px solid var(--line);
}
.history .icon-btn { min-width: 38px; height: 38px; }
.tools[hidden] { display: none; }
.tool-sep { width: 1px; height: 22px; background: var(--line); flex: 0 0 auto; margin: 0 2px; }
.chip {
  flex: 0 0 auto; display: inline-flex; align-items: center; gap: 5px;
  min-height: 38px; padding: 0 12px; border-radius: 11px;
  background: var(--surface-2); border: 1px solid transparent;
  font-size: 14px; font-weight: 600; white-space: nowrap;
}
.chip[aria-pressed="true"] { background: var(--accent); color: #fff; }
.chip.ghost { background: transparent; border-color: var(--line); font-weight: 500; color: var(--text-dim); }
.chip.mk0[aria-pressed="true"] { background: var(--mk0); }
.chip.mk1[aria-pressed="true"] { background: var(--mk1); }
.chip.mk2[aria-pressed="true"] { background: var(--mk2); }
.chip.mk { min-width: 44px; justify-content: center; }
.chip.narrow { min-width: 44px; padding: 0 8px; justify-content: center; font-size: 16px; gap: 0; }

/* 二択のセグメント（描く/消す、なぞる/四角） */
.seg {
  flex: 0 0 auto; display: flex; gap: 2px; padding: 2px;
  background: var(--surface-2); border-radius: 11px;
}
.seg button {
  min-height: 34px; padding: 0 11px; border-radius: 9px;
  background: transparent; border: none; color: var(--text-dim);
  font-size: 13px; font-weight: 600; white-space: nowrap;
}
.seg button[aria-pressed="true"] { background: var(--accent); color: #fff; }
.seg button[data-draw="del"][aria-pressed="true"] { background: var(--danger); }
.seg[hidden] { display: none; }
.chip.narrow svg { width: 20px; height: 20px; }

/* ===== bottom nav ===== */
/* 下部＝親指の届く領域。モード切替に加えてメニューもここに置く。
   メニューはモードではないので区切り線で分け、右利きが届きやすい右端にする */
#modes {
  flex: 0 0 auto; display: grid; grid-template-columns: repeat(3, 1fr) 76px;
  background: var(--surface); border-top: 1px solid var(--line);
  padding-bottom: var(--safe-b);
}
.mode-btn.is-menu { border-left: 1px solid var(--line); color: var(--text-dim); }
.mode-btn.is-menu svg { fill: currentColor; stroke: none; }
.mode-btn {
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px;
  background: transparent; border: none; padding: 7px 0 8px; color: var(--text-dim);
  font-size: 11px; font-weight: 600;
}
.mode-btn svg { width: 22px; height: 22px; }
.mode-btn.is-active { color: var(--accent); }

/* ===== hidden input ===== */
#kbd {
  position: fixed; opacity: 0; pointer-events: none;
  width: 1px; height: 1px; left: 50%; top: 50%; border: 0; padding: 0;
  font-size: 16px; /* iOS の自動ズーム防止 */
}

/* ===== sheets ===== */
.overlay { position: fixed; inset: 0; background: rgba(8, 11, 16, .45); z-index: 20; }
.overlay[hidden] { display: none; }
.sheet {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 21;
  background: var(--surface); border-radius: 18px 18px 0 0;
  padding: 0 14px calc(16px + var(--safe-b));
  max-height: 86vh; overflow-y: auto; overscroll-behavior: contain; box-shadow: var(--shadow);
  animation: sheetUp .18s ease-out;
}
@keyframes sheetUp { from { transform: translateY(16px); opacity: 0; } }
@media (min-width: 700px) {
  .sheet {
    left: 50%; right: auto; transform: translateX(-50%);
    width: 460px; border-radius: 18px; bottom: 24px;
    animation-name: sheetUpCentered;
  }
}
/* 中央寄せのシートは translateX(-50%) を保ったまま動かす必要がある */
@keyframes sheetUpCentered {
  from { transform: translateX(-50%) translateY(16px); opacity: 0; }
  to   { transform: translateX(-50%) translateY(0); opacity: 1; }
}
.sheet[hidden] { display: none; }
.sheet-handle { width: 38px; height: 4px; border-radius: 2px; background: var(--line-strong); margin: 10px auto; opacity: .6; }
.sheet h2 { margin: 0 0 10px; font-size: 15px; color: var(--text-dim); font-weight: 600; }
/* メニューは種類ごとにまとめる（盤面への操作とボードの管理は別物） */
.menu-group { margin-bottom: 14px; }
.menu-group:last-child { margin-bottom: 0; }
.menu-label {
  padding: 0 4px 6px; font-size: 12px; font-weight: 700;
  color: var(--text-dim); letter-spacing: .04em;
}
.menu-group .sheet-list {
  border: 1px solid var(--line); border-radius: 14px; overflow: hidden;
}
.menu-group .sheet-list button { padding: 13px 12px; }
.menu-help {
  display: flex; align-items: center; gap: 12px; width: 100%;
  padding: 13px 12px; margin-bottom: 18px;
  background: transparent; border: 1px solid var(--line); border-radius: 14px;
  font-size: 15px; text-align: left;
}
.menu-help:active { background: var(--surface-2); }

.sheet-list { display: flex; flex-direction: column; }
.sheet-list button {
  display: flex; align-items: center; gap: 12px; width: 100%;
  background: transparent; border: none; border-bottom: 1px solid var(--line);
  padding: 14px 4px; font-size: 15px; text-align: left;
}
.sheet-list button:last-child { border-bottom: none; }
.sheet-list button:active { background: var(--surface-2); }
.si { width: 22px; text-align: center; color: var(--text-dim); }
.wide-btn {
  width: 100%; margin-top: 12px; padding: 13px; border-radius: 12px;
  background: var(--accent); color: #fff; border: none; font-weight: 700; font-size: 15px;
}
.board-list { display: flex; flex-direction: column; gap: 6px; }
.board-row {
  display: flex; align-items: center; gap: 6px; padding: 8px 6px;
  border: 1px solid var(--line); border-radius: 12px;
}
.board-row.is-current { border-color: var(--accent); background: var(--accent-soft); }
.board-row .bname { flex: 1 1 auto; background: transparent; border: none; text-align: left; padding: 6px 2px; font-size: 15px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.board-row .bmeta { font-size: 11px; color: var(--text-dim); display: block; margin-top: 2px; }
.board-row .mini { background: var(--surface-2); border: none; border-radius: 8px; width: 34px; height: 34px; font-size: 14px; }
/* ===== 確認・入力ダイアログ ===== */
#dialog h2 { font-size: 17px; color: var(--text); font-weight: 700; margin-bottom: 6px; }
.dlg-msg { margin: 0 0 14px; font-size: 14px; line-height: 1.6; color: var(--text-dim); }
.dlg-msg[hidden] { display: none; }
.dlg-input {
  width: 100%; margin: 4px 0 16px; padding: 12px 14px;
  font-size: 16px; font-family: inherit; color: var(--text);
  background: var(--surface-2); border: 1px solid var(--line); border-radius: 12px;
  user-select: text; -webkit-user-select: text;
}
.dlg-input[hidden] { display: none; }
.dlg-input:focus { outline: 2px solid var(--accent); outline-offset: -1px; border-color: transparent; }
.dlg-btns { display: flex; gap: 8px; }
.dlg-btn {
  flex: 1 1 0; min-height: 48px; border-radius: 12px; font-size: 15px; font-weight: 700;
  background: var(--surface-2); border: 1px solid var(--line); color: var(--text);
}
.dlg-btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.dlg-btn.danger { background: var(--danger); border-color: var(--danger); color: #fff; }

/* ===== ヘルプの解説アニメーション =====
   4枚のフレームをCSSで切り替える紙芝居方式。
   GIFのようなバイナリを持たず、拡大しても綺麗で、配色もテーマに追従する。 */
.d {
  display: block; width: 100%; max-width: 300px; height: auto;
  margin: 12px auto 8px; border-radius: 12px;
  background: var(--bg); padding: 4px 0;
  /* アイコン用のグローバル svg 指定（stroke: currentColor）が
     文字や点にまで継承されるので、ここで打ち消しておく */
  fill: none; stroke: none;
}
.d-dot { fill: var(--grid-dot); }
.d-on { fill: var(--cell); stroke: var(--cell-line); stroke-width: 1; }
.d-sel { fill: var(--accent); fill-opacity: .2; stroke: var(--accent); stroke-width: 2.4; }
.d-band { fill: var(--accent); fill-opacity: .16; stroke: var(--accent); stroke-width: 1.6; stroke-dasharray: 4 3; }
.d-mk { fill: var(--mk0); fill-opacity: .22; stroke: var(--mk0); stroke-width: 2.4; }
.d-ch { fill: var(--text); font-size: 16px; font-weight: 700; text-anchor: middle; }
.d-ord { fill: var(--mk0); font-size: 9px; font-weight: 700; }
.d-dir { fill: var(--accent); font-size: 10px; font-weight: 700; text-anchor: end; }
.d-label { fill: var(--text-dim); font-size: 9.5px; text-anchor: middle; }
.d-ptr-halo { fill: var(--accent); opacity: .2; }
.d-ptr-tip { fill: var(--accent); }
.d-badge { fill: var(--mk0); }
.d-badge-t { fill: #fff; font-size: 9px; font-weight: 700; text-anchor: middle; }
.d-res { fill: var(--text); font-size: 14px; font-weight: 700; letter-spacing: .14em; }

.d-frame { opacity: 0; animation: dFrame 5.2s steps(1, end) infinite; }
.d-frame.f1 { animation-delay: 0s; }
.d-frame.f2 { animation-delay: 1.3s; }
.d-frame.f3 { animation-delay: 2.6s; }
.d-frame.f4 { animation-delay: 3.9s; }
@keyframes dFrame {
  0%, 24.99% { opacity: 1; }
  25%, 100%  { opacity: 0; }
}
/* 動きが苦手な人には最後のコマだけ見せる */
@media (prefers-reduced-motion: reduce) {
  .d-frame { animation: none; opacity: 0; }
  .d-frame.f4 { opacity: 1; }
}

.help-lead {
  margin: 0 0 4px; padding: 12px 14px; border-radius: 12px;
  background: var(--surface-2); font-size: 14px; line-height: 1.7;
}
.d-cap { margin: 0 0 14px; font-size: 13.5px; line-height: 1.7; color: var(--text-dim); }
.d-cap strong { color: var(--text); }
.help-n {
  display: inline-flex; align-items: center; justify-content: center;
  width: 20px; height: 20px; margin-right: 7px; border-radius: 6px;
  background: var(--accent); color: #fff; font-size: 12px;
}
/* 読みながら切り替える操作なので、シート下端に固定して親指側に置く */
.help-foot {
  position: sticky; bottom: 0; z-index: 3;
  margin: 8px -14px 0; padding: 8px 14px calc(8px + var(--safe-b));
  background: var(--surface); border-top: 1px solid var(--line);
}
#helpSheet > h2 { position: sticky; top: 0; background: var(--surface); margin: 0; padding: 4px 0 8px; z-index: 2; }
.dev-tabs {
  display: flex; gap: 4px; padding: 3px;
  background: var(--surface-2); border-radius: 12px;
}
.dev-tabs button {
  flex: 1 1 0; display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  min-height: 40px; border: none; border-radius: 9px;
  background: transparent; color: var(--text-dim);
  font-size: 14px; font-weight: 700;
}
.dev-tabs button svg { width: 17px; height: 17px; }
.dev-tabs button[aria-pressed="true"] { background: var(--accent); color: #fff; }

.help { font-size: 14px; line-height: 1.65; }
.help h3 {
  display: flex; align-items: center;
  font-size: 15px; margin: 22px 0 2px; color: var(--text); font-weight: 700;
}
.help h3:first-of-type { margin-top: 16px; }
.help ul { margin: 0; padding-left: 1.2em; }
.help li { margin-bottom: 4px; }

.toast {
  position: fixed; left: 50%; bottom: calc(84px + var(--safe-b)); transform: translateX(-50%);
  background: var(--text); color: var(--bg); padding: 10px 18px; border-radius: 999px;
  font-size: 14px; font-weight: 600; z-index: 40; box-shadow: var(--shadow);
  animation: toastUp .15s ease-out;
}
.toast[hidden] { display: none; }
@keyframes toastUp {
  from { transform: translateX(-50%) translateY(10px); opacity: 0; }
  to   { transform: translateX(-50%) translateY(0); opacity: 1; }
}


