/* つないでおえかき */
:root {
  --bg: #fff8ee;
  --surface: #ffffff;
  --surface-2: #fff1dc;
  --board-bg: #fffdf8;
  --text: #2b2420;
  --muted: #8a7f76;
  --border: #ead9c2;
  --primary: #e8613c;
  --primary-ink: #ffffff;
  --primary-soft: #fde3d9;
  --line: #3a63c4;
  --dot: #2b2420;
  --dot-done: #3a63c4;
  --hint: #f2a400;
  --focus: #1f7a5a;
  --easy: #4caf7a;
  --normal: #3a8fd9;
  --hard: #8a5cc7;
  --shadow: 0 2px 10px rgba(80, 50, 20, 0.1);
  --radius: 14px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #1d1a17;
    --surface: #2a2622;
    --surface-2: #332d27;
    --board-bg: #f7f1e6; /* 絵が見やすいよう盤面は明るいまま */
    --text: #f3ece4;
    --muted: #b3a89c;
    --border: #463e36;
    --primary: #f07a55;
    --primary-ink: #1d1a17;
    --primary-soft: #4a2e24;
    --shadow: 0 2px 10px rgba(0, 0, 0, 0.4);
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #1d1a17;
  --surface: #2a2622;
  --surface-2: #332d27;
  --board-bg: #f7f1e6;
  --text: #f3ece4;
  --muted: #b3a89c;
  --border: #463e36;
  --primary: #f07a55;
  --primary-ink: #1d1a17;
  --primary-soft: #4a2e24;
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font-family: system-ui, -apple-system, "Hiragino Sans", "Hiragino Kaku Gothic ProN", "Yu Gothic UI", "Meiryo", sans-serif;
  line-height: 1.6;
  -webkit-text-size-adjust: 100%;
}
[hidden] { display: none !important; }
a { color: inherit; }
h1, h2, h3 { line-height: 1.3; }
.visually-hidden {
  position: absolute !important; width: 1px; height: 1px; overflow: hidden;
  clip: rect(0 0 0 0); white-space: nowrap; border: 0; padding: 0; margin: -1px;
}
:focus-visible { outline: 3px solid var(--focus); outline-offset: 2px; }

/* ───── ボタン ───── */
.btn {
  appearance: none;
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  min-height: 48px; padding: 8px 16px;
  border: 2px solid var(--border); border-radius: 999px;
  background: var(--surface); color: var(--text);
  font: inherit; font-weight: 700; font-size: 1rem;
  text-decoration: none; cursor: pointer; white-space: nowrap;
  touch-action: manipulation;
  transition: transform 0.08s, background 0.15s;
}
.btn:hover { background: var(--surface-2); }
.btn:active { transform: scale(0.97); }
.btn:disabled { opacity: 0.45; cursor: not-allowed; transform: none; }
.btn.primary { background: var(--primary); border-color: var(--primary); color: var(--primary-ink); }
.btn.primary:hover { filter: brightness(1.05); }
.btn.ghost { background: transparent; border-color: transparent; }
.btn.ghost:hover { background: var(--surface-2); }
.btn.small { min-height: 44px; padding: 6px 4px; }
.btn.icon { width: 48px; padding: 0; font-size: 1.2rem; }
.btn.subtle { background: transparent; border-color: transparent; color: var(--muted); font-weight: 600; }
.btn[aria-pressed="true"] { background: var(--primary-soft); border-color: var(--primary); }

/* ───── お知らせ帯 ───── */
.banner {
  position: sticky; top: 0; z-index: 20;
  display: flex; flex-wrap: wrap; align-items: center; gap: 8px 12px;
  padding: 10px 16px; background: #fff3c4; color: #4a3b00;
  border-bottom: 1px solid #e8d48a; font-size: 0.95rem;
}
.banner p { margin: 0; flex: 1 1 240px; }
.banner-actions { display: flex; gap: 8px; flex-wrap: wrap; }
.banner .btn { min-height: 40px; }

