:root {
  --bg: #f4ede0;
  --surface: #fffcf6;
  --ink: #2a2118;
  --muted: #7b6b59;
  --line: #e3d7c3;
  --accent: #b4462a;
  --accent-ink: #fff;
  --good: #2e7a4c;
  --good-bg: #e6f2e9;
  --bad: #b8392b;
  --bad-bg: #f9e6e2;
  --info-bg: #f3ece0;
  --wood: #e9c98b;
  --wood-line: #8d6a37;
  --piece: #f8e7c0;
  --piece-edge: #b08a4f;
  --piece-ink: #1f1810;
  --promoted-ink: #c0301c;
  --hl-last: rgba(180, 70, 42, .22);
  --hl-sel: rgba(46, 122, 76, .35);
  --dot: rgba(46, 122, 76, .75);
  --ghost: rgba(42, 33, 24, .28);
  --hint: #e0a100;
  --shadow: 0 1px 2px rgba(60, 40, 10, .08), 0 4px 14px rgba(60, 40, 10, .07);
  --piece-font: "Hiragino Mincho ProN", "Yu Mincho", "YuMincho", "Noto Serif JP", serif;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #1c1813;
    --surface: #27211a;
    --ink: #f0e7d7;
    --muted: #a8977f;
    --line: #3b3227;
    --accent: #e0714f;
    --accent-ink: #1c1813;
    --good: #6fc08f;
    --good-bg: #1f3326;
    --bad: #ef7f6d;
    --bad-bg: #3a221d;
    --info-bg: #2e271f;
    --wood: #c7a466;
    --wood-line: #6b4f25;
    --piece: #f3e1b8;
    --hl-last: rgba(224, 113, 79, .32);
    --ghost: rgba(42, 33, 24, .38);
    --shadow: 0 1px 2px rgba(0, 0, 0, .3), 0 4px 14px rgba(0, 0, 0, .25);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--ink);
  font-family: -apple-system, BlinkMacSystemFont, "Hiragino Sans", "Yu Gothic UI", "Noto Sans JP", sans-serif;
  line-height: 1.6;
  -webkit-tap-highlight-color: transparent;
  -webkit-text-size-adjust: 100%;
}
/* ダブルタップでの拡大を止める(盤を素早くタップするため) */
html, body, button, .cell { touch-action: manipulation; }
button { font: inherit; color: inherit; cursor: pointer; }
[hidden] { display: none !important; }

#app {
  max-width: 520px;
  margin: 0 auto;
  padding: 0 16px calc(24px + env(safe-area-inset-bottom));
}

/* ---- 一覧 ---- */
.home-head { padding: 28px 0 8px; }
.home-head h1 {
  font-family: var(--piece-font);
  font-size: 32px;
  letter-spacing: .12em;
  margin: 0;
}
.lead { margin: 4px 0 0; color: var(--muted); font-size: 14px; }
.overall { margin-top: 14px; font-size: 13px; color: var(--muted); }
.overall b { color: var(--ink); font-size: 18px; }

.group-title {
  font-size: 13px;
  font-weight: 700;
  color: var(--muted);
  letter-spacing: .08em;
  margin: 24px 0 8px;
}
.lesson-card {
  display: flex;
  align-items: center;
  gap: 12px;
  width: 100%;
  text-align: left;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 14px;
  padding: 14px;
  margin-bottom: 10px;
  box-shadow: var(--shadow);
}
.lesson-card:active { transform: scale(.99); }
.lesson-icon {
  flex: none;
  width: 44px; height: 48px;
  display: grid; place-items: center;
  font-family: var(--piece-font);
  font-size: 22px; font-weight: 700;
  color: var(--piece-ink);
  background: var(--piece);
  clip-path: polygon(50% 0, 88% 16%, 100% 100%, 0 100%, 12% 16%);
}
.lesson-body { flex: 1; min-width: 0; }
.lesson-name { font-weight: 700; font-size: 16px; display: flex; align-items: center; gap: 8px; }
.lesson-sum { font-size: 13px; color: var(--muted); margin-top: 2px; line-height: 1.5; }
.lesson-state { flex: none; font-size: 12px; color: var(--muted); text-align: right; }
.lesson-state.cleared { color: var(--good); font-weight: 700; }
.tag {
  font-size: 11px;
  font-weight: 600;
  padding: 1px 8px;
  border-radius: 999px;
  background: var(--info-bg);
  color: var(--muted);
  white-space: nowrap;
}

