:root {
  --bg: #ffffff;
  --panel: #ffffff;
  --text: #111111;
  --muted: #6b7280;
  --line: #e5e7eb;      /* light borders (cards, inputs) */
  --grid: #111111;      /* tic-tac-toe board lines */
  --fill: #111111;      /* marked cell fill */
  --accent: #111111;    /* primary button */
  --good: #16a34a;
  --amber: #b45309;
  --bad: #dc2626;
  --radius: 12px;
  --font: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
body {
  font-family: var(--font);
  color: var(--text);
  background: var(--bg);
  min-height: 100vh;
  -webkit-font-smoothing: antialiased;
}

.screen { min-height: 100vh; }
[hidden] { display: none !important; }

/* ---------- shared ---------- */
.card {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 18px;
}
h1, h2, h3 { color: var(--text); }
.muted { color: var(--muted); }
.small { font-size: 13px; }
.center { text-align: center; }
.error { color: var(--bad); font-size: 14px; margin: 6px 0 0; }

.btn {
  font-family: inherit;
  font-size: 15px;
  font-weight: 600;
  border: 1px solid var(--line);
  border-radius: 9px;
  padding: 10px 16px;
  cursor: pointer;
  transition: transform 0.06s ease, background 0.15s, border-color 0.15s, color 0.15s;
  color: var(--text);
  background: #fff;
}
.btn:active { transform: translateY(1px) scale(0.99); }
.btn-primary { background: var(--accent); color: #fff; border-color: var(--accent); }
.btn-primary:hover { background: #000; }
.btn-ghost { background: #fff; border: 1px solid var(--line); color: var(--muted); }
.btn-ghost:hover { color: var(--text); border-color: var(--text); }
.btn-lg { padding: 13px 22px; font-size: 16px; width: 100%; }
.btn-sm { padding: 6px 12px; font-size: 13px; }
.btn:disabled { opacity: 0.45; cursor: not-allowed; }

/* ---------- join ---------- */
#screen-join {
  display: flex; flex-direction: column;
  align-items: center; justify-content: center;
  gap: 18px; padding: 24px;
}
.join-card { width: 100%; max-width: 380px; text-align: center; }
.logo { font-size: 30px; margin: 4px 0 6px; letter-spacing: -0.5px; display: flex; align-items: center; justify-content: center; gap: 10px; }
.logo-mark { width: 40px; height: 40px; flex-shrink: 0; }
.tagline { color: var(--muted); margin: 0 0 20px; font-size: 14px; }
.field { display: block; text-align: left; margin-bottom: 14px; }
.field > span { display: block; font-size: 13px; color: var(--muted); margin-bottom: 6px; }
.field input, .phrase-form input {
  width: 100%; padding: 12px 14px; font-size: 15px;
  background: #fff; border: 1px solid var(--line);
  border-radius: 9px; color: var(--text); font-family: inherit;
}
.field input:focus, .phrase-form input:focus { outline: none; border-color: var(--text); }
.footer-note { color: var(--muted); font-size: 12px; }
.footer-note a { color: var(--text); text-decoration: underline; text-underline-offset: 2px; }

/* ---------- how it works ---------- */
.how-it-works {
  width: 100%; max-width: 380px;
  border: 1px solid var(--line); border-radius: var(--radius); background: #fff;
}
.how-it-works summary {
  cursor: pointer; padding: 13px 16px; font-weight: 600; font-size: 14px;
  list-style: none; display: flex; align-items: center; justify-content: space-between;
}
.how-it-works summary::-webkit-details-marker { display: none; }
.how-it-works summary::after { content: "+"; color: var(--muted); font-size: 18px; line-height: 1; }
.how-it-works[open] summary::after { content: "\2212"; }
.how-steps { margin: 0; padding: 2px 18px 6px 36px; display: flex; flex-direction: column; gap: 9px; }
.how-steps li { font-size: 13px; color: var(--text); line-height: 1.45; }
.how-steps li::marker { color: var(--muted); font-weight: 700; }
.how-note { padding: 0 16px 14px; margin: 6px 0 0; text-align: left; }

/* ---------- game layout ---------- */
#screen-game {
  display: grid;
  grid-template-columns: 1fr 280px;
  grid-template-rows: auto 1fr;
  grid-template-areas: "top top" "main side";
  gap: 18px; padding: 18px;
  max-width: 1040px; margin: 0 auto;
}
.topbar {
  grid-area: top;
  display: flex; align-items: center; justify-content: space-between;
  gap: 12px; flex-wrap: wrap;
  padding-bottom: 14px; border-bottom: 1px solid var(--line);
}
.topbar-left, .topbar-right { display: flex; align-items: center; gap: 10px; }
.key-chip, .state-chip, .online-chip {
  background: #fff; border: 1px solid var(--line);
  padding: 6px 12px; border-radius: 999px; font-size: 13px; color: var(--muted);
}
.key-chip b { color: var(--text); }
.state-chip.lobby { color: var(--amber); border-color: var(--amber); }
.state-chip.playing { color: var(--good); border-color: var(--good); }
.state-chip.finished { color: var(--text); border-color: var(--text); }
.game-main { grid-area: main; display: flex; flex-direction: column; gap: 16px; }
.leaderboard { grid-area: side; align-self: start; }

/* ---------- lobby ---------- */
.admin-config { margin-top: 12px; display: flex; flex-direction: column; gap: 12px; }
.config-row { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.config-label { font-size: 14px; color: var(--muted); }
.seg { display: inline-flex; background: #f3f4f6; border: 1px solid var(--line); border-radius: 9px; padding: 3px; }
.seg-btn { background: transparent; border: none; color: var(--muted); padding: 7px 14px; border-radius: 6px; cursor: pointer; font-weight: 600; font-family: inherit; }
.seg-btn.active { background: var(--text); color: #fff; }

.card-head { display: flex; align-items: center; justify-content: space-between; gap: 10px; flex-wrap: wrap; }
.card-actions { display: flex; gap: 6px; flex-wrap: wrap; }
.card-inputs { display: grid; gap: 8px; margin-top: 12px; }
.card-input {
  width: 100%; padding: 9px 10px; font-size: 13px; line-height: 1.35;
  background: #fff; border: 1px solid var(--line); border-radius: 8px;
  color: var(--text); font-family: inherit;
  min-height: 4.6em;          /* ~3 lines */
  resize: vertical;
  white-space: pre-wrap; word-break: break-word; overflow-wrap: anywhere;
}
.card-input:focus { outline: none; border-color: var(--text); }
.card-input::placeholder { color: #9ca3af; }

.key-actions { display: flex; gap: 8px; margin-top: 8px; }
.key-actions .btn { flex: 1; }

/* ---------- invite / share (lobby) ---------- */
.invite-card { display: flex; flex-direction: column; gap: 10px; }
.invite-top { display: flex; align-items: center; justify-content: space-between; gap: 10px; flex-wrap: wrap; }
.invite-key {
  font-family: ui-monospace, Menlo, Consolas, monospace; font-size: 12.5px;
  background: #f3f4f6; border: 1px solid var(--line); border-radius: 8px;
  padding: 6px 10px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
  max-width: 62%; cursor: default;
}
.invite-actions { display: flex; gap: 8px; flex-wrap: wrap; }
.invite-actions .btn { flex: 1; min-width: 100px; display: inline-flex; align-items: center; justify-content: center; gap: 6px; }
.btn-wa { background: #25d366; border-color: #25d366; color: #ffffff; }
.btn-wa:hover { filter: brightness(1.04); }
.key-chip { cursor: pointer; }

.start-bar { display: flex; flex-direction: column; gap: 6px; align-items: center; }

/* ---------- language switch ---------- */
.lang-switch { display: inline-flex; background: #f3f4f6; border: 1px solid var(--line); border-radius: 999px; padding: 2px; }
.lang-fixed { position: fixed; top: 14px; right: 14px; z-index: 30; }
.lang-btn {
  background: transparent; border: none; color: var(--muted);
  padding: 5px 10px; border-radius: 999px; cursor: pointer;
  font-family: inherit; font-size: 12px; font-weight: 600;
}
.lang-btn.active { background: var(--text); color: #fff; }

/* ---------- board (tic-tac-toe: inner grid lines only) ---------- */
.board-wrap { display: flex; flex-direction: column; gap: 12px; align-items: center; }
.board {
  display: grid;
  gap: 3px;                 /* the gaps become the black grid lines */
  background: var(--grid);  /* shows through the gaps only (no outer padding) */
  padding: 0;
  width: 100%;
  aspect-ratio: 1 / 1;
  max-width: 540px;
}
.cell {
  background: #fff;
  border: none;
  border-radius: 0;
  padding: 8px;
  cursor: pointer;
  display: flex; align-items: center; justify-content: center; text-align: center;
  font-size: clamp(11px, 1.7vw, 15px); line-height: 1.18;
  color: var(--text); font-family: inherit; position: relative;
  transition: background 0.12s, color 0.12s;
  overflow: hidden; word-break: break-word;
}
.cell:hover { background: #f3f4f6; }
.cell.marked { background: var(--fill); color: #fff; font-weight: 600; }
.cell.marked:hover { background: #000; }
.cell.marked::after {
  content: "✓"; position: absolute; top: 4px; right: 6px;
  font-size: 12px; opacity: 0.85;
}

/* branded free center cell */
.cell.free {
  background: #fff; color: var(--text); text-decoration: none; cursor: pointer;
  flex-direction: column; gap: 4px; padding: 4px;
}
.cell.free .brand-img {
  width: 70%; max-width: 92px; aspect-ratio: 1 / 1; object-fit: contain;
}
.cell.free .brand-name {
  font-size: clamp(9px, 1.3vw, 12px); color: var(--text);
  letter-spacing: 0.2px; line-height: 1; text-decoration: underline; text-underline-offset: 2px;
}
.cell.free:hover { background: #f3f4f6; }
.cell.free:hover .brand-img { transform: scale(1.04); transition: transform 0.12s ease; }

.winner-banner {
  background: #fff; border: 2px solid var(--text); border-radius: var(--radius);
  padding: 16px; text-align: center; font-size: 18px; font-weight: 700; color: var(--text);
}
.winner-banner .sub { font-size: 13px; font-weight: 400; color: var(--muted); margin-top: 4px; }
.admin-play-bar { display: flex; justify-content: center; }

/* ---------- leaderboard ---------- */
.lb-list { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 8px; }
.lb-item { display: flex; align-items: center; gap: 8px; }
.lb-name { flex: 1; font-size: 14px; display: flex; align-items: center; gap: 6px; min-width: 0; }
.lb-name b { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-weight: 600; }
.lb-badge { font-size: 11px; }
.lb-bar { height: 7px; border-radius: 999px; background: #eef0f2; overflow: hidden; width: 70px; flex-shrink: 0; }
.lb-bar > i { display: block; height: 100%; background: var(--text); }
.lb-count { font-size: 12px; color: var(--muted); width: 40px; text-align: right; }
.lb-item.me .lb-name b { text-decoration: underline; text-underline-offset: 2px; }
.lb-item.winner .lb-bar > i { background: var(--good); }
.lb-item.winner .lb-count { color: var(--good); font-weight: 600; }
.lb-status { font-size: 12px; color: var(--muted); white-space: nowrap; }
.lb-status.ok { color: var(--good); font-weight: 600; }

/* ---------- toast + confetti ---------- */
.toast {
  position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%);
  background: var(--text); border: 1px solid var(--text); color: #fff;
  padding: 10px 16px; border-radius: 9px; font-size: 14px; z-index: 50;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.18); max-width: 90vw;
}
.confetti { position: fixed; inset: 0; pointer-events: none; z-index: 40; overflow: hidden; }
.confetti i {
  position: absolute; top: -12px; width: 9px; height: 14px; opacity: 0.9;
  animation: fall linear forwards;
}
@keyframes fall {
  to { transform: translateY(105vh) rotate(720deg); opacity: 0.2; }
}

/* ---------- responsive ---------- */
@media (max-width: 800px) {
  #screen-game {
    grid-template-columns: 1fr;
    grid-template-areas: "top" "main" "side";
  }
  .leaderboard { order: 3; }
}
