/*
  Pibb Game — stylesheet
  ======================
  Design tokens live in :root. Layout is a horizontal split:
    [ game-area (flex 1) | chat-sidebar (fixed width) ]

  Legacy lobby styles (.tabs, .card, etc.) remain for potential reuse;
  the live app skips the lobby and uses body.in-room immediately.
*/

/* ── Reset ─────────────────────────────────────────────────────────── */

*,
*::before,
*::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

/* ── Design tokens ─────────────────────────────────────────────────── */

:root {
  --bg: #102a2e;
  --surface: #26554e;
  --surface-2: #0e252b;
  --border: #163747;
  --text: #e8edf4;
  --text-muted: #8b9cb3;
  --accent: #941cc4;
  --accent-hover: #25eb8f;
  --success: #22c55e;
  --error: #ef4444;
  --warning: #f59e0b;
  --radius: 12px;
  --shadow: 0 8px 32px rgba(0, 0, 0, 0.35);
  --chat-width: 320px;
}

/* ── Base page ─────────────────────────────────────────────────────── */

html {
  height: 100%;
  overflow: hidden;
  -webkit-text-size-adjust: 100%;
}

body {
  font-family: "Segoe UI", system-ui, -apple-system, sans-serif;
  background: var(--bg);
  background-image:
    radial-gradient(ellipse at 20% 0%, rgba(59, 130, 246, 0.12), transparent 50%),
    radial-gradient(ellipse at 80% 100%, rgba(34, 197, 94, 0.08), transparent 50%);
  color: var(--text);
  min-height: 100vh;
  line-height: 1.5;
}

/* Full-viewport mode while in-game — lock document scroll on iOS */
body.in-room {
  position: fixed;
  inset: 0;
  width: 100%;
  height: 100%;
  overflow: hidden;
  overscroll-behavior: none;
  -webkit-overflow-scrolling: auto;
  padding:
    env(safe-area-inset-top)
    env(safe-area-inset-right)
    env(safe-area-inset-bottom)
    env(safe-area-inset-left);
}

.container {
  max-width: 480px;
  margin: 0 auto;
  padding: 2rem 1.25rem;
}

body.in-room .container {
  max-width: none;
  margin: 0;
  padding: 0;
  width: 100%;
  height: 100%;
  height: 100dvh;
  overflow: hidden;
}

/* ── Legacy lobby components (unused in auto-join flow) ────────────── */

.header {
  text-align: center;
  margin-bottom: 2rem;
}

.header h1 {
  font-size: 1.75rem;
  font-weight: 700;
  letter-spacing: -0.02em;
}

.subtitle {
  color: var(--text-muted);
  margin-top: 0.35rem;
  font-size: 0.95rem;
}

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1.5rem;
  box-shadow: var(--shadow);
}

.hidden {
  display: none !important;
}

.tabs {
  display: flex;
  gap: 0.5rem;
  margin-bottom: 1.5rem;
  background: var(--bg);
  padding: 0.25rem;
  border-radius: 8px;
}

.tab {
  flex: 1;
  padding: 0.6rem 1rem;
  border: none;
  background: transparent;
  color: var(--text-muted);
  font-size: 0.9rem;
  font-weight: 600;
  border-radius: 6px;
  cursor: pointer;
  transition: background 0.15s, color 0.15s;
}

.tab:hover {
  color: var(--text);
}

.tab.active {
  background: var(--surface-2);
  color: var(--text);
}

.panel {
  display: none;
}

.panel.active {
  display: block;
}

label {
  display: block;
  font-size: 0.85rem;
  font-weight: 600;
  margin-bottom: 0.4rem;
  color: var(--text-muted);
}

input[type="text"] {
  width: 100%;
  padding: 0.75rem 1rem;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 8px;
  color: var(--text);
  font-size: 1.1rem;
  font-family: "Cascadia Code", "Consolas", monospace;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  transition: border-color 0.15s;
}

input[type="text"]:focus {
  outline: none;
  border-color: var(--accent);
}

