/* ===== Tokens ===== */
:root {
  --bg: #fafafa;
  --surface: #ffffff;
  --surface-2: #f5f5f5;
  --text: #0a0a0a;
  --muted: #525252;
  --border: #e5e5e5;
  --border-strong: #d4d4d4;
  --accent: #0a0a0a;
  --accent-fg: #fafafa;
  --danger: #b91c1c;
  --success: #16a34a;
  --leader: #0a0a0a;
  --leader-bg: rgba(10, 10, 10, 0.04);
  --shadow: 0 1px 2px rgba(0,0,0,0.04), 0 4px 16px rgba(0,0,0,0.06);
  --radius: 12px;
  --radius-sm: 8px;
  --gap: 16px;
  --tap: 44px;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0a0a0a;
    --surface: #111111;
    --surface-2: #171717;
    --text: #fafafa;
    --muted: #a3a3a3;
    --border: #262626;
    --border-strong: #404040;
    --accent: #fafafa;
    --accent-fg: #0a0a0a;
    --danger: #f87171;
    --success: #4ade80;
    --leader: #fafafa;
    --leader-bg: rgba(250, 250, 250, 0.06);
    --shadow: 0 1px 2px rgba(0,0,0,0.4), 0 4px 16px rgba(0,0,0,0.3);
  }
}

/* ===== Base ===== */
*, *::before, *::after { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  font-family: "Geist", -apple-system, BlinkMacSystemFont, "Segoe UI", system-ui, sans-serif;
  font-size: 16px;
  line-height: 1.5;
  color: var(--text);
  background: var(--bg);
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  min-height: 100dvh;
}
code, .mono { font-family: "Geist Mono", ui-monospace, SFMono-Regular, Menlo, monospace; }

button, input, select, textarea {
  font: inherit;
  color: inherit;
}

button { cursor: pointer; background: none; border: 0; padding: 0; }
:focus { outline: none; }
:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
  border-radius: 4px;
}

.skip {
  position: absolute; left: -9999px;
  padding: 8px 12px; background: var(--accent); color: var(--accent-fg);
  z-index: 999; border-radius: var(--radius-sm);
}
.skip:focus { left: 8px; top: 8px; }

[hidden] { display: none !important; }

/* ===== Header ===== */
.app-header {
  display: flex; align-items: center; justify-content: space-between;
  padding: 12px 16px;
  border-bottom: 1px solid var(--border);
  background: var(--surface);
  position: sticky; top: 0; z-index: 10;
  padding-top: max(12px, env(safe-area-inset-top));
}
.brand { display: flex; align-items: center; gap: 10px; font-weight: 600; }
.brand-mark {
  width: 28px; height: 28px;
  display: inline-flex; align-items: center; justify-content: center;
  background: var(--accent); color: var(--accent-fg);
  border-radius: 8px; font-weight: 700; font-size: 16px;
}
.brand-name { font-size: 15px; letter-spacing: -0.01em; }

.header-actions { display: flex; gap: 4px; }

.icon-btn {
  min-width: var(--tap); min-height: var(--tap);
  display: inline-flex; align-items: center; justify-content: center;
  border-radius: var(--radius-sm);
  color: var(--text);
  transition: background-color 0.12s;
}
.icon-btn:hover { background: var(--surface-2); }
.icon-btn:active { background: var(--border); }
.icon-btn svg { width: 20px; height: 20px; }

/* ===== Main ===== */
.main {
  max-width: 720px; margin: 0 auto;
  padding: 16px 16px max(16px, env(safe-area-inset-bottom));
  display: flex; flex-direction: column; gap: 20px;
}

.active-game-pill {
  align-self: flex-start;
  display: inline-flex; align-items: center; gap: 8px;
  padding: 6px 12px 6px 10px;
  border: 1px solid var(--border);
  border-radius: 999px;
  background: var(--surface);
  font-size: 13px;
  color: var(--muted);
  transition: border-color 0.12s;
}
.active-game-pill:hover { border-color: var(--border-strong); color: var(--text); }
.status-dot {
  width: 8px; height: 8px; border-radius: 50%;
  background: var(--success);
  box-shadow: 0 0 0 3px color-mix(in srgb, var(--success) 25%, transparent);
}
.active-game-name { color: var(--text); font-weight: 500; }
.active-game-sync { color: var(--muted); font-size: 12px; }

