/* ==========================================================================
   Hive Online - dark, high-contrast, touch-first
   ========================================================================== */

:root {
  color-scheme: dark;

  --bg: #000000;
  --bg-elev: #0b0b0f;
  --surface: #16161d;
  --surface-2: #22222c;
  --border: #3a3a48;
  --text: #ffffff;
  --text-muted: #c9c9d1;

  --accent: #ffd60a;        /* selection / focus */
  --target: #22d3ee;        /* legal destination */
  --ok: #4ade80;            /* your turn */
  --wait: #9ca3af;          /* opponent's turn */
  --danger: #ff5c5c;

  --tile-white: #f5f5f5;
  --tile-white-ink: #000000;
  --tile-black: #1f1f1f;
  --tile-black-ink: #ffffff;
  --tile-black-edge: #f5f5f5;

  --bug-Q: #ffd60a;
  --bug-B: #c77dff;
  --bug-G: #4ade80;
  --bug-S: #fb923c;
  --bug-A: #38bdf8;

  --tap: 44px;
  --radius: 14px;
  --font: system-ui, -apple-system, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;

  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-left: env(safe-area-inset-left, 0px);
  --safe-right: env(safe-area-inset-right, 0px);
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  font-size: 17px;
  line-height: 1.35;
  -webkit-text-size-adjust: 100%;
  -webkit-tap-highlight-color: transparent;
}

#app {
  height: 100dvh;
  display: flex;
  flex-direction: column;
}

.screen { flex: 1; min-height: 0; display: flex; flex-direction: column; }
[hidden] { display: none !important; }

:focus-visible {
  outline: 3px solid var(--accent);
  outline-offset: 2px;
}

b { font-weight: 700; }
.muted { color: var(--text-muted); }

/* ---------- Buttons ---------- */

.btn, .icon-btn, .menu-item, .hand-tile {
  font: inherit;
  color: var(--text);
  background: var(--surface);
  border: 2px solid var(--border);
  border-radius: var(--radius);
  min-height: var(--tap);
  min-width: var(--tap);
  padding: 0 18px;
  cursor: pointer;
  touch-action: manipulation;
  user-select: none;
  -webkit-user-select: none;
}
.btn:active, .icon-btn:active, .menu-item:active { background: var(--surface-2); }
.btn.big { min-height: 52px; font-size: 1.05rem; font-weight: 600; }
.btn.primary { background: var(--accent); color: #000; border-color: var(--accent); }
.btn.primary:active { filter: brightness(0.9); }
.btn.ghost { background: transparent; }
.btn.danger, .menu-item.danger { color: var(--danger); border-color: var(--danger); }
.btn:disabled, .hand-tile:disabled { opacity: 0.4; cursor: default; }

.icon-btn {
  padding: 0;
  width: 48px;
  height: 48px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-size: 1.6rem;
  font-weight: 700;
  line-height: 1;
}
.icon-btn svg { width: 26px; height: 26px; }

/* ---------- Lobby ---------- */

#lobby {
  align-items: center;
  justify-content: center;
  padding: calc(16px + var(--safe-top)) 16px calc(16px + var(--safe-bottom));
  overflow: auto;
}
.lobby-card {
  width: 100%;
  max-width: 460px;
  background: var(--bg-elev);
  border: 2px solid var(--border);
  border-radius: 20px;
  padding: 24px 20px;
}
.lobby-card h1 {
  margin: 0 0 6px;
  font-size: 2.2rem;
  letter-spacing: 0.02em;
  display: flex;
  align-items: center;
  gap: 12px;
}
.logo-hex {
  width: 34px; height: 38px;
  background: var(--accent);
  clip-path: polygon(50% 0, 100% 25%, 100% 75%, 50% 100%, 0 75%, 0 25%);
  display: inline-block;
}
.lead { margin: 0 0 20px; color: var(--text-muted); }
.lobby-block { display: flex; flex-direction: column; gap: 14px; }
.check { display: flex; align-items: center; gap: 12px; min-height: var(--tap); cursor: pointer; }
.check input { width: 26px; height: 26px; accent-color: var(--accent); }
.field-label { font-weight: 600; }
.join-row { display: flex; gap: 10px; }
.join-row input {
  flex: 1;
  min-width: 0;
  min-height: 52px;
  font: inherit;
  font-size: 1.3rem;
  letter-spacing: 0.18em;
  text-transform: uppercase;
  color: var(--text);
  background: var(--surface);
  border: 2px solid var(--border);
  border-radius: var(--radius);
  padding: 0 14px;
}
#share-link { letter-spacing: 0; text-transform: none; font-size: 0.95rem; }
.divider {
  display: flex; align-items: center; gap: 12px;
  margin: 22px 0; color: var(--text-muted);
}
.divider::before, .divider::after { content: ""; flex: 1; height: 2px; background: var(--border); }
.rules { margin-top: 24px; }
.rules summary { cursor: pointer; min-height: var(--tap); display: flex; align-items: center; font-weight: 600; }
.rules ul { padding-left: 20px; color: var(--text-muted); }
.rules li { margin: 6px 0; }

