:root {
  --bg: #0e1320;
  --panel: #1b2336;
  --panel-2: #232d45;
  --ink: #eef2fb;
  --muted: #9aa6c4;
  --accent: #ffcb3d;
  --accent-2: #ff8a3d;
  --red: #e24a4a;
  --green: #3cb44a;
  --yellow: #f2c12e;
  --blue: #3b7dd8;
  --radius: 14px;
  --shadow: 0 10px 40px rgba(0, 0, 0, 0.45);
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  font-family: "Segoe UI", system-ui, -apple-system, sans-serif;
  background: var(--bg);
  color: var(--ink);
  overflow: hidden;
}

.screen { display: none; height: 100vh; height: 100dvh; }
.screen.active { display: flex; }

/* ---------- buttons ---------- */
.btn {
  border: none;
  border-radius: 10px;
  padding: 11px 18px;
  min-height: 44px; /* comfortable touch target */
  font-size: 15px;
  font-weight: 600;
  cursor: pointer;
  transition: transform 0.08s ease, filter 0.15s ease, opacity 0.15s;
  color: #fff;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation; /* no double-tap-to-zoom delay */
  user-select: none; /* don't select the label on a long-press */
}
.btn:active { transform: translateY(1px); }
.btn.primary {
  background: linear-gradient(135deg, var(--accent), var(--accent-2));
  color: #2a1c00;
}
.btn.primary:hover { filter: brightness(1.06); }
.btn.ghost { background: transparent; border: 1px solid rgba(255,255,255,0.25); }
.btn.big { padding: 14px 26px; font-size: 17px; width: 100%; }
.btn:disabled { opacity: 0.45; cursor: not-allowed; filter: grayscale(0.3); }

/* ---------- auth ---------- */
#auth-screen {
  align-items: center;
  justify-content: center;
  background: radial-gradient(1200px 800px at 70% -10%, #243152, var(--bg));
  position: relative;
}
.auth-card, .avatar-card {
  background: var(--panel);
  padding: 34px 32px;
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  width: 360px;
  z-index: 2;
}
.logo { margin: 0 0 4px; font-size: 30px; }
.tagline { margin: 0 0 22px; color: var(--muted); }
.tabs { display: flex; gap: 8px; margin-bottom: 18px; }
.tab {
  flex: 1; padding: 9px; min-height: 44px; border: none; border-radius: 9px;
  background: var(--panel-2); color: var(--muted); cursor: pointer; font-weight: 600;
  -webkit-tap-highlight-color: transparent; touch-action: manipulation;
}
.tab.active { background: var(--accent); color: #2a1c00; }
.tab, .diff-opt, .avatar-opt { user-select: none; }
#auth-form { display: flex; flex-direction: column; gap: 12px; }
input {
  background: var(--panel-2);
  border: 1px solid transparent;
  border-radius: 10px;
  padding: 12px 14px;
  min-height: 48px;
  color: var(--ink);
  /* 16px keeps iOS Safari from auto-zooming the page when a field is focused. */
  font-size: 16px;
}
input:focus { outline: none; border-color: var(--accent); }
.error { color: #ff8585; min-height: 18px; margin: 2px 0; font-size: 13px; }
input[hidden] { display: none; }
.bg-dice {
  position: absolute;
  font-size: 460px;
  right: -60px;
  bottom: -120px;
  opacity: 0.05;
  z-index: 1;
  user-select: none;
}

/* ---------- avatar select ---------- */
#avatar-screen { align-items: center; justify-content: center; background: radial-gradient(1200px 800px at 30% -10%, #243152, var(--bg)); }
.avatar-card { width: 460px; text-align: center; }
.avatar-card h2 { margin-top: 0; }
.muted { color: var(--muted); }
.small { font-size: 12px; }
.avatar-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 12px;
  margin: 22px 0;
}
.avatar-opt {
  background: var(--panel-2);
  border: 2px solid transparent;
  border-radius: 12px;
  padding: 14px 6px;
  min-height: 88px;
  cursor: pointer;
  transition: transform 0.1s, border-color 0.15s;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}
.avatar-opt:hover { transform: translateY(-3px); }
.avatar-opt .emoji { font-size: 42px; }
.avatar-opt .name { display: block; margin-top: 4px; font-size: 13px; color: var(--muted); }
.avatar-opt.selected { border-color: var(--accent); box-shadow: 0 0 0 3px rgba(255,203,61,0.2); }
.avatar-opt .swatch { width: 34px; height: 6px; border-radius: 4px; margin: 6px auto 0; }

.difficulty-label { margin-bottom: 10px; }
.difficulty { display: flex; gap: 10px; margin-bottom: 22px; }
.diff-opt {
  flex: 1;
  min-height: 48px;
  padding: 12px 14px;
  border: 2px solid transparent;
  border-radius: 10px;
  background: var(--panel-2);
  color: var(--muted);
  font-size: 15px;
  font-weight: 600;
  cursor: pointer;
  transition: transform 0.1s, border-color 0.15s, color 0.15s;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}
.diff-opt:active { transform: translateY(1px); }
.diff-opt.selected { border-color: var(--accent); color: var(--ink); box-shadow: 0 0 0 3px rgba(255,203,61,0.2); }

/* ---------- game ---------- */
#game-screen { flex-direction: column; }
.topbar {
  display: flex; align-items: center; gap: 14px;
  padding: 10px 18px; background: var(--panel); box-shadow: 0 2px 10px rgba(0,0,0,0.3);
  z-index: 3;
}
.brand { font-weight: 700; font-size: 18px; }
.spacer { flex: 1; }
.hud-user { color: var(--muted); }
.hud-user b { color: var(--ink); }

.game-wrap { flex: 1; display: flex; min-height: 0; min-width: 0; }
/* min-width:0 lets the canvas shrink — otherwise its drawing-buffer width
   becomes an implicit min-width and crushes the sidebar. */
#scene {
  flex: 1 1 0; display: block; min-width: 0; min-height: 0;
  touch-action: none; /* let OrbitControls own touch gestures (no page scroll/zoom) */
  -webkit-tap-highlight-color: transparent;
}

