/* ── Reset & variables ─────────────────────────────────────────────── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

/* Dark theme (default) */
:root {
  --bg:         #0d0d1a;
  --surface:    #14142b;
  --surface-2:  #1e1e3a;
  --surface-3:  #272748;
  --accent:     #7c3aed;
  --accent-h:   #6d28d9;
  --danger:     #ef4444;
  --success:    #22c55e;
  --text:       #e8e8f0;
  --text-muted: #8888aa;
  --border:     #2a2a4a;
  --r:          12px;
  --r-sm:       8px;
  --r-xs:       6px;
  font-size: 16px;
}

/* Light theme */
[data-theme="light"] {
  --bg:         #f2f2fa;
  --surface:    #ffffff;
  --surface-2:  #ebebf5;
  --surface-3:  #e0e0ee;
  --accent:     #7c3aed;
  --accent-h:   #6d28d9;
  --danger:     #dc2626;
  --success:    #16a34a;
  --text:       #1a1a2e;
  --text-muted: #60608a;
  --border:     #c8c8e0;
}

/* Respect system preference when no explicit choice has been made */
@media (prefers-color-scheme: light) {
  :root:not([data-theme="dark"]) {
    --bg:         #f2f2fa;
    --surface:    #ffffff;
    --surface-2:  #ebebf5;
    --surface-3:  #e0e0ee;
    --accent:     #7c3aed;
    --accent-h:   #6d28d9;
    --danger:     #dc2626;
    --success:    #16a34a;
    --text:       #1a1a2e;
    --text-muted: #60608a;
    --border:     #c8c8e0;
  }
}

/* Theme toggle button */
#theme-toggle {
  font-size: 1rem;
  padding: 0.4rem 0.6rem;
  line-height: 1;
}

html, body {
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: system-ui, -apple-system, sans-serif;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
}

/* ── Accessibility ─────────────────────────────────────────────────── */
.sr-only {
  position: absolute;
  width: 1px; height: 1px;
  padding: 0; margin: -1px;
  overflow: hidden;
  clip: rect(0,0,0,0);
  white-space: nowrap;
  border: 0;
}

/* Focus ring visible for keyboard navigation */
:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

/* High contrast: boost text on low-contrast backgrounds */
@media (prefers-contrast: more) {
  :root {
    --text-muted: #c0c0d8;
    --border: #5a5a7a;
  }
}

/* ── Utilities ─────────────────────────────────────────────────────── */
.hidden { display: none !important; }

/* ── Buttons ───────────────────────────────────────────────────────── */
button {
  cursor: pointer;
  border: none;
  font: inherit;
  border-radius: var(--r-sm);
  padding: 0.6rem 1.2rem;
  transition: background 0.15s, transform 0.1s;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
}
button:active { transform: scale(0.96); }
button:disabled { opacity: 0.5; cursor: not-allowed; }

.btn-primary {
  background: var(--accent);
  color: #fff;
  font-weight: 600;
  font-size: 1rem;
}
.btn-primary:hover:not(:disabled) { background: var(--accent-h); }

.btn-ghost {
  background: transparent;
  color: var(--text-muted);
  border: 1px solid var(--border);
}
.btn-ghost:hover:not(:disabled) { background: var(--surface-2); color: var(--text); }

/* ── Inputs ────────────────────────────────────────────────────────── */
input[type="text"], input[type="color"] {
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: var(--r-sm);
  color: var(--text);
  font: inherit;
  padding: 0.65rem 0.9rem;
  width: 100%;
  transition: border-color 0.15s;
  outline: none;
}
input[type="text"]:focus { border-color: var(--accent); }
input[type="text"]::placeholder { color: var(--text-muted); }
input[type="checkbox"] { accent-color: var(--accent); width: 1.1rem; height: 1.1rem; cursor: pointer; flex-shrink: 0; }

label { display: flex; flex-direction: column; gap: 0.35rem; font-size: 0.9rem; color: var(--text-muted); }
label span { color: var(--text); }

/* ── ════════════════════════════════════════════════════════════════ */
/* ── HOME PAGE ─────────────────────────────────────────────────────── */

.home-wrap {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 1.5rem;
}

.home-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--r);
  padding: 2rem 1.75rem;
  width: 100%;
  max-width: 400px;
  display: flex;
  flex-direction: column;
  gap: 1.5rem;
}

