/* ===========================================================================
   Tower of London — theme, layout, components, board
   =========================================================================== */

:root {
  --bg-1: #eef2f9;
  --bg-2: #e3e9f4;
  --surface: #ffffff;
  --surface-2: #f4f7fc;
  --surface-3: #e9eef7;
  --text: #1a2233;
  --muted: #69728a;
  --border: #e3e8f1;
  --primary: #4f46e5;
  --primary-600: #4338ca;
  --primary-tint: rgba(79, 70, 229, 0.12);
  --ring: rgba(79, 70, 229, 0.45);
  --success: #1f9d6b;
  --gold: #d99a1c;
  --danger: #e5484d;

  --rod-1: #d6dce9;
  --rod-2: #bcc4d6;
  --base-1: #aeb7cb;
  --base-2: #8c96b1;

  --ball-r-1: #ff6b6b; --ball-r-2: #e0334e;
  --ball-g-1: #4fd49b; --ball-g-2: #189a66;
  --ball-b-1: #6ba6ff; --ball-b-2: #2f6fe0;

  --shadow-sm: 0 1px 2px rgba(20, 28, 50, 0.06), 0 1px 3px rgba(20, 28, 50, 0.07);
  --shadow-md: 0 8px 24px rgba(20, 28, 50, 0.1);
  --shadow-lg: 0 22px 48px rgba(20, 28, 50, 0.18);
  --radius: 16px;
  --radius-sm: 10px;
}

[data-theme='dark'] {
  --bg-1: #0e1320;
  --bg-2: #0a0e18;
  --surface: #151b29;
  --surface-2: #1a2233;
  --surface-3: #222c42;
  --text: #e8ecf5;
  --muted: #98a1b9;
  --border: #27314a;
  --primary: #6f6af8;
  --primary-600: #5b54f0;
  --primary-tint: rgba(111, 106, 248, 0.2);
  --ring: rgba(111, 106, 248, 0.55);
  --success: #2bb37e;
  --gold: #e9b23f;
  --danger: #f06b70;

  --rod-1: #49526d; --rod-2: #333c54;
  --base-1: #424e6c; --base-2: #28324c;

  --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.3);
  --shadow-md: 0 10px 28px rgba(0, 0, 0, 0.4);
  --shadow-lg: 0 24px 52px rgba(0, 0, 0, 0.55);
}

* { box-sizing: border-box; }

/* The [hidden] attribute must always win, even over components that set their
   own display (e.g. .solve-panel uses display:flex). */
[hidden] { display: none !important; }

html, body { height: 100%; }

body {
  margin: 0;
  font-family: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif;
  color: var(--text);
  background: radial-gradient(1200px 800px at 80% -10%, var(--bg-1), var(--bg-2));
  background-attachment: fixed;
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
  line-height: 1.5;
}

.app {
  max-width: 920px;
  margin: 0 auto;
  padding: 18px clamp(14px, 4vw, 28px) 40px;
  min-height: 100%;
  display: flex;
  flex-direction: column;
}

/* ---- app bar ------------------------------------------------------------- */

.appbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 22px;
}
.appbar__brand { display: flex; align-items: center; gap: 12px; }
.appbar__mark {
  width: 42px; height: 42px; border-radius: 12px;
  background: var(--surface);
  box-shadow: var(--shadow-sm);
  display: grid; grid-auto-flow: column; align-items: center; justify-content: center; gap: 4px;
  border: 1px solid var(--border);
}
.appbar__mark .dot { width: 9px; height: 9px; border-radius: 50%; }
.dot-r { background: radial-gradient(circle at 35% 30%, var(--ball-r-1), var(--ball-r-2)); }
.dot-g { background: radial-gradient(circle at 35% 30%, var(--ball-g-1), var(--ball-g-2)); }
.dot-b { background: radial-gradient(circle at 35% 30%, var(--ball-b-1), var(--ball-b-2)); }
.appbar__title { font-weight: 800; font-size: 1.05rem; letter-spacing: -0.01em; }
.appbar__sub { font-size: 0.78rem; color: var(--muted); }

