:root {
  --bg: #1f2933;
  --panel: #2b3744;
  --panel-2: #34424f;
  --ink: #eef2f5;
  --muted: #9fb0bd;
  --accent: #f0a500;
  --accent-ink: #1f2933;
  --tile: #f3e1bd;
  --tile-ink: #3a2c12;
  --tw: #d7484b;
  --dw: #e79aa0;
  --tl: #2f78b5;
  --dl: #a8d4ef;
  --cell: #1c8a5a;
  --cell-line: #15623f;
  --good: #4caf50;
  --bad: #ef5350;
}

* { box-sizing: border-box; }

html {
  -webkit-text-size-adjust: 100%; /* iOS: don't auto-inflate text */
}

body {
  margin: 0;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--ink);
  -webkit-tap-highlight-color: transparent; /* no grey flash on tap (iOS) */
  overscroll-behavior: none;
}

.hidden { display: none !important; }

/* ---------- Lobby ---------- */
#lobby {
  min-height: 100vh;
  min-height: 100dvh; /* iOS: account for the dynamic toolbar */
  display: grid;
  place-items: center;
  padding: max(1rem, env(safe-area-inset-top)) 1rem
           max(1rem, env(safe-area-inset-bottom));
}
.card {
  background: var(--panel);
  padding: 2rem;
  border-radius: 16px;
  width: min(420px, 100%);
  box-shadow: 0 16px 40px rgba(0, 0, 0, 0.4);
}
.card h1 {
  margin: 0;
  font-size: 2.5rem;
  letter-spacing: 0.05em;
  text-align: center;
  color: var(--accent);
}
.tagline { text-align: center; color: var(--muted); margin-top: 0.25rem; }
.field { display: block; margin: 1.5rem 0 1rem; }
.field span { display: block; font-size: 0.85rem; color: var(--muted); margin-bottom: 0.35rem; }
input[type="text"] {
  width: 100%;
  padding: 0.7rem 0.8rem;
  border-radius: 10px;
  border: 1px solid #46566590;
  background: var(--panel-2);
  color: var(--ink);
  font-size: 1rem;
}
#join-code, #chat-input { text-transform: none; }
#join-code { text-transform: uppercase; letter-spacing: 0.2em; text-align: center; }
.lobby-actions { display: grid; gap: 0.75rem; }
.join-row { display: flex; gap: 0.5rem; }
.join-row input { flex: 1; }

button {
  cursor: pointer;
  border: none;
  border-radius: 10px;
  padding: 0.7rem 1rem;
  font-size: 0.95rem;
  font-weight: 600;
  background: var(--panel-2);
  color: var(--ink);
  transition: filter 0.15s, transform 0.05s;
}
button:hover { filter: brightness(1.15); }
button:active { transform: translateY(1px); }
button:disabled { opacity: 0.45; cursor: not-allowed; }
button.primary { background: var(--accent); color: var(--accent-ink); }

.error { color: var(--bad); min-height: 1.2em; margin: 0.75rem 0 0; font-size: 0.9rem; }

/* ---------- Game ---------- */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  padding: 0.6rem 1rem;
  background: var(--panel);
  flex-wrap: wrap;
}
.turn-banner { font-weight: 700; color: var(--accent); }
.room, .bag { color: var(--muted); }
.room strong, .bag strong { color: var(--ink); letter-spacing: 0.15em; }

.layout {
  display: grid;
  grid-template-columns: minmax(320px, 1fr) 320px;
  gap: 1rem;
  padding: 1rem;
  align-items: start;
}
.board-wrap { display: grid; place-items: center; }
#board {
  /* fits both width (portrait) and height (landscape) without scrolling */
  width: min(640px, 96vw, 70vh);
  height: auto;
  background: var(--cell-line);
  border-radius: 8px;
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
}

.sidebar { display: grid; gap: 1rem; }
.panel { background: var(--panel); border-radius: 12px; padding: 0.9rem 1rem; }
.panel h2 { margin: 0 0 0.6rem; font-size: 0.95rem; color: var(--muted); text-transform: uppercase; letter-spacing: 0.05em; }
.hint { color: var(--muted); font-size: 0.85rem; }

#players { list-style: none; margin: 0; padding: 0; display: grid; gap: 0.4rem; }
#players li {
  display: flex; justify-content: space-between; align-items: center;
  padding: 0.45rem 0.6rem; border-radius: 8px; background: var(--panel-2);
}
#players li.active { outline: 2px solid var(--accent); }
#players li.offline { opacity: 0.5; }
#players .pscore { font-weight: 700; color: var(--accent); }

.history, #chat-log { list-style: none; margin: 0; padding: 0; max-height: 160px; overflow-y: auto; font-size: 0.88rem; display: grid; gap: 0.25rem; }
.history li { color: var(--muted); }
.history li b { color: var(--ink); }
#chat-log li b { color: var(--accent); }
.chat form { margin-top: 0.5rem; }

