/* Letterloom — linen & yarn. Tokens first; components only use tokens. */

:root {
  --flax: #e9e6de;         /* page ground */
  --flax-deep: #ddd8cc;
  --ink: #2b3240;          /* primary text, indigo ink */
  --ink-soft: #5c6474;
  --walnut: #5c4a38;       /* board frame */
  --weave: #d9d2c4;        /* plain squares */
  --weave-line: #c4bcab;
  --tile: #f4e7c9;         /* maple tiles */
  --tile-edge: #d9c69a;
  --tile-ink: #3b2f1e;
  --dl: #9fbfda;           /* sky wool */
  --tl: #4e739e;           /* indigo wool */
  --dw: #d99a8c;           /* madder rose */
  --tw: #b05244;           /* deep madder */
  --star: #d9a441;         /* gold thread */
  --act: #3e5c8a;          /* action indigo */
  --act-ink: #f5f4ef;
  --danger: #a63d2f;
  --good: #3e7a4e;
  --card: #f4f2ec;
  --shadow: 0 1px 3px rgb(43 50 64 / .18);
  --sel: #e8b64c;
}
@media (prefers-color-scheme: dark) {
  :root {
    --flax: #1b212c;
    --flax-deep: #141922;
    --ink: #e3e2da;
    --ink-soft: #9aa1ae;
    --walnut: #4a3b2c;
    --weave: #39404e;
    --weave-line: #2c3340;
    --card: #242b38;
    --shadow: 0 1px 4px rgb(0 0 0 / .5);
    --dl: #4e6d8c;
    --tl: #38557d;
    --dw: #8c5a50;
    --tw: #8c3d31;
  }
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--flax);
  color: var(--ink);
  font: 16px/1.45 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  overscroll-behavior: none;
  -webkit-user-select: none;
  user-select: none;
  touch-action: manipulation;
}

.screen { display: none; max-width: 560px; margin: 0 auto; padding: 12px 12px calc(20px + env(safe-area-inset-bottom)); }
.screen.on { display: block; }

/* ---- wordmark / home ---- */
.wordmark {
  font-family: ui-serif, "New York", Georgia, serif;
  font-weight: 700;
  letter-spacing: .01em;
  text-align: center;
}
#home .wordmark { font-size: 44px; margin: 14vh 0 4px; }
#home .tagline { text-align: center; color: var(--ink-soft); margin: 0 0 36px; }
.menu { display: flex; flex-direction: column; gap: 12px; }