/* ===== Scoreboard ===== */
.scoreboard {
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  gap: 8px;
  align-items: stretch;
  margin-top: 4px;
}
.party {
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 20px 16px;
  background: var(--surface);
  display: flex; flex-direction: column; gap: 6px;
  text-align: center;
  transition: border-color 0.15s, background-color 0.15s, transform 0.15s;
}
.party.leader {
  border-color: var(--leader);
  background: var(--leader-bg);
  box-shadow: 0 0 0 2px var(--leader);
}
.party-name {
  font-size: 13px;
  color: var(--muted);
  letter-spacing: -0.005em;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.party-points {
  font-family: "Geist", -apple-system, BlinkMacSystemFont, system-ui, sans-serif;
  font-size: clamp(48px, 13vw, 72px);
  font-weight: 600;
  letter-spacing: -0.04em;
  line-height: 1;
  font-variant-numeric: tabular-nums;
}
.diff {
  display: flex; flex-direction: column; justify-content: center;
  padding: 0 8px;
  text-align: center;
}
.diff-label { font-size: 11px; color: var(--muted); text-transform: uppercase; letter-spacing: 0.06em; }
.diff-value {
  font-family: "Geist", -apple-system, BlinkMacSystemFont, system-ui, sans-serif;
  font-size: clamp(20px, 5vw, 28px);
  font-weight: 500;
  font-variant-numeric: tabular-nums;
  color: var(--text);
  margin-top: 4px;
}

.banner {
  padding: 12px 16px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  font-size: 14px;
  display: flex; align-items: center; gap: 8px;
}

/* ===== Actions ===== */
.actions { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; margin-top: 4px; }
@media (max-width: 400px) {
  .actions { grid-template-columns: 1fr; }
}

button.primary, button.ghost {
  min-height: var(--tap);
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  padding: 10px 16px;
  border-radius: var(--radius-sm);
  font-size: 15px; font-weight: 500;
  transition: background-color 0.12s, border-color 0.12s, opacity 0.12s;
}
button.primary {
  background: var(--accent); color: var(--accent-fg);
  border: 1px solid var(--accent);
}
button.primary:hover { opacity: 0.88; }
button.primary:active { opacity: 0.78; }
button.ghost {
  background: var(--surface); color: var(--text);
  border: 1px solid var(--border);
}
button.ghost:hover { border-color: var(--border-strong); background: var(--surface-2); }
button.ghost.danger { color: var(--danger); }
button.ghost.danger:hover { border-color: var(--danger); }

button[disabled], button[aria-disabled="true"] { opacity: 0.45; cursor: not-allowed; }
button[disabled]:hover, button[aria-disabled="true"]:hover { background: var(--surface) !important; }

button svg { width: 18px; height: 18px; }

.hint {
  display: inline-flex; align-items: center; gap: 8px;
  padding: 8px 12px; border-radius: var(--radius-sm);
  background: var(--surface-2); color: var(--muted);
  font-size: 13px;
}
.hint svg { width: 16px; height: 16px; }

.readonly-block {
  display: flex; flex-direction: column; gap: 8px;
  align-items: flex-start;
}

/* ===== Toast ===== */
.toast {
  position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%);
  background: var(--accent); color: var(--accent-fg);
  padding: 10px 16px; border-radius: 999px;
  font-size: 14px; box-shadow: var(--shadow);
  z-index: 200; max-width: 92vw;
}

/* ===== Dialogs ===== */
dialog.dlg { display: none; } /* native <dialog> default; explicit for clarity */
.dlg[open] {
  border: 0; padding: 0; background: var(--surface); color: var(--text);
  border-radius: 16px 16px 0 0;
  width: 100%; max-width: 480px;
  margin: auto auto 0; /* bottom-sheet on mobile */
  box-shadow: var(--shadow);
  max-height: 90dvh; overflow: hidden;
  display: flex; flex-direction: column;
}
.dlg.dlg-wide[open] { max-width: 560px; }
.dlg::backdrop {
  background: rgba(0,0,0,0.5);
  backdrop-filter: blur(2px);
}
.dlg[open] { animation: slideUp 0.18s ease-out; }
@keyframes slideUp {
  from { transform: translateY(20px); opacity: 0; }
  to { transform: translateY(0); opacity: 1; }
}
@media (min-width: 600px) {
  .dlg[open] {
    border-radius: 16px;
    margin: auto;
  }
  @keyframes slideUp {
    from { transform: scale(0.96); opacity: 0; }
    to { transform: scale(1); opacity: 1; }
  }
}