.appbar__actions { display: flex; align-items: center; gap: 8px; }
.icon-btn {
  width: 40px; height: 40px; border-radius: 12px;
  border: 1px solid var(--border); background: var(--surface);
  color: var(--text); cursor: pointer; font-size: 1.15rem;
  box-shadow: var(--shadow-sm); transition: transform 0.12s, background 0.2s;
  display: inline-grid; place-items: center;
}
.icon-btn:hover { transform: translateY(-1px); background: var(--surface-2); }
.icon-btn svg { display: block; }

/* ---- screens ------------------------------------------------------------- */

.main { flex: 1; }
.screen { display: none; animation: fade-in 0.35s ease both; }
.screen--active { display: block; }
@keyframes fade-in { from { opacity: 0; transform: translateY(8px); } to { opacity: 1; transform: none; } }

.screen__head {
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  margin-bottom: 16px;
}
.screen__title { font-size: 1.35rem; font-weight: 800; letter-spacing: -0.02em; margin: 0; }

/* ---- cards --------------------------------------------------------------- */

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow-md);
  padding: clamp(18px, 4vw, 30px);
  margin-bottom: 18px;
}
.card__title { margin: 0 0 14px; font-size: 1.05rem; font-weight: 700; }

.hero__title { font-size: clamp(1.6rem, 4.5vw, 2.3rem); font-weight: 800; letter-spacing: -0.03em; margin: 0 0 10px; }
.hero__lead { color: var(--muted); font-size: 1.02rem; margin: 0 0 22px; max-width: 60ch; }
.hero__actions { display: flex; flex-wrap: wrap; gap: 12px; margin: 18px 0 10px; }

/* ---- forms --------------------------------------------------------------- */

.field { display: block; margin: 18px 0; }
.field__label { display: block; font-size: 0.86rem; font-weight: 600; margin-bottom: 7px; }
.input {
  width: 100%; max-width: 340px; padding: 12px 14px; font-size: 1rem;
  color: var(--text); background: var(--surface-2);
  border: 1px solid var(--border); border-radius: var(--radius-sm);
  transition: border 0.15s, box-shadow 0.15s; font-family: inherit;
}
.input:focus { outline: none; border-color: var(--primary); box-shadow: 0 0 0 4px var(--primary-tint); }

/* ---- buttons ------------------------------------------------------------- */