input[type="text"]::placeholder {
  text-transform: none;
  letter-spacing: normal;
  font-family: inherit;
  color: var(--text-muted);
  opacity: 0.6;
}

.hint {
  font-size: 0.8rem;
  color: var(--text-muted);
  margin: 0.4rem 0 1rem;
}

/* ── Shared button styles ──────────────────────────────────────────── */

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 0.7rem 1.25rem;
  border: none;
  border-radius: 8px;
  font-size: 0.95rem;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.15s, opacity 0.15s;
}

.btn:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}

.btn-primary {
  width: 100%;
  background: var(--accent);
  color: #fff;
}

.btn-primary:hover:not(:disabled) {
  background: var(--accent-hover);
}

.btn-secondary {
  background: var(--surface-2);
  color: var(--text);
  border: 1px solid var(--border);
}

.btn-secondary:hover:not(:disabled) {
  background: var(--border);
}

.btn-ghost {
  width: 100%;
  margin-top: 0.75rem;
  background: transparent;
  color: var(--text-muted);
  border: 1px solid var(--border);
}

.btn-ghost:hover {
  color: var(--text);
  border-color: var(--text-muted);
}

.status {
  min-height: 1.25rem;
  margin-top: 0.75rem;
  font-size: 0.875rem;
}

.status.error {
  color: var(--error);
}

.status.success {
  color: var(--success);
}

/* ── In-game layout: game canvas + chat sidebar ────────────────────── */

.room-layout {
  display: flex;
  height: 100%;
  width: 100%;
  max-width: 100%;
  overflow: hidden;
}

.game-area {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-width: 0;
  background: var(--bg);
}

.game-header {
  display: flex;
  align-items: center;
  gap: 1rem;
  padding: 0.75rem 1rem;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
  min-width: 0;
  max-width: 100%;
  overflow: hidden;
}

.room-info {
  display: flex;
  align-items: center;
  gap: 0.75rem;
}

.room-info h2 {
  font-size: 1.1rem;
  font-weight: 600;
}

.player-name-field {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  margin-left: 0.5rem;
}

.player-name-label {
  font-size: 0.8rem;
  font-weight: 600;
  color: var(--text-muted);
}

#player-name-input {
  width: 10rem;
  padding: 0.4rem 0.65rem;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 6px;
  color: var(--text);
  font-size: 0.9rem;
  font-family: inherit;
  letter-spacing: normal;
  text-transform: none;
}

#player-name-input:focus {
  outline: none;
  border-color: var(--accent);
}

#player-name-input:disabled {
  opacity: 0.45;
}

/* Host vs guest role indicator */
.badge {
  font-size: 0.7rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  padding: 0.25rem 0.5rem;
  border-radius: 4px;
  background: var(--accent);
  color: #fff;
}

.badge.guest {
  background: var(--success);
}

/* Connection state colors (waiting / connected / disconnected) */
.connection {
  font-size: 0.8rem;
}

.connection.waiting {
  color: var(--warning);
}

.connection.connected {
  color: var(--success);
}

.connection.disconnected {
  color: var(--error);
}

.connection.error {
  color: var(--error);
}

.controls-hint {
  margin-left: auto;
  font-size: 0.8rem;
  color: var(--text-muted);
}

/* Canvas container; leaderboard is absolutely positioned inside */
.game-body {
  position: relative;
  flex: 1;
  min-height: 0;
  display: flex;
}

#game-canvas {
  flex: 1;
  display: block;
  width: 100%;
  height: 100%;
  cursor: crosshair;
  touch-action: none; /* Prevent browser scroll/zoom on touch; app handles pinch */
}

/* Overlay leaderboard — does not block canvas pointer events */
.leaderboard {
  position: absolute;
  top: 0.75rem;
  right: 0.75rem;
  width: 11.5rem;
  max-height: calc(100% - 1.5rem);
  overflow-y: auto;
  background: rgba(26, 35, 50, 0.92);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 0.65rem 0.75rem;
  box-shadow: var(--shadow);
  pointer-events: none;
  z-index: 2;
}

