:root {
  --bg: #0f1420;
  --panel: #1a2233;
  --panel2: #222c42;
  --text: #e8ecf4;
  --muted: #93a0b8;
  --accent: #4f8ef7;
  --accent2: #34c77b;
  --danger: #f06262;
  --gold: #f3c14b;
  --die-bg: #f7f5ee;
  --die-pip: #1c2333;
  --radius: 12px;
}
* { box-sizing: border-box; margin: 0; padding: 0; -webkit-tap-highlight-color: transparent; }
html, body { height: 100%; }
body {
  background: var(--bg);
  color: var(--text);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  line-height: 1.45;
}

#topbar {
  display: flex; align-items: center; justify-content: space-between;
  padding: 10px 16px; background: var(--panel);
  position: sticky; top: 0; z-index: 20;
  box-shadow: 0 2px 8px rgba(0,0,0,.35);
}
.link-btn { background: none; border: none; color: var(--text); font-size: 16px; font-weight: 700; cursor: pointer; }

.view { max-width: 1100px; margin: 0 auto; padding: 16px; }
.hidden { display: none !important; }

/* ---------- Home ---------- */
.home-hero { text-align: center; padding: 40px 10px 24px; }
.home-hero h1 { font-size: clamp(28px, 6vw, 44px); }
.tagline { color: var(--muted); margin-top: 8px; font-size: 18px; }
.mode-cards { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; max-width: 760px; margin: 20px auto; }
@media (max-width: 640px) { .mode-cards { grid-template-columns: 1fr; } }
.mode-card {
  background: var(--panel); border: 2px solid transparent; border-radius: var(--radius);
  padding: 28px 20px; text-align: left; color: var(--text); cursor: pointer;
  display: flex; flex-direction: column; gap: 8px; transition: border-color .15s, transform .15s;
}
.mode-card:hover { border-color: var(--accent); transform: translateY(-2px); }
.mode-emoji { font-size: 40px; }
.mode-title { font-size: 22px; font-weight: 800; }
.mode-desc { color: var(--muted); font-size: 15px; }

