:root {
  --bg: #f4f1ea; --panel: #ffffff; --text: #1d1d1f; --muted: #6b6b70; --line: #ddd8cc;
  --accent: #15803d; --accent-2: #f59e0b; --danger: #b91c1c; --btn: #f1efe8; --btn-text: #1d1d1f;
  --card: #fffdf8; --card-line: #c9c4b8; --red: #c0392b; --black: #1d1d1f; --blue: #1d4ed8; --green: #15803d;
  --turn: #fbbf24;
  --cw: 64px; --ch: 90px; --cfs: 15px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #121214; --panel: #1c1c1f; --text: #ececf0; --muted: #9a9aa3; --line: #2e2e33;
    --btn: #2a2a2f; --btn-text: #ececf0; --card: #f7f5ef; --card-line: #888; color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #121214; --panel: #1c1c1f; --text: #ececf0; --muted: #9a9aa3; --line: #2e2e33;
  --btn: #2a2a2f; --btn-text: #ececf0; --card: #f7f5ef; --card-line: #888; color-scheme: dark;
}
:root[data-size="s"] { --cw: 50px; --ch: 72px; --cfs: 13px; }
:root[data-size="l"] { --cw: 80px; --ch: 112px; --cfs: 18px; }

* { box-sizing: border-box; }
html, body { margin: 0; background: var(--bg); color: var(--text); font: 15px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif; }
body { min-height: 100vh; overflow-x: hidden; }
button, select, input { font: inherit; color: var(--btn-text); }
button { background: var(--btn); border: 1px solid var(--line); border-radius: 8px; padding: 7px 12px; cursor: pointer; }
button:hover:not(:disabled) { filter: brightness(0.96); }
button:disabled { opacity: 0.45; cursor: not-allowed; }
button.primary:disabled, .actions button.primary:disabled { background: rgba(255,255,255,.12); border-color: rgba(255,255,255,.2); color: inherit; }
.panel button.primary:disabled { background: var(--btn); border-color: var(--line); color: var(--btn-text); }
button.primary { background: var(--accent); border-color: var(--accent); color: #fff; font-weight: 600; }
button.big { padding: 10px 22px; font-size: 1.05rem; }
button.link { background: none; border: none; padding: 2px 4px; color: var(--accent); text-decoration: underline; }
button.link.danger { color: var(--danger); }
button.icon { width: 36px; height: 36px; padding: 0; border-radius: 50%; }
select, input { background: var(--panel); color: var(--text); border: 1px solid var(--line); border-radius: 8px; padding: 6px 8px; max-width: 100%; }
input[type="number"] { width: 90px; }
h1, h2, h3 { margin: 0 0 10px; line-height: 1.2; }
h2 { font-size: 1.2rem; } h3 { font-size: 1rem; margin-top: 14px; }
.muted { color: var(--muted); } .small { font-size: 0.85em; }
.row { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; margin: 8px 0; }
.row.end { justify-content: flex-end; }
.center-msg { padding: 80px 16px; text-align: center; color: var(--muted); }
.tag { font-size: 0.72rem; background: var(--btn); border: 1px solid var(--line); border-radius: 99px; padding: 0 7px; margin-left: 4px; color: var(--muted); vertical-align: middle; }
.dot { display: inline-block; width: 8px; height: 8px; border-radius: 50%; background: #999; margin-right: 6px; }
.dot.on { background: #22c55e; }
.badge { background: var(--danger); color: #fff; border-radius: 99px; padding: 0 6px; font-size: 0.75rem; }

/* header */
header.top { display: flex; align-items: center; gap: 12px; padding: 8px 16px; border-bottom: 1px solid var(--line); background: var(--panel); position: sticky; top: 0; z-index: 20; }
.logo { font-weight: 800; font-size: 1.15rem; color: var(--text); text-decoration: none; }
header nav { margin-left: auto; display: flex; gap: 6px; }
.conn { font-size: 0.8rem; color: var(--muted); }
.conn.connected::before { content: "● "; color: #22c55e; }
.conn.reconnecting::before, .conn.connecting::before { content: "● "; color: var(--accent-2); }
.conn.replaced { color: var(--danger); }

/* panels */
.panel { background: var(--panel); border: 1px solid var(--line); border-radius: 14px; padding: 16px; margin-bottom: 16px; min-width: 0; }
.panel label { display: flex; flex-direction: column; gap: 3px; margin: 8px 0; font-size: 0.9rem; }
.panel button + button { margin-left: 4px; }

/* home */
.home { max-width: 960px; margin: 0 auto; padding: 16px; }
.hero { text-align: center; padding: 24px 0 18px; }
.hero h1 { font-size: 2.6rem; }
.home-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(280px, 1fr)); gap: 16px; }
.home-grid .panel { display: flex; flex-direction: column; gap: 8px; }
.home-grid .panel button { margin-left: 0; }
.recent-row { display: flex; flex-wrap: wrap; gap: 4px 14px; padding: 8px 0; border-bottom: 1px solid var(--line); font-size: 0.9rem; }
.recent-row a { font-family: ui-monospace, monospace; color: var(--accent); font-weight: 600; }

/* lobby */
.lobby { max-width: 1280px; margin: 0 auto; padding: 16px; display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1.3fr) minmax(0, 0.9fr); gap: 16px; align-items: start; }
.share { display: flex; gap: 6px; }
.share input { flex: 1; min-width: 0; font-family: ui-monospace, monospace; font-size: 0.85rem; }
.seats { list-style: none; padding: 0; margin: 0; }
.seats li { display: flex; align-items: center; gap: 6px; padding: 7px 0; border-bottom: 1px solid var(--line); flex-wrap: wrap; }
.seats select { padding: 2px 6px; }
.rules fieldset, .settings fieldset { border: 1px solid var(--line); border-radius: 10px; margin: 0 0 12px; padding: 8px 12px; }
legend { font-weight: 700; padding: 0 6px; }
label.chk { flex-direction: row !important; align-items: flex-start; gap: 8px !important; cursor: pointer; }
label.chk input { margin-top: 3px; }
label.chk small { display: block; color: var(--muted); }
.chips { display: flex; gap: 6px; flex-wrap: wrap; margin: 6px 0; }
.chip { padding: 3px 10px; border-radius: 99px; font-size: 0.85rem; margin: 0 !important; }
.chip.on { background: var(--accent); color: #fff; border-color: var(--accent); }
.look { display: flex; gap: 12px; flex-wrap: wrap; align-items: flex-end; }
.look-preview { padding: 10px 16px; border-radius: 10px; }
.chatpanel { position: sticky; top: 70px; }
.chat { height: 300px; overflow-y: auto; border: 1px solid var(--line); border-radius: 8px; padding: 8px; font-size: 0.9rem; background: var(--bg); }
.chat .msg { margin: 3px 0; word-wrap: break-word; }
.chat .msg.sys { color: var(--muted); font-style: italic; font-size: 0.82rem; }
.chat .msg.mine b { color: var(--accent); }
form.row input { flex: 1; min-width: 0; }

/* felts */
.felt-green { background: radial-gradient(ellipse at center, #1f7a45 0%, #145530 75%); }
.felt-blue { background: radial-gradient(ellipse at center, #1f5f8b 0%, #12395a 75%); }
.felt-red { background: radial-gradient(ellipse at center, #8b2333 0%, #5a1220 75%); }
.felt-purple { background: radial-gradient(ellipse at center, #5b3a8c 0%, #36225a 75%); }
.felt-slate { background: radial-gradient(ellipse at center, #4a5563 0%, #2a313b 75%); }
.felt-wood { background: repeating-linear-gradient(95deg, #8a5a2b 0 14px, #7a4e24 14px 22px, #93623a 22px 40px); }

/* cards */
.card { position: relative; width: var(--cw); height: var(--ch); border-radius: calc(var(--cw) * 0.1); background: var(--card); border: 1px solid var(--card-line);
  box-shadow: 0 1px 3px rgba(0,0,0,.35); color: var(--black); flex: none; user-select: none; -webkit-user-select: none; font-family: Georgia, "Times New Roman", serif; }
.card.s-H, .card.s-D { color: var(--red); }
:root[data-cards="four-color"] .card.s-D { color: var(--blue); }
:root[data-cards="four-color"] .card.s-C { color: var(--green); }
.card.s-J { color: #7c3aed; }
.card .ci { position: absolute; font-size: var(--cfs); line-height: 0.95; text-align: center; font-weight: 700; }
.card .tl { top: 4px; left: 4px; } .card .br { bottom: 4px; right: 4px; transform: rotate(180deg); }
.card .cc { position: absolute; inset: 0; display: flex; align-items: center; justify-content: center; font-size: calc(var(--cfs) * 2.1); }
.card .cc.face { font-weight: 700; font-size: calc(var(--cfs) * 1.9); }
.card .cc.joker { font-size: calc(var(--cfs) * 0.62); writing-mode: vertical-rl; letter-spacing: 2px; font-weight: 700; }
:root[data-cards="large"] .card .ci { font-size: calc(var(--cfs) * 1.45); }
:root[data-cards="large"] .card .br { display: none; }
:root[data-cards="large"] .card .cc { font-size: calc(var(--cfs) * 1.6); top: 30%; }
.card .wild { position: absolute; top: 3px; right: 3px; font: 700 9px system-ui; background: #7c3aed; color: #fff; border-radius: 4px; padding: 0 3px; }
.card.small { --cw: calc(var(--cwbase, 64px) * 0.62); width: calc(var(--cw-s, 40px)); height: calc(var(--ch-s, 56px)); }
.card.small .ci { font-size: calc(var(--cfs) * 0.78); top: 2px; left: 3px; }
.card.small .br, .card.small .cc { display: none; }
.card.small .wild { font-size: 7px; top: auto; bottom: 2px; }
:root { --cw-s: 40px; --ch-s: 56px; }
:root[data-size="s"] { --cw-s: 34px; --ch-s: 48px; }
:root[data-size="l"] { --cw-s: 48px; --ch-s: 68px; }
:root[data-cards="large"] .card.small .ci { font-size: calc(var(--cfs) * 0.9); }
.card.empty { background: transparent; border: 2px dashed rgba(255,255,255,.35); box-shadow: none; }

/* card backs */
.card.back { background-color: #1e3a8a; border: 3px solid #fff;
  background-image: repeating-linear-gradient(45deg, rgba(255,255,255,.15) 0 4px, transparent 4px 8px), repeating-linear-gradient(-45deg, rgba(255,255,255,.15) 0 4px, transparent 4px 8px); }
:root[data-back="classic-red"] .card.back, .card.back[data-b="classic-red"] { background-color: #9f1239; }
:root[data-back="classic-blue"] .card.back, .card.back[data-b="classic-blue"] { background-color: #1e3a8a; }
:root[data-back="dots"] .card.back, .card.back[data-b="dots"] { background-color: #0f766e; background-image: radial-gradient(rgba(255,255,255,.35) 2px, transparent 2.5px); background-size: 9px 9px; }
:root[data-back="stripes"] .card.back, .card.back[data-b="stripes"] { background-color: #b45309; background-image: repeating-linear-gradient(90deg, rgba(255,255,255,.25) 0 3px, transparent 3px 9px); }
:root[data-back="plaid"] .card.back, .card.back[data-b="plaid"] { background-color: #7f1d1d; background-image: repeating-linear-gradient(0deg, rgba(0,0,0,.25) 0 6px, transparent 6px 14px), repeating-linear-gradient(90deg, rgba(255,255,255,.18) 0 6px, transparent 6px 14px); }
:root[data-back="midnight"] .card.back, .card.back[data-b="midnight"] { background-color: #111827; background-image: radial-gradient(circle at 30% 30%, rgba(250,204,21,.5) 1px, transparent 1.5px), radial-gradient(circle at 70% 60%, rgba(255,255,255,.4) 1px, transparent 1.5px); background-size: 14px 14px, 11px 11px; }
.card.back.mini { width: 16px; height: 23px; border-width: 1px; border-radius: 3px; margin-right: -9px; box-shadow: 0 1px 1px rgba(0,0,0,.3); }

/* game layout */
.table-wrap { display: grid; grid-template-columns: minmax(0, 1fr) 300px; gap: 0; min-height: calc(100vh - 53px); }
.table { padding: 10px 14px 14px; display: flex; flex-direction: column; gap: 10px; color: #f5f5f4; min-width: 0; }
.table .muted { color: rgba(255,255,255,.65); }
.table button.link { color: #fde68a; }
.infobar { display: flex; flex-wrap: wrap; gap: 4px 16px; font-size: 0.82rem; opacity: 0.9; align-items: center; }
.opps { display: flex; gap: 10px; flex-wrap: wrap; justify-content: center; }
.opp { background: rgba(0,0,0,.22); border: 2px solid transparent; border-radius: 12px; padding: 8px 12px; min-width: 140px; text-align: center; transition: border-color .2s, box-shadow .2s; }
.opp.turn { border-color: var(--turn); box-shadow: 0 0 0 3px rgba(251,191,36,.35), 0 0 22px rgba(251,191,36,.45); }
.opp-name { font-weight: 700; font-size: 0.92rem; }
.opp .tag { background: rgba(255,255,255,.15); color: #fff; border-color: transparent; }
.opp-cards { display: flex; justify-content: center; padding: 6px 9px 4px 0; min-height: 33px; }
.opp-meta { font-size: 0.78rem; opacity: 0.85; }
.center { display: flex; gap: 18px; align-items: flex-start; flex: 1; min-height: 170px; }
.piles { display: flex; gap: 14px; padding-top: 6px; }
.pile { position: relative; display: flex; flex-direction: column; align-items: center; border-radius: 10px; padding: 4px; cursor: default; }
.pile.can { cursor: pointer; }
.pile.can > .card { box-shadow: 0 0 0 3px var(--turn), 0 0 18px rgba(251,191,36,.6); }
:root[data-anim="on"] .pile.can > .card, :root[data-anim="on"] .discard-pile.can .fan-slot:last-child .card { animation: pulse 1.6s ease-in-out infinite; }
.pile .count { position: absolute; top: -4px; right: -6px; background: rgba(0,0,0,.6); border-radius: 99px; font-size: 0.72rem; padding: 0 6px; }
.pile-label { font-size: 0.75rem; opacity: 0.8; margin-top: 4px; }
.pile.hinted > .card, .meld.hinted { outline: 3px dashed #a7f3d0; outline-offset: 3px; }
.melds { flex: 1; display: flex; flex-wrap: wrap; gap: 10px 16px; align-content: flex-start; min-width: 0; }
.empty-melds { padding-top: 40px; }
.meld-group { display: flex; flex-wrap: wrap; gap: 6px; align-items: center; background: rgba(0,0,0,.15); border-radius: 10px; padding: 6px 8px; }
.meld-owner { width: 100%; font-size: 0.72rem; opacity: 0.8; }
.meld { display: flex; padding: 3px; border-radius: 8px; border: 2px solid transparent; }
.meld .card + .card { margin-left: calc(var(--cw-s) * -0.45); }
.meld.target { border-color: #a7f3d0; cursor: pointer; background: rgba(167,243,208,.15); }
.banner { text-align: center; padding: 8px 12px; background: rgba(0,0,0,.28); border-radius: 10px; font-size: 0.98rem; position: relative; overflow: hidden; }
.banner.mine { background: var(--turn); color: #422006; font-weight: 600; }
:root[data-anim="on"] .banner.mine { animation: glow 1.8s ease-in-out infinite; }
.timer { height: 4px; background: rgba(0,0,0,.25); border-radius: 2px; margin-top: 6px; overflow: hidden; }
.timer i { display: block; height: 100%; width: 100%; background: #22c55e; transition: width .25s linear; }
.timer.low i { background: #ef4444; }
.me { background: rgba(0,0,0,.22); border-radius: 12px; padding: 10px; border: 2px solid transparent; }
.me.turn { border-color: var(--turn); }
.me-bar { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; font-size: 0.9rem; margin-bottom: 8px; }
.me-bar .spacer { flex: 1; }
.me-bar button, .actions button { background: rgba(255,255,255,.12); color: #fff; border-color: rgba(255,255,255,.2); }
.actions button.primary { background: #f59e0b; border-color: #f59e0b; color: #1c1917; }
.hand { display: flex; flex-wrap: wrap; gap: 6px 0; padding: 14px 10px 6px 4px; min-height: calc(var(--ch) + 24px); }
.hand .card { margin-right: calc(var(--cw) * -0.3); cursor: pointer; transition: transform .12s, box-shadow .12s; touch-action: none; }
.hand .card:last-child { margin-right: 0; }
.hand .card:hover { transform: translateY(-6px); z-index: 2; }
.hand .card.sel { transform: translateY(-14px); box-shadow: 0 0 0 3px #38bdf8, 0 6px 12px rgba(0,0,0,.4); z-index: 3; }
.hand .card.fresh { box-shadow: 0 0 0 3px #a78bfa, 0 1px 3px rgba(0,0,0,.35); }
.hand .card.hinted::after { content: ""; position: absolute; left: 6px; right: 6px; bottom: -7px; height: 4px; border-radius: 2px; background: #34d399; }
.hand .card.dragging { z-index: 10; opacity: 0.9; transition: none; box-shadow: 0 10px 20px rgba(0,0,0,.5); }
.actions { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 10px; }
.actions button { margin: 0 !important; }
.hintbox { margin-top: 8px; background: rgba(16,185,129,.18); border: 1px solid rgba(167,243,208,.5); padding: 8px 10px; border-radius: 8px; font-size: 0.9rem; }

/* side panel */
.side { background: var(--panel); border-left: 1px solid var(--line); padding: 10px 12px; display: flex; flex-direction: column; min-width: 0; }
.tabs { display: flex; gap: 4px; margin-bottom: 10px; }
.tabs button { flex: 1; padding: 5px; font-size: 0.88rem; }
.tabs button.on { background: var(--accent); color: #fff; border-color: var(--accent); }
.standings { padding-left: 22px; margin: 0 0 10px; }
.standings li { padding: 4px 0; }
.standings li span { display: inline-block; min-width: 120px; }
.standings li.turn span::after { content: " ◀"; color: var(--accent-2); }
.bar { height: 5px; background: var(--line); border-radius: 3px; overflow: hidden; margin-top: 3px; }
.bar i { display: block; height: 100%; background: var(--accent); }
.scroll { overflow: auto; max-height: 50vh; }
table.hist, table.result { border-collapse: collapse; width: 100%; font-size: 0.85rem; }
.hist th, .hist td, .result th, .result td { padding: 4px 6px; border-bottom: 1px solid var(--line); text-align: right; white-space: nowrap; }
.hist th:first-child, .hist td:first-child, .result td:first-child, .result th:first-child { text-align: left; }
.hist td.won { color: var(--accent); font-weight: 700; }
.hist tfoot td { font-weight: 700; }
.side .chat { height: calc(100vh - 250px); min-height: 200px; }
.log { font-size: 0.84rem; overflow-y: auto; max-height: calc(100vh - 160px); }
.log div { padding: 3px 0; border-bottom: 1px solid var(--line); }

/* modal */
.modal-bg { position: fixed; inset: 0; background: rgba(0,0,0,.5); display: flex; align-items: center; justify-content: center; padding: 16px; z-index: 50; }
.modal { background: var(--panel); color: var(--text); border-radius: 16px; padding: 18px; width: min(720px, 100%); max-height: calc(100vh - 32px); overflow: auto; box-shadow: 0 20px 50px rgba(0,0,0,.4); }
.modal-head { display: flex; align-items: center; justify-content: space-between; gap: 10px; }
.modal label { display: flex; flex-direction: column; gap: 3px; margin: 8px 0; font-size: 0.9rem; }
.modal form.row label { flex-direction: row; align-items: center; gap: 8px; flex: 1; }
.result-title { font-size: 1.15rem; font-weight: 700; }
.result .mini-hand { display: flex; gap: 2px; flex-wrap: wrap; justify-content: flex-start; text-align: left; white-space: normal; }
.result tr.won td { color: var(--accent); font-weight: 600; }
.stat-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(130px, 1fr)); gap: 10px; }
.stat-grid div { background: var(--bg); border-radius: 10px; padding: 10px; text-align: center; }
.stat-grid b { display: block; font-size: 1.6rem; color: var(--accent); }
.stat-grid span { font-size: 0.8rem; color: var(--muted); }

.toast { position: fixed; left: 50%; bottom: 24px; transform: translate(-50%, 30px); opacity: 0; background: #1f2937; color: #fff; padding: 10px 16px; border-radius: 10px; transition: all .25s; z-index: 100; pointer-events: none; max-width: calc(100vw - 32px); text-align: center; }
.toast.show { opacity: 1; transform: translate(-50%, 0); }
.toast.error { background: var(--danger); }

@keyframes pulse { 50% { box-shadow: 0 0 0 3px var(--turn), 0 0 28px rgba(251,191,36,.9); } }
@keyframes glow { 50% { filter: brightness(1.1); } }
:root[data-anim="off"] * { transition: none !important; animation: none !important; }

/* responsive */
@media (max-width: 1100px) {
  .lobby { grid-template-columns: 1fr 1fr; }
  .lobby .chatpanel { grid-column: 1 / -1; position: static; }
}
@media (max-width: 860px) {
  .table-wrap { grid-template-columns: 1fr; }
  .side { border-left: none; border-top: 1px solid var(--line); }
  .side .chat { height: 260px; }
  .lobby { grid-template-columns: 1fr; padding: 16px; }
  .center { flex-direction: column; align-items: stretch; }
  .piles { justify-content: center; }
}
@media (max-width: 520px) {
  :root { --cw: 50px; --ch: 72px; --cfs: 13px; --cw-s: 32px; --ch-s: 46px; }
  .table { padding: 8px; }
  .opp { min-width: 0; padding: 6px 8px; flex: 1 1 40%; }
  header.top { padding: 8px 16px; }
  .hero h1 { font-size: 2rem; }
}

/* discard pile, spread out so every card is visible */
.discard-pile { align-items: flex-start; min-width: 0; flex: 1; }
.fan { display: flex; flex-wrap: wrap; row-gap: 8px; padding: 0 0 0 calc(var(--cw) * 0.62); }
.fan-slot { margin-left: calc(var(--cw) * -0.62); border-radius: calc(var(--cw) * 0.1); transition: transform .12s; }
.fan-slot .card { box-shadow: -2px 1px 3px rgba(0,0,0,.35); }
.fan-slot:last-child .card { box-shadow: 0 0 0 0 transparent, 0 1px 3px rgba(0,0,0,.35); }
.discard-pile.can .fan-slot.takeable { cursor: pointer; }
.discard-pile.can .fan-slot:last-child .card { box-shadow: 0 0 0 3px var(--turn), 0 0 18px rgba(251,191,36,.6); }
.discard-pile.can.multi .fan:hover .fan-slot:last-child .card { box-shadow: 0 1px 3px rgba(0,0,0,.35); }
/* hovering a card highlights it and every card above it (all of which you'd take) */
.discard-pile.can .fan-slot.takeable:hover, .discard-pile.can .fan-slot.takeable:hover ~ .fan-slot { transform: translateY(-8px); }
.discard-pile.can .fan-slot.takeable:hover .card, .discard-pile.can .fan-slot.takeable:hover ~ .fan-slot .card { box-shadow: 0 0 0 3px #38bdf8, 0 6px 12px rgba(0,0,0,.4); }
.discard-pile.can .fan-slot.takeable:hover .card { box-shadow: 0 0 0 3px #f97316, 0 6px 12px rgba(0,0,0,.4); }
.fan-slot.hinted .card { outline: 3px dashed #a7f3d0; outline-offset: 3px; }
.discard-pile .pile-label { margin-top: 6px; }
.center { flex-direction: column; }
.piles { width: 100%; align-items: flex-start; }
.hand .card.must { box-shadow: 0 0 0 3px #f97316, 0 1px 3px rgba(0,0,0,.35); }
.hand .card.must::before { content: "PLAY"; position: absolute; top: -9px; left: 50%; transform: translateX(-50%); font: 700 9px system-ui; background: #f97316; color: #fff; border-radius: 4px; padding: 1px 4px; z-index: 2; }
