:root {
  --bg: #f4f1ea; --panel: #fff; --ink: #1d1d1f; --muted: #6b6b70; --line: #ddd8cc;
  --accent: #2b59c3; --accent-ink: #fff; --danger: #b3261e;
  --r: #d7352f; --g: #2f9e44; --b: #2b6fd6; --y: #f2b705; --wild: #26262b;
  --card-w: 64px; --card-h: 96px;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #17181b; --panel: #22242a; --ink: #ececee; --muted: #9a9aa2; --line: #34363e;
    --accent: #6f95ee; --accent-ink: #10131a; --danger: #f2877f;
  }
}
[hidden] { display: none !important; }
* { box-sizing: border-box; }
html { color-scheme: light dark; -webkit-text-size-adjust: 100%; }
body { margin: 0; touch-action: manipulation; -webkit-tap-highlight-color: transparent; background: var(--bg); color: var(--ink); font: 16px/1.4 system-ui, sans-serif; }
h1, h2, h3 { margin: 0; }
h1 { font-size: 1.4rem; letter-spacing: .04em; }
h2 { font-size: 1rem; }
button, input, select { font: inherit; color: inherit; }
button { min-height: 40px; background: var(--panel); border: 1px solid var(--line); border-radius: 8px; padding: .45em .9em; cursor: pointer; }
button:hover:not(:disabled) { border-color: var(--accent); }
button:disabled { opacity: .45; cursor: default; }
button.primary { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }
button.danger { color: var(--danger); }
input, select { background: var(--panel); border: 1px solid var(--line); border-radius: 8px; padding: .45em .6em; }
:focus-visible { outline: 3px solid var(--accent); outline-offset: 2px; }

#top { position: sticky; top: 0; z-index: 15; display: flex; align-items: center; gap: .6rem; padding: .5rem 16px; padding-top: calc(.5rem + env(safe-area-inset-top)); background: var(--panel); border-bottom: 1px solid var(--line); }
#roominfo { flex: 1; color: var(--muted); }
#roominfo code { font-weight: 700; color: var(--ink); letter-spacing: .12em; }
#banner { background: var(--danger); color: #fff; padding: .5rem 16px; text-align: center; }
#banner button { margin-left: .5rem; }
#chattoggle { display: none; position: relative; }
#unread { background: var(--danger); color: #fff; border-radius: 99px; padding: 0 .45em; font-size: .8rem; margin-left: .2rem; }
.sheethead { display: flex; align-items: center; justify-content: space-between; }
#chatclose { display: none; }
#layout { display: grid; grid-template-columns: minmax(0, 1fr) 300px; gap: 16px; padding: 16px; max-width: 1200px; margin: 0 auto; }
#screen { min-width: 0; }
.panel { background: var(--panel); border: 1px solid var(--line); border-radius: 12px; padding: 16px; margin-bottom: 16px; }
.panel > h2 { margin-bottom: .6rem; }
.row { display: flex; flex-wrap: wrap; gap: .5rem; align-items: center; }
.muted { color: var(--muted); }
.stack { display: grid; gap: .6rem; max-width: 360px; }

/* landing + lobby */
.seats { list-style: none; margin: 0 0 1rem; padding: 0; display: grid; gap: .4rem; }
.seats li { display: flex; align-items: center; gap: .6rem; padding: .4rem .6rem; border: 1px solid var(--line); border-radius: 8px; }
.seats .name { flex: 1; }
.tag { font-size: .75rem; padding: .05em .5em; border-radius: 99px; background: var(--line); color: var(--muted); }

