:root {
  --ink: #161226;
  --ink-2: #2a2140;
  --paper: #f6f1e7;
  --cream: #f4efe4;
  --muted: #b7adcc;
  --gold: #e6c36a;
  --good: #2f9e78;
  --good-2: #1d7a5c;
  --coral: #d4655d;
  --line: rgba(255, 255, 255, 0.12);
  --board: #24343c;
  --sq: #314854;
  --dl: #1c7c74;
  --tl: #c9842e;
  --dw: #6d4d96;
  --tw: #c45454;
  --star: #e6c36a;
  --shadow: 0 18px 50px rgba(0, 0, 0, 0.35);
  color-scheme: dark;
  font-family: "Avenir Next", "Segoe UI", system-ui, sans-serif;
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; background: #161226; }
body {
  background-color: #161226;
  background-image: radial-gradient(ellipse 90% 70% at 50% -15%, #322652 0%, #161226 62%);
  background-attachment: fixed;
  color: var(--paper);
}
button, input { font: inherit; color: inherit; }
button { cursor: pointer; }
#app { height: 100%; }

.home, .lobby {
  min-height: 100%;
  padding: calc(16px + env(safe-area-inset-top)) 16px calc(16px + env(safe-area-inset-bottom));
}

.home { max-width: 440px; margin: 0 auto; }
.brand { display: flex; gap: 14px; align-items: center; margin: 12px 0 8px; }
.brand img { width: 56px; height: 56px; }
.brand h1 { font-family: Georgia, "Iowan Old Style", serif; font-weight: 500; font-size: 34px; margin: 0; letter-spacing: -0.03em; }
.lede { color: var(--muted); margin: 0 0 22px; line-height: 1.45; }
.card {
  background: rgba(255, 255, 255, 0.04);
  border: 1px solid var(--line);
  border-radius: 18px;
  padding: 14px;
  margin: 0 0 12px;
}
label.field { display: block; font-size: 13px; color: var(--muted); margin-bottom: 10px; }
input[type="text"] {
  width: 100%;
  margin-top: 6px;
  background: #100d18;
  color: #f6f1e7;
  caret-color: #e6c36a;
  border: 1px solid #6d6488;
  border-radius: 12px;
  padding: 12px 14px;
  min-height: 48px;
}
input::placeholder { color: #b7adcc; opacity: 1; }
.row { display: flex; gap: 8px; flex-wrap: wrap; }
.seg { display: flex; gap: 6px; margin: 8px 0 12px; }
.seg button, .btn, .icon-btn {
  min-height: 44px;
  border-radius: 12px;
  border: 1px solid var(--line);
  background: #241c33;
  padding: 0 14px;
}
.seg button.on, .btn.primary {
  background: var(--gold);
  color: #24180a;
  border-color: transparent;
  font-weight: 650;
}
.btn.primary { width: 100%; }
.btn.good { background: var(--good); border-color: transparent; color: white; font-weight: 650; }
.btn.ghost { background: transparent; }
.btn.danger { background: transparent; color: #ffb4ae; border-color: rgba(212, 101, 93, 0.45); }
.btn:disabled { opacity: 0.45; cursor: default; }
.join-row { display: flex; gap: 8px; align-items: center; }
.join-row input { flex: 1 1 auto; min-width: 0; width: auto; margin-top: 0; }
.join-row .btn.primary { flex: 0 0 auto; width: auto; min-width: 96px; }
.fine { color: var(--muted); font-size: 13px; line-height: 1.45; }

.topbar {
  display: flex;
  align-items: center;
  gap: 10px;
  min-height: 48px;
  padding-right: 52px;
}
.topbar h1 { font-family: Georgia, serif; font-size: 22px; font-weight: 500; margin: 0; }
.menu-btn {
  position: fixed;
  z-index: 30;
  top: calc(8px + env(safe-area-inset-top));
  right: 10px;
  width: 44px;
  height: 44px;
  border-radius: 14px;
  border: 1px solid var(--line);
  background: #2a2144;
  font-size: 20px;
}
.sheet {
  position: fixed;
  z-index: 40;
  inset: 0;
  background: rgba(8, 6, 14, 0.62);
  display: flex;
  align-items: flex-end;
  justify-content: center;
}
.sheet .panel {
  width: min(440px, 100%);
  background: #221a30;
  border-radius: 20px 20px 0 0;
  padding: 16px 16px calc(16px + env(safe-area-inset-bottom));
  box-shadow: var(--shadow);
}
.sheet h2 { font-family: Georgia, serif; font-weight: 500; margin: 4px 0 12px; }
.sheet .btn { width: 100%; margin-bottom: 8px; }
.check { display: flex; align-items: center; gap: 10px; min-height: 44px; }

.scores {
  display: flex;
  gap: 8px;
  overflow-x: auto;
  padding-bottom: 6px;
}
.score {
  min-width: 88px;
  background: rgba(255,255,255,0.05);
  border-radius: 12px;
  padding: 8px 10px;
  border: 1px solid transparent;
}
.score.on { border-color: var(--gold); }
.score b { display: block; font-size: 18px; font-variant-numeric: tabular-nums; }
.score span { color: var(--muted); font-size: 12px; }
.score .helps { display: block; margin-top: 2px; color: var(--gold); font-size: 11px; letter-spacing: 0.02em; }
.meta-line { display: flex; justify-content: space-between; align-items: center; gap: 8px; color: var(--muted); font-size: 13px; margin: 2px 0 8px; }
.meta-line strong { color: var(--paper); font-weight: 600; }
.meta-actions { display: flex; align-items: center; gap: 8px; flex: 0 0 auto; }
.btn.tiny { min-height: 28px; padding: 0 10px; font-size: 13px; }
.table-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; font-family: Georgia, serif; font-weight: 500; margin: 8px 0 0; }
.define-hint { margin: 0 0 6px; }

.table {
  display: flex;
  flex-direction: column;
  height: 100dvh;
  max-height: 100dvh;
  overflow: hidden;
  width: 100%;
  max-width: none;
  margin: 0;
  padding: calc(6px + env(safe-area-inset-top)) 10px calc(8px + env(safe-area-inset-bottom));
  box-sizing: border-box;
}
.play {
  flex: 1 1 auto;
  min-height: 0;
  min-width: 0;
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.stage {
  flex: 1 1 auto;
  min-height: 0;
  min-width: 0;
  display: flex;
  flex-direction: column;
}
.board-view {
  flex: 1 1 auto;
  min-height: 0;
  min-width: 0;
  container-type: size;
  display: grid;
  place-items: center;
  position: relative;
  z-index: 1;
  overflow: hidden;
  touch-action: none;
}
.board {
  width: min(100cqw, 100cqh);
  height: min(100cqw, 100cqh);
  aspect-ratio: 1;
  max-width: 100%;
  max-height: 100%;
  container-type: size;
  display: grid;
  grid-template-columns: repeat(15, 1fr);
  grid-template-rows: repeat(15, 1fr);
  background: #1b2b33;
  border-radius: 10px;
  padding: 3px;
  gap: 1px;
  touch-action: none;
  box-shadow: var(--shadow);
  transform-origin: center center;
}
.sq {
  position: relative;
  background: var(--sq);
  border-radius: 2px;
  min-width: 0;
  min-height: 0;
  display: grid;
  place-items: center;
}
.sq.out { background: transparent; }
.board.cut { background: transparent; box-shadow: none; padding: 0; }
.shapes { display: flex; flex-wrap: wrap; gap: 8px; margin: 8px 0 12px; }
.shapes.scroll {
  flex-wrap: nowrap;
  overflow-x: auto;
  margin-right: -8px;
  padding-bottom: 4px;
}
.shapes.scroll button { flex: 0 0 72px; }
.more { margin: 4px 0 12px; }
.more summary { cursor: pointer; font-weight: 650; padding: 6px 0; }
.more .seg { margin-bottom: 8px; }
.shapes button {
  width: 76px;
  min-height: 0;
  padding: 6px 4px 4px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  font-size: 11px;
  line-height: 1.1;
}
.shapes button.on { background: var(--gold); color: #24180a; border-color: transparent; }
.thumb {
  width: 42px;
  height: 42px;
  display: grid;
  gap: 0;
}
.thumb i { background: transparent; display: block; }
.thumb i.on { background: #6d8c9a; }
.thumb i.star { background: var(--star); }
.thumb.rand {
  display: grid;
  place-items: center;
  font-family: Georgia, serif;
  font-size: 22px;
  color: var(--gold);
}
.sq.dl { background: var(--dl); }
.sq.tl { background: var(--tl); }
.sq.dw { background: var(--dw); }
.sq.tw { background: var(--tw); }
.sq.star { background: #8a6a22; }
.sq i {
  font-style: normal;
  font-size: clamp(6px, 1.7vw, 9px);
  letter-spacing: -0.03em;
  opacity: 0.85;
  pointer-events: none;
}
.sq.plus { background: #e6c36a; }
.sq.gift { background: #d4655d; }
.sq.plus i, .sq.gift i {
  font-weight: 800;
  opacity: 1;
  font-size: min(14px, calc(100cqw / 28));
  letter-spacing: -0.04em;
}
.sq.plus i { color: #24180a; }
.sq.gift i { color: #fff; }
.sq.last { outline: 2px solid var(--gold); outline-offset: -2px; z-index: 1; }
.tile, .ghost-tile {
  width: 100%;
  height: 100%;
  background: var(--cream);
  color: #24180a;
  border-radius: 3px;
  display: grid;
  place-items: center;
  position: relative;
  font-family: Georgia, "Iowan Old Style", serif;
  font-weight: 600;
  box-shadow: 0 1px 0 rgba(255,255,255,0.7) inset, 0 1px 2px rgba(0,0,0,0.35);
  user-select: none;
  touch-action: none;
}
.tile em, .ghost-tile em {
  font-style: normal;
  font-weight: 650;
  line-height: 1;
  transform: translateY(-1px);
}
.sq .tile em { font-size: min(28px, calc(100cqw / 22)); }
.tile small, .ghost-tile small {
  position: absolute;
  right: 1px;
  bottom: 0;
  font-family: "Avenir Next", system-ui, sans-serif;
  font-size: min(16px, calc(100cqw / 32));
  font-weight: 800;
  line-height: 1;
  color: #3d2918;
}
.tile.pending { outline: 2px solid var(--good); }
.ghost-tile {
  position: fixed;
  z-index: 50;
  width: 44px;
  height: 54px;
  border-radius: 6px;
  pointer-events: none;
  font-size: 22px;
}
.sq.aim-ok { outline: 2px solid var(--good); outline-offset: -2px; z-index: 2; }
.sq.aim-bad { outline: 2px solid var(--coral); outline-offset: -2px; z-index: 2; }
.board.word-armed { box-shadow: 0 0 0 3px var(--gold), var(--shadow); }
.zoom-hint {
  position: absolute;
  bottom: 6px;
  left: 0;
  right: 0;
  text-align: center;
  font-size: 12px;
  color: var(--muted);
  pointer-events: none;
}

.dock {
  position: relative;
  z-index: 2;
  flex: 0 0 auto;
  background: #161226;
  padding-top: 4px;
}
.dock > * { flex-shrink: 0; }
.preview { min-height: 22px; font-size: 14px; margin-bottom: 6px; }
.legend { font-size: 12px; color: var(--muted); margin: 0 0 6px; line-height: 1.4; }
.legend .key {
  display: inline-block;
  min-width: 16px;
  padding: 0 3px;
  border-radius: 3px;
  font-weight: 800;
  text-align: center;
}
.legend .key.plus { background: #e6c36a; color: #24180a; }
.legend .key.gift { background: #d4655d; color: #fff; }
.multi { display: flex; gap: 8px; align-items: center; margin-bottom: 6px; }
.multi > .btn { flex: 0 0 auto; min-width: 92px; }
.multi .seg { margin: 0; flex: 1; }
.multi .seg button { flex: 1; }
.btn.on { background: var(--gold); color: #24180a; border-color: transparent; font-weight: 700; }
.rack .tile .pick-n {
  position: absolute;
  left: 3px;
  top: 1px;
  font-size: 11px;
  font-weight: 800;
  line-height: 1;
  color: #24180a;
}
.preview.bad { color: #ffb4ae; }
.preview.ok { color: var(--gold); }
.rack {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  justify-content: center;
  min-height: 52px;
  margin-bottom: 8px;
  touch-action: none;
}
.rack .tile {
  width: 44px;
  height: 54px;
  border-radius: 6px;
  flex: 0 0 auto;
}
.rack .tile em { font-size: 26px; transform: translateY(-2px); }
.rack .tile small { font-size: 14px; right: 3px; bottom: 2px; }
.rack .tile.on { transform: translateY(-8px); outline: 2px solid var(--gold); }
.rack .tile.mark { outline: 2px solid var(--coral); }
.rack .tile.lifting { opacity: 0; pointer-events: none; }
.rack .tile { transition: transform 140ms ease; }
.rack .slot {
  width: 44px;
  height: 54px;
  border-radius: 6px;
  flex: 0 0 auto;
  border: 2px dashed rgba(230, 195, 106, 0.75);
  background: rgba(230, 195, 106, 0.08);
}
.word-move { display: flex; gap: 8px; margin-top: 8px; }
.word-move .btn { flex: 1; }
.word-hint { margin: 4px 0 0; }
.btn.move-word.on {
  background: #3d3158;
  border-color: var(--gold);
  color: var(--paper);
  font-weight: 700;
}
.challenge-banner {
  background: #3a2430;
  border: 1px solid var(--coral);
  border-radius: 12px;
  padding: 10px 12px;
  margin-bottom: 8px;
}
.challenge-banner p { margin: 0 0 8px; font-size: 14px; }
.challenge-banner .btn { width: 100%; }
.actions { display: flex; flex-wrap: wrap; gap: 8px; }
.actions .btn { flex: 1 1 30%; padding: 0 8px; }
.commit { display: flex; gap: 8px; margin-top: 8px; }
.commit .btn { flex: 1; }
.btn.recall {
  background: #3d3158;
  border-color: var(--gold);
  color: var(--paper);
  font-weight: 700;
}
.btn.recall:disabled { opacity: 0.38; }

.seat {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 8px;
  padding: 8px 0;
  border-bottom: 1px solid var(--line);
}
.code {
  font-family: Georgia, serif;
  font-size: 40px;
  letter-spacing: 0.18em;
  margin: 4px 0;
}
.toast {
  position: fixed;
  z-index: 60;
  left: 50%;
  bottom: calc(18px + env(safe-area-inset-bottom));
  transform: translateX(-50%);
  background: #3a2430;
  color: white;
  border-radius: 12px;
  padding: 12px 16px;
  max-width: min(420px, calc(100% - 24px));
  box-shadow: var(--shadow);
}
.coach {
  background: #2a2418;
  border: 1px solid rgba(230, 195, 106, 0.35);
  border-radius: 12px;
  padding: 10px 12px;
  margin-bottom: 8px;
  font-size: 14px;
}
.hint-moves { margin: 8px 0 0; padding-left: 18px; }
.hint-moves li { margin: 4px 0; }
.define-word {
  background: none;
  border: 0;
  padding: 0;
  color: var(--gold);
  font: inherit;
  text-decoration: underline;
  cursor: pointer;
}
.define-row { margin: 12px 0; }
.define-row .row { display: flex; gap: 8px; margin-top: 8px; }
.define-row .btn { flex: 1; min-width: 0; }
.clock { font-variant-numeric: tabular-nums; color: var(--gold); }
.finished {
  background: rgba(47, 158, 120, 0.15);
  border: 1px solid rgba(47, 158, 120, 0.4);
  border-radius: 12px;
  padding: 10px 12px;
  margin-bottom: 8px;
}
.results { list-style: none; margin: 10px 0 0; padding: 0; }
.results li { padding: 4px 0; font-size: 16px; }
.results em { color: var(--gold); font-style: normal; font-weight: 700; margin-left: 8px; }

.side { display: none; }
.log { list-style: none; padding: 0; margin: 0; }
.log li { padding: 6px 0; border-bottom: 1px solid var(--line); font-size: 14px; color: var(--muted); }
@media (min-width: 900px), (orientation: landscape) and (max-height: 560px) {
  .play {
    display: grid;
    grid-template-columns: minmax(0, 1fr) min(340px, 40vw);
    grid-template-rows: auto minmax(0, 1fr);
    align-items: stretch;
  }
  .stage { display: contents; }
  .hud { grid-column: 1 / -1; grid-row: 1; }
  .board-view { grid-column: 1; grid-row: 2; }
  .dock {
    grid-column: 2;
    grid-row: 2;
    width: auto;
    max-height: 100%;
    overflow: auto;
    display: flex;
    flex-direction: column;
    justify-content: flex-start;
  }
  .side { display: block; max-height: 120px; overflow: auto; margin-top: 8px; }
  .home { padding-top: 40px; }
  .sheet { align-items: center; }
  .sheet .panel { border-radius: 20px; margin-bottom: 8vh; }
}
@media (orientation: landscape) and (max-height: 560px) {
  .topbar { min-height: 36px; }
  .topbar h1 { font-size: 18px; }
  .score { padding: 4px 8px; min-width: 0; }
  .score b { font-size: 16px; }
  .meta-line { margin: 0 0 4px; }
  .side { display: none; }
  .preview { min-height: 0; margin-bottom: 4px; }
  .rack { gap: 4px; }
  .rack .tile, .rack .slot { width: 38px; height: 46px; }
  .rack .tile em { font-size: 20px; }
  .rack .tile small { font-size: 12px; }
  .actions { flex-wrap: nowrap; }
  .actions .btn, .commit .btn, .word-move .btn { min-height: 32px; padding: 0 4px; }
  .word-hint, .legend { display: none; }
  .word-move, .multi { margin-top: 2px; margin-bottom: 2px; }
  .multi > .btn, .multi .seg button { min-height: 32px; padding: 0 6px; }
  .preview { font-size: 13px; }
  .commit { margin-top: 4px; }
  .challenge-banner { padding: 6px 8px; margin-bottom: 4px; }
  .challenge-banner p { margin-bottom: 4px; font-size: 13px; }
  .home { padding-top: 40px; }
  .sheet { align-items: center; }
  .sheet .panel { border-radius: 20px; margin-bottom: 8vh; }
}
@media (max-height: 720px) and (orientation: portrait) {
  .word-hint { display: none; }
}