.home-logo { text-align: center; }
.home-logo h1 {
  font-size: 2rem;
  font-weight: 800;
  letter-spacing: -0.5px;
  background: linear-gradient(135deg, #a78bfa, #ec4899);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}
.home-logo p { color: var(--text-muted); font-size: 0.9rem; margin-top: 0.25rem; }

.home-fields { display: flex; flex-direction: column; gap: 1rem; }

.color-row { display: flex; align-items: center; gap: 0.75rem; }
.color-row input[type="color"] { width: 48px; height: 40px; padding: 2px 4px; flex-shrink: 0; }
.color-row input[type="text"] { flex: 1; }

.avatar-btn {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  background: var(--surface-2);
  border: 1px dashed var(--border);
  border-radius: var(--r-sm);
  padding: 0.65rem 0.9rem;
  width: 100%;
  color: var(--text-muted);
  font-size: 0.9rem;
  cursor: pointer;
  transition: border-color 0.15s, color 0.15s;
}
.avatar-btn:hover, .avatar-btn:focus-visible {
  border-color: var(--accent);
  color: var(--text);
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}
.avatar-btn img { width: 28px; height: 28px; border-radius: 50%; object-fit: cover; }
#avatar-input { display: none; }

.home-card .btn-primary { width: 100%; padding: 0.85rem; font-size: 1.05rem; }

/* ── ════════════════════════════════════════════════════════════════ */
/* ── BOARD PAGE ────────────────────────────────────────────────────── */

.board-layout {
  display: grid;
  grid-template-rows: auto auto 1fr;
  min-height: 100vh;
}

/* Header */
.board-header {
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  padding: 0.75rem 1rem;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
  position: sticky;
  top: 0;
  z-index: 10;
}

.room-name {
  font-weight: 700;
  font-size: 1.05rem;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  max-width: 50vw;
}
.player-count-label { color: var(--text-muted); font-weight: 400; font-size: 0.8rem; }

.header-actions { display: flex; gap: 0.5rem; flex-shrink: 0; }
.header-actions button { padding: 0.45rem 0.85rem; font-size: 0.85rem; }

/* Controls panel */
.board-controls {
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  padding: 1rem;
  display: flex;
  flex-direction: column;
  gap: 0.9rem;
}

/* ── Dice tabs ─────────────────────────────────────────────────────── */
.dice-tab-nav {
  display: flex;
  gap: 0.25rem;
  margin-bottom: 0.5rem;
}
.tab-btn {
  flex: 1;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 6px 6px 0 0;
  color: var(--text-muted);
  cursor: pointer;
  font-size: 0.78rem;
  font-weight: 600;
  padding: 0.35rem 0.5rem;
  transition: background 0.15s, color 0.15s;
}
.tab-btn:hover { background: var(--surface-3); color: var(--text); }
.tab-btn.tab-active {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}
.tab-btn:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

/* ── Dice grid ─────────────────────────────────────────────────────── */
/* auto-fill so it wraps naturally at any container width.
   No scrollbar on desktop sidebar (300px → ~5 cols) or mobile (full width → 4-5 cols). */
.dice-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(54px, 1fr));
  gap: 0.5rem;
}

.die-control {
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: var(--r-sm);
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 0.45rem 0.25rem 0.35rem;
  gap: 0.3rem;
}
.die-control.has-count { border-color: var(--accent); }

.die-label {
  font-size: 0.7rem;
  font-weight: 700;
  color: var(--text-muted);
  letter-spacing: 0.4px;
  text-transform: uppercase;
  line-height: 1;
}
.die-control.has-count .die-label { color: var(--accent); }

.die-count {
  font-size: 1.1rem;
  font-weight: 800;
  min-width: 1.4rem;
  text-align: center;
  line-height: 1;
}

.die-btns { display: flex; gap: 0.2rem; }