.btn {
  appearance: none; border: 1px solid transparent; cursor: pointer;
  font-family: inherit; font-weight: 600; font-size: 0.95rem;
  padding: 11px 18px; border-radius: var(--radius-sm);
  transition: transform 0.12s ease, box-shadow 0.2s, background 0.2s, border-color 0.2s;
  display: inline-flex; align-items: center; gap: 8px; line-height: 1;
}
.btn:active { transform: translateY(1px); }
.btn--lg { padding: 14px 24px; font-size: 1.02rem; }
.btn--sm { padding: 8px 13px; font-size: 0.85rem; }
.btn--primary {
  background: linear-gradient(180deg, var(--primary), var(--primary-600));
  color: #fff; box-shadow: 0 6px 16px var(--primary-tint);
}
.btn--primary:hover { transform: translateY(-1px); box-shadow: 0 10px 22px var(--primary-tint); }
.btn--primary:disabled { opacity: 0.6; cursor: default; transform: none; box-shadow: none; }
.btn--ghost { background: var(--surface-2); color: var(--text); border-color: var(--border); }
.btn--ghost:hover { background: var(--surface-3); }
.btn--danger { background: var(--danger); color: #fff; }
.btn--danger:hover { transform: translateY(-1px); filter: brightness(1.05); }
.btn--xs { padding: 5px 11px; font-size: 0.78rem; border-radius: 8px; }
.linkbtn {
  background: none; border: none; color: var(--primary); cursor: pointer;
  font-family: inherit; font-weight: 600; font-size: 0.95rem; padding: 6px 0;
}
.linkbtn:hover { text-decoration: underline; }

/* ---- "how it works" ------------------------------------------------------ */

.how { margin-top: 22px; border-top: 1px solid var(--border); padding-top: 14px; }
.how summary { cursor: pointer; font-weight: 600; color: var(--muted); }
.how__body ul { margin: 12px 0 0; padding-left: 20px; color: var(--muted); }
.how__body li { margin: 6px 0; }

/* ---- stage grid ---------------------------------------------------------- */

.stage-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); gap: 16px; }
.stage-card {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); box-shadow: var(--shadow-sm); padding: 18px;
  display: flex; flex-direction: column; transition: transform 0.15s, box-shadow 0.2s, border-color 0.2s;
}
.stage-card:hover { transform: translateY(-3px); box-shadow: var(--shadow-md); border-color: var(--primary); }
.stage-card--featured { border-color: var(--primary); background: linear-gradient(180deg, var(--primary-tint), transparent); }
.stage-card__top { display: flex; align-items: baseline; justify-content: space-between; gap: 8px; }
.stage-card__name { font-weight: 800; font-size: 1.05rem; }
.stage-card__moves { font-weight: 800; font-size: 1.2rem; color: var(--primary); }
.stage-card__moves span { font-size: 0.7rem; color: var(--muted); font-weight: 600; }
.stage-card__blurb { color: var(--muted); font-size: 0.9rem; flex: 1; margin: 10px 0 14px; }
.stage-card__foot { display: flex; align-items: center; justify-content: space-between; }

/* ---- play bar ------------------------------------------------------------ */

.playbar { display: flex; align-items: center; justify-content: space-between; gap: 16px; padding: 14px 18px; }
.playbar__left { display: flex; align-items: center; gap: 14px; }
.playbar__progress { font-weight: 800; font-size: 1rem; }
.playbar__stage { font-size: 0.82rem; color: var(--muted); }

.stats { display: flex; gap: 10px; }
.stat {
  background: var(--surface-2); border: 1px solid var(--border); border-radius: 12px;
  padding: 7px 13px; text-align: center; min-width: 62px;
}
.stat__v { font-weight: 800; font-size: 1.1rem; font-variant-numeric: tabular-nums; }
.stat__k { font-size: 0.66rem; color: var(--muted); text-transform: uppercase; letter-spacing: 0.04em; }

/* ---- arena & boards ------------------------------------------------------ */

.arena { display: grid; grid-template-columns: 0.62fr 1fr; gap: 18px; align-items: stretch; margin-bottom: 16px; }
.boardwrap {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); box-shadow: var(--shadow-md); padding: 14px 14px 22px;
}
.boardwrap--goal { background: var(--surface-2); }
.boardwrap__label {
  font-size: 0.8rem; font-weight: 700; color: var(--muted); text-transform: uppercase;
  letter-spacing: 0.05em; margin-bottom: 6px; display: flex; align-items: center; gap: 8px;
}

.board { position: relative; width: 100%; height: 300px; --ball: 56px; overflow: visible; }
.board--compact { height: 168px; --ball: 34px; }

.pegs {
  position: absolute; inset: 0; display: flex; align-items: flex-end;
  justify-content: space-around; padding: 0 4%;
}
.peg {
  position: relative; width: 26%; display: flex; justify-content: center;
  height: calc(var(--cap) * (var(--ball) - 8px) + 30px);
}
.peg-rod {
  position: absolute; bottom: 12px; width: 12px; height: calc(100% - 12px);
  border-radius: 8px 8px 3px 3px;
  background: linear-gradient(90deg, var(--rod-2), var(--rod-1) 45%, var(--rod-2));
  box-shadow: inset -1px 0 2px rgba(0, 0, 0, 0.12);
}
.peg-base {
  position: absolute; bottom: 0; left: 50%; transform: translateX(-50%);
  width: calc(var(--ball) + 40px); height: 14px; border-radius: 9px;
  background: linear-gradient(180deg, var(--base-1), var(--base-2));
  box-shadow: var(--shadow-sm), inset 0 1px 1px rgba(255, 255, 255, 0.25);
}
.board--compact .peg-base { width: calc(var(--ball) + 22px); height: 9px; }
.peg-cap {
  position: absolute; bottom: -22px; left: 50%; transform: translateX(-50%);
  font-size: 0.72rem; font-weight: 700; color: var(--muted); font-variant-numeric: tabular-nums;
}
.board--compact .peg-cap { display: none; }

