:root {
  color-scheme: light dark;
  --bg: #f6f1e7;
  --surface: #fffdf8;
  --ink: #2c2418;
  --muted: #8a7f6d;
  --border: #e3d9c6;
  --accent: #2e6b4f;
  --accent-ink: #ffffff;
  --danger: #b03a2e;
  --warn: #a5762c;
  --ok: #1e8e4e;
  --shadow: 0 1px 2px rgba(60, 45, 20, 0.06), 0 8px 24px rgba(60, 45, 20, 0.08);
  --serif: Georgia, "Iowan Old Style", "Palatino Linotype", "Times New Roman", serif;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #191613;
    --surface: #211d18;
    --ink: #ece4d4;
    --muted: #9c917d;
    --border: #3a332a;
    --accent: #55a57c;
    --accent-ink: #0f1611;
    --danger: #e06a5c;
    --warn: #d0a04e;
    --ok: #52b788;
    --shadow: 0 1px 2px rgba(0, 0, 0, 0.4), 0 8px 24px rgba(0, 0, 0, 0.35);
  }
}

* { box-sizing: border-box; }

body {
  font: 15px/1.55 system-ui, sans-serif;
  background: var(--bg);
  color: var(--ink);
  max-width: 960px;
  margin: 0 auto;
  padding: 24px 20px 48px;
}

/* --- Header ---------------------------------------------------------------- */

header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  margin-bottom: 20px;
}

.brand { display: flex; align-items: center; gap: 12px; }

.logo {
  display: flex;
  align-items: flex-end;
  gap: 3px;
  padding: 8px 9px;
  border-radius: 10px;
  background: linear-gradient(150deg, var(--accent), #1d4634);
  box-shadow: var(--shadow);
}
.logo span {
  display: block;
  width: 5px;
  border-radius: 1.5px;
  background: rgba(255, 255, 255, 0.9);
}
.logo span:nth-child(1) { height: 20px; }
.logo span:nth-child(2) { height: 26px; }
.logo span:nth-child(3) { height: 17px; transform: rotate(8deg); transform-origin: bottom; }

header h1 { margin: 0; font: 700 26px/1.2 var(--serif); letter-spacing: 0.2px; }
.tagline { margin: 2px 0 0; color: var(--muted); font-size: 13px; }

.session { display: flex; align-items: center; gap: 10px; white-space: nowrap; }
.credits {
  font-weight: 650;
  font-size: 13px;
  padding: 5px 11px;
  border: 1px solid var(--border);
  border-radius: 999px;
  background: var(--surface);
}
.topup { font-size: 13px; color: var(--accent); }

/* --- Controls -------------------------------------------------------------- */

button {
  font: inherit;
  font-size: 14px;
  padding: 8px 15px;
  border: 1px solid transparent;
  border-radius: 9px;
  background: var(--accent);
  color: var(--accent-ink);
  cursor: pointer;
  transition: filter 0.12s ease, transform 0.12s ease;
}
button:hover:not(:disabled) { filter: brightness(1.08); }
button:active:not(:disabled) { transform: translateY(1px); }
button:disabled { opacity: 0.5; cursor: default; }
button.secondary {
  background: transparent;
  color: inherit;
  border-color: var(--border);
}

.shop-toolbar { display: flex; gap: 10px; margin-bottom: 12px; }

input[type="search"], select {
  font: inherit;
  font-size: 14px;
  color: inherit;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 9px;
  padding: 8px 12px;
}
input[type="search"] { flex: 1; min-width: 0; }
input[type="search"]:focus, select:focus {
  outline: 2px solid color-mix(in srgb, var(--accent) 45%, transparent);
  outline-offset: 1px;
}

.chips { display: flex; flex-wrap: wrap; gap: 8px; margin-bottom: 18px; }
.chip {
  font-size: 13px;
  padding: 5px 12px;
  border: 1px solid var(--border);
  border-radius: 999px;
  background: var(--surface);
  color: inherit;
}
.chip .count { color: var(--muted); font-size: 12px; margin-left: 4px; }
.chip.active {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--accent-ink);
}
.chip.active .count { color: inherit; opacity: 0.75; }

h2 { font: 650 19px/1.3 var(--serif); margin: 30px 0 12px; }

/* --- Catalog --------------------------------------------------------------- */

.grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
  gap: 14px;
}

.card {
  display: flex;
  gap: 14px;
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 14px;
  background: var(--surface);
  box-shadow: var(--shadow);
  transition: transform 0.14s ease;
}
.card:hover { transform: translateY(-2px); }