.die-btn {
  background: var(--surface-3);
  border: 1px solid transparent;
  border-radius: 4px;
  color: var(--text-muted);
  font-size: 0.85rem;
  font-weight: 700;
  padding: 0.15rem 0.4rem;
  line-height: 1.2;
  min-width: 22px;
  text-align: center;
}
.die-btn:hover, .die-btn:focus-visible { background: var(--accent); color: #fff; border-color: var(--accent); }

/* Options row */
.options-row {
  display: flex;
  align-items: center;
  gap: 0.75rem 1rem;
  flex-wrap: wrap;
}
.option-check {
  display: flex;
  align-items: center;
  gap: 0.4rem;
  font-size: 0.9rem;
  color: var(--text-muted);
  cursor: pointer;
  flex-direction: row;
}

.color-swatch {
  width: 26px; height: 26px;
  border-radius: 50%;
  border: 2px solid var(--border);
  cursor: pointer;
  flex-shrink: 0;
  position: relative;
}
.color-swatch input[type="color"] {
  position: absolute; inset: 0;
  opacity: 0; cursor: pointer;
  width: 100%; height: 100%;
  border: none; padding: 0;
}

/* Command input */
.cmd-row {
  display: flex;
  gap: 0.5rem;
}
.cmd-row input[type="text"] {
  flex: 1;
  font-family: ui-monospace, 'Cascadia Code', monospace;
  font-size: 0.875rem;
  padding: 0.55rem 0.8rem;
}
.cmd-row button { flex-shrink: 0; padding: 0.55rem 0.9rem; font-size: 0.875rem; }
.cmd-error { border-color: var(--danger) !important; }

/* Action buttons */
.action-row { display: flex; gap: 0.5rem; align-items: stretch; }
.action-row .btn-primary { flex: 1; }
.btn-roll {
  padding: 0.8rem 1.2rem;
  font-size: 1.1rem;
  font-weight: 700;
  min-height: 3rem;
}
@media (max-width: 500px) {
  .action-row { flex-direction: column; }
  .btn-roll { width: 100%; }
}

/* ── Savage Worlds initiative ──────────────────────────────────────── */
.sw-section {
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: var(--r-sm);
  overflow: hidden;
}

.sw-summary {
  padding: 0.55rem 0.8rem;
  cursor: pointer;
  font-size: 0.875rem;
  font-weight: 600;
  list-style: none;
  display: flex;
  align-items: center;
  gap: 0.5rem;
  user-select: none;
}
.sw-summary::-webkit-details-marker { display: none; }
.sw-arrow { font-size: 0.65rem; transition: transform 0.2s; display: inline-block; }
details[open] .sw-arrow { transform: rotate(90deg); }

.sw-body {
  padding: 0.75rem;
  display: flex;
  flex-direction: column;
  gap: 0.6rem;
  border-top: 1px solid var(--border);
}

.sw-actions { display: flex; gap: 0.5rem; }
.sw-actions button { padding: 0.45rem 0.8rem; font-size: 0.85rem; }

.sw-meta {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  font-size: 0.82rem;
  color: var(--text-muted);
}

.sw-hint {
  font-size: 0.78rem;
  color: var(--text-muted);
  line-height: 1.4;
}

/* ── Help section ───────────────────────────────────────────────────── */
.help-body { flex-direction: column; gap: 0.6rem; }
.help-intro { font-size: 0.82rem; margin: 0; }

.help-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.76rem;
  line-height: 1.4;
}
.help-table th {
  text-align: left;
  color: var(--text-muted);
  font-weight: 600;
  border-bottom: 1px solid var(--border);
  padding: 0.15rem 0.4rem;
}
.help-table td {
  padding: 0.15rem 0.4rem;
  vertical-align: top;
}
.help-table td:first-child { white-space: nowrap; }
.help-table code {
  background: var(--surface-3);
  border-radius: 3px;
  padding: 0.05rem 0.3rem;
  font-size: 0.82em;
  color: var(--accent);
}
.help-table .help-sep {
  color: var(--text-muted);
  font-style: italic;
  padding-top: 0.5rem;
  font-size: 0.73rem;
}

/* Playing card chip */
.card-chip {
  display: inline-flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  background: #f0f0f0;
  color: #111;
  border-radius: 6px;
  width: 44px;
  height: 58px;
  font-weight: 800;
  border: 1px solid #bbb;
  box-shadow: 2px 3px 6px rgba(0,0,0,0.5);
  animation: roll-in 0.35s cubic-bezier(0.34, 1.56, 0.64, 1) both;
  gap: 1px;
  flex-shrink: 0;
}
.card-chip.red   { color: #c0392b; }
.card-chip.joker {
  background: linear-gradient(135deg, #f39c12, #e74c3c);
  color: #fff;
  border-color: gold;
  box-shadow: 2px 3px 6px rgba(0,0,0,0.4), 0 0 8px rgba(255,215,0,0.4);
}
.card-rank { font-size: 0.95rem; line-height: 1; }
.card-suit { font-size: 0.8rem;  line-height: 1; }

.initiative-area {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  margin-bottom: 0.4rem;
}
.initiative-label { font-size: 0.75rem; color: var(--text-muted); }

/* ── Players area ──────────────────────────────────────────────────── */
.board-players {
  padding: 1rem;
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
  overflow-y: auto;
}

/* Player card */
.player-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--r);
  padding: 0.85rem 1rem;
  border-left: 3px solid transparent;
  transition: border-color 0.3s, opacity 0.2s, box-shadow 0.2s;
  touch-action: pan-y; /* allow vertical scroll, pointer events handle drag */
}
.player-card.is-me { box-shadow: 0 0 0 1px var(--accent) inset; }
.player-card.is-dragging {
  opacity: 0.45;
  box-shadow: 0 8px 28px rgba(0,0,0,0.5);
  z-index: 5;
}