/* valid drop target highlight */
.peg::before {
  content: ''; position: absolute; bottom: 8px; left: 50%; transform: translateX(-50%);
  width: calc(var(--ball) + 26px); height: calc(100% + 6px); border-radius: 16px;
  background: var(--primary-tint); opacity: 0; transition: opacity 0.18s; pointer-events: none;
}
.peg.drop-ok::before { opacity: 0.55; }
.peg.drop-hover::before { opacity: 1; box-shadow: 0 0 0 2px var(--primary) inset; }

/* balls */
.ball {
  position: absolute; top: 0; left: 0; width: var(--ball); height: var(--ball);
  border-radius: 50%; z-index: 2; touch-action: none; cursor: grab; will-change: transform;
  transition: transform 0.32s cubic-bezier(0.22, 0.68, 0.32, 1), box-shadow 0.2s, filter 0.2s;
  box-shadow: 0 5px 12px rgba(20, 28, 50, 0.28), inset 0 -3px 7px rgba(0, 0, 0, 0.25),
    inset 0 2px 4px rgba(255, 255, 255, 0.35);
}
.board--display .ball, .ball[role='img'] { cursor: default; }
.ball.no-anim { transition: none !important; }
.ball-r { background: radial-gradient(circle at 32% 28%, var(--ball-r-1), var(--ball-r-2) 78%); }
.ball-g { background: radial-gradient(circle at 32% 28%, var(--ball-g-1), var(--ball-g-2) 78%); }
.ball-b { background: radial-gradient(circle at 32% 28%, var(--ball-b-1), var(--ball-b-2) 78%); }
.ball-gloss {
  position: absolute; top: 14%; left: 20%; width: 42%; height: 32%; border-radius: 50%;
  background: linear-gradient(180deg, rgba(255, 255, 255, 0.7), rgba(255, 255, 255, 0));
  filter: blur(1px); pointer-events: none;
}
.ball.dragging {
  cursor: grabbing; z-index: 60;
  box-shadow: 0 22px 34px rgba(20, 28, 50, 0.4), inset 0 -3px 7px rgba(0, 0, 0, 0.25),
    inset 0 2px 4px rgba(255, 255, 255, 0.4);
}
.ball.selected { box-shadow: 0 0 0 4px var(--ring), 0 10px 20px rgba(20, 28, 50, 0.3); }

.drop-ghost {
  position: absolute; top: 0; left: 0; border-radius: 50%; z-index: 1; pointer-events: none;
  border: 2px dashed var(--primary); background: var(--primary-tint);
  transition: transform 0.08s ease; box-sizing: border-box;
}

.board--solved .peg-base { box-shadow: 0 0 0 2px var(--success), var(--shadow-sm); }
.ball-pop { animation: ball-pop 0.55s ease; }
@keyframes ball-pop {
  0% { filter: brightness(1); }
  35% { filter: brightness(1.35) saturate(1.2); }
  100% { filter: brightness(1); }
}
.ball-reject { animation: ball-reject 0.4s ease; }
@keyframes ball-reject {
  0%, 100% { box-shadow: 0 5px 12px rgba(20, 28, 50, 0.28); }
  50% { box-shadow: 0 0 0 4px var(--danger), 0 5px 12px rgba(20, 28, 50, 0.28); }
}
.peg-reject { animation: peg-shake 0.36s ease; }
@keyframes peg-shake {
  0%, 100% { transform: translateX(0); }
  25% { transform: translateX(-5px); }
  75% { transform: translateX(5px); }
}

