:root {
  --red: #d7263d;
  --green: #1f9d55;
  --yellow: #f2b705;
  --blue: #1e63c8;
  --bg: #0a0e1a;
  --glow: #1f2d5c;
  --panel: rgba(255, 255, 255, 0.06);
  --line: rgba(255, 255, 255, 0.12);
  --cream: #eef1f8;
  --gold: #ffcc33;
  --ink: #10131f;
  --cur: var(--gold);
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
[hidden] { display: none !important; }

html, body { height: 100%; margin: 0; }
body {
  background:
    radial-gradient(ellipse 80% 55% at 50% -10%, var(--glow) 0%, transparent 70%),
    radial-gradient(ellipse 60% 40% at 100% 110%, color-mix(in srgb, var(--gold) 14%, transparent) 0%, transparent 70%),
    var(--bg);
  background-attachment: fixed;
  color: var(--cream);
  font-family: "Segoe UI Variable Display", "SF Pro Display", system-ui, -apple-system, Roboto, sans-serif;
  -webkit-font-smoothing: antialiased;
  overscroll-behavior: none;
  user-select: none;
  -webkit-user-select: none;
}

#app { height: 100dvh; display: flex; justify-content: center; overflow: hidden; }
.screen {
  width: 100%;
  max-width: 760px;
  padding: max(12px, env(safe-area-inset-top)) 12px max(12px, env(safe-area-inset-bottom));
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 10px;
}

.logo { margin: 0; font-size: 2.8rem; font-weight: 800; letter-spacing: -0.02em; }
.logo span {
  background: linear-gradient(120deg, var(--gold), color-mix(in srgb, var(--gold) 55%, #ff5e8a));
  -webkit-background-clip: text; background-clip: text; color: transparent;
}
.logo.small { font-size: 1.3rem; white-space: nowrap; }
.tagline { margin: 6px 0 0; font-size: 0.75rem; font-weight: 700; letter-spacing: 0.14em; text-transform: uppercase; opacity: 0.55; }

/* Frosted-glass surface shared by panels, cards and buttons. */
.btn, .icon-btn, .seat, .piece-card, .chip, .dialog {
  background: var(--panel);
  border: 1px solid var(--line);
  backdrop-filter: blur(14px); -webkit-backdrop-filter: blur(14px);
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.25), inset 0 1px 0 rgba(255, 255, 255, 0.06);
}

/* ---------- buttons ---------- */
.btn {
  font: inherit; font-weight: 700; color: var(--cream);
  border-radius: 999px; padding: 13px 24px; min-width: 220px; cursor: pointer;
  transition: transform 0.15s, border-color 0.2s, background 0.2s;
}
.btn:hover { border-color: color-mix(in srgb, var(--gold) 60%, transparent); }
.btn:active { transform: scale(0.97); }
.btn.primary {
  background: linear-gradient(135deg, var(--gold), color-mix(in srgb, var(--gold) 60%, #ff7a45));
  border-color: transparent; color: var(--ink);
  box-shadow: 0 10px 28px color-mix(in srgb, var(--gold) 35%, transparent);
}
.btn.small { min-width: 0; padding: 8px 14px; font-size: 0.8rem; white-space: nowrap; }
.icon-btn {
  font-size: 1.05rem; width: 38px; height: 38px; border-radius: 999px; cursor: pointer;
}
.icon-btn[aria-pressed="false"] { opacity: 0.45; }
#view[aria-pressed="true"] { border-color: var(--gold); color: var(--gold); }
button:focus-visible { outline: 3px solid var(--cream); outline-offset: 2px; }

/* ---------- setup ---------- */
#setup { justify-content: safe center; overflow-y: auto; }
#setup > * { flex-shrink: 0; }
.seat.red .seg { --c: var(--red); } .seat.green .seg { --c: var(--green); }
.seat.yellow .seg { --c: var(--yellow); } .seat.blue .seg { --c: var(--blue); }
.setup-head { width: 100%; max-width: 440px; display: flex; flex-direction: column; align-items: center; gap: 10px; }
.head-actions { display: flex; gap: 8px; }
.seats { width: 100%; max-width: 440px; display: grid; gap: 8px; margin: 2px 0 6px; }
.seat {
  display: flex; align-items: center; gap: 10px; min-width: 0;
  border-radius: 18px; padding: 8px 8px 8px 8px; transition: opacity 0.2s;
}
.seat.off { opacity: 0.5; }
.avatar {
  flex: none; width: 42px; height: 42px; border-radius: 50%; font-size: 1.35rem; cursor: pointer;
  background: color-mix(in srgb, var(--c) 22%, transparent); border: 2px solid var(--c);
  box-shadow: 0 0 14px color-mix(in srgb, var(--c) 45%, transparent);
}
.seat-input {
  flex: 1; min-width: 0; width: 0; font: inherit; font-weight: 700; color: var(--cream);
  background: transparent; border: 0; border-bottom: 1px solid transparent; padding: 6px 2px; outline: none;
}
.seat-input::placeholder { color: var(--cream); opacity: 0.55; }
.seat-input:focus { border-bottom-color: var(--c); }
.seat .seg button { padding: 7px 9px; font-size: 0.75rem; }

.tabs {
  width: 100%; max-width: 440px; display: flex; gap: 4px; padding: 4px; border-radius: 999px;
  background: rgba(0, 0, 0, 0.28); border: 1px solid var(--line);
}
.tabs button {
  flex: 1; font: inherit; font-weight: 700; font-size: 0.85rem; color: var(--cream); opacity: 0.6;
  background: transparent; border: 0; border-radius: 999px; padding: 9px; cursor: pointer;
}
.tabs button[aria-selected="true"] { opacity: 1; background: var(--panel); box-shadow: inset 0 0 0 1px var(--line); color: var(--gold); }
.tab-panels { width: 100%; max-width: 440px; min-height: 150px; }
.tab-panel { margin: 0 auto; }
.tabs button { padding: 9px 4px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.settings-panel { width: 100%; }
.panel-head { margin: 4px 2px 2px; font-size: 0.78rem; letter-spacing: 0.14em; text-transform: uppercase; opacity: 0.7; }
.panel-head ~ .panel-head { margin-top: 18px; }
.panel-help { margin: 0 2px 10px; font-size: 0.85rem; opacity: 0.65; }
.piece-cards.flags .piece-card img { aspect-ratio: 3 / 2; object-fit: cover; border-radius: 6px; box-shadow: 0 2px 8px rgba(0, 0, 0, 0.4); }
.piece-cards.flags .piece-card span { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 100%; }
#settings-slot .field:last-child { border-bottom: 0; }

.rules { display: grid; gap: 8px; }
.rule {
  display: flex; align-items: center; justify-content: space-between; gap: 10px;
  padding: 10px 12px; border-radius: 16px; background: var(--panel); border: 1px solid var(--line);
}
.rule strong { display: block; font-size: 0.88rem; }
.rule small { display: block; opacity: 0.6; font-size: 0.75rem; margin-top: 2px; }
.rule .seg { flex: none; }
.setup-actions { display: flex; flex-direction: column; gap: 8px; align-items: center; }

.piece-cards { width: 100%; max-width: 440px; display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; }
.piece-cards.three { gap: 8px; }
.piece-cards.three .piece-card { font-size: 0.8rem; padding: 8px 6px; }
.piece-cards.four { grid-template-columns: repeat(4, 1fr); gap: 8px; }
.piece-cards.four .piece-card { font-size: 0.8rem; padding: 6px 4px 8px; }
.piece-card {
  font: inherit; font-weight: 700; font-size: 0.85rem; color: var(--cream); cursor: pointer;
  border-radius: 18px; padding: 10px 6px 10px;
  display: flex; flex-direction: column; align-items: center; gap: 6px;
  transition: transform 0.15s, border-color 0.2s, background 0.2s;
}
.piece-card img { width: 100%; max-width: 96px; aspect-ratio: 1; object-fit: contain; }
.piece-card:hover { transform: translateY(-2px); }
.piece-card[aria-pressed="true"] {
  border-color: var(--gold); color: var(--gold);
  background: color-mix(in srgb, var(--gold) 12%, transparent);
  box-shadow: 0 0 0 1px var(--gold), 0 10px 30px color-mix(in srgb, var(--gold) 25%, transparent);
}
.seg { --c: var(--gold); --on: var(--ink); display: flex; gap: 2px; padding: 3px; border-radius: 999px; background: rgba(0, 0, 0, 0.25); }
.seat .seg { --on: #fff; }
.seg button {
  font: inherit; font-size: 0.8rem; font-weight: 600; color: var(--cream); opacity: 0.7;
  background: transparent; border: 0; padding: 7px 12px; border-radius: 999px; cursor: pointer;
  transition: background 0.2s, opacity 0.2s;
}
.seg button[aria-pressed="true"] { background: var(--c); color: var(--on); opacity: 1; box-shadow: 0 4px 14px color-mix(in srgb, var(--c) 45%, transparent); }
.seat.yellow .seg button[aria-pressed="true"] { color: var(--ink); }
.setup-error { margin: 0; min-height: 1.2em; color: #ff9d8a; font-weight: 600; }

/* ---------- game chrome ---------- */
.topbar { width: 100%; display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.topbar-actions { display: flex; gap: 8px; align-items: center; }

.chips { width: 100%; display: flex; gap: 6px; }
.chip {
  flex: 1; min-width: 0; padding: 7px 10px; border-radius: 14px;
  opacity: 0.55; transition: opacity 0.2s, transform 0.2s, box-shadow 0.2s, border-color 0.2s;
}
.chip.active {
  opacity: 1; border-color: var(--c); transform: translateY(-2px);
  box-shadow: 0 0 0 1px var(--c), 0 8px 26px color-mix(in srgb, var(--c) 40%, transparent);
}
.chip.done { opacity: 0.85; }
.chip-name { font-weight: 700; font-size: 0.85rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.chip-name small { font-weight: 600; opacity: 0.7; }
.chip-dots { display: flex; gap: 3px; margin-top: 4px; }
.chip-dots i { width: 9px; height: 9px; border-radius: 50%; border: 2px solid var(--c); }
.chip-dots i.on { background: var(--c); }

/* ---------- 3D scene: fills whatever space is left; view3d.js fits the board into it ---------- */
.scene { flex: 1; min-height: 0; width: 100%; position: relative; }
.scene canvas { position: absolute; inset: 0; width: 100%; height: 100%; display: block; touch-action: manipulation; cursor: pointer; }

/* Pop-up words over the board */
.pop {
  position: absolute; left: 50%; top: 34%; z-index: 3; pointer-events: none; white-space: nowrap;
  font-size: clamp(2.2rem, 11vw, 4.2rem); font-weight: 900; letter-spacing: 0.04em;
  color: var(--c);
  -webkit-text-stroke: 0.1em #fff; paint-order: stroke fill;
  text-shadow: 0 0.09em 0 rgba(0, 0, 0, 0.45), 0 0 0.5em var(--c);
  animation: pop 1.15s cubic-bezier(0.2, 0.9, 0.3, 1) forwards;
}
@keyframes pop {
  0% { transform: translate(-50%, -50%) rotate(var(--tilt)) scale(0); opacity: 0; }
  18% { transform: translate(-50%, -50%) rotate(var(--tilt)) scale(1.3); opacity: 1; }
  30% { transform: translate(-50%, -50%) rotate(var(--tilt)) scale(0.92); }
  42% { transform: translate(-50%, -50%) rotate(var(--tilt)) scale(1.05); }
  75% { transform: translate(-50%, -62%) rotate(var(--tilt)) scale(1); opacity: 1; }
  100% { transform: translate(-50%, -120%) rotate(var(--tilt)) scale(0.9); opacity: 0; }
}

.confetti {
  position: absolute; top: -20px; width: 10px; height: 16px; border-radius: 2px; pointer-events: none;
  animation: confetti linear infinite;
}
@keyframes confetti {
  to { transform: translate(var(--drift), 105vh) rotate(900deg); }
}

.controls { display: flex; flex-direction: column; align-items: center; gap: 8px; width: 100%; }
.msg { margin: 0; min-height: 1.3em; font-weight: 700; text-align: center; }
.btn.roll {
  font-size: 1.05rem; letter-spacing: 0.02em; padding: 15px 22px; min-width: 240px;
  background: linear-gradient(135deg, var(--cur), color-mix(in srgb, var(--cur) 65%, #000));
  border-color: transparent; color: #fff;
  box-shadow: 0 10px 30px color-mix(in srgb, var(--cur) 45%, transparent), inset 0 1px 0 rgba(255, 255, 255, 0.25);
  transition: background 0.3s, opacity 0.2s;
}
.btn.roll:disabled { opacity: 0.3; cursor: default; box-shadow: none; }
.btn.roll:not(:disabled) { animation: bob 0.9s ease-in-out infinite; }
@keyframes bob { 50% { transform: translateY(-4px); } }

/* ---------- game over ---------- */
.overlay {
  position: fixed; inset: 0; background: color-mix(in srgb, var(--bg) 55%, transparent);
  display: grid; place-items: center; padding: 16px; z-index: 20; overflow: hidden;
}
.dialog { position: relative; z-index: 1; animation: dialogIn 0.5s cubic-bezier(0.2, 1.4, 0.4, 1); }
@keyframes dialogIn { from { transform: scale(0.4); opacity: 0; } }
.dialog {
  background: color-mix(in srgb, var(--bg) 92%, transparent);
  backdrop-filter: blur(24px); -webkit-backdrop-filter: blur(24px);
  border-radius: 24px; padding: 26px 22px; width: 100%; max-width: 340px;
  display: flex; flex-direction: column; align-items: center; gap: 12px;
}
.dialog h2 { margin: 0; font-size: 1.7rem; font-weight: 800; letter-spacing: -0.01em; }
.ranks { list-style: none; margin: 0; padding: 0; width: 100%; display: grid; gap: 6px; }
.ranks li {
  display: flex; gap: 10px; align-items: center; font-weight: 700;
  padding: 10px 14px; border-radius: 14px; background: rgba(255, 255, 255, 0.05);
  border: 1px solid color-mix(in srgb, var(--c) 45%, transparent);
}

/* ---------- sheets ---------- */
.sheet { z-index: 30; backdrop-filter: blur(6px); -webkit-backdrop-filter: blur(6px); }
.sheet .dialog { gap: 10px; }
.dialog.wide { max-width: 400px; max-height: calc(100dvh - 32px); overflow-y: auto; align-items: stretch; }
.dialog.wide h2 { text-align: center; }
.dialog .btn { min-width: 0; width: 100%; }
.field {
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: 8px 2px; border-bottom: 1px solid var(--line); font-weight: 600; font-size: 0.9rem;
}
.field select {
  font: inherit; color: var(--cream); background: var(--panel); border: 1px solid var(--line);
  border-radius: 999px; padding: 6px 12px;
}
.field select option { color: #111; }
.field input[type="range"] { width: 55%; accent-color: var(--gold); }
.how { margin: 0; padding-left: 1.3em; display: grid; gap: 8px; line-height: 1.45; font-size: 0.92rem; }
.how li::marker { color: var(--gold); font-weight: 800; }
.stats { width: 100%; border-collapse: collapse; font-size: 0.9rem; }
.stats th { text-align: right; font-size: 0.72rem; text-transform: uppercase; letter-spacing: 0.08em; opacity: 0.6; padding: 4px 6px; }
.stats td { padding: 8px 6px; border-top: 1px solid var(--line); text-align: right; }
.stats td:first-child { text-align: left; font-weight: 700; }
.empty { text-align: center; opacity: 0.65; }

@media (max-width: 380px) {
  .seat .seg button { padding: 6px 7px; font-size: 0.7rem; }
  .avatar { width: 36px; height: 36px; font-size: 1.15rem; }
}

@media (prefers-reduced-motion: reduce) {
  .btn.roll:not(:disabled) { animation: none; }
  .confetti { display: none; }
}