/* ───── ホーム ───── */
.home { min-height: 100dvh; display: grid; place-items: center; padding: 24px 16px; }
.home-inner { width: 100%; max-width: 560px; text-align: center; }
.logo { font-size: clamp(2rem, 8vw, 3rem); margin: 0 0 8px; letter-spacing: 0.04em; }
.logo-dots { display: flex; justify-content: center; gap: 28px; margin-bottom: 8px; position: relative; }
.logo-dots::before {
  content: ""; position: absolute; top: 50%; left: calc(50% - 56px); width: 112px; height: 3px;
  background: var(--line); transform: translateY(-50%);
}
.logo-dots i {
  position: relative; display: grid; place-items: center;
  width: 30px; height: 30px; border-radius: 50%;
  background: var(--surface); border: 3px solid var(--text);
  font-style: normal; font-size: 0.9rem; font-weight: 800;
}
.lead { color: var(--muted); margin: 0 0 24px; }
.difficulty-list { display: grid; gap: 12px; }
.difficulty-card {
  display: grid; grid-template-columns: 1fr auto; grid-template-areas: "name done" "range done";
  align-items: center; text-align: left;
  padding: 16px 20px; border-radius: var(--radius);
  background: var(--surface); border: 2px solid var(--border); border-left: 10px solid var(--c);
  text-decoration: none; box-shadow: var(--shadow);
  transition: transform 0.1s;
}
.difficulty-card:hover { transform: translateY(-2px); }
.difficulty-card.easy { --c: var(--easy); }
.difficulty-card.normal { --c: var(--normal); }
.difficulty-card.hard { --c: var(--hard); }
.d-name { grid-area: name; font-size: 1.35rem; font-weight: 800; }
.d-range { grid-area: range; color: var(--muted); font-size: 0.9rem; }
.d-done { grid-area: done; font-weight: 700; color: var(--c); font-size: 0.95rem; }
.home-tools { display: flex; justify-content: center; align-items: center; flex-wrap: wrap; gap: 12px 20px; margin-top: 20px; }
.toggle { display: inline-flex; align-items: center; gap: 8px; min-height: 48px; cursor: pointer; }
.toggle input { width: 22px; height: 22px; accent-color: var(--primary); }
.home-footer { margin-top: 16px; font-size: 0.85rem; color: var(--muted); line-height: 1.9; }
.home-footer a { white-space: nowrap; }
.load-error { color: #b3261e; font-weight: 700; }

/* ───── 上部バー ───── */
.topbar {
  display: grid; grid-template-columns: auto 1fr auto; align-items: center; gap: 8px;
  padding: 6px 8px; padding-top: max(6px, env(safe-area-inset-top));
  background: var(--bg);
}
.topbar h1 { margin: 0; font-size: 1.15rem; text-align: center; }
.back::before { content: "‹"; font-size: 1.4em; line-height: 1; margin-right: 2px; }

/* ───── 一覧 ───── */
.list { min-height: 100dvh; }
.tabs { display: flex; gap: 6px; padding: 4px 16px 12px; max-width: 760px; margin: 0 auto; overflow-x: auto; }
.tabs a {
  flex: 1; text-align: center; padding: 10px 8px; min-height: 44px; border-radius: 999px;
  text-decoration: none; font-weight: 700; color: var(--muted); border: 2px solid var(--border); white-space: nowrap;
}
.tabs a.active { background: var(--text); color: var(--bg); border-color: var(--text); }
.puzzle-list {
  list-style: none; margin: 0 auto; padding: 0 16px 32px; max-width: 760px;
  display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 12px;
}
.puzzle-card {
  display: grid; gap: 4px; padding: 12px; height: 100%;
  background: var(--surface); border: 2px solid var(--border); border-radius: var(--radius);
  text-decoration: none; box-shadow: var(--shadow); transition: transform 0.1s;
}
.puzzle-card:hover { transform: translateY(-2px); }
.thumb {
  display: grid; place-items: center; aspect-ratio: 1; border-radius: 10px;
  background: var(--board-bg); color: #c9bba8; font-size: 3rem; font-weight: 800;
  background-image: radial-gradient(#d9cbb6 2px, transparent 2.5px); background-size: 22px 22px;
}
.puzzle-card.done .thumb { background-image: none; }
.thumb img { width: 100%; height: 100%; object-fit: contain; }
.p-name { font-weight: 800; font-size: 1.1rem; }
.p-genre {
  display: inline-block; margin-left: 4px; padding: 1px 8px; border-radius: 999px; vertical-align: 2px;
  background: var(--board-bg); color: var(--text, #2b2420); border: 1px solid #d9cbb6; font-size: 0.8rem; font-weight: 700;
}
.p-meta { color: var(--muted); font-size: 0.9rem; }
.p-done { color: var(--easy); font-weight: 700; font-size: 0.9rem; }

/* ───── プレイ ───── */
.play {
  height: 100dvh; min-height: 0;
  display: grid;
  grid-template-areas: "top" "board" "panel";
  grid-template-rows: auto minmax(0, 1fr) auto;
}
.play-top { grid-area: top; }
.board-wrap {
  grid-area: board; position: relative; overflow: hidden; min-height: 0;
  margin: 0 8px; border-radius: var(--radius);
  background: var(--board-bg); border: 2px solid var(--border);
}
.board {
  display: block; width: 100%; height: 100%;
  touch-action: none; user-select: none; -webkit-user-select: none; -webkit-touch-callout: none;
  cursor: pointer;
}
.board.move-mode { cursor: grab; }
.board:focus-visible { outline: 3px solid var(--focus); outline-offset: -3px; }
.panel {
  grid-area: panel; padding: 10px 12px; padding-bottom: max(10px, env(safe-area-inset-bottom));
  overflow-y: auto;
}
.play-controls { display: grid; gap: 8px; }
.status { display: grid; grid-template-columns: 1fr auto; align-items: baseline; gap: 0 12px; }
.next { margin: 0; font-size: 1.15rem; font-weight: 700; }
.next strong { font-size: 1.9rem; color: var(--primary); font-variant-numeric: tabular-nums; margin-left: 2px; }
.progress-text { margin: 0; color: var(--muted); font-variant-numeric: tabular-nums; font-weight: 700; }
.progress-bar { grid-column: 1 / -1; height: 8px; border-radius: 99px; background: var(--surface-2); overflow: hidden; }
.progress-bar span { display: block; height: 100%; width: 0; background: var(--line); border-radius: 99px; transition: width 0.2s; }
/* 文言の長さで高さが変わらないよう2行分を確保（盤面の大きさが変わると番号が動くため） */
.message { margin: 0; height: 2.8em; line-height: 1.4; overflow: hidden; font-weight: 600; font-size: 0.95rem; }
.row { display: grid; gap: 8px; }
.main-actions { grid-template-columns: 1fr 1fr; }
.view-actions { grid-template-columns: repeat(4, 1fr); }
.zoom-note { margin: 0; font-size: 0.8rem; line-height: 1.4; height: 2.8em; overflow: hidden; color: var(--muted); min-width: 9em; flex: 1; }
.foot { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: 0 8px; }
.foot .zoom-note { flex: 1; }
#btn-restart { margin-left: auto; min-height: 40px; padding: 4px 8px; font-size: 0.9rem; }
/* 背の低い縦画面では操作欄を詰めて盤面を広く */
@media (max-aspect-ratio: 1/1) and (max-height: 760px) {
  .panel { padding-top: 6px; }
  .play-controls { gap: 6px; }
  .btn { min-height: 44px; }
  .next strong { font-size: 1.6rem; }
  .progress-bar { height: 6px; }
}

.toast {
  position: absolute; left: 50%; top: 12px; transform: translate(-50%, -8px);
  margin: 0; padding: 8px 16px; border-radius: 999px;
  background: rgba(43, 36, 32, 0.88); color: #fff; font-weight: 700;
  opacity: 0; pointer-events: none; transition: opacity 0.2s, transform 0.2s; white-space: nowrap;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }

/* 結果 */
.result { text-align: center; display: grid; gap: 4px; padding: 4px 0; }
.result-kicker { margin: 0; color: var(--primary); font-weight: 800; font-size: 1.1rem; letter-spacing: 0.1em; }
.result h2 { margin: 0; font-size: 1.7rem; }
.result h2:focus { outline: none; }
.result-desc { margin: 0 0 8px; color: var(--muted); font-size: 0.95rem; }
.result-actions { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }

/* 盤面のSVG要素 */
.board .art { opacity: 0; transition: opacity 0.8s ease; }
/* 完成前は正解の絵を必ず隠す（消えるときはすぐ消し、完成したときだけふわっと出す） */
.board:not(.finished) .art { visibility: hidden; transition: none; }
.board .lines { fill: none; stroke: var(--line); stroke-width: 3; stroke-linecap: round; stroke-linejoin: round; vector-effect: non-scaling-stroke; }
.board .dot { fill: var(--dot); }
.board .dot.done { fill: var(--dot-done); }
.board .label {
  fill: #2b2420; font-weight: 700; text-anchor: middle; dominant-baseline: central;
  paint-order: stroke; stroke: var(--board-bg); stroke-linejoin: round;
  font-family: system-ui, sans-serif; font-variant-numeric: tabular-nums;
}
.board .label.done { fill: #9a8f86; font-weight: 500; }
.board .points, .board .labels { transition: opacity 0.5s; }
.board.finished .art { opacity: 1; }
.board.finished .points, .board.finished .labels { opacity: 0; }
.board.finished .lines { stroke: transparent; }
.board .hint-outer { fill: none; stroke: var(--hint); stroke-width: 4; vector-effect: non-scaling-stroke; animation: pulse 1s ease-in-out infinite alternate; }
.board .hint-inner { fill: var(--hint); fill-opacity: 0.35; stroke: #8a5d00; stroke-width: 2; vector-effect: non-scaling-stroke; }
.board .start-ring { fill: none; stroke: #2b2420; stroke-width: 2.5; vector-effect: non-scaling-stroke; }
.board .goal-star { fill: #2b2420; }
.board .marks { transition: opacity 0.5s; }
.board.finished .marks, .board.finished .guide { opacity: 0; }
.board .focus-ring { fill: none; stroke: var(--focus); stroke-width: 3; vector-effect: non-scaling-stroke; }
@keyframes pulse { from { stroke-opacity: 1; } to { stroke-opacity: 0.35; } }

/* 横向き・広い画面: 操作欄を盤面の横に */
@media (min-aspect-ratio: 1/1) {
  .play {
    grid-template-areas: "top top" "board panel";
    grid-template-columns: minmax(0, 1fr) clamp(220px, 32vw, 320px);
    grid-template-rows: auto minmax(0, 1fr);
  }
  .board-wrap { margin: 0 0 8px 8px; margin-left: max(8px, env(safe-area-inset-left)); }
  .panel { padding-top: 0; padding-right: max(12px, env(safe-area-inset-right)); }
  .result-actions { grid-template-columns: 1fr; }
}
/* 背の低い横画面（スマホ横） */
@media (min-aspect-ratio: 1/1) and (max-height: 480px) {
  .topbar { padding-top: 2px; padding-bottom: 2px; }
  .topbar .btn { min-height: 40px; }
  .play-controls { gap: 6px; }
  .next strong { font-size: 1.5rem; }
  .result h2 { font-size: 1.3rem; }
}

/* ダイアログ */
.dialog {
  border: none; border-radius: 18px; padding: 0; max-width: min(520px, calc(100vw - 32px));
  background: var(--surface); color: var(--text); box-shadow: 0 10px 40px rgba(0, 0, 0, 0.3);
}
.dialog::backdrop { background: rgba(30, 20, 10, 0.45); }
.dialog form { padding: 20px; }
.dialog h2 { margin: 0 0 8px; font-size: 1.25rem; }
.dialog h3 { font-size: 1rem; margin: 16px 0 4px; }
.dialog p, .dialog li { font-size: 0.95rem; }
.dialog ol, .dialog ul { padding-left: 1.3em; margin: 4px 0; }
.dialog-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 16px; flex-wrap: wrap; }
.help { max-height: calc(100dvh - 32px); overflow-y: auto; }

/* 動きを減らす */
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation: none !important; transition: none !important; }
}
.reduce-motion *, .reduce-motion *::before, .reduce-motion *::after { animation: none !important; transition: none !important; }