.sidebar {
  flex: 0 0 300px;
  width: 300px;
  background: var(--panel);
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 14px;
  overflow-y: auto;
}
.players { display: flex; flex-direction: column; gap: 8px; }
.player-card {
  display: flex; align-items: center; gap: 10px;
  background: var(--panel-2); border-radius: 10px; padding: 9px 11px;
  border-left: 5px solid transparent; opacity: 0.7;
  transition: opacity 0.2s, box-shadow 0.2s;
}
.player-card.current { opacity: 1; box-shadow: 0 0 0 2px var(--accent); }
.player-card .pc-emoji { font-size: 26px; }
.player-card .pc-name { font-weight: 600; }
.player-card .pc-sub { font-size: 12px; color: var(--muted); }
.player-card .pc-home { margin-left: auto; font-size: 13px; }
.pip { display: inline-block; width: 9px; height: 9px; border-radius: 50%; background: rgba(255,255,255,0.2); margin-left: 2px; }
.pip.on { background: var(--accent); }

.turn-box { background: var(--panel-2); border-radius: 12px; padding: 14px; text-align: center; }
.turn-label { font-weight: 600; margin-bottom: 8px; min-height: 20px; }
.dice-readout {
  font-size: 40px; font-weight: 800; margin: 4px 0 12px;
  background: #fff; color: #222; border-radius: 12px; padding: 8px; line-height: 1;
}
.message {
  background: var(--panel-2); border-radius: 10px; padding: 12px;
  font-size: 14px; color: var(--ink); min-height: 44px;
}