/* Drag placeholder (the gap that shows where the card will drop) */
.drag-placeholder {
  background: var(--surface-2);
  border: 2px dashed var(--border);
  border-radius: var(--r);
  margin-bottom: 0; /* height set via JS */
}

/* Drag handle */
.drag-handle {
  cursor: grab;
  color: var(--text-muted);
  font-size: 1rem;
  padding: 0 0.2rem;
  line-height: 1;
  user-select: none;
  touch-action: none;
  flex-shrink: 0;
}
.drag-handle:active { cursor: grabbing; }

.player-header {
  display: flex;
  align-items: center;
  gap: 0.6rem;
  margin-bottom: 0.5rem;
}

.player-avatar {
  width: 32px; height: 32px;
  border-radius: 50%;
  object-fit: cover;
  flex-shrink: 0;
  background: var(--surface-2);
}

.player-name {
  font-weight: 600;
  font-size: 0.95rem;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  flex: 1;
}

.player-badge {
  font-size: 0.7rem;
  color: var(--text-muted);
  background: var(--surface-2);
  border-radius: 99px;
  padding: 0.1rem 0.5rem;
}

/* Counter */
.player-counter {
  display: flex;
  align-items: center;
  gap: 0.4rem;
  margin-bottom: 0.5rem;
}
.counter-val {
  font-size: 1rem;
  font-weight: 700;
  min-width: 2rem;
  text-align: center;
  background: var(--surface-2);
  border-radius: var(--r-xs);
  padding: 0.1rem 0.4rem;
}
.counter-btn {
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: var(--r-xs);
  font-size: 0.9rem;
  padding: 0.15rem 0.5rem;
  color: var(--text);
}

/* Results area */
.player-results {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  align-items: flex-end;
  min-height: 0.5rem;
}

/* Sum display chip */
.result-sum {
  font-size: 1.1rem;
  font-weight: 800;
  padding: 0.1rem 0.6rem;
  background: transparent;
  border-left: 2px solid currentColor;
  margin-left: 0.2rem;
  align-self: flex-end;
  animation: roll-in 0.2s ease-out both;
}

.result-group { display: flex; flex-direction: column; gap: 0.3rem; }

.result-group-label {
  font-size: 0.65rem;
  font-weight: 700;
  text-transform: uppercase;
  color: var(--text-muted);
  letter-spacing: 0.5px;
}

.result-dice-row { display: flex; flex-wrap: wrap; gap: 0.3rem; }

