:root {
  --felt-0: #05211a;
  --felt-1: #0a3427;
  --felt-2: #11503a;
  --line: rgba(246, 239, 220, .14);
  --ivory: #f6efdc;
  --ivory-dim: rgba(246, 239, 220, .72);
  --muted: rgba(246, 239, 220, .55);
  --brass: #d8b45c;
  --brass-dk: #a8832f;
  --vermilion: #c3282b;
  --ink: #17120d;
  --panel: rgba(4, 24, 18, .62);
  --radius: 14px;
  --serif: 'Songti SC', 'Iowan Old Style', 'Palatino Linotype', Palatino, 'Noto Serif', Georgia, serif;
  --sans: 'Avenir Next', 'Segoe UI', system-ui, -apple-system, Roboto, sans-serif;
  --mono: ui-monospace, 'SF Mono', 'Cascadia Mono', Menlo, Consolas, monospace;
}

* { box-sizing: border-box; }
html { color-scheme: dark; }
body {
  margin: 0;
  min-height: 100dvh;
  font-family: var(--sans);
  color: var(--ivory);
  background:
    radial-gradient(1200px 700px at 50% -10%, var(--felt-2), transparent 70%),
    repeating-linear-gradient(45deg, rgba(255,255,255,.012) 0 2px, transparent 2px 5px),
    var(--felt-1);
  background-attachment: fixed;
  -webkit-font-smoothing: antialiased;
}
[hidden] { display: none !important; }
a { color: inherit; }
.mono { font-family: var(--mono); font-variant-numeric: tabular-nums; }
.muted { color: var(--muted); }
.center { text-align: center; }
.strong { font-weight: 700; }
.error { color: #ff9b8f; }

/* ---------- top bar ---------- */
.topbar {
  position: sticky; top: 0; z-index: 20;
  display: flex; align-items: center; justify-content: space-between;
  padding: 10px max(16px, env(safe-area-inset-left));
  background: rgba(5, 33, 26, .86);
  backdrop-filter: blur(8px);
  border-bottom: 1px solid var(--line);
}
.brand { display: flex; align-items: center; gap: 10px; text-decoration: none; }
.brand-tile {
  display: grid; place-items: center; width: 28px; height: 36px; border-radius: 6px;
  background: linear-gradient(145deg, #fffdf6, #eadfc2);
  color: var(--vermilion); font: 900 20px/1 var(--serif);
  box-shadow: 2px 2px 0 #2f8a5f, 3px 3px 0 #1c5c40;
}
.brand-name { font: 700 20px/1 var(--serif); letter-spacing: .08em; text-transform: uppercase; }
.topnav { display: flex; align-items: center; gap: 14px; }
.navlink { text-decoration: none; color: var(--ivory-dim); font-weight: 600; font-size: 14px; padding: 6px 10px; border-radius: 8px; }
.navlink:hover { color: var(--ivory); background: rgba(255,255,255,.07); }
.lang { display: inline-flex; border: 1px solid var(--line); border-radius: 999px; overflow: hidden; }
.lang button {
  all: unset; cursor: pointer; padding: 5px 11px; font: 700 12px var(--sans); letter-spacing: .06em; color: var(--ivory-dim);
}
.lang button[aria-pressed='true'] { background: var(--brass); color: var(--ink); }
.lang button:focus-visible, .btn:focus-visible, a:focus-visible, select:focus-visible, input:focus-visible { outline: 2px solid var(--brass); outline-offset: 2px; }

main { max-width: 1120px; margin: 0 auto; padding: 0 16px 40px; }
.view { padding-top: 22px; }

/* ---------- home ---------- */
.hero { text-align: center; padding: 28px 0 8px; }
.hero-title { font: 700 clamp(30px, 6vw, 52px)/1.1 var(--serif); margin: 0 0 12px; letter-spacing: .02em; }
.hero-cjk { color: var(--brass); margin-right: .15em; }
.hero-sub { max-width: 56ch; margin: 0 auto 20px; color: var(--ivory-dim); font-size: 17px; line-height: 1.5; }
.field { display: flex; flex-direction: column; gap: 6px; font-size: 13px; font-weight: 600; color: var(--ivory-dim); max-width: 320px; margin: 0 auto; text-align: left; }
.field.inline { flex-direction: row; align-items: center; max-width: none; margin: 0; }
.field-row { display: flex; gap: 8px; }
input[type=text], select {
  font: 500 16px var(--sans); color: var(--ivory); background: rgba(0,0,0,.28);
  border: 1px solid var(--line); border-radius: 10px; padding: 10px 12px; min-width: 0; width: 100%;
}
select { width: auto; max-width: 70vw; }
input[type=text]::placeholder { color: var(--muted); }

.section-title { font: 700 22px/1.2 var(--serif); margin: 26px 0 14px; }
.level-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(235px, 1fr)); gap: 16px; }
.level-card {
  position: relative; display: flex; flex-direction: column; gap: 6px; text-decoration: none;
  padding: 18px; border-radius: 18px; border: 1px solid var(--line);
  background: linear-gradient(160deg, rgba(255,255,255,.07), rgba(0,0,0,.2));
  transition: transform .18s cubic-bezier(.2,.8,.2,1), border-color .18s, box-shadow .18s;
}
.level-card:hover { transform: translateY(-4px); border-color: var(--brass); box-shadow: 0 14px 30px rgba(0,0,0,.35); }
.level-num { position: absolute; top: 12px; right: 16px; font: 900 34px/1 var(--serif); color: var(--brass); opacity: .85; }
.level-thumb { height: 120px; display: grid; place-items: center; margin-bottom: 6px; }
.level-card h3 { margin: 0; font: 700 22px var(--serif); }
.level-card p { margin: 0; color: var(--ivory-dim); font-size: 14px; min-height: 2.6em; }
.chip { align-self: flex-start; margin-top: 6px; padding: 3px 10px; border-radius: 999px; border: 1px solid var(--brass-dk); color: var(--brass); font-size: 12px; font-weight: 700; }
.thumb { width: 100%; height: 100%; max-height: 120px; filter: drop-shadow(1px 2px 1px rgba(0,0,0,.4)); }