.cover {
  flex: none;
  width: 88px;
  aspect-ratio: 2 / 3;
  border-radius: 3px 7px 7px 3px;
  padding: 10px 8px 8px 12px;
  display: flex;
  flex-direction: column;
  color: rgba(255, 255, 255, 0.94);
  background: linear-gradient(150deg, var(--c1, #5c6670), var(--c2, #3a424a));
  box-shadow: inset 4px 0 6px -3px rgba(0, 0, 0, 0.55), inset -1px 0 0 rgba(255, 255, 255, 0.12), var(--shadow);
}
.cover .rule { width: 22px; border-top: 1.5px solid rgba(255, 255, 255, 0.65); margin-bottom: 6px; }
.cover .cv-title {
  font: 650 11px/1.25 var(--serif);
  overflow: hidden;
  display: -webkit-box;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 5;
}
.cover .cv-author { margin-top: auto; font-size: 9px; opacity: 0.8; letter-spacing: 0.3px; text-transform: uppercase; }

/* Cover palettes, picked per genre in app.js. */
.pal-crimson { --c1: #8a3040; --c2: #571e2a; }
.pal-indigo  { --c1: #46508f; --c2: #2b3260; }
.pal-brown   { --c1: #7a5230; --c2: #52371f; }
.pal-ochre   { --c1: #a5762c; --c2: #6e4e1d; }
.pal-teal    { --c1: #2e6b6b; --c2: #1d4747; }
.pal-rust    { --c1: #b05a2e; --c2: #7a3d1e; }
.pal-plum    { --c1: #7a4a8f; --c2: #503060; }
.pal-forest  { --c1: #2e6b4f; --c2: #1d4634; }
.pal-slate   { --c1: #5c6670; --c2: #3a424a; }

.card .info { display: flex; flex-direction: column; gap: 3px; min-width: 0; flex: 1; }
.card .title { font: 650 16px/1.3 var(--serif); }
.card .author { color: var(--muted); font-size: 13px; }
.card .genre-tag {
  align-self: flex-start;
  font-size: 11px;
  letter-spacing: 0.4px;
  text-transform: uppercase;
  color: var(--muted);
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 1px 8px;
  margin-top: 2px;
}
.card .desc { font-size: 13px; color: var(--muted); flex: 1; margin-top: 4px; }

.buyrow {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  margin-top: 8px;
}
.price { font-weight: 650; font-size: 14px; }
.price .usd { color: var(--muted); font-weight: 400; font-size: 12px; }
.stock { font-size: 12px; color: var(--muted); }
.stock.low { color: var(--warn); }
.stock.out { color: var(--danger); }

.empty {
  border: 1px dashed var(--border);
  border-radius: 12px;
  padding: 28px 16px;
  text-align: center;
  color: var(--muted);
  grid-column: 1 / -1;
}
.empty button { margin-top: 10px; }

/* Loading skeletons */
.skel { pointer-events: none; }
.skel .cover, .skel .bar {
  background: linear-gradient(100deg, var(--border) 40%, color-mix(in srgb, var(--border) 40%, var(--surface)) 50%, var(--border) 60%);
  background-size: 200% 100%;
  animation: shimmer 1.2s infinite linear;
  box-shadow: none;
}
.skel .bar { height: 13px; border-radius: 6px; }
.skel .bar.w60 { width: 60%; }
.skel .bar.w40 { width: 40%; }
.skel .bar.w80 { width: 80%; margin-top: 10px; }
@keyframes shimmer { to { background-position: -200% 0; } }

/* --- Orders ---------------------------------------------------------------- */

.orders-wrap {
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--surface);
  box-shadow: var(--shadow);
  overflow: hidden;
}
table { width: 100%; border-collapse: collapse; font-size: 14px; }
th, td { text-align: left; padding: 10px 14px; border-bottom: 1px solid var(--border); }
th { color: var(--muted); font-weight: 600; font-size: 12px; letter-spacing: 0.4px; text-transform: uppercase; }
tbody tr:last-child td { border-bottom: none; }
.badge {
  font-size: 11px;
  font-weight: 650;
  letter-spacing: 0.4px;
  text-transform: uppercase;
  padding: 2px 9px;
  border-radius: 999px;
  color: var(--ok);
  background: color-mix(in srgb, var(--ok) 14%, transparent);
}
.orders-total {
  padding: 10px 14px;
  font-size: 13px;
  color: var(--muted);
  border-top: 1px solid var(--border);
}
.orders-total b { color: var(--ink); }

/* --- Buy dialog ------------------------------------------------------------ */

dialog {
  border: 1px solid var(--border);
  border-radius: 14px;
  background: var(--surface);
  color: inherit;
  padding: 20px;
  width: min(420px, calc(100vw - 32px));
  box-shadow: var(--shadow);
}
dialog::backdrop { background: rgba(20, 15, 5, 0.45); }
dialog h3 { margin: 0 0 14px; font: 650 18px/1.3 var(--serif); }

.dlg-book { display: flex; gap: 14px; margin-bottom: 14px; }
.dlg-book .cover { width: 72px; }
.dlg-title { font: 650 16px/1.3 var(--serif); }
.dlg-author { color: var(--muted); font-size: 13px; margin-top: 2px; }
.dlg-desc { font-size: 13px; color: var(--muted); margin: 8px 0 0; }

.dlg-sums { margin: 0 0 6px; border-top: 1px solid var(--border); }
.dlg-sums > div {
  display: flex;
  justify-content: space-between;
  padding: 8px 2px;
  border-bottom: 1px solid var(--border);
  font-size: 14px;
}
.dlg-sums dt { color: var(--muted); }
.dlg-sums dd { margin: 0; font-weight: 600; }
.dlg-sums .after dd { font-weight: 700; }

.dlg-warn { color: var(--danger); font-size: 13px; margin: 8px 0 0; }
.dlg-warn a { color: var(--accent); }
.dlg-actions { display: flex; justify-content: flex-end; gap: 10px; margin-top: 16px; }

/* --- Toasts ---------------------------------------------------------------- */

#toasts {
  position: fixed;
  bottom: 18px;
  right: 18px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  z-index: 10;
  max-width: min(360px, calc(100vw - 36px));
}
.toast {
  border: 1px solid var(--border);
  border-left: 4px solid var(--accent);
  border-radius: 10px;
  background: var(--surface);
  box-shadow: var(--shadow);
  padding: 11px 14px;
  font-size: 14px;
  cursor: pointer;
  animation: toast-in 0.18s ease-out;
}
.toast.error { border-left-color: var(--danger); }
.toast.ok { border-left-color: var(--ok); }
.toast a { color: var(--accent); font-weight: 600; }
@keyframes toast-in { from { opacity: 0; transform: translateY(8px); } }

/* --- Misc ------------------------------------------------------------------ */

.muted { color: var(--muted); }
.hidden { display: none; }

/* --- Disclosure notice ------------------------------------------------------ */

.notice {
  border: 1px solid var(--border);
  border-left: 4px solid var(--warn);
  border-radius: 0 12px 12px 0;
  background: var(--surface);
  box-shadow: var(--shadow);
  padding: 13px 16px;
  font-size: 13.5px;
  line-height: 1.6;
  margin: 0 0 18px;
}
.notice strong { color: var(--ink); }

.conn {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  border: 1px solid var(--border);
  border-left: 4px solid var(--danger);
  border-radius: 0 12px 12px 0;
  background: var(--surface);
  padding: 10px 14px;
  font-size: 13.5px;
  margin: 0 0 18px;
}

.catalog-note { font-size: 12.5px; margin: 0 0 10px; }

.section-head { display: flex; align-items: baseline; justify-content: space-between; gap: 12px; }
.section-head h2 { margin-bottom: 12px; }

.tagrow { display: flex; flex-wrap: wrap; gap: 6px; align-items: center; margin-top: 2px; }
.owned-tag {
  font-size: 11px;
  letter-spacing: 0.4px;
  text-transform: uppercase;
  padding: 1px 8px;
  border-radius: 999px;
  color: var(--ok);
  background: color-mix(in srgb, var(--ok) 14%, transparent);
}

.card-note {
  font-size: 11.5px;
  line-height: 1.45;
  color: var(--muted);
  border-top: 1px dashed var(--border);
  padding-top: 6px;
  margin-top: 6px;
}

.retry-box {
  border: 1px solid var(--danger);
  border-radius: 12px;
  padding: 12px 14px;
  margin-bottom: 12px;
  font-size: 13.5px;
  background: color-mix(in srgb, var(--danger) 8%, var(--surface));
}
.retry-box p { margin: 0 0 10px; }

.badge.none {
  color: var(--muted);
  background: color-mix(in srgb, var(--muted) 14%, transparent);
}
td code {
  font: 12px ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  color: var(--muted);
  word-break: break-all;
}
.orders-wrap { overflow-x: auto; }

.dlg-disclosure {
  font-size: 13px;
  line-height: 1.55;
  margin: 12px 0 10px;
  padding: 10px 12px;
  border-radius: 9px;
  border: 1px solid var(--border);
  border-left: 4px solid var(--warn);
  background: color-mix(in srgb, var(--warn) 8%, transparent);
}
.dlg-ack {
  display: flex;
  gap: 9px;
  align-items: flex-start;
  font-size: 13px;
  line-height: 1.5;
  cursor: pointer;
}
.dlg-ack input { margin-top: 2px; flex: none; }

.faq dl, dl.faq { margin: 0; }
dl.faq dt { font-weight: 650; margin-top: 14px; }
dl.faq dd { margin: 4px 0 0; color: var(--muted); font-size: 13.5px; line-height: 1.6; }
dl.faq dd a { color: var(--accent); }
dl.faq code, .faq code {
  font: 12px ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  background: color-mix(in srgb, var(--border) 45%, transparent);
  border-radius: 5px;
  padding: 0.05em 0.3em;
}

footer p { margin: 4px 0; }

footer { margin-top: 48px; font-size: 13px; color: var(--muted); text-align: center; }
footer a { color: var(--accent); }

@media (max-width: 560px) {
  header { flex-direction: column; align-items: flex-start; }
  .shop-toolbar { flex-direction: column; }
  .grid { grid-template-columns: 1fr; }
}
