:root {
  /* Match Dungeon Builder light mode palette */
  --bg: #e9eff6;
  --paper: #f8fafc;
  --ink: #0b1220;
  --muted: #475569;
  --line: #cbd5e1;
  --accent: #2563eb;
  --accent-hover: #1d4ed8;
  --radius: 0px; /* cards: no border radius */
  --btn-radius: 4px;
  --max: 1120px;
}

* {
  box-sizing: border-box;
}
html,
body {
  height: 100%;
}
body {
  margin: 0;
  font-family:
    ui-sans-serif,
    system-ui,
    -apple-system,
    Segoe UI,
    Roboto,
    Inter,
    Arial,
    sans-serif;
  color: var(--ink);
  background: var(--bg);
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

a {
  color: inherit;
}
.hidden {
  display: none !important;
}

.container {
  max-width: var(--max);
  margin: 0 auto;
  padding: 0 16px;
}

/* Top bar */
.topbar {
  position: sticky;
  top: 0;
  z-index: 20;
  background: var(--paper);
  border-bottom: 1px solid var(--line);
  backdrop-filter: none;
}
.topbar-inner {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 14px 0;
}
.brand {
  display: flex;
  align-items: center;
  gap: 10px;
  min-width: 0;
  text-decoration: none;
}
.brand-logo {
  width: 30px;
  height: 30px;
  border-radius: 6px;
  background: transparent;
  border: none;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
}
.brand-logo img {
  width: 100%;
  height: 100%;
  object-fit: contain;
}
.brand-title {
  font-weight: 900;
  letter-spacing: -0.02em;
  font-size: 18px;
  white-space: nowrap;
}
.brand-sub {
  color: var(--muted);
  font-size: 12px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.actions {
  display: flex;
  align-items: center;
  gap: 10px;
  flex: 0 0 auto;
}

.auth {
  display: flex;
  align-items: center;
  gap: 10px;
}
.auth-anon,
.auth-user {
  display: flex;
  align-items: center;
  gap: 10px;
}
.btn {
  appearance: none;
  display: inline-block;
  border: 1px solid var(--line);
  background: var(--paper);
  color: var(--ink);
  border-radius: var(--btn-radius);
  padding: 10px 14px;
  font-weight: 800;
  font-size: 12px;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  cursor: pointer;
  box-shadow: none;
  text-decoration: none;
}
.btn.primary {
  border-color: var(--accent);
  background: var(--accent);
  color: #fff;
}
.btn:hover {
  border-color: color-mix(in srgb, var(--accent) 45%, var(--line));
}
.btn.primary:hover {
  background: var(--accent-hover);
  border-color: var(--accent-hover);
}
.btn:active {
  transform: translateY(1px);
}

/* Responsive nav (hamburger on mobile) */
.nav-toggle {
  display: none;
  appearance: none;
  border: 1px solid var(--line);
  background: var(--paper);
  color: var(--ink);
  border-radius: var(--btn-radius);
  padding: 10px 12px;
  font-weight: 900;
  font-size: 12px;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  cursor: pointer;
}
.nav-menu {
  display: flex;
  align-items: center;
  gap: 10px;
}

@media (max-width: 720px) {
  .actions {
    position: relative;
  }
  .nav-toggle {
    display: inline-flex;
    align-items: center;
    justify-content: center;
  }
  .nav-menu {
    display: none;
    position: absolute;
    right: 0;
    top: calc(100% + 10px);
    z-index: 40;
    min-width: 220px;
    padding: 10px;
    border: 1px solid var(--line);
    background: #fff;
    flex-direction: column;
    align-items: stretch;
    gap: 10px;
  }
  .nav-menu.open {
    display: flex;
  }
  .nav-menu .btn {
    width: 100%;
    text-align: center;
  }
  .auth {
    width: 100%;
  }
  .auth-anon,
  .auth-user {
    width: 100%;
    flex-direction: column;
    align-items: stretch;
  }
}

/* Page */
main {
  padding: 18px 0 28px;
  flex: 1 0 auto;
}

/* Footer */
.site-footer {
  border-top: 1px solid var(--line);
  background: var(--paper);
  padding: 16px 0;
  margin-top: auto;
}

.footer-inner {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  flex-wrap: wrap;
}

.footer-links {
  display: flex;
  gap: 12px;
  flex-wrap: wrap;
  align-items: center;
}

.footer-link {
  color: var(--muted);
  text-decoration: none;
  font-size: 13px;
  font-weight: 800;
}

.footer-link:hover {
  color: var(--ink);
  text-decoration: underline;
}

.footer-meta {
  color: var(--muted);
  font-size: 12px;
}

.status {
  color: var(--muted);
  font-size: 13px;
  min-height: 18px;
  margin: 8px 0 10px;
}

.section {
  margin: 16px 0 22px;
}

.section-head {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 10px;
}
.section-title {
  margin: 0;
  font-size: 13px;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  color: var(--ink);
}

.count-pill {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 22px;
  height: 20px;
  margin-left: 8px;
  padding: 0 7px;
  border: 1px solid var(--line);
  background: #fff;
  border-radius: 999px;
  font-size: 12px;
  font-weight: 900;
  letter-spacing: 0;
  text-transform: none;
  line-height: 1;
}
.section-hint {
  color: var(--muted);
  font-size: 12px;
}

/* Shelf */
.shelf {
  display: flex;
  gap: 14px;
  overflow-x: auto;
  overflow-y: hidden;
  padding: 6px 2px 12px;
  scroll-snap-type: x mandatory;
  -webkit-overflow-scrolling: touch;
}
.shelf::-webkit-scrollbar {
  height: 10px;
}
.shelf::-webkit-scrollbar-track {
  background: transparent;
}
.shelf::-webkit-scrollbar-thumb {
  background: rgba(15, 23, 42, 0.18);
  border-radius: 10px;
}

.book-wrap {
  flex: 0 0 auto;
  width: 220px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  scroll-snap-align: start;
  cursor: pointer;
}

.book {
  width: 100%;
  height: 290px;
  background: #fff;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 18px;
  display: flex;
  align-items: center;
  justify-content: center;
  text-align: center;
  cursor: pointer;
  position: relative;
  text-decoration: none;
}

.book.has-cover {
  padding: 0;
}

.book-cover {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
}

.book-gradient {
  position: absolute;
  inset: 0;
  background: linear-gradient(180deg, rgba(0, 0, 0, 0.05) 0%, rgba(0, 0, 0, 0.45) 55%, rgba(0, 0, 0, 0.7) 100%);
}

.book:hover {
  border-color: color-mix(in srgb, var(--accent) 30%, var(--line));
}
.book-wrap:hover .book {
  border-color: color-mix(in srgb, var(--accent) 30%, var(--line));
}
.book:active {
  transform: translateY(1px);
}
.book-wrap:active .book {
  transform: translateY(1px);
}
.book-title {
  font-size: 18px;
  line-height: 1.2;
  font-weight: 900;
  letter-spacing: -0.01em;
  overflow: hidden;
  display: -webkit-box;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 3;
  line-clamp: 3;
}

/* When there's a cover, title is rendered below the card: .book-wrap > .book-title */
.book-wrap > .book-title {
  text-align: left;
  font-size: 13px;
  letter-spacing: 0.01em;
  color: var(--ink);
}

.book-meta {
  margin-top: 8px;
  text-align: center;
  font-size: 12px;
  font-weight: 800;
  color: var(--muted);
  letter-spacing: 0.02em;
}

.book-actions {
  margin-top: 6px;
  display: flex;
  justify-content: center;
}

.book-list-btn,
#btn-mylist {
  appearance: none;
  border: 1px solid var(--line);
  background: #fff;
  color: var(--ink);
  padding: 6px 10px;
  font-size: 12px;
  font-weight: 800;
  letter-spacing: 0.02em;
  border-radius: var(--btn-radius);
  cursor: pointer;
}

.book-list-btn:hover,
#btn-mylist:hover {
  border-color: color-mix(in srgb, var(--accent) 35%, var(--line));
  color: var(--accent);
}

