:root {
  --felt: #1f3d2e;
  --felt-dark: #16291f;
  --felt-light: #2b5440;
  --cream: #f4ecd9;
  --ink: #2b2118;
  --tile: #f0d9a3;
  --tile-edge: #c9a86a;
  --tile-new: #ffe9a8;
  --sq: #d8ceb4;
  --tw: #d9523c;
  --dw: #efa99a;
  --tl: #3c86b8;
  --dl: #a9d0e6;
  --gold: #e8b93f;
  --john: #7fb7e6;
  --mark: #f0a35e;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

html {
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
}

html, body {
  margin: 0;
  max-width: 100%;
  overflow-x: hidden;
  background: var(--felt);
  color: var(--cream);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  font-size: 17px;
  line-height: 1.35;
  overscroll-behavior-y: none;
}

#app {
  max-width: 620px;
  margin: 0 auto;
  padding: max(6px, env(safe-area-inset-top)) 8px calc(24px + var(--dock-h, 0px) + env(safe-area-inset-bottom));
  min-height: 100vh;
  min-height: 100dvh;
}

h1, h2 { font-family: Georgia, "Times New Roman", serif; margin: 0; }
button { font: inherit; color: inherit; cursor: pointer; touch-action: manipulation; }
button:disabled { opacity: 0.45; cursor: default; }

