:root {
  --bg: #0e1218; --bg2: #151b25; --panel: #1a212e; --line: #2a3445; --text: #e8ecf4; --mute: #93a0b8;
  --gold: #e2b659; --gold-ink: #241a05; --green: #52d6a0; --red: #ff6a5c; --orange: #ffa94d; --blue: #6aa9ff;
  --ivory: #f1e7cf; --slate: #232b3b;
  --serif: "Iowan Old Style", "Palatino Linotype", Palatino, "Book Antiqua", Georgia, serif;
  --sans: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --cell: 22px;
}
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { height: 100%; margin: 0; }
body { touch-action: manipulation; background: radial-gradient(1200px 700px at 70% -10%, #1b2536 0%, var(--bg) 60%) fixed, var(--bg); color: var(--text); font: 15px/1.45 var(--sans); overscroll-behavior: none; }
[hidden] { display: none !important; }
h1, h2, h3, p { margin: 0; }
button, input { font: inherit; color: inherit; }
button { cursor: pointer; }

.btn { border: 1px solid var(--line); background: var(--panel); border-radius: 10px; padding: 9px 14px; font-weight: 600; min-height: 40px; transition: background .12s, border-color .12s, transform .05s; white-space: nowrap; }
.btn:hover { border-color: #3d4b63; background: #202a3a; }
.btn:active { transform: translateY(1px); }
.btn.primary { background: var(--gold); border-color: var(--gold); color: var(--gold-ink); }
.btn.primary:hover { background: #edc670; }
.btn.danger { border-color: #6b2f34; color: #ffb3ab; }
.btn.ghost { background: transparent; }
.btn.big { width: 100%; min-height: 48px; font-size: 16px; }
.btn.sm { min-height: 34px; padding: 5px 11px; font-size: 13.5px; border-radius: 8px; }
.btn:disabled { opacity: .45; cursor: default; }
.icon { border: 1px solid var(--line); background: var(--panel); border-radius: 9px; width: 38px; height: 38px; font-size: 22px; line-height: 1; padding: 0 0 3px; }
.icon:disabled { opacity: .35; }

.seg { display: flex; background: var(--bg2); border: 1px solid var(--line); border-radius: 11px; padding: 3px; gap: 3px; }
.seg button { flex: 1; border: 0; background: transparent; border-radius: 8px; padding: 9px 8px; font-weight: 600; color: var(--mute); min-height: 38px; }
.seg button.on { background: #2b3649; color: var(--text); box-shadow: inset 0 0 0 1px #3c4a63; }
.seg.small button { padding: 6px 8px; min-height: 32px; font-size: 13.5px; }

/* ---------------------------------------------------------------- lobby */
#lobby { max-width: 1080px; margin: 0 auto; padding: calc(18px + env(safe-area-inset-top)) 16px calc(28px + env(safe-area-inset-bottom)); }
.lobby-head { display: flex; align-items: flex-end; justify-content: space-between; gap: 12px; flex-wrap: wrap; margin-bottom: 18px; }
h1 { font: 600 40px/1 var(--serif); letter-spacing: .5px; }
h1 span { color: var(--gold); font-style: italic; }
.tag { color: var(--mute); margin-top: 6px; }
h2 { font: 600 13px/1 var(--sans); letter-spacing: 1.4px; text-transform: uppercase; color: var(--mute); margin-bottom: 12px; }
.card { background: linear-gradient(180deg, #1b2330, #171d29); border: 1px solid var(--line); border-radius: 16px; padding: 16px; }
.lobby-grid { display: grid; gap: 14px; grid-template-columns: 1fr; }
.lists { display: grid; gap: 14px; align-content: start; }
@media (min-width: 860px) { .lobby-grid { grid-template-columns: 380px 1fr; } }
.field { display: grid; gap: 6px; margin-bottom: 12px; }
.field > span { font-size: 12.5px; color: var(--mute); font-weight: 600; }
input { background: var(--bg2); border: 1px solid var(--line); border-radius: 10px; padding: 10px 12px; min-height: 42px; width: 100%; outline: none; font-size: 16px; }
input:focus { border-color: var(--gold); }
.mode-note { color: var(--mute); font-size: 13.5px; margin: 10px 2px 14px; min-height: 2.9em; }
.opts { margin-bottom: 4px; }
.list { list-style: none; margin: 0; padding: 0; display: grid; gap: 8px; }
.list li { display: flex; align-items: center; gap: 10px; background: var(--bg2); border: 1px solid var(--line); border-radius: 12px; padding: 9px 10px 9px 12px; }
.list .main { flex: 1; min-width: 0; }
.list .who { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.list .meta { color: var(--mute); font-size: 12.5px; }
.list .empty { color: var(--mute); background: transparent; border-style: dashed; justify-content: center; padding: 14px; }
.banner { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; background: #2a2413; border: 1px solid #6b5722; border-radius: 14px; padding: 12px 14px; margin-bottom: 14px; }
.banner .grow { flex: 1; min-width: 180px; }
.banner b { display: block; }
.banner small { color: #d9c48f; word-break: break-all; }
.foot { color: var(--mute); font-size: 12.5px; text-align: center; margin-top: 18px; }

/* ---------------------------------------------------------------- game */
#game { height: 100vh; height: 100dvh; display: flex; flex-direction: column; padding-top: env(safe-area-inset-top); padding-bottom: env(safe-area-inset-bottom); overflow: hidden; }
.pbar { display: flex; align-items: center; gap: 8px; padding: 6px 12px; min-height: 40px; background: var(--bg2); border-bottom: 1px solid var(--line); }
#pb-bottom { border-top: 1px solid var(--line); border-bottom: 0; }
.pbar .chip { width: 14px; height: 14px; border-radius: 50%; flex: none; border: 2px solid #a9c4e8; background: var(--slate); }
.pbar .chip.w { background: var(--ivory); border-color: #3a3122; box-shadow: 0 0 0 1px #8a7a58; }
.pbar .pname { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 38vw; }
.pbar .off { font-size: 11.5px; color: var(--mute); border: 1px solid var(--line); border-radius: 6px; padding: 0 5px; }
.pbar .caps { flex: 1; display: flex; flex-wrap: wrap; align-items: center; min-width: 0; height: 24px; overflow: hidden; }
.pbar .caps i { width: 18px; height: 20px; margin-right: -5px; }
.pbar .caps em { font-style: normal; color: var(--mute); font-size: 12px; margin-left: 9px; }
.pbar .clock { font: 600 15px/1 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; padding: 6px 9px; border-radius: 8px; background: var(--panel); border: 1px solid var(--line); color: var(--mute); white-space: nowrap; }
.pbar.turn .clock { color: var(--gold-ink); background: var(--gold); border-color: var(--gold); }
.pbar.turn .pname::after { content: ""; display: inline-block; width: 7px; height: 7px; border-radius: 50%; background: var(--gold); margin-left: 7px; vertical-align: middle; }
.pbar .clock.low { background: var(--red); border-color: var(--red); color: #2a0703; }

#board { flex: 1; min-height: 0; position: relative; display: flex; }
#view3d { flex: 1.25; min-width: 0; position: relative; }
#view3d canvas { display: block; width: 100%; height: 100%; touch-action: none; cursor: grab; outline: none; }
#slices { flex: 1; min-width: 0; display: grid; gap: 6px 10px; padding: 8px 10px; align-content: center; justify-content: center; grid-template-columns: repeat(var(--cols, 2), max-content); border-left: 1px solid var(--line); background: rgba(10, 13, 19, .45); }
.tabs { position: absolute; top: 8px; left: 8px; z-index: 5; display: none; background: rgba(21, 27, 37, .92); }
#levels { position: absolute; right: 6px; top: 50%; transform: translateY(-50%); z-index: 4; display: flex; flex-direction: column; gap: 3px; }
#levels button { width: 34px; height: 30px; border-radius: 8px; border: 1px solid var(--line); background: rgba(21, 27, 37, .88); color: var(--mute); font-weight: 700; font-size: 13px; padding: 0; position: relative; }
#levels button.on { background: var(--gold); color: var(--gold-ink); border-color: var(--gold); }
#levels button.all { font-size: 11px; height: 26px; }
#levels button b { position: absolute; top: -4px; left: -5px; min-width: 15px; height: 15px; border-radius: 8px; background: var(--green); color: #05241a; font-size: 10px; line-height: 15px; padding: 0 3px; }
#levels button b.cap { background: var(--red); color: #2a0703; }
#hover-label { position: absolute; left: 50%; bottom: 8px; transform: translateX(-50%); background: rgba(14, 18, 24, .9); border: 1px solid var(--line); border-radius: 8px; padding: 3px 9px; font-size: 13px; pointer-events: none; z-index: 4; white-space: nowrap; }
#no3d { position: absolute; inset: 0; display: grid; place-items: center; text-align: center; padding: 30px; color: var(--mute); }
@media (max-width: 899px) {
  .tabs { display: flex; }
  #board[data-tab="3d"] #slices { display: none; }
  #board[data-tab="layers"] #view3d, #board[data-tab="layers"] #levels, #board[data-tab="layers"] #hover-label { display: none; }
  #slices { border-left: 0; padding-top: 50px; }
}
@media (min-width: 900px) { #hover-label { left: 27.8%; } #levels { right: calc(44.4% + 6px); } #slices { flex: 0 0 44.4%; } #view3d { flex: 1; } }

.slice-head { display: flex; align-items: baseline; gap: 6px; font-size: 11.5px; font-weight: 700; letter-spacing: .8px; text-transform: uppercase; color: var(--mute); height: 16px; }
.slice-head b { color: var(--green); font-weight: 700; letter-spacing: 0; text-transform: none; }
.slice.focus .slice-head { color: var(--gold); }
.grid { display: grid; grid-template-columns: repeat(8, var(--cell)); grid-template-rows: repeat(8, var(--cell)); border: 1px solid #3a475d; border-radius: 4px; overflow: hidden; }
.slice.focus .grid { border-color: var(--gold); }
.cell { position: relative; border: 0; padding: 0; margin: 0; width: var(--cell); height: var(--cell); background: #3b475c; display: block; }
.cell.d { background: #242d3c; }
.cell i, .pc { display: block; width: 100%; height: 100%; background: center / 96% no-repeat; pointer-events: none; }
.cell i { position: relative; z-index: 1; }
.cell.last { box-shadow: inset 0 0 0 100px rgba(106, 169, 255, .34); }
.cell.thr::before { content: ""; position: absolute; inset: 0; background: rgba(217, 72, 95, .3); }
.cell.chk { box-shadow: inset 0 0 0 100px rgba(255, 61, 61, .55); }
.cell.sel { box-shadow: inset 0 0 0 100px rgba(242, 193, 78, .5); }
.cell.tm::after, .cell.tc::after { content: ""; position: absolute; pointer-events: none; z-index: 2; }
.cell.tm::after { inset: 31%; border-radius: 50%; background: var(--green); box-shadow: 0 0 0 1.5px rgba(0, 0, 0, .55); }
.cell.tc::after { inset: 1px; border-radius: 50%; border: 2.5px solid var(--red); }
.cell.pk.tm::after { background: var(--orange); }
.cell.gfrom i { opacity: .28; }
.cell.gto { box-shadow: inset 0 0 0 100px rgba(242, 193, 78, .42); outline: 2px solid var(--gold); outline-offset: -2px; z-index: 1; }
.cell.gto i { animation: bob .9s ease-in-out infinite; }
@keyframes bob { 50% { transform: scale(.86); } }

#slices .strip { display: contents; }
#slices .slice.big { display: none; }
#slices.focus-mode { display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 14px; }
#slices.focus-mode .strip { display: flex; gap: 5px; }
#slices.focus-mode .strip .slice { --cell: var(--mini); cursor: pointer; padding: 3px 0 0; border-radius: 6px; }
#slices.focus-mode .strip .slice-head { justify-content: center; gap: 3px; font-size: 11px; height: 15px; letter-spacing: 0; }
#slices.focus-mode .strip .grid { border-radius: 2px; }
#slices.focus-mode .strip .cell { pointer-events: none; }
#slices.focus-mode .strip .cell i, #slices.focus-mode .strip .cell::after, #slices.focus-mode .strip .cell::before { display: none; }
#slices.focus-mode .strip .cell.gto { outline: 0; }
#slices.focus-mode .strip .cell.pw { background: var(--ivory); }
#slices.focus-mode .strip .cell.pb { background: #7397cc; }
#slices.focus-mode .strip .cell.sel, #slices.focus-mode .strip .cell.gto { background: var(--gold); box-shadow: none; }
#slices.focus-mode .strip .cell.tm { background: var(--green); }
#slices.focus-mode .strip .cell.tm.pk { background: var(--orange); }
#slices.focus-mode .strip .cell.tc { background: var(--red); }
#slices.focus-mode .slice.big { display: block; --cell: var(--big); }
#slices.focus-mode .slice.big .slice-head { font-size: 13px; height: 22px; }

#notice { display: flex; align-items: center; gap: 10px; padding: 8px 12px; background: #2a2413; border-top: 1px solid #6b5722; font-weight: 600; }
#notice span { flex: 1; }
#action { display: flex; align-items: center; gap: 10px; padding: 8px 10px; background: var(--panel); border-top: 1px solid var(--line); min-height: 58px; }
#action .nav { display: flex; gap: 4px; }
#status { flex: 1; min-width: 0; font-weight: 600; line-height: 1.25; }
#status small { display: block; color: var(--mute); font-weight: 500; font-size: 12.5px; }
#action-btns { display: flex; gap: 8px; }
#tools { display: flex; gap: 6px; padding: 6px 10px 8px; background: var(--panel); overflow-x: auto; scrollbar-width: none; }
#tools::-webkit-scrollbar { display: none; }
#tools .btn { flex: 1; }
#conn { position: fixed; top: calc(6px + env(safe-area-inset-top)); left: 50%; transform: translateX(-50%); z-index: 50; background: #5c2328; border: 1px solid #a2434b; border-radius: 20px; padding: 4px 14px; font-size: 13px; font-weight: 600; }

/* ---------------------------------------------------------------- modal, toast */
#modal { position: fixed; inset: 0; z-index: 40; background: rgba(5, 7, 11, .66); display: flex; align-items: center; justify-content: center; padding: 14px; }
.sheet { width: min(560px, 100%); max-height: min(86dvh, 760px); background: var(--panel); border: 1px solid var(--line); border-radius: 18px; display: flex; flex-direction: column; box-shadow: 0 24px 80px rgba(0, 0, 0, .6); }
.sheet-head { display: flex; align-items: center; justify-content: space-between; padding: 14px 14px 10px 18px; }
.sheet-head h3 { font: 600 21px/1.2 var(--serif); }
#modal-body { padding: 4px 18px 14px; overflow: auto; -webkit-overflow-scrolling: touch; }
#modal-foot { display: flex; gap: 8px; justify-content: flex-end; padding: 0 18px 16px; }
#modal-foot:empty { display: none; }
#modal-body p { color: #c9d2e3; margin-bottom: 10px; }
.guide { display: grid; gap: 10px; }
.guide .row { display: grid; grid-template-columns: 52px 1fr; gap: 12px; align-items: center; background: var(--bg2); border: 1px solid var(--line); border-radius: 12px; padding: 8px 12px 8px 8px; }
.guide .pc { width: 52px; height: 52px; }
.guide b { display: block; }
.guide span { color: #c0cadd; font-size: 13.5px; }
.guide h4 { margin: 10px 0 0; font: 600 12px/1 var(--sans); letter-spacing: 1.3px; text-transform: uppercase; color: var(--mute); }
.promo { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; }
.promo button { background: var(--bg2); border: 1px solid var(--line); border-radius: 12px; padding: 8px 4px 6px; font-size: 12.5px; font-weight: 600; }
.promo button:hover { border-color: var(--gold); }
.promo .pc { width: 48px; height: 48px; margin: 0 auto 2px; }
.moves { display: grid; grid-template-columns: 34px 1fr 1fr; gap: 3px 6px; align-items: center; font: 13.5px/1.2 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; }
.moves .n { color: var(--mute); text-align: right; }
.moves button { text-align: left; background: var(--bg2); border: 1px solid transparent; border-radius: 7px; padding: 6px 8px; }
.moves button.on { border-color: var(--gold); color: var(--gold); }
.opt-row { display: flex; align-items: center; justify-content: space-between; gap: 14px; padding: 10px 0; border-bottom: 1px solid var(--line); }
.opt-row:last-child { border-bottom: 0; }
.opt-row input[type=range] { width: 150px; padding: 0; min-height: 0; accent-color: var(--gold); }
.switch { width: 46px; height: 28px; border-radius: 14px; border: 1px solid var(--line); background: var(--bg2); position: relative; padding: 0; flex: none; }
.switch::after { content: ""; position: absolute; top: 3px; left: 3px; width: 20px; height: 20px; border-radius: 50%; background: var(--mute); transition: transform .15s; }
.switch.on { background: var(--gold); border-color: var(--gold); }
.switch.on::after { transform: translateX(18px); background: var(--gold-ink); }
#toast { position: fixed; left: 50%; bottom: calc(140px + env(safe-area-inset-bottom)); transform: translateX(-50%); z-index: 60; background: #2b3649; border: 1px solid #44536e; border-radius: 12px; padding: 9px 14px; font-weight: 600; max-width: 92vw; text-align: center; box-shadow: 0 8px 30px rgba(0, 0, 0, .5); }
@media (max-width: 520px) {
  h1 { font-size: 34px; }
  #tools { gap: 5px; padding: 6px 8px 8px; }
  #tools .btn { padding: 5px 2px; font-size: 13px; min-height: 36px; flex: 1 1 0; min-width: 0; }
  #action-btns .btn { padding: 9px 12px; }
}
@media (prefers-reduced-motion: reduce) { .cell.gto i { animation: none; } }
