:root {
  --bg: #0e0b1a;
  --bg2: #171232;
  --panel: #1d1740;
  --panel2: #251c52;
  --ink: #ece9ff;
  --muted: #a79fd6;
  --gold: #f5c542;
  --green: #38d39f;
  --red: #ff5d73;
  --purple: #8b5cf6;
  --line: #372c6b;
  --shadow: 0 10px 30px rgba(0,0,0,.45);
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  font-family: "Trebuchet MS", "Segoe UI", system-ui, sans-serif;
  /* Solid, non-tiling background. Per-mode color is set from the admin console
     via the --game-bg variable; falls back to the base dark if unset. */
  background-color: var(--game-bg, #0e0b1a);
  color: var(--ink);
  min-height: 100%;
  transition: background-color .35s ease;
}
a { color: var(--gold); }
button {
  font: inherit; cursor: pointer; border: none; border-radius: 10px;
  padding: 10px 16px; background: var(--purple); color: white; font-weight: 700;
  transition: transform .06s ease, filter .15s ease;
}
button:hover { filter: brightness(1.1); }
button:active { transform: translateY(1px); }
button.secondary { background: var(--panel2); color: var(--ink); border: 1px solid var(--line); }
button.ghost { background: transparent; color: var(--muted); }
button:disabled { opacity: .5; cursor: not-allowed; }
input {
  font: inherit; width: 100%; padding: 11px 12px; border-radius: 10px;
  border: 1px solid var(--line); background: #120e2a;
  color: var(--ink); margin-top: 6px;
}
.hidden { display: none !important; }

/* Top bar */
.topbar {
  display: flex; align-items: center; justify-content: space-between;
  padding: 14px 22px; border-bottom: 1px solid var(--line);
  background: rgba(20,15,45,.7); backdrop-filter: blur(6px);
  position: sticky; top: 0; z-index: 20;
}
.brand { display: flex; align-items: center; gap: 10px; font-weight: 900; letter-spacing: .5px; }
.brand .logo { font-size: 22px; }
.brand b { color: var(--gold); }
.topbar-right { display: flex; align-items: center; gap: 12px; }
.pill { background: var(--panel); border: 1px solid var(--line); padding: 6px 12px; border-radius: 999px; font-size: 13px; color: var(--muted); }
.pill b { color: var(--ink); }

.wrap { max-width: 1040px; margin: 0 auto; padding: 26px 20px 60px; }

/* Auth */
.auth-card {
  max-width: 420px; margin: 8vh auto 0; background: var(--panel);
  border: 1px solid var(--line); border-radius: 18px; padding: 30px;
  box-shadow: var(--shadow);
}
.auth-card h1 { margin: 0 0 4px; font-size: 26px; }
.auth-card .logo { font-size: 40px; text-align: center; }
.auth-card p.sub { color: var(--muted); margin-top: 0; text-align: center; }
.field { margin-bottom: 12px; }
.field label { font-size: 13px; color: var(--muted); }
.tabs { display: flex; gap: 8px; margin: 18px 0; }
.tabs button { flex: 1; }
.tabs button.active { background: var(--purple); }
.tabs button:not(.active) { background: var(--panel2); color: var(--muted); }
.msg { margin-top: 12px; padding: 10px 12px; border-radius: 10px; font-size: 14px; }
.msg.err { background: rgba(255,93,115,.15); color: var(--red); border: 1px solid rgba(255,93,115,.4); }
.msg.ok { background: rgba(56,211,159,.13); color: var(--green); border: 1px solid rgba(56,211,159,.35); }

/* Dungeon map */
.section-head { display:flex; align-items:baseline; justify-content:space-between; margin-bottom: 8px; }
.section-head h2 { margin: 0; }
.progress-line { height: 10px; background: var(--panel); border-radius: 999px; overflow: hidden; border: 1px solid var(--line); margin: 10px 0 22px; }
.progress-line > div { height: 100%; background: linear-gradient(90deg, var(--green), var(--gold)); }

.map {
  display: grid; grid-template-columns: repeat(10, 1fr); gap: 10px;
}
.node {
  aspect-ratio: 1 / 1; border-radius: 12px; border: 1px solid var(--line);
  background: var(--panel); display: flex; flex-direction: column; align-items: center; justify-content: center;
  position: relative; font-weight: 800; color: var(--muted); font-size: 15px;
}
.node .tier { font-size: 9px; font-weight: 700; text-transform: uppercase; letter-spacing: .4px; opacity: .8; margin-top: 2px; }
.node.done { background: linear-gradient(160deg, #1c5c48, #123c30); color: var(--green); border-color: #2b7a5f; }
.node.done::after { content: "✓"; position: absolute; top: 4px; right: 7px; font-size: 12px; color: var(--green); }
.node.current { background: linear-gradient(160deg, #6d4bd6, #45308f); color: white; border-color: var(--gold); box-shadow: 0 0 0 2px var(--gold), var(--shadow); cursor: pointer; }
.node.current:hover { filter: brightness(1.1); }
.node.locked { opacity: .55; }
.node.boss { border-color: var(--gold); }
.node.boss .crown { position:absolute; top:-10px; font-size: 16px; }
.node.boss.locked { background: linear-gradient(160deg, #4a2154, #2a1436); color: var(--gold); }

/* Play screen */
.play { display: grid; grid-template-columns: auto 1fr; gap: 26px; align-items: start; }
@media (max-width: 820px){ .play { grid-template-columns: 1fr; } }
.grid-wrap { background: var(--panel); border:1px solid var(--line); border-radius: 16px; padding: 16px; box-shadow: var(--shadow); }
.xgrid { display: grid; gap: 2px; background: var(--line); border: 3px solid var(--line); border-radius: 6px; }
.cell { width: var(--cell, 34px); height: var(--cell, 34px); background: #100c26; position: relative; }
.cell.block { background: transparent; }
.cell input {
  width: 100%; height: 100%; margin: 0; border: none; border-radius: 0; text-align: center;
  text-transform: uppercase; font-weight: 800; font-size: calc(var(--cell, 34px) * 0.5); background: #fbf7ff; color: #1a1030; padding: 0;
}
.cell input:focus { outline: 2px solid var(--gold); outline-offset: -2px; background: #fff; }
.cell.hl input { background: #fff4cf; }
.cell .num { position: absolute; top: 0; left: 2px; font-size: calc(var(--cell, 34px) * 0.3); color: #6b5fb0; font-weight: 700; pointer-events: none; line-height: 1; }
.cell.correct input { background: #d6ffe9; color: #0d5c3c; }

.clue-cols { display: grid; grid-template-columns: 1fr 1fr; gap: 18px; }
@media (max-width: 560px){ .clue-cols { grid-template-columns: 1fr; } }
.clue-col h3 { margin: 0 0 8px; color: var(--gold); font-size: 15px; }
.clue { padding: 7px 9px; border-radius: 8px; font-size: 14px; cursor: pointer; color: var(--ink); }
.clue:hover { background: var(--panel2); }
.clue.active { background: var(--panel2); outline: 1px solid var(--gold); }
.clue.solved { color: var(--green); }
.clue b { color: var(--gold); margin-right: 5px; }

.play-head { display:flex; align-items:center; justify-content: space-between; margin-bottom: 14px; }
.play-head .tier-badge { background: var(--panel2); border:1px solid var(--line); padding: 4px 10px; border-radius: 999px; font-size: 12px; color: var(--muted); }
.timer { font-variant-numeric: tabular-nums; font-weight: 800; color: var(--gold); font-size: 18px; }
.actions { margin-top: 16px; display: flex; gap: 10px; align-items: center; }

/* Leaderboard */
.board { width: 100%; border-collapse: collapse; background: var(--panel); border-radius: 14px; overflow: hidden; border: 1px solid var(--line); }
.board th, .board td { padding: 11px 14px; text-align: left; border-bottom: 1px solid var(--line); }
.board th { color: var(--muted); font-size: 13px; text-transform: uppercase; letter-spacing: .5px; }
.board tr:last-child td { border-bottom: none; }
.board td.rank { font-weight: 900; color: var(--gold); width: 60px; }
.board tr.you { background: rgba(139,92,246,.14); }
.board .medal { font-size: 16px; }

/* Victory */
.victory { text-align: center; padding: 40px 20px; }
.victory .big { font-size: 60px; }
.victory h1 { margin: 8px 0; }
.victory .time { font-size: 34px; color: var(--gold); font-weight: 900; }

.toast { position: fixed; bottom: 22px; left: 50%; transform: translateX(-50%); background: var(--panel2); border:1px solid var(--line); padding: 12px 18px; border-radius: 12px; box-shadow: var(--shadow); z-index: 50; }
.toast.ok { border-color: var(--green); color: var(--green); }
.toast.err { border-color: var(--red); color: var(--red); }
.muted { color: var(--muted); }
.center { text-align: center; }

/* ---- User menu dropdown (name pill) ---- */
.menu { position: relative; }
.menu-btn { display: inline-flex; align-items: center; gap: 6px; cursor: pointer; }
.menu-caret { font-size: 10px; opacity: .8; }
.menu-list {
  position: absolute; right: 0; top: calc(100% + 8px); min-width: 190px;
  background: var(--panel2); border: 1px solid var(--line); border-radius: 12px;
  box-shadow: var(--shadow); padding: 6px; z-index: 40; display: none;
}
.menu-list.open { display: block; }
.menu-list button {
  display: block; width: 100%; text-align: left; background: transparent; color: var(--ink);
  border-radius: 8px; padding: 9px 11px; font-weight: 600;
}
.menu-list button:hover { background: var(--panel); }
.menu-list .sep { height: 1px; background: var(--line); margin: 5px 4px; }

/* ---- Modal ---- */
.modal-overlay {
  position: fixed; inset: 0; background: rgba(6,4,16,.66); backdrop-filter: blur(3px);
  display: flex; align-items: center; justify-content: center; z-index: 60;
}
.modal {
  background: var(--panel); border: 1px solid var(--line); border-radius: 16px;
  padding: 24px; width: min(420px, 92vw); box-shadow: var(--shadow);
}
.modal h3 { margin: 0 0 6px; }
.modal .modal-actions { display: flex; gap: 10px; justify-content: flex-end; margin-top: 16px; }

/* ---- Footer ---- */
.site-footer {
  margin-top: 40px; border-top: 1px solid var(--line); padding: 22px 20px 30px;
  text-align: center; color: var(--muted); font-size: 14px;
}
.site-footer .foot-brand { font-weight: 800; color: var(--ink); }
.site-footer .foot-brand .logo-img { height: 22px; vertical-align: middle; margin-right: 6px; }
.site-footer .foot-copy { margin-top: 6px; font-size: 13px; }

/* ---- Right-side vertical ad rail ---- */
.ad-rail {
  position: fixed; top: 84px; right: 14px; width: 168px; z-index: 15;
  display: none; /* shown only when ad enabled + wide enough */
}
.ad-rail.show { display: block; }
.ad-rail .ad-label { font-size: 10px; letter-spacing: .5px; text-transform: uppercase; color: var(--muted); text-align: center; margin-bottom: 4px; }
.ad-rail .ad-box { background: var(--panel); border: 1px solid var(--line); border-radius: 10px; overflow: hidden; min-height: 300px; }
@media (max-width: 1250px) { .ad-rail { display: none !important; } }

/* Review-mode banner on the play screen */
.review-banner {
  background: rgba(56,211,159,.13); border: 1px solid rgba(56,211,159,.4); color: var(--green);
  border-radius: 10px; padding: 8px 12px; font-weight: 700; margin-bottom: 14px; font-size: 14px;
}
.cell input:disabled { background: #eef6ff; color: #24406b; -webkit-text-fill-color: #24406b; opacity: 1; }
.cell.review input:disabled { background: #e7fff2; color: #0d5c3c; -webkit-text-fill-color: #0d5c3c; }

/* Leaderboard scope tabs */
.lb-tabs { display: flex; gap: 8px; margin: 6px 0 10px; }
.lb-tabs button { background: var(--panel2); color: var(--muted); border: 1px solid var(--line); padding: 8px 16px; border-radius: 999px; font-weight: 700; }
.lb-tabs button.active { background: var(--purple); color: #fff; border-color: var(--gold); }

/* Active-theme banner on the dungeon map */
.theme-banner {
  background: linear-gradient(90deg, rgba(139,92,246,.18), rgba(245,197,66,.12));
  border: 1px solid var(--line); border-radius: 12px; padding: 10px 14px; margin: 4px 0 14px;
  font-size: 15px; color: var(--ink);
}
.theme-banner b { color: var(--gold); }

/* Large daily grid can scroll horizontally on narrow screens */
.grid-wrap { overflow-x: hidden; }

/* The Long Crawl calendar */
.crawl-years { display: flex; flex-wrap: wrap; gap: 6px; margin: 6px 0; }
.crawl-years button { background: var(--panel2); color: var(--muted); border: 1px solid var(--line); padding: 6px 12px; border-radius: 999px; font-weight: 700; }
.crawl-years button.active { background: var(--purple); color: #fff; border-color: var(--gold); }
.crawl-cal { display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 12px; }
.crawl-month { background: var(--panel); border: 1px solid var(--line); border-radius: 10px; padding: 8px; }
.crawl-month h4 { margin: 0 0 6px; font-size: 12px; color: var(--muted); text-transform: uppercase; letter-spacing: .4px; }
.crawl-grid { display: grid; grid-template-columns: repeat(7, 1fr); gap: 2px; }
.crawl-day { aspect-ratio: 1/1; display: flex; align-items: center; justify-content: center; font-size: 10px; border-radius: 3px; color: #0b0820; font-weight: 700; }
.crawl-day.blank { background: transparent; }
.crawl-day.future { background: #1a1440; color: var(--muted); }
.crawl-day.g { background: var(--green); }
.crawl-day.o { background: #f5a742; }
.crawl-day.r { background: var(--red); }
.crawl-day.a { background: var(--gold); cursor: pointer; box-shadow: 0 0 0 2px var(--gold); }
.crawl-legend { display: flex; gap: 16px; flex-wrap: wrap; margin-top: 12px; color: var(--muted); font-size: 13px; align-items: center; }
.crawl-legend .sw { display: inline-block; width: 12px; height: 12px; border-radius: 3px; margin-right: 5px; vertical-align: middle; }
.crawl-legend .sw.g { background: var(--green); } .crawl-legend .sw.o { background: #f5a742; }
.crawl-legend .sw.r { background: var(--red); } .crawl-legend .sw.a { background: var(--gold); }

/* Rendered markdown (About modal + admin preview) */
.md-body { line-height: 1.6; }
.md-body h2 { color: var(--gold); margin: 18px 0 6px; font-size: 20px; }
.md-body h3 { color: var(--ink); margin: 14px 0 4px; font-size: 17px; }
.md-body p { margin: 8px 0; color: var(--ink); }
.md-body ul, .md-body ol { margin: 8px 0 8px 20px; color: var(--ink); }
.md-body li { margin: 4px 0; }
.md-body strong { color: var(--gold); }
.md-body code { background: #120e2a; padding: 1px 5px; border-radius: 5px; font-size: 13px; }
.md-body blockquote { border-left: 3px solid var(--purple); margin: 8px 0; padding: 4px 12px; color: var(--muted); }
.md-body hr { border: none; border-top: 1px solid var(--line); margin: 14px 0; }

/* Admin About editor: side-by-side edit + preview */
.about-edit { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; }
@media (max-width: 860px) { .about-edit { grid-template-columns: 1fr; } }

/* ---- Achievements ------------------------------------------------------ */
.ach-progress { margin: 10px 0 4px; }
.ach-progress .ach-count { font-size: 14px; margin-bottom: 6px; }
.ach-bar { height: 10px; border-radius: 6px; background: #241d45; overflow: hidden; }
.ach-bar > div { height: 100%; background: linear-gradient(90deg, var(--gold), #ffd977); border-radius: 6px; }
.ach-bar.sm { height: 6px; margin-top: 6px; }
.ach-group { margin: 18px 0 8px; font-size: 15px; border-bottom: 1px solid var(--line); padding-bottom: 5px; }
.ach-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(240px, 1fr)); gap: 10px; }
.ach-card { display: flex; gap: 10px; padding: 10px; border: 1px solid var(--line); border-radius: 10px; background: #171232; }
.ach-card.earned { border-color: var(--gold); background: #241d0e; box-shadow: 0 0 0 1px rgba(230,190,90,.25) inset; }
.ach-card.locked { opacity: .72; }
.ach-ico { font-size: 26px; line-height: 1; width: 34px; text-align: center; }
.ach-card.locked .ach-ico { filter: grayscale(1) opacity(.7); }
.ach-cbody { flex: 1; min-width: 0; }
.ach-name { font-weight: 800; font-size: 14px; }
.ach-desc { font-size: 12.5px; color: var(--muted); margin: 2px 0; }
.ach-meta { font-size: 12px; }
.ach-done { color: var(--gold); font-weight: 700; }

/* ---- My Stats ---------------------------------------------------------- */
.stat-hero { margin: 12px 0 4px; padding: 10px 12px; border: 1px solid var(--gold); border-radius: 10px; background: #241d0e; font-size: 15px; }
.stat-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(140px, 1fr)); gap: 10px; }
.stat-card { border: 1px solid var(--line); border-radius: 10px; background: #171232; padding: 12px; text-align: center; }
.stat-val { font-size: 24px; font-weight: 900; color: var(--gold); }
.stat-label { font-size: 12.5px; color: var(--muted); margin-top: 2px; }
.stat-sub { font-size: 11px; color: var(--muted); opacity: .8; }

/* Admin: background color pickers */
.bg-row { display: flex; align-items: center; gap: 10px; margin: 8px 0; }
.bg-row label { flex: 0 0 190px; font-size: 14px; }
.bg-row input[type=color] { width: 44px; height: 30px; padding: 0; border: 1px solid var(--line); border-radius: 6px; background: none; cursor: pointer; }
.bg-row .hexin { width: 110px; font-family: monospace; padding: 7px 9px; border-radius: 8px; border: 1px solid var(--line); background: #120e2a; color: var(--ink); }

/* Guest "try the daily" divider on the login card */
#guestTry { margin-top: 18px; }
.guest-or { display: flex; align-items: center; text-align: center; color: var(--muted); font-size: 12px; margin-bottom: 12px; }
.guest-or::before, .guest-or::after { content: ""; flex: 1; height: 1px; background: var(--line); }
.guest-or span { padding: 0 10px; }

/* Brand acts as a home link */
a.brand { text-decoration: none; color: var(--ink); cursor: pointer; }
a.brand:hover { filter: brightness(1.15); }

/* Inline login/sign-up dropdown on the main page */
#guestMenuList { width: 272px; padding: 14px; }
#guestMenuList input { width: 100%; margin: 0 0 8px; }
.menu-list button.primary { background: var(--purple); color: #fff; text-align: center; font-weight: 700; padding: 10px 12px; }
.menu-list button.primary:hover { background: var(--purple); filter: brightness(1.12); }
.pill-links { margin-top: 10px; font-size: 12.5px; text-align: center; color: var(--muted); }
.pill-links a { cursor: pointer; color: var(--gold); }
#pillMsg .msg { margin-top: 10px; }

/* ---- Sticky footer: keep the footer at the bottom on short pages ---- */
#appView { display: flex; flex-direction: column; min-height: 100vh; }
#appView > .wrap { flex: 1 0 auto; }        /* content grows to fill the viewport */
#appView > .site-footer { flex-shrink: 0; margin-top: auto; }  /* pinned to the bottom */
