:root {
  --bg: #f4ede1;
  --bg-2: #ece2d1;
  --ink: #2a1d12;
  --muted: #7b6a58;
  --line: #ddcdb4;
  --card: #fffaf1;
  --accent: #b2361f;
  --accent-ink: #fff;
  --accent-soft: rgba(178, 54, 31, .1);
  --ok: #2f9e5b;
  --wood-1: #ecc683;
  --wood-2: #dcaa5c;
  --wood-edge: #a8702f;
  --wood-edge-2: #8a5722;
  --tray: #e6c48c;
  --grid: #6b4218;
  --piece: #f7e3b5;
  --piece-edge: #a7773c;
  --sel: rgba(178, 54, 31, .3);
  --target: rgba(24, 100, 62, .6);
  --last-to: rgba(255, 214, 80, .62);
  --last-from: rgba(255, 214, 80, .3);
  --shadow: 0 1px 2px rgba(60, 30, 5, .06), 0 6px 20px rgba(60, 30, 5, .08);
  --cell: min(calc((100vw - 56px) / 9), 56px);
  --font-piece: 'Shippori Mincho B1', 'Hiragino Mincho ProN', 'Yu Mincho', serif;
  --font-ui: 'Zen Kaku Gothic New', 'Hiragino Sans', system-ui, sans-serif;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #1b1612; --bg-2: #241d17; --ink: #f1e7d8; --muted: #a8977f; --line: #3b3128; --card: #241d17;
    --accent-soft: rgba(230, 110, 85, .16);
    --shadow: 0 1px 2px rgba(0, 0, 0, .3), 0 6px 20px rgba(0, 0, 0, .3);
  }
}
:root[data-theme="dark"] {
  --bg: #1b1612; --bg-2: #241d17; --ink: #f1e7d8; --muted: #a8977f; --line: #3b3128; --card: #241d17;
  --accent-soft: rgba(230, 110, 85, .16);
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--ink);
  font-family: var(--font-ui);
  -webkit-tap-highlight-color: transparent;
  min-height: 100dvh;
}
[hidden] { display: none !important; }
button { font-family: inherit; }