/* ---------- Buttons / forms ---------- */
button.primary, button.secondary {
  border: none; border-radius: 10px; padding: 10px 18px; font-size: 16px; font-weight: 700;
  cursor: pointer; color: #fff; touch-action: manipulation;
}
button.primary { background: var(--accent); }
button.primary:disabled { background: #38445e; color: #7d8aa5; cursor: default; }
button.secondary { background: var(--panel2); }
button.big { padding: 14px 22px; font-size: 18px; }
.panel { background: var(--panel); border-radius: var(--radius); padding: 22px; }
.panel.narrow { max-width: 420px; margin: 24px auto; display: flex; flex-direction: column; gap: 14px; }
.panel label { display: flex; flex-direction: column; gap: 6px; font-weight: 600; }
.panel input {
  background: var(--bg); border: 1px solid #33405c; color: var(--text);
  border-radius: 8px; padding: 12px; font-size: 18px;
}
.divider { text-align: center; color: var(--muted); }
.error { color: var(--danger); min-height: 1.2em; }
.muted { color: var(--muted); }
.room-code { color: var(--gold); letter-spacing: 4px; font-family: ui-monospace, monospace; }
.player-list { list-style: none; display: flex; flex-direction: column; gap: 6px; }
.player-list li { background: var(--panel2); border-radius: 8px; padding: 10px 12px; }
.player-list .off { opacity: .5; }

/* ---------- Game layout ---------- */
.game-layout { display: grid; grid-template-columns: minmax(300px, 1fr) minmax(320px, 1.2fr); gap: 20px; }
@media (max-width: 860px) { .game-layout { grid-template-columns: 1fr; } }
.play-col { display: flex; flex-direction: column; gap: 14px; }
.turn-banner {
  background: var(--panel); border-radius: var(--radius); padding: 12px 16px;
  font-size: 18px; font-weight: 800; text-align: center;
}
.turn-banner.you { background: #1e3a2a; color: var(--accent2); }
.rolls-left { color: var(--muted); font-weight: 700; }
.controls { display: flex; align-items: center; gap: 14px; flex-wrap: wrap; }
.hint-toggle { color: var(--muted); display: flex; gap: 6px; align-items: center; font-size: 14px; margin-left: auto; }

/* ---------- Dice ---------- */
.dice-row { display: flex; gap: 10px; justify-content: center; min-height: 74px; flex-wrap: wrap; }
.die {
  width: clamp(52px, 13vw, 68px); height: clamp(52px, 13vw, 68px);
  background: var(--die-bg); border-radius: 12px; position: relative; cursor: pointer;
  box-shadow: 0 3px 6px rgba(0,0,0,.4); border: 3px solid transparent;
  display: grid; grid-template: repeat(3, 1fr) / repeat(3, 1fr); padding: 8px;
  transition: transform .12s, border-color .12s;
  user-select: none;
}
.die.held { border-color: var(--accent2); transform: translateY(-6px); }
.die.held::after {
  content: 'HELD'; position: absolute; bottom: -20px; left: 0; right: 0;
  text-align: center; font-size: 10px; font-weight: 800; color: var(--accent2);
}
.die.suggest { border-color: var(--gold); }
.die .pip { width: 72%; height: 72%; background: var(--die-pip); border-radius: 50%; place-self: center; visibility: hidden; }
.die[data-v="1"] .pip:nth-child(5),
.die[data-v="2"] .pip:nth-child(1), .die[data-v="2"] .pip:nth-child(9),
.die[data-v="3"] .pip:nth-child(1), .die[data-v="3"] .pip:nth-child(5), .die[data-v="3"] .pip:nth-child(9),
.die[data-v="4"] .pip:nth-child(1), .die[data-v="4"] .pip:nth-child(3), .die[data-v="4"] .pip:nth-child(7), .die[data-v="4"] .pip:nth-child(9),
.die[data-v="5"] .pip:nth-child(1), .die[data-v="5"] .pip:nth-child(3), .die[data-v="5"] .pip:nth-child(5), .die[data-v="5"] .pip:nth-child(7), .die[data-v="5"] .pip:nth-child(9),
.die[data-v="6"] .pip:nth-child(1), .die[data-v="6"] .pip:nth-child(3), .die[data-v="6"] .pip:nth-child(4), .die[data-v="6"] .pip:nth-child(6), .die[data-v="6"] .pip:nth-child(7), .die[data-v="6"] .pip:nth-child(9)
{ visibility: visible; }
@keyframes shake { 0%,100%{transform:rotate(0)} 25%{transform:rotate(-8deg)} 75%{transform:rotate(8deg)} }
.die.rolling { animation: shake .35s ease; }

/* ---------- Hint box ---------- */
.hint-box {
  background: #2b2416; border: 1px solid var(--gold); border-radius: var(--radius);
  padding: 12px 14px; font-size: 15px;
}
.hint-box .hint-title { color: var(--gold); font-weight: 800; margin-bottom: 4px; }
.ai-log { background: var(--panel); border-radius: var(--radius); padding: 12px 14px; font-size: 14px; color: var(--muted); }
.ai-log b { color: var(--text); }

/* ---------- Scorecards ---------- */
.scorecards { display: flex; flex-direction: column; gap: 14px; }
.scorecard { background: var(--panel); border-radius: var(--radius); overflow: hidden; }
.scorecard .sc-head {
  display: flex; justify-content: space-between; padding: 10px 14px;
  font-weight: 800; background: var(--panel2);
}
.scorecard .sc-head .sc-total { color: var(--gold); }
.scorecard.active .sc-head { background: #274160; }
.sc-grid { display: grid; grid-template-columns: 1fr 1fr; }
.sc-cell {
  display: flex; justify-content: space-between; align-items: center;
  padding: 7px 12px; border-top: 1px solid #2a344e;
  font-size: 14px; min-height: 38px;
}
.sc-cell .cat { color: var(--muted); }
.sc-cell .val { font-weight: 700; min-width: 28px; text-align: right; }
.sc-cell.open .val { color: #536180; }
.sc-cell.scorable { cursor: pointer; background: #21324e; }
.sc-cell.scorable:hover { background: #2a4062; }
.sc-cell.scorable .val { color: var(--accent2); }
.sc-cell.best-pick { outline: 2px solid var(--gold); outline-offset: -2px; }
.sc-cell.zero-pick .val { color: var(--danger); }
.sc-foot { display: flex; justify-content: space-around; padding: 8px 12px; background: var(--panel2); font-size: 13px; color: var(--muted); }
.sc-foot b { color: var(--text); }

/* ---------- Modal ---------- */
.modal {
  position: fixed; inset: 0; background: rgba(5,8,15,.75); z-index: 50;
  display: flex; align-items: center; justify-content: center; padding: 16px;
}
.modal-body {
  background: var(--panel); border-radius: var(--radius); padding: 24px;
  max-width: 560px; width: 100%; max-height: 85vh; overflow-y: auto;
}
.modal-body h2 { margin-bottom: 12px; }
.modal-actions { display: flex; gap: 12px; margin-top: 18px; }
.review-item { background: var(--panel2); border-radius: 8px; padding: 10px 12px; margin: 8px 0; font-size: 14px; }
.review-item .loss { color: var(--danger); font-weight: 700; }
.grade { font-size: 40px; font-weight: 900; color: var(--gold); }

/* ---------- Chat ---------- */
.chat-fab {
  position: fixed; right: 16px; bottom: 16px; z-index: 30;
  width: 56px; height: 56px; border-radius: 50%; border: none;
  background: var(--accent); font-size: 24px; cursor: pointer; color: #fff;
  box-shadow: 0 4px 12px rgba(0,0,0,.5);
}
.chat-badge {
  position: absolute; top: -4px; right: -4px; background: var(--danger); color: #fff;
  border-radius: 10px; font-size: 12px; font-weight: 800; padding: 2px 7px;
}
.chat-panel {
  position: fixed; right: 16px; bottom: 84px; z-index: 40;
  width: min(360px, calc(100vw - 32px)); height: min(440px, 60vh);
  background: var(--panel); border-radius: var(--radius);
  display: flex; flex-direction: column; box-shadow: 0 8px 30px rgba(0,0,0,.6);
}
.chat-head { display: flex; justify-content: space-between; padding: 10px 14px; font-weight: 800; background: var(--panel2); border-radius: var(--radius) var(--radius) 0 0; }
.chat-msgs { flex: 1; overflow-y: auto; padding: 10px 14px; display: flex; flex-direction: column; gap: 6px; font-size: 14px; }
.chat-msgs .sysmsg { color: var(--muted); font-style: italic; font-size: 13px; }
.chat-msgs .from { font-weight: 800; color: var(--accent); }
.chat-form { display: flex; gap: 8px; padding: 10px; }
.chat-form input { flex: 1; background: var(--bg); border: 1px solid #33405c; color: var(--text); border-radius: 8px; padding: 10px; font-size: 15px; }

@media (max-width: 640px) {
  .view { padding: 10px; }
  .sc-cell { padding: 6px 10px; font-size: 13px; min-height: 34px; }
  button.big { width: 100%; }
  .controls { flex-direction: column; align-items: stretch; }
  .hint-toggle { margin: 0 auto; }
}
