:root {
  --bg: #060245;          /* original WORTOMAT navy */
  --bg-raised: #110c63;
  --bg-sunken: #04012f;
  --line: #2a2483;
  --text: #f3f1ff;
  --muted: #a7a2d8;
  --accent: #ee6800;      /* original WORTOMAT orange */
  --accent-hi: #ff8a2b;
  --tile: #fbf4ea;
  --tile-ink: #0b0650;
  --good: #23b26b;
  --bad: #e5484d;
  --dup: #8d86c9;
  --radius: 14px;
  --font: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  color-scheme: dark;
}

* { box-sizing: border-box; }

html, body { margin: 0; }

body {
  min-height: 100dvh;
  background: radial-gradient(120% 80% at 50% -10%, #1a128a 0%, var(--bg) 55%) fixed, var(--bg);
  color: var(--text);
  font: 16px/1.45 var(--font);
  -webkit-tap-highlight-color: transparent;
}

a { color: var(--accent-hi); }
button { font: inherit; color: inherit; }
:focus-visible { outline: 3px solid var(--accent-hi); outline-offset: 2px; }

/* ------------------------------------------------------------ top bar */

.topbar {
  position: sticky; top: 0; z-index: 10;
  display: flex; align-items: center; gap: 12px;
  padding: calc(10px + env(safe-area-inset-top)) 16px 10px;
  background: rgb(6 2 69 / .85);
  backdrop-filter: blur(10px);
  border-bottom: 1px solid var(--line);
}

.brand {
  font-weight: 900; letter-spacing: .08em; font-size: 1.15rem;
  color: var(--accent); text-decoration: none;
}

.stats { display: flex; gap: 18px; margin-left: auto; }
.stats[hidden] { display: none; }
.stat { display: flex; flex-direction: column; align-items: flex-end; line-height: 1.1; }
.stat-label { font-size: .68rem; text-transform: uppercase; letter-spacing: .1em; color: var(--muted); }
.stat-value { font-size: 1.25rem; font-weight: 800; font-variant-numeric: tabular-nums; }
#score { color: var(--accent-hi); }

.icon-btn {
  margin-left: auto; flex: none;
  display: grid; place-items: center; width: 42px; height: 42px;
  background: none; border: 1px solid var(--line); border-radius: 12px; cursor: pointer;
}
.stats:not([hidden]) + .icon-btn { margin-left: 4px; }
.icon-btn:hover { background: var(--bg-raised); }

.menu {
  position: fixed; z-index: 20; right: 12px; top: calc(68px + env(safe-area-inset-top));
  display: flex; flex-direction: column; min-width: 230px; padding: 6px;
  background: var(--bg-raised); border: 1px solid var(--line); border-radius: var(--radius);
  box-shadow: 0 18px 40px rgb(0 0 0 / .45);
}
.menu[hidden] { display: none; }
.menu > a { padding: 11px 14px; border-radius: 10px; color: var(--text); text-decoration: none; font-weight: 600; }
.menu > a:hover { background: var(--line); }
.menu-user { margin-top: 4px; padding: 11px 14px 8px; border-top: 1px solid var(--line); font-size: .9rem; color: var(--muted); }
.menu-user b { color: var(--text); }
.link { background: none; border: 0; padding: 0; color: var(--accent-hi); text-decoration: underline; cursor: pointer; }

main:focus, main:focus-visible { outline: none; }
main { padding: 16px 16px calc(32px + env(safe-area-inset-bottom)); max-width: 1040px; margin: 0 auto; }

/* ------------------------------------------------------------ game */

.game { display: grid; gap: 20px; }
.play-area { display: flex; flex-direction: column; align-items: center; gap: 12px; }

.current {
  min-height: 2.6rem; max-width: 100%;
  display: flex; align-items: center; justify-content: center;
  font-size: 1.9rem; font-weight: 900; letter-spacing: .14em;
  overflow-wrap: anywhere; text-align: center;
}
.current .placeholder { font-size: .95rem; font-weight: 500; letter-spacing: 0; color: var(--muted); }
.current[data-state="invalid"], .current[data-state="bad"] { color: var(--bad); }
.current[data-state="good"] { color: var(--good); }
.current[data-state="dup"] { color: var(--muted); }
.current .msg { font-size: 1.05rem; font-weight: 700; letter-spacing: .02em; }
.current .pts { margin-left: .5em; font-size: 1.1rem; letter-spacing: 0; }

.board {
  --size: min(calc(100vw - 32px), 62dvh, 520px);
  position: relative;
  width: var(--size); aspect-ratio: 1;
  display: grid; grid-template-columns: repeat(5, 1fr); gap: 2.4%;
  padding: 2.4%;
  background: var(--bg-sunken); border: 1px solid var(--line); border-radius: calc(var(--radius) + 6px);
  touch-action: none; user-select: none; -webkit-user-select: none;
  container-type: inline-size;
}

.tile {
  display: grid; place-items: center;
  padding: 0; border: 0; border-radius: var(--radius);
  background: var(--tile); color: var(--tile-ink);
  font-size: 11cqi; font-weight: 900; line-height: 1;
  box-shadow: 0 4px 0 #c9bba7, 0 6px 14px rgb(0 0 0 / .35);
  cursor: pointer; touch-action: none;
  transition: transform .08s ease, background-color .12s ease, color .12s ease, box-shadow .12s ease;
}
.tile:hover { background: #fff; }
.tile.selected {
  background: var(--accent); color: #fff;
  box-shadow: 0 4px 0 #a84a00, 0 6px 18px rgb(238 104 0 / .45);
  transform: translateY(2px) scale(.96);
}
.tile.pending { background: var(--accent-hi); color: #fff; animation: pulse .6s ease-in-out infinite alternate; }
.tile.good { background: var(--good); color: #fff; box-shadow: 0 4px 0 #157546; animation: pop .45s ease; }
.tile.bad  { background: var(--bad);  color: #fff; box-shadow: 0 4px 0 #9c2a2e; animation: shake .4s ease; }
.tile.dup  { background: var(--dup);  color: #fff; box-shadow: 0 4px 0 #5a5396; animation: shake .4s ease; }

.trail { position: absolute; inset: 2.4%; width: 95.2%; height: 95.2%; pointer-events: none; overflow: visible; }
.trail polyline {
  fill: none; stroke: rgb(255 255 255 / .7); stroke-width: .13;
  stroke-linecap: round; stroke-linejoin: round;
}

@keyframes pop { 40% { transform: scale(1.1); } }
@keyframes shake { 20%, 60% { transform: translateX(-6%); } 40%, 80% { transform: translateX(6%); } }
@keyframes pulse { to { filter: brightness(1.15); } }

.hint { margin: 0; max-width: var(--size, 520px); text-align: center; font-size: .8rem; color: var(--muted); }

.side { display: flex; flex-direction: column; gap: 14px; min-width: 0; }
.save-row { display: flex; justify-content: space-between; gap: 12px; min-height: 1.3em; font-size: .85rem; color: var(--muted); }
.save-status[data-state="error"] { color: var(--bad); }

.found { background: var(--bg-raised); border: 1px solid var(--line); border-radius: var(--radius); padding: 14px 16px; }
.found h2 { margin: 0 0 10px; font-size: 1rem; display: flex; align-items: center; gap: 8px; }
.count { background: var(--line); border-radius: 999px; padding: 1px 9px; font-size: .85rem; }
.found-list { list-style: none; margin: 0; padding: 0; display: flex; flex-wrap: wrap; gap: 6px; max-height: 40dvh; overflow-y: auto; }
.found-list li {
  display: inline-flex; align-items: baseline; gap: 5px;
  padding: 4px 10px; border-radius: 999px;
  background: var(--bg-sunken); border: 1px solid var(--line);
  font-weight: 700; letter-spacing: .04em; font-size: .9rem;
}
.found-list li small { color: var(--muted); font-weight: 600; font-size: .7rem; }
.found-list li.new { border-color: var(--good); background: rgb(35 178 107 / .18); }
.empty { margin: 0; color: var(--muted); font-size: .9rem; }
.empty[hidden] { display: none; }

.game-actions { display: flex; gap: 10px; flex-wrap: wrap; }

@media (min-width: 860px) {
  .game { grid-template-columns: auto minmax(260px, 1fr); align-items: start; gap: 32px; }
  .board { --size: min(62dvh, 520px); }
  .side { padding-top: 3.2rem; }
  .found-list { max-height: 52dvh; }
}

/* ------------------------------------------------------------ buttons, panels, forms */

.btn {
  display: inline-flex; align-items: center; justify-content: center;
  min-height: 44px; padding: 0 18px; border-radius: 12px;
  background: var(--accent); color: #fff; border: 0;
  font-weight: 700; text-decoration: none; cursor: pointer;
  touch-action: manipulation;
}
.btn:hover { background: var(--accent-hi); }
.btn:disabled { opacity: .6; cursor: progress; }
.btn-ghost { background: transparent; border: 1px solid var(--line); color: var(--text); }
.btn-ghost:hover { background: var(--bg-raised); }

.panel { max-width: 720px; margin: 8px auto; }
.panel.narrow { max-width: 420px; }
.panel h1 { margin: 4px 0 6px; font-size: 1.5rem; }
.muted { color: var(--muted); }
.loading { color: var(--muted); text-align: center; padding: 40px 0; }
.prose p { margin: 0 0 14px; }

.tabs { display: flex; gap: 4px; padding: 4px; margin-bottom: 18px; background: var(--bg-sunken); border: 1px solid var(--line); border-radius: 12px; }
.tabs a { flex: 1; text-align: center; padding: 9px; border-radius: 9px; color: var(--muted); text-decoration: none; font-weight: 700; }
.tabs a[aria-selected="true"] { background: var(--bg-raised); color: var(--text); }

form { display: flex; flex-direction: column; gap: 14px; }
label { display: flex; flex-direction: column; gap: 6px; font-size: .9rem; font-weight: 600; }
label.check { flex-direction: row; align-items: center; gap: 8px; font-weight: 500; }
input:not([type=checkbox]) {
  font: inherit; color: var(--text);
  padding: 11px 12px; border-radius: 10px;
  background: var(--bg-sunken); border: 1px solid var(--line);
}
input:focus { border-color: var(--accent); outline: none; }
input[type=checkbox] { width: 18px; height: 18px; accent-color: var(--accent); }
form:not(.register) .register-only { display: none; }
.form-msg { margin: 0; min-height: 1.2em; color: var(--bad); font-size: .9rem; }
.form-msg.ok { color: var(--good); }

.table-list { list-style: none; margin: 16px 0 0; padding: 0; counter-reset: rank; }
.table-list > li { border-bottom: 1px solid var(--line); }
.table-list > li.muted, .table-list > li.loading { padding: 16px 0; border: 0; }
.row { display: flex; align-items: center; gap: 12px; padding: 12px 6px; color: var(--text); text-decoration: none; border-radius: 10px; }
.row:hover { background: var(--bg-raised); }
#list:has(.col-main) .row::before {
  counter-increment: rank; content: counter(rank);
  width: 1.8em; flex: none; text-align: right; color: var(--muted); font-variant-numeric: tabular-nums;
}
.col-main { display: flex; flex-direction: column; min-width: 0; flex: 1; font-weight: 700; overflow-wrap: anywhere; }
.col-main small { font-weight: 400; color: var(--muted); }
.col-score { font-weight: 900; font-size: 1.15rem; color: var(--accent-hi); font-variant-numeric: tabular-nums; }
.badge { font-size: .72rem; font-weight: 800; text-transform: uppercase; letter-spacing: .06em; padding: 2px 8px; border-radius: 999px; background: var(--accent); color: #fff; }

/* ------------------------------------------------------------ toast */

.toast {
  position: fixed; left: 50%; bottom: calc(20px + env(safe-area-inset-bottom)); z-index: 30;
  transform: translate(-50%, 20px); opacity: 0; pointer-events: none;
  max-width: calc(100vw - 32px); padding: 10px 18px; border-radius: 999px;
  background: #fff; color: var(--tile-ink); font-weight: 700;
  box-shadow: 0 10px 30px rgb(0 0 0 / .4);
  transition: opacity .18s ease, transform .18s ease;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }
.toast.good { background: var(--good); color: #fff; }
.toast.bad { background: var(--bad); color: #fff; }
.toast.record { background: var(--accent); color: #fff; font-size: 1.1rem; }

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