:root {
  --bg: #12151c; --panel: #1b202b; --panel2: #232a38; --line: #2e3647;
  --text: #e8ebf2; --muted: #8b94a8; --accent: #e5484d; --accent2: #ff6a6f;
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; background: var(--bg); color: var(--text);
  font: 15px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif; -webkit-tap-highlight-color: transparent; }
[hidden] { display: none !important; }
.page { max-width: 860px; margin: 0 auto; padding: 20px 16px 60px; }
h1 { margin: 0 0 4px; font-size: 30px; }
h2 { margin: 0 0 10px; font-size: 17px; }
small { color: var(--muted); font-weight: 400; margin-left: 6px; }
.logo { color: var(--accent); }
.sub, .muted { color: var(--muted); }
.sub { margin: 0 0 16px; }
.error { color: #ff8a8a; background: #3a1c1f; border: 1px solid #5a2a2e; padding: 8px 12px; border-radius: 8px; }
.cards { display: grid; grid-template-columns: repeat(auto-fit, minmax(300px, 1fr)); gap: 14px; margin-bottom: 14px; }
.card { background: var(--panel); border: 1px solid var(--line); border-radius: 12px; padding: 16px; margin-bottom: 14px; }
.cards .card { margin-bottom: 0; }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin-bottom: 12px; }
label { display: block; color: var(--muted); font-size: 13px; }
label.field { margin-bottom: 12px; }
label.check { display: flex; align-items: center; gap: 8px; margin: -4px 0 14px; color: var(--text); font-size: 14px; cursor: pointer; width: max-content; }
label.check input { width: auto; margin: 0; accent-color: var(--accent); }
input, select, button { font: inherit; color: var(--text); background: var(--panel2); border: 1px solid var(--line);
  border-radius: 8px; padding: 9px 12px; }
