/* ============================================================================
 * 軍儀 (Gungi) — スタイル
 * ========================================================================== */
:root {
  --board-bg: #e7cf98;
  --board-bg2: #ecd7a4;
  --board-line: #4a3823;
  --board-edge: #8a5f34;
  --cell: clamp(34px, 8.4vw, 62px);
  --w-piece: #faf3e4;
  --w-text: #2a2a2a;
  --b-piece: #2f2a25;
  --b-text: #f4e9d4;
  --accent: #2e7d5b;
  --danger: #b5443a;
  --sel: #f2c14e;
  --dest: rgba(46, 125, 91, 0.45);
  --dest-cap: rgba(181, 68, 58, 0.55);
  --last: rgba(242, 193, 78, 0.35);
  --panel: #fffdf7;
  --ink: #33291d;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: "Hiragino Sans", "Yu Gothic", "Noto Sans JP", system-ui, sans-serif;
  background: linear-gradient(160deg, #2b2019, #40312284 60%, #1c1611);
  color: var(--ink);
  min-height: 100vh;
  padding: 12px;
}

h1 { margin: 0; font-size: 1.5rem; letter-spacing: 0.15em; }
.subtitle { color: #d9c39c; font-size: 0.75rem; letter-spacing: 0.1em; }

header {
  text-align: center;
  color: #f4e9d4;
  margin-bottom: 10px;
}

.layout {
  display: flex;
  flex-wrap: wrap;
  gap: 16px;
  justify-content: center;
  align-items: flex-start;
  max-width: 1100px;
  margin: 0 auto;
}

.board-area { display: flex; flex-direction: column; align-items: center; gap: 8px; }

/* ---- 手駒パネル ---- */
.hand {
  background: var(--panel);
  border-radius: 10px;
  padding: 8px 10px;
  width: min(560px, 96vw);
  box-shadow: 0 3px 10px rgba(0,0,0,.35);
}
.hand h3 {
  margin: 0 0 6px; font-size: .8rem; display: flex; justify-content: space-between;
  align-items: center;
}
.hand h3 .turn-dot { font-size: .7rem; color: var(--accent); font-weight: 700; }
.hand-pieces { display: flex; flex-wrap: wrap; gap: 6px; min-height: 44px; }
.hand-piece {
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  width: 44px; height: 48px; border-radius: 50% / 46%; cursor: pointer;
  border: 1.5px solid #c3a877; font-weight: 800; color: #241f18;
  background: radial-gradient(circle at 40% 34%, #fffdf6, #f3e8ce 62%, #e6d3aa);
  box-shadow: 0 2px 3px rgba(0,0,0,.3), inset 0 2px 2px rgba(255,255,255,.6);
  user-select: none; position: relative; transition: transform .08s;
}
.hand-piece:hover { transform: translateY(-2px); }
.hand-piece.b {
  color: #f4e9d4; border-color: #050403;
  background: radial-gradient(circle at 40% 34%, #4e453b, #2c261f 60%, #130f0a);
}
.hand-piece.selected { border-color: var(--sel); box-shadow: 0 0 0 3px var(--sel); }
.hand-piece .k { font-size: 1.15rem; line-height: 1; }
.hand-piece .n { font-size: .55rem; opacity: .8; margin-top: 2px; }
.hand-piece .cnt {
  position: absolute; top: -6px; right: -6px; background: var(--accent);
  color: #fff; border-radius: 10px; font-size: .6rem; padding: 0 4px; min-width: 15px;
  text-align: center;
}
.hand.inactive { opacity: .55; }
.hand.inactive .hand-piece { cursor: default; }

/* ---- 盤 ---- */
.board-wrap { position: relative; padding: 22px; border-radius: 10px;
  background: linear-gradient(160deg, #9a6c3c, #7a5230);
  box-shadow: 0 6px 22px rgba(0,0,0,.5), inset 0 0 0 2px rgba(0,0,0,.15); }
.board {
  position: relative;
  display: grid;
  grid-template-columns: repeat(9, var(--cell));
  grid-template-rows: repeat(9, var(--cell));
  /* 木目 */
  background:
    repeating-linear-gradient(90deg, rgba(120,80,30,.05) 0 2px, transparent 2px 7px),
    linear-gradient(180deg, var(--board-bg2), var(--board-bg));
  border: 1.5px solid var(--board-line);
}
.sq {
  position: relative;
  border: 0.5px solid rgba(74, 56, 35, .75);
  display: flex; align-items: center; justify-content: center;
  cursor: pointer;
}
.sq.home-w { background: rgba(250, 243, 228, 0.22); }
.sq.home-b { background: rgba(47, 42, 37, 0.12); }
.sq.dest::after {
  content: ""; position: absolute; width: 30%; height: 30%; border-radius: 50%;
  background: var(--dest); box-shadow: 0 0 0 2px rgba(46,125,91,.25);
}
.sq.dest-cap::after { background: var(--dest-cap); width: 86%; height: 86%; border-radius: 50%;
  box-shadow: inset 0 0 0 2px rgba(181,68,58,.5); }
.sq.sel { box-shadow: inset 0 0 0 3px var(--sel); border-radius: 3px; }
.sq.last { background: var(--last); }

/* 星 (盤の交点の点) */
.star { position: absolute; width: 6px; height: 6px; margin: -3px 0 0 -3px;
  background: #3a2c1a; border-radius: 50%; pointer-events: none; z-index: 1; }

/* 駒 — 実物の丸い円盤(碁石を厚くした形) */
.piece {
  --edge: #c8ac76; --edge2: #b89860;
  --top: inset 0 2px 2px rgba(255,255,255,.65), inset 0 -4px 5px rgba(120,85,35,.18);
  width: 86%; aspect-ratio: 1 / 1; border-radius: 50%;
  display: flex; align-items: center; justify-content: center;
  font-weight: 800; position: relative; color: #241f18;
  background: radial-gradient(circle at 40% 34%, #fffdf6 0%, #f3e8ce 60%, #e6d3aa 100%);
  border: 1.5px solid #c3a877;
  box-shadow: 0 2.5px 3px rgba(0,0,0,.42), var(--top);
}
/* 黒駒: 黒い円盤 + 中央クリーム円 + 黒字, 180°回転(相手向き) */
.piece.b {
  --edge: #241e17; --edge2: #100c08;
  --top: inset 0 2px 2px rgba(255,255,255,.14), inset 0 -4px 6px rgba(0,0,0,.45);
  color: #1b160f; transform: rotate(180deg);
  background: radial-gradient(circle at 40% 34%, #4e453b 0%, #2c261f 58%, #130f0a 100%);
  border-color: #050403;
}
.piece.b::before {
  content: ""; position: absolute; width: 70%; height: 70%; border-radius: 50%;
  background: radial-gradient(circle at 42% 38%, #fbf4e2, #ecdfc1 82%);
  box-shadow: inset 0 1px 2px rgba(0,0,0,.28); z-index: 1;
}
.piece .k { font-size: calc(var(--cell) * .46); line-height: 1; position: relative; z-index: 2; }

/* ツケ(積み重ね)の円柱表現: 下に円盤の縁を覗かせる */
.piece.t2 { box-shadow: 0 4px 0 -2px var(--edge), 0 6px 5px rgba(0,0,0,.42), var(--top); }
.piece.t3 { box-shadow: 0 4px 0 -2px var(--edge), 0 8px 0 -2px var(--edge2),
  0 10px 6px rgba(0,0,0,.45), var(--top); }

.piece .tier {
  position: absolute; top: -3px; right: -3px; z-index: 3;
  min-width: 15px; height: 15px; padding: 0 3px; box-sizing: border-box;
  background: var(--accent); color: #fff; font-size: .6rem; line-height: 15px;
  border-radius: 8px; font-weight: 800; text-align: center;
  border: 1px solid #fffdf7;
}
.piece.b .tier { transform: rotate(180deg); top: auto; bottom: -3px; left: -3px; right: auto; }

/* 座標ラベル */
.coord { position: absolute; color: #d9c39c; font-size: .62rem; font-weight: 700; }
.coord.top { top: 5px; }
.coord.bottom { bottom: 5px; }
.coord.left { left: 5px; }
.coord.right { right: 5px; }

/* ---- サイドパネル ---- */
.side { width: min(320px, 96vw); display: flex; flex-direction: column; gap: 12px; }
.card { background: var(--panel); border-radius: 10px; padding: 12px 14px;
  box-shadow: 0 3px 10px rgba(0,0,0,.35); }
.status { font-size: .95rem; font-weight: 700; }
.status .who { padding: 2px 8px; border-radius: 6px; color:#fff; }
.who.w { background: #b07a2a; } .who.b { background: #3a3128; }
.msg { font-size: .82rem; color: #6b5638; margin-top: 6px; min-height: 1.2em; }

.controls { display: flex; flex-wrap: wrap; gap: 8px; }
button {
  font-family: inherit; font-size: .82rem; font-weight: 700; cursor: pointer;
  border: none; border-radius: 8px; padding: 8px 12px; color: #fff;
  background: var(--accent); transition: filter .1s;
}
button:hover { filter: brightness(1.08); }
button.secondary { background: #7a6a52; }
button.danger { background: var(--danger); }
button.toggle.on { background: #1f5c43; box-shadow: inset 0 0 0 2px #9be3c3; }
button.accent2 { background: #b07a2a; }
button:disabled { opacity: .4; cursor: default; }

.level-row { display: flex; align-items: center; gap: 8px; font-size: .82rem; font-weight: 700; }
.level-row select {
  flex: 1; font-family: inherit; font-size: .78rem; padding: 6px 8px; border-radius: 7px;
  border: 1px solid #cdbb98; background: #fffdf7; color: var(--ink);
}

/* オンライン対戦 */
.mode-row { display: flex; align-items: center; justify-content: space-between; gap: 8px;
  font-size: .8rem; font-weight: 700; flex-wrap: wrap; }
#mode-label { color: #6b5638; }
.mode-row.online #mode-label { color: var(--accent); }
.invite-row { display: flex; gap: 6px; }
.invite-row input {
  flex: 1; min-width: 0; font-family: ui-monospace, monospace; font-size: .78rem;
  padding: 8px 10px; border-radius: 7px; border: 1px solid #cdbb98; background: #f6efe0; color: var(--ink);
}
.online-status { margin-top: 12px; font-size: .84rem; font-weight: 700; color: #6b5638;
  padding: 8px 10px; background: #f3ead7; border-radius: 8px; text-align: center; }
.online-status.ok { color: #1f5c43; background: #ddf0e5; }
.online-status.err { color: var(--danger); background: #f6e0dd; }

.net-banner {
  position: fixed; left: 50%; bottom: 14px; transform: translateX(-50%); z-index: 60;
  background: #1f5c43; color: #eafaf2; padding: 8px 16px; border-radius: 20px;
  font-size: .8rem; font-weight: 700; box-shadow: 0 4px 14px rgba(0,0,0,.4); max-width: 92vw;
}
.net-banner.wait { background: #7a6a52; color: #fff; }
.net-banner.err { background: var(--danger); color: #fff; }
.net-banner.think { background: #b07a2a; color: #fff; }

/* CPU設定モーダル */
.cpu-field { margin: 14px 0; }
.cpu-field > label { display: block; font-size: .82rem; font-weight: 700; color: #6b5638; margin-bottom: 6px; }
.seg { display: flex; gap: 6px; flex-wrap: wrap; }
.seg button { background: #ece0c8; color: #5a4a30; border: 1.5px solid #d3c2a0; }
.seg button.on { background: var(--accent); color: #fff; border-color: var(--accent); }

.log { max-height: 220px; overflow-y: auto; font-size: .75rem; line-height: 1.7;
  font-family: ui-monospace, monospace; color: #4a3c28; }
.log div:nth-child(odd) { background: #f6efe0; }

.inspect { font-size: .8rem; }
.inspect .stack { display: flex; flex-direction: column-reverse; gap: 3px; margin-top: 6px; }
.inspect .layer { display: flex; align-items: center; gap: 6px; padding: 3px 6px;
  border-radius: 5px; background: #f3e7cf; }
.inspect .layer.b { background: #4a4038; color: var(--b-text); }
.inspect .layer .kk { font-weight: 800; font-size: 1rem; width: 1.4em; text-align:center; }

/* ---- モーダル ---- */
.overlay { position: fixed; inset: 0; background: rgba(0,0,0,.6);
  display: none; align-items: center; justify-content: center; z-index: 50; padding: 16px;
  -webkit-tap-highlight-color: transparent; }
.overlay.show { display: flex; }
.modal { position: relative; background: var(--panel); border-radius: 12px; padding: 20px 22px;
  max-width: 560px; width: 100%; max-height: 86vh; overflow-y: auto;
  -webkit-overflow-scrolling: touch; }
.modal h2 { margin-top: 0; padding-right: 40px; }
.modal .choice-btns { display: flex; gap: 10px; margin-top: 14px; flex-wrap: wrap; }
/* ×閉じるボタン */
.modal-x {
  position: absolute; top: 8px; right: 8px; width: 40px; height: 40px; padding: 0;
  border-radius: 50%; background: #eadfca; color: #6b5638; font-size: 1.5rem; line-height: 1;
  font-weight: 700; display: flex; align-items: center; justify-content: center;
}
.modal-x:hover { background: #ddcfb0; }
.modal-hint { font-size: .72rem; color: #9a8763; margin: -4px 0 8px; }
.rules-body h3 { margin: 14px 0 4px; color: var(--accent); }
.rules-body { font-size: .85rem; line-height: 1.7; }
.rules-body ul { margin: 4px 0; padding-left: 1.2em; }
.rules-body table { border-collapse: collapse; width: 100%; font-size: .78rem; margin-top: 6px; }
.rules-body th, .rules-body td { border: 1px solid #d8c9ab; padding: 3px 6px; text-align: left; }
.rules-body th { background: #f0e4cc; }

.winner-banner { text-align: center; }
.winner-banner .big { font-size: 2rem; font-weight: 800; color: var(--accent); }

.foot { text-align:center; color:#c9b48c; font-size:.7rem; margin-top:14px; }

/* ==== タブレット ==== */
@media (max-width: 900px) {
  .side { width: min(560px, 96vw); }
  .layout { gap: 12px; }
}

/* ==== スマホ ==== */
@media (max-width: 560px) {
  :root { --cell: min(9.5vw, 46px); }
  body { padding: 8px; }
  h1 { font-size: 1.25rem; }
  .subtitle { font-size: .68rem; }
  header { margin-bottom: 6px; }

  .layout { gap: 10px; }
  .hand, .side, .board-area { width: 100%; max-width: 100%; }
  .board-wrap { padding: 16px 15px; border-radius: 6px; }
  .board { border-width: 1px; }

  .coord { font-size: .55rem; }
  .coord.top { top: 3px; } .coord.bottom { bottom: 3px; }
  .coord.left { left: 3px; } .coord.right { right: 3px; }

  /* 手駒: タップしやすく折返し */
  .hand { padding: 7px 8px; }
  .hand-pieces { gap: 4px; }
  .hand-piece { width: 42px; height: 46px; }
  .hand-piece .k { font-size: 1.15rem; }

  .card { padding: 10px 12px; }
  .controls { gap: 6px; }
  button { padding: 9px 12px; font-size: .8rem; }
  .level-row { flex-wrap: wrap; }
  .level-row select { min-width: 0; }

  .modal { padding: 16px 16px; max-height: 88vh; }
  .rules-body { font-size: .82rem; }
}

/* タップ操作の最適化: 300ms遅延/ダブルタップズーム抑止 */
button, .hand-piece, .sq, select, .modal-x { touch-action: manipulation; }
.sq, .hand-piece, button, .modal-x { -webkit-tap-highlight-color: rgba(242,193,78,.25); }
