:root {
  --paper: #f3ecdc;
  --paper-2: #e9e0cb;
  --ink: #1f1c17;
  --ink-soft: #5d564a;
  --rule: #2b2720;
  --you: #1f56c4;
  --you-soft: #d5e1fa;
  --them: #c4372c;
  --them-soft: #f7d8d3;
  --gold: #d9a21b;
  --tile: #fffaf0;
  --tile-edge: #c9bc9c;
  --shadow: 0 2px 0 rgba(0,0,0,.18);
  --serif: 'Playfair Display', Georgia, serif;
  --mono: 'Courier Prime', 'Courier New', monospace;
  --sans: 'Inter', system-ui, sans-serif;
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--paper);
  background-image: radial-gradient(rgba(0,0,0,.035) 1px, transparent 1px);
  background-size: 4px 4px;
  color: var(--ink);
  font-family: var(--sans);
  font-size: 15px;
}
.hidden { display: none !important; }
kbd { font-family: var(--mono); border: 1px solid var(--tile-edge); border-bottom-width: 2px; border-radius: 4px; padding: 0 5px; background: var(--tile); }

.btn {
  font: 700 14px var(--sans); padding: 10px 16px; border: 2px solid var(--ink); background: var(--tile);
  color: var(--ink); border-radius: 6px; cursor: pointer; box-shadow: 3px 3px 0 var(--ink);
  transition: transform .06s, box-shadow .06s;
}
.btn:hover:not(:disabled) { transform: translate(-1px,-1px); box-shadow: 4px 4px 0 var(--ink); }
.btn:active:not(:disabled) { transform: translate(2px,2px); box-shadow: 1px 1px 0 var(--ink); }
.btn:disabled { opacity: .5; cursor: default; }
.btn.primary { background: var(--ink); color: var(--paper); }

/* ---------------- setup ---------------- */
#setup { max-width: 1100px; margin: 0 auto; padding: 24px 16px 60px; }
.masthead { text-align: center; }
.masthead h1 { font: 900 clamp(40px, 8vw, 84px)/1 var(--serif); margin: 10px 0 4px; letter-spacing: -1px; }
.masthead-sub { font: italic 15px var(--serif); color: var(--ink-soft); margin-bottom: 10px; }
.masthead-rule { border-top: 3px double var(--rule); border-bottom: 1px solid var(--rule); height: 5px; }
.lede { font: 17px/1.5 var(--serif); max-width: 760px; margin: 18px auto; text-align: center; }
.section-title { font: 700 22px var(--serif); border-bottom: 2px solid var(--rule); padding-bottom: 4px; margin-top: 28px; }
.desk-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(200px, 1fr)); gap: 12px; }
.desk {
  text-align: left; font: inherit; color: inherit; background: var(--tile); border: 2px solid var(--tile-edge);
  border-radius: 8px; padding: 12px; cursor: pointer; display: flex; flex-direction: column; gap: 4px;
}
.desk:hover { border-color: var(--ink); }
.desk.selected { border-color: var(--you); box-shadow: 0 0 0 3px var(--you-soft); }
.desk-name { font: 700 11px var(--sans); text-transform: uppercase; letter-spacing: .08em; color: var(--ink-soft); }
.desk-char { font: 700 17px var(--serif); }
.desk-style { font-size: 12px; color: var(--ink-soft); font-style: italic; }
.desk-ability { font-size: 13px; line-height: 1.35; flex: 1; }
.badge { align-self: flex-start; font: 700 10px var(--sans); text-transform: uppercase; letter-spacing: .06em; padding: 3px 6px; border-radius: 4px; background: var(--paper-2); color: var(--ink-soft); }
.badge.live { background: var(--ink); color: var(--paper); }
.setup-row { display: flex; gap: 16px; align-items: end; flex-wrap: wrap; margin: 22px 0; }
.setup-row label { display: flex; flex-direction: column; font: 600 12px var(--sans); gap: 4px; }
.setup-row select { font: 14px var(--sans); padding: 8px; border: 2px solid var(--ink); border-radius: 6px; background: var(--tile); color: var(--ink); }
.howto { background: var(--tile); border: 1px solid var(--tile-edge); border-radius: 8px; padding: 10px 16px; }
.howto summary { font: 700 16px var(--serif); cursor: pointer; }
.howto li { margin: 6px 0; line-height: 1.45; }