.dlg-head {
  display: flex; align-items: center; justify-content: space-between;
  padding: 16px 16px 8px;
  flex-shrink: 0;
}
.dlg-head h2 { margin: 0; font-size: 18px; font-weight: 600; letter-spacing: -0.01em; }

.dlg-form {
  display: flex; flex-direction: column;
  gap: 14px;
  padding: 0 16px 16px;
  overflow-y: auto;
}
.dlg > *:not(.dlg-head):not(.dlg-foot) { overflow-y: auto; }

.dlg-foot {
  display: flex; gap: 10px; justify-content: flex-end;
  padding: 14px 16px;
  border-top: 1px solid var(--border);
  background: var(--surface);
  flex-shrink: 0;
  padding-bottom: max(14px, env(safe-area-inset-bottom));
}
.dlg-foot.column { flex-direction: column; align-items: stretch; gap: 8px; }
.dlg-foot.hint-foot { font-size: 12px; color: var(--muted); }
.dlg-foot.hint-foot p { margin: 0; }
.dlg-foot.hint-foot strong { color: var(--text); margin-right: 4px; }

.field-group {
  border: 0; padding: 0; margin: 0;
  display: flex; flex-direction: column; gap: 10px;
}
.field-group legend {
  font-size: 13px; font-weight: 500; color: var(--muted);
  text-transform: uppercase; letter-spacing: 0.06em;
  padding: 0 0 4px;
}

.field { display: flex; flex-direction: column; gap: 6px; font-size: 14px; }
.field > span { color: var(--muted); font-size: 13px; }

input[type="text"], input[type="number"], select {
  min-height: var(--tap);
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--surface);
  color: var(--text);
  transition: border-color 0.12s;
  width: 100%;
}
input[type="text"]:focus-visible, input[type="number"]:focus-visible, select:focus-visible {
  border-color: var(--accent);
  outline-offset: 0;
}
input[type="number"] { font-family: "Geist Mono", ui-monospace, monospace; font-variant-numeric: tabular-nums; }

.radio {
  display: grid;
  grid-template-columns: auto 1fr auto;
  gap: 10px; align-items: center;
  padding: 8px 10px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  cursor: pointer;
  transition: border-color 0.12s, background-color 0.12s;
}
.radio:has(input:checked) { border-color: var(--accent); background: var(--surface-2); }
.radio input[type="radio"] { width: 18px; height: 18px; accent-color: var(--accent); }
.radio input[type="number"] { width: 110px; }

.checkbox {
  display: flex; align-items: center; gap: 10px;
  font-size: 14px;
}
.checkbox input { width: 20px; height: 20px; accent-color: var(--accent); }

.preview {
  padding: 10px 12px;
  background: var(--surface-2);
  border-radius: var(--radius-sm);
  font-size: 14px;
  color: var(--muted);
  font-family: "Geist Mono", ui-monospace, monospace;
  font-variant-numeric: tabular-nums;
  min-height: 40px;
  display: flex; align-items: center;
}

/* ===== History ===== */
.history-toolbar {
  padding: 0 16px 8px;
  display: flex; justify-content: flex-end;
}
.history-list {
  list-style: none; margin: 0;
  padding: 0 16px 16px;
  display: flex; flex-direction: column; gap: 6px;
}
.history-item {
  display: grid;
  grid-template-columns: auto 1fr auto;
  gap: 10px; align-items: center;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  font-size: 14px;
}
.history-item .ts { font-size: 12px; color: var(--muted); font-family: "Geist Mono", monospace; }
.history-item .desc { color: var(--text); display: flex; align-items: center; gap: 8px; min-width: 0; }
.history-item .desc .dev {
  display: inline-block;
  padding: 1px 6px;
  border: 1px solid var(--border);
  border-radius: 999px;
  font-size: 11px;
  color: var(--muted);
  background: var(--surface-2);
  white-space: nowrap;
  flex-shrink: 0;
}
.history-item .desc .dev.mono { font-family: "Geist Mono", monospace; font-size: 10px; }
.history-item .val { font-family: "Geist Mono", monospace; font-weight: 600; font-variant-numeric: tabular-nums; }