body.is-dragging { cursor: grabbing; user-select: none; }

/* ---- pills, controls, solve panel --------------------------------------- */

.pill {
  display: inline-block; font-size: 0.68rem; font-weight: 700; padding: 3px 9px;
  border-radius: 999px; text-transform: uppercase; letter-spacing: 0.04em;
}
.pill--plan { background: var(--primary-tint); color: var(--primary); }
.pill--exec { background: rgba(31, 157, 107, 0.16); color: var(--success); }

.controls { display: flex; gap: 12px; justify-content: center; margin-bottom: 14px; }

.solve-panel {
  display: flex; align-items: center; gap: 16px;
  background: var(--surface); border: 1px solid var(--border);
  border-left: 4px solid var(--success);
  border-radius: var(--radius); box-shadow: var(--shadow-md); padding: 16px 20px;
  animation: slide-up 0.35s cubic-bezier(0.22, 0.68, 0.32, 1) both;
}
@keyframes slide-up { from { opacity: 0; transform: translateY(14px); } to { opacity: 1; transform: none; } }
.solve-panel__icon {
  flex: none; width: 44px; height: 44px; border-radius: 50%;
  background: rgba(31, 157, 107, 0.16); color: var(--success);
  display: grid; place-items: center; font-size: 1.4rem; font-weight: 800;
}
.solve-panel__body { flex: 1; }
.solve-panel__headline { font-weight: 700; font-size: 1.05rem; }
.solve-panel__detail { color: var(--muted); font-size: 0.86rem; margin-top: 2px; font-variant-numeric: tabular-nums; }

.badge { font-size: 0.66rem; font-weight: 800; padding: 2px 8px; border-radius: 999px; vertical-align: middle; }
.badge--gold { background: rgba(217, 154, 28, 0.18); color: var(--gold); }
.badge--danger { background: rgba(229, 72, 77, 0.16); color: var(--danger); }
.badge--ok { background: rgba(31, 157, 107, 0.16); color: var(--success); }

/* ---- results ------------------------------------------------------------- */

.results-hero { text-align: center; }
.results-hero__label { color: var(--muted); font-weight: 600; text-transform: uppercase; letter-spacing: 0.06em; font-size: 0.78rem; }
.results-hero__score {
  font-size: clamp(3rem, 12vw, 4.6rem); font-weight: 800; letter-spacing: -0.03em; line-height: 1.05;
  background: linear-gradient(180deg, var(--primary), var(--primary-600));
  -webkit-background-clip: text; background-clip: text; color: transparent; font-variant-numeric: tabular-nums;
}
.chips { display: flex; flex-wrap: wrap; gap: 12px; justify-content: center; margin-top: 18px; }
.chip { background: var(--surface-2); border: 1px solid var(--border); border-radius: 12px; padding: 10px 16px; min-width: 92px; }
.chip__v { font-weight: 800; font-size: 1.25rem; font-variant-numeric: tabular-nums; }
.chip__k { font-size: 0.72rem; color: var(--muted); }

.table-scroll { overflow-x: auto; }
.rtable { width: 100%; border-collapse: collapse; font-size: 0.9rem; font-variant-numeric: tabular-nums; }
.rtable th {
  text-align: left; font-size: 0.72rem; text-transform: uppercase; letter-spacing: 0.04em;
  color: var(--muted); padding: 8px 12px; border-bottom: 2px solid var(--border); white-space: nowrap;
}
.rtable td { padding: 9px 12px; border-bottom: 1px solid var(--border); white-space: nowrap; }
.rtable tbody tr:hover { background: var(--surface-2); }
/* numeric columns: right-aligned so digits line up at the units */
.rtable .num { text-align: right; }
/* the Name column absorbs slack, keeping number columns tight and aligned */
.rtable .name { text-align: left; width: 99%; }
.rtable td.name { font-weight: 600; }
.rtable .actions { text-align: right; }
.rtable .strong { font-weight: 700; }
.rtable .empty { text-align: center; color: var(--muted); padding: 26px; white-space: normal; }
.rtable .rank { font-weight: 700; color: var(--muted); }
.rtable tr.mine { background: var(--primary-tint); }
.rtable tr.mine td:first-child { box-shadow: inset 3px 0 0 var(--primary); }
.lb-note { margin: 14px 2px 0; }

