:root {
  --bg: #0f1219;
  --bg-elev: #171b26;
  --bg-panel: #1c2230;
  --line: #2a3347;
  --text: #e8ecf5;
  --muted: #9aa3b8;
  --red: #e85d4c;
  --red-dim: #8a3228;
  --blue: #4c8fe8;
  --blue-dim: #284e8a;
  --accent: #f0c14b;
  --ok: #3ecf8e;
  --danger: #ff6b6b;
  --radius: 14px;
  --font: "Noto Sans SC", "Space Grotesk", system-ui, sans-serif;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --app-h: 100dvh;
}

* { box-sizing: border-box; }
html, body {
  margin: 0;
  height: var(--app-h);
  max-height: var(--app-h);
  overflow: hidden;
  background:
    radial-gradient(1200px 600px at 10% -10%, #243056 0%, transparent 55%),
    radial-gradient(900px 500px at 100% 0%, #3a1f2e 0%, transparent 50%),
    var(--bg);
  color: var(--text);
  font-family: var(--font);
}

body {
  display: flex;
  flex-direction: column;
}

.app-shell {
  display: flex;
  flex-direction: column;
  height: var(--app-h);
  max-height: var(--app-h);
  overflow: hidden;
}
.app-shell.hidden { display: none !important; }

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.5rem;
  padding: 0.55rem 0.85rem;
  padding-top: calc(0.55rem + var(--safe-top));
  border-bottom: 1px solid var(--line);
  background: rgba(15, 18, 25, 0.92);
  backdrop-filter: blur(10px);
  flex-shrink: 0;
  z-index: 10;
}

.brand { display: flex; align-items: center; gap: 0.55rem; min-width: 0; }
.brand-mark {
  width: 28px; height: 28px; border-radius: 8px; flex-shrink: 0;
  background:
    linear-gradient(135deg, var(--red) 50%, transparent 50%),
    linear-gradient(315deg, var(--blue) 50%, #2a3347 50%);
  box-shadow: 0 0 0 2px var(--line);
}
.brand h1 { margin: 0; font-size: 0.98rem; letter-spacing: 0.02em; white-space: nowrap; }
.subtitle { margin: 0; color: var(--muted); font-size: 0.68rem; white-space: nowrap; }

.lang-label {
  display: flex; align-items: center; gap: 0.35rem;
  font-size: 0.78rem; color: var(--muted);
}
.lang-label select {
  background: var(--bg-elev);
  color: var(--text);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 0.28rem 0.4rem;
  font: inherit;
}
.topbar-right {
  display: flex;
  align-items: center;
  gap: 0.4rem;
  flex-shrink: 0;
}

main {
  flex: 1;
  min-height: 0;
  padding: 0.65rem 0.75rem;
  padding-bottom: calc(0.65rem + var(--safe-bottom));
  max-width: 1280px;
  width: 100%;
  margin: 0 auto;
  overflow: hidden;
  display: flex;
  flex-direction: column;
}

.panel {
  flex: 1;
  min-height: 0;
  display: grid;
  grid-template-columns: minmax(0, 1fr) 280px;
  gap: 0.75rem;
  align-items: stretch;
  overflow: hidden;
}

.hidden { display: none !important; }

.lobby-card, .game-main, .rules-panel {
  background: var(--bg-panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 0.85rem;
  min-height: 0;
  overflow: hidden;
}

.lobby-card {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
  overflow: auto;
}
.lobby-card h2 { margin: 0; font-size: 1.05rem; }
.muted { color: var(--muted); font-size: 0.88rem; line-height: 1.4; }
.lobby-actions {
  display: flex; flex-direction: column; gap: 0.55rem;
  margin-top: 0.35rem;
}
.join-row { display: flex; gap: 0.4rem; }
.join-row input, .share-row input {
  flex: 1;
  min-width: 0;
  background: var(--bg);
  border: 1px solid var(--line);
  border-radius: 10px;
  color: var(--text);
  padding: 0.55rem 0.7rem;
  font: inherit;
}

.btn {
  appearance: none;
  border: 1px solid var(--line);
  background: var(--bg-elev);
  color: var(--text);
  border-radius: 10px;
  padding: 0.55rem 0.85rem;
  font: inherit;
  font-weight: 500;
  cursor: pointer;
  transition: transform 0.12s ease, border-color 0.12s ease, background 0.12s ease;
}
.btn:hover { border-color: #4a5878; transform: translateY(-1px); }
.btn:active { transform: translateY(0); }
.btn.primary {
  background: linear-gradient(135deg, #3d6fd6, #2a4fa8);
  border-color: #5b86e0;
}
.btn.danger {
  background: #3a2228;
  border-color: #7a3a45;
  color: #ffb4b4;
}
.btn.ghost { background: transparent; }
.btn.small { padding: 0.32rem 0.55rem; font-size: 0.78rem; }

.share-box {
  margin-top: 0.35rem;
  padding-top: 0.65rem;
  border-top: 1px dashed var(--line);
}
.share-row { display: flex; gap: 0.4rem; margin: 0.4rem 0 0.55rem; }

.error {
  color: #ff8f8f;
  background: #3a2228;
  border: 1px solid #7a3a45;
  border-radius: 10px;
  padding: 0.45rem 0.65rem;
  margin-top: 0.35rem;
  font-size: 0.85rem;
}

.rules-panel {
  overflow: auto;
}
.rules-details summary {
  cursor: pointer;
  font-size: 0.95rem;
  color: var(--accent);
  font-weight: 700;
  list-style: none;
}
.rules-details summary::-webkit-details-marker { display: none; }
.rules-details summary::before {
  content: "▸ ";
  color: var(--muted);
}
.rules-details[open] summary::before { content: "▾ "; }
.rules-details ol {
  margin: 0.55rem 0 0;
  padding-left: 1.05rem;
  color: var(--muted);
  font-size: 0.82rem;
  line-height: 1.45;
}
.rules-details li { margin-bottom: 0.4rem; }
.rules-details strong { color: var(--text); font-weight: 600; }

.game-main {
  display: flex;
  flex-direction: column;
  gap: 0.4rem;
  overflow: hidden;
}

.status-bar {
  display: flex;
  flex-wrap: wrap;
  gap: 0.35rem;
  align-items: center;
  flex-shrink: 0;
}
.you-badge, .turn-badge, .seats-badge {
  border-radius: 999px;
  padding: 0.28rem 0.6rem;
  font-size: 0.75rem;
  border: 1px solid var(--line);
  background: var(--bg);
}
.you-badge.red { background: var(--red-dim); border-color: var(--red); }
.you-badge.blue { background: var(--blue-dim); border-color: var(--blue); }
.turn-badge.active { border-color: var(--accent); color: var(--accent); }

.board-wrap {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.25rem;
  flex: 1 1 auto;
  min-height: 0;
  justify-content: center;
}
#board {
  width: min(100%, 560px);
  max-height: 100%;
  aspect-ratio: 1;
  height: auto;
  border-radius: 12px;
  border: 1px solid var(--line);
  background: #12161f;
  cursor: crosshair;
  touch-action: none;
}
.start-legend {
  display: flex; align-items: center; gap: 0.4rem;
  color: var(--muted); font-size: 0.72rem;
  flex-shrink: 0;
}
.dot {
  width: 9px; height: 9px; border-radius: 2px; display: inline-block;
}
.dot.red { background: var(--red); }
.dot.blue { background: var(--blue); }

.tray {
  flex-shrink: 0;
  max-height: 28%;
  display: flex;
  flex-direction: column;
  min-height: 0;
}
.tray-header {
  display: flex; justify-content: space-between; align-items: center;
  margin-bottom: 0.3rem;
  flex-shrink: 0;
}
.tray-header h3 { margin: 0; font-size: 0.85rem; }
.pieces {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(56px, 1fr));
  gap: 0.3rem;
  overflow: auto;
  padding: 0.15rem;
  min-height: 0;
  flex: 1;
}
.piece {
  background: var(--bg);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 0.25rem;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 48px;
}
.piece:hover { border-color: #5a6a90; }
.piece.selected {
  border-color: var(--accent);
  box-shadow: 0 0 0 1px var(--accent);
}
.piece.used {
  opacity: 0.35;
  pointer-events: none;
  filter: grayscale(0.4);
}
.piece canvas { pointer-events: none; max-width: 100%; }
.hint { font-size: 0.72rem; margin: 0.25rem 0 0; flex-shrink: 0; }

.result {
  margin-top: 0.25rem;
  padding: 0.65rem;
  border-radius: 12px;
  border: 1px solid var(--accent);
  background: #2a2414;
  text-align: center;
  font-size: 0.95rem;
  flex-shrink: 0;
}

/* —— Full-screen tutorial —— */
.tutorial {
  position: fixed;
  inset: 0;
  z-index: 100;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  height: var(--app-h);
  max-height: var(--app-h);
  color: var(--text);
}
.tutorial.hidden { display: none !important; }

.tutorial-bg {
  position: absolute;
  inset: 0;
  background:
    radial-gradient(900px 500px at 15% 20%, rgba(232, 93, 76, 0.22) 0%, transparent 55%),
    radial-gradient(800px 480px at 85% 75%, rgba(76, 143, 232, 0.22) 0%, transparent 55%),
    radial-gradient(1200px 700px at 50% 100%, #1a2030 0%, transparent 60%),
    #0b0e14;
  pointer-events: none;
}

.tutorial-shell {
  position: relative;
  z-index: 1;
  display: flex;
  flex-direction: column;
  height: 100%;
  max-height: 100%;
  max-width: 1180px;
  width: 100%;
  margin: 0 auto;
  padding: 0.55rem 0.75rem;
  padding-top: calc(0.55rem + var(--safe-top));
  padding-bottom: calc(0.55rem + var(--safe-bottom));
  overflow: hidden;
  gap: 0.4rem;
}

.tutorial-top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.5rem;
  flex-shrink: 0;
}
.tutorial-brand {
  display: flex;
  align-items: center;
  gap: 0.45rem;
  font-weight: 700;
  font-size: 0.88rem;
  letter-spacing: 0.02em;
  min-width: 0;
}
.tutorial-brand span:last-child {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.tutorial-top-right {
  display: flex;
  align-items: center;
  gap: 0.4rem;
  flex-shrink: 0;
}

.tutorial-progress {
  display: flex;
  gap: 0.3rem;
  flex-shrink: 0;
}
.tutorial-progress .dot-step {
  flex: 1;
  height: 3px;
  border-radius: 999px;
  background: #2a3347;
  transition: background 0.35s ease, transform 0.35s ease;
}
.tutorial-progress .dot-step.done { background: #5a6a90; }
.tutorial-progress .dot-step.active {
  background: linear-gradient(90deg, var(--red), var(--blue));
  transform: scaleY(1.5);
}

.tutorial-body {
  flex: 1;
  min-height: 0;
  display: grid;
  grid-template-columns: minmax(240px, 1fr) minmax(260px, 1.05fr);
  gap: 0.75rem;
  align-items: stretch;
  overflow: hidden;
}

.tutorial-copy {
  background: rgba(28, 34, 48, 0.78);
  border: 1px solid var(--line);
  border-radius: 14px;
  padding: 0.85rem 0.95rem;
  backdrop-filter: blur(12px);
  animation: tut-fade-in 0.35s ease;
  display: flex;
  flex-direction: column;
  min-height: 0;
  overflow: hidden;
}
.tutorial-kicker {
  margin: 0 0 0.25rem;
  font-size: 0.68rem;
  letter-spacing: 0.1em;
  text-transform: uppercase;
  color: var(--accent);
  font-weight: 700;
  flex-shrink: 0;
}
.tutorial-copy h2 {
  margin: 0 0 0.4rem;
  font-size: clamp(1rem, 2.2vw, 1.45rem);
  line-height: 1.25;
  flex-shrink: 0;
}
.tutorial-text {
  margin: 0 0 0.45rem;
  color: #c5cce0;
  font-size: 0.88rem;
  line-height: 1.45;
  overflow: auto;
  min-height: 0;
}
.tutorial-bullets {
  margin: 0 0 0.55rem;
  padding-left: 1rem;
  color: var(--muted);
  font-size: 0.8rem;
  line-height: 1.4;
  overflow: auto;
  min-height: 0;
  flex: 1;
}
.tutorial-bullets li { margin-bottom: 0.3rem; }
.tutorial-bullets strong { color: var(--text); }
.tutorial-nav {
  display: flex;
  gap: 0.45rem;
  flex-wrap: wrap;
  flex-shrink: 0;
  margin-top: auto;
  padding-top: 0.35rem;
  align-items: flex-end;
}
.tutorial-nav .btn { padding: 0.5rem 0.85rem; font-size: 0.88rem; }
.tutorial-nav .btn.primary { min-width: 110px; }
.tutorial-next-wrap {
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  gap: 0.25rem;
  margin-left: auto;
}
.tutorial-next-hint {
  margin: 0;
  font-size: 0.68rem;
  color: var(--accent);
  max-width: 180px;
  text-align: right;
  line-height: 1.3;
}
.tutorial-next-hint[hidden] { display: none !important; }
#btn-tutorial-next:disabled,
#btn-tutorial-next.locked {
  opacity: 0.35;
  cursor: not-allowed;
  transform: none !important;
  filter: grayscale(0.4);
  pointer-events: none;
  box-shadow: none;
}

.tutorial-stage {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.4rem;
  min-height: 0;
  overflow: hidden;
}
.tutorial-stage-frame {
  position: relative;
  background: rgba(18, 22, 31, 0.9);
  border: 1px solid var(--line);
  border-radius: 14px;
  padding: 0.45rem;
  box-shadow: 0 12px 40px rgba(0, 0, 0, 0.35);
  flex: 1 1 auto;
  min-height: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100%;
  max-width: 420px;
}
#tutorial-board {
  display: block;
  width: min(100%, 380px);
  max-height: 100%;
  aspect-ratio: 1;
  height: auto;
  border-radius: 8px;
}
#tutorial-board.awaiting-place {
  cursor: pointer;
  outline: 2px dashed rgba(240, 193, 75, 0.55);
  outline-offset: 2px;
}
.tutorial-callout {
  position: absolute;
  left: 50%;
  bottom: 0.55rem;
  transform: translateX(-50%);
  background: rgba(15, 18, 25, 0.92);
  border: 1px solid var(--accent);
  color: var(--accent);
  padding: 0.28rem 0.55rem;
  border-radius: 999px;
  font-size: 0.7rem;
  white-space: nowrap;
  max-width: 92%;
  overflow: hidden;
  text-overflow: ellipsis;
  pointer-events: none;
  animation: tut-pulse 1.6s ease infinite;
  z-index: 2;
}
.tutorial-callout.bad {
  border-color: var(--danger);
  color: #ffb4b4;
}

.coach-pointer {
  position: absolute;
  z-index: 3;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.15rem;
  pointer-events: none;
  animation: coach-bounce 1.1s ease-in-out infinite;
}
.coach-pointer.hidden { display: none !important; }
.coach-finger {
  font-size: 1.35rem;
  line-height: 1;
  filter: drop-shadow(0 2px 4px rgba(0,0,0,0.5));
}
.coach-label {
  background: rgba(15, 18, 25, 0.95);
  border: 1px solid var(--accent);
  color: var(--accent);
  font-size: 0.65rem;
  font-weight: 700;
  padding: 0.2rem 0.45rem;
  border-radius: 999px;
  white-space: nowrap;
  max-width: 140px;
  overflow: hidden;
  text-overflow: ellipsis;
}
.board-pointer {
  right: 8%;
  top: 12%;
}
.tray-pointer {
  position: absolute;
  left: 50%;
  top: -0.15rem;
  transform: translateX(-50%);
  z-index: 4;
}

.tutorial-tray-wrap {
  position: relative;
  width: 100%;
  max-width: 420px;
  flex-shrink: 0;
}
.tutorial-tray-wrap.hidden { display: none !important; }
.tutorial-tray-wrap.needs-select {
  box-shadow: 0 0 0 2px rgba(240, 193, 75, 0.45);
  border-radius: 12px;
}

.tutorial-tray {
  width: 100%;
  display: grid;
  grid-template-columns: repeat(7, 1fr);
  gap: 0.25rem;
  max-height: 22%;
  overflow: auto;
  padding: 0.35rem 0.2rem 0.2rem;
  background: rgba(18, 22, 31, 0.7);
  border: 1px solid var(--line);
  border-radius: 10px;
}
.tutorial-tray .piece {
  min-height: 32px;
  padding: 0.12rem;
  cursor: default;
  position: relative;
}
.tutorial-tray .piece.selectable {
  cursor: pointer;
  border-color: rgba(240, 193, 75, 0.65);
  animation: piece-glow 1.2s ease-in-out infinite;
}
.tutorial-tray .piece.current,
.tutorial-tray .piece.selected {
  border-color: var(--accent);
  box-shadow: 0 0 0 2px var(--accent);
  animation: none;
}
.tutorial-tray .piece.used {
  opacity: 0.28;
  filter: grayscale(0.5);
  pointer-events: none;
}
.tutorial-tray .piece.dimmed {
  opacity: 0.4;
  pointer-events: none;
}
.tutorial-tray-hint {
  grid-column: 1 / -1;
  text-align: center;
  font-size: 0.68rem;
  color: var(--muted);
  padding: 0.1rem 0;
}

@keyframes coach-bounce {
  0%, 100% { transform: translateY(0); }
  50% { transform: translateY(5px); }
}
.tray-pointer {
  animation: coach-bounce-up 1.1s ease-in-out infinite;
}
@keyframes coach-bounce-up {
  0%, 100% { transform: translateX(-50%) translateY(0); }
  50% { transform: translateX(-50%) translateY(-5px); }
}
@keyframes piece-glow {
  0%, 100% { box-shadow: 0 0 0 0 rgba(240, 193, 75, 0.35); }
  50% { box-shadow: 0 0 0 3px rgba(240, 193, 75, 0.55); }
}

.tutorial-move-card {
  width: 100%;
  max-width: 420px;
  display: flex;
  align-items: center;
  gap: 0.65rem;
  padding: 0.45rem 0.65rem;
  background: rgba(28, 34, 48, 0.85);
  border: 1px solid var(--line);
  border-radius: 12px;
  flex-shrink: 0;
}
.tutorial-move-card.hidden { display: none !important; }
.tutorial-move-card.red { border-color: rgba(232, 93, 76, 0.55); }
.tutorial-move-card.blue { border-color: rgba(76, 143, 232, 0.55); }
.tutorial-move-piece {
  width: 52px;
  height: 52px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--bg);
  border-radius: 8px;
  border: 1px solid var(--line);
  flex-shrink: 0;
}
.tutorial-move-label {
  font-size: 0.82rem;
  font-weight: 700;
}
.tutorial-move-sub {
  font-size: 0.72rem;
  color: var(--muted);
  margin-top: 0.15rem;
  line-height: 1.35;
}

.tutorial-players {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  font-size: 0.78rem;
  flex-shrink: 0;
}
.t-player {
  display: flex;
  align-items: center;
  gap: 0.3rem;
  padding: 0.25rem 0.55rem;
  border-radius: 999px;
  border: 1px solid var(--line);
  background: var(--bg-elev);
  opacity: 0.55;
  transition: opacity 0.3s ease, border-color 0.3s ease, box-shadow 0.3s ease;
}
.t-player.active {
  opacity: 1;
  box-shadow: 0 0 0 1px currentColor;
}
.t-player.red.active { border-color: var(--red); color: #ffb4a8; }
.t-player.blue.active { border-color: var(--blue); color: #a8c8ff; }
.t-swatch {
  width: 10px; height: 10px; border-radius: 2px;
}
.t-player.red .t-swatch { background: var(--red); }
.t-player.blue .t-swatch { background: var(--blue); }
.t-vs { color: var(--muted); font-weight: 700; font-size: 0.68rem; }

@keyframes tut-fade-in {
  from { opacity: 0; transform: translateY(6px); }
  to { opacity: 1; transform: none; }
}
@keyframes tut-pulse {
  0%, 100% { opacity: 0.85; transform: translateX(-50%) translateY(0); }
  50% { opacity: 1; transform: translateX(-50%) translateY(-2px); }
}

/* Desktop rules always open-looking */
@media (min-width: 901px) {
  .rules-details summary { pointer-events: none; }
  .rules-details summary::before { content: ""; }
  .rules-details:not([open]) ol { display: block; }
}

@media (max-width: 900px) {
  .panel {
    grid-template-columns: 1fr;
    grid-template-rows: minmax(0, 1fr) auto;
  }
  .lobby .rules-panel,
  .game .rules-panel {
    max-height: 22%;
  }
  .rules-details[open] {
    max-height: 100%;
    overflow: auto;
  }
  .lobby-card { overflow: auto; }
  .game-main { overflow: hidden; }
  #board {
    width: min(100%, calc(var(--app-h) * 0.42));
  }
  .pieces {
    grid-template-columns: repeat(auto-fill, minmax(48px, 1fr));
  }
  .piece { min-height: 42px; }
  .subtitle { display: none; }
  .lang-label span { display: none; }
}

@media (max-width: 860px) {
  .tutorial-body {
    grid-template-columns: 1fr;
    grid-template-rows: minmax(0, 1fr) minmax(0, 1fr);
  }
  .tutorial-stage { order: -1; min-height: 0; }
  .tutorial-stage-frame {
    max-width: none;
    width: 100%;
    flex: 1 1 0;
  }
  #tutorial-board {
    width: min(100%, calc(var(--app-h) * 0.32));
  }
  .tutorial-copy {
    padding: 0.55rem 0.65rem;
  }
  .tutorial-copy h2 {
    font-size: 0.92rem;
    margin-bottom: 0.25rem;
  }
  .tutorial-text {
    font-size: 0.76rem;
    line-height: 1.3;
    margin-bottom: 0.3rem;
  }
  .tutorial-bullets {
    font-size: 0.7rem;
    margin-bottom: 0.35rem;
  }
  .tutorial-brand .brand-mark { width: 22px; height: 22px; }
  .tutorial-tray {
    grid-template-columns: repeat(7, 1fr);
    max-height: 28%;
  }
  .tutorial-players { display: none; }
  .tutorial.has-tray .tutorial-body {
    grid-template-rows: minmax(0, 1.15fr) minmax(0, 0.85fr);
  }
  .tutorial.has-tray #tutorial-board {
    width: min(100%, calc(var(--app-h) * 0.24));
  }
  .tutorial-move-card {
    padding: 0.35rem 0.5rem;
  }
  .tutorial-move-piece {
    width: 40px;
    height: 40px;
  }
}

@media (max-width: 420px) {
  .topbar { padding-left: 0.55rem; padding-right: 0.55rem; }
  #btn-replay-tutorial { display: none; }
  .tutorial-nav .btn { flex: 1; }
  .status-bar .seats-badge { display: none; }
  .start-legend { font-size: 0.65rem; }
}
