/* =========================================================
   PuzzleTales AI — themeable UI (5 themes)
   Theme is set with <body class="theme-NAME">
   ========================================================= */

:root, .theme-midnight {
  --bg: #0C0E1A; --surface: #141831; --surface-2: #11142A; --border: #22264A; --border-w: 1px;
  --text: #E7E9F5; --muted: #A7ACCB; --faint: #6E7399;
  --accent: #7C6CFF; --accent-ink: #FFFFFF; --accent-2: #38D0E0; --accent-2-ink: #06212A; --accent-soft: #1E2244;
  --side-bg: #11142A; --side-text: #A7ACCB; --side-active-bg: #1E2244; --side-active-text: #FFFFFF; --side-active-mark: #7C6CFF;
  --input-bg: #0F1226; --success: #4ADE9B; --warn: #F5C451; --danger: #FF6B7D;
  --radius: 16px; --radius-sm: 10px; --shadow: none;
  --font-body: 'Plus Jakarta Sans', system-ui, sans-serif; --font-head: 'Plus Jakarta Sans', system-ui, sans-serif; --font-mono: 'JetBrains Mono', ui-monospace, monospace;
  --head-weight: 800; --upsell-bg: #1A1640; --upsell-border: #3B3380; --upsell-text: #C2C6E2;
  color-scheme: dark;
}
.theme-storybook {
  --bg: #FBF6EC; --surface: #FFFFFF; --surface-2: #F3EADB; --border: #E6D9C3; --border-w: 1px;
  --text: #2B2118; --muted: #6B5644; --faint: #7A6551;
  --accent: #D9603B; --accent-ink: #FFFFFF; --accent-2: #2F7F7A; --accent-2-ink: #F2FBF9; --accent-soft: #FDF1EA;
  --side-bg: #F3EADB; --side-text: #4A3B2E; --side-active-bg: #2B2118; --side-active-text: #FBF6EC; --side-active-mark: transparent;
  --input-bg: #FBF6EC; --success: #2F7F7A; --warn: #B7791F; --danger: #B83A2B;
  --radius: 22px; --radius-sm: 12px; --shadow: none;
  --font-body: 'Nunito Sans', system-ui, sans-serif; --font-head: 'Fraunces', Georgia, serif; --font-mono: ui-monospace, monospace;
  --head-weight: 700; --upsell-bg: #F6DCCB; --upsell-border: #F6DCCB; --upsell-text: #4A3B2E;
  color-scheme: light;
}
.theme-cleanpro {
  --bg: #F6F7F9; --surface: #FFFFFF; --surface-2: #F9FAFB; --border: #E5E7EB; --border-w: 1px;
  --text: #111827; --muted: #4B5563; --faint: #6B7280;
  --accent: #3B5BDB; --accent-ink: #FFFFFF; --accent-2: #111827; --accent-2-ink: #FFFFFF; --accent-soft: #EEF2FF;
  --side-bg: #FFFFFF; --side-text: #374151; --side-active-bg: #EEF2FF; --side-active-text: #2F4BC4; --side-active-mark: #3B5BDB;
  --input-bg: #FFFFFF; --success: #047857; --warn: #B45309; --danger: #B91C1C;
  --radius: 10px; --radius-sm: 6px; --shadow: 0 1px 2px rgba(16,24,40,.05);
  --font-body: 'IBM Plex Sans', system-ui, sans-serif; --font-head: 'IBM Plex Sans', system-ui, sans-serif; --font-mono: 'IBM Plex Mono', ui-monospace, monospace;
  --head-weight: 700; --upsell-bg: #EEF2FF; --upsell-border: #C7D2FE; --upsell-text: #374151;
  color-scheme: light;
}
.theme-noir {
  --bg: #14120E; --surface: #1F1B15; --surface-2: #1B1813; --border: #3A3227; --border-w: 1px;
  --text: #EFE6D2; --muted: #D8CDB5; --faint: #A89A80;
  --accent: #E9B54A; --accent-ink: #14120E; --accent-2: #7A2E1E; --accent-2-ink: #F8F2E4; --accent-soft: #2A241B;
  --side-bg: #1B1813; --side-text: #D8CDB5; --side-active-bg: #E9B54A; --side-active-text: #14120E; --side-active-mark: transparent;
  --input-bg: #17140F; --success: #9FD49B; --warn: #E9B54A; --danger: #E57C6B;
  --radius: 2px; --radius-sm: 2px; --shadow: none;
  --font-body: 'Libre Franklin', system-ui, sans-serif; --font-head: 'Playfair Display', Georgia, serif; --font-mono: 'Courier Prime', ui-monospace, monospace;
  --head-weight: 800; --upsell-bg: transparent; --upsell-border: #E9B54A; --upsell-text: #D8CDB5;
  color-scheme: dark;
}
.theme-candypop {
  --bg: #FFF4D6; --surface: #FFFFFF; --surface-2: #FFF9EA; --border: #1B1B1B; --border-w: 3px;
  --text: #1B1B1B; --muted: #3A3A3A; --faint: #555555;
  --accent: #E0457B; --accent-ink: #FFFFFF; --accent-2: #3A86FF; --accent-2-ink: #FFFFFF; --accent-soft: #FFD23F;
  --side-bg: #FFFFFF; --side-text: #1B1B1B; --side-active-bg: #FFD23F; --side-active-text: #1B1B1B; --side-active-mark: transparent;
  --input-bg: #FFF4D6; --success: #1E8A55; --warn: #B45309; --danger: #C62828;
  --radius: 20px; --radius-sm: 14px; --shadow: 5px 5px 0 #1B1B1B;
  --font-body: 'Space Grotesk', system-ui, sans-serif; --font-head: 'Baloo 2', system-ui, sans-serif; --font-mono: ui-monospace, monospace;
  --head-weight: 800; --upsell-bg: #FFD23F; --upsell-border: #1B1B1B; --upsell-text: #1B1B1B;
  color-scheme: light;
}