.btn {
  appearance: none; border: 0; border-radius: 12px;
  font: 600 17px/1 -apple-system, BlinkMacSystemFont, sans-serif;
  padding: 15px 18px; cursor: pointer;
  background: var(--card); color: var(--ink); box-shadow: var(--shadow);
}
.btn:active { transform: translateY(1px); }
.btn.primary { background: var(--act); color: var(--act-ink); }
.btn.danger { background: var(--danger); color: #fff; }
.btn.quiet { background: transparent; box-shadow: none; color: var(--act); }
.btn:disabled { opacity: .45; cursor: default; }
.btn .sub { display: block; font-weight: 400; font-size: 13px; opacity: .75; margin-top: 4px; }

/* ---- setup ---- */
.setup-block { background: var(--card); border-radius: 14px; padding: 14px; margin: 12px 0; box-shadow: var(--shadow); }
.setup-block h3 { margin: 0 0 10px; font-size: 15px; text-transform: uppercase; letter-spacing: .06em; color: var(--ink-soft); }
.seg { display: flex; gap: 6px; }
.seg button {
  flex: 1; border: 0; border-radius: 9px; padding: 10px 6px;
  background: var(--flax-deep); color: var(--ink); font: 600 15px/1.2 inherit; cursor: pointer;
}
.seg button.on { background: var(--act); color: var(--act-ink); }
.field { display: flex; align-items: center; gap: 10px; margin: 10px 0; }
.field label { width: 74px; color: var(--ink-soft); font-size: 14px; }
.field input {
  flex: 1; border: 1px solid var(--weave-line); border-radius: 9px;
  padding: 10px 12px; font: 16px inherit; background: var(--flax); color: var(--ink);
  -webkit-user-select: text; user-select: text;
}

/* ---- game header ---- */
.topbar { display: flex; align-items: center; justify-content: space-between; margin-bottom: 8px; }
.topbar .wordmark { font-size: 20px; }
.iconbtn { border: 0; background: transparent; font-size: 22px; color: var(--ink-soft); padding: 6px 10px; cursor: pointer; }

.scores { display: flex; gap: 8px; margin-bottom: 8px; }
.player-chip {
  flex: 1; background: var(--card); border-radius: 12px; padding: 8px 12px;
  box-shadow: var(--shadow); border: 2px solid transparent; min-width: 0;
}
.player-chip.turn { border-color: var(--star); }
.player-chip .name { font-size: 13px; color: var(--ink-soft); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.player-chip .pts { font-size: 22px; font-weight: 700; font-variant-numeric: tabular-nums; }
.player-chip .tiles-left { font-size: 11px; color: var(--ink-soft); }

.bag-note { text-align: center; font-size: 12.5px; color: var(--ink-soft); margin: 2px 0 6px; }

/* ---- board ---- */
.board-frame {
  background: var(--walnut); border-radius: 10px; padding: 5px;
  box-shadow: var(--shadow);
}
.board {
  display: grid; grid-template-columns: repeat(15, minmax(0, 1fr)); gap: 2px;
  container-type: inline-size;
}
.cell {
  aspect-ratio: 1; border-radius: 8%;
  background: var(--weave);
  display: flex; align-items: center; justify-content: center;
  position: relative;
  font-size: 2.2cqw; color: rgb(43 50 64 / .55);
  font-weight: 700; letter-spacing: .02em;
}
@media (prefers-color-scheme: dark) { .cell { color: rgb(227 226 218 / .5); } }
.cell.dl { background: var(--dl); }
.cell.tl { background: var(--tl); color: rgb(255 255 255 / .85); }
.cell.dw { background: var(--dw); }
.cell.tw { background: var(--tw); color: rgb(255 255 255 / .85); }
.cell.ctr { background: var(--dw); font-size: 4.5cqw; }

/* tiles on the board */
.cell .t {
  position: absolute; inset: 0; border-radius: 12%;
  background: linear-gradient(160deg, var(--tile), var(--tile-edge));
  color: var(--tile-ink);
  display: flex; align-items: center; justify-content: center;
  font: 700 4.4cqw/1 ui-rounded, "SF Pro Rounded", -apple-system, sans-serif;
  box-shadow: 0 1px 1px rgb(0 0 0 / .3);
}
.cell .t .pt { position: absolute; right: 6%; bottom: 3%; font-size: 1.9cqw; font-weight: 600; }
.cell .t.blank-tile { color: #7a5f9e; }
.cell .t.prov { outline: 2.5px solid var(--sel); outline-offset: -1px; z-index: 2; }
.cell .t.last { box-shadow: 0 0 0 2px var(--star), 0 1px 1px rgb(0 0 0 / .3); }

/* ---- status strip ---- */
.status {
  min-height: 40px; margin: 8px 0; padding: 8px 12px; border-radius: 10px;
  background: var(--card); box-shadow: var(--shadow);
  display: flex; align-items: center; justify-content: center; gap: 8px;
  font-size: 14.5px; text-align: center;
}
.status .score-pill {
  background: var(--good); color: #fff; border-radius: 999px;
  padding: 2px 10px; font-weight: 700; font-variant-numeric: tabular-nums;
}
.status.bad { color: var(--danger); }
.status .thinking { color: var(--ink-soft); font-style: italic; }

/* ---- rack ---- */
.rack {
  display: flex; gap: 6px; justify-content: center;
  background: var(--card); border-radius: 14px; padding: 10px; box-shadow: var(--shadow);
  min-height: 66px;
}
.rack .rtile {
  width: 46px; height: 46px; border-radius: 9px; border: 0;
  background: linear-gradient(160deg, var(--tile), var(--tile-edge));
  color: var(--tile-ink);
  font: 700 24px/1 ui-rounded, "SF Pro Rounded", -apple-system, sans-serif;
  position: relative; cursor: pointer;
  box-shadow: 0 1.5px 2px rgb(0 0 0 / .3);
}
.rack .rtile .pt { position: absolute; right: 4px; bottom: 2px; font-size: 10px; font-weight: 600; }
.rack .rtile.sel { transform: translateY(-7px); box-shadow: 0 0 0 2.5px var(--sel), 0 6px 8px rgb(0 0 0 / .25); }
.rack .rtile.gone { visibility: hidden; }
.rack .rtile.swap { box-shadow: 0 0 0 2.5px var(--tl), 0 1.5px 2px rgb(0 0 0 / .3); transform: translateY(-7px); }

/* drag & drop */
.rack .rtile, .cell .t.prov { touch-action: none; } /* drag sources must not scroll the page */
#drag-ghost {
  position: fixed; left: 0; top: 0; z-index: 90;
  width: 52px; height: 52px; border-radius: 10px;
  background: linear-gradient(160deg, var(--tile), var(--tile-edge));
  color: var(--tile-ink);
  font: 700 27px/52px ui-rounded, "SF Pro Rounded", -apple-system, sans-serif;
  text-align: center;
  box-shadow: 0 8px 18px rgb(0 0 0 / .35);
  pointer-events: none; display: none;
}
#drag-ghost .pt { position: absolute; right: 5px; bottom: 2px; font-size: 11px; line-height: 1; font-weight: 600; }
#drag-ghost.blank-tile { color: #7a5f9e; }
.cell.drop-hint { outline: 3px solid var(--sel); outline-offset: -1px; z-index: 1; }

/* ---- actions ---- */
.actions { display: flex; gap: 8px; margin-top: 10px; }
.actions .btn { flex: 1; padding: 13px 6px; font-size: 15px; }
.actions .btn.play { flex: 1.6; }

/* ---- overlays ---- */
.overlay {
  position: fixed; inset: 0; background: rgb(20 25 34 / .55);
  display: none; align-items: flex-end; justify-content: center; z-index: 50;
}
.overlay.on { display: flex; }
.sheet {
  background: var(--card); border-radius: 18px 18px 0 0; width: 100%; max-width: 560px;
  padding: 18px 16px calc(18px + env(safe-area-inset-bottom));
  max-height: 82vh; overflow-y: auto;
}
.sheet h2 { margin: 0 0 6px; font-size: 20px; }
.sheet p { color: var(--ink-soft); margin: 6px 0 14px; font-size: 15px; }
.sheet .row { display: flex; gap: 10px; margin-top: 8px; }
.sheet .row .btn { flex: 1; }

/* curtain is a full screen, not a sheet */
.curtain {
  position: fixed; inset: 0; z-index: 60; display: none;
  background: var(--flax);
  flex-direction: column; align-items: center; justify-content: center; gap: 10px;
  text-align: center; padding: 24px;
}
.curtain.on { display: flex; }
.curtain .big { font-size: 26px; font-weight: 700; }
.curtain .hint { color: var(--ink-soft); }

/* blank picker */
.alpha { display: grid; grid-template-columns: repeat(6, 1fr); gap: 8px; }
.alpha button {
  aspect-ratio: 1; border: 0; border-radius: 9px;
  background: linear-gradient(160deg, var(--tile), var(--tile-edge));
  color: var(--tile-ink); font: 700 20px/1 ui-rounded, -apple-system, sans-serif; cursor: pointer;
}

/* history + game over */
.hist { list-style: none; margin: 0; padding: 0; font-size: 14.5px; }
.hist li { padding: 7px 2px; border-bottom: 1px solid var(--weave-line); display: flex; justify-content: space-between; gap: 8px; }
.hist li .who { color: var(--ink-soft); }
.hist li .sc { font-variant-numeric: tabular-nums; font-weight: 700; }

.final-table { width: 100%; border-collapse: collapse; font-size: 15px; margin: 8px 0 4px; }
.final-table td, .final-table th { padding: 7px 4px; text-align: right; border-bottom: 1px solid var(--weave-line); }
.final-table td:first-child, .final-table th:first-child { text-align: left; }
.final-table .win td { font-weight: 700; }
.winner-line { text-align: center; font-size: 19px; font-weight: 700; margin: 8px 0 2px; }

/* ---- online: lobby & home ---- */
.home-section { margin-top: 26px; }
.home-section h3 { font-size: 13px; text-transform: uppercase; letter-spacing: .08em; color: var(--ink-soft); text-align: center; margin: 0 0 10px; }
.code-display {
  font: 700 52px/1 ui-monospace, SFMono-Regular, Menlo, monospace;
  letter-spacing: .18em; text-align: center; margin: 18px 0 4px; color: var(--ink);
}
.code-hint { text-align: center; color: var(--ink-soft); margin: 0 0 18px; font-size: 14.5px; }
.seat-list { list-style: none; margin: 0 0 16px; padding: 0; }
.seat-list li {
  display: flex; align-items: center; gap: 10px;
  background: var(--card); border-radius: 11px; padding: 12px 14px; margin-bottom: 8px;
  box-shadow: var(--shadow); font-weight: 600;
}
.seat-list li .tag { font-weight: 400; font-size: 13px; color: var(--ink-soft); }
.seat-list li .dot { width: 9px; height: 9px; border-radius: 50%; background: var(--good); flex: none; }
.seat-list li.empty { color: var(--ink-soft); font-weight: 400; box-shadow: none; background: var(--flax-deep); }
.room-tag {
  font: 600 12px/1 ui-monospace, Menlo, monospace; letter-spacing: .1em;
  color: var(--ink-soft); background: var(--flax-deep); border-radius: 6px; padding: 4px 7px;
}
.offline-dot { color: var(--danger); font-weight: 700; }
#build-tag { color: var(--ink-soft); }

/* toast */
#toast {
  position: fixed; left: 50%; bottom: calc(24px + env(safe-area-inset-bottom));
  transform: translateX(-50%) translateY(20px);
  background: var(--ink); color: var(--flax);
  padding: 10px 18px; border-radius: 999px; font-size: 15px; font-weight: 600;
  opacity: 0; pointer-events: none; transition: opacity .25s, transform .25s;
  z-index: 70; max-width: 90vw; text-align: center;
}
#toast.on { opacity: 1; transform: translateX(-50%) translateY(0); }

@media (prefers-reduced-motion: reduce) {
  * { transition: none !important; animation: none !important; }
}

/* iOS install hint */
#install-hint {
  position: fixed; left: 12px; right: 12px;
  bottom: calc(14px + env(safe-area-inset-bottom));
  display: none; align-items: center; gap: 10px;
  background: var(--card); color: var(--ink);
  border-radius: 14px; padding: 13px 8px 13px 16px;
  box-shadow: 0 4px 10px rgb(0 0 0 / .25), var(--shadow);
  font-size: 14.5px; line-height: 1.45; z-index: 45;
  max-width: 536px; margin: 0 auto;
}
#install-hint.on { display: flex; }
#install-hint .hint-text { flex: 1; }
#install-hint .share-glyph {
  width: 17px; height: 17px; vertical-align: -3px;
  fill: var(--act);
}
#install-hint em { font-style: normal; font-weight: 700; }

/* hover tooltips — only on devices that actually hover (no phantom tips on touch) */
@media (hover: hover) {
  [data-tip] { position: relative; }
  [data-tip]:hover::after {
    content: attr(data-tip);
    position: absolute; bottom: calc(100% + 9px); left: 50%; transform: translateX(-50%);
    background: var(--ink); color: var(--flax);
    padding: 6px 11px; border-radius: 8px;
    font: 600 12.5px/1 -apple-system, BlinkMacSystemFont, sans-serif;
    white-space: nowrap; pointer-events: none; z-index: 40;
    box-shadow: var(--shadow);
  }
  [data-tip]:hover::before {
    content: '';
    position: absolute; bottom: calc(100% + 4px); left: 50%; transform: translateX(-50%);
    border: 5px solid transparent; border-top-color: var(--ink); border-bottom: 0;
    pointer-events: none; z-index: 40;
  }
  /* keep edge buttons' tips on screen */
  .actions [data-tip]:first-child:hover::after { left: 0; transform: none; }
  .actions [data-tip]:last-child:hover::after { left: auto; right: 0; transform: none; }
}

/* keyboard focus */
:focus-visible { outline: 3px solid var(--sel); outline-offset: 2px; }