.book-list-btn.saved,
#btn-mylist.saved {
  border-color: var(--accent);
  color: var(--accent);
}

.book-list-btn:disabled,
#btn-mylist:disabled {
  opacity: 0.6;
  cursor: not-allowed;
}

/* Filters */
.filters {
  display: flex;
  gap: 10px;
  align-items: end;
  flex-wrap: wrap;
  margin: 2px 0 10px;
}
.filter {
  display: flex;
  flex-direction: column;
  gap: 6px;
}
.filter label {
  font-size: 12px;
  color: var(--muted);
}
.select {
  min-width: 180px;
  max-width: 220px;
  border: 1px solid var(--line);
  border-radius: var(--btn-radius);
  padding: 10px 12px;
  background: #fff;
  font-weight: 700;
  color: var(--ink);
}

/* Story page */
.story {
  background: #fff;
  border: 1px solid var(--line);
  padding: 18px;
}

.story-layout {
  display: flex;
  gap: 18px;
  align-items: flex-start;
}

.story-cover-wrap {
  flex: 0 0 180px;
}

.story-cover {
  display: block;
  width: 180px;
  aspect-ratio: 3 / 4;
  object-fit: cover;
  background: transparent;
  border: none;
  box-shadow: none;
  filter: none;
}

.story-main {
  flex: 1 1 auto;
  min-width: 0;
}
.story-title {
  margin: 0 0 6px;
  font-size: 28px;
  line-height: 1.12;
  letter-spacing: -0.02em;
}
.story-author {
  margin: 0 0 14px;
  color: var(--muted);
  font-size: 13px;
}
.chips {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin: 0 0 14px;
}
.chip {
  border: 1px solid var(--line);
  background: transparent;
  color: var(--ink);
  padding: 4px 10px;
  border-radius: var(--btn-radius);
  font-size: 12px;
  font-weight: 700;
}
.story-desc {
  margin: 0 0 18px;
  font-size: 14px;
  line-height: 1.55;
}
.story-actions {
  display: flex;
  gap: 10px;
  justify-content: flex-end;
  flex-wrap: wrap;
}
.story-actions-below {
  margin-top: 10px;
}