/* ---------- Rack ---------- */
.rack-bar {
  position: sticky; bottom: 0;
  display: flex; align-items: center; justify-content: center; flex-wrap: wrap;
  gap: 1rem; padding: 0.8rem 1rem 1rem; background: var(--panel);
  padding-bottom: calc(1rem + env(safe-area-inset-bottom)); /* iOS home bar */
  box-shadow: 0 -8px 24px rgba(0,0,0,0.35);
  z-index: 5;
}
.rack {
  display: flex; gap: 6px; padding: 8px; border-radius: 12px;
  background: #5b3d1e; min-height: 64px; min-width: 64px;
  box-shadow: inset 0 2px 6px rgba(0,0,0,0.4);
}
.controls { display: flex; gap: 0.5rem; flex-wrap: wrap; }

.tile {
  width: 48px; height: 48px; flex: 0 0 auto;
  cursor: grab; touch-action: none;
  user-select: none; -webkit-user-select: none; -webkit-touch-callout: none;
}
.tile.selected { outline: 3px solid var(--accent); border-radius: 6px; }
.tile.ghost {
  position: fixed; pointer-events: none; z-index: 1000;
  opacity: 0.9; transform: translate(-50%, -50%) scale(1.1);
}

.game-msg { text-align: center; min-height: 1.4em; margin: 0.4rem; font-weight: 600; }
.game-msg.good { color: var(--good); }
.game-msg.bad { color: var(--bad); }

@media (max-width: 860px) {
  .layout { grid-template-columns: 1fr; gap: 0.75rem; padding: 0.6rem; }
  .sidebar { order: 2; }
  /* keep the side panels from dominating the screen on a phone */
  .history, #chat-log { max-height: 110px; }
}

/* phones: bigger tap targets, board/rack come first, sidebar after */
@media (max-width: 560px), (pointer: coarse) {
  .topbar { padding: 0.5rem 0.7rem; font-size: 0.9rem; }
  button { padding: 0.7rem 0.9rem; min-height: 44px; } /* iOS touch target */
  .controls { width: 100%; justify-content: center; }
  .controls button { flex: 1 1 30%; }
  .rack { gap: 4px; padding: 6px; }
  .rack-bar { gap: 0.6rem; padding: 0.6rem 0.6rem; }
}

/* very small screens: shrink rack tiles so 7 fit without wrapping */
@media (max-width: 380px) {
  .tile { width: 40px; height: 40px; }
}

@media (prefers-reduced-motion: reduce) {
  button { transition: none; }
}

/* ---------- Light mode ---------- */
:root { color-scheme: dark; }
:root[data-theme="light"] {
  color-scheme: light;
  --bg: #eef1f5;
  --panel: #ffffff;
  --panel-2: #e9eef3;
  --ink: #1f2933;
  --muted: #5d6b78;
  --cell-line: #c4ccd4;
}

/* ---------- Icons & icon buttons ---------- */
.ico {
  width: 18px; height: 18px;
  fill: none; stroke: currentColor; stroke-width: 2;
  stroke-linecap: round; stroke-linejoin: round;
}
.controls button { display: inline-flex; align-items: center; gap: 6px; }
.icon-btn { display: inline-grid; place-items: center; padding: 0.55rem; }
.icon-btn .ico { width: 20px; height: 20px; }

.theme-toggle {
  position: fixed; top: 8px; right: 10px; z-index: 60;
  width: 40px; height: 40px; border-radius: 50%;
  background: var(--panel-2); color: var(--ink);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.3);
}
.theme-toggle .ico { width: 20px; height: 20px; }
.topbar { padding-right: 62px; } /* clear the fixed theme toggle */

#btn-dir.active { outline: 2px solid var(--accent); }

/* ---------- Selection order badge on rack tiles ---------- */
.tile { position: relative; }
.tile .badge {
  position: absolute; top: -6px; left: -6px;
  min-width: 18px; height: 18px; padding: 0 4px;
  border-radius: 9px; background: var(--accent); color: var(--accent-ink);
  font: 800 11px/18px system-ui, sans-serif; text-align: center;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.4);
}

/* ---------- Lobby logo ---------- */
.logo { display: block; margin: 0 auto 0.25rem; width: 96px; height: 96px; border-radius: 18px; }

/* ---------- Invite link ---------- */
.invite { margin-top: 0.85rem; }
.invite label { display: block; font-size: 0.8rem; color: var(--muted); margin-bottom: 0.35rem; }
.invite label strong { color: var(--ink); letter-spacing: 0.12em; }
.invite-row { display: flex; gap: 0.4rem; }
.invite-row input { flex: 1; font-size: 0.85rem; }
#btn-copy { flex: 0 0 auto; }
#btn-copy.copied { background: var(--good); color: #06210f; }

/* ---------- Reconnect banner ---------- */
.reconnect {
  position: fixed; left: 50%; bottom: 16px; transform: translateX(-50%);
  z-index: 70; display: flex; align-items: center; gap: 0.8rem;
  background: var(--panel); color: var(--ink);
  padding: 0.7rem 1rem; border-radius: 12px;
  box-shadow: 0 10px 34px rgba(0, 0, 0, 0.5); border: 1px solid var(--bad);
  padding-bottom: calc(0.7rem + env(safe-area-inset-bottom));
}
.reconnect.hidden { display: none; }