.rules { margin: 28px auto 0; max-width: 680px; color: var(--ivory-dim); font-size: 15px; line-height: 1.55; }
.rules summary { cursor: pointer; font: 700 16px var(--serif); color: var(--ivory); }
.rules li { margin: 6px 0; }

/* ---------- puzzles ---------- */
.view-head { display: grid; grid-template-columns: 1fr auto 1fr; align-items: center; gap: 10px; }
.view-head > :last-child { justify-self: end; }
.view-head .section-title { margin: 6px 0; text-align: center; }
.back { text-decoration: none; font-weight: 700; color: var(--ivory-dim); }
.back::before { content: '‹ '; }
.puzzle-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(165px, 1fr)); gap: 14px; margin-top: 10px; }
.puzzle-card {
  position: relative; display: flex; flex-direction: column; gap: 4px; padding: 12px 12px 14px; text-decoration: none;
  border-radius: 14px; border: 1px solid var(--line); background: rgba(0,0,0,.22);
  transition: transform .15s ease-out, border-color .15s, background .15s;
}
.puzzle-card:hover { transform: translateY(-3px); border-color: var(--brass); background: rgba(255,255,255,.06); }
.puzzle-no { position: absolute; top: 8px; left: 12px; font: 700 12px var(--mono); color: var(--muted); }
.puzzle-thumb { height: 92px; display: grid; place-items: center; padding-top: 8px; }
.puzzle-card strong { font: 700 15px var(--serif); }
.puzzle-meta { font-size: 12px; color: var(--ivory-dim); }
.puzzle-best { font-size: 12px; font-family: var(--mono); }
.puzzle-best.has-best { color: var(--brass); }