/* ---------------- match ---------------- */
#match { max-width: 1240px; margin: 0 auto; padding: 10px 16px 20px; display: flex; flex-direction: column; gap: 8px; min-height: 100vh; }
.topbar { display: grid; grid-template-columns: 1fr auto 1fr; align-items: center; border-top: 3px double var(--rule); border-bottom: 1px solid var(--rule); padding: 4px 0; }
.score { display: flex; align-items: baseline; gap: 8px; font-family: var(--serif); }
.score.them { justify-content: flex-end; }
.score-label { font: 700 12px var(--sans); text-transform: uppercase; letter-spacing: .1em; }
.score-num { font-weight: 900; font-size: 28px; }
.score.you .score-num { color: var(--you); }
.score.them .score-num { color: var(--them); }
.clock-wrap { text-align: center; }
.clock { font: 700 26px var(--mono); }
.overtime { font: 700 11px var(--sans); color: var(--paper); background: var(--them); padding: 1px 6px; border-radius: 3px; letter-spacing: .1em; animation: pulse 1s infinite alternate; }
@keyframes pulse { to { opacity: .55; } }

.player-strip { display: flex; align-items: center; gap: 14px; flex-wrap: wrap; padding: 6px 10px; border-radius: 6px; font-size: 13px; }
.player-strip.them { background: var(--them-soft); }
.player-strip.you { background: var(--you-soft); }
.who-desk { font: 700 11px var(--sans); text-transform: uppercase; letter-spacing: .08em; }
.who-name { font: 700 16px var(--serif); }
.you-tag { font: 700 10px var(--sans); background: var(--you); color: #fff; padding: 1px 5px; border-radius: 3px; text-transform: uppercase; }
.ability { flex: 1; min-width: 200px; color: var(--ink-soft); }
.wilds { display: flex; gap: 3px; }
.pip { width: 18px; height: 22px; border-radius: 3px; border: 1px dashed var(--ink-soft); font: 700 12px/20px var(--mono); text-align: center; color: transparent; }
.pip.on { border: 1px solid var(--gold); background: #fff3cf; color: var(--ink); }
.backs { display: flex; gap: 2px; }
.back { width: 10px; height: 16px; background: var(--them); border-radius: 2px; opacity: .75; }

.table { display: grid; grid-template-columns: 1fr 220px; gap: 12px; }
.board { display: grid; grid-template-columns: repeat(8, 1fr); gap: 6px; min-height: 340px; }
.lane {
  position: relative; display: grid; grid-template-rows: auto auto minmax(140px, 1fr) auto auto; background: var(--paper-2);
  border: 1px solid var(--tile-edge); border-radius: 8px; padding: 4px; cursor: pointer; transition: background .15s;
  min-width: 0;
}
.lane:hover { background: #e3d8bf; }
.lane.previewing { outline: 2px dashed var(--you); outline-offset: -2px; }
.lane-head { font: 700 15px var(--serif); text-align: center; color: var(--ink); }
.lane-head::before { content: 'Needs '; font: 600 9px var(--sans); text-transform: uppercase; color: var(--ink-soft); letter-spacing: .05em; }
.lane-head.lowered { color: var(--them); }
.lane-foot { text-align: center; font: 700 10px var(--sans); color: var(--ink-soft); }
.stack { display: flex; flex-direction: column; align-items: center; gap: 2px; overflow: hidden; min-height: 60px; }
.them-stack { justify-content: flex-end; }
.you-stack { justify-content: flex-start; }

.meter { display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px; }
.meter-track { position: relative; width: 14px; flex: 1; background: var(--tile); border: 1px solid var(--tile-edge); border-radius: 7px; overflow: hidden; }
.meter-track::before, .meter-track::after { content: ''; position: absolute; left: 0; right: 0; height: 3px; }
.meter-track::before { top: 0; background: var(--you); }
.meter-track::after { bottom: 0; background: var(--them); }
.meter-fill { position: absolute; left: 0; right: 0; transition: height .2s, top .2s, bottom .2s; }
.meter-fill.you { background: var(--you); }
.meter-fill.them { background: var(--them); }
.meter-mid { position: absolute; left: -2px; right: -2px; top: 50%; height: 2px; background: var(--ink); }
.meter-num { font: 700 13px var(--mono); }
.meter-num.you { color: var(--you); }
.meter-num.them { color: var(--them); }

.lane.cap-you, .lane.cap-them { cursor: not-allowed; }
.lane.cap-you { background: repeating-linear-gradient(-45deg, var(--you-soft), var(--you-soft) 8px, #c7d6f5 8px, #c7d6f5 16px); }
.lane.cap-them { background: repeating-linear-gradient(-45deg, var(--them-soft), var(--them-soft) 8px, #f1c8c1 8px, #f1c8c1 16px); }
.lane.cap-you .tile, .lane.cap-them .tile { opacity: .55; }
.stamp {
  position: absolute; top: 45%; left: 50%; transform: translate(-50%, -50%) rotate(-18deg);
  font: 900 clamp(11px, 1.5vw, 17px) var(--serif); letter-spacing: .05em; padding: 3px 6px; border: 3px solid; border-radius: 4px;
  background: rgba(255,255,255,.7); pointer-events: none; animation: stamp .35s ease-out;
}
.cap-you .stamp { color: var(--you); }
.cap-them .stamp { color: var(--them); }
@keyframes stamp { from { transform: translate(-50%, -50%) rotate(-18deg) scale(2.2); opacity: 0; } }

.flash-you { box-shadow: 0 0 0 3px var(--you); }
.flash-them { box-shadow: 0 0 0 3px var(--them); }
.flash-hit { box-shadow: 0 0 0 3px var(--gold); }
.flash-capture { box-shadow: 0 0 0 5px var(--gold); }

/* tiles */
.tile {
  position: relative; width: 52px; height: 56px; background: var(--tile); border: 1px solid var(--tile-edge);
  border-bottom-width: 3px; border-radius: 6px; display: flex; align-items: center; justify-content: center;
  box-shadow: var(--shadow); flex: none; user-select: none;
}
.tile.you { border-bottom-color: var(--you); }
.tile.them { border-bottom-color: var(--them); }
.tile .letters { font: 700 28px var(--mono); letter-spacing: -1px; }
.tile.len2 .letters { font-size: 22px; }
.tile.len3 .letters { font-size: 17px; }
.tile.len4 .letters { font-size: 13px; }
.tile .str { position: absolute; right: 3px; bottom: 1px; font: 700 11px var(--sans); color: var(--ink-soft); }
.tile .str.boost { color: #188a3a; }
.tile .fx { position: absolute; left: 3px; top: 1px; font-size: 11px; color: var(--gold); }
.tile.special { background: #fff3cf; }
.tile.facedown { background: repeating-linear-gradient(45deg, #d8cfbb, #d8cfbb 4px, #cfc4ac 4px, #cfc4ac 8px); }
.tile.small { width: 100%; max-width: 46px; height: 30px; border-bottom-width: 2px; }
.tile.small .letters { font-size: 17px; }
.tile.small.len2 .letters { font-size: 14px; }
.tile.small.len3 .letters, .tile.small.len4 .letters { font-size: 11px; }
.tile.small .str { font-size: 9px; right: 2px; bottom: 0; }
.tile.small .fx { font-size: 9px; left: 2px; top: 0; }

.ghost {
  width: 100%; max-width: 46px; height: 30px; border: 2px dashed var(--you); border-radius: 6px; flex: none;
  display: flex; align-items: center; justify-content: center; gap: 3px; background: rgba(255,255,255,.6);
  animation: bob .8s infinite alternate;
}
.ghost .letters { font: 700 15px var(--mono); color: var(--you); }
.ghost .str { font: 700 10px var(--sans); color: var(--you); }
.ghost.invalid { border-color: var(--ink-soft); }
.ghost.invalid .letters, .ghost.invalid .str { color: var(--ink-soft); }
.ghost.wasted { border-color: var(--them); }
.ghost.wasted .str { color: var(--them); }
.ghost.capturing { border-style: solid; border-color: var(--gold); background: #fff3cf; }
@keyframes bob { to { transform: translateY(-2px); } }

/* sidebar */
.sidebar { background: var(--tile); border: 1px solid var(--tile-edge); border-radius: 8px; padding: 8px 10px; overflow: hidden; display: flex; flex-direction: column; max-height: 520px; }
.sidebar h3 { font: 700 14px var(--serif); margin: 4px 0; border-bottom: 1px solid var(--rule); }
.printed { list-style: none; padding: 0; margin: 0 0 8px; display: flex; flex-wrap: wrap; gap: 3px; max-height: 140px; overflow: auto; }
.printed li { font: 700 12px var(--mono); padding: 1px 5px; border-radius: 3px; text-decoration: line-through; text-decoration-thickness: 1px; }
.printed li.you { background: var(--you-soft); }
.printed li.them { background: var(--them-soft); }
.ticker { list-style: none; padding: 0; margin: 0; font-size: 12px; line-height: 1.4; overflow: auto; flex: 1; }
.ticker li { padding: 2px 0; border-bottom: 1px dotted var(--tile-edge); }
.ticker .t { font-family: var(--mono); color: var(--ink-soft); }

/* composer + hand */
.composer { display: flex; flex-direction: column; gap: 4px; }
.composer-row { display: flex; gap: 8px; align-items: center; }
#word-input {
  flex: 1; min-width: 0; font: 700 26px var(--mono); letter-spacing: 4px; padding: 8px 12px; text-transform: uppercase;
  border: 2px solid var(--ink); border-radius: 6px; background: var(--tile); color: var(--ink);
}
#word-input:focus { outline: 3px solid var(--you-soft); }
#word-input.shake { animation: shake .3s; }
@keyframes shake { 25% { transform: translateX(-6px); } 75% { transform: translateX(6px); } }
.status-row { min-height: 22px; justify-content: space-between; font-size: 13px; }
.plan { color: var(--ink-soft); }
.plan-word { font: 700 14px var(--mono); margin-right: 6px; }
.plan-word.ok { color: var(--you); }
.bad { color: var(--them); }
.status.good { color: #188a3a; font-weight: 700; }
.status.bad { color: var(--them); font-weight: 700; }

.hand-wrap { display: flex; flex-direction: column; gap: 6px; }
.draw-bar { height: 4px; background: var(--paper-2); border-radius: 2px; overflow: hidden; }
.draw-fill { height: 100%; background: var(--you); }
.draw-fill.full { background: var(--them); }
.hand { display: flex; flex-wrap: wrap; gap: 6px; align-items: center; }
.hand .tile { cursor: pointer; transition: transform .1s; }
.hand .tile:hover { transform: translateY(-3px); }
.hand .tile.used { transform: translateY(-6px); border-color: var(--you); box-shadow: 0 0 0 2px var(--you); }
.hand-count { font: 700 13px var(--mono); color: var(--ink-soft); margin-left: 6px; }

/* overlays */
.overlay { position: fixed; inset: 0; background: rgba(31,28,23,.55); display: flex; align-items: center; justify-content: center; z-index: 10; padding: 16px; }
.countdown-num { font: 900 120px var(--serif); color: var(--paper); text-shadow: 4px 4px 0 var(--ink); }
.panel { background: var(--paper); border: 3px solid var(--ink); border-radius: 10px; padding: 24px; max-width: 480px; width: 100%; text-align: center; box-shadow: 8px 8px 0 var(--ink); }
.panel h2 { font: 700 22px var(--serif); margin: 6px 0; }
.headline { font: 900 40px/1 var(--serif); border-top: 3px double var(--rule); border-bottom: 3px double var(--rule); padding: 8px 0; }
.stats { margin: 12px auto; border-collapse: collapse; font-size: 14px; }
.stats td, .stats th { padding: 4px 12px; border-bottom: 1px solid var(--tile-edge); }
.stats td:first-child { text-align: left; color: var(--ink-soft); }
.result-actions { display: flex; gap: 10px; justify-content: center; margin-top: 12px; }

@media (max-width: 900px) {
  .table { grid-template-columns: 1fr; }
  .sidebar { max-height: 180px; }
  .board { gap: 3px; min-height: 360px; }
  .lane { padding: 2px; }
  .tile { width: 42px; height: 46px; }
  .tile .letters { font-size: 22px; }
  .lane-head::before { content: ''; }
}

@media (max-width: 600px) {
  .sidebar { display: none; }
  #word-input { font-size: 20px; letter-spacing: 2px; }
}

.lane-picker { display: flex; align-items: center; gap: 4px; }
.btn.small { padding: 8px 10px; font-size: 13px; }
.lane-label { font: 700 13px var(--mono); min-width: 92px; text-align: center; }
@media (max-width: 600px) {
  .composer-row { flex-wrap: wrap; }
  #word-input { flex-basis: 100%; }
}

.spinner { display: flex; align-items: center; gap: 6px; font: 14px var(--sans); }
.spinner input { width: 72px; font: 700 16px var(--mono); padding: 7px 8px; border: 2px solid var(--ink); border-radius: 6px; background: var(--tile); color: var(--ink); }

.more {
  font: 700 11px var(--sans); border: 1px solid var(--tile-edge); background: var(--tile); color: var(--ink-soft);
  border-radius: 10px; padding: 1px 8px; cursor: pointer; flex: none;
}
.more:hover { border-color: var(--ink); color: var(--ink); }
.more.you { border-color: var(--you); color: var(--you); }
.more.them { border-color: var(--them); color: var(--them); }

.online-help { margin: 6px 0 0; color: var(--ink-soft); font-size: 14px; }
.online-status { min-height: 24px; font-size: 15px; display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
.room-code { font: 700 24px var(--mono); letter-spacing: 4px; background: var(--tile); border: 2px solid var(--ink); border-radius: 6px; padding: 2px 10px; }
#join-code { font: 700 16px var(--mono); letter-spacing: 3px; text-transform: uppercase; width: 120px; padding: 8px; border: 2px solid var(--ink); border-radius: 6px; background: var(--tile); color: var(--ink); }
.waiting { font-style: italic; color: var(--ink-soft); align-self: center; }

/* Tug-of-war dot: slides toward the side that's closer to capturing. */
.stack { min-height: 34px; }
.track { position: relative; margin: 4px 0; }
.track-line { position: absolute; left: 50%; top: 6px; bottom: 6px; width: 2px; transform: translateX(-50%);
  background: repeating-linear-gradient(to bottom, var(--tile-edge) 0 4px, transparent 4px 8px); }
.track-mid { position: absolute; left: 30%; right: 30%; top: 50%; height: 1px; background: var(--ink-soft); opacity: .5; }
.goal { position: absolute; left: 12%; right: 12%; height: 4px; border-radius: 2px; }
.goal.them { top: 0; background: var(--them); }
.goal.you { bottom: 0; background: var(--you); }
.dot {
  position: absolute; left: 50%; width: 30px; height: 30px; transform: translate(-50%, -50%);
  border-radius: 50%; background: var(--tile); border: 2px solid var(--ink-soft); color: var(--ink);
  font: 700 12px/26px var(--mono); text-align: center; z-index: 2;
  transition: top .35s cubic-bezier(.3, 1.4, .5, 1), background .2s; box-shadow: 0 2px 0 rgba(0,0,0,.2);
}
.dot.you { background: var(--you); border-color: var(--you); color: #fff; }
.dot.them { background: var(--them); border-color: var(--them); color: #fff; }
.dot-ghost {
  position: absolute; left: 50%; width: 30px; height: 30px; transform: translate(-50%, -50%);
  border-radius: 50%; border: 2px dashed var(--you); z-index: 1; transition: top .15s;
}
.dot-ghost.capturing { border-style: solid; border-color: var(--gold); box-shadow: 0 0 0 3px #fff3cf; }
.cap-you .dot, .cap-them .dot { opacity: .5; }

/* Fixed stack heights keep every lane's goal lines aligned; an expanded lane grows. */
.lane .stack { height: 54px; min-height: 0; }
.lane.expanded .stack { height: auto; }