.tag { font-size: 0.72rem; font-weight: 700; padding: 2px 9px; border-radius: 999px; white-space: nowrap; }
.tag--opt { background: rgba(217, 154, 28, 0.18); color: var(--gold); }
.tag--ok { background: rgba(31, 157, 107, 0.16); color: var(--success); }
.tag--miss { background: var(--surface-3); color: var(--muted); }

.submit-row { display: flex; gap: 12px; flex-wrap: wrap; align-items: center; }
.submit-status { margin-top: 12px; color: var(--muted); font-size: 0.9rem; min-height: 1.2em; }
.results-actions { display: flex; gap: 12px; flex-wrap: wrap; margin-top: 18px; padding-top: 16px; border-top: 1px solid var(--border); }

/* ---- leaderboard --------------------------------------------------------- */

.lb-source { display: flex; align-items: center; gap: 8px; color: var(--muted); font-size: 0.86rem; margin-bottom: 12px; }
.lb-source code { background: var(--surface-3); padding: 1px 6px; border-radius: 6px; font-size: 0.85em; }
.lb-source__dot { width: 9px; height: 9px; border-radius: 50%; background: var(--muted); display: inline-block; }
.lb-source__dot--live { background: var(--success); box-shadow: 0 0 0 4px rgba(31, 157, 107, 0.18); }

/* ---- admin --------------------------------------------------------------- */

.head-actions { display: flex; gap: 8px; align-items: center; }
.admin-gate { text-align: center; max-width: 460px; margin: 0 auto; }
.admin-gate__icon { font-size: 2.2rem; margin-bottom: 6px; }
.admin-gate .submit-row { justify-content: center; margin-top: 14px; }
.admin-gate .input { max-width: 220px; }
.admin-status { display: flex; align-items: center; justify-content: space-between; gap: 16px; flex-wrap: wrap; }
.admin-status__info { display: flex; align-items: center; gap: 12px; }
.admin-status__season { font-weight: 800; font-size: 1.15rem; }
.admin-actions { display: flex; gap: 8px; flex-wrap: wrap; }

/* ---- misc ---------------------------------------------------------------- */

.muted { color: var(--muted); }
.small { font-size: 0.82rem; }
.foot { margin-top: 26px; text-align: center; color: var(--muted); font-size: 0.78rem; }
.foot sup { font-size: 0.7em; }

.toast {
  position: fixed; left: 50%; bottom: 26px; transform: translateX(-50%) translateY(12px);
  background: var(--text); color: var(--surface); padding: 11px 18px; border-radius: 12px;
  font-size: 0.9rem; font-weight: 600; box-shadow: var(--shadow-lg); opacity: 0;
  transition: opacity 0.25s, transform 0.25s; z-index: 100; pointer-events: none;
}
.toast--show { opacity: 1; transform: translateX(-50%) translateY(0); }

/* ---- responsive ---------------------------------------------------------- */

@media (max-width: 680px) {
  .playbar { flex-direction: column; align-items: stretch; gap: 12px; }
  .stats { justify-content: space-between; }
  .arena { grid-template-columns: 1fr; }
  .boardwrap--goal { order: -1; }
  .board { height: 260px; --ball: 50px; }
  .board--compact { height: 130px; --ball: 30px; }
  .solve-panel { flex-wrap: wrap; }
  .solve-panel__body { min-width: 60%; }
}

@media (prefers-reduced-motion: reduce) {
  * { animation-duration: 0.001ms !important; transition-duration: 0.05ms !important; }
}