/* ---------- game ---------- */
#view-game { padding: 0; max-width: none; }
.game { display: flex; flex-direction: column; height: calc(100dvh - 57px); margin: 0 -16px; }
.gamebar {
  display: grid; grid-template-columns: auto 1fr auto; align-items: center; gap: 12px;
  padding: 10px 16px;
}
.gametitle { display: flex; flex-direction: column; min-width: 0; }
.gt-level { font-size: 12px; text-transform: uppercase; letter-spacing: .14em; color: var(--brass); font-weight: 700; }
.gt-puzzle { font: 700 18px/1.2 var(--serif); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.stats { display: flex; gap: 8px; }
.stat { display: flex; flex-direction: column; align-items: flex-end; min-width: 64px; padding: 4px 10px; border-radius: 10px; background: var(--panel); border: 1px solid var(--line); }
.stat-label { font-size: 10px; text-transform: uppercase; letter-spacing: .1em; color: var(--muted); }
.stat-value { font-size: 20px; font-weight: 700; }
#clock { min-width: 5.2ch; text-align: right; color: var(--brass); }
.stat-pen .stat-value { color: var(--muted); font-size: 16px; line-height: 1.5; }
.stat-pen.active .stat-value { color: #ffb3a7; }

.board-wrap { position: relative; flex: 1; min-height: 0; display: grid; place-items: center; padding: 6px 8px; overflow: hidden; }
.board { position: relative; flex: none; --tw: 50px; --th: 65px; --d: 2.5px; }
.loading { position: absolute; inset: 0; display: grid; place-items: center; font-size: 40px; color: var(--brass); animation: pulse 1s infinite; }

.tile {
  position: absolute; width: var(--tw); height: var(--th);
  border-radius: calc(var(--tw) * .13);
  background: linear-gradient(150deg, #fffef8 0%, #f7efd9 55%, #e9dec0 100%);
  box-shadow:
    calc(var(--d) * 1) calc(var(--d) * 1) 0 #3a9a6c,
    calc(var(--d) * 2) calc(var(--d) * 2) 0 #237653,
    calc(var(--d) * 3) calc(var(--d) * 3) 0 #1a5c40,
    calc(var(--d) * 3.4) calc(var(--d) * 3.6) calc(var(--d) * 2.4) rgba(0, 0, 0, .5),
    inset 0 0 0 1px rgba(120, 100, 50, .35);
  cursor: pointer; user-select: none; -webkit-tap-highlight-color: transparent;
  transition: filter .15s, transform .12s;
}
.tile .face { position: absolute; inset: 9% 9% 11% 9%; width: 82%; height: 80%; pointer-events: none; }
.tile.blocked { filter: brightness(.8) saturate(.75); cursor: not-allowed; }
.tile:not(.blocked):hover { transform: translate(-1px, -2px); }
.tile.selected { outline: 3px solid var(--brass); outline-offset: 0; background: linear-gradient(150deg, #fff6c9, #f3dc8b); z-index: 999; transform: translate(-1px, -3px); }
.tile.hint { animation: hint 0.9s ease-in-out infinite; z-index: 998; }
.tile.removing { animation: remove .26s ease-in forwards; pointer-events: none; z-index: 1000; }
.tile.shake { animation: shake .3s; }
.tile.flip { animation: flip .5s cubic-bezier(.2,.7,.2,1) backwards; }

@keyframes remove { to { transform: scale(.55) translateY(-12px); opacity: 0; } }
@keyframes shake { 25% { transform: translateX(-3px); } 75% { transform: translateX(3px); } }
@keyframes flip { from { transform: rotateY(90deg) scale(.8); opacity: 0; } }
@keyframes hint {
  0%, 100% { outline: 3px solid rgba(125, 255, 190, .95); outline-offset: 0; filter: brightness(1.08); }
  50% { outline: 3px solid rgba(125, 255, 190, .25); outline-offset: 3px; filter: brightness(1.0); }
}
@keyframes pulse { 50% { opacity: .35; } }

.toast {
  position: absolute; left: 50%; top: 10px; transform: translate(-50%, -14px); opacity: 0; pointer-events: none;
  background: rgba(0, 0, 0, .78); border: 1px solid var(--brass-dk); color: var(--ivory);
  padding: 9px 16px; border-radius: 999px; font-weight: 600; font-size: 14px; transition: opacity .2s, transform .2s; z-index: 2000; text-align: center;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }

.controls { display: flex; justify-content: center; gap: 10px; padding: 10px 16px calc(14px + env(safe-area-inset-bottom)); flex-wrap: wrap; }

/* ---------- buttons ---------- */
.btn {
  font: 700 15px var(--sans); color: var(--ivory); cursor: pointer;
  display: inline-flex; align-items: center; gap: 8px; padding: 10px 18px; border-radius: 12px;
  border: 1px solid var(--line); background: rgba(255,255,255,.08);
  transition: background .15s, transform .1s, opacity .15s;
}
.btn:hover:not(:disabled) { background: rgba(255,255,255,.15); }
.btn:active:not(:disabled) { transform: scale(.97); }
.btn:disabled { opacity: .4; cursor: not-allowed; }
.btn.primary { background: var(--brass); color: var(--ink); border-color: var(--brass-dk); }
.btn.primary:hover:not(:disabled) { background: #e6c673; }
.btn.ghost { background: transparent; }
.btn.small { padding: 8px 12px; font-size: 14px; white-space: nowrap; }
.pips { display: inline-flex; gap: 4px; margin-left: 2px; }
.pips i { width: 8px; height: 8px; border-radius: 50%; border: 1.5px solid currentColor; opacity: .55; }
.pips i.on { background: currentColor; opacity: 1; }

/* ---------- win dialog ---------- */
.dialog {
  width: min(460px, calc(100vw - 28px)); padding: 24px; border-radius: 20px; border: 1px solid var(--brass-dk);
  background: linear-gradient(170deg, #12513a, #08261d); color: var(--ivory); box-shadow: 0 30px 80px rgba(0,0,0,.6);
}
.dialog::backdrop { background: rgba(0, 12, 8, .72); backdrop-filter: blur(3px); }
.dialog[open] { animation: pop .3s cubic-bezier(.2,.9,.3,1.2); }
@keyframes pop { from { transform: scale(.9); opacity: 0; } }
.win-hero { text-align: center; }
.win-seal { display: inline-grid; place-items: center; width: 56px; height: 56px; border-radius: 12px; background: var(--vermilion); color: #fff3df; font: 900 32px var(--serif); transform: rotate(-6deg); box-shadow: 0 4px 0 #7d1517; }
.win-hero h2 { font: 700 28px var(--serif); margin: 14px 0 2px; }
.result { margin: 14px 0 8px; border-top: 1px solid var(--line); }
.res-row { display: flex; justify-content: space-between; padding: 8px 2px; border-bottom: 1px solid var(--line); color: var(--ivory-dim); }
.res-row.total { color: var(--brass); font-size: 22px; font-weight: 700; border-bottom: 2px solid var(--brass-dk); }
.rank-line { text-align: center; font-weight: 700; margin: 12px 0 16px; }
.dialog-actions { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 18px; justify-content: center; }

/* ---------- ranking ---------- */
.rank-filters { display: flex; flex-wrap: wrap; gap: 12px; align-items: center; justify-content: space-between; margin: 8px 0 14px; }
.tabs { display: inline-flex; gap: 6px; flex-wrap: wrap; }
.tab { text-decoration: none; padding: 8px 16px; border-radius: 999px; border: 1px solid var(--line); font-weight: 700; font-size: 14px; color: var(--ivory-dim); }
.tab.active { background: var(--brass); color: var(--ink); border-color: var(--brass-dk); }
.table-wrap { overflow-x: auto; border: 1px solid var(--line); border-radius: var(--radius); background: var(--panel); }
.rank-table { width: 100%; border-collapse: collapse; font-size: 15px; }
.rank-table th { text-align: left; font-size: 11px; text-transform: uppercase; letter-spacing: .1em; color: var(--muted); padding: 12px 14px; border-bottom: 1px solid var(--line); white-space: nowrap; }
.rank-table td { padding: 11px 14px; border-bottom: 1px solid rgba(246,239,220,.07); white-space: nowrap; }
.rank-table tr:last-child td { border-bottom: 0; }
.rank-table .who { font-weight: 600; max-width: 18ch; overflow: hidden; text-overflow: ellipsis; }
.rank-table .empty { text-align: center; color: var(--muted); padding: 36px; white-space: normal; }
.rk { font: 700 16px var(--serif); width: 3ch; }
.rk-1 { color: #ffd54a; } .rk-2 { color: #d4dbe3; } .rk-3 { color: #e0955a; }

/* ---------- responsive ---------- */
@media (max-width: 640px) {
  .brand-name { display: none; }
  .gamebar { grid-template-columns: auto 1fr; }
  .stats { grid-column: 1 / -1; justify-content: space-between; }
  .stat { flex: 1; align-items: center; min-width: 0; }
  .stat-value { font-size: 18px; }
  .game { height: calc(100dvh - 57px); }
  .btn { padding: 10px 14px; }
  .view-head { grid-template-columns: auto 1fr; }
  .view-head > :last-child { display: none; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: .01ms !important; animation-iteration-count: 1 !important; transition-duration: .01ms !important; }
}

/* ---------- multiplayer ---------- */
.mp-panel {
  display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: 16px;
  margin-top: 28px; padding: 6px 22px 18px; border: 1px solid var(--brass-dk); border-radius: 18px;
  background: linear-gradient(160deg, rgba(216, 180, 92, .1), rgba(0, 0, 0, .18));
}
.mp-panel .section-title { margin: 14px 0 4px; }
.mp-panel p { margin: 0; max-width: 46ch; }
.mp-actions { display: flex; flex-wrap: wrap; gap: 12px; align-items: center; }
.mp-actions input { text-align: center; letter-spacing: .2em; font-family: var(--mono); }
.linklike { all: unset; cursor: pointer; font-weight: 700; color: var(--ivory-dim); }
.linklike::before { content: '‹ '; }
.linklike:hover { color: var(--ivory); }
.small { font-size: 13px; }

.tabs.modes { margin: 4px 0 10px; }
.tab.small { padding: 5px 12px; font-size: 13px; }

.lobby { display: grid; grid-template-columns: repeat(auto-fit, minmax(300px, 1fr)); gap: 16px; align-items: start; }
.lobby-card { padding: 20px; border: 1px solid var(--line); border-radius: 18px; background: var(--panel); display: flex; flex-direction: column; gap: 12px; }
#room-join { max-width: 460px; margin: 0 auto; }
.lobby-h { margin: 0; font: 700 18px var(--serif); }
.room-code-row { display: flex; justify-content: space-between; align-items: center; gap: 12px; flex-wrap: wrap; }
.room-code { font-size: clamp(34px, 8vw, 52px); font-weight: 800; letter-spacing: .22em; color: var(--brass); line-height: 1.1; }
.room-info { display: flex; gap: 16px; align-items: center; }
.room-info h3 { margin: 6px 0 0; font: 700 20px var(--serif); }
.room-info p { margin: 4px 0 0; }
.room-thumb { width: 130px; flex: none; }
.lobby-players { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 8px; }
.lobby-players li { display: flex; align-items: center; gap: 10px; padding: 10px 12px; border-radius: 12px; background: rgba(255,255,255,.06); border-left: 4px solid var(--pc); }
.lobby-players li.offline { opacity: .55; }
.pdot { width: 12px; height: 12px; border-radius: 50%; background: var(--pc); box-shadow: 0 0 0 2px rgba(0,0,0,.35); flex: none; }
.pname { font-weight: 700; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; min-width: 0; }
.chip.tiny { margin: 0; padding: 1px 8px; font-size: 11px; }
.chip.alt { border-color: var(--line); color: var(--ivory-dim); }

.players-bar { display: flex; gap: 8px; padding: 0 16px 6px; overflow-x: auto; }
.players-bar[hidden] { display: none; }
.pchip {
  position: relative; display: flex; align-items: center; gap: 8px; padding: 6px 12px; border-radius: 12px; flex: none;
  background: var(--panel); border: 1px solid var(--line); border-bottom: 3px solid var(--pc); font-size: 14px;
}
.pchip.me { background: rgba(255,255,255,.1); }
.pchip.offline { opacity: .5; }
.pchip .pname { max-width: 11ch; }
.pchip .pscore { color: var(--brass); font-weight: 700; }
.pchip .pplace { font-weight: 800; color: var(--ink); background: var(--pc); border-radius: 6px; padding: 0 6px; font-size: 12px; }
.pbar { position: absolute; left: 8px; right: 8px; bottom: -2px; height: 3px; border-radius: 2px; background: rgba(255,255,255,.12); overflow: hidden; }
.pbar i { display: block; height: 100%; background: var(--pc); transition: width .3s; }
.pchip .pbar { bottom: 2px; }

body.mp .controls, body.mp .stat-pen { display: none; }

.board.covered { filter: blur(10px) brightness(.7); pointer-events: none; }
.countdown {
  position: absolute; inset: 0; display: grid; place-items: center; pointer-events: none; z-index: 3000;
  font: 900 min(28vmin, 220px)/1 var(--serif); color: var(--brass); text-shadow: 0 6px 0 #7a5a12, 0 12px 40px rgba(0,0,0,.6);
  animation: cdpulse 1s ease-out infinite;
}
.countdown[hidden] { display: none; }
@keyframes cdpulse { from { transform: scale(1.25); opacity: .3; } to { transform: scale(1); opacity: 1; } }

/* co-op must not hint which tiles are free */
.mp-coop .tile:hover { transform: none; }
.mp-coop .tile.blocked { filter: none; cursor: pointer; }
.tile.remote { outline: 3px dashed var(--rc); outline-offset: 0; z-index: 997; }

/* dialogs for rooms */
.dlg-title { font: 700 24px var(--serif); margin: 0 0 14px; }
.mode-pick { border: 0; padding: 0; margin: 0 0 14px; display: grid; gap: 8px; }
.mode-opt { display: flex; gap: 12px; align-items: flex-start; padding: 12px; border: 1px solid var(--line); border-radius: 12px; cursor: pointer; background: rgba(255,255,255,.04); }
.mode-opt:has(input:checked) { border-color: var(--brass); background: rgba(216, 180, 92, .12); }
.mode-opt small { display: block; color: var(--ivory-dim); margin-top: 2px; line-height: 1.35; }
.mode-opt input { margin-top: 4px; accent-color: var(--brass); }
.create-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.create-grid .field { max-width: none; margin: 0; }
.create-grid select, .create-grid input { width: 100%; max-width: none; }
@media (max-width: 520px) { .create-grid { grid-template-columns: 1fr; } .room-info { flex-direction: column; align-items: flex-start; } }
.res-table { display: flex; flex-direction: column; gap: 6px; margin-top: 12px; }
.res-player { position: relative; display: flex; align-items: center; gap: 10px; padding: 10px 12px 12px; border-radius: 12px; background: rgba(255,255,255,.06); border-left: 4px solid var(--pc); }
.res-player .rk { width: 1.4ch; text-align: center; }
.res-player .pname { flex: 1; }
.res-player .score { color: var(--brass); font-weight: 700; min-width: 5ch; text-align: right; }
.res-player .pbar { left: 12px; right: 12px; bottom: 4px; }
.dialog .res-row.total { margin-top: 8px; }
