/* The felt card table, trimmed down from the design mockup. */
@font-face { font-family: "Alegreya SC"; font-weight: 400; font-display: swap; src: url(fonts/alegreya-sc-latin-400-normal.woff2) format("woff2"); }
@font-face { font-family: "Alegreya SC"; font-weight: 500; font-display: swap; src: url(fonts/alegreya-sc-latin-500-normal.woff2) format("woff2"); }
@font-face { font-family: "Alegreya SC"; font-weight: 700; font-display: swap; src: url(fonts/alegreya-sc-latin-700-normal.woff2) format("woff2"); }
@font-face { font-family: "Alegreya"; font-weight: 700; font-display: swap; src: url(fonts/alegreya-latin-700-normal.woff2) format("woff2"); }
@font-face { font-family: "Atkinson Hyperlegible"; font-weight: 400; font-display: swap; src: url(fonts/atkinson-hyperlegible-latin-400-normal.woff2) format("woff2"); }
@font-face { font-family: "Atkinson Hyperlegible"; font-weight: 400; font-style: italic; font-display: swap; src: url(fonts/atkinson-hyperlegible-latin-400-italic.woff2) format("woff2"); }
@font-face { font-family: "Atkinson Hyperlegible"; font-weight: 700; font-display: swap; src: url(fonts/atkinson-hyperlegible-latin-700-normal.woff2) format("woff2"); }