/* ---- 練習 ---- */
.topbar {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 12px 0 8px;
  position: sticky; top: 0;
  background: var(--bg);
  z-index: 5;
}
.icon-btn {
  border: none; background: none;
  font-size: 30px; line-height: 1;
  width: 36px; height: 36px;
  margin-left: -8px;
}
.topbar-title { flex: 1; min-width: 0; font-weight: 700; font-size: 17px; display: flex; align-items: center; gap: 8px; }
.topbar-title span:first-child { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.seg { display: flex; background: var(--info-bg); border-radius: 10px; padding: 3px; }
.seg button {
  border: none; background: none;
  padding: 5px 12px; border-radius: 8px;
  font-size: 13px; font-weight: 600; color: var(--muted);
}
.seg button.on { background: var(--surface); color: var(--ink); box-shadow: var(--shadow); }

.play { --cell: min(calc((100vw - 32px - 18px) / 9), 50px); }
.hand {
  min-height: calc(var(--cell) * .9);
  display: flex; align-items: center; gap: 4px;
  padding: 4px 2px;
  flex-wrap: wrap;
}
.hand-gote { justify-content: flex-start; }
.hand-sente { justify-content: flex-end; }
.hand-label { font-size: 12px; color: var(--muted); margin: 0 6px; }
.hand-piece {
  position: relative;
  border: none; background: none; padding: 0;
  width: calc(var(--cell) * .82); height: calc(var(--cell) * .9);
}
.hand-piece .count {
  position: absolute; right: -4px; bottom: -2px;
  font-size: 11px; font-weight: 700;
  background: var(--ink); color: var(--bg);
  border-radius: 999px; min-width: 16px; height: 16px; line-height: 16px;
  text-align: center;
}

.board-wrap { width: calc(var(--cell) * 9 + 18px); margin: 0 auto; }
.files { display: grid; grid-template-columns: repeat(9, var(--cell)); width: calc(var(--cell) * 9); }
.files span, .ranks span { font-size: 11px; color: var(--muted); text-align: center; }
.board-row { display: flex; }
.ranks { display: grid; grid-template-rows: repeat(9, var(--cell)); width: 18px; }
.ranks span { display: grid; place-items: center; }
.board {
  display: grid;
  grid-template-columns: repeat(9, var(--cell));
  grid-template-rows: repeat(9, var(--cell));
  background: var(--wood);
  border: 2px solid var(--wood-line);
  box-shadow: var(--shadow);
}
.board.shake { animation: shake .35s; }
@keyframes shake {
  20%, 60% { transform: translateX(-5px); }
  40%, 80% { transform: translateX(5px); }
}
.cell {
  position: relative;
  border-right: 1px solid var(--wood-line);
  border-bottom: 1px solid var(--wood-line);
  display: grid; place-items: center;
}
.cell:nth-child(9n) { border-right: none; }
.cell:nth-child(n+73) { border-bottom: none; }
.cell.last { background: var(--hl-last); }
.cell.sel { background: var(--hl-sel); }
.cell.dest::after {
  content: "";
  position: absolute;
  width: 28%; height: 28%;
  border-radius: 50%;
  background: var(--dot);
  pointer-events: none;
}
.cell.dest.capture::after {
  width: 86%; height: 86%;
  background: none;
  border: 3px solid var(--dot);
}
.cell.hint-from, .hand-piece.hint-from { animation: pulse 1.1s infinite; }
.cell.hint-to { box-shadow: inset 0 0 0 3px var(--hint); }
@keyframes pulse {
  0%, 100% { box-shadow: inset 0 0 0 3px transparent; }
  50% { box-shadow: inset 0 0 0 3px var(--hint); }
}
/* 星(3-6 の交点) */
.cell.star::before {
  content: ""; position: absolute;
  width: 5px; height: 5px; border-radius: 50%;
  background: var(--wood-line);
  right: -3px; bottom: -3px; z-index: 1;
}

.piece {
  width: 86%; height: 92%;
  display: grid; place-items: center;
  font-family: var(--piece-font);
  font-weight: 700;
  font-size: calc(var(--cell) * .52);
  line-height: 1;
  color: var(--piece-ink);
  background: var(--piece);
  clip-path: polygon(50% 0, 86% 15%, 100% 100%, 0 100%, 14% 15%);
  filter: drop-shadow(0 1px 0 var(--piece-edge));
  padding-top: 8%;
  pointer-events: none;
  user-select: none;
}
.piece.gote { transform: rotate(180deg); }
.piece.promoted { color: var(--promoted-ink); }
.piece.small { font-size: calc(var(--cell) * .4); }
.piece.ghost {
  background: transparent;
  color: var(--ghost);
  outline: 1.5px dashed var(--ghost);
  outline-offset: -4px;
  clip-path: none;
  filter: none;
  border-radius: 4px;
}

.status-row { display: flex; align-items: center; gap: 10px; margin: 10px 0 8px; }
.progress-bar { flex: 1; height: 6px; background: var(--line); border-radius: 999px; overflow: hidden; }
#progress-fill { height: 100%; width: 0; background: var(--accent); transition: width .3s; }
#progress-text { font-size: 12px; color: var(--muted); white-space: nowrap; font-variant-numeric: tabular-nums; }

.card {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 14px;
  box-shadow: var(--shadow);
}
.msg { padding: 12px 14px; min-height: 96px; font-size: 15px; }
.msg.good { border-color: var(--good); }
.msg.bad { border-color: var(--bad); background: var(--bad-bg); }
.msg-entry + .msg-entry { margin-top: 8px; padding-top: 8px; border-top: 1px dashed var(--line); }
.msg-move { font-weight: 700; font-family: var(--piece-font); margin-right: 6px; }
.msg-move.opp { color: var(--muted); }
.msg-title { font-weight: 700; margin-bottom: 4px; }
.msg ul { margin: 6px 0 0; padding-left: 1.2em; }
.msg li { margin: 2px 0; }
.msg .note { color: var(--muted); font-size: 13px; margin-top: 6px; }

.actions { display: flex; gap: 8px; margin-top: 12px; }
.btn {
  flex: 1;
  border: 1px solid var(--line);
  background: var(--surface);
  border-radius: 12px;
  padding: 11px 6px;
  font-size: 14px; font-weight: 600;
  box-shadow: var(--shadow);
}
.btn:disabled { opacity: .4; }
.btn.wide { flex: 2; }
.btn.primary { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }
.btn.toggle.on { background: var(--info-bg); border-color: var(--muted); }

.variations { margin-top: 12px; display: flex; flex-wrap: wrap; gap: 6px; }
.variations .label { width: 100%; font-size: 12px; color: var(--muted); }
.chip {
  border: 1px solid var(--line);
  background: var(--surface);
  border-radius: 999px;
  padding: 5px 12px;
  font-size: 13px;
}
.chip.on { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }

/* ---- オーバーレイ ---- */
.overlay {
  position: fixed; inset: 0;
  background: rgba(20, 14, 6, .45);
  display: grid; place-items: end center;
  z-index: 20;
  padding: 16px;
}
.sheet {
  width: 100%; max-width: 488px;
  background: var(--surface);
  border-radius: 20px;
  padding: 22px 20px calc(18px + env(safe-area-inset-bottom));
  box-shadow: var(--shadow);
  animation: rise .25s ease-out;
  max-height: 88vh; overflow: auto;
}
.sheet.small { text-align: center; }
@keyframes rise { from { transform: translateY(24px); opacity: 0; } }
.sheet h2 { margin: 2px 0 6px; font-size: 22px; }
.sheet-title { font-weight: 700; margin: 0 0 12px; }
.done-badge {
  display: inline-block; margin: 0;
  font-size: 12px; font-weight: 700; letter-spacing: .1em;
  color: var(--good); background: var(--good-bg);
  padding: 2px 10px; border-radius: 999px;
}
.done-score { font-weight: 700; color: var(--accent); margin: 8px 0 0; }
.var-list { list-style: none; padding: 0; margin: 12px 0 0; }
.var-list li {
  display: flex; align-items: center; gap: 8px;
  padding: 8px 0; border-top: 1px solid var(--line); font-size: 14px;
}
.var-list .mark { width: 20px; text-align: center; color: var(--muted); }
.var-list .mark.ok { color: var(--good); font-weight: 700; }
.var-list .name { flex: 1; }
.var-list button { border: none; background: none; color: var(--accent); font-weight: 600; font-size: 13px; }
.sheet-actions { display: flex; flex-direction: column; gap: 8px; margin-top: 16px; }
.promo-choices { display: flex; gap: 16px; justify-content: center; }
.promo-btn {
  width: 84px;
  padding: 10px 0 6px;
  border: 1px solid var(--line); border-radius: 12px;
  background: var(--wood);
  display: flex; flex-direction: column; align-items: center;
  --cell: 56px;
}
.promo-btn .piece { width: 46px; height: 50px; }
.promo-btn .caption { font-size: 12px; color: var(--muted); margin-top: 4px; }

@media (prefers-reduced-motion: reduce) {
  * { animation: none !important; transition: none !important; }
}
