:root {
  --bg: #1a0f08;
  --panel: #2c1a0e;
  --panel2: #3b2414;
  --text: #fbefdc;
  --muted: #c9ab89;
  --accent: #f0a63a;
  --accent2: #ffcf6e;
  --danger: #e2523f;
  --radius: 14px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

html, body {
  margin: 0;
  height: 100%;
  background: radial-gradient(circle at 50% 20%, #3a2212 0%, var(--bg) 70%);
  color: var(--text);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  overflow: hidden;
  overscroll-behavior: none;
  user-select: none;
  -webkit-user-select: none;
}

button { font: inherit; color: inherit; }
.hidden { display: none !important; }

.screen {
  position: fixed;
  inset: 0;
  display: none;
  padding: calc(10px + var(--safe-top)) 10px calc(10px + var(--safe-bottom));
}
.screen.active { display: flex; }

/* ---------- Menu ---------- */
#menu, #setup { align-items: center; justify-content: center; overflow-y: auto; }

.menu-card {
  width: min(420px, 100%);
  margin: auto;
  background: linear-gradient(180deg, var(--panel2), var(--panel));
  border: 1px solid rgba(255, 200, 140, 0.15);
  border-radius: 22px;
  padding: 26px 22px 20px;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.5);
  display: flex;
  flex-direction: column;
  gap: 12px;
  text-align: center;
}

h1 { margin: 0; font-size: 2.6rem; letter-spacing: 0.06em; color: var(--accent2); text-shadow: 0 3px 0 #6b3a12; }
h2 { margin: 0; color: var(--accent2); }
.tag { margin: 0 0 6px; color: var(--muted); font-size: 0.95rem; }

.logo { display: flex; justify-content: center; gap: 6px; margin-bottom: -4px; }
.lc { width: 30px; height: 30px; border-radius: 50%; box-shadow: inset -3px -4px 6px rgba(0,0,0,.35), 0 3px 6px rgba(0,0,0,.4); }
.lc.white { background: radial-gradient(circle at 35% 35%, #fffdf6, #e6d5ac 60%, #b19865); }
.lc.black { background: radial-gradient(circle at 35% 35%, #777, #222 60%, #000); }
.lc.queen { background: radial-gradient(circle at 35% 35%, #ff8a80, #d92b2b 60%, #6e0b0b); transform: translateY(-6px); }

.group { display: flex; flex-direction: column; gap: 8px; background: rgba(0,0,0,0.18); padding: 12px; border-radius: var(--radius); }
.label { color: var(--muted); font-size: 0.85rem; text-transform: uppercase; letter-spacing: 0.08em; }

.btn {
  border: 0;
  border-radius: var(--radius);
  padding: 14px 16px;
  font-size: 1.05rem;
  font-weight: 600;
  background: #4d2f1a;
  box-shadow: 0 3px 0 #2a170b;
  cursor: pointer;
  transition: transform 0.06s, filter 0.15s;
}
.btn:hover { filter: brightness(1.12); }
.btn:active { transform: translateY(2px); box-shadow: 0 1px 0 #2a170b; }
.btn.primary { background: linear-gradient(180deg, var(--accent2), var(--accent)); color: #3a1d06; box-shadow: 0 3px 0 #9c5c12; }
.btn.small { padding: 10px 12px; font-size: 0.95rem; flex: 1; }
.btn.danger { background: var(--danger); box-shadow: 0 3px 0 #7d2316; }
.btn.install { background: #2f5d3a; box-shadow: 0 3px 0 #173020; }
.btn small { font-weight: 400; opacity: 0.75; }
.row { display: flex; gap: 10px; }

.seg { display: flex; background: rgba(0,0,0,0.3); border-radius: 12px; padding: 4px; gap: 4px; }
.seg button {
  flex: 1; border: 0; background: transparent; padding: 9px 6px; border-radius: 9px; cursor: pointer; font-weight: 600; color: var(--muted);
}
.seg button.on { background: var(--accent); color: #3a1d06; }

.stats { margin: 4px 0 0; color: var(--muted); font-size: 0.85rem; min-height: 1em; }

.player-rows { display: flex; flex-direction: column; gap: 8px; text-align: left; }
.prow { display: grid; grid-template-columns: 1fr auto; gap: 8px; align-items: center; background: rgba(0,0,0,0.2); padding: 8px 10px; border-radius: 12px; }
.prow .seatlbl { grid-column: 1 / -1; font-size: 0.78rem; color: var(--muted); }
.prow input, .prow select {
  width: 100%; padding: 9px 10px; border-radius: 9px; border: 1px solid rgba(255,255,255,0.12);
  background: #1d120a; color: var(--text); font: inherit;
}
.prow select { width: auto; }

/* ---------- Game ---------- */
#game { flex-direction: column; align-items: stretch; gap: 6px; padding-left: max(6px, env(safe-area-inset-left)); padding-right: max(6px, env(safe-area-inset-right)); }

.hud {
  display: grid;
  grid-template-columns: auto 1fr auto 1fr;
  gap: 6px;
  align-items: stretch;
  width: min(100%, 860px);
  margin: 0 auto;
}
.icon-btn {
  border: 0; background: var(--panel2); border-radius: 12px; font-size: 1.3rem; width: 44px; cursor: pointer;
}
.team {
  background: var(--panel);
  border-radius: 12px;
  padding: 6px 8px;
  border: 2px solid transparent;
  min-width: 0;
  transition: border-color 0.2s, background 0.2s;
}
.team.active { border-color: var(--accent2); background: var(--panel2); box-shadow: 0 0 14px rgba(255,207,110,0.25); }
.team .tname { display: flex; align-items: center; gap: 6px; font-weight: 700; font-size: 0.9rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.team .chip { width: 14px; height: 14px; border-radius: 50%; flex: none; }
.chip.white { background: radial-gradient(circle at 35% 35%, #fff, #d9c79c); }
.chip.black { background: radial-gradient(circle at 35% 35%, #777, #000); border: 1px solid #555; }
.team .tinfo { display: flex; justify-content: space-between; font-size: 0.78rem; color: var(--muted); margin-top: 2px; gap: 6px; }
.team .tinfo b { color: var(--text); }
.tray { display: flex; flex-wrap: wrap; gap: 2px; margin-top: 3px; min-height: 9px; }
.tray i { width: 9px; height: 9px; border-radius: 50%; display: block; }
.tray i.white { background: #efe2c2; }
.tray i.black { background: #111; border: 1px solid #666; }
.tray i.queen { background: #d92b2b; box-shadow: 0 0 4px #ff6b6b; }
.tray i.queen.pending { opacity: 0.5; }
.status {
  display: flex; flex-direction: column; justify-content: center; align-items: center;
  font-size: 0.75rem; color: var(--muted); text-align: center; padding: 0 4px; min-width: 52px;
}
.status b { color: var(--accent2); font-size: 0.85rem; }

.stage {
  position: relative;
  flex: 1;
  min-height: 0;
  display: flex;
  align-items: center;
  justify-content: center;
}
#cv { touch-action: none; display: block; border-radius: 18px; box-shadow: 0 16px 50px rgba(0,0,0,0.55); }

.toast {
  position: absolute; left: 50%; top: 18%; transform: translate(-50%, -10px);
  background: rgba(20, 10, 4, 0.88); color: var(--text); padding: 10px 16px; border-radius: 12px;
  font-weight: 600; text-align: center; pointer-events: none; opacity: 0; transition: opacity 0.25s, transform 0.25s;
  max-width: 86%; line-height: 1.35; border: 1px solid rgba(255,200,140,0.2);
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }
.toast.bad { border-color: var(--danger); color: #ffd0c8; }

.banner {
  position: absolute; left: 50%; top: 50%; transform: translate(-50%, -50%) scale(0.9);
  background: linear-gradient(180deg, var(--accent2), var(--accent)); color: #3a1d06; font-weight: 800; font-size: 1.3rem;
  padding: 12px 22px; border-radius: 14px; pointer-events: none; opacity: 0; transition: opacity 0.25s, transform 0.25s;
  box-shadow: 0 10px 30px rgba(0,0,0,0.5); white-space: nowrap;
}
.banner.show { opacity: 1; transform: translate(-50%, -50%) scale(1); }

.controls { width: min(100%, 560px); margin: 0 auto; display: flex; flex-direction: column; gap: 4px; align-items: stretch; }
.hint { text-align: center; color: var(--muted); font-size: 0.8rem; min-height: 1.2em; }

input[type=range] { -webkit-appearance: none; appearance: none; width: 100%; height: 34px; background: transparent; touch-action: none; }
input[type=range]::-webkit-slider-runnable-track { height: 10px; border-radius: 6px; background: linear-gradient(90deg, #6b3a18, #a8682c, #6b3a18); }
input[type=range]::-moz-range-track { height: 10px; border-radius: 6px; background: linear-gradient(90deg, #6b3a18, #a8682c, #6b3a18); }
input[type=range]::-webkit-slider-thumb {
  -webkit-appearance: none; width: 30px; height: 30px; margin-top: -10px; border-radius: 50%;
  background: radial-gradient(circle at 35% 35%, #fff, #c9d3df 60%, #7d8ea3); border: 3px solid #2e5aa8; box-shadow: 0 2px 6px rgba(0,0,0,0.5);
}
input[type=range]::-moz-range-thumb {
  width: 26px; height: 26px; border-radius: 50%;
  background: radial-gradient(circle at 35% 35%, #fff, #c9d3df 60%, #7d8ea3); border: 3px solid #2e5aa8;
}
input[type=range]:disabled { opacity: 0.35; }

/* ---------- Modal ---------- */
.modal {
  position: fixed; inset: 0; background: rgba(0,0,0,0.6); display: flex; align-items: center; justify-content: center;
  z-index: 10; padding: 16px; backdrop-filter: blur(3px);
}
.modal-card {
  width: min(440px, 100%); max-height: 88vh; overflow-y: auto; background: linear-gradient(180deg, var(--panel2), var(--panel));
  border-radius: 20px; padding: 20px; border: 1px solid rgba(255,200,140,0.18); box-shadow: 0 20px 60px rgba(0,0,0,0.6);
  user-select: text; -webkit-user-select: text;
}
.modal-card h2 { margin-top: 0; text-align: center; }
.modal-card p, .modal-card li { color: #ecdcc4; line-height: 1.5; }
.modal-card ul { padding-left: 1.2em; }
.modal-buttons { display: flex; flex-direction: column; gap: 8px; margin-top: 14px; }
.big-score { display: flex; justify-content: center; gap: 22px; font-size: 1.1rem; margin: 10px 0; }
.big-score div { text-align: center; }
.big-score b { display: block; font-size: 2rem; color: var(--accent2); }

.toggle { display: flex; justify-content: space-between; align-items: center; padding: 10px 2px; border-bottom: 1px solid rgba(255,255,255,0.07); }
.toggle input { width: 22px; height: 22px; accent-color: var(--accent); }

@media (orientation: landscape) and (max-height: 520px) {
  #game.active { display: grid; grid-template-columns: 220px 1fr; grid-template-rows: 1fr auto; gap: 8px; }
  .hud { grid-column: 1; grid-row: 1 / span 2; grid-template-columns: 1fr; align-content: start; width: auto; }
  .icon-btn { width: 100%; height: 40px; }
  .stage { grid-column: 2; grid-row: 1; }
  .controls { grid-column: 2; grid-row: 2; }
}