/* ---------- Game shell ---------- */

#game { position: relative; }

.topbar {
  position: relative;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: calc(8px + var(--safe-top)) calc(10px + var(--safe-right)) 8px calc(12px + var(--safe-left));
  background: var(--bg-elev);
  border-bottom: 2px solid var(--border);
}
.status {
  flex: 1;
  min-height: var(--tap);
  display: flex;
  align-items: center;
  font-weight: 700;
  font-size: 1.1rem;
}
.status.mine { color: var(--ok); }
.status.theirs { color: var(--wait); }
.status.over { color: var(--accent); }

.menu {
  position: absolute;
  top: calc(100% + 6px);
  right: 10px;
  z-index: 20;
  display: flex;
  flex-direction: column;
  gap: 6px;
  padding: 8px;
  background: var(--surface);
  border: 2px solid var(--border);
  border-radius: var(--radius);
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.8);
  min-width: 240px;
}
.menu-item { text-align: left; min-height: 48px; }

.opp-hand {
  display: flex;
  gap: 6px;
  padding: 6px calc(10px + var(--safe-right)) 6px calc(10px + var(--safe-left));
  background: var(--bg-elev);
  border-bottom: 2px solid var(--border);
  overflow-x: auto;
  align-items: center;
  min-height: 46px;
}
.opp-hand .label { color: var(--text-muted); font-size: 0.9rem; margin-right: 4px; white-space: nowrap; }

.board-wrap {
  position: relative;
  flex: 1;
  min-height: 0;
  background: var(--bg);
}
.board {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  touch-action: none;
  display: block;
}
.board-controls {
  position: absolute;
  right: calc(10px + var(--safe-right));
  bottom: 10px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  z-index: 5;
}
.banner {
  position: absolute;
  left: 50%;
  top: 12px;
  transform: translateX(-50%);
  z-index: 6;
  max-width: calc(100% - 24px);
  background: var(--surface);
  border: 2px solid var(--accent);
  border-radius: var(--radius);
  padding: 10px 14px;
  display: flex;
  gap: 10px;
  align-items: center;
  flex-wrap: wrap;
  font-weight: 600;
}
.banner .btn { min-height: var(--tap); }

/* ---------- Hand tray ---------- */