.btn {
  border: 0;
  border-radius: 10px;
  padding: 12px 14px;
  min-height: 48px;
  background: var(--felt-light);
  color: var(--cream);
  font-weight: 600;
}
.btn.primary { background: var(--gold); color: var(--ink); }
.btn.danger { background: var(--tw); color: #fff; }
.btn.quiet { background: transparent; border: 1px solid rgba(244, 236, 217, 0.35); }
.btn.small { min-height: 40px; padding: 8px 12px; font-size: 15px; }

/* ---- Who are you ---- */
.who { text-align: center; padding-top: 12vh; }
.who h1 { font-size: 34px; }
.who p { opacity: 0.85; margin: 10px 0 28px; }
.who .choices { display: grid; gap: 14px; max-width: 340px; margin: 0 auto; }
.who .choices button { font-size: 24px; padding: 22px; border-radius: 16px; font-family: Georgia, serif; }
.who .choices small { display: block; font-size: 14px; font-family: -apple-system, sans-serif; font-weight: 400; opacity: 0.8; margin-top: 4px; }

/* ---- PIN ---- */
.pin { text-align: center; padding-top: 5vh; max-width: 340px; margin: 0 auto; }
.pin h1 { font-size: 26px; line-height: 1.2; }
.pin p { opacity: 0.88; margin: 10px 0 0; }
.pin-dots { display: flex; justify-content: center; gap: 16px; margin: 22px 0 8px; }
.pin-dots span { width: 18px; height: 18px; border-radius: 50%; border: 2px solid var(--cream); }
.pin-dots span.on { background: var(--gold); border-color: var(--gold); }
.pin-error { min-height: 26px; color: #ffc9bd; font-weight: 600; margin: 6px 0; }
.pinpad { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 10px; margin: 6px auto 16px; max-width: 290px; }
.pinpad button {
  border: 0; border-radius: 14px; min-height: 62px; background: var(--felt-dark); color: var(--cream);
  font-family: Georgia, serif; font-size: 28px; font-weight: 700;
}
.pinpad button:active { background: var(--felt-light); }
.pin .pin-help { font-size: 14px; opacity: 0.75; margin-bottom: 4px; }
.linkish { background: none; border: 0; text-decoration: underline; padding: 12px; font-size: 15px; opacity: 0.85; }

/* ---- Home ---- */
.masthead { text-align: center; padding: 14px 0 6px; }
.masthead h1 { font-size: 30px; letter-spacing: 0.3px; }
.masthead .sub { opacity: 0.8; font-size: 15px; margin-top: 4px; }
.series {
  display: grid; grid-template-columns: 1fr auto 1fr; align-items: center; gap: 10px;
  background: var(--felt-dark); border-radius: 14px; padding: 14px; margin: 14px 0;
  text-align: center;
}
.series .n { font-family: Georgia, serif; font-size: 40px; font-weight: 700; line-height: 1; }
.series .who-name { font-size: 15px; opacity: 0.85; margin-top: 4px; }
.series .mid { font-size: 13px; opacity: 0.7; }
.series .john .n { color: var(--john); }
.series .mark .n { color: var(--mark); }
.home-actions { display: grid; gap: 10px; margin-bottom: 18px; }
.home-actions .btn { font-size: 19px; }
.section-title { font-size: 13px; text-transform: uppercase; letter-spacing: 1.2px; opacity: 0.7; margin: 18px 4px 8px; }
.games { display: grid; gap: 8px; }
.game-card {
  display: grid; grid-template-columns: 1fr auto; gap: 4px 10px; align-items: center;
  width: 100%; text-align: left; border: 0; border-radius: 12px; padding: 12px 14px;
  background: var(--felt-dark); color: var(--cream);
}
.game-card .title { font-weight: 700; }
.game-card .meta { font-size: 14px; opacity: 0.75; }
.game-card .score { font-family: Georgia, serif; font-size: 17px; font-weight: 700; text-align: right; white-space: nowrap; line-height: 1.3; }
.game-card .score .john { color: var(--john); }
.game-card .score .mark { color: var(--mark); }
.game-card .status { grid-column: 1 / -1; font-size: 14px; }
.game-card.mine { outline: 2px solid var(--gold); }
.game-card.mine .status { color: var(--gold); font-weight: 700; }
.empty { opacity: 0.75; text-align: center; padding: 18px; }
.foot { text-align: center; font-size: 13px; opacity: 0.65; margin-top: 26px; }
.foot button { background: none; border: 0; text-decoration: underline; padding: 6px; }

/* ---- Game ---- */
.topbar { display: grid; grid-template-columns: auto minmax(0, 1fr) auto; align-items: center; gap: 8px; margin-bottom: 4px; }
.topbar .back { background: none; border: 0; font-size: 16px; padding: 5px 6px; }
.topbar .name { text-align: center; font-family: Georgia, serif; font-weight: 700; }
.topbar .bag { font-size: 14px; opacity: 0.85; text-align: right; padding-right: 4px; }

.scores { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); gap: 6px; margin-bottom: 5px; }
.scorebox {
  background: var(--felt-dark); border-radius: 10px; padding: 2px 10px;
  display: flex; align-items: baseline; justify-content: space-between; border: 2px solid transparent;
}
.scorebox .label { font-size: 15px; }
.scorebox .label small { opacity: 0.7; font-size: 12px; margin-left: 4px; }
.scorebox .pts { font-family: Georgia, serif; font-size: 23px; font-weight: 700; }
.scorebox.turn { border-color: var(--gold); }
.scorebox.john .pts { color: var(--john); }
.scorebox.mark .pts { color: var(--mark); }

.banner {
  border-radius: 10px; padding: 6px 10px; margin-bottom: 6px; text-align: center;
  background: var(--felt-dark); font-weight: 600; font-size: 16px; line-height: 1.25;
}
.banner.has-act { display: grid; grid-template-columns: minmax(0, 1fr) auto; align-items: center; gap: 8px; text-align: left; }
.banner.go { background: var(--gold); color: var(--ink); }
.banner.done { background: var(--cream); color: var(--ink); }
.banner small { display: block; font-weight: 400; font-size: 13px; opacity: 0.9; margin-top: 1px; }
.banner .row { display: flex; gap: 8px; justify-content: center; margin-top: 8px; flex-wrap: wrap; }

/* The board never grows taller than the space above the pinned rack. */
/* --u is 1% of the board's width, set by the page (fitBoard). Older phone browsers don't support
   container units, which is what scrambled the tiles on Mark's phone, so the page measures it instead. */
.board-wrap { --u: 3.7px; width: 100%; max-width: min(100%, var(--board-max, 78vh)); margin: 0 auto; }
.board {
  display: grid; grid-template-columns: repeat(15, minmax(0, 1fr)); gap: calc(0.45 * var(--u));
  background: var(--felt-dark); padding: calc(0.9 * var(--u)); border-radius: calc(1.4 * var(--u));
  user-select: none; -webkit-user-select: none;
}
.sq {
  position: relative; aspect-ratio: 1; min-width: 0; overflow: hidden; background: var(--sq); border-radius: calc(0.7 * var(--u));
  display: flex; align-items: center; justify-content: center;
  font-size: calc(2.1 * var(--u)); font-weight: 700; color: rgba(43, 33, 24, 0.62); border: 0; padding: 0;
}
.sq.T { background: var(--tw); color: rgba(255, 255, 255, 0.92); }
.sq.D, .sq.star { background: var(--dw); }
.sq.t { background: var(--tl); color: rgba(255, 255, 255, 0.92); }
.sq.d { background: var(--dl); }
.sq.star { font-size: calc(3.6 * var(--u)); }
.sq.target { box-shadow: inset 0 0 0 calc(0.5 * var(--u)) var(--gold); }

.tile {
  position: absolute; top: 0; right: 0; bottom: 0; left: 0; border-radius: calc(0.7 * var(--u));
  background: var(--tile); color: var(--ink);
  box-shadow: inset 0 calc(-0.5 * var(--u)) 0 var(--tile-edge);
  display: flex; align-items: center; justify-content: center;
  padding: 0 calc(1.25 * var(--u)) calc(0.9 * var(--u)) 0;
  font-family: Georgia, serif; font-size: calc(3.7 * var(--u)); font-weight: 700; line-height: 1;
}
.tile .v { position: absolute; right: calc(0.3 * var(--u)); bottom: calc(0.65 * var(--u)); font-size: calc(1.85 * var(--u)); line-height: 1; letter-spacing: -0.05em; font-family: -apple-system, sans-serif; font-weight: 700; }
.tile.blank { color: #9a3c2b; }
.tile.pending { background: var(--tile-new); box-shadow: inset 0 0 0 calc(0.45 * var(--u)) var(--gold), inset 0 calc(-0.5 * var(--u)) 0 var(--tile-edge); }
.tile.last { box-shadow: inset 0 0 0 calc(0.45 * var(--u)) #fff, inset 0 calc(-0.5 * var(--u)) 0 var(--tile-edge); }

/* Rack and buttons stay pinned to the bottom of the screen, above the phone's own bar. */
.dock {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 10;
  background: var(--felt-dark); border-top: 1px solid rgba(244, 236, 217, 0.14);
  padding: 6px 8px calc(8px + env(safe-area-inset-bottom));
}
.dock-in { max-width: 460px; margin: 0 auto; }
.hint { min-height: 20px; text-align: center; font-size: 14px; line-height: 1.3; margin: 0 0 6px; }
.hint.bad { color: #ffc9bd; }
.hint.good { color: var(--gold); font-weight: 700; }

.rack {
  display: grid; grid-template-columns: repeat(7, minmax(0, 1fr)); gap: 6px;
  background: #5b4025; border-radius: 12px; padding: 6px; max-width: 460px; margin: 0 auto 6px;
  box-shadow: inset 0 -4px 0 rgba(0, 0, 0, 0.25);
}
.rtile {
  position: relative; aspect-ratio: 1; min-width: 0; border: 0; border-radius: 8px; padding: 0;
  background: var(--tile); color: var(--ink);
  box-shadow: inset 0 -4px 0 var(--tile-edge);
  font-family: Georgia, serif; font-size: clamp(20px, 6.2vw, 30px); font-weight: 700; line-height: 1;
  display: flex; align-items: center; justify-content: center;
  padding: 0 22% 14% 0;
}
.rtile .v { position: absolute; right: 7%; bottom: 13%; font-size: clamp(10px, 2.9vw, 13px); line-height: 1; letter-spacing: -0.04em; font-family: -apple-system, sans-serif; font-weight: 700; }
.rtile.sel { outline: 3px solid var(--gold); transform: translateY(-6px); }
.rtile.used { background: rgba(0, 0, 0, 0.22); box-shadow: none; color: transparent; }
.rtile.used .v { display: none; }
.rtile.gap { background: rgba(0, 0, 0, 0.22); box-shadow: none; }
.dock .rack .rtile { touch-action: none; }
.rtile.lifted, .tile.lifted { opacity: 0.3; }
/* Tiles you have laid but not played can be dragged to another square or back to the rack. */
.tile.pending { touch-action: none; }
/* A long press must not select text or pop up the phone's copy menu in the middle of a move. */
.board, .dock, .scores, .banner, .pinpad { -webkit-user-select: none; user-select: none; -webkit-touch-callout: none; }
/* The tile under your finger. Moved with transform so it follows the finger without re-laying-out the page. */
.rtile.drag-ghost {
  position: fixed; left: 0; top: 0; z-index: 40; pointer-events: none; margin: 0; aspect-ratio: auto;
  will-change: transform; box-shadow: 0 8px 18px rgba(0, 0, 0, 0.5), inset 0 -4px 0 var(--tile-edge);
}
/* Measures the screen height with the browser's address bar showing (see fitBoard). */
#svh { position: fixed; top: 0; left: 0; width: 0; height: 100svh; visibility: hidden; pointer-events: none; }
/* A tile that has just arrived on the board pops in, so you can see what changed. */
@keyframes tile-pop { from { transform: scale(0.55); opacity: 0.4; } 70% { transform: scale(1.08); opacity: 1; } to { transform: scale(1); } }
.tile.pop { animation: tile-pop 0.24s ease-out; }
.sq.drop, .tile, .rtile { transition: box-shadow 0.08s; }
@media (prefers-reduced-motion: reduce) { .tile.pop { animation: none; } }
.sq.drop { box-shadow: inset 0 0 0 calc(0.6 * var(--u)) var(--gold); }
.rtile.pick { outline: 3px solid var(--tw); transform: translateY(-6px); }

.controls { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1.08fr) minmax(0, 1.32fr) minmax(0, 0.8fr) minmax(0, 1.3fr); gap: 5px; max-width: 460px; margin: 0 auto; }
.controls .btn { padding: 6px 2px; font-size: 14px; min-height: 46px; white-space: nowrap; }
.controls .btn.play { font-size: 16px; }
.sheet .rack { margin-bottom: 14px; }
.play-row { max-width: 460px; margin: 0 auto 14px; }
.play-row .btn { width: 100%; font-size: 20px; min-height: 54px; }

.history { background: var(--felt-dark); border-radius: 12px; padding: 6px 12px; }
.history .h { padding: 8px 0; border-bottom: 1px solid rgba(244, 236, 217, 0.12); font-size: 15px; display: flex; gap: 10px; justify-content: space-between; }
.history .h:last-child { border-bottom: 0; }
.history .h .who { font-weight: 700; }
.history .h .who.john { color: var(--john); }
.history .h .who.mark { color: var(--mark); }
.history .h.off .what { text-decoration: line-through; opacity: 0.6; }
.history .h .tot { opacity: 0.7; white-space: nowrap; font-size: 13px; padding-top: 2px; }
.history .h.note { display: block; opacity: 0.85; }
.history .src { display: block; font-size: 12px; opacity: 0.65; }

.final-racks { display: grid; gap: 6px; margin: 10px 0; font-size: 15px; text-align: center; }
.final-racks b { letter-spacing: 2px; font-family: Georgia, serif; }

/* ---- Toast and modal ---- */
#toast {
  position: fixed; left: 50%; top: calc(10px + env(safe-area-inset-top)); transform: translateX(-50%);
  background: var(--cream); color: var(--ink); padding: 12px 16px; border-radius: 12px;
  max-width: min(92vw, 520px); width: max-content; text-align: center; font-weight: 600; z-index: 30;
  box-shadow: 0 6px 24px rgba(0, 0, 0, 0.45);
}
#toast.bad { background: #ffd9d0; }
#modal {
  position: fixed; top: 0; right: 0; bottom: 0; left: 0; background: rgba(8, 16, 12, 0.72); z-index: 20;
  display: flex; align-items: center; justify-content: center; padding: 16px;
}
#modal[hidden], #toast[hidden] { display: none; }
.sheet { background: var(--cream); color: var(--ink); border-radius: 16px; padding: 18px; width: 100%; max-width: 420px; }
.sheet h2 { font-size: 22px; margin-bottom: 6px; }
.sheet p { margin: 6px 0 14px; }
.sheet .row { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.sheet .btn.quiet { border-color: rgba(43, 33, 24, 0.4); color: var(--ink); }
.sheet .btn { background: #e3d8bd; color: var(--ink); }
.sheet .btn.primary { background: var(--gold); }
.sheet .btn.danger { background: var(--tw); color: #fff; }
.letters { display: grid; grid-template-columns: repeat(7, 1fr); gap: 6px; margin-bottom: 12px; }
.letters button {
  aspect-ratio: 1; border: 0; border-radius: 8px; background: var(--tile); color: var(--ink);
  font-family: Georgia, serif; font-size: 20px; font-weight: 700; box-shadow: inset 0 -3px 0 var(--tile-edge); padding: 0;
}
.verdict { font-size: 19px; font-weight: 700; }

@media (min-width: 700px) {
  html, body { font-size: 18px; }
  .sq { font-size: calc(1.9 * var(--u)); }
}
