:root {
  --felt: #17583f; --felt-2: #0f4230; --felt-line: rgb(255 255 255 / .1);
  --ink: #f3efe4; --muted: #b5cdbf; --accent: #f0c04a; --danger: #ffb4a8;
  --tile: #f6eedb; --tile-edge: #d2c39e;
  --red: #c4312b; --yellow: #8a6200; --blue: #1f5aa3; --black: #23211d;
  --wood: #7a5230; --wood-2: #5e3d21;
  --tw: clamp(20px, min(4.6vw, 7.4vh, (100vw - 366px) / 15), 52px); /* narrow phones: the rack must leave room for the side columns */
  font-family: system-ui, -apple-system, "Segoe UI", sans-serif;
  color: var(--ink);
}
* { box-sizing: border-box; }
body { margin: 0; min-height: 100dvh; background: radial-gradient(ellipse at 50% 40%, var(--felt) 0%, var(--felt-2) 75%) fixed; }
body.playing { height: 100dvh; overflow: hidden; }
[hidden] { display: none !important; }
button, input { font: inherit; }
button {
  color: var(--ink); background: rgb(0 0 0 / .25); border: 1px solid rgb(255 255 255 / .16);
  padding: 10px 16px; border-radius: 8px; cursor: pointer; font-weight: 600;
}
button.primary { background: var(--accent); border-color: var(--accent); color: #241c08; }
button:disabled { opacity: .45; cursor: default; }
button:focus-visible, input:focus-visible, .tile:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

/* ---------- home, name entry, waiting ---------- */
.card-page { max-width: 440px; margin: 0 auto; padding: 10vh 16px 32px; }
.card-page h1 { font-size: 28px; margin: 0 0 8px; letter-spacing: -.01em; }
.card-page p { color: var(--muted); line-height: 1.5; margin: 0 0 24px; }
.card-page label { display: block; font-size: 14px; color: var(--muted); margin-bottom: 6px; }
.card-page input:not([type]), .card-page #link {
  width: 100%; padding: 12px; border-radius: 6px; border: 1px solid var(--felt-line);
  background: rgb(0 0 0 / .2); color: var(--ink); font-size: 18px;
}
#join-form, #create-form { display: grid; }
#join-form button, #create-form > button { margin-top: 16px; padding: 12px; font-size: 17px; }
.choice { border: 0; padding: 0; margin: 0 0 16px; display: flex; gap: 6px; flex-wrap: wrap; }
.choice legend { font-size: 14px; color: var(--muted); margin-bottom: 6px; width: 100%; }
.choice label { margin: 0; }
.choice input { position: absolute; opacity: 0; }
.choice span {
  display: grid; place-items: center; min-width: 52px; height: 44px; border-radius: 8px;
  border: 1px solid rgb(255 255 255 / .2); color: var(--ink); font-weight: 600; cursor: pointer;
}
.choice input:checked + span { background: var(--accent); border-color: var(--accent); color: #241c08; }
.choice input:focus-visible + span { outline: 2px solid var(--accent); outline-offset: 2px; }
.card-page .check { display: flex; gap: 10px; align-items: flex-start; padding: 10px 0; border-top: 1px solid var(--felt-line); margin: 0; line-height: 1.4; }
.check input { width: 20px; height: 20px; margin: 1px 0 0; accent-color: var(--accent); flex: none; }
.check b { display: block; color: var(--ink); }
.error { color: var(--danger); margin-top: 16px; }
.seats { list-style: none; padding: 0; margin: 0 0 24px; display: grid; gap: 6px; }
.seats li { padding: 10px 12px; border-radius: 6px; background: rgb(0 0 0 / .2); border: 1px solid var(--felt-line); }
.seats li.empty { color: var(--muted); border: 1px dashed rgb(255 255 255 / .3); background: none; }
.copy { display: flex; gap: 8px; }
.copy input { font-size: 14px !important; }

/* ---------- table layout ---------- */
.table {
  height: 100dvh; display: grid; gap: 6px; padding: 8px 12px 6px;
  grid-template: "bar bar bar" auto "left board right" 1fr "rack rack rack" auto / minmax(84px, 13vw) 1fr minmax(84px, 13vw);
}
.bar { grid-area: bar; display: flex; align-items: center; gap: 10px; font-size: 13px; color: var(--muted); }
.bar .round { font-weight: 600; color: var(--ink); }
.bar .spacer { flex: 1; }
.bar button { font-size: 13px; padding: 6px 10px; border-radius: 6px; }

/* ---------- players ---------- */
.seat { display: flex; align-items: center; gap: 8px; min-width: 0; }
.seat.top { grid-area: bar; justify-self: center; }
.seat.left { grid-area: left; flex-direction: column; justify-content: center; }
.seat.right { grid-area: right; flex-direction: column; justify-content: center; }
@property --p { syntax: "<percentage>"; inherits: false; initial-value: 0%; }
@keyframes ring { from { --p: var(--start); } to { --p: 0%; } }
.avatar {
  --c: #3d7a5f; width: 40px; height: 40px; border-radius: 50%; display: grid; place-items: center; flex: none;
  background: var(--c); font-weight: 700; font-size: 17px; position: relative;
  box-shadow: 0 0 0 2px rgb(0 0 0 / .25);
}
.avatar.offline { opacity: .45; }
.avatar.turn { box-shadow: 0 0 0 3px var(--accent); }
.avatar.turn::after { /* time left, as a ring */
  content: ""; position: absolute; inset: -6px; border-radius: 50%;
  background: conic-gradient(var(--accent) 0 var(--p), transparent var(--p));
  animation: ring linear forwards; animation-duration: inherit;
  -webkit-mask: radial-gradient(farthest-side, transparent calc(100% - 3px), #000 calc(100% - 2px));
          mask: radial-gradient(farthest-side, transparent calc(100% - 3px), #000 calc(100% - 2px));
}
.who { display: grid; gap: 1px; text-align: center; min-width: 0; }
.seat.top .who { text-align: left; }
.who .name { font-weight: 600; font-size: 14px; overflow-wrap: anywhere; }
.who .meta { font-size: 12px; color: var(--muted); }
.meta span + span::before { content: " · "; }
.seat.left .meta span, .seat.right .meta span { display: block; }
.seat.left .meta span::before, .seat.right .meta span::before { content: none; }
.badge { display: inline-block; font-size: 11px; padding: 1px 6px; border-radius: 4px; background: rgb(240 192 74 / .18); color: var(--accent); font-weight: 600; }

/* ---------- tiles ---------- */
.tile {
  width: var(--tw); height: calc(var(--tw) * 1.36); flex: none;
  background: var(--tile); border-radius: calc(var(--tw) * .12);
  box-shadow: inset 0 calc(var(--tw) * -.08) 0 var(--tile-edge), 0 1px 2px rgb(0 0 0 / .4);
  display: grid; place-items: start center; padding-top: calc(var(--tw) * .12);
  font-weight: 800; font-size: calc(var(--tw) * .52); line-height: 1; position: relative;
  font-variant-numeric: tabular-nums; user-select: none; -webkit-user-select: none;
}
.tile::after {
  content: ""; position: absolute; left: 50%; bottom: calc(var(--tw) * .3);
  width: calc(var(--tw) * .17); height: calc(var(--tw) * .17); border-radius: 50%;
  background: currentColor; transform: translateX(-50%);
}
.c0 { color: var(--red); } .c1 { color: var(--yellow); } .c2 { color: var(--blue); } .c3 { color: var(--black); }
.tile.okey { box-shadow: inset 0 calc(var(--tw) * -.08) 0 var(--accent), 0 0 0 2px var(--accent), 0 1px 2px rgb(0 0 0 / .4); }
.tile.fj { color: var(--black); font-size: calc(var(--tw) * .26); padding-top: calc(var(--tw) * .3); text-align: center; }
.tile.fj::after { display: none; }
.tile.back { background: #e2d5b4; display: block; } .tile.back::after { display: none; }
.tile.sel { transform: translateY(-8px); outline: 2px solid #fff; outline-offset: 1px; }
.tile.can::before, .tile.taken::before { /* yardımlı: can be laid off; taken: must be used */
  content: ""; position: absolute; top: -4px; right: -4px; width: 10px; height: 10px; border-radius: 50%; z-index: 1;
  background: #1d8a52; box-shadow: 0 0 0 1.5px #0b3d22;
}
.tile.taken::before { width: 11px; height: 11px; background: var(--accent); box-shadow: 0 0 0 2px #4a3800; }
.sm { --tw: clamp(18px, min(2.7vw, 4.6vh), 32px); }
.ghost { position: fixed; z-index: 10; pointer-events: none; transform: translate(-50%, -60%); opacity: .9; }

/* ---------- board: melds on the table, deck, indicator ---------- */
.board {
  grid-area: board; display: grid; grid-template-columns: 1fr auto; gap: 10px; min-height: 0; position: relative;
  background: rgb(0 0 0 / .14); border: 1px solid var(--felt-line); border-radius: 12px; padding: 8px 10px;
}
.melds { display: grid; align-content: start; gap: 6px; overflow: auto; min-height: 0; }
.owner { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
.owner .label { font-size: 12px; color: var(--muted); width: 64px; flex: none; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.group { display: flex; gap: 1px; padding: 3px; border-radius: 6px; }
.group.can { background: rgb(240 192 74 / .2); outline: 1px dashed var(--accent); cursor: pointer; }
.group.over-target { background: rgb(255 255 255 / .18); }
.empty-note { font-size: 13px; color: var(--muted); }
.center { display: grid; align-content: center; justify-items: center; gap: 6px; padding-left: 10px; border-left: 1px solid var(--felt-line); }
.deck::before, .pile::before { content: ""; position: absolute; inset: -8px; } /* larger tap area */
.deck { position: relative; padding: 0; border: 0; background: none; border-radius: 6px; touch-action: none; }
.deck .tile { box-shadow: 0 2px 0 #b7a986, 0 4px 0 #a39570, 0 5px 4px rgb(0 0 0 / .4); }
.deck .count { position: absolute; inset: 0; display: grid; place-items: center; font-weight: 700; color: #4a4232; font-size: 14px; }
.deck.can-take { outline: 2px solid var(--accent); outline-offset: 3px; }
.caption { font-size: 11px; color: var(--muted); text-align: center; }

/* round over: all hands and the scores over the board */
.board.over > :not(.result) { visibility: hidden; }
.result { position: absolute; inset: 0; overflow: auto; padding: 10px 12px; display: grid; grid-template-columns: 1fr auto; align-content: start; gap: 8px; }
.result > * { grid-column: 1 / -1; }
.result h2 { grid-column: 1; grid-row: 1; align-self: center; }
.result .result-actions { grid-column: 2; grid-row: 1; }
.result h2 { font-size: 16px; margin: 0; }
.result p { margin: 0; color: var(--accent); font-weight: 600; font-size: 14px; }
.hands { display: grid; gap: 4px; max-width: 820px; }
.hand-row { display: grid; grid-template-columns: 64px 1fr auto auto; align-items: center; gap: 8px; }
.hand-row .tiles.sm { --tw: clamp(20px, min(2.9vw, 5.8vh), 30px); }
.hand-row .sum { font-size: 12px; color: var(--muted); min-width: 72px; text-align: right; font-variant-numeric: tabular-nums; }
.hand-row .label { font-size: 12px; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.hand-row .tiles { display: flex; flex-wrap: wrap; gap: 1px; }
.hand-row .score { font-variant-numeric: tabular-nums; min-width: 44px; text-align: right; }
.result-actions { display: flex; gap: 8px; }
.result-actions button { padding: 6px 12px; }

/* ---------- discard piles ---------- */
.pile { position: relative; display: grid; justify-items: center; padding: 0; border: 0; background: none; border-radius: 6px; }
.pile .slot { display: block; width: var(--tw); height: calc(var(--tw) * 1.36); border-radius: 6px; border: 1px dashed rgb(255 255 255 / .35); }
.pile.can-take .tile, .pile.can-drop .slot, .pile.can-drop .tile, .pile.over-target .slot, .pile.over-target .tile { outline: 2px solid var(--accent); outline-offset: 2px; }
#take { touch-action: none; }

/* ---------- my area ---------- */
.mine { grid-area: rack; display: grid; grid-template-columns: auto minmax(0, 1fr) auto auto; gap: 10px; align-items: end; }
.side { display: grid; justify-items: center; gap: 3px; }
.rack-wrap { display: grid; gap: 4px; min-width: 0; }
.open-meter { display: flex; align-items: center; gap: 8px; font-size: 13px; min-height: 26px; flex-wrap: wrap; }
.open-meter .bar-bg { flex: 1; max-width: 200px; min-width: 60px; height: 6px; border-radius: 3px; background: rgb(0 0 0 / .3); overflow: hidden; }
.open-meter .bar-bg i { display: block; height: 100%; background: var(--accent); }
.open-meter b { color: var(--accent); font-variant-numeric: tabular-nums; }
.open-meter .pairs { color: var(--muted); }
.open-meter .avatar { width: 28px; height: 28px; font-size: 13px; margin-left: 6px; }
.open-meter .avatar.turn::after { inset: -5px; }
.open-meter .warn { color: var(--danger); font-weight: 600; }
.status { color: var(--muted); }
.status strong { color: var(--ink); }
.rack {
  display: grid; grid-template-columns: repeat(15, var(--tw)); gap: 2px 3px; justify-content: center;
  padding: 6px 8px 8px; border-radius: 8px;
  background: linear-gradient(var(--wood), var(--wood-2));
  box-shadow: inset 0 2px 0 rgb(255 255 255 / .12), 0 4px 10px rgb(0 0 0 / .35);
  position: relative;
}
.rack::after { /* ledge between rows */
  content: ""; position: absolute; left: 6px; right: 6px; top: calc(6px + var(--tw) * 1.36);
  height: 2px; background: rgb(0 0 0 / .3); pointer-events: none;
}
.rack .cell { height: calc(var(--tw) * 1.36); border-radius: 4px; }
.rack .cell.over-target { background: rgb(255 255 255 / .14); }
.rack .tile { cursor: grab; touch-action: none; }
.actions { display: grid; gap: 6px; }
.actions button { font-size: 14px; min-width: 92px; padding: 9px 12px; }

/* ---------- scores ---------- */
dialog {
  border: 1px solid var(--felt-line); border-radius: 12px; padding: 16px 18px; color: var(--ink);
  background: var(--felt-2); max-width: min(560px, calc(100vw - 32px)); max-height: calc(100dvh - 32px);
}
dialog::backdrop { background: rgb(0 0 0 / .5); }
dialog h2 { margin: 0 0 10px; font-size: 18px; }
dialog form { margin-top: 12px; text-align: right; }
.score-table { border-collapse: collapse; font-variant-numeric: tabular-nums; width: 100%; font-size: 14px; }
.score-table th, .score-table td { padding: 4px 8px; text-align: right; border-bottom: 1px solid var(--felt-line); }
.score-table :is(th, td):first-child { text-align: left; }
.score-table tfoot th { color: var(--accent); border-bottom: 0; }
.team { margin: 8px 0 0; color: var(--muted); }

/* phones held sideways: tighter everything */
@media (max-height: 460px) {
  .table { padding: 4px 8px 4px; gap: 4px; grid-template-columns: 78px 1fr 78px; }
  .avatar { width: 32px; height: 32px; font-size: 14px; }
  .who .name { font-size: 12px; } .who .meta { font-size: 11px; }
  .owner .label, .hand-row .label { width: 48px; font-size: 11px; }
  .hand-row { grid-template-columns: 48px 1fr auto auto; }
  .hand-row .sum { min-width: 0; }
  .actions { gap: 4px; grid-template-columns: 1fr 1fr; }
  .actions button { min-width: 0; min-height: 40px; padding: 4px 8px; font-size: 13px; }
  .bar { font-size: 12px; } .bar button { padding: 4px 10px; min-height: 34px; }
  .open-meter { font-size: 12px; min-height: 28px; }
}
.rotate { display: grid; place-items: center; height: 100dvh; text-align: center; padding: 24px; color: var(--muted); }
.rotate strong { display: block; color: var(--ink); font-size: 20px; margin-bottom: 6px; }
@media (orientation: portrait) and (max-width: 700px) {
  .table { display: none !important; }
}
