/*
 * ひとふでスイッチのデザイン
 * - 紙の上に、墨で一筆書きするイメージ。盤面に線は描かない
 * - 通ったマスには黄色い灯りがつく。ブロックは影・つや・縁なしの平らな面だけ
 * - × に灯りがついたら赤。クリアしたら灯りが緑に変わる
 */
:root {
  --bg: #f4f1ea;          /* 紙 */
  --surface: #ffffff;
  --tile: #e5e0d5;        /* 灯りのついていないマス */
  --tile-hover: #dcd6c9;
  --mark: #a59d8e;
  --ink: #26252b;         /* 墨（文字と一筆書きの線） */
  --muted: #7a7368;
  --lit: #ffc21a;         /* 灯りのついたマス */
  --o-mark: #2fa866;      /* ○ は緑、× は赤 */
  --x-mark: #e5483f;
  --o-lit: #17824a;       /* 黄色い灯りの上で見える濃さ */
  --x-lit: #d1281f;
  --bad: #f0524a;
  --bad-mark: #ffffff;
  --ok: #3aa86b;
  --ok-mark: #ffffff;
  --radius: 14px;
  color-scheme: light;
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--ink);
  font-family: "Zen Maru Gothic", system-ui, -apple-system, "Hiragino Maru Gothic ProN", "Hiragino Sans", "Noto Sans JP", sans-serif;
  font-weight: 500;
  line-height: 1.6;
  -webkit-tap-highlight-color: transparent;
}
[hidden] { display: none !important; }

.screen {
  max-width: 480px;
  margin: 0 auto;
  padding: max(16px, env(safe-area-inset-top)) 16px max(24px, env(safe-area-inset-bottom));
}
.row { display: flex; gap: 10px; }
.row > * { flex: 1; }
.muted { color: var(--muted); }
.small { font-size: 0.85rem; }
.center { text-align: center; }

/* ---------- ボタン（平らな面だけ） ---------- */
button, .button {
  appearance: none;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: 48px;
  padding: 0 18px;
  border: none;
  border-radius: var(--radius);
  background: var(--tile);
  color: var(--ink);
  font: inherit;
  font-weight: 700;
  text-decoration: none;
  cursor: pointer;
  transition: background-color 0.12s, transform 0.08s;
}
button:active:not(:disabled), .button:active { transform: scale(0.97); }
button:disabled { opacity: 0.35; cursor: default; }
button.primary { background: var(--ink); color: var(--bg); }
button.big { min-height: 60px; font-size: 1.15rem; width: 100%; }
.icon { width: 48px; padding: 0; font-size: 1.3rem; background: transparent; }
.icon svg { display: block; }
.icon-spacer { width: 48px; }
:focus-visible { outline: 3px solid var(--lit); outline-offset: 2px; }
.board:focus, .board:focus-visible { outline: none; }
@media (hover: hover) {
  button:not(:disabled):not(.primary):hover, .button:hover { background: var(--tile-hover); }
}

/* ---------- 読み込み中・失敗 ---------- */
.boot { padding: 96px 24px; text-align: center; color: var(--muted); }
.boot.error { color: var(--bad); word-break: break-all; }

/* ---------- ホーム ---------- */
#screen-home { position: relative; }
.home-sound { position: absolute; top: max(8px, env(safe-area-inset-top)); right: 8px; color: var(--muted); }
.home-head { text-align: center; margin: 40px 0 28px; }
.home-head h1 { margin: 0; font-size: 2.2rem; font-weight: 900; letter-spacing: 0.06em; }
.tagline { margin: 6px 0 0; color: var(--muted); font-size: 0.95rem; text-wrap: balance; }
.home-actions { display: grid; gap: 10px; margin-bottom: 28px; }
.size-group h3 { margin: 24px 0 10px; font-size: 0.95rem; color: var(--muted); font-weight: 700; }
.level-grid { display: grid; grid-template-columns: repeat(5, 1fr); gap: 8px; }
.level-btn { min-height: 56px; padding: 0; font-size: 1.1rem; font-variant-numeric: tabular-nums; position: relative; }
.level-btn.done { background: var(--lit); color: var(--ink); }
.level-btn.next { background: var(--ink); color: var(--bg); }
.level-btn.locked { background: transparent; color: var(--mark); box-shadow: inset 0 0 0 2px var(--tile); }
.level-btn.locked:disabled { opacity: 1; }

/* ---------- プレイ ---------- */
.play-head { display: grid; grid-template-columns: 1fr auto 1fr; align-items: center; gap: 8px; margin-bottom: 4px; }
.play-head > :first-child { justify-self: start; }
.play-head > :last-child { justify-self: end; }
.play-head h2 { margin: 0; font-size: 1.25rem; font-weight: 700; color: var(--muted); }
.head-tools { display: flex; }
.head-tools .sound-toggle { color: var(--muted); }