:root {
  --felt: #2f6a4c; --felt-deep: #255a3f; --felt-line: #3f7f5e;
  --rail: #f3efe4; --rail-ink: #23302a; --rail-muted: #5f6b64; --rail-line: #d9d3c3;
  --card: #fbf8f0; --card-edge: #cfc7b3; --ink-black: #1c2420; --ink-red: #b3262f;
  --on-felt: #f2efe3; --on-felt-muted: #bfd3c6;
  --brass: #d7a740; --brass-ink: #2a1f05; --new: #79c8ff; --down: #f0b7a2; --err: #b3262f; --win: #2f6a4c;
  --page: #e7e2d4; --shadow: rgba(10, 30, 20, .35);
  --f-display: "Alegreya SC", "Palatino Linotype", Palatino, "URW Palladio L", Georgia, serif;
  --f-card: "Alegreya", "Palatino Linotype", Palatino, "URW Palladio L", Georgia, serif;
  --f-ui: "Atkinson Hyperlegible", "Segoe UI", system-ui, sans-serif;
  --f-mono: ui-monospace, "DejaVu Sans Mono", Menlo, monospace;
}
@media (prefers-color-scheme: dark) {
  :root {
    --felt: #1d4632; --felt-deep: #163a29; --felt-line: #2a5a42;
    --rail: #1b2420; --rail-ink: #e6e9e4; --rail-muted: #9aa8a0; --rail-line: #33413a;
    --card: #ece7da; --card-edge: #a59d89; --on-felt: #eae8dc; --on-felt-muted: #9fbcaa;
    --brass: #d1a03a; --brass-ink: #241a03; --new: #6cb8ec; --down: #e2a690; --err: #f08a8a; --win: #7fd1a3;
    --page: #111815; --shadow: rgba(0, 0, 0, .5);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
body {
  margin: 0; background: var(--page); color: var(--rail-ink);
  font-family: var(--f-ui); font-size: 15px; line-height: 1.45;
  padding: 20px max(16px, env(safe-area-inset-right, 0px)) 32px max(16px, env(safe-area-inset-left, 0px));
}
.wrap { max-width: 1760px; margin: 0 auto; }
.layout { display: grid; grid-template-columns: minmax(0, 1fr) clamp(300px, 24vw, 380px); gap: 18px; align-items: start; }
@media (max-width: 900px) { .layout { grid-template-columns: minmax(0, 1fr); } }

/* felt */
.table {
  background: radial-gradient(ellipse at 50% 35%, var(--felt) 0%, var(--felt-deep) 100%);
  color: var(--on-felt); border-radius: 22px; border: 6px solid #6b4a2b;
  box-shadow: inset 0 0 0 1px var(--felt-line), 0 10px 30px var(--shadow);
  padding: 18px 20px 20px; display: grid; gap: 18px; min-width: 0;
}
.table.start { max-width: 640px; margin: 40px auto; }
.table.start h1 { font-family: var(--f-display); font-size: 40px; margin: 0; }
.table.start p { margin: 0; color: var(--on-felt-muted); }
.hand-head { display: flex; flex-wrap: wrap; gap: 6px 18px; align-items: baseline; justify-content: space-between; }
.hand-head h1 { font-family: var(--f-display); font-size: 26px; margin: 0; }
.hand-head h1 small { font-size: 16px; color: var(--on-felt-muted); font-weight: 500; }
.contract { font-family: var(--f-display); font-size: 17px; border: 1px solid var(--brass); color: var(--brass); padding: 3px 12px; border-radius: 999px; }
.head-right { display: inline-flex; flex-wrap: wrap; gap: 8px; align-items: baseline; }
.clock { font-size: 15px; color: var(--on-felt-muted); font-variant-numeric: tabular-nums; }
.label { font-size: 12px; text-transform: uppercase; letter-spacing: .1em; color: var(--on-felt-muted); }

/* players */
.players { display: grid; grid-template-columns: repeat(auto-fit, minmax(130px, 1fr)); gap: 10px; }
.seat { border: 1px solid var(--felt-line); border-radius: 12px; padding: 9px 11px; display: grid; gap: 3px; background: rgba(0,0,0,.12); }
.seat.turn { box-shadow: 0 0 0 2px var(--brass) inset; border-color: var(--brass); }
.seat .name { display: flex; gap: 6px; align-items: center; flex-wrap: wrap; font-weight: 700; }
.seat .stats { font-size: 13px; color: var(--on-felt-muted); font-variant-numeric: tabular-nums; }
.buy-dots { color: var(--brass); letter-spacing: .08em; }
.seat .style { font-size: 12px; font-style: italic; color: var(--brass); }
.pill { font-size: 11px; font-weight: 700; letter-spacing: .06em; padding: 1px 7px; border-radius: 999px; text-transform: uppercase; }
.pill.down { background: var(--down); color: var(--brass-ink); }
.pill.turn { background: var(--brass); color: var(--brass-ink); }

/* cards */
.card {
  --w: 58px; width: var(--w); aspect-ratio: 5 / 7; flex: none; position: relative;
  background: var(--card); color: var(--ink-black); border: 1px solid var(--card-edge);
  border-radius: calc(var(--w) * .12); box-shadow: 0 2px 4px var(--shadow);
  font-family: var(--f-card); font-weight: 700; display: grid; place-items: center;
}
.card.red { color: var(--ink-red); }
.card .ix { position: absolute; top: 4%; left: 7%; line-height: .95; font-size: calc(var(--w) * .34); text-align: center; }
.card .ix span { display: block; font-size: .85em; }
.card .pip { font-size: calc(var(--w) * .58); line-height: 1; transform: translate(calc(var(--w) * .1), calc(var(--w) * .2)); }
.card.joker .pip { font-family: var(--f-display); font-size: calc(var(--w) * .27); writing-mode: vertical-rl; letter-spacing: .05em; transform: translate(calc(var(--w) * .14), calc(var(--w) * .06)); }
.card.joker .ix { font-size: calc(var(--w) * .24); }
.card.sm { --w: 40px; margin-left: -12px; }
.card.sm:first-child { margin-left: 0; }
.card.back { --w: 70px; background: repeating-linear-gradient(45deg, #8c2f36 0 6px, #7a262d 6px 12px); border: 3px solid var(--card); }
/* card back designs: each game gets one at random (BACKS in session.py) */
.card.back-blue { background: repeating-linear-gradient(45deg, #2b4a7a 0 6px, #223c66 6px 12px); }
.card.back-green {
  background:
    repeating-linear-gradient(45deg, transparent 0 7px, #3f8a5f 7px 8px),
    repeating-linear-gradient(-45deg, transparent 0 7px, #3f8a5f 7px 8px),
    #1f5136;
}
.card.back-crest { background: radial-gradient(circle at 50% 50%, #9a3640 0%, #6e2229 75%); }
.card.back-crest::after {
  content: "♠"; font-size: calc(var(--w) * .45); line-height: 1; color: var(--brass);
  border: 2px solid var(--brass); border-radius: 50%; width: 62%; aspect-ratio: 1; display: grid; place-items: center;
}
.card.empty { --w: 70px; background: transparent; border: 2px dashed var(--felt-line); box-shadow: none; }
.pile .card { --w: 70px; }

/* middle */
.middle { display: grid; grid-template-columns: auto minmax(0, 1fr); gap: 22px; align-items: start; }
@media (max-width: 640px) { .middle { grid-template-columns: minmax(0, 1fr); } }
.piles { display: flex; gap: 18px; }
.pile { display: grid; gap: 6px; justify-items: center; }
.pile .cap { font-size: 13px; color: var(--on-felt-muted); }
.melds { display: grid; gap: 10px; min-width: 0; }
.meld-row { display: flex; flex-wrap: wrap; gap: 10px 14px; }
.meld { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; border: 1px dashed var(--felt-line); border-radius: 12px; padding: 7px 9px; }
.meld .no { font-family: var(--f-display); color: var(--brass); }
.meld .fan { display: flex; }
.meld .owner { font-size: 12px; color: var(--on-felt-muted); }
.empty-note { color: var(--on-felt-muted); font-style: italic; }

/* hand */
.hand-area h2 { font-family: var(--f-display); font-size: 19px; margin: 0 0 4px; }
/* long hands wrap onto another row rather than running off the side */
.hand { display: flex; flex-wrap: wrap; row-gap: 34px; padding: 22px 8px 24px 30px; }
.hand .slot { margin-left: -18px; position: relative; }
.hand .card { --w: 76px; }
.hand .card.new { box-shadow: 0 0 0 3px var(--new), 0 2px 4px var(--shadow); transform: translateY(-10px); }
.hand .num { position: absolute; left: 50%; bottom: -20px; transform: translateX(-50%); font-size: 12px; color: var(--on-felt-muted); font-variant-numeric: tabular-nums; }

/* console */
.console { border-top: 1px solid var(--felt-line); padding-top: 14px; display: grid; gap: 10px; }
.prompt { font-weight: 700; font-size: 16px; margin: 0; display: block; }
.prompt.waiting { color: var(--on-felt-muted); font-weight: 400; font-style: italic; }
.last-move { font-size: 13px; color: var(--on-felt-muted); margin: 2px 0 0; }
.typed summary { cursor: pointer; color: var(--on-felt-muted); font-size: 14px; }
.typed .row { display: flex; gap: 8px; margin-top: 8px; }
.typed input { flex: 1; min-width: 0; font: 16px var(--f-mono); padding: 9px 12px; border-radius: 10px; border: 1px solid var(--felt-line); background: var(--card); color: var(--ink-black); }
button { font: inherit; font-weight: 700; border-radius: 10px; padding: 9px 18px; border: none; background: var(--brass); color: var(--brass-ink); cursor: pointer; }
button:focus-visible, input:focus-visible, select:focus-visible { outline: 3px solid var(--new); outline-offset: 2px; }
button.ghost { background: transparent; color: var(--on-felt); box-shadow: inset 0 0 0 1px var(--on-felt-muted); }
button[disabled] { background: transparent; color: var(--on-felt-muted); box-shadow: inset 0 0 0 1px var(--felt-line); cursor: not-allowed; opacity: .7; }
button.link { background: none; color: var(--rail-muted); padding: 0; font-weight: 400; text-decoration: underline; }
.new-game { display: flex; flex-wrap: wrap; gap: 12px 18px; align-items: center; }
.new-game input, .new-game select { font: inherit; padding: 5px 8px; border-radius: 8px; border: 1px solid var(--felt-line); background: var(--card); color: var(--ink-black); }
.new-game input[type=number] { width: 4.5em; }

/* rail */
.rail { display: grid; gap: 16px; min-width: 0; }
.panel { background: var(--rail); border: 1px solid var(--rail-line); border-radius: 14px; padding: 14px 16px; min-width: 0; }
.panel h3, .panel summary { font-family: var(--f-display); font-size: 17px; margin: 0 0 8px; font-weight: 700; cursor: default; }
.panel summary { cursor: pointer; }
/* newest line last, and the list stays scrolled to it (items are reversed in the template) */
.log { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column-reverse; gap: 4px; font-size: 14px; max-height: 60vh; overflow-y: auto; }
.log li { padding-left: 10px; border-left: 2px solid var(--rail-line); }
.log li.mark { font-family: var(--f-display); border: none; padding: 6px 0 0; color: var(--rail-muted); }
.log li.you { border-left-color: var(--new); }
.log li.err { border-left-color: var(--err); color: var(--err); }
pre { font: 13px/1.4 var(--f-mono); margin: 0; overflow-x: auto; }
.meta { font-size: 13px; color: var(--rail-muted); margin: 10px 0 4px; }

/* ----- step 2: clicking ----- */
.actions { display: flex; flex-wrap: wrap; gap: 10px; align-items: center; }
.actions .spacer { flex: 1; }

/* the piles are the draw buttons */
.pile { background: none; padding: 4px; border-radius: 12px; color: inherit; font-weight: 400; }
button.pile:not([disabled]) { cursor: pointer; box-shadow: 0 0 0 2px var(--brass); }
@media (hover: hover) { button.pile:not([disabled]):hover .card { transform: translateY(-4px); } }
.pile[disabled] { box-shadow: none; opacity: 1; cursor: default; }

/* lay-off targets on table melds */
.lay { font-size: 12px; padding: 4px 9px; border-radius: 8px; }
.lay.end { padding: 4px 7px; background: transparent; color: var(--brass); box-shadow: inset 0 0 0 1px var(--brass); }

/* selecting cards in your hand */
.hand-bar { display: flex; flex-wrap: wrap; gap: 6px 14px; align-items: baseline; justify-content: space-between; }
.sort { font-size: 13px; color: var(--on-felt-muted); display: inline-flex; gap: 6px; align-items: center; }
.chip { font-size: 12px; font-weight: 400; padding: 3px 10px; border-radius: 999px; background: transparent; color: var(--on-felt); box-shadow: inset 0 0 0 1px var(--felt-line); }
.hand .slot input { position: absolute; opacity: 0; pointer-events: none; }
.hand.picking .slot { cursor: pointer; }
.hand .card { transition: transform .12s ease; }
/* hover lifts only where there's a real hover: on touch screens it sticks after a tap */
@media (hover: hover) { .hand.picking .slot:hover .card { transform: translateY(-5px); } }
.hand .slot input:checked + .card { transform: translateY(-18px); box-shadow: 0 0 0 3px var(--brass), 0 6px 12px var(--shadow); }
.hand .slot input:focus-visible + .card { outline: 3px solid var(--new); outline-offset: 2px; }
.hand .card.staged { opacity: .45; transform: none; }
.hand .badge { position: absolute; top: -10px; right: 4px; font-size: 11px; font-weight: 700; background: var(--brass); color: var(--brass-ink); border-radius: 999px; padding: 0 6px; }
@media (prefers-reduced-motion: reduce) { .hand .card, .pile .card { transition: none; } }

/* melds being built */
.tray { display: flex; flex-wrap: wrap; gap: 10px 14px; }
.staged { display: flex; align-items: center; gap: 8px; padding: 6px 10px; border-radius: 12px; background: rgba(0,0,0,.18); border: 1px solid var(--felt-line); }
.staged .no { font-family: var(--f-display); color: var(--brass); }
.staged .fan { display: flex; }
.check { font-size: 13px; color: var(--select, var(--brass)); font-weight: 700; }
.check.bad { color: var(--down); }

/* the "May I?" offer */
.buy { display: flex; gap: 16px; align-items: flex-start; background: rgba(0,0,0,.2); border: 1px solid var(--brass); border-radius: 14px; padding: 12px 14px; }
.buy p { margin: 0 0 8px; }
.buy .prompt { font-family: var(--f-display); font-size: 22px; }
.timer { margin-top: 12px; }
.timer .secs { font-family: var(--f-display); font-size: 30px; font-weight: 700; line-height: 1; color: var(--brass);
  font-variant-numeric: tabular-nums; }

/* what the game just told you */
.messages { list-style: none; margin: 0; padding: 0; display: grid; gap: 4px; font-size: 14px; }
.messages li { padding: 4px 10px; border-left: 3px solid var(--new); background: rgba(0,0,0,.15); border-radius: 0 8px 8px 0; }
.messages li.err { border-left-color: var(--down); }
.messages li.notice { border-left-color: var(--brass); color: var(--brass); font-weight: 700; }
.start-notice { color: var(--brass); font-weight: 700; }

/* ----- results and scores ----- */
.result { display: grid; gap: 12px; background: rgba(0,0,0,.22); border: 2px solid var(--brass); border-radius: 16px; padding: 16px 18px; }
.result .headline { font-family: var(--f-display); font-size: 28px; font-weight: 700; color: var(--brass); margin: 0; text-wrap: balance; }
.result p { margin: 0; }
.result.final .headline { font-size: 34px; }
.hand-points { border-collapse: collapse; font-variant-numeric: tabular-nums; }
.hand-points th, .hand-points td { padding: 4px 14px 4px 0; text-align: right; }
.hand-points th:first-child, .hand-points td:first-child { text-align: left; color: var(--on-felt-muted); }
.hand-points td.zero { color: var(--brass); font-weight: 700; }
.scroll-x { overflow-x: auto; }
.scores { border-collapse: collapse; width: 100%; font-variant-numeric: tabular-nums; font-size: 14px; }
.scores th, .scores td { padding: 3px 6px; text-align: right; border-bottom: 1px solid var(--rail-line); white-space: nowrap; }
.scores th:first-child, .scores td:first-child { text-align: left; color: var(--rail-muted); }
.scores tfoot td { font-weight: 700; border-bottom: none; }
.scores td.zero { color: var(--win); font-weight: 700; }
.scores td.lead { color: var(--win); }
.scores td:first-child { white-space: normal; }
.scores .sub { display: block; font-size: 11px; line-height: 1.3; }
.scores .nobody { font-style: italic; }
/* the same table on the felt, in the final result card */
.result .scores th, .result .scores td { border-bottom-color: var(--felt-line); }
.result .scores th:first-child, .result .scores td:first-child { color: var(--on-felt-muted); }
.result .scores td.zero, .result .scores td.lead { color: var(--brass); }

/* ----- step 3: live updates ----- */
#play { display: contents; }    /* the form wraps the table without becoming a box in its layout */
.console:empty { display: none; }

/* ----- step 4: drag and drop ----- */
.hand .slot[draggable="true"] { cursor: grab; }
.hand .slot.dragging .card { opacity: .4; }
[data-drop] { transition: box-shadow .12s ease; }
.drop-ok { box-shadow: 0 0 0 3px var(--brass), 0 0 18px var(--brass) !important; border-radius: 12px; }
.meld-slots { display: grid; grid-template-columns: repeat(auto-fill, minmax(190px, 1fr)); gap: 12px; margin-bottom: 12px; }
.meld-slot {
  display: flex; flex-direction: column; align-items: flex-start; gap: 8px;
  min-height: 132px; padding: 10px 12px; border-radius: 12px;
  border: 2px dashed var(--felt-line); background: rgba(0,0,0,.10);
}
.meld-slot.filled { border-style: solid; background: rgba(0,0,0,.18); }
.meld-slot .slot-head { display: flex; align-items: center; gap: 8px; width: 100%; font-size: 13px; color: var(--on-felt-muted); }
.meld-slot .no { font-family: var(--f-display); font-size: 17px; color: var(--brass); }
.meld-slot .x { margin-left: auto; padding: 0 8px; min-height: 0; font-size: 13px; background: transparent; color: var(--on-felt-muted); border: 1px solid var(--felt-line); }
.meld-slot .drop-hint { flex: 1; display: grid; place-items: center; width: 100%; color: var(--on-felt-muted); font-size: 13px; }
.meld-slot .fan { display: flex; flex-wrap: wrap; row-gap: 6px; }
.meld-slot .chip { margin-top: auto; }
.meld-slot.drop-ok { border-style: solid; border-color: var(--brass); }
@media (prefers-reduced-motion: reduce) { [data-drop] { transition: none; } }

/* ----- step 5: phones and tablets ----- */
button, .hand .slot, [data-drop] { touch-action: manipulation; -webkit-tap-highlight-color: transparent; }
.new-game input, .new-game select { font-size: 16px; }    /* below 16px, iOS zooms in on focus */

/* tablet portrait and smaller: the rail moves under the table, its panels side by side */
@media (max-width: 900px) {
  .rail { grid-template-columns: repeat(auto-fit, minmax(280px, 1fr)); align-items: start; }
  .happened { display: none; }      /* the messages over your hand say what matters on a small screen */
  .typed { display: none; }         /* typing commands is for a keyboard */
}

/* phones */
@media (max-width: 600px) {
  body { padding-top: 10px; padding-bottom: 20px; font-size: 14px; }
  .table { border-width: 4px; border-radius: 16px; padding: 12px 12px 14px; gap: 14px; }
  .hand-head h1 { font-size: 22px; }
  .contract { font-size: 14px; padding: 2px 10px; }
  .players { grid-template-columns: repeat(auto-fit, minmax(120px, 1fr)); gap: 6px; }
  .seat { padding: 6px 8px; gap: 1px; }
  .seat .stats { font-size: 12px; }
  .middle { gap: 12px; }
  .piles { gap: 12px; }
  .pile .card, .card.back, .card.empty { --w: 54px; }
  .hand { row-gap: 30px; padding: 20px 4px 22px 26px; }
  .hand .card { --w: 56px; }
  .hand .slot { margin-left: -24px; }                       /* still shows each card's corner index */
  .hand .slot input:checked + .card { transform: translateY(-14px); }
  .meld-slots { grid-template-columns: repeat(auto-fill, minmax(140px, 1fr)); gap: 8px; }
  .meld-slot { min-height: 96px; padding: 8px 10px; }
  .actions { gap: 8px; }
  .actions button { flex: 1 1 auto; }                       /* full-width-ish buttons that are easy to hit */
  .actions .spacer { display: none; }
  .buy { gap: 12px; padding: 10px 12px; }                   /* short, so it fits above the docked hand */
  .buy .card { --w: 46px; flex: none; }
  .buy .prompt { font-size: 19px; margin-bottom: 4px; }
  .timer { margin-top: 8px; }
  .result { padding: 12px 14px; }
  .result .headline { font-size: 22px; }
  .result.final .headline { font-size: 26px; }
  .hand-points th, .hand-points td { padding-right: 10px; }
}

/* fingers rather than a mouse: bigger targets for the small buttons */
@media (pointer: coarse) {
  button { min-height: 44px; }
  .chip, .lay, .lay.end, .meld-slot .x { min-height: 36px; padding-left: 12px; padding-right: 12px; }
  button.link { min-height: 0; }
}

/* Phones and tablets held upright: your hand docks at the bottom of the screen and the
   table scrolls above it, with the prompt, meld slots and buttons just over the hand.
   The form is display: contents, so its sections are grid items of .table and can be reordered. */
@media (max-width: 900px) and (min-height: 600px) {
  .table { --pad: 20px; }
  .console.moves { order: 1; }
  .console.notes { order: 2; }
  .hand-area {
    order: 3; position: sticky; bottom: 0; z-index: 2;
    margin: 0 calc(-1 * var(--pad)) calc(-1 * var(--pad));
    padding: 8px var(--pad) max(6px, env(safe-area-inset-bottom, 0px));
    background: var(--felt-deep); border-top: 1px solid var(--brass);
    border-radius: 0 0 16px 16px; box-shadow: 0 -8px 18px var(--shadow);
  }
  .hand { max-height: 42vh; overflow-y: auto; }
  .hand-area.ended { position: static; box-shadow: none; }  /* game over: don't cover the final scores */
}
@media (max-width: 600px) and (min-height: 600px) {
  .table { --pad: 12px; }
  .hand-area h2 { font-size: 16px; margin: 0; }
}

/* phones: the seats become one strip you swipe sideways, and table melds get a little smaller */
@media (max-width: 600px) {
  .players { display: flex; overflow-x: auto; gap: 6px; padding-bottom: 2px; scrollbar-width: none; }
  .players .seat { flex: 0 0 auto; min-width: 112px; }
  .meld .card.sm, .meld-slot .card.sm { --w: 34px; margin-left: -10px; }
  .meld .card.sm:first-child, .meld-slot .card.sm:first-child { margin-left: 0; }
  .meld { padding: 5px 7px; gap: 6px; }
  .meld-row { gap: 8px; }
}
