@font-face { font-family: "Alfa Slab One"; src: url(../fonts/alfa-slab-one.woff2) format("woff2"); font-weight: 400; font-display: swap; }
@font-face { font-family: "Caveat Brush"; src: url(../fonts/caveat-brush.woff2) format("woff2"); font-weight: 400; font-display: swap; }
@font-face { font-family: "Figtree"; src: url(../fonts/figtree-latin-400-normal.woff2) format("woff2"); font-weight: 400; font-display: swap; unicode-range: U+0000-00FF, U+2000-206F; }
@font-face { font-family: "Figtree"; src: url(../fonts/figtree-latin-ext-400-normal.woff2) format("woff2"); font-weight: 400; font-display: swap; unicode-range: U+0100-024F, U+0259, U+1E00-1EFF; }
@font-face { font-family: "Figtree"; src: url(../fonts/figtree-latin-600-normal.woff2) format("woff2"); font-weight: 600; font-display: swap; unicode-range: U+0000-00FF, U+2000-206F; }
@font-face { font-family: "Figtree"; src: url(../fonts/figtree-latin-ext-600-normal.woff2) format("woff2"); font-weight: 600; font-display: swap; unicode-range: U+0100-024F, U+0259, U+1E00-1EFF; }

/* ==========================================================================
   THEME. Every colour, font, material and radius lives in this one block.
   The 3D board (js/board3d.js) reads the same variables, so restyling is done here.
   Direction: Alex's vintage Greek cafe backgammon set and its hand-painted poster.
   Red and green on crackled cream, mustard, light beech, black brush lettering.
   ========================================================================== */
:root {
  /* surfaces and text */
  --table: #6f4524;         /* wooden table under the board, also the page background */
  --paper: #f4ecd8;         /* cards and panels: the cream of the board */
  --paper-2: #e9dfc6;       /* inputs, quiet fills */
  --ink: #1b1915;           /* black sign lettering */
  --ink-soft: #595043;      /* secondary text, 7:1 on paper */
  --line: rgba(27, 25, 21, 0.22);
  --green: #1c6a3a;         /* bottle green: the bands */
  --green-deep: #134a28;
  --on-green: #f7f0dc;
  --on-green-soft: #cfe0c8;
  --mustard: #ecae24;       /* poster yellow: whose turn, scores, markers */
  --accent: #c7222b;        /* poster red: the one primary action */
  --accent-hi: #d9333a;
  --on-accent: #fff6e6;
  --error: #a3161f;
  /* board materials (read by board3d.js) */
  --wood: #cf9a58;          /* varnished beech frame */
  --surface: #f3f0e6;       /* printed off-white playing surface */
  --crackle: #b3a88c;
  --tray: #e6e0cf;
  --point-a: #c7222b;
  --point-b: #1c7a40;
  --mark-edge: #5e0f14;
  --checker-w: #dc2f2f;     /* first player: red, the lighter of the two */
  --checker-b: #0f6a34;     /* second player: green, darker, with a pale ring in the dish */
  --checker-ring: #f1e9d2;
  --die: #f8f3e6;
  --pip: #1b1915;
  --pip-one: #c7222b;
  --brass: #d9ae52;
  --highlight: #ecae24;
  /* type */
  --display: "Alfa Slab One", Georgia, serif;      /* the slab lettering of the board mark */
  --brush: "Caveat Brush", "Figtree", sans-serif;  /* hand-painted lines */
  --sans: "Figtree", system-ui, sans-serif;
  /* shape and depth */
  --r-s: 5px;
  --r-m: 8px;
  --r-pill: 999px;
  --band: 12px;             /* the green band on top and bottom of every card */
  --shadow: 0 16px 34px -14px rgba(20, 10, 0, 0.7), 0 2px 6px rgba(20, 10, 0, 0.25);
  --ease: cubic-bezier(0.16, 1, 0.3, 1);
  color-scheme: light;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; background: var(--table); color: var(--ink); font: 400 16px/1.5 var(--sans); -webkit-font-smoothing: antialiased; }