/* ---------- overlays ---------- */
.overlay {
  position: absolute; inset: 0; display: none;
  align-items: center; justify-content: center;
  background: rgba(8, 12, 22, 0.78); backdrop-filter: blur(4px); z-index: 10;
}
.overlay.show { display: flex; }
.win-card {
  background: var(--panel); padding: 36px 40px; border-radius: var(--radius);
  text-align: center; box-shadow: var(--shadow); width: 360px;
}
.win-emoji { font-size: 70px; }
.win-card h2 { margin: 6px 0; }

/* ---------- replays ---------- */
/* "Watch a replay" sits under Start Game on the avatar card. */
#open-replays { margin-top: 10px; }

/* Browser overlay: a scrollable list of the player's stored games. */
.replays-card {
  background: var(--panel); padding: 28px 30px; border-radius: var(--radius);
  text-align: center; box-shadow: var(--shadow);
  width: 460px; max-width: calc(100vw - 32px); max-height: 80vh;
  display: flex; flex-direction: column;
}
.replays-card h2 { margin: 0 0 6px; }
.replays-list {
  flex: 1 1 auto; overflow-y: auto; -webkit-overflow-scrolling: touch;
  display: flex; flex-direction: column; gap: 8px;
  margin: 14px 0 18px; min-height: 80px;
}
.replay-row {
  display: flex; align-items: center; gap: 12px; width: 100%;
  background: var(--panel-2); border: 1px solid transparent; border-radius: 10px;
  padding: 12px 14px; min-height: 56px; cursor: pointer; color: var(--ink);
  text-align: left; transition: border-color 0.12s ease, transform 0.08s ease;
  -webkit-tap-highlight-color: transparent; touch-action: manipulation;
}
.replay-row:hover { border-color: var(--accent); }
.replay-row:active { transform: translateY(1px); }
.replay-trophy { font-size: 24px; flex: 0 0 auto; }
.replay-text { display: flex; flex-direction: column; flex: 1 1 auto; min-width: 0; }
.replay-outcome { font-weight: 600; font-size: 15px; }
.replay-meta {
  color: var(--muted); font-size: 12px;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.replay-play { font-size: 18px; color: var(--accent); flex: 0 0 auto; }

/* Playback transport, shown in the turn box while spectating a replay. */
.replay-controls { display: flex; flex-direction: column; gap: 10px; }
.replay-progress { font-size: 13px; color: var(--muted); }
.replay-bar {
  height: 6px; border-radius: 4px; background: var(--panel-2); overflow: hidden;
}
.replay-bar-fill {
  height: 100%; width: 0;
  background: linear-gradient(90deg, var(--accent), var(--accent-2));
  transition: width 0.25s ease;
}
.replay-buttons { display: flex; gap: 8px; }

/* ---------- online multiplayer ---------- */
/* "Play online" sits between Start Game and Watch a replay on the avatar card. */
#play-online { margin-top: 10px; }

.mp-card {
  background: var(--panel); padding: 28px 30px; border-radius: var(--radius);
  text-align: center; box-shadow: var(--shadow);
  width: 420px; max-width: calc(100vw - 32px);
}
.mp-card h2 { margin: 0 0 12px; }
.or-divider { margin: 16px 0 10px; }

.mp-join-row { display: flex; gap: 8px; margin-bottom: 6px; }
.mp-join-row input {
  flex: 1 1 auto; min-width: 0; text-align: center;
  font-size: 22px; letter-spacing: 6px; text-transform: uppercase;
  font-weight: 700; font-family: inherit;
}
.mp-join-row .btn { flex: 0 0 auto; }

/* Lobby view: shared room code + the live seat roster. */
.mp-roomcode {
  font-size: 40px; font-weight: 800; letter-spacing: 10px;
  margin: 6px 0 16px; color: var(--accent);
  font-variant-numeric: tabular-nums;
}
.mp-roster {
  display: flex; flex-direction: column; gap: 8px;
  margin: 0 0 18px; text-align: left;
}
.mp-seat {
  display: flex; align-items: center; gap: 12px;
  background: var(--panel-2); border-radius: 10px;
  padding: 10px 14px; min-height: 48px;
}
.mp-seat.empty { opacity: 0.55; }
.mp-seat-emoji { font-size: 22px; flex: 0 0 auto; }
.mp-seat-name { font-weight: 600; flex: 1 1 auto; min-width: 0;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.mp-badge {
  flex: 0 0 auto; font-size: 11px; font-weight: 700;
  text-transform: uppercase; letter-spacing: 0.5px;
  background: var(--accent); color: #08111e;
  padding: 2px 8px; border-radius: 999px;
}
#mp-wait { margin: 10px 0 0; }
.replay-buttons .btn { flex: 1; }

/* ---------- mobile / portrait ---------- */
@media (max-width: 820px), (orientation: portrait) {
  /* Auth + avatar cards should never overflow a narrow screen. */
  #auth-screen, #avatar-screen { padding: 16px; }
  .auth-card { width: 100%; max-width: 360px; padding: 28px 22px; }
  .avatar-card { width: 100%; max-width: 460px; padding: 28px 22px; }
  .bg-dice { font-size: 320px; right: -40px; bottom: -80px; }

  /* Account for notches / rounded corners on the top bar, and keep its
     contents on one line so the Log out button stays tappable. */
  .topbar {
    gap: 10px;
    padding: 10px 14px;
    padding-top: calc(10px + env(safe-area-inset-top));
    padding-left: calc(14px + env(safe-area-inset-left));
    padding-right: calc(14px + env(safe-area-inset-right));
  }
  .brand { flex-shrink: 0; font-size: 16px; }
  /* Long "Playing as … · AI: …" string truncates instead of wrapping or
     shoving the logout button off-screen. */
  .hud-user {
    min-width: 0;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    font-size: 13px;
  }
  #logout-btn { flex-shrink: 0; padding: 10px 14px; }

  /* Stack the board above the controls instead of side-by-side. */
  .game-wrap { flex-direction: column; }
  #scene { flex: 1 1 auto; min-height: 42vh; }

  .sidebar {
    flex: 0 0 auto;
    width: 100%;
    max-height: 50vh;
    flex-direction: row;
    flex-wrap: wrap;
    align-items: flex-start;
    gap: 10px;
    padding: 12px;
    padding-bottom: calc(12px + env(safe-area-inset-bottom));
    overflow-y: auto;
  }

  /* Roll / turn controls come first and span the full width so the primary
     action sits in the easy-to-reach thumb zone. */
  .turn-box { order: -1; flex: 1 1 100%; }

  /* Player cards become a horizontal, swipeable strip to save vertical space. */
  .players {
    flex: 1 1 100%;
    flex-direction: row;
    overflow-x: auto;
    gap: 8px;
    -webkit-overflow-scrolling: touch;
    padding-bottom: 2px;
  }
  .player-card { flex: 0 0 auto; min-width: 156px; opacity: 1; }

  .message { flex: 1 1 100%; }

  /* Win overlay card must fit a narrow screen too. */
  .win-card { width: 100%; max-width: 360px; padding: 30px 26px; margin: 16px; }
}

/* Short landscape (phone held sideways): vertical room is scarce, so keep the
   board and controls side-by-side instead of stacking them. Overrides the
   stacking rules above, which the orientation:portrait / max-width branch would
   otherwise apply to a small landscape phone. */
@media (max-height: 500px) and (orientation: landscape) {
  .game-wrap { flex-direction: row; }
  #scene { flex: 1 1 0; min-height: 0; }
  .sidebar {
    flex: 0 0 260px;
    width: 260px;
    max-height: none;
    flex-direction: column;
    flex-wrap: nowrap;
    padding-right: calc(12px + env(safe-area-inset-right));
  }
  .players { flex-direction: column; }
  .player-card { min-width: 0; }
}

/* Pointer devices that can't hover (touch): drop hover-only transforms that
   otherwise "stick" after a tap. */
@media (hover: none) {
  .avatar-opt:hover { transform: none; }
  .btn.primary:hover { filter: none; }
}