.leaderboard-title {
  font-size: 0.8rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--text-muted);
  margin-bottom: 0.5rem;
}

.leaderboard-list {
  list-style: none;
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
}

.leaderboard-list li {
  display: grid;
  grid-template-columns: 1.25rem 1fr auto;
  gap: 0.35rem;
  align-items: center;
  font-size: 0.85rem;
  padding: 0.25rem 0.15rem;
  border-radius: 4px;
}

.leaderboard-list li.is-local {
  background: rgba(59, 130, 246, 0.15);
}

.leaderboard-list .rank {
  color: var(--text-muted);
  font-size: 0.75rem;
  font-weight: 600;
  text-align: center;
}

.leaderboard-list .name {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.leaderboard-list .score {
  font-weight: 700;
  color: #c4a574;
  font-variant-numeric: tabular-nums;
}

.leaderboard-empty {
  display: block;
  font-size: 0.8rem;
  color: var(--text-muted);
  font-style: italic;
  padding: 0.15rem 0;
}

/* ── Chat sidebar ──────────────────────────────────────────────────── */

.chat-sidebar {
  width: var(--chat-width);
  flex-shrink: 0;
  display: flex;
  flex-direction: column;
  background: var(--surface);
  border-left: 1px solid var(--border);
  padding: 1rem;
  min-width: 0;
  max-width: 100%;
  overflow: hidden;
  touch-action: pan-y;
}

.chat-sidebar-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 0.75rem;
  flex-shrink: 0;
}

.chat-sidebar-header h3 {
  font-size: 1rem;
  font-weight: 600;
}

.player-count {
  font-size: 0.75rem;
  color: var(--text-muted);
}