body { overflow: hidden; -webkit-tap-highlight-color: transparent; overscroll-behavior: none; }
::selection { background: var(--accent); color: var(--on-accent); }
button, input { font: inherit; color: inherit; }
input { caret-color: var(--accent); }
button { cursor: pointer; -webkit-user-select: none; user-select: none; touch-action: manipulation; }
:focus-visible { outline: 3px solid var(--mustard); outline-offset: 2px; border-radius: var(--r-s); }
[hidden] { display: none !important; }
h1, h2, p, ul { margin: 0; }
ul { padding: 0; list-style: none; }

#board { position: fixed; inset: 0; width: 100%; height: 100%; display: block; touch-action: none; }
/* the room falls away into shade around the table */
body::after { content: ""; position: fixed; inset: 0; z-index: 1; pointer-events: none; background: radial-gradient(ellipse at 50% 45%, transparent 45%, rgba(18, 8, 0, 0.5) 100%); }

/* ---------- screens and the paper card ---------- */
.screen, .sheet {
  position: fixed; inset: 0; display: flex; padding: max(16px, env(safe-area-inset-top)) 16px max(16px, env(safe-area-inset-bottom));
  overflow: auto; animation: fade 0.4s var(--ease);
}
.screen { z-index: 10; background: color-mix(in srgb, var(--table) 78%, transparent); }
.sheet { z-index: 20; background: rgba(20, 10, 0, 0.5); }
@keyframes fade { from { opacity: 0; } }
/* Cream sheet between two green bands, like the poster. */
.card {
  margin: auto; width: min(100%, 400px); padding: 26px 26px 22px; display: flex; flex-direction: column; gap: 14px; text-align: center;
  background: var(--paper); border-block: var(--band) solid var(--green); border-radius: var(--r-s);
  box-shadow: var(--shadow); animation: rise 0.45s var(--ease);
}
@keyframes rise { from { transform: translateY(14px); opacity: 0; } }
.wordmark { font: 400 clamp(3.4rem, 17vw, 4.6rem)/0.95 var(--display); color: var(--accent); text-shadow: 0 2px 0 var(--mark-edge); letter-spacing: 0.01em; }
.wordmark.small { font-size: 2.6rem; }
.tagline { font: 400 1.6rem/1 var(--brush); color: var(--green); margin-top: -8px; }
.card h2 { font: 400 2rem/1.05 var(--brush); text-wrap: balance; }
.card section h2 { font: 600 0.78rem/1 var(--sans); letter-spacing: 0.12em; text-transform: uppercase; color: var(--ink-soft); text-align: left; margin: 8px 0 6px; }

.btn {
  min-height: 48px; padding: 0 22px; border-radius: var(--r-s); border: 2px solid var(--ink); font-weight: 600; letter-spacing: 0.02em;
  transition: transform 0.15s var(--ease), background-color 0.2s, opacity 0.2s;
}
.btn:active { transform: translateY(1px) scale(0.98); }
.btn:disabled { opacity: 0.45; cursor: default; transform: none; }
.btn.primary { background: var(--accent); color: var(--on-accent); border-color: var(--mark-edge); box-shadow: 0 6px 12px -8px rgba(20, 10, 0, 0.8); }
.btn.primary:hover:not(:disabled) { background: var(--accent-hi); }
.btn.ghost { background: transparent; }
.btn.ghost:hover:not(:disabled) { background: var(--paper-2); }
.btn.big { min-height: 54px; font-size: 1.05rem; }
.link { background: none; border: 0; padding: 10px 6px; min-height: 44px; color: var(--ink-soft); text-decoration: underline; text-decoration-color: var(--line); text-underline-offset: 5px; }
.link:hover { color: var(--ink); text-decoration-color: currentColor; }
.btn-row, .link-row { display: flex; gap: 10px; justify-content: center; flex-wrap: wrap; }
.link-row.foot { border-top: 1px solid var(--line); padding-top: 6px; justify-content: space-between; }