/* table */
.players { display: flex; gap: .5rem; margin-bottom: 8px; overflow-x: auto; padding: 4px 2px 8px; }
.pl { flex: 0 0 auto; padding: .4rem .7rem; border: 2px solid var(--line); border-radius: 10px; background: var(--panel); min-width: 90px; }
.pl.turn { border-color: var(--accent); box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent) 25%, transparent); }
.pl .n { font-weight: 600; overflow-wrap: anywhere; }
.pl .c { color: var(--muted); font-size: .85rem; }
.pl .c.uno { color: var(--danger); font-weight: 700; }
.center { display: flex; flex-wrap: wrap; gap: 1.5rem; align-items: center; justify-content: center; padding: 1rem 0; }
.pile { display: grid; gap: .3rem; justify-items: center; color: var(--muted); font-size: .85rem; }
.info { display: grid; gap: .3rem; justify-items: center; }
.dot { display: inline-block; width: 1.1em; height: 1.1em; border-radius: 50%; vertical-align: -.2em; border: 2px solid var(--line); }
.dot.c-r { background: var(--r); } .dot.c-g { background: var(--g); } .dot.c-b { background: var(--b); } .dot.c-y { background: var(--y); }
.badge { background: var(--danger); color: #fff; border-radius: 99px; padding: .05em .7em; font-weight: 700; }
.banner-turn { text-align: center; font-weight: 700; min-height: 1.5em; margin-bottom: .4rem; }
.dock { margin-top: auto; position: sticky; bottom: 0; z-index: 5; background: var(--panel); border: 1px solid var(--line); border-radius: 12px; padding: 10px 12px calc(6px + env(safe-area-inset-bottom)); margin-bottom: 16px; box-shadow: 0 -4px 14px rgba(0,0,0,.12); }
.dock.mine { border-color: var(--accent); box-shadow: 0 -4px 14px color-mix(in srgb, var(--accent) 30%, transparent); }
.dock.mine .banner-turn { color: var(--accent); }
.ticker { list-style: none; margin: 0; padding: 0; min-height: 4.1em; font-size: .9rem; color: var(--muted); display: flex; flex-direction: column-reverse; justify-content: flex-start; }
.ticker li:first-child { color: var(--ink); }
details.panel summary { cursor: pointer; font-weight: 600; }
.actions { display: flex; flex-wrap: wrap; gap: .5rem; justify-content: center; align-items: center; min-height: 3.2rem; margin-bottom: .4rem; }
.actions button { min-height: 46px; padding-inline: 1.2em; }
.colors button { width: 3rem; height: 3rem; border-radius: 50%; border: 3px solid var(--panel); outline: 1px solid var(--line); }
.colors .c-r { background: var(--r); } .colors .c-g { background: var(--g); } .colors .c-b { background: var(--b); } .colors .c-y { background: var(--y); }

.card { position: relative; display: inline-grid; place-items: center; width: var(--card-w); height: var(--card-h); padding: 0; border-radius: 9px; border: 3px solid #fff; color: #fff; font-weight: 800; font-size: 1.5rem; text-shadow: 0 1px 2px rgba(0,0,0,.4); box-shadow: 0 1px 3px rgba(0,0,0,.35); user-select: none; }
.card.c-r { background: var(--r); } .card.c-g { background: var(--g); } .card.c-b { background: var(--b); } .card.c-y { background: var(--y); color: #1d1d1f; text-shadow: none; }
.card.c-w { background: conic-gradient(var(--r) 0 25%, var(--b) 0 50%, var(--g) 0 75%, var(--y) 0); }
.card.c-w span { background: rgba(0,0,0,.65); border-radius: 6px; padding: 0 .25em; }
.card.back { background: var(--wild); font-size: 1rem; }
.card .sm { position: absolute; top: 2px; left: 5px; font-size: .75rem; }
.hand { display: flex; gap: 8px; overflow-x: auto; padding: 14px 6px 8px; scroll-snap-type: x proximity; overscroll-behavior-x: contain; -webkit-overflow-scrolling: touch; }
.hand > .card { flex: 0 0 auto; scroll-snap-align: center; }
.hand > :first-child { margin-left: auto; }
.hand > :last-child { margin-right: auto; }
.hand .card { cursor: default; }
.hand .card.playable { cursor: pointer; transform: translateY(-8px); box-shadow: 0 0 0 3px var(--accent), 0 3px 8px rgba(0,0,0,.4); }
.hand.mine .card:not(.playable) { filter: saturate(.5) brightness(.92); opacity: .85; }
.hand button.card { padding: 0; }
.card, button { user-select: none; -webkit-user-select: none; }

.log { list-style: none; margin: 0; padding: 0; max-height: 11rem; overflow-y: auto; font-size: .9rem; display: flex; flex-direction: column-reverse; }
.log li { padding: .1rem 0; border-bottom: 1px dashed var(--line); }
table.stats { border-collapse: collapse; width: 100%; font-size: .9rem; }
.stats th, .stats td { padding: .25rem .5rem; text-align: right; border-bottom: 1px solid var(--line); }
.stats th:first-child, .stats td:first-child { text-align: left; }
.hands { display: grid; gap: .4rem; margin: .6rem 0; }
.hands .card { --card-w: 34px; --card-h: 50px; font-size: .8rem; border-width: 2px; }
.hands .sm { display: none; }
.winner { font-size: 1.2rem; font-weight: 700; margin-bottom: .5rem; }

/* chat */
#chat { background: var(--panel); border: 1px solid var(--line); border-radius: 12px; padding: 12px; display: flex; flex-direction: column; height: min(70vh, 640px); position: sticky; top: 16px; }
#messages { list-style: none; margin: .5rem 0; padding: 0; overflow-y: auto; flex: 1; font-size: .9rem; }
#messages li { padding: .15rem 0; overflow-wrap: anywhere; }
#messages .sys { color: var(--muted); font-style: italic; }
#messages .from { font-weight: 700; }
#chatform { display: flex; gap: .4rem; }
#chatform input { flex: 1; min-width: 0; }

#toast { position: fixed; top: 64px; left: 50%; transform: translateX(-50%); background: var(--ink); color: var(--bg); padding: .6rem 1rem; border-radius: 10px; max-width: 90vw; z-index: 10; }

@media (max-width: 800px) {
  :root { --card-w: 56px; --card-h: 84px; }
  #layout { grid-template-columns: minmax(0, 1fr); padding: 10px 12px 0; }
  .panel { padding: 12px; margin-bottom: 12px; }
  .center { gap: 1rem; padding: .4rem 0; }
  #screen { display: flex; flex-direction: column; min-height: calc(100dvh - 75px); }
  .dock { margin: auto -12px 0; border-radius: 14px 14px 0 0; border-width: 1px 0 0; }
  #chattoggle { display: inline-flex; align-items: center; }
  #chatclose { display: inline-block; }
  #chat { position: fixed; z-index: 20; left: 0; right: 0; bottom: 0; top: auto; height: 70dvh; border-radius: 16px 16px 0 0; padding-bottom: calc(12px + env(safe-area-inset-bottom)); transform: translateY(100%); visibility: hidden; transition: transform .2s, visibility 0s .2s; box-shadow: 0 -8px 30px rgba(0,0,0,.35); }
  #chat.open { transform: none; visibility: visible; transition: transform .2s; }
  .stats th, .stats td { padding: .2rem .3rem; font-size: .8rem; }
}
@media (pointer: coarse) {
  button, input:not([type="checkbox"]), select { min-height: 44px; }
  input[type="checkbox"] { width: 24px; height: 24px; }
}
@media (prefers-reduced-motion: no-preference) {
  .card { transition: transform .12s; }
}
@media (prefers-reduced-motion: no-preference) {
  @keyframes pop { from { transform: scale(.6) rotate(-10deg); opacity: .3; } to { transform: none; opacity: 1; } }
  .card.pop { animation: pop .25s ease-out; }
}