.field-hint { font-size: 12px; color: var(--muted); margin: -4px 0 0; line-height: 1.4; }
.empty {
  padding: 24px 16px;
  text-align: center;
  color: var(--muted);
  font-size: 14px;
}

/* ===== Games dialog ===== */
.games-list {
  list-style: none; margin: 0;
  padding: 0 16px;
  display: flex; flex-direction: column; gap: 8px;
}
.game-row {
  display: grid;
  grid-template-columns: auto 1fr auto;
  gap: 10px;
  align-items: center;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--surface);
  transition: border-color 0.12s;
}
.game-row.active {
  border-color: var(--accent);
  background: var(--leader-bg);
}
.game-row .game-info {
  display: flex; flex-direction: column; gap: 2px; min-width: 0;
}
.game-row .game-name {
  font-weight: 500;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.game-row .game-name input {
  width: 100%; padding: 4px 8px; min-height: 32px;
}
.game-row .game-id {
  font-family: "Geist Mono", monospace;
  font-size: 12px;
  color: var(--muted);
  overflow: hidden; text-overflow: ellipsis;
}
.game-row-actions { display: flex; gap: 4px; }
.game-row-actions .text-btn {
  padding: 6px 10px;
  min-height: 32px;
  font-size: 13px;
  border-radius: 6px;
  border: 1px solid var(--border);
  color: var(--muted);
  background: var(--surface);
  transition: border-color 0.12s, color 0.12s;
}
.game-row-actions .text-btn:hover { color: var(--text); border-color: var(--border-strong); }
.game-row-actions .text-btn.danger:hover { color: var(--danger); border-color: var(--danger); }

.games-actions {
  display: grid; grid-template-columns: 1fr 1fr; gap: 8px;
  padding: 14px 16px;
}

/* ===== Keys dialog ===== */
.keys-body { padding: 8px 16px 16px; display: flex; flex-direction: column; gap: 10px; }
.key-row {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 8px;
  align-items: center;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
}
.key-row > .key-label {
  grid-column: 1 / -1;
  font-size: 12px; color: var(--muted);
  text-transform: uppercase; letter-spacing: 0.06em;
}
.key-row > .key-value {
  font-family: "Geist Mono", monospace;
  font-size: 13px;
  word-break: break-all;
  user-select: all;
}
.key-row > .key-value.secret { letter-spacing: 0.05em; }

/* ===== Rules dialog ===== */
.dlg-head-actions { display: flex; gap: 4px; }
.rules-intro { margin: 0 16px 8px; }
.rules-list {
  list-style: none; margin: 0;
  padding: 0 16px 16px;
  display: flex; flex-direction: column; gap: 10px;
  counter-reset: rule;
  overflow-y: auto;
}
.rule-item {
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px 16px;
  background: var(--surface);
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 6px 12px;
  transition: border-color 0.15s, background-color 0.15s;
}
.rule-item.speaking { border-color: var(--accent); background: var(--leader-bg); }
.rule-item .rule-title {
  font-weight: 600;
  font-size: 15px;
  letter-spacing: -0.01em;
  display: flex; align-items: center; gap: 8px;
}
.rule-item .rule-title::before {
  counter-increment: rule;
  content: counter(rule);
  font-family: "Geist Mono", monospace;
  font-size: 12px;
  color: var(--muted);
  background: var(--surface-2);
  border-radius: 6px;
  padding: 2px 6px;
  min-width: 24px;
  text-align: center;
  font-weight: 500;
}
.rule-item .rule-body {
  grid-column: 1 / 2;
  font-size: 14px;
  color: var(--muted);
  line-height: 1.55;
}
.rule-item .rule-speak {
  grid-row: 1 / 2;
  grid-column: 2 / 3;
  align-self: start;
}

/* ===== Reduced motion ===== */
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.001ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.001ms !important;
  }
}