.form { display: flex; flex-direction: column; text-align: left; }
.form label { font-size: 0.9rem; font-weight: 600; margin: 10px 0 6px 2px; }
.form input {
  width: 100%; min-height: 48px; padding: 0 16px; border-radius: var(--r-s); border: 2px solid var(--ink);
  background: var(--paper-2); transition: background-color 0.2s;
}
.form input:focus { outline: 3px solid var(--mustard); outline-offset: 1px; background: var(--on-accent); }
.form input::placeholder { color: var(--ink-soft); }
.form .btn { margin-top: 12px; }
.help { color: var(--ink-soft); font-size: 0.88rem; text-wrap: pretty; }
.form .help { margin: 6px 2px 0; }
.err { min-height: 1.4em; margin: 8px 2px 0; color: var(--error); font-size: 0.92rem; font-weight: 600; }
.join-row { display: flex; gap: 8px; }
.join-row input { flex: 1; min-width: 0; }
#code { font-weight: 600; letter-spacing: 0.16em; text-transform: uppercase; }
#code::placeholder { letter-spacing: 0; font-weight: 400; text-transform: none; }
.join-row .btn { margin: 0; }
.divider { display: flex; align-items: center; gap: 12px; color: var(--ink-soft); font-size: 0.85rem; }
.divider::before, .divider::after { content: ""; flex: 1; height: 1px; background: var(--line); }

