:root {
  --bg: #f5f1e8;
  --ink: #1f2a44;
  --muted: #5d6680;
  --card: #ffffff;
  --line: #d9d3c4;
  --empty: #e7e2d6;
  --p1: #2f6fdf;
  --p1-soft: #dbe7fb;
  --p2: #e8772a;
  --p2-soft: #fbe6d5;
  --ok: #2ea56a;
  --bad: #d64545;
  --gold: #f4c542;
  --t1: #8a93a8; --t2: #3f9b8f; --t3: #7a5cc7; --t4: #c2477b; --t5: #b8860b;
  --r: 16px;
  --safe-b: env(safe-area-inset-bottom, 0px);
}
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg); color: var(--ink);
  font: 18px/1.35 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  touch-action: manipulation; -webkit-user-select: none; user-select: none;
  overscroll-behavior: none;
}
[hidden] { display: none !important; }
button { font: inherit; color: inherit; cursor: pointer; }

/* ---------- buttons ---------- */
.big {
  display: block; width: 100%; min-height: 60px; margin: 10px 0;
  border-radius: var(--r); border: 2px solid var(--ink); background: var(--card);
  font-size: 21px; font-weight: 700; padding: 12px 16px;
}
.big.primary { background: var(--ink); color: #fff; }
.big.ghost { border-color: transparent; background: transparent; color: var(--muted); }
.big:disabled { opacity: .35; }
.big:active:not(:disabled) { transform: scale(.98); }
.icon-btn {
  width: 52px; height: 52px; border-radius: 14px; border: 2px solid var(--line);
  background: var(--card); font-size: 24px; font-weight: 700;
}

/* ---------- home ---------- */
.screen { min-height: 100%; }
#home { display: flex; align-items: center; justify-content: center; padding: 24px 20px calc(24px + var(--safe-b)); }
.home-inner { width: 100%; max-width: 420px; text-align: center; }
h1 { font-size: 56px; margin: 8px 0 4px; letter-spacing: -1px; }
.tag { font-size: 20px; color: var(--muted); margin: 0 0 24px; }
.logo { display: grid; grid-template-columns: repeat(3, 34px); gap: 5px; justify-content: center; margin-top: 8px; }
.lb { width: 34px; height: 34px; border-radius: 8px; background: var(--empty); }
.lb.a { background: var(--p1); } .lb.b { background: var(--p2); }
.level { display: flex; align-items: center; justify-content: center; gap: 8px; margin: 2px 0 10px; color: var(--muted); }
.seg { min-height: 48px; padding: 0 18px; border-radius: 12px; border: 2px solid var(--line); background: var(--card); font-weight: 600; }
.seg.on { border-color: var(--ink); background: var(--ink); color: #fff; }
.fine { color: var(--muted); font-size: 14px; margin-top: 18px; }

/* ---------- overlays ---------- */
.overlay {
  position: fixed; inset: 0; z-index: 20; background: rgba(31, 42, 68, .55);
  display: flex; align-items: flex-end; justify-content: center;
  padding: 16px 16px calc(16px + var(--safe-b));
}
.overlay.solid { background: var(--ink); align-items: center; }
.sheet {
  width: 100%; max-width: 440px; max-height: 100%; overflow: auto;
  background: var(--card); border-radius: 22px; padding: 22px 20px;
  animation: up .2s ease-out;
}
@keyframes up { from { transform: translateY(30px); opacity: 0; } }
.sheet h2 { font-size: 28px; margin: 0 0 12px; }
.sheet.center { text-align: center; }
.sheet label { display: block; font-weight: 600; margin: 12px 0; color: var(--muted); }
.sheet input {
  display: block; width: 100%; margin-top: 6px; font: inherit; font-size: 22px; color: var(--ink);
  padding: 12px 14px; border-radius: 12px; border: 2px solid var(--line); -webkit-user-select: text; user-select: text;
}
.kicker { margin: 0; color: var(--muted); font-weight: 600; }
.rules ol { padding-left: 22px; margin: 0 0 8px; }
.rules li { margin-bottom: 12px; }
.piece-show { display: flex; justify-content: center; margin: 14px 0; }
.over-score { font-size: 22px; font-weight: 700; }

/* ---------- game ---------- */
#game {
  max-width: 520px; margin: 0 auto; padding: 8px 12px calc(12px + var(--safe-b));
  padding-top: max(8px, env(safe-area-inset-top));
}
.topbar { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.turnline { font-size: 20px; font-weight: 700; text-align: center; }
.turnline small { display: block; font-size: 14px; font-weight: 600; color: var(--muted); }

.scores { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; margin: 8px 0; }
.pcard {
  background: var(--card); border: 3px solid transparent; border-radius: var(--r);
  padding: 8px 10px; position: relative; opacity: .75;
}
.pcard.active { opacity: 1; }
.pcard.p1.active { border-color: var(--p1); background: var(--p1-soft); }
.pcard.p2.active { border-color: var(--p2); background: var(--p2-soft); }
.pname { font-weight: 800; font-size: 18px; display: flex; align-items: center; gap: 6px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.dot { width: 14px; height: 14px; border-radius: 4px; flex: none; }
.p1 .dot { background: var(--p1); } .p2 .dot { background: var(--p2); }
.plines { font-size: 15px; color: var(--muted); }
.plines b { font-size: 24px; color: var(--ink); }
.caps { display: flex; gap: 4px; margin-top: 4px; flex-wrap: wrap; }
.cap {
  min-width: 34px; padding: 2px 5px; border-radius: 8px; color: #fff; font-size: 13px; font-weight: 700; text-align: center;
}
.cap.zero { background: #c9c4b8 !important; text-decoration: line-through; }
.cap small { opacity: .8; font-weight: 600; }

.board-wrap { display: flex; justify-content: center; }
.board {
  --cell: min(calc((100vw - 40px) / 8), 54px, calc((100vh - 360px) / 8));
  display: grid; grid-template-columns: repeat(8, var(--cell)); grid-auto-rows: var(--cell);
  gap: 3px; padding: 6px; border-radius: 14px; background: var(--ink); touch-action: none;
}
@media (max-height: 640px) { .board { --cell: min(calc((100vw - 40px) / 8), 54px, 34px); } }
.cell { border-radius: 6px; background: var(--empty); position: relative; }
.cell.f1 { background: var(--p1); }
.cell.f2 { background: var(--p2); }
.cell.last::after { content: ""; position: absolute; inset: 30%; border-radius: 50%; background: rgba(255,255,255,.55); }
.cell.pv-ok { background: rgba(46,165,106,.85); box-shadow: inset 0 0 0 3px #1b7a4b; }
.cell.pv-bad { box-shadow: inset 0 0 0 3px var(--bad); background: rgba(214,69,69,.55); }
.cell.will-clear { box-shadow: inset 0 0 0 3px var(--gold); }
.cell.cleared { animation: clear .7s ease-out; }
@keyframes clear { 0% { background: var(--gold); transform: scale(1.08); } 100% { background: var(--empty); } }

.panel { margin-top: 10px; }
.panel h3 { margin: 4px 0 8px; font-size: 21px; }
.panel .hint { color: var(--muted); margin: 0 0 6px; font-size: 16px; }
.dealt-row { display: flex; align-items: center; gap: 14px; background: var(--card); border-radius: var(--r); padding: 10px 12px; }
.dealt-row .meta { font-size: 15px; color: var(--muted); }
.dealt-row .meta b { display: block; font-size: 19px; color: var(--ink); }

.mini { display: grid; gap: 2px; }
.mini i { display: block; border-radius: 3px; }
.mini i.on { background: var(--ink); }
.mini.c1 i.on { background: var(--p1); } .mini.c2 i.on { background: var(--p2); }

.tier { margin: 10px 0 4px; display: flex; align-items: center; gap: 8px; font-weight: 700; }
.tier .badge { color: #fff; border-radius: 8px; padding: 2px 8px; font-size: 14px; }
.tier .left { color: var(--muted); font-weight: 600; font-size: 15px; }
.tiles { display: grid; grid-template-columns: repeat(auto-fill, minmax(76px, 1fr)); gap: 8px; }
.tile {
  min-height: 76px; border-radius: 14px; border: 3px solid var(--line); background: var(--card);
  display: flex; align-items: center; justify-content: center; padding: 6px;
}
.tile:disabled { opacity: .25; }
.tile.sel { border-color: var(--ink); background: #fff8dc; box-shadow: 0 0 0 3px var(--gold); }
.sticky { position: sticky; bottom: 0; padding: 8px 0 calc(4px + var(--safe-b)); background: linear-gradient(to top, var(--bg) 75%, rgba(245,241,232,0)); }
.thinking { text-align: center; color: var(--muted); font-size: 19px; padding: 20px 0; }

.toast {
  position: fixed; left: 50%; top: 38%; transform: translate(-50%, -50%); z-index: 30;
  background: var(--gold); color: var(--ink); font-weight: 800; font-size: 26px;
  padding: 12px 22px; border-radius: 16px; box-shadow: 0 8px 30px rgba(0,0,0,.25);
  animation: pop 1.2s ease-out forwards; pointer-events: none;
}
@keyframes pop { 0% { opacity: 0; transform: translate(-50%,-30%) scale(.8); } 15% { opacity: 1; transform: translate(-50%,-50%) scale(1); } 80% { opacity: 1; } 100% { opacity: 0; } }
