/* ===== 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 が走る前の一瞬に両方出ないよう、既定ではマウス向けを表示しておく */
.only-touch { display: none; }
body[data-input="touch"] .only-touch { display: revert; }
body[data-input="touch"] .only-mouse { display: none; }

/* ===== header ===== */
#header {
  display: flex; align-items: center; gap: 6px;
  padding: max(6px, env(safe-area-inset-top, 0px)) 8px 6px;
  background: var(--surface); border-bottom: 1px solid var(--line);
  flex: 0 0 auto; z-index: 5;
}
.spacer { flex: 1; }
.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 {
  background: transparent; border: none; padding: 6px 10px; border-radius: 10px;
  font-size: 16px; font-weight: 600; max-width: 46vw;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.board-name:active { background: var(--surface-2); }

/* ===== 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);
  background: var(--surface); border-top: 1px solid var(--line);
  padding-bottom: var(--safe-b);
}
.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: 6px 14px calc(16px + var(--safe-b));
  max-height: 82vh; overflow-y: auto; box-shadow: var(--shadow);
  animation: slideUp .18s ease-out;
}
@media (min-width: 700px) {
  .sheet { left: 50%; right: auto; transform: translateX(-50%); width: 460px; border-radius: 18px; bottom: 24px; }
  @keyframes slideUp { from { transform: translateX(-50%) translateY(16px); opacity: 0; } }
}
@keyframes slideUp { from { transform: translateY(16px); opacity: 0; } }
.sheet[hidden] { display: none; }
.sheet-handle { width: 38px; height: 4px; border-radius: 2px; background: var(--line-strong); margin: 6px auto 10px; opacity: .6; }
.sheet h2 { margin: 0 0 10px; font-size: 15px; color: var(--text-dim); font-weight: 600; }
.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; }

.help { font-size: 14px; line-height: 1.65; }
.help h3 { font-size: 14px; margin: 14px 0 4px; color: var(--accent); }
.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: slideUp .15s ease-out;
}
.toast[hidden] { display: none; }