input, select { width: 100%; margin-top: 4px; }
button { cursor: pointer; touch-action: manipulation; }
button:hover { border-color: #46506a; }
button.primary { background: var(--accent); border-color: var(--accent); color: #fff; font-weight: 600; width: 100%; }
button.primary:hover { background: var(--accent2); }
button.small { padding: 5px 10px; font-size: 13px; }
button:disabled { opacity: .5; cursor: default; }
.row { display: flex; gap: 10px; align-items: center; }
.row.between { justify-content: space-between; }
.row.wrap { flex-wrap: wrap; }
.row.center { justify-content: center; }
.row.join { margin-top: 12px; }
.row.join input { margin: 0; text-transform: uppercase; letter-spacing: 3px; font-weight: 700; }
.row.join button { white-space: nowrap; }
.list { display: flex; flex-direction: column; gap: 6px; margin-bottom: 10px; }
.item { display: flex; align-items: center; gap: 10px; background: var(--panel2); border-radius: 8px; padding: 8px 12px; }
.item .grow { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.item.dim { opacity: .55; }
.swatch { width: 14px; height: 14px; border-radius: 4px; flex: none; }
.tag { font-size: 11px; padding: 1px 6px; border-radius: 99px; background: #313b52; color: #b8c2d9; }
.code { font-size: 38px; font-weight: 800; letter-spacing: 8px; }

/* game */
#game { position: fixed; inset: 0; display: flex; flex-direction: column; background: #0c0e13; }
#hud { display: flex; justify-content: space-between; align-items: center; padding: 8px 12px; background: var(--panel); border-bottom: 1px solid var(--line); z-index: 2; }
#board-wrap { flex: 1; min-height: 0; position: relative; }
#board { position: absolute; inset: 0; width: 100%; height: 100%; touch-action: manipulation; }
#scores { position: absolute; top: 52px; right: 8px; background: rgba(20,24,32,.88); border: 1px solid var(--line);
  border-radius: 8px; border-collapse: collapse; font-size: 13px; z-index: 2; }
#scores th { color: var(--muted); font-weight: 500; padding: 4px 8px; text-align: right; }
#scores th:first-child { text-align: left; }
#scores td { padding: 3px 8px; text-align: right; font-variant-numeric: tabular-nums; }
#scores td:first-child { text-align: left; max-width: 110px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
#scores tr.dead td { opacity: .4; text-decoration: line-through; }
#scores tr.me td:first-child { font-weight: 700; }
#banner { position: absolute; top: 52px; left: 50%; transform: translateX(-50%); background: rgba(20,24,32,.92);
  border: 1px solid var(--line); border-radius: 99px; padding: 6px 16px; z-index: 2; }
.hint { position: absolute; bottom: 6px; left: 0; right: 0; text-align: center; color: #5b6479; font-size: 12px; pointer-events: none; }
/* on-screen pad: ½ ▲ ⛏ over ◀ ▼ ▶, so ▲ sits directly above ▼ */
#pad { display: grid; grid-template-columns: repeat(3, 84px); gap: 8px; justify-content: center; padding: 8px 0 calc(10px + env(safe-area-inset-bottom, 0px)); background: var(--panel); border-top: 1px solid var(--line); z-index: 3; }
#pad button { height: 56px; font-size: 24px; padding: 0; }
#pad-half { grid-column: 1; grid-row: 1; }
#pad [data-d=up] { grid-column: 2; grid-row: 1; }
#pad-gather { grid-column: 3; grid-row: 1; }
#pad [data-d=left] { grid-column: 1; grid-row: 2; }
#pad [data-d=down] { grid-column: 2; grid-row: 2; }
#pad [data-d=right] { grid-column: 3; grid-row: 2; }
/* small screens: the in-game scores become a strip between the HUD and the board, so they never cover tiles */
@media (max-width: 760px), (max-height: 520px) {
  #hud { order: 0; } #scores { order: 1; } #board-wrap { order: 2; } #pad { order: 3; }
  #scores { position: static; flex: none; display: flex; gap: 6px; overflow-x: auto; scrollbar-width: none;
    padding: 5px 8px; border: 0; border-bottom: 1px solid var(--line); border-radius: 0; background: var(--panel); font-size: 12px; }
  #scores::-webkit-scrollbar { display: none; }
  #scores tbody, #scores thead { display: contents; }
  #scores tr { display: flex; align-items: baseline; gap: 5px; flex: none; background: var(--panel2); border-radius: 99px; padding: 2px 9px; }
  #scores tr:first-child { display: none; }
  #scores td { padding: 0; }
  #scores td:first-child { max-width: 90px; }
  #scores td:nth-child(3) { color: var(--muted); }
  #scores td:nth-child(3)::before { content: "· "; }
  #scores tr.dead td { text-decoration: none; }
  #scores tr.dead td:first-child { text-decoration: line-through; }
  #scores tr.me { order: -1; } /* your own chip always comes first, so it never scrolls out of view */
  #banner { top: 96px; }
}
/* phones held upright: the board sits right under the scores (sized in app.js) and the pad fills the rest with big buttons */
@media (orientation: portrait) {
  body.touch #pad { flex: 1; align-content: center; gap: 10px;
    grid-template-columns: repeat(3, min(128px, calc((100vw - 40px) / 3))); grid-auto-rows: clamp(66px, 14vh, 132px); }
  body.touch #pad button { height: auto; font-size: 36px; border-radius: 14px; }
}
/* narrow phones: keep the HUD on one line so the board starts higher */
@media (max-width: 380px) {
  #hud { padding: 6px 8px; } #hud .row { gap: 6px; } #hud button.small { padding: 5px 8px; }
  .charges { margin-left: 4px; white-space: nowrap; } .charges .lbl { display: none; }
}
body.touch .hint { display: none; } /* keyboard hints mean nothing on a touch screen */
/* phones in landscape: height is scarce, so the pad (touch devices only) moves beside the board */
@media (max-height: 520px) and (orientation: landscape) {
  body.touch #board-wrap { margin-right: 212px; }
  body.touch #pad { position: absolute; right: calc(8px + env(safe-area-inset-right, 0px)); bottom: 8px; padding: 0; border: 0; background: none;
    grid-template-columns: repeat(3, 62px); gap: 6px; }
  body.touch #pad button { height: 54px; font-size: 24px; }
}
#hud button.icon { width: 34px; padding: 5px 0; }
#paused { position: absolute; inset: 0; background: rgba(8,10,14,.55); display: flex; align-items: center; justify-content: center; z-index: 4; padding: 16px; }
#paused .panel { background: var(--panel); border: 1px solid var(--line); border-radius: 14px; padding: 20px 28px; text-align: center; width: min(320px, 100%); }
#paused h2 { font-size: 24px; }
#paused .primary { margin-top: 14px; }
#over { position: absolute; inset: 0; background: rgba(8,10,14,.72); display: flex; align-items: center; justify-content: center; z-index: 5; padding: 16px; }
#over .panel { background: var(--panel); border: 1px solid var(--line); border-radius: 14px; padding: 20px; width: min(420px, 100%); }
#over h2 { text-align: center; font-size: 24px; }
#over .row button { width: auto; flex: 1; }
@media (max-width: 560px) { .hint { display: none; } .grid2 { grid-template-columns: 1fr 1fr; } .code { font-size: 30px; } }
.howto { padding: 12px 16px; }
.howto summary { cursor: pointer; font-weight: 600; font-size: 17px; }
.howto .goal { margin: 10px 0 4px; color: var(--muted); font-size: 14px; }
.howto b { color: var(--text); }
.howto h3 { margin: 12px 0 6px; font-size: 14px; text-transform: uppercase; letter-spacing: .05em; color: var(--muted); }
.howto-cols { display: grid; grid-template-columns: repeat(auto-fit, minmax(300px, 1fr)); gap: 0 24px; }
.legend { list-style: none; margin: 0; padding: 0; font-size: 14px; color: var(--muted); }
.legend li { display: flex; align-items: center; gap: 12px; padding: 5px 0; }
.legend .keys { flex: 0 0 172px; display: flex; align-items: center; gap: 6px; flex-wrap: nowrap; color: var(--text); }
.legend .ico { flex: 0 0 auto; display: flex; }
.howto .tile { width: 38px; height: 38px; display: block; }
.howto .mouse { width: 16px; height: 22px; }
.howto .then { font-size: 12px; color: var(--muted); }
kbd { display: inline-flex; align-items: center; justify-content: center; min-width: 26px; height: 26px; padding: 0 6px;
  font: 600 13px/1 system-ui, sans-serif; color: var(--text); background: var(--panel2); border: 1px solid var(--line);
  border-bottom-width: 3px; border-radius: 6px; }
