:root {
  --bg: #0e1420; --bg2: #141d2e; --panel: #172136; --panel2: #1f2c47; --line: rgba(255,255,255,.1);
  --ink: #eef1f7; --muted: #8e9ab0; --gold: #e9b949; --teal: #38c2b0; --danger: #e5646b;
  --tw: clamp(34px, calc((100vw - 34px) / 8.6), 54px);   /* tile width: 8 tiles fit in a row of the rack on a phone */
  --th: calc(var(--tw) * 1.34);
}
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; height: 100%; }
body {
  background: radial-gradient(ellipse at 50% 30%, #1a2742 0%, var(--bg) 70%) fixed;
  color: var(--ink); font: 16px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  overflow: hidden; user-select: none; -webkit-user-select: none;
  padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
}
#app { height: 100%; display: flex; flex-direction: column; align-items: center; max-width: 1100px; margin: 0 auto; }

/* header */
#top { width: 100%; display: flex; align-items: center; gap: 6px; padding: 8px 10px 4px; flex: none; }
.brand { font-weight: 800; letter-spacing: .02em; font-size: 16px; white-space: nowrap; }
.brand span, h1 span { color: var(--gold); }
.status { flex: 1; text-align: center; font-size: 13px; color: var(--muted); padding: 5px 6px; border-radius: 999px; background: var(--panel); border: 1px solid var(--line); min-width: 0; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.status.mine { color: #1b1405; background: var(--gold); border-color: var(--gold); font-weight: 600; }
.tools { display: flex; gap: 4px; }
button.icon { width: 32px; height: 32px; padding: 0; border-radius: 10px; font-size: 17px; line-height: 1; }
button.icon.off { opacity: .45; }

/* opponent strip */
#opp { width: 100%; display: flex; align-items: center; justify-content: space-between; padding: 2px 14px 4px; flex: none; min-height: 28px; font-size: 14px; color: var(--muted); }
#opp .who { display: flex; align-items: center; gap: 8px; min-width: 0; }
#opp b { color: var(--ink); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 40vw; }
#opp .cnt { color: var(--ink); }
#opp .tagline { font-size: 12px; opacity: .8; }
#opp .arrow { width: 0; height: 0; border-left: 9px solid transparent; border-top: 6px solid transparent; border-bottom: 6px solid transparent; }
#opp .who.turn .arrow { border-left-color: var(--gold); filter: drop-shadow(0 0 4px rgba(233,185,73,.7)); }
#opp .pool b { color: var(--gold); }

/* table */
#tablewrap { flex: 1; min-height: 0; width: calc(100% - 16px); overflow-y: auto; overflow-x: hidden; background: rgba(255,255,255,.035); border: 1px solid var(--line); border-radius: 16px; -webkit-overflow-scrolling: touch; }
#table { display: flex; flex-wrap: wrap; gap: 9px 8px; align-content: flex-start; padding: 10px; min-height: 100%; }
.emptytable { margin: auto; color: var(--muted); font-size: 14px; padding: 20px; width: 100%; text-align: center; }
.set { display: flex; gap: 3px; padding: 5px 6px; background: rgba(255,255,255,.08); border: 2px solid transparent; border-radius: 12px; max-width: 100%; flex-wrap: nowrap; }
.set.bad { border-color: var(--danger); background: rgba(229,100,107,.13); }
.set.newslot { border: 2px dashed rgba(255,255,255,.22); background: none; min-width: calc(var(--tw) + 14px); min-height: calc(var(--th) + 14px); align-items: center; justify-content: center; color: rgba(255,255,255,.4); font-size: 26px; font-weight: 300; }
.set.over, #rackzone.over, #tablewrap.over { outline: 2px solid var(--teal); outline-offset: -2px; background-color: rgba(56,194,176,.16); }
#tablewrap.over { background-color: rgba(56,194,176,.07); }

/* tiles */
.tile {
  width: var(--tw); height: var(--th); flex: none; position: relative;
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  border-radius: calc(var(--tw) * .16); font-weight: 800;
  background: linear-gradient(180deg, #fbf5e2 0%, #eadfbf 100%);
  box-shadow: 0 2px 0 #b6a778, 0 3px 5px rgba(0,0,0,.5);
  transition: transform .1s ease;
}
.tile .n { font-size: calc(var(--tw) * .56); line-height: 1; letter-spacing: -.02em; }
.tile .s { font-size: calc(var(--tw) * .25); line-height: 1; margin-top: calc(var(--tw) * .06); }
.tile.c0 { color: #1d1d21; } .tile.c1 { color: #1860d4; } .tile.c2 { color: #d62828; } .tile.c3 { color: #e27600; }
.tile.joker { color: #6b36b8; background: linear-gradient(180deg, #fbf5e2 0%, #eadfbf 100%); }
.tile.joker .n { font-size: calc(var(--tw) * .62); }
.tile.back { background: linear-gradient(135deg, #2c3d63, #1f2c47); box-shadow: 0 2px 0 #121a2b; }
.tile.drag { cursor: grab; touch-action: none; }
.tile.sel { transform: translateY(-5px); outline: 2px solid var(--gold); outline-offset: 1px; }
.tile.added { box-shadow: 0 0 0 2px #ff9d2e, 0 3px 5px rgba(0,0,0,.5); }
.tile.fresh { box-shadow: 0 0 0 2px var(--gold), 0 0 12px rgba(233,185,73,.85); }
.tile.dragging { opacity: .3; }
.ghost { position: fixed; left: 0; top: 0; z-index: 60; pointer-events: none; transform: translate(-50%, -60%) scale(1.12); box-shadow: 0 12px 16px rgba(0,0,0,.55); }

/* info line + rack */
#info { width: 100%; display: flex; align-items: center; gap: 8px; padding: 5px 14px 2px; flex: none; min-height: 32px; font-size: 14px; }
#info .msg { flex: 1; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
#info .msg.good { color: var(--teal); } #info .msg.warn { color: var(--gold); }
#info button { padding: 3px 10px; font-size: 13px; border-radius: 9px; }
#react { display: flex; gap: 4px; }
.rb { width: 30px; height: 26px; padding: 0; font-size: 15px; line-height: 1; border-radius: 8px; text-align: center; }
.rb[disabled] { opacity: .35; cursor: default; }
#rackzone { width: calc(100% - 16px); flex: none; background: var(--panel); border: 1px solid var(--line); border-radius: 16px; padding: 4px 8px 8px; }
.rackhead { display: flex; justify-content: space-between; align-items: center; font-size: 12px; color: var(--muted); min-height: 28px; }
.sorts { display: flex; gap: 6px; }
.sorts button { padding: 2px 9px; font-size: 12px; border-radius: 8px; }
#rack { display: flex; flex-wrap: wrap; gap: 5px 4px; align-content: flex-start; min-height: calc(var(--th) * 2 + 14px); max-height: calc(var(--th) * 3 + 24px); overflow-y: auto; padding: 4px 2px 6px; }
#rack .empty { color: var(--muted); font-size: 14px; margin: auto; }
#actions { width: 100%; display: flex; gap: 8px; padding: 8px 8px calc(8px + env(safe-area-inset-bottom)); flex: none; }
#actions button { flex: 1; padding: 11px 6px; }
#actions #btnDone { flex: 2; }
#actions #btnDraw { flex: 1.2; }
#actions.off button { opacity: .55; }
body.dragging-now, body.dragging-now * { cursor: grabbing !important; }

/* buttons & inputs */
button { font: inherit; color: var(--ink); background: var(--panel2); border: 1px solid var(--line); border-radius: 12px; padding: 10px 16px; cursor: pointer; }
button:hover:not(:disabled) { border-color: rgba(255,255,255,.3); }
button.primary { background: var(--gold); color: #1b1405; border-color: var(--gold); font-weight: 700; }
button:disabled { opacity: .45; cursor: default; }
button.primary:disabled { background: #6e5f33; border-color: #6e5f33; color: #cbbf9d; }
input[type=text] { font: inherit; color: var(--ink); background: var(--bg); border: 1px solid var(--line); border-radius: 10px; padding: 10px 12px; width: 100%; user-select: text; -webkit-user-select: text; }
input[type=text]:focus { outline: 2px solid var(--teal); outline-offset: 1px; }

/* overlays */
.overlay { position: fixed; inset: 0; z-index: 40; background: rgba(6,9,16,.78); display: flex; align-items: center; justify-content: center; padding: 16px; overflow-y: auto; }
.overlay[hidden], #toast[hidden], button[hidden], .note[hidden], #btnInstall[hidden] { display: none; }
.dialog { width: 100%; max-width: 400px; background: var(--panel); border: 1px solid var(--line); border-radius: 18px; padding: 22px 20px; display: flex; flex-direction: column; gap: 10px; box-shadow: 0 20px 60px rgba(0,0,0,.5); max-height: 100%; overflow-y: auto; }
.dialog.wide { max-width: 560px; }
.dialog h1 { margin: 0; font-size: 32px; letter-spacing: .01em; }
.dialog h2 { margin: 0; font-size: 22px; }
.dialog h3 { margin: 6px 0 0; font-size: 15px; color: var(--gold); }
.dialog label { font-size: 13px; color: var(--muted); }
.tag { margin: 0 0 4px; color: var(--muted); font-size: 14px; }
.note { padding: 8px 12px; border-radius: 10px; background: rgba(233,185,73,.12); color: var(--gold); font-size: 14px; }
.chips { display: flex; gap: 8px; }
.chips label { flex: 1; color: var(--ink); }
.chips input { position: absolute; opacity: 0; pointer-events: none; }
.chips span { display: block; text-align: center; padding: 9px 4px; border-radius: 10px; border: 1px solid var(--line); background: var(--bg2); cursor: pointer; font-size: 14px; }
.chips input:checked + span { border-color: var(--gold); color: var(--gold); background: rgba(233,185,73,.1); }
.chips input:focus-visible + span { outline: 2px solid var(--teal); }
.joinrow, .row { display: flex; gap: 8px; }
.joinrow input { text-transform: uppercase; letter-spacing: .15em; }
.code { font-size: 20px; letter-spacing: .2em; color: var(--gold); }
details { font-size: 14px; color: var(--muted); }
summary { cursor: pointer; padding: 4px 0; }
details ul { padding-left: 18px; margin: 6px 0 0; }
details li { margin-bottom: 6px; }
button small { opacity: .7; }
#newGameBlock { display: flex; flex-direction: column; gap: 10px; }
#newGameBlock[hidden] { display: none; }

.rules p, .rules ul { margin: 4px 0 0; font-size: 14px; color: var(--ink); }
.rules ul { padding-left: 18px; }
.rules li { margin-bottom: 4px; }
.rules button { margin-top: 10px; }

#toast { position: fixed; left: 50%; top: calc(92px + env(safe-area-inset-top)); transform: translateX(-50%); z-index: 70; pointer-events: none; max-width: min(92vw, 440px); background: #2a1519; color: #ffd9dc; border: 1px solid var(--danger); border-radius: 12px; padding: 10px 16px; font-size: 14px; text-align: center; box-shadow: 0 8px 24px rgba(0,0,0,.5); }
#toast.info { background: var(--panel2); color: var(--ink); border-color: var(--line); }

/* result: a banner at the bottom, the finished table stays visible; a win gets a golden glow (plus confetti from game.js) */
#result.overlay { background: none; pointer-events: none; align-items: flex-end; padding: 12px 12px calc(12px + env(safe-area-inset-bottom)); }
#result .dialog { pointer-events: auto; max-width: 440px; text-align: center; padding: 12px 16px; gap: 6px; background: rgba(23,33,54,.97); animation: rise .45s ease-out; }
#result .dialog h2 { font-size: 24px; } #result .tag { margin: 0; }
#result .row { justify-content: center; }
#result .dialog.party { border-color: var(--gold); box-shadow: 0 0 34px rgba(233,185,73,.6), 0 10px 40px rgba(0,0,0,.5); }
#result .dialog.party h2 { color: var(--gold); animation: bounce 1s ease-in-out infinite; }
@keyframes rise { from { transform: translateY(40px); opacity: 0; } to { transform: none; opacity: 1; } }
@keyframes bounce { 0%, 100% { transform: scale(1); } 50% { transform: scale(1.08); } }
@media (prefers-reduced-motion: reduce) { #result .dialog, #result .dialog.party h2, .tile { animation: none; transition: none; } }

/* ranking overlay */
.ranking .rankwrap { max-height: 52vh; overflow: auto; border-radius: 10px; }
.ranking table { width: 100%; border-collapse: collapse; font-size: 14px; }
.ranking th, .ranking td { padding: 6px 8px; text-align: left; border-bottom: 1px solid rgba(255,255,255,.07); }
.ranking th { position: sticky; top: 0; background: var(--panel2); font-weight: 600; font-size: 12px; color: var(--muted); }
.ranking td.n, .ranking th.n { text-align: right; white-space: nowrap; }
.ranking tr.me td { background: rgba(233,185,73,.14); }
.ranking tr.top1 td:first-child { color: #ffd34d; font-weight: 800; } .ranking tr.top2 td:first-child { color: #d4d9e2; font-weight: 800; } .ranking tr.top3 td:first-child { color: #d99a63; font-weight: 800; }
.ranking .up { color: #54d18a; } .ranking .down { color: #ef7a80; } .ranking .muted { color: var(--muted); }
.ranking small { color: var(--gold); }
#pidShow { font-family: ui-monospace, Consolas, monospace; font-size: 11px; letter-spacing: 0; padding-left: 8px; padding-right: 6px; }
#pidInput { font-family: ui-monospace, Consolas, monospace; font-size: 12px; }

/* desktop: bigger tiles */
@media (min-width: 820px) {
  :root { --tw: clamp(44px, calc((100dvh - 330px) / 9), 60px); }
  #tablewrap { width: calc(100% - 24px); }
  #rackzone { width: calc(100% - 24px); }
}
@media (max-height: 640px) {
  :root { --tw: clamp(32px, calc((100vw - 34px) / 9.6), 44px); }
}

/* wide screens: keep the status pill and the buttons compact */
@media (min-width: 820px) {
  .status { flex: 0 1 280px; margin: 0 auto; }
  #top { justify-content: space-between; }
  #actions { max-width: 640px; }
}