/* Auth pages */
.auth-card {
  background: #fff;
  border: 1px solid var(--line);
  padding: 18px;
  width: 100%;
  max-width: 440px;
}
.auth-layout {
  display: flex;
  justify-content: center;
}
.auth-title {
  margin: 0 0 6px;
  font-size: 22px;
  font-weight: 900;
  letter-spacing: -0.02em;
}
.auth-sub {
  margin: 0 0 16px;
  color: var(--muted);
  font-size: 12px;
}
.auth-form {
  display: flex;
  flex-direction: column;
  gap: 12px;
}
.auth-label {
  display: flex;
  flex-direction: column;
  gap: 6px;
  font-size: 12px;
  color: var(--muted);
}
.auth-input {
  border: 1px solid var(--line);
  border-radius: var(--btn-radius);
  padding: 10px 12px;
  font-size: 14px;
  color: var(--ink);
  background: #fff;
}
.auth-actions {
  display: flex;
  gap: 10px;
  justify-content: flex-end;
  flex-wrap: wrap;
}
.btn.block {
  width: 100%;
}
.auth-divider {
  margin: 14px 0;
  text-align: center;
  color: var(--muted);
  font-size: 11px;
  font-weight: 800;
  letter-spacing: 0.08em;
}
.auth-help {
  margin: 0;
  font-size: 12px;
  color: var(--muted);
  line-height: 1.45;
}
.auth-link {
  color: var(--accent);
  text-decoration: none;
  font-weight: 800;
}
.auth-link:hover {
  text-decoration: underline;
}
.auth-msg {
  min-height: 18px;
  font-size: 13px;
  color: var(--muted);
}

.legal-row {
  display: flex;
  gap: 10px;
  align-items: flex-start;
  font-size: 12px;
  color: var(--muted);
  line-height: 1.35;
}
.legal-row input[type='checkbox'] {
  margin-top: 2px;
}
.legal-row a {
  color: var(--accent);
  text-decoration: none;
  font-weight: 800;
}
.legal-row a:hover {
  text-decoration: underline;
}

.legal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.55);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
  z-index: 9999;
}
.legal-modal {
  background: #fff;
  border: 1px solid var(--line);
  width: min(520px, 92vw);
  padding: 18px;
  box-shadow: 0 18px 70px rgba(0, 0, 0, 0.25);
}
.legal-title {
  margin: 0 0 8px;
  font-size: 18px;
  font-weight: 900;
  letter-spacing: -0.02em;
}
.legal-text {
  margin: 0 0 12px;
  font-size: 13px;
  color: var(--muted);
  line-height: 1.45;
}
.legal-actions {
  display: flex;
  gap: 10px;
  justify-content: flex-end;
  margin-top: 14px;
}

/* Profile */
.profile-grid {
  display: grid;
  grid-template-columns: 1fr;
  gap: 14px;
  margin-top: 10px;
}

.profile-card {
  border: 1px solid var(--line);
  background: var(--paper);
  padding: 14px;
}

.profile-list {
  margin-top: 8px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.profile-empty {
  color: var(--muted);
  font-size: 13px;
  padding: 6px 0;
}

.profile-row {
  border-top: 1px solid var(--line);
  padding-top: 10px;
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 12px;
}

.profile-row:first-child {
  border-top: none;
  padding-top: 0;
}

.profile-row-main {
  min-width: 0;
}

.profile-title {
  font-weight: 900;
  text-decoration: none;
  display: inline-block;
  max-width: 100%;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.profile-title:hover {
  text-decoration: underline;
}

.profile-meta {
  font-size: 12px;
  color: var(--muted);
  margin-top: 2px;
}

.profile-actions {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
  justify-content: flex-end;
  flex: 0 0 auto;
}

.btn.small {
  padding: 8px 10px;
  font-size: 11px;
  letter-spacing: 0.08em;
}

.btn.ghost {
  background: transparent;
}

.rating-pill {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  border: 1px solid var(--line);
  padding: 6px 8px;
  font-size: 12px;
  font-weight: 800;
  letter-spacing: 0.04em;
  background: #fff;
}

@media (min-width: 980px) {
  .profile-grid {
    grid-template-columns: 1fr 1fr;
  }
  .profile-card[aria-label='My ratings'] {
    grid-column: 1 / -1;
  }
}

@media (max-width: 520px) {
  .profile-row {
    flex-direction: column;
    align-items: stretch;
  }
  .profile-actions {
    justify-content: flex-start;
  }
}

@media (max-width: 520px) {
  .book-wrap {
    width: 190px;
  }
  .book {
    height: 250px;
  }
  .story-title {
    font-size: 22px;
  }
  .select {
    min-width: 150px;
  }

  .story-layout {
    flex-direction: column;
  }
  .story-cover-wrap {
    flex: 0 0 auto;
  }
  .story-cover {
    width: 100%;
    max-width: 220px;
  }
}