.wasd { display: inline-grid; grid-template-columns: repeat(3, 22px); grid-template-rows: 22px 22px; gap: 2px; }
.wasd kbd { min-width: 0; height: 22px; padding: 0; font-size: 12px; }
.wasd .w { grid-column: 2; grid-row: 1; } .wasd .a { grid-column: 1; grid-row: 2; }
.wasd .s { grid-column: 2; grid-row: 2; } .wasd .d { grid-column: 3; grid-row: 2; }
.howto .small { font-size: 13px; margin: 6px 0 0; }
@media (max-width: 560px) { .legend .keys { flex-basis: 150px; } .wasd { grid-template-columns: repeat(3, 19px); grid-template-rows: 19px 19px; } .wasd kbd { height: 19px; font-size: 11px; } }
.charges { margin-left: 8px; font-size: 13px; padding: 2px 8px; border-radius: 99px; background: #3b2f12; color: #ffd76a; border: 1px solid #6b5420; }
.charges.empty { background: #2a2f3a; color: var(--muted); border-color: var(--line); }

/* weekly leaderboard: bottom-right corner on wide screens, end of page on narrow ones */
#leaderboard { position: fixed; right: 16px; bottom: 16px; width: 250px; z-index: 6;
  background: var(--panel); border: 1px solid var(--line); border-radius: 12px; padding: 12px 14px;
  box-shadow: 0 6px 24px rgba(0,0,0,.35); }
#leaderboard h2 { font-size: 15px; margin-bottom: 8px; }
#lb-list { margin: 0 0 8px; padding: 0; list-style: none; font-size: 14px; }
#lb-list li { display: flex; gap: 8px; padding: 3px 0; border-bottom: 1px solid var(--line); }
#lb-list li:last-child { border-bottom: 0; }
#lb-list .pos { width: 20px; color: var(--muted); font-variant-numeric: tabular-nums; }
#lb-list .nm { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
#lb-list .st { color: var(--muted); font-variant-numeric: tabular-nums; white-space: nowrap; }
#lb-list li.me .nm { font-weight: 700; color: var(--accent2); }
#lb-note { font-size: 11px; }
@media (max-width: 1400px) {
  #leaderboard { position: static; width: auto; max-width: 828px; margin: -40px auto 40px; box-shadow: none; }
  #leaderboard { margin-left: max(16px, calc((100% - 828px) / 2)); margin-right: max(16px, calc((100% - 828px) / 2)); }
}
.modal { position: fixed; inset: 0; background: rgba(8,10,14,.72); display: flex; align-items: center; justify-content: center; z-index: 10; padding: 16px; }
.modal .panel { background: var(--panel); border: 1px solid var(--line); border-radius: 14px; padding: 20px; width: min(360px, 100%); }
.modal input { margin: 0 0 14px; }
.modal .row button { flex: 1; width: auto; }
.center { text-align: center; }
#over-rank { margin-bottom: 10px; }

/* Quick play */
.card.quick { text-align: center; }
.card.quick .small { font-size: 13px; margin: 8px 0 0; }
button.big { font-size: 20px; padding: 14px 18px; }
.countdown { margin-top: 10px; font-weight: 600; color: var(--accent); }
#over .row + .row { margin-top: 8px; }