.moves { display: grid; justify-items: center; gap: 10px; margin: 8px 0 20px; }
.moves-text { font-variant-numeric: tabular-nums; font-weight: 900; font-size: 2.6rem; line-height: 1; white-space: nowrap; }
.moves-sep { color: var(--mark); margin: 0 6px; font-weight: 500; }
.moves-unit { font-size: 1rem; margin-left: 4px; color: var(--muted); font-weight: 700; }
.moves.over .moves-text #moves-used { color: var(--bad); }
/* 手数ゲージ：最短手数ぶんの目盛り */
.ink { display: flex; gap: 3px; width: 100%; height: 8px; }
.ink i { flex: 1; border-radius: 4px; background: var(--tile); transition: background-color 0.1s; }
.ink i.used { background: var(--ink); }
.ink i.over { background: var(--bad); }

.board {
  --size: 3;
  position: relative;
  width: 100%;
  aspect-ratio: 1;
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
}
.cells {
  display: grid;
  grid-template-columns: repeat(var(--size), 1fr);
  grid-template-rows: repeat(var(--size), 1fr);
  gap: 3px;
  width: 100%;
  height: 100%;
}
.cell {
  position: relative;
  border-radius: 10px;
  background: var(--tile);
  display: grid;
  place-items: center;
  transition: background-color 0.12s;
}
.cell .mark { width: 46%; height: 46%; transition: color 0.12s; }
.cell.circle .mark { color: var(--o-mark); }
.cell.cross .mark { color: var(--x-mark); }
.cell.colored { background: var(--lit); animation: lit 0.18s ease-out; }
.cell.circle.colored .mark { color: var(--o-lit); }
.cell.cross.colored .mark { color: var(--x-lit); }
@keyframes lit {
  from { transform: scale(0.9); }
  to { transform: scale(1); }
}

/* クリア：最後のマスから波のように緑へ変わる（--d はマスごとの遅れ。効果音もこの波に合わせて鳴る） */
.board.cleared .cell { transition: background-color 0.3s ease-out var(--d, 0s); }
.board.cleared .cell .mark { transition: color 0.3s ease-out var(--d, 0s); }
.board.cleared .cell.colored { background: var(--ok); animation: pop 0.32s ease-out var(--d, 0s) both; }
.board.cleared .cell.colored .mark { color: var(--ok-mark); }
/* 一筆書きの線：半透明。重なった所だけ濃くなる（濃さは JS が決める） */
.path-layer { position: absolute; inset: 0; width: 100%; height: 100%; pointer-events: none; overflow: visible; }
.path-layer rect, .path-layer path { fill: var(--ink); }
.board.cleared .path-layer { opacity: 0; transition: opacity 0.3s; }

@keyframes pop {
  40% { transform: scale(1.07); }
}
@media (prefers-reduced-motion: reduce) {
  .board.cleared .cell.colored { animation: none; }
}

/* ---------- ランキング ---------- */
.name-form { background: var(--surface); border-radius: var(--radius); padding: 14px; margin: 8px 0 16px; }
.name-form label { display: block; font-weight: 700; margin-bottom: 8px; }
.name-form .row > button { flex: 0 0 auto; }
.name-form input {
  min-height: 48px; padding: 0 14px; font: inherit; border-radius: var(--radius);
  border: none; background: var(--bg); color: var(--ink); min-width: 0;
}
.name-form p { margin: 8px 0 0; }
.ranking-list { list-style: none; margin: 0; padding: 0; display: grid; gap: 6px; }
.ranking-list li {
  display: grid; grid-template-columns: 3em 1fr auto; align-items: center; gap: 8px;
  padding: 12px 14px; border-radius: var(--radius); background: var(--surface);
}
.ranking-list li.me { background: var(--lit); }
.ranking-list .rank { font-weight: 900; font-variant-numeric: tabular-nums; }
.ranking-list .name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.ranking-list .lv { font-weight: 700; font-variant-numeric: tabular-nums; }

/* ---------- ダイアログ ---------- */
dialog {
  border: none; border-radius: 22px; padding: 24px; width: min(420px, calc(100vw - 32px));
  background: var(--surface); color: var(--ink);
}
dialog::backdrop { background: rgba(38, 37, 43, 0.4); }
dialog h3 { margin: 0 0 10px; font-size: 1.35rem; font-weight: 900; }
.rules { padding-left: 1.3em; margin: 0 0 12px; }
.rules li { margin-bottom: 6px; }
.sym-o, .sym-x { font-weight: 900; }
.clear-badge { margin: 0; font-weight: 900; letter-spacing: 0.3em; color: var(--ok); }
#dlg-clear { text-align: center; }
#dlg-clear .row { margin-top: 14px; }