/* ---------- Base ---------- */
*, *::before, *::after { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body { margin: 0; background: var(--bg); color: var(--text); font-family: var(--font-body); font-size: 15px; line-height: 1.55; }
a { color: var(--accent); }
h1, h2, h3, h4 { font-family: var(--font-head); font-weight: var(--head-weight); line-height: 1.2; margin: 0 0 .4em; letter-spacing: -0.01em; }
h1 { font-size: clamp(24px, 3vw, 34px); }
h2 { font-size: 20px; }
h3 { font-size: 17px; }
p { margin: 0 0 1em; }
code, .mono { font-family: var(--font-mono); font-size: .92em; }
img { max-width: 100%; }
.muted { color: var(--muted); }
.faint { color: var(--faint); }
.small { font-size: 13px; }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
hr { border: 0; border-top: var(--border-w) solid var(--border); margin: 20px 0; }

/* ---------- Layout ---------- */
.layout { display: flex; min-height: 100vh; }
.sidebar { width: 256px; flex: 0 0 256px; background: var(--side-bg); border-right: var(--border-w) solid var(--border); padding: 24px 16px; display: flex; flex-direction: column; gap: 3px; position: sticky; top: 0; height: 100vh; overflow-y: auto; }
.theme-candypop .sidebar { margin: 16px 0 16px 16px; height: calc(100vh - 32px); border-radius: var(--radius); box-shadow: var(--shadow); border: 3px solid #1B1B1B; top: 16px; }
.brand { display: flex; align-items: center; gap: 10px; padding: 0 10px 22px; text-decoration: none; color: var(--text); }
.brand-mark { width: 36px; height: 36px; border-radius: var(--radius-sm); background: var(--accent); color: var(--accent-ink); display: flex; align-items: center; justify-content: center; flex: 0 0 36px; }
.brand-name { font-family: var(--font-head); font-weight: var(--head-weight); font-size: 19px; line-height: 1.1; }
.brand-name span { color: var(--accent-2); }
.theme-noir .brand-name { color: var(--accent); }
.theme-noir .brand-name span { color: var(--muted); }
.brand img { max-height: 36px; }
.nav-link { display: flex; align-items: center; gap: 11px; min-height: 42px; padding: 0 12px; border-radius: var(--radius-sm); color: var(--side-text); text-decoration: none; font-weight: 600; font-size: 14.5px; border: var(--border-w) solid transparent; }
.nav-link svg { flex: 0 0 18px; opacity: .85; }
.nav-link:hover { background: var(--accent-soft); color: var(--text); }
.nav-link.active { background: var(--side-active-bg); color: var(--side-active-text); box-shadow: inset 3px 0 0 var(--side-active-mark); }
.theme-candypop .nav-link.active { border-color: #1B1B1B; }
.theme-noir .nav-link { font-family: var(--font-mono); font-weight: 400; }
.nav-link .tag { margin-left: auto; font-size: 10px; font-weight: 700; padding: 2px 7px; border-radius: 6px; border: 1px solid var(--accent-2); color: var(--accent-2); font-family: var(--font-mono); }
.theme-storybook .nav-link .tag { background: #E3EFEC; color: #1F5F5B; border-color: transparent; border-radius: 99px; }
.theme-candypop .nav-link .tag { background: var(--accent-2); color: #fff; border: 0; border-radius: 99px; }
.theme-noir .nav-link .tag { border-color: var(--faint); color: var(--faint); }
.nav-section { margin: 16px 12px 6px; font-size: 11px; letter-spacing: .13em; text-transform: uppercase; color: var(--faint); font-weight: 700; }
.nav-spacer { flex: 1; }
.main { flex: 1; min-width: 0; padding: 28px 36px 60px; }
.theme-candypop .main { padding-top: 16px; }
.topbar { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: 14px; margin-bottom: 24px; }
.topbar h1 { margin: 0; }
.topbar .actions { display: flex; flex-wrap: wrap; gap: 10px; align-items: center; }
.btn.menu-toggle { display: none; margin-bottom: 14px; }
.container-narrow { max-width: 760px; }

/* ---------- Components ---------- */
.card { background: var(--surface); border: var(--border-w) solid var(--border); border-radius: var(--radius); padding: 22px; box-shadow: var(--shadow); }
.card + .card { margin-top: 18px; }
.grid > .card, .row > .card, .stack > .card, .row > * > .card:first-child { margin-top: 0; }
.card-head { display: flex; justify-content: space-between; align-items: center; gap: 12px; margin-bottom: 14px; flex-wrap: wrap; }
.card-head h2, .card-head h3 { margin: 0; }
.grid { display: grid; gap: 16px; }
.grid-2 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.grid-3 { grid-template-columns: repeat(3, minmax(0, 1fr)); }
.grid-4 { grid-template-columns: repeat(4, minmax(0, 1fr)); }
.row { display: flex; flex-wrap: wrap; gap: 18px; }
.row > .grow { flex: 2 1 480px; min-width: 0; }
.row > .side { flex: 1 1 280px; min-width: 0; }
.stack { display: flex; flex-direction: column; gap: 14px; }
.inline { display: flex; flex-wrap: wrap; gap: 10px; align-items: center; }

.stat .label { font-size: 12px; font-weight: 700; color: var(--faint); text-transform: uppercase; letter-spacing: .07em; }
.theme-noir .stat .label { font-family: var(--font-mono); }
.stat .value { font-family: var(--font-head); font-size: 30px; font-weight: var(--head-weight); margin-top: 4px; line-height: 1.15; }
.stat .value small { font-size: 15px; color: var(--faint); font-weight: 500; }
.stat .note { font-size: 12.5px; color: var(--muted); }
.progress { height: 7px; border-radius: 99px; background: var(--surface-2); margin-top: 10px; overflow: hidden; border: var(--border-w) solid var(--border); }
.theme-midnight .progress, .theme-storybook .progress, .theme-cleanpro .progress { border: 0; }
.progress > span { display: block; height: 100%; background: var(--accent-2); border-radius: 99px; }

.btn { display: inline-flex; align-items: center; justify-content: center; gap: 8px; min-height: 42px; padding: 0 18px; border-radius: var(--radius-sm); border: var(--border-w) solid var(--border); background: var(--surface); color: var(--text); font: inherit; font-weight: 700; font-size: 14.5px; text-decoration: none; cursor: pointer; white-space: nowrap; }
.btn:hover { filter: brightness(1.06); }
.btn:disabled { opacity: .55; cursor: not-allowed; }
.btn-primary { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }
.btn-secondary { background: var(--accent-2); color: var(--accent-2-ink); border-color: var(--accent-2); }
.btn-ghost { background: transparent; }
.btn-danger { background: transparent; color: var(--danger); border-color: var(--danger); }
.btn-sm { min-height: 34px; padding: 0 12px; font-size: 13px; }
.btn-lg { min-height: 50px; padding: 0 26px; font-size: 16px; }
.theme-storybook .btn-primary, .theme-storybook .btn-secondary { border-radius: 99px; }
.theme-candypop .btn { border-color: #1B1B1B; }
.theme-candypop .btn-primary, .theme-candypop .btn-secondary { box-shadow: 3px 3px 0 #1B1B1B; }
.theme-noir .btn { font-family: var(--font-mono); text-transform: uppercase; letter-spacing: .03em; }

.badge { display: inline-block; font-size: 11.5px; font-weight: 700; padding: 2px 9px; border-radius: 99px; background: var(--accent-soft); color: var(--text); border: 1px solid var(--border); white-space: nowrap; }
.badge-success { color: var(--success); border-color: currentColor; background: transparent; }
.badge-warn { color: var(--warn); border-color: currentColor; background: transparent; }
.badge-danger { color: var(--danger); border-color: currentColor; background: transparent; }
.badge-accent { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }

label { display: block; font-size: 13px; font-weight: 700; color: var(--muted); margin-bottom: 6px; }
.field { margin-bottom: 14px; }
input[type=text], input[type=email], input[type=password], input[type=number], input[type=url], input[type=search], select, textarea {
  width: 100%; min-height: 44px; padding: 9px 12px; border-radius: var(--radius-sm); border: var(--border-w) solid var(--border); background: var(--input-bg); color: var(--text); font: inherit; font-size: 15px; }
textarea { min-height: 110px; resize: vertical; line-height: 1.5; }
input:focus, select:focus, textarea:focus, .btn:focus-visible, a:focus-visible { outline: 2px solid var(--accent-2); outline-offset: 2px; }
.help { font-size: 12.5px; color: var(--faint); margin-top: 5px; font-weight: 400; }
.check { display: flex; align-items: center; gap: 9px; font-weight: 600; color: var(--text); font-size: 14px; }
.check input { width: 18px; height: 18px; accent-color: var(--accent); }

.choices { display: grid; grid-template-columns: repeat(auto-fit, minmax(180px, 1fr)); gap: 10px; }
.choice { position: relative; display: block; padding: 15px; border-radius: var(--radius-sm); border: var(--border-w) solid var(--border); background: var(--surface-2); cursor: pointer; margin: 0; color: var(--text); font-size: 14px; }
.choice input { position: absolute; opacity: 0; }
.choice b { display: block; font-size: 15.5px; }
.choice span { color: var(--muted); font-weight: 400; font-size: 13px; }
.choice:has(input:checked) { border-color: var(--accent); background: var(--accent-soft); box-shadow: 0 0 0 1px var(--accent) inset; }
.theme-candypop .choice:has(input:checked) { box-shadow: 3px 3px 0 #1B1B1B; }
.choice.locked { opacity: .55; }

.alert { padding: 12px 16px; border-radius: var(--radius-sm); border: var(--border-w) solid var(--border); background: var(--surface); margin-bottom: 16px; font-weight: 600; }
.alert-success { border-color: var(--success); color: var(--success); }
.alert-error { border-color: var(--danger); color: var(--danger); }
.alert-info { border-color: var(--accent-2); }

.table-wrap { overflow-x: auto; border: var(--border-w) solid var(--border); border-radius: var(--radius); background: var(--surface); }
table.table { width: 100%; border-collapse: collapse; font-size: 14px; }
.table th { text-align: left; padding: 12px 16px; font-size: 12px; text-transform: uppercase; letter-spacing: .06em; color: var(--faint); background: var(--surface-2); white-space: nowrap; }
.table td { padding: 12px 16px; border-top: 1px solid var(--border); vertical-align: middle; }
.table tr:hover td { background: var(--accent-soft); }

.upsell { background: var(--upsell-bg); border: var(--border-w) solid var(--upsell-border); border-radius: var(--radius); padding: 22px; color: var(--upsell-text); display: flex; flex-direction: column; gap: 10px; box-shadow: var(--shadow); }
.theme-noir .upsell { border-style: dashed; }
.upsell .eyebrow { font-family: var(--font-mono); font-size: 11.5px; letter-spacing: .12em; text-transform: uppercase; color: var(--accent); font-weight: 700; }
.upsell h3 { color: var(--text); margin: 0; font-size: 20px; }

.books { display: grid; grid-template-columns: repeat(auto-fill, minmax(190px, 1fr)); gap: 18px; }
.book-card { display: flex; flex-direction: column; gap: 8px; text-decoration: none; color: var(--text); }
.book-cover { aspect-ratio: 8.5 / 11; border-radius: var(--radius-sm); padding: 16px; display: flex; flex-direction: column; justify-content: flex-end; font-family: var(--font-head); font-weight: var(--head-weight); font-size: 18px; line-height: 1.15; color: #fff; background-size: cover; background-position: center; position: relative; overflow: hidden; border: var(--border-w) solid var(--border); }
.book-cover.c0 { background-color: #2F7F7A; } .book-cover.c1 { background-color: #D9603B; } .book-cover.c2 { background-color: #5B4BD6; } .book-cover.c3 { background-color: #4A3B2E; } .book-cover.c4 { background-color: #C2185B; } .book-cover.c5 { background-color: #1F6FB2; }
.book-cover .type { position: absolute; top: 12px; left: 12px; font-family: var(--font-body); font-size: 11px; font-weight: 700; background: rgba(0,0,0,.35); padding: 3px 8px; border-radius: 99px; }
.book-cover .t { text-shadow: 0 1px 8px rgba(0,0,0,.35); }
.book-meta { font-size: 13px; color: var(--muted); }

.empty { text-align: center; padding: 48px 20px; color: var(--muted); }
.tabs { display: flex; gap: 6px; border-bottom: var(--border-w) solid var(--border); margin-bottom: 18px; flex-wrap: wrap; }
.tab { padding: 10px 14px; border: 0; background: none; color: var(--muted); font: inherit; font-weight: 700; cursor: pointer; border-bottom: 2px solid transparent; margin-bottom: -1px; text-decoration: none; }
.tab.active { color: var(--text); border-bottom-color: var(--accent); }
.pager { display: flex; gap: 6px; margin-top: 16px; flex-wrap: wrap; }

.chapter-list { display: flex; flex-direction: column; gap: 8px; }
.chapter-item { display: flex; gap: 12px; align-items: center; padding: 12px 14px; border-radius: var(--radius-sm); border: var(--border-w) solid var(--border); background: var(--surface-2); }
.chapter-item .num { font-family: var(--font-mono); font-size: 12px; color: var(--faint); min-width: 26px; }
.chapter-item .ttl { flex: 1; min-width: 0; font-weight: 700; }
.chapter-item .st { font-size: 12px; }
.log { font-family: var(--font-mono); font-size: 12.5px; background: var(--input-bg); border: var(--border-w) solid var(--border); border-radius: var(--radius-sm); padding: 12px; max-height: 220px; overflow-y: auto; white-space: pre-wrap; color: var(--muted); }
.spinner { width: 16px; height: 16px; border: 2px solid currentColor; border-right-color: transparent; border-radius: 50%; display: inline-block; animation: spin .7s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
.img-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(160px, 1fr)); gap: 12px; }
.img-grid figure { margin: 0; border-radius: var(--radius-sm); overflow: hidden; border: var(--border-w) solid var(--border); background: var(--surface-2); }
.img-grid img { display: block; width: 100%; aspect-ratio: 3/4; object-fit: cover; }
.img-grid figcaption { font-size: 12px; padding: 6px 8px; color: var(--muted); }
.score { display: inline-flex; align-items: center; justify-content: center; width: 30px; height: 30px; border-radius: 50%; font-weight: 800; font-size: 13px; background: var(--accent-soft); border: 1px solid var(--border); }
.kv { display: grid; grid-template-columns: 160px 1fr; gap: 8px 16px; font-size: 14px; }
.kv dt { color: var(--faint); font-weight: 700; } .kv dd { margin: 0; }
.copybox { position: relative; }
.copybox pre { white-space: pre-wrap; background: var(--input-bg); border: var(--border-w) solid var(--border); border-radius: var(--radius-sm); padding: 12px; font-size: 13.5px; margin: 0; }

/* Locked overlay page */
.locked-hero { max-width: 720px; margin: 40px auto; text-align: center; }
.locked-hero .lock { width: 64px; height: 64px; border-radius: 50%; margin: 0 auto 16px; display: flex; align-items: center; justify-content: center; background: var(--accent-soft); color: var(--accent); border: var(--border-w) solid var(--border); }
.locked-hero ul { text-align: left; display: inline-block; margin: 10px auto 22px; }

/* Auth pages */
.auth-wrap { min-height: 100vh; display: flex; align-items: center; justify-content: center; padding: 24px 16px; }
.auth-card { width: 100%; max-width: 420px; }
.auth-card .brand { justify-content: center; padding-bottom: 18px; }

/* ---------- Responsive ---------- */
@media (max-width: 1100px) { .grid-4 { grid-template-columns: repeat(2, minmax(0, 1fr)); } }
@media (max-width: 860px) {
  .layout { display: block; }
  .sidebar { position: fixed; z-index: 50; left: 0; top: 0; bottom: 0; transform: translateX(-105%); transition: transform .2s; box-shadow: 0 0 40px rgba(0,0,0,.3); }
  .theme-candypop .sidebar { margin: 0; height: 100vh; top: 0; border-radius: 0; }
  body.nav-open .sidebar { transform: none; }
  .btn.menu-toggle { display: inline-flex; }
  .main { padding: 18px 16px 48px; }
  .grid-2, .grid-3 { grid-template-columns: 1fr; }
  .kv { grid-template-columns: 1fr; }
}
@media (max-width: 520px) { .grid-4 { grid-template-columns: 1fr 1fr; } .stat .value { font-size: 24px; } }