.messages {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  overflow-x: hidden;
  overscroll-behavior: contain;
  -webkit-overflow-scrolling: touch;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 0.75rem;
  margin-bottom: 0.75rem;
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

/* Chat bubble variants: local (you), remote, system (italic center) */
.message {
  font-size: 0.9rem;
  padding: 0.4rem 0.6rem;
  border-radius: 6px;
  max-width: 85%;
  word-break: break-word;
}

.message.local {
  align-self: flex-end;
  background: var(--accent);
  color: #fff;
}

.message.remote {
  align-self: flex-start;
  background: var(--surface-2);
}

.message.system {
  align-self: center;
  font-size: 0.8rem;
  color: var(--text-muted);
  font-style: italic;
  max-width: 100%;
  text-align: center;
}

.message-form {
  display: flex;
  gap: 0.5rem;
  flex-shrink: 0;
  min-width: 0;
}

.message-form input {
  flex: 1;
  text-transform: none;
  letter-spacing: normal;
  /* iOS Safari auto-zooms focused inputs smaller than 16px */
  font-size: 16px;
  font-family: inherit;
}

/* ── Mobile: stack game above chat ─────────────────────────────────── */

@media (max-width: 640px) {
  .room-layout {
    flex-direction: column;
  }

  .game-header {
    flex-wrap: wrap;
    gap: 0.4rem 0.6rem;
    padding: 0.5rem 0.75rem;
  }

  .room-info h2 {
    font-size: 1rem;
  }

  .player-name-field {
    order: 3;
    flex: 1 1 100%;
    margin-left: 0;
    min-width: 0;
  }

  #player-name-input {
    width: 100%;
    min-width: 0;
    /* Keep 16px on iOS to avoid focus-zoom */
    font-size: 16px;
  }

  .connection {
    font-size: 0.75rem;
    max-width: 7.5rem;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  .controls-hint {
    display: none;
  }

  .game-area {
    flex: 3;
    min-height: 0;
    height: auto;
  }

  .chat-sidebar {
    flex: 2;
    min-height: 0;
    width: 100%;
    height: auto;
    border-left: none;
    border-top: 1px solid var(--border);
    padding: 0.75rem;
    padding-bottom: max(0.75rem, env(safe-area-inset-bottom));
  }

  .message-form input {
    min-width: 0;
  }
}

/* ── Site footer links (in-game sidebar) ───────────────────────────── */

.site-links {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: center;
  gap: 0.35rem;
  margin-top: 0.75rem;
  font-size: 0.75rem;
  color: var(--text-muted);
}

.site-links a {
  color: var(--text-muted);
  text-decoration: none;
}

.site-links a:hover {
  color: var(--text);
  text-decoration: underline;
}

/* ── Cookie consent banner ─────────────────────────────────────────── */

.consent-banner {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 1000;
  padding: 0.75rem 1rem;
  padding-bottom: max(0.75rem, env(safe-area-inset-bottom));
  background: var(--surface);
  border-top: 1px solid var(--border);
  box-shadow: 0 -4px 24px rgba(0, 0, 0, 0.35);
}

.consent-banner-inner {
  max-width: 960px;
  margin: 0 auto;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem 1rem;
}

.consent-banner p {
  flex: 1;
  min-width: 200px;
  font-size: 0.875rem;
  color: var(--text);
}

.consent-banner a {
  color: var(--accent-hover);
}

.consent-banner-actions {
  display: flex;
  gap: 0.5rem;
  flex-shrink: 0;
}

.consent-banner .btn {
  width: auto;
  padding: 0.5rem 1rem;
  font-size: 0.875rem;
}

/* ── Legal / policy pages ──────────────────────────────────────────── */

body.legal {
  min-height: 100vh;
  overflow: auto;
}

body.legal .legal-page {
  max-width: 720px;
  margin: 0 auto;
  padding: 2rem 1.25rem 3rem;
}

.legal-header {
  margin-bottom: 2rem;
}

.legal-back {
  display: inline-block;
  margin-bottom: 1rem;
  font-size: 0.9rem;
  color: var(--text-muted);
  text-decoration: none;
}

.legal-back:hover {
  color: var(--text);
}

.legal-header h1 {
  font-size: 1.75rem;
  font-weight: 700;
  letter-spacing: -0.02em;
}

.legal-meta {
  margin-top: 0.35rem;
  font-size: 0.875rem;
  color: var(--text-muted);
}

.legal-content h2 {
  margin-top: 1.75rem;
  margin-bottom: 0.75rem;
  font-size: 1.15rem;
  font-weight: 600;
}

.legal-content h3 {
  margin-top: 1.25rem;
  margin-bottom: 0.5rem;
  font-size: 1rem;
  font-weight: 600;
}

.legal-content p,
.legal-content li {
  margin-bottom: 0.75rem;
  color: var(--text);
}

.legal-content ul {
  margin: 0 0 0.75rem 1.25rem;
}

.legal-content a {
  color: var(--accent-hover);
}

.legal-content code {
  font-size: 0.875em;
  background: var(--surface-2);
  padding: 0.1em 0.35em;
  border-radius: 4px;
}

.legal-table {
  width: 100%;
  border-collapse: collapse;
  margin: 1rem 0;
  font-size: 0.9rem;
}

.legal-table th,
.legal-table td {
  border: 1px solid var(--border);
  padding: 0.6rem 0.75rem;
  text-align: left;
}

.legal-table th {
  background: var(--surface);
  font-weight: 600;
}

.legal-table td {
  background: var(--surface-2);
}

.legal-play-cta {
  margin-top: 2rem;
}

.legal-play-cta .btn {
  width: auto;
  min-width: 10rem;
}

.legal-footer {
  display: flex;
  flex-wrap: wrap;
  gap: 1rem;
  margin-top: 2.5rem;
  padding-top: 1.5rem;
  border-top: 1px solid var(--border);
  font-size: 0.875rem;
}

.legal-footer a {
  color: var(--text-muted);
  text-decoration: none;
}

.legal-footer a:hover,
.legal-footer a[aria-current="page"] {
  color: var(--text);
}

.legal-footer a[aria-current="page"] {
  font-weight: 600;
}