/* Individual die result chip */
.die-chip {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 36px;
  height: 36px;
  border-radius: var(--r-xs);
  font-weight: 800;
  font-size: 1rem;
  animation: roll-in 0.35s cubic-bezier(0.34, 1.56, 0.64, 1) both;
}
/* Shapes: clip-path clips both bg and border, so we use filter:drop-shadow for is-max */
.die-chip.die-d4  { clip-path: polygon(50% 3%, 98% 97%, 2% 97%); border-radius: 0; }
.die-chip.die-d8  { clip-path: polygon(50% 3%, 97% 50%, 50% 97%, 3% 50%); border-radius: 0; }
.die-chip.die-d10 { clip-path: polygon(50% 2%, 95% 32%, 78% 98%, 22% 98%, 5% 32%); border-radius: 0; }
.die-chip.die-d12 { clip-path: polygon(50% 2%, 94% 34%, 78% 96%, 22% 96%, 6% 34%); border-radius: 0; }
.die-chip.die-d20 { clip-path: polygon(50% 2%, 93% 25%, 93% 75%, 50% 98%, 7% 75%, 7% 25%); border-radius: 0; }
.die-chip.die-d100{ clip-path: polygon(50% 2%, 95% 32%, 78% 98%, 22% 98%, 5% 32%); border-radius: 0; font-size: 0.72rem; }
.die-chip.die-d2  { clip-path: circle(46%); border-radius: 0; }
/* d3, d5, d7: triangle-like but rounder */
.die-chip.die-d3  { clip-path: polygon(50% 3%, 98% 97%, 2% 97%); border-radius: 0; }
.die-chip.die-d5  { clip-path: polygon(50% 2%, 97% 37%, 79% 98%, 21% 98%, 3% 37%); border-radius: 0; }
.die-chip.die-d7  { clip-path: polygon(50% 2%, 93% 25%, 93% 75%, 50% 98%, 7% 75%, 7% 25%); border-radius: 0; }
.die-chip.die-d14 { clip-path: polygon(50% 3%, 97% 50%, 50% 97%, 3% 50%); border-radius: 0; }
.die-chip.die-d16 { clip-path: polygon(50% 2%, 84% 2%, 98% 50%, 84% 98%, 50% 98%, 16% 98%, 2% 50%, 16% 2%); border-radius: 0; }
.die-chip.die-d24 { clip-path: polygon(50% 2%, 93% 25%, 93% 75%, 50% 98%, 7% 75%, 7% 25%); border-radius: 0; }
.die-chip.die-d30 { clip-path: polygon(50% 2%, 93% 25%, 93% 75%, 50% 98%, 7% 75%, 7% 25%); border-radius: 0; }
/* Fudge dice: rounded square (keep default) */

.die-chip.is-max {
  filter: drop-shadow(0 0 5px gold) drop-shadow(0 0 2px gold);
}
.die-chip.is-dropped {
  opacity: 0.4;
  text-decoration: line-through;
  filter: grayscale(1);
}
.die-chip.fudge-plus  { font-size: 1.3rem; }
.die-chip.fudge-minus { font-size: 1.3rem; }
.die-chip.fudge-blank { font-size: 0.75rem; opacity: 0.6; }
.result-successes {
  font-size: 0.85rem;
  font-weight: 700;
  padding: 0.1rem 0.5rem;
  align-self: center;
}
.result-formula {
  font-size: 0.75rem;
  opacity: 0.55;
  font-family: monospace;
  margin-bottom: 0.2rem;
}

@keyframes roll-in {
  from { transform: scale(0.2) rotate(180deg); opacity: 0; }
  to   { transform: scale(1)   rotate(0deg);   opacity: 1; }
}

/* ── Connection status ─────────────────────────────────────────────── */
.conn-status {
  position: fixed;
  bottom: 1rem;
  left: 50%;
  transform: translateX(-50%);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 99px;
  padding: 0.4rem 1rem;
  font-size: 0.8rem;
  color: var(--text-muted);
  display: flex;
  align-items: center;
  gap: 0.4rem;
  z-index: 100;
  transition: opacity 0.5s;
}
.conn-dot { width: 8px; height: 8px; border-radius: 50%; background: var(--text-muted); }
.conn-status.connected .conn-dot { background: var(--success); }
.conn-status.error     .conn-dot { background: var(--danger); }
.conn-status.fade-out            { opacity: 0; pointer-events: none; }

/* ── Toast notifications ───────────────────────────────────────────── */
.toast-container {
  position: fixed;
  top: 1rem; right: 1rem;
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
  z-index: 200;
}
.toast {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--r-sm);
  padding: 0.6rem 1rem;
  font-size: 0.875rem;
  max-width: 280px;
  animation: slide-in 0.25s ease-out;
}
.toast.leaving { animation: slide-out 0.25s ease-in forwards; }

@keyframes slide-in  { from { transform: translateX(120%); opacity: 0; } to { transform: none; opacity: 1; } }
@keyframes slide-out { from { opacity: 1; } to { transform: translateX(120%); opacity: 0; } }

/* ── Desktop layout ────────────────────────────────────────────────── */
@media (min-width: 768px) {
  .board-layout {
    grid-template-rows: auto 1fr;
    grid-template-columns: 300px 1fr;
  }
  .board-header { grid-column: 1 / -1; }
  .board-controls {
    border-right: 1px solid var(--border);
    border-bottom: none;
    overflow-y: auto;
    align-self: start;
    position: sticky;
    top: 53px;
    max-height: calc(100vh - 53px);
  }
  .board-players { padding: 1.25rem; }
}
