:root {
  --felt: #0d5c3f;
  --felt-dark: #083d2a;
  --ink: #1d1d1d;
  --tile: #fbf8ee;
  --tile-edge: #d9d1b8;
  --gold: #f3c34a;
  --red: #c62828;
  --green: #1b7a3e;
  --blue: #1e5aa8;
  --tw: min(calc((100vw - 34px) / 14.6), 50px);
  --sw: min(calc((100vw - 40px) / 18), 26px);
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background: radial-gradient(ellipse at center, #137a52 0%, var(--felt) 45%, var(--felt-dark) 100%) fixed;
  color: #fff;
  -webkit-tap-highlight-color: transparent;
}
button {
  font: inherit; cursor: pointer; border: none; border-radius: 10px;
  padding: 8px 14px; background: #ffffff22; color: #fff;
}
button:hover { background: #ffffff33; }
button:disabled { opacity: .45; cursor: default; }
button.primary { background: var(--gold); color: #3b2a00; font-weight: 700; }
button.primary:hover { background: #ffd566; }
button.big { width: 100%; padding: 14px; font-size: 1.05rem; margin: 6px 0; }
button.link { background: none; text-decoration: underline; padding: 6px; }
input, select {
  font: inherit; padding: 10px; border-radius: 10px; border: none; width: 100%;
  background: #fff; color: var(--ink);
}
.hidden { display: none !important; }
.screen { min-height: 100%; }
.card {
  background: #062c1ecc; border: 1px solid #ffffff22; border-radius: 18px;
  padding: 20px; box-shadow: 0 10px 40px #0007;
}
.sub { opacity: .8; margin-top: 4px; }
.small { font-size: .85rem; opacity: .8; }
.error { color: #ffb4b4; min-height: 1.2em; }
.row { display: flex; justify-content: space-between; }

/* ---------- home & lobby ---------- */
#home, #lobby { display: flex; align-items: center; justify-content: center; padding: 16px; }
.home-card, .lobby-card { width: min(440px, 100%); }
.home-card h1 { margin: 0 0 4px; font-size: 2rem; display: flex; align-items: center; gap: 10px; }
.logo-tiles { display: inline-flex; gap: 2px; }
.home-card label { display: block; margin: 14px 0 8px; font-weight: 600; }
.home-card label input { margin-top: 6px; }
.join-row { display: flex; gap: 8px; margin: 6px 0; }
.join-row input { width: 110px; text-transform: uppercase; letter-spacing: 3px; text-align: center; font-weight: 700; }
.join-row button { flex: 1; }
.code { background: var(--gold); color: #3b2a00; padding: 2px 10px; border-radius: 8px; letter-spacing: 4px; }
.share { display: flex; gap: 8px; margin: 10px 0; }
.share input { font-size: .85rem; }
.seat-row {
  display: flex; align-items: center; justify-content: space-between;
  padding: 10px 12px; margin: 6px 0; border-radius: 12px; background: #ffffff14;
}
.seat-row button { white-space: nowrap; }
.seat-row.empty { opacity: .7; border: 1px dashed #ffffff55; background: none; }
.tag { font-size: .75rem; padding: 2px 7px; border-radius: 6px; background: #ffffff26; margin-left: 6px; }
.tag.gold { background: var(--gold); color: #3b2a00; }
.setting { display: flex; align-items: center; justify-content: space-between; gap: 10px; margin: 8px 0; }
.setting select { width: auto; }

/* ---------- tiles ---------- */
.tile {
  display: inline-flex; flex-direction: column; align-items: center; justify-content: center;
  width: var(--tw); height: calc(var(--tw) * 1.36);
  background: linear-gradient(#fffdf6, var(--tile));
  border-radius: calc(var(--tw) * .14);
  border: 1px solid var(--tile-edge);
  box-shadow: 0 calc(var(--tw) * .08) 0 #2f8f63, 0 calc(var(--tw) * .1) 3px #0006;
  color: var(--ink); line-height: 1; position: relative; user-select: none;
  margin: 0 1px calc(var(--tw) * .1);
  vertical-align: top;
}
.tile svg { width: 92%; height: 92%; display: block; }
.tile .lbl {
  position: absolute; top: 1px; left: 2px; font-size: max(8px, calc(var(--tw) * .26)); font-weight: 800;
  color: #444; line-height: 1;
}
.tile.back { background: linear-gradient(#2c9b6b, #1c7a50); border-color: #155d3c; }
.tile.small { --tw: var(--sw); }
.tile.tiny { --tw: 14px; }
.tile.xl { --tw: min(9vw, 38px); }
.tile.last { outline: 3px solid var(--gold); outline-offset: 1px; animation: pulse 1s ease-in-out infinite alternate; }
.tile.claimed { background: linear-gradient(#fff6cf, #f4e7b0); }
@keyframes pulse { from { outline-color: var(--gold); } to { outline-color: #f3c34a33; } }

/* ---------- table ---------- */
#table { max-width: 900px; margin: 0 auto; padding: 6px 8px 10px; display: flex; flex-direction: column; gap: 6px; }
.topbar { display: flex; justify-content: space-between; align-items: center; font-size: .9rem; }
.topbar .tools button { padding: 6px 10px; margin-left: 2px; }
.topbar .tools button.off { opacity: .4; }
#mine.turn { border-color: var(--gold); box-shadow: 0 0 14px #f3c34a66; }
.who { display: flex; align-items: center; gap: 4px; flex-wrap: wrap; font-size: .85rem; }
.who .nm { font-weight: 700; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; max-width: 100%; }
.wind { display: inline-block; background: #fff; color: var(--ink); border-radius: 5px; padding: 0 5px; font-weight: 700; font-size: .8rem; }
.wind.dealer { background: var(--gold); }
.score { opacity: .9; font-variant-numeric: tabular-nums; }
.backs { margin: 3px 0; line-height: 0; }
.melds { display: flex; flex-wrap: wrap; gap: 6px; margin: 2px 0; }
.meld { display: inline-flex; white-space: nowrap; }
#timer { width: 100%; height: 26px; background: #00000040; border-radius: 13px; position: relative; overflow: hidden; }
#timer-bar { height: 100%; width: 0; background: linear-gradient(90deg, #6bd49d, #f3c34a); border-radius: 13px; transition: background .3s; }
#timer.urgent #timer-bar { background: linear-gradient(90deg, #ff5252, #ff8a65); }
#timer.urgent { animation: shake .25s linear infinite; }
#timer.mine:not(.urgent) #timer-bar { background: linear-gradient(90deg, #f3c34a, #ffe08a); }
#timer-text { position: absolute; inset: 0; display: flex; align-items: center; justify-content: center; font-weight: 700; font-size: .9rem; text-shadow: 0 1px 2px #000a; }
@keyframes shake { 0%,100% { transform: translateX(0); } 25% { transform: translateX(-1px); } 75% { transform: translateX(1px); } }
#log { font-size: .78rem; opacity: .75; text-align: center; min-height: 1.1em; }
#mine { background: #00000026; border-radius: 12px; padding: 8px; border: 2px solid transparent; }
#coach { font-size: .88rem; margin: 4px 0; min-height: 1.2em; color: #ffe9a8; }
@keyframes glow { from { box-shadow: 0 0 4px #ff5252; } to { box-shadow: 0 0 18px #ff5252; } }
#hand { display: flex; align-items: flex-end; justify-content: center; flex-wrap: nowrap; padding-top: 10px; }
#hand .tile { cursor: pointer; transition: transform .12s; }
#hand.active .tile:hover { transform: translateY(-4px); }
#hand .tile.sel { transform: translateY(-12px); box-shadow: 0 0 0 3px var(--gold), 0 6px 10px #0008; }
#hand .tile.suggest { box-shadow: 0 0 0 2px var(--gold), 0 0 12px #f3c34aaa; }
#hand .tile.suggest::after {
  content: "★"; position: absolute; top: calc(var(--tw) * -.2); right: calc(var(--tw) * -.2);
  font-size: calc(var(--tw) * .42); line-height: 1; color: var(--gold); text-shadow: 0 0 2px #000, 0 0 2px #000;
}
#hand .gap { width: calc(var(--tw) * .4); }
.away-banner { background: var(--red); padding: 8px; border-radius: 10px; margin: 4px 0; display: flex; justify-content: space-between; align-items: center; }

/* ---------- overlays ---------- */
.overlay {
  position: fixed; inset: 0; background: #000a; display: flex; align-items: flex-start; justify-content: center;
  overflow-y: auto; padding: 16px; z-index: 10;
}
.result-card { background: #062c1e; width: min(560px, 100%); margin-top: 6vh; }
.result-card h2 { margin-top: 0; }
.result-hand { background: #00000033; border-radius: 10px; padding: 8px; margin: 8px 0; display: flex; flex-wrap: wrap; gap: 6px; }
.result-hand .tile { --tw: min(7.5vw, 36px); }
.fanlist { width: 100%; border-collapse: collapse; margin: 8px 0; }
.fanlist td { padding: 4px 6px; border-bottom: 1px solid #ffffff1a; }
.fanlist td:last-child { text-align: right; font-weight: 700; }
.pay.plus { color: #9ef0b9; } .pay.minus { color: #ffb4b4; }
.rules-card { background: #062c1e; width: min(720px, 100%); position: relative; line-height: 1.5; }
.rules-card h3 { color: var(--gold); margin-top: 1.6em; }
.close { position: sticky; top: 0; float: right; }
.toc { display: flex; flex-wrap: wrap; gap: 6px; }
.toc a { color: #fff; background: #ffffff1f; padding: 4px 10px; border-radius: 8px; text-decoration: none; font-size: .85rem; }
.ex { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; margin: 8px 0; }
.ex .lbl { min-width: 90px; }
.ex.wide { gap: 12px; }
.fan { border-collapse: collapse; width: 100%; }
.fan td { padding: 5px 6px; border-bottom: 1px solid #ffffff1a; }
.fan td:last-child { text-align: right; font-weight: 700; color: var(--gold); white-space: nowrap; }
#toasts { position: fixed; top: 38%; left: 0; right: 0; display: flex; flex-direction: column; align-items: center; pointer-events: none; z-index: 20; }
.toast {
  background: #000c; padding: 10px 18px; border-radius: 14px; margin: 4px; font-weight: 700;
  animation: pop 2.4s ease forwards; text-align: center; max-width: 90vw;
}
.toast.claim, .toast.big { font-size: 1.3rem; background: var(--gold); color: #3b2a00; }
.toast.timeout { background: var(--red); }
.toast small { display: block; font-size: .8rem; font-weight: 500; opacity: .85; }
body.reading #toasts { display: none; }
@keyframes pop {
  0% { transform: scale(.6); opacity: 0; } 10% { transform: scale(1.08); opacity: 1; }
  20% { transform: scale(1); } 80% { opacity: 1; } 100% { opacity: 0; transform: translateY(-20px); }
}

@media (min-width: 700px) {
  :root { --tw: min(calc((100vw - 60px) / 15), 50px); }
}

.modes { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; margin: 12px 0 6px; }
.mode { text-align: left; display: flex; flex-direction: column; gap: 4px; padding: 12px; border: 2px solid transparent; }
.mode span { font-size: .8rem; opacity: .85; }
.mode.on { border-color: var(--gold); background: #f3c34a2e; }
.mode:disabled { opacity: .35; }
.mode.on:disabled { opacity: 1; }

/* phones: your hand in two rows of bigger tiles */
@media (max-width: 600px) {
  :root { --tw: min(calc((100vw - 44px) / 7.4), 56px); }
  #hand { flex-wrap: wrap; row-gap: 4px; }
}

/* coach mode: the hand sorted into groups */
#hand.grouped { flex-wrap: wrap; gap: 10px 8px; align-items: flex-start; }
.group { display: flex; flex-direction: column; align-items: center; border-radius: 8px; padding: 3px 3px 2px; }
.gtiles { display: flex; }
.glabel { font-size: .72rem; font-weight: 700; margin-top: 3px; display: flex; align-items: center; gap: 3px; white-space: nowrap; }
.group.set { background: #6bd49d2e; box-shadow: inset 0 -3px 0 #6bd49d; }
.group.set .glabel { color: #9ef0b9; }
.group.pair { background: #7fb2ff26; box-shadow: inset 0 -3px 0 #7fb2ff; }
.group.pair .glabel { color: #b9d4ff; }
.group.wait { background: #f3c34a24; box-shadow: inset 0 -3px 0 var(--gold); }
.group.wait .glabel { color: #ffe08a; }
.group.loose { background: #ffffff0d; box-shadow: inset 0 -3px 0 #ffffff40; }
.group.loose .glabel { color: #ffffffaa; }
.tile.mini { --tw: 15px; margin: 0; box-shadow: none; }
.tile.mini .lbl { display: none; }
#hand .tile.new::before {
  content: "new"; position: absolute; bottom: calc(var(--tw) * -.12); left: 0; right: 0; text-align: center;
  font-size: max(9px, calc(var(--tw) * .22)); font-weight: 800; color: #fff; background: var(--red);
  border-radius: 0 0 4px 4px; line-height: 1.3;
}
@media (max-width: 600px) {
  #hand.grouped { --tw: min(calc((100vw - 70px) / 8.6), 50px); }
}

/* ---------- the square table ---------- */
#board {
  --S: min(calc(100vw - 16px), 620px, max(300px, calc(100vh - 330px)));
  --E: calc(var(--S) * .2);
  --C: calc(var(--S) - 2 * var(--E));
  --T: calc(var(--C) * .37);
  --L: calc(var(--C) - var(--T));
  width: var(--S); height: var(--S); margin: 0 auto; position: relative; flex: none;
  background: radial-gradient(circle, #188a5c 0%, #0f6b47 60%, #0a5537 100%);
  border-radius: 18px; box-shadow: inset 0 0 0 3px #6b4a2b, inset 0 0 0 7px #3f2a17, 0 8px 24px #0006;
}
.edge {
  position: absolute; display: flex; flex-direction: column; align-items: center; gap: 2px;
  padding: 6px 4px; border-radius: 12px; overflow: hidden;
  font-size: max(10px, calc(var(--S) * .027));
}
.edge.top { left: var(--E); top: 4px; width: var(--C); height: calc(var(--E) - 4px); }
.edge.bottom { left: var(--E); top: calc(var(--S) - var(--E)); width: var(--C); height: calc(var(--E) - 4px); justify-content: flex-end; flex-direction: column-reverse; }
.edge.left { left: 4px; top: var(--E); width: calc(var(--E) - 4px); height: var(--C); }
.edge.right { left: calc(var(--S) - var(--E)); top: var(--E); width: calc(var(--E) - 4px); height: var(--C); }
.edge.turn { background: #f3c34a33; box-shadow: inset 0 0 0 2px var(--gold); }
.edge .who { font-size: inherit; justify-content: center; text-align: center; gap: 3px; }
.edge.left .who, .edge.right .who { flex-direction: column; }
.edge .who .wind { font-size: inherit; }
.edge .backs { display: flex; flex-wrap: wrap; justify-content: center; gap: 1px; margin: 1px 0; }
.edge .melds { justify-content: center; gap: 4px; }
.edge.left .melds, .edge.right .melds { flex-direction: column; align-items: center; }
.tile.tiny { --tw: calc(var(--S) * .026); margin: 0; box-shadow: 0 1px 0 #155d3c; }
.tile.mtile { --tw: calc(var(--S) * .046); margin: 0; }
#pondbox { position: absolute; left: var(--E); top: var(--E); width: var(--C); height: var(--C); }
.pondwrap { position: absolute; width: var(--L); height: var(--T); }
.pondwrap.p0 { left: var(--T); top: calc(var(--C) - var(--T)); }
.pondwrap.p1 { left: calc(var(--C) - var(--T) / 2 - var(--L) / 2); top: calc(var(--L) / 2 - var(--T) / 2); transform: rotate(-90deg); }
.pondwrap.p2 { left: 0; top: 0; transform: rotate(180deg); }
.pondwrap.p3 { left: calc(var(--T) / 2 - var(--L) / 2); top: calc(var(--T) / 2 + var(--L) / 2); transform: rotate(90deg); }
.pond { display: flex; flex-wrap: wrap; align-content: flex-start; gap: 1px; padding: 2px; }
.tile.ptile { --tw: calc((var(--L) - 12px) / 7); margin: 0 0 calc(var(--tw) * .08); }
#middle {
  position: absolute; left: var(--T); top: var(--T); width: calc(var(--C) - 2 * var(--T)); height: calc(var(--C) - 2 * var(--T));
  display: flex; align-items: center; justify-content: center; border-radius: 8px; background: #00000033;
}
#middle .wall { display: flex; flex-direction: column; align-items: center; line-height: 1; }
#middle .wall b { font-size: calc(var(--S) * .05); }
#middle .wall span { font-size: max(8px, calc(var(--S) * .018)); opacity: .75; }
#middle .arrow { position: absolute; color: var(--gold); font-size: calc(var(--S) * .035); line-height: 1; }
#middle .arrow.a0 { bottom: 1px; } #middle .arrow.a1 { right: 2px; } #middle .arrow.a2 { top: 1px; } #middle .arrow.a3 { left: 2px; }

/* ---------- "you can take it" prompt ---------- */
#actions {
  position: absolute; left: 5%; right: 5%; top: 50%; transform: translateY(-50%); z-index: 5;
  display: flex; flex-direction: column; gap: 6px; max-width: 460px; margin: 0 auto;
}
#actions:empty { display: none; }
.prompt { background: #fffaf0; color: var(--ink); border-radius: 14px; padding: 10px; box-shadow: 0 0 0 3px var(--gold), 0 6px 20px #0008; animation: rise .25s ease-out; }
@keyframes rise { from { transform: translateY(10px); opacity: 0; } to { transform: none; opacity: 1; } }
.prompt-head { display: flex; align-items: center; gap: 10px; font-size: .95rem; margin-bottom: 8px; }
.tile.big { --tw: 44px; margin: 0; }
.prompt-note { font-size: .8rem; opacity: .7; margin-bottom: 4px; }
.prompt-buttons { display: flex; flex-wrap: wrap; gap: 8px; }
.take {
  flex: 1 1 140px; display: flex; flex-direction: column; align-items: center; gap: 4px; padding: 10px 8px;
  border-radius: 12px; background: #1b7a3e; color: #fff; border: 3px solid transparent;
}
.take:hover { background: #22914b; }
.take b { font-size: 1.25rem; letter-spacing: 1px; }
.take span { font-size: .78rem; opacity: .92; line-height: 1.25; }
.take .tile.btile { --tw: 26px; margin: 0 1px; }
.take.win { background: var(--red); animation: glow .6s ease-in-out infinite alternate; }
.take.pass { background: #6b6b6b; flex: 0 1 110px; }
.take.pass:hover { background: #7d7d7d; }
.take.suggest { border-color: var(--gold); box-shadow: 0 0 12px #f3c34a; }
#hand .tile.match { box-shadow: 0 0 0 3px #4fc3f7, 0 0 14px #4fc3f7; transform: translateY(-6px); }

/* hard mode: arranging your own hand */
#hand-tools { display: flex; align-items: center; justify-content: flex-end; gap: 6px; margin-bottom: 2px; }
#hand-tools span { margin-right: auto; }
#hand-tools button { padding: 4px 10px; font-size: .85rem; white-space: nowrap; }
.ugap {
  width: calc(var(--tw) * .55); height: calc(var(--tw) * 1.36); margin: 0 2px; cursor: grab;
  border-left: 2px dashed #ffffff66; border-right: 2px dashed #ffffff66; border-radius: 4px; flex: none;
}
#hand .dragging { z-index: 20; opacity: .9; transition: none; box-shadow: 0 10px 20px #000a; }
#hand .drop-before { box-shadow: -5px 0 0 var(--gold); }
#hand .drop-after { box-shadow: 5px 0 0 var(--gold); }