/* ---------- ボタン ---------- */
.btn {
  font: 700 16px/1 var(--font-ui);
  border-radius: 12px;
  padding: 14px 18px;
  border: 1.5px solid transparent;
  cursor: pointer;
  transition: transform .08s, filter .15s, background .15s, border-color .15s;
  text-align: center;
  text-decoration: none;
  display: inline-block;
}
.btn:active { transform: scale(.97); }
.btn.block { display: block; width: 100%; }
.btn.primary { background: var(--accent); color: var(--accent-ink); box-shadow: 0 2px 0 #7f2414; }
.btn.primary:hover { filter: brightness(1.07); }
.btn.dark { background: var(--ink); color: var(--bg); }
.btn.dark:hover { filter: brightness(1.2); }
.btn.ghost { background: var(--card); color: var(--ink); border-color: var(--line); }
.btn.ghost:hover { border-color: var(--muted); }
.btn.small { font-size: 14px; padding: 10px 14px; }
.btn { white-space: nowrap; }
.btn.line { background: #06c755; color: #fff; }
.btn.line:hover { filter: brightness(1.06); }
.btn:disabled { opacity: .55; cursor: default; transform: none; }
:focus-visible { outline: 2.5px solid var(--accent); outline-offset: 2px; }

.field { display: flex; flex-direction: column; gap: 6px; text-align: left; font-size: 13px; color: var(--muted); font-weight: 700; }
input {
  font: 16px var(--font-ui); color: var(--ink);
  background: var(--bg); border: 1.5px solid var(--line); border-radius: 10px; padding: 12px;
  width: 100%; min-width: 0;
}
input:focus { outline: 2px solid var(--accent); outline-offset: 1px; border-color: transparent; }
input::placeholder { color: var(--muted); opacity: .75; }

/* ---------- トップ ---------- */
.home {
  min-height: 100dvh; padding: 32px 16px 0;
  background:
    radial-gradient(1200px 400px at 50% -120px, rgba(220, 170, 92, .35), transparent 70%),
    var(--bg);
}
.hero { text-align: center; margin: 0 auto 28px; max-width: 640px; }
.hero-mark {
  width: 72px; height: 82px; margin: 0 auto 10px;
  background: linear-gradient(165deg, #fbebc6, #e3bf80);
  clip-path: polygon(50% 0, 87% 14%, 100% 100%, 0 100%, 13% 14%);
  display: grid; place-items: center; padding-top: 12px;
  font: 800 40px var(--font-piece); color: #2a1a0c;
  filter: drop-shadow(0 4px 8px rgba(90, 50, 10, .25));
}
.hero-parent { display: inline-block; font-size: 13px; font-weight: 700; color: var(--muted); text-decoration: none; margin-bottom: 2px; letter-spacing: .08em; }
.hero-parent:hover { color: var(--accent); }
.hero h1 { font: 800 clamp(40px, 9vw, 56px)/1.1 var(--font-piece); margin: 0; letter-spacing: .06em; }
.lead { color: var(--muted); margin: 10px 0 0; font-size: 15px; text-wrap: balance; word-break: keep-all; overflow-wrap: anywhere; }

.modes {
  display: grid; gap: 16px; max-width: 1000px; margin: 0 auto;
  grid-template-columns: repeat(auto-fit, minmax(280px, 1fr)); align-items: start;
}
.mode {
  background: var(--card); border: 1px solid var(--line); border-radius: 18px; padding: 20px;
  box-shadow: var(--shadow); display: flex; flex-direction: column; gap: 12px;
}
.mode-friend { border-top: 4px solid var(--accent); }
.mode h2 { font: 800 21px/1.3 var(--font-piece); margin: 0 0 2px; display: flex; align-items: center; gap: 10px; }
.mode-icon {
  width: 30px; height: 34px; flex-shrink: 0;
  background: linear-gradient(165deg, #fbebc6, #e3bf80);
  clip-path: polygon(50% 0, 87% 14%, 100% 100%, 0 100%, 13% 14%);
  display: grid; place-items: center; padding-top: 5px;
  font: 800 16px var(--font-piece); color: #2a1a0c;
}
.mode-friend .mode-icon { color: var(--accent); }
.mode-note { margin: 0; font-size: 13px; color: var(--muted); line-height: 1.6; text-wrap: pretty; }
.or { display: flex; align-items: center; gap: 10px; color: var(--muted); font-size: 12px; margin: 4px 0 0; }
.or::before, .or::after { content: ''; flex: 1; height: 1px; background: var(--line); }
.code-row { display: flex; gap: 8px; }
.code-row .btn { padding: 12px 18px; flex-shrink: 0; }
.seg-label { font-size: 13px; color: var(--muted); font-weight: 700; margin-bottom: -6px; }
.seg { display: grid; grid-template-columns: repeat(3, 1fr); background: var(--bg-2); border-radius: 12px; padding: 4px; gap: 4px; }
.seg button {
  font: 700 14px var(--font-ui); color: var(--muted); background: transparent;
  border: 0; border-radius: 9px; padding: 10px 4px; cursor: pointer; transition: background .15s, color .15s;
}
.seg button[aria-checked="true"] { background: var(--card); color: var(--ink); box-shadow: 0 1px 3px rgba(60, 30, 5, .15); }
.rules-teaser { margin-top: 4px; padding: 14px; border-radius: 12px; background: var(--bg-2); font-size: 14px; }
.rules-teaser p { margin: 4px 0 6px; color: var(--muted); font-size: 13px; }
.rules-teaser a { color: var(--accent); font-weight: 700; text-decoration: none; }
.home-footer { text-align: center; font-size: 12px; color: var(--muted); padding: 40px 16px 28px; }
.home-footer nav { display: flex; gap: 18px; justify-content: center; flex-wrap: wrap; margin-bottom: 8px; }
.home-footer a { color: var(--muted); }

/* ---------- 対局画面 ---------- */
.game { max-width: 1000px; margin: 0 auto; padding: 8px 16px 32px; }
.topbar { display: flex; align-items: center; justify-content: space-between; gap: 12px; min-height: 48px; }
.logo { font: 800 20px var(--font-piece); color: var(--ink); text-decoration: none; letter-spacing: .05em; display: flex; align-items: center; gap: 8px; }
.logo-mark {
  width: 24px; height: 27px; background: linear-gradient(165deg, #fbebc6, #e3bf80);
  clip-path: polygon(50% 0, 87% 14%, 100% 100%, 0 100%, 13% 14%);
  display: grid; place-items: center; padding-top: 4px; font-size: 13px; color: #2a1a0c;
}
.icon-btn {
  width: 40px; height: 40px; border-radius: 50%; border: 1px solid var(--line); background: var(--card);
  display: grid; place-items: center; cursor: pointer; color: var(--ink);
}
.icon-btn svg { width: 20px; height: 20px; fill: currentColor; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; }
.icon-btn svg .mute { display: none; }
.icon-btn[aria-pressed="false"] { color: var(--muted); }
.icon-btn[aria-pressed="false"] svg .wave { display: none; }
.icon-btn[aria-pressed="false"] svg .mute { display: inline; }

.banner {
  display: flex; align-items: center; justify-content: center; gap: 8px;
  margin: 4px auto 12px; padding: 10px 18px; border-radius: 999px; width: fit-content; max-width: 100%;
  background: var(--card); border: 1px solid var(--line); font-weight: 700; font-size: 15px; box-shadow: var(--shadow);
  text-align: center;
}
.banner::before { content: ''; width: 9px; height: 9px; border-radius: 50%; background: var(--muted); flex-shrink: 0; }
.banner.mine { background: var(--accent); color: #fff; border-color: transparent; }
.banner.mine::before { background: #fff; animation: pulse 1.4s ease-in-out infinite; }
.banner.check { background: #8f1d0d; color: #fff; border-color: transparent; }
.banner.check::before { background: #ffd34d; }
.banner.done { background: var(--ink); color: var(--bg); }
.banner.done::before { display: none; }
.banner .dots::after { content: '…'; display: inline-block; width: 1.2em; text-align: left; animation: dots 1.2s steps(4) infinite; overflow: hidden; vertical-align: bottom; }
@keyframes dots { 0% { width: 0 } 100% { width: 1.2em } }
@keyframes pulse { 50% { opacity: .35 } }

.invite {
  background: var(--card); border: 1.5px solid var(--accent); border-radius: 16px;
  padding: 14px; margin: 6px auto 12px; max-width: 560px; box-shadow: var(--shadow);
  display: flex; flex-direction: column; gap: 10px;
}
.invite-head { display: flex; flex-direction: column; gap: 4px; font-size: 13px; color: var(--muted); line-height: 1.6; }
.invite-head > b { font: 800 17px var(--font-piece); color: var(--ink); }
.code-chip { background: var(--bg-2); color: var(--ink); padding: 1px 8px; border-radius: 6px; letter-spacing: .04em; }
.share-row { display: flex; gap: 8px; }
.share-row .btn { flex: 1; }
.invite-row { display: flex; gap: 8px; }
.invite-row input { font-size: 13px; padding: 9px 10px; color: var(--muted); }

.layout { display: flex; gap: 24px; align-items: flex-start; justify-content: center; }
.table { display: flex; flex-direction: column; gap: 10px; align-items: stretch; }

/* 対局者と駒台 */
.player {
  display: flex; align-items: center; gap: 10px; padding: 6px 6px 6px 10px;
  background: var(--card); border: 1.5px solid var(--line); border-radius: 14px;
  transition: border-color .2s, box-shadow .2s;
}
.player.turn { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
.pinfo { display: flex; align-items: center; gap: 8px; min-width: 0; width: 132px; flex-shrink: 0; }
.avatar {
  width: 34px; height: 34px; border-radius: 50%; flex-shrink: 0; position: relative;
  display: grid; place-items: center; font-size: 18px; background: var(--bg-2);
}
.avatar::after {
  content: ''; position: absolute; right: -1px; bottom: -1px; width: 10px; height: 10px; border-radius: 50%;
  background: var(--line); border: 2px solid var(--card);
}
.avatar.on::after { background: var(--ok); }
.nm { font-weight: 700; font-size: 14px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.sub { font-size: 11px; color: var(--muted); }
.player.turn .sub { color: var(--accent); font-weight: 700; }

.hand {
  flex: 1; display: flex; flex-wrap: wrap; gap: 3px; align-items: center; align-content: center;
  min-height: calc(var(--cell) * .9 + 8px); padding: 4px 6px; border-radius: 9px;
  background:
    repeating-linear-gradient(96deg, rgba(120, 70, 20, .05) 0 2px, transparent 2px 11px),
    linear-gradient(135deg, #efd3a2, var(--tray));
  box-shadow: inset 0 1px 3px rgba(90, 50, 10, .25);
}
.hand:empty::before { content: '持ち駒なし'; font-size: 11px; color: rgba(90, 55, 20, .5); margin: auto; }
.hand .piece { width: calc(var(--cell) * .82); height: calc(var(--cell) * .88); position: relative; font-size: calc(var(--cell) * .46); }
.hand .count {
  position: absolute; right: -3px; bottom: -2px; font: 700 11px var(--font-ui);
  background: var(--ink); color: #fff; border-radius: 99px; min-width: 17px; height: 17px;
  display: grid; place-items: center; padding: 0 4px; z-index: 2;
}
.hand .piece.enemy .count { transform: rotate(180deg); }

/* 盤 */
.board-frame {
  padding: 4px 6px 10px 10px; border-radius: 10px; align-self: center;
  background: linear-gradient(160deg, var(--wood-edge), var(--wood-edge-2));
  box-shadow: 0 2px 0 #6e4319, 0 14px 30px rgba(60, 30, 5, .28);
}
.files, .ranks { display: flex; color: rgba(255, 240, 210, .85); font: 700 11px var(--font-ui); }
.files { height: 16px; align-items: center; margin-right: 16px; }
.files span { width: var(--cell); text-align: center; }
.board-row { display: flex; }
.ranks { flex-direction: column; width: 16px; }
.ranks span { height: var(--cell); display: grid; place-items: center; font-family: var(--font-piece); }
.board {
  display: grid;
  grid-template-columns: repeat(9, var(--cell));
  grid-template-rows: repeat(9, var(--cell));
  background:
    repeating-linear-gradient(93deg, rgba(130, 80, 25, .06) 0 2px, transparent 2px 13px, rgba(130, 80, 25, .04) 13px 15px, transparent 15px 26px),
    linear-gradient(135deg, var(--wood-1), var(--wood-2));
  border: 1.5px solid var(--grid);
  position: relative;
}
.cell {
  border-right: 1px solid rgba(107, 66, 24, .75); border-bottom: 1px solid rgba(107, 66, 24, .75);
  position: relative; display: grid; place-items: center; cursor: pointer;
}
.cell:nth-child(9n) { border-right: 0; }
.cell:nth-last-child(-n+9) { border-bottom: 0; }
.cell.last-from { background: var(--last-from); }
.cell.last { background: var(--last-to); }
.cell.sel { background: var(--sel); }
.cell.check { background: radial-gradient(circle, rgba(220, 30, 10, .65) 0%, rgba(220, 30, 10, .25) 55%, transparent 75%); }
.cell.target::after {
  content: ''; position: absolute; width: 28%; height: 28%; border-radius: 50%;
  background: var(--target); pointer-events: none;
}
.cell.target.has::after {
  width: 100%; height: 100%; border-radius: 0; background: transparent;
  box-shadow: inset 0 0 0 3px var(--target);
}
@media (hover: hover) { .cell.target:hover { background: rgba(24, 100, 62, .18); } }
.star { position: absolute; width: 6px; height: 6px; background: var(--grid); border-radius: 50%; right: -3.5px; bottom: -3.5px; z-index: 1; }

.piece {
  width: 86%; height: 92%;
  background: linear-gradient(172deg, #fff1cf, var(--piece) 55%, #e6c88a);
  clip-path: polygon(50% 0, 86% 15%, 100% 100%, 0 100%, 14% 15%);
  display: grid; place-items: center; padding-top: 12%;
  font: 800 calc(var(--cell) * .5)/1 var(--font-piece); color: #1d130a;
  user-select: none; cursor: pointer;
  filter: drop-shadow(0 1.5px 0 var(--piece-edge)) drop-shadow(0 2px 2px rgba(60, 30, 5, .25));
}
.piece.enemy { transform: rotate(180deg); padding-top: 0; padding-bottom: 12%; }
.piece.promoted { color: #b0200f; }
.cell.sel .piece, .hand .piece.sel { transform: translateY(-4px) scale(1.06); filter: drop-shadow(0 1.5px 0 var(--piece-edge)) drop-shadow(0 6px 6px rgba(60, 30, 5, .35)); }
.cell.sel .piece.enemy { transform: rotate(180deg) translateY(4px) scale(1.06); }
.hand .piece.sel { background: linear-gradient(172deg, #ffe0cf, #f4b39a); }
.piece.just { animation: place .22s ease-out; }
.piece.enemy.just { animation-name: place-enemy; }
@keyframes place { from { transform: scale(1.22); } to { transform: scale(1); } }
@keyframes place-enemy { from { transform: rotate(180deg) scale(1.22); } to { transform: rotate(180deg) scale(1); } }

/* サイド */
.side { width: 240px; flex-shrink: 0; display: flex; flex-direction: column; gap: 14px; }
.actions { display: grid; grid-template-columns: repeat(auto-fit, minmax(64px, 1fr)); gap: 8px; }
.act {
  display: flex; flex-direction: column; align-items: center; gap: 4px; padding: 10px 4px;
  background: var(--card); border: 1px solid var(--line); border-radius: 12px; cursor: pointer;
  font: 700 12px var(--font-ui); color: var(--ink); transition: border-color .15s, background .15s;
}
.act:hover { border-color: var(--muted); }
.act svg { width: 22px; height: 22px; stroke: currentColor; stroke-width: 1.9; stroke-linecap: round; stroke-linejoin: round; }
.act.danger { color: var(--accent); }
.actions .btn { grid-column: 1 / -1; }
.kifu-box { background: var(--card); border: 1px solid var(--line); border-radius: 14px; padding: 12px 14px; }
.kifu-box h2 { font: 800 16px var(--font-piece); margin: 0 0 8px; display: flex; justify-content: space-between; align-items: baseline; }
.kifu-box h2 span { font: 400 12px var(--font-ui); color: var(--muted); }
.kifu {
  margin: 0; padding: 0; list-style: none; max-height: 440px; overflow-y: auto; counter-reset: mv;
  font-size: 14px; line-height: 1.9; font-variant-numeric: tabular-nums;
}
.kifu:empty::before { content: 'まだ指していません'; color: var(--muted); font-size: 13px; }
.kifu li { counter-increment: mv; padding: 0 6px; border-radius: 6px; }
.kifu li::before { content: counter(mv); display: inline-block; width: 2.2em; color: var(--muted); font-size: 12px; }
.kifu li:last-child { background: var(--last-to); color: #2a1d12; font-weight: 700; }

/* ダイアログ */
.dialog {
  border: 0; border-radius: 20px; padding: 24px; background: var(--card); color: var(--ink);
  box-shadow: 0 20px 60px rgba(0, 0, 0, .35); text-align: center; width: min(340px, calc(100vw - 32px));
}
.dialog[open] { animation: pop .18s ease-out; }
@keyframes pop { from { transform: scale(.94); opacity: 0; } }
.dialog::backdrop { background: rgba(20, 10, 0, .45); backdrop-filter: blur(2px); }
.dialog-title { font: 800 22px var(--font-piece); margin: 0 0 16px; }
.dialog-row { display: flex; gap: 10px; justify-content: center; }
.dialog-row form { margin: 0; }
.promote-choices { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.choice {
  display: flex; flex-direction: column; align-items: center; gap: 10px; padding: 16px 8px 14px;
  border-radius: 14px; border: 1.5px solid var(--line); background: var(--bg); cursor: pointer;
  font: 700 15px var(--font-ui); color: var(--ink);
}
.choice:hover { border-color: var(--accent); }
.choice .piece { width: 52px; height: 58px; font-size: 28px; cursor: pointer; }
#promote-yes { border-color: var(--accent); background: var(--accent-soft); }
.result-mark { font: 800 64px/1 var(--font-piece); margin-bottom: 8px; }
.result-mark.win { color: var(--accent); }
.result-mark.lose { color: var(--muted); }
.result .dialog-title { margin-bottom: 6px; font-size: 26px; }
.result-sub { color: var(--muted); margin: 0 0 20px; font-size: 14px; }

.toast {
  position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%);
  background: var(--ink); color: var(--bg); padding: 11px 18px; border-radius: 12px; font-size: 14px;
  box-shadow: 0 4px 16px rgba(0, 0, 0, .25); z-index: 10; max-width: calc(100vw - 32px);
}

/* スマホ */
@media (max-width: 820px) {
  .layout { flex-direction: column; align-items: center; gap: 12px; }
  .table { width: 100%; align-items: stretch; }
  .side { width: 100%; max-width: 560px; }
  .kifu-box { padding: 10px 12px; }
  .kifu { display: flex; gap: 4px; overflow-x: auto; max-height: none; white-space: nowrap; padding-bottom: 4px; scrollbar-width: thin; }
  .kifu li { flex-shrink: 0; background: var(--bg-2); padding: 0 8px; }
  .kifu li::before { width: auto; margin-right: 6px; }
  .actions { grid-template-columns: repeat(auto-fit, minmax(70px, 1fr)); }
}
@media (max-width: 420px) {
  .game { padding: 4px 8px 24px; }
  .pinfo { width: 104px; }
  .avatar { width: 28px; height: 28px; font-size: 15px; }
  .nm { font-size: 13px; }
  .banner { font-size: 14px; padding: 8px 14px; margin-bottom: 10px; }
  .board-frame { padding: 3px 4px 8px 8px; }
  .home { padding-top: 20px; }
  .hero { margin-bottom: 20px; }
  .hero-mark { width: 52px; height: 60px; font-size: 28px; padding-top: 9px; margin-bottom: 6px; }
  .mode { padding: 18px 16px; }
}
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation: none !important; transition: none !important; }
}