.rows { border-top: 1px solid var(--line); }
.rows li { border-bottom: 1px solid var(--line); }
.rows .row { width: 100%; min-height: 48px; display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 6px 4px; background: none; border: 0; text-align: left; }
.rows button.row:hover { background: var(--paper-2); }
.rows .main { min-width: 0; flex: 1; display: flex; flex-direction: column; }
.rows .who { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.rows .sub { color: var(--ink-soft); font-size: 0.82rem; line-height: 1.3; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.rows .mark { flex: none; min-width: 5.6em; padding: 3px 8px; border: 2px solid var(--ink); border-radius: var(--r-pill); font-size: 0.78rem; font-weight: 600; text-align: center; }
.rows .mark.won { background: var(--green); border-color: var(--green-deep); color: var(--on-green); }
.rows .mark.lost { color: var(--ink-soft); border-color: var(--line); }
#histList { max-height: min(60vh, 520px); overflow: auto; }

/* ---------- new game: two primary buttons, the second has a target stepper next to it ---------- */
#lobby .card { width: min(100%, 460px); }
.new-game { display: grid; grid-template-columns: 1fr 1fr auto auto; gap: 8px; align-items: center; }
.new-game .btn { padding: 0 12px; white-space: nowrap; }
.step { width: 44px; height: 44px; border: 2px solid var(--ink); border-radius: var(--r-s); background: transparent; font: 600 1.35rem/1 var(--sans); }
.step:hover:not(:disabled) { background: var(--paper-2); }
.step:disabled { opacity: 0.35; cursor: default; }
.rows .state { color: var(--ink-soft); font-size: 0.9rem; flex: none; }
.rows .state.go { color: var(--green); font-weight: 600; }
.rows .tally { font: 400 1.2rem/1 var(--display); font-variant-numeric: tabular-nums; flex: none; }

/* ---------- share ---------- */
.room-code { font: 400 clamp(2.4rem, 12vw, 3.2rem)/1 var(--display); letter-spacing: 0.1em; color: var(--accent); text-shadow: 0 2px 0 var(--mark-edge); }
.fmt-line { font: 600 0.78rem/1 var(--sans); letter-spacing: 0.12em; text-transform: uppercase; color: var(--green); }
.share-link { color: var(--ink-soft); font-size: 0.85rem; overflow-wrap: anywhere; }
.waiting { display: inline-flex; gap: 10px; align-items: center; justify-content: center; font-weight: 600; }
.dot { width: 9px; height: 9px; border-radius: 50%; background: var(--green); animation: breathe 1.6s ease-in-out infinite; }
@keyframes breathe { 50% { opacity: 0.25; } }

/* ---------- dice drawn in CSS (HUD and opening roll) ---------- */
.die {
  --s: 30px; width: var(--s); height: var(--s); flex: none; display: grid; grid-template: repeat(3, 1fr) / repeat(3, 1fr); padding: calc(var(--s) * 0.17); gap: calc(var(--s) * 0.05);
  background: var(--die); border: 1.5px solid var(--ink); border-radius: 20%; box-shadow: 0 3px 5px -2px rgba(20, 10, 0, 0.6); transition: opacity 0.2s;
}
.die i { border-radius: 50%; }
.die[data-n="1"] i:nth-child(5) { background: var(--pip-one); }
.die[data-n="2"] i:is(:nth-child(1), :nth-child(9)),
.die[data-n="3"] i:is(:nth-child(1), :nth-child(5), :nth-child(9)),
.die[data-n="4"] i:is(:nth-child(1), :nth-child(3), :nth-child(7), :nth-child(9)),
.die[data-n="5"] i:is(:nth-child(1), :nth-child(3), :nth-child(5), :nth-child(7), :nth-child(9)),
.die[data-n="6"] i:is(:nth-child(1), :nth-child(3), :nth-child(4), :nth-child(6), :nth-child(7), :nth-child(9)) { background: var(--pip); }
.die.used { opacity: 0.3; }
.die.rolling { animation: tumble 0.32s linear infinite; }
@keyframes tumble { 0% { transform: rotate(-14deg) translateY(0); } 50% { transform: rotate(12deg) translateY(-7px); } 100% { transform: rotate(-14deg) translateY(0); } }

/* ---------- HUD ---------- */
#hud { position: fixed; inset: 0; z-index: 5; pointer-events: none; }
.hud-top, .hud-bottom { position: absolute; left: 0; right: 0; display: flex; justify-content: center; gap: 10px; padding-left: max(12px, env(safe-area-inset-left)); padding-right: max(12px, env(safe-area-inset-right)); }
.hud-top { top: 0; padding-top: max(12px, env(safe-area-inset-top)); align-items: stretch; }
.hud-bottom { bottom: 0; padding-bottom: max(12px, env(safe-area-inset-bottom)); }
.icon-btn, .scoreboard, .panel { pointer-events: auto; box-shadow: var(--shadow); border-radius: var(--r-s); }
.icon-btn, .scoreboard { background: var(--green); color: var(--on-green); border: 0; }
.icon-btn { width: 56px; display: grid; place-items: center; flex: none; }
.icon-btn:hover { background: var(--green-deep); }
.scoreboard { flex: 0 1 600px; min-width: 0; height: 60px; display: grid; grid-template-columns: 1fr auto 1fr; align-items: stretch; overflow: hidden; }
.plaque { min-width: 0; display: flex; align-items: center; gap: 10px; padding: 0 14px; border-bottom: 5px solid transparent; border-top: 5px solid transparent; transition: background-color 0.3s, border-color 0.3s; }
.plaque.right { justify-content: flex-end; text-align: right; }
.plaque.active { background: var(--green-deep); border-bottom-color: var(--mustard); }
.disc { position: relative; width: 22px; height: 22px; border-radius: 50%; flex: none; border: 2px solid var(--ink); box-shadow: inset 0 0 0 3px rgba(255, 255, 255, 0.18); }
.disc.w { background: var(--checker-w); }
.disc.b { background: var(--checker-b); }
.pname { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; min-width: 0; }
/* pip count: how many pips this side still has to roll to bring everything off */
.pip { font: 400 1.25rem/1 var(--display); color: var(--mustard); font-variant-numeric: tabular-nums; flex: none; }
.score { display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 3px; padding: 0 16px; background: var(--paper); color: var(--ink); font: 400 1.7rem/1 var(--display); font-variant-numeric: tabular-nums; }
.score .nums { display: flex; align-items: center; gap: 6px; }
/* match target under the match score */
#target { font: 600 0.62rem/1 var(--sans); letter-spacing: 0.1em; text-transform: uppercase; color: var(--ink-soft); white-space: nowrap; }
.score b { font-weight: 400; min-width: 0.7em; text-align: center; }
.score i { font-style: normal; color: var(--ink-soft); transform: translateY(-0.06em); }
.score.big { justify-content: center; background: none; font-size: 2.6rem; color: var(--accent); }

.panel { flex: 0 1 440px; min-width: 0; background: var(--paper); border-block: 8px solid var(--green); padding: 10px 14px 12px; display: grid; gap: 8px; }
.status-row { display: flex; align-items: center; justify-content: center; gap: 14px; min-height: 34px; }
.status { font: 400 1.9rem/1 var(--brush); text-align: center; }
.status.mine { color: var(--accent); }
.chips { display: flex; gap: 6px; }
.actions { display: flex; gap: 10px; justify-content: center; }
.actions:not(:has(.btn:not([hidden]))) { display: none; }
.actions .btn { flex: 1; }

/* ---------- opening roll ---------- */
#opening .card { gap: 12px; }
.open-row { display: grid; grid-template-columns: auto 1fr auto minmax(2ch, auto); align-items: center; gap: 12px; padding: 12px 12px; border: 1px solid var(--line); border-radius: var(--r-s); text-align: left; transition: background-color 0.4s, border-color 0.4s, opacity 0.4s; }
.open-row.win { background: color-mix(in srgb, var(--mustard) 38%, var(--paper)); border-color: var(--ink); }
.open-row.lose { opacity: 0.55; }
.open-name { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.open-dice { display: flex; gap: 8px; }
.open-dice .die { --s: 44px; }
.open-sum { font: 400 1.2rem/1 var(--display); font-variant-numeric: tabular-nums; text-align: right; }
.open-note { min-height: 2.4em; font: 400 1.7rem/1.1 var(--brush); text-wrap: balance; display: grid; place-items: center; }

#over h2 { font: 400 2.1rem/1 var(--display); color: var(--accent); text-shadow: 0 2px 0 var(--mark-edge); }

#toast { position: fixed; z-index: 30; left: 50%; top: max(88px, calc(env(safe-area-inset-top) + 80px)); transform: translateX(-50%); max-width: calc(100% - 32px); padding: 10px 16px; border-radius: var(--r-s); background: var(--ink); color: var(--paper); box-shadow: var(--shadow); font-size: 0.92rem; font-weight: 600; text-align: center; }

@media (max-width: 480px) {
  .card { padding: 26px 20px 20px; }
  .plaque { padding: 0 10px; gap: 8px; }
  .pname { font-size: 0.92rem; }
  .score { padding: 0 10px; font-size: 1.4rem; }
  #target { letter-spacing: 0.03em; }
  .new-game { grid-template-columns: 1fr auto auto; }
  .new-game #createOne { grid-column: 1 / -1; }
  .pip { font-size: 1.05rem; }
  .icon-btn { width: 48px; }
  .status { font-size: 1.7rem; }
  .open-dice .die { --s: 38px; }
}
/* phone on its side: the HUD moves to the two short edges so the board gets the full height */
@media (max-height: 520px) and (orientation: landscape) {
  .hud-top, .hud-bottom { top: 0; bottom: 0; width: 170px; flex-direction: column; justify-content: center; padding: 8px; }
  .hud-top { right: auto; padding-left: max(8px, env(safe-area-inset-left)); }
  .hud-bottom { left: auto; padding-right: max(8px, env(safe-area-inset-right)); }
  .icon-btn { width: 100%; height: 44px; }
  .scoreboard { flex: none; height: auto; grid-template-columns: 1fr; }
  .plaque, .plaque.right { height: 50px; justify-content: flex-start; text-align: left; }
  .plaque.right .pip { order: 3; }
  .plaque.right .pname { order: 2; }
  .plaque .pip { margin-left: auto; }
  .score { justify-content: center; padding: 6px; }
  .panel { flex: none; }
  .status-row, .actions { flex-direction: column; }
  .status { font-size: 1.5rem; }
}
@media (prefers-reduced-motion: reduce) { *, *::before, *::after { animation: none !important; transition: none !important; } }

.icon-btn:disabled { opacity: 0.45; cursor: default; }
@media (max-width: 480px) { .hud-top { gap: 6px; } .icon-btn { width: 44px; } }