.bottombar {
  background: var(--bg-elev);
  border-top: 2px solid var(--border);
  padding: 8px calc(10px + var(--safe-right)) calc(8px + var(--safe-bottom)) calc(10px + var(--safe-left));
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.hand {
  display: flex;
  gap: 10px;
  overflow-x: auto;
  padding-bottom: 2px;
  scrollbar-width: thin;
  min-height: 72px;
  align-items: center;
}
.hand:empty { min-height: 0; }
.hand-tile {
  position: relative;
  flex: 0 0 auto;
  width: 64px;
  height: 64px;
  padding: 0;
  display: grid;
  place-items: center;
  border-radius: 12px;
}
.hand-tile.white { background: var(--tile-white); color: var(--tile-white-ink); border-color: var(--tile-white); }
.hand-tile.black { background: var(--tile-black); color: var(--tile-black-ink); border-color: var(--tile-black-edge); }
.hand-tile .glyph {
  width: 40px; height: 40px;
  border-radius: 50%;
  display: grid; place-items: center;
  font-weight: 800; font-size: 1.35rem;
  color: #000;
}
.hand-tile .count {
  position: absolute;
  top: -8px; right: -8px;
  min-width: 26px; height: 26px;
  padding: 0 6px;
  border-radius: 13px;
  background: var(--text);
  color: #000;
  font-size: 0.9rem; font-weight: 800;
  display: grid; place-items: center;
  border: 2px solid var(--bg);
}
.hand-tile.selected { outline: 4px solid var(--accent); outline-offset: 2px; }
.hand-tile.compact { width: 36px; height: 36px; border-radius: 8px; min-width: 0; min-height: 0; cursor: default; }
.hand-tile.compact:disabled { opacity: 1; }
.hand-tile.compact .glyph { width: 24px; height: 24px; font-size: 0.85rem; }
.hand-tile.compact .count { min-width: 20px; height: 20px; font-size: 0.75rem; top: -7px; right: -7px; }

.glyph.Q { background: var(--bug-Q); }
.glyph.B { background: var(--bug-B); }
.glyph.G { background: var(--bug-G); }
.glyph.S { background: var(--bug-S); }
.glyph.A { background: var(--bug-A); }

.actions { display: flex; gap: 10px; flex-wrap: wrap; }
.actions:empty { display: none; }
.actions .btn { flex: 1; }

/* ---------- SVG board pieces ---------- */

.hex-tile { stroke-width: 3; }
.hex-tile.white { fill: var(--tile-white); stroke: #000; }
.hex-tile.black { fill: var(--tile-black); stroke: var(--tile-black-edge); }
.bug-disc { stroke: #000; stroke-width: 2; }
.bug-disc.Q { fill: var(--bug-Q); }
.bug-disc.B { fill: var(--bug-B); }
.bug-disc.G { fill: var(--bug-G); }
.bug-disc.S { fill: var(--bug-S); }
.bug-disc.A { fill: var(--bug-A); }
.bug-letter {
  fill: #000;
  font-family: var(--font);
  font-weight: 800;
  text-anchor: middle;
  dominant-baseline: central;
  pointer-events: none;
}
.stack-badge { fill: var(--text); stroke: #000; stroke-width: 2; }
.stack-badge-text { fill: #000; font-weight: 800; text-anchor: middle; dominant-baseline: central; pointer-events: none; }

.hex-target {
  fill: rgba(34, 211, 238, 0.28);
  stroke: var(--target);
  stroke-width: 4;
  stroke-dasharray: 10 6;
  cursor: pointer;
}
.hex-target.on-stack { fill: rgba(34, 211, 238, 0.45); }
.hex-selected { fill: none; stroke: var(--accent); stroke-width: 6; pointer-events: none; }
.hex-last { fill: none; stroke: var(--text); stroke-width: 2.5; stroke-dasharray: 4 4; pointer-events: none; }
.hex-pickable { cursor: pointer; }

@media (prefers-reduced-motion: no-preference) {
  .hex-target { animation: pulse 1.4s ease-in-out infinite; }
  @keyframes pulse { 50% { stroke-opacity: 0.45; } }
}

/* ---------- Modals / toast ---------- */

.modal {
  position: fixed;
  inset: 0;
  z-index: 50;
  background: rgba(0, 0, 0, 0.78);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
}
.modal-card {
  width: 100%;
  max-width: 440px;
  background: var(--surface);
  border: 2px solid var(--border);
  border-radius: 20px;
  padding: 22px 20px calc(20px + var(--safe-bottom));
  display: flex;
  flex-direction: column;
  gap: 14px;
  max-height: 90dvh;
  overflow: auto;
}
.modal-card h2 { margin: 0; font-size: 1.5rem; }
.modal-card p { margin: 0; }
.modal-actions { display: flex; flex-direction: column; gap: 10px; }
.code { font-size: 1.4rem; letter-spacing: 0.18em; }

.history-list {
  margin: 0;
  padding-left: 2.2em;
  font-family: ui-monospace, Consolas, monospace;
  font-size: 1rem;
  columns: 2;
  column-gap: 20px;
}
.history-list li { margin: 3px 0; break-inside: avoid; }

.toast {
  position: fixed;
  left: 50%;
  bottom: calc(110px + var(--safe-bottom));
  transform: translateX(-50%);
  z-index: 60;
  max-width: calc(100% - 32px);
  background: var(--text);
  color: #000;
  font-weight: 700;
  font-size: 1.05rem;
  padding: 14px 18px;
  border-radius: var(--radius);
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.8);
}
.toast.error { background: var(--danger); color: #000; }

/* ---------- Onboarding / home ---------- */

#onboard { align-items: center; justify-content: center; padding: 16px; overflow: auto; }

.logo-bee { width: 36px; height: 40px; color: var(--accent); flex: 0 0 auto; }

.text-input {
  min-height: 52px;
  font: inherit;
  font-size: 1.1rem;
  color: var(--text);
  background: var(--surface);
  border: 2px solid var(--border);
  border-radius: var(--radius);
  padding: 0 14px;
  width: 100%;
}
.error-text { color: var(--danger); font-weight: 600; margin: 0; }

.home-title {
  margin: 0;
  font-size: 1.5rem;
  display: flex;
  align-items: center;
  gap: 10px;
}
.home-title .logo-bee { width: 26px; height: 30px; }

.badge {
  min-width: 26px;
  height: 26px;
  padding: 0 7px;
  border-radius: 13px;
  background: var(--accent);
  color: #000;
  font-weight: 800;
  font-size: 0.95rem;
  display: inline-grid;
  place-items: center;
}

.home-scroll {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  padding: 16px calc(14px + var(--safe-right)) calc(20px + var(--safe-bottom)) calc(14px + var(--safe-left));
  display: flex;
  flex-direction: column;
  gap: 18px;
  width: 100%;
  max-width: 560px;
  margin: 0 auto;
}
.home-actions { display: flex; flex-direction: column; gap: 12px; }

.section-title {
  margin: 0 0 8px;
  font-size: 1.05rem;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.06em;
}
.games-section { display: flex; flex-direction: column; gap: 10px; }
.games-section:empty { display: none; }

.game-card {
  display: flex;
  align-items: center;
  gap: 12px;
  background: var(--surface);
  border: 2px solid var(--border);
  border-radius: var(--radius);
  padding: 12px 14px;
}
.game-card.urgent { border-color: var(--accent); }
.color-dot {
  position: relative;
  flex: 0 0 auto;
  width: 20px; height: 22px;
  clip-path: polygon(50% 0, 100% 25%, 100% 75%, 50% 100%, 0 75%, 0 25%);
}
.color-dot.white { background: var(--tile-white); }
/* Black tile needs a light rim, and outlines are clipped by the hexagon clip-path,
   so draw rim + fill as two nested hexagons. */
.color-dot.black { background: var(--tile-white); }
.color-dot.black::after {
  content: "";
  position: absolute;
  inset: 2.5px;
  background: var(--tile-black);
  clip-path: polygon(50% 0, 100% 25%, 100% 75%, 50% 100%, 0 75%, 0 25%);
}
.game-info { flex: 1; min-width: 0; }
.game-opponent { font-weight: 700; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.game-sub { color: var(--text-muted); font-size: 0.9rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.game-card-actions { display: flex; gap: 8px; flex: 0 0 auto; }
.game-card-actions .btn { padding: 0 14px; }

/* ---------- Game topbar extras ---------- */

.status-wrap { flex: 1; min-width: 0; }
.players-line {
  color: var(--text-muted);
  font-size: 0.85rem;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.players-line:empty { display: none; }

/* ---------- Hand glyph icons ---------- */

.hand-tile .glyph svg { width: 80%; height: 80%; color: #000; display: block; margin: auto; }

/* ---------- Board tile icons ---------- */

.bug-icon { color: #000; pointer-events: none; }
.bug-badge { stroke-width: 1.5; }
.bug-badge.white { fill: #000; stroke: var(--tile-white); }
.bug-badge.black { fill: var(--tile-white); stroke: #000; }
.bug-badge-letter {
  font-family: var(--font);
  font-weight: 800;
  text-anchor: middle;
  dominant-baseline: central;
  pointer-events: none;
}
.bug-badge-letter.white { fill: var(--tile-white); }
.bug-badge-letter.black { fill: #000; }

/* ---------- Replay ---------- */

.replay-bar { gap: 10px; }
.replay-notation {
  text-align: center;
  font-weight: 700;
  font-family: ui-monospace, Consolas, monospace;
  min-height: 1.4em;
}
.replay-controls { display: flex; justify-content: center; gap: 10px; }
#replay-slider {
  width: 100%;
  accent-color: var(--accent);
  min-height: var(--tap);
}

/* ---------- Stats ---------- */

.stats-card {
  background: var(--surface);
  border: 2px solid var(--border);
  border-radius: var(--radius);
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}
.stat-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 10px;
}
.stat {
  background: var(--surface-2);
  border-radius: 10px;
  padding: 10px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
}
.stat b { font-size: 1.4rem; }
.stat span { color: var(--text-muted); font-size: 0.85rem; }
.stats-table { width: 100%; border-collapse: collapse; }
.stats-table th, .stats-table td {
  text-align: left;
  padding: 8px 6px;
  border-bottom: 1px solid var(--border);
}
.stats-table th { color: var(--text-muted); font-size: 0.85rem; text-transform: uppercase; letter-spacing: 0.05em; }
.stats-table tr:last-child td { border-bottom: 0; }
.stats-table td:first-child, .stats-table th:first-child { padding-left: 0; }

/* ---------- Landscape / desktop: trays to the side ---------- */

@media (min-width: 900px) and (min-aspect-ratio: 1/1) {
  #game {
    display: grid;
    grid-template-columns: 1fr 320px;
    grid-template-rows: auto auto 1fr;
    grid-template-areas:
      "top top"
      "board opp"
      "board hand";
  }
  .topbar { grid-area: top; }
  .board-wrap { grid-area: board; }
  .opp-hand {
    grid-area: opp;
    border-bottom: 2px solid var(--border);
    border-left: 2px solid var(--border);
    flex-wrap: wrap;
    padding: 14px;
  }
  .bottombar {
    grid-area: hand;
    border-top: 0;
    border-left: 2px solid var(--border);
    justify-content: flex-end;
    padding: 14px;
  }
  .hand { flex-wrap: wrap; overflow: visible; padding-top: 10px; }
  .actions { flex-direction: column; }
}
