:root {
  color-scheme: dark;
  --bg: #08090d;
  --panel: #111621;
  --panel-2: #171d2a;
  --line: #273044;
  --text: #f5f5f5;
  --muted: #9aa3b7;
  --red: #e50914;
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; text-size-adjust: 100%; }
body:not(.authenticated) > .sidebar,
body:not(.authenticated) > main { display: none; }
[hidden] { display: none !important; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: Segoe UI, system-ui, sans-serif;
  display: grid;
  grid-template-columns: 230px 1fr;
  min-height: 100vh;
}
.authGate {
  position: fixed;
  inset: 0;
  z-index: 30;
  display: grid;
  place-items: center;
  padding: max(20px, env(safe-area-inset-top)) 18px max(20px, env(safe-area-inset-bottom));
  background: radial-gradient(ellipse at top, #252b3a, var(--bg) 60%);
}
.authCard {
  width: min(420px, 100%);
  padding: 26px;
  border: 1px solid var(--line);
  border-radius: 18px;
  background: var(--panel);
  box-shadow: 0 18px 60px rgba(0,0,0,.35);
}
.authCard .brand { margin: 0 0 12px; color: var(--red); }
.authCard h1 { font-size: 28px; margin: 0 0 8px; }
.authCard p { color: var(--muted); line-height: 1.5; }
.authCard label { color: var(--muted); display: block; margin: 18px 0 7px; }
.authCard button { width: 100%; margin-top: 14px; min-height: 48px; }
.authError { color: #ff9299; min-height: 1.4em; margin-top: 8px; }
.profileCard { width: min(560px, 100%); text-align: center; }
.profileCard #profilePinForm { text-align: left; }
.profilesGrid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(105px, 1fr));
  gap: 14px;
  margin: 22px 0 10px;
}
.otherProfilesGrid { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 14px; margin-top: 20px; }
.otherProfileChoice {
  min-height: 150px; display: grid; justify-items: center; align-content: center; gap: 9px;
  padding: 16px; border: 1px solid var(--line); border-radius: 12px; background: var(--panel); color: var(--text);
}
.otherProfileChoice:hover { border-color: var(--red); }
.readOnlyCard { cursor: default; }
#otherProfile h2 { margin-top: 28px; }
.profileCollectionFilters { display: grid; grid-template-columns: repeat(auto-fit, minmax(175px, 1fr)); gap: 10px; margin: 18px 0; padding: 14px; border: 1px solid var(--line); border-radius: 8px; background: var(--panel); }
.profileCollectionFilters label { display: grid; gap: 5px; margin: 0; color: var(--muted); font-size: 13px; }
.profileChoice {
  min-height: 112px;
  display: grid;
  place-items: center;
  align-content: center;
  gap: 10px;
  background: transparent;
  border: 1px solid var(--line);
}
.profileAvatar {
  width: 58px;
  height: 58px;
  display: grid;
  place-items: center;
  border-radius: 14px;
  background: linear-gradient(145deg, #b50b17, #5d1621);
  color: #fff;
  font-size: 23px;
  font-weight: 900;
}
@media (hover: hover) and (pointer: fine) {
  .profileChoice:hover { border-color: var(--red); transform: translateY(-2px); }
}
#profilePinForm button { width: 100%; margin-top: 10px; min-height: 46px; }
.managedProfiles { display: grid; gap: 8px; margin: 14px 0 20px; }
.managedProfile {
  display: grid;
  grid-template-columns: 1fr auto auto;
  align-items: center;
  gap: 8px;
  padding: 9px;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 8px;
}
.profileCreateFields { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
#pinEditPanel { padding: 14px; border: 1px solid var(--line); border-radius: 8px; margin: 14px 0; }

.sidebar {
  border-right: 1px solid var(--line);
  background: #0d0f16;
  padding: 20px 12px;
  position: sticky;
  top: 0;
  height: 100vh;
}
.brand { font-size: 28px; font-weight: 900; margin: 0 8px 22px; }
.brand { display: flex; align-items: center; gap: 9px; color: var(--red); }
.brandIcon { width: 32px; height: 32px; flex: 0 0 32px; fill: none; stroke: currentColor; stroke-width: 2.8; stroke-linecap: round; stroke-linejoin: round; }
.iconSprite { position: absolute; width: 0; height: 0; overflow: hidden; }
.sidebar button {
  width: 100%;
  display: flex;
  align-items: center;
  gap: 12px;
  text-align: left;
  margin: 4px 0;
  padding: 13px 14px;
  border: 0;
  border-radius: 8px;
  background: transparent;
  color: #cdd2e0;
  font-weight: 700;
}
.navIcon { width: 21px; height: 21px; flex: 0 0 21px; fill: none; stroke: currentColor; stroke-width: 2.5; stroke-linecap: round; stroke-linejoin: round; }
.sidebar button.active, .primary { background: var(--red); color: #fff; }

main { padding: 22px; min-width: 0; }
.topbar {
  display: flex;
  gap: 10px;
  margin-bottom: 20px;
}
.quiet { flex: 0 0 auto; }
input, select, textarea, button {
  font: inherit;
}
input, select, textarea {
  background: #121620;
  color: var(--text);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 10px 12px;
  width: 100%;
}
textarea { min-height: 90px; resize: vertical; }
button {
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 10px 14px;
  background: var(--panel-2);
  color: var(--text);
  cursor: pointer;
  font-weight: 700;
  -webkit-appearance: none;
  appearance: none;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
}
.danger { background: #58151b; border-color: #842029; color: #ffd5d8; }
.view { display: none; }
.view.active { display: block; }
h1 { margin: 0 0 18px; font-size: clamp(28px, 5vw, 46px); }
h2 { margin: 24px 0 14px; }

.hero {
  min-height: 190px;
  display: flex;
  align-items: flex-end;
  padding: 24px;
  border-radius: 8px;
  border: 1px solid var(--line);
  background:
    linear-gradient(135deg, rgba(229,9,20,.35), transparent 55%),
    linear-gradient(160deg, #1b2030, #0d1018);
}
.hero p { color: var(--muted); max-width: 620px; }
.stats {
  display: grid;
  grid-template-columns: repeat(6, minmax(0, 1fr));
  gap: 12px;
  margin-top: 16px;
}
.stat {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 16px;
}
.stat strong { display: block; font-size: 28px; }
.stat span { color: var(--muted); }
.grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
  gap: 16px;
}
.card {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 8px;
  overflow: hidden;
  min-width: 0;
  cursor: pointer;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
}
.poster {
  aspect-ratio: 2 / 3;
  background: #202638;
  display: grid;
  place-items: center;
  color: var(--muted);
  font-weight: 800;
}
.poster img { width: 100%; height: 100%; object-fit: cover; display: block; }
.bookCover { display: block; width: 100%; height: 100%; object-fit: cover; }
.posterDownload {
  margin: 12px;
  min-height: 44px;
  background: var(--panel-2);
  color: var(--text);
  border-color: #49546d;
}
.posterDownload:disabled { opacity: .7; cursor: wait; }
.cardBody { padding: 10px; }
.title { font-weight: 800; line-height: 1.2; }
.meta { color: var(--muted); font-size: 12px; margin-top: 5px; }
.recommendationPrivacy { max-width: 760px; font-size: 14px; line-height: 1.5; }
.recommendationShare {
  max-width: 780px;
  margin: 28px 0;
  padding: 18px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--panel);
}
.recommendationShare h2 { margin-top: 0; }
.recommendationFields { display: grid; grid-template-columns: minmax(140px, 220px) 1fr; gap: 14px; align-items: start; }
.recommendationFields textarea { min-height: 100px; resize: vertical; }
.recommendationChoices { display: grid; gap: 6px; max-width: 780px; max-height: 320px; overflow-y: auto; margin: -4px 0 14px; }
.recommendationChoice { width: 100%; display: flex; justify-content: space-between; align-items: center; gap: 12px; text-align: left; }
.recommendationChoice span { color: var(--muted); font-size: 12px; white-space: nowrap; }
.recommendationSelected { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin: 8px 0 14px; padding: 10px 12px; border: 1px solid var(--line); border-radius: 8px; background: var(--panel-2); }
.recommendationSelected strong { overflow-wrap: anywhere; }
.recommendationGrid { display: grid; gap: 14px; }
.recommendationCard {
  display: grid;
  grid-template-columns: 110px minmax(0, 1fr);
  gap: 16px;
  max-width: 900px;
  padding: 14px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--panel);
}
.recommendationPoster { aspect-ratio: 2 / 3; overflow: hidden; border-radius: 7px; background: #202638; }
.recommendationPoster img { width: 100%; height: 100%; object-fit: cover; display: block; }
.recommendationPosterFallback { height: 100%; padding: 8px; display: grid; place-items: center; text-align: center; color: var(--muted); font-size: 12px; }
.recommendationPosterDownload { width: calc(100% - 12px); min-height: 70px; margin: 6px; padding: 6px; font-size: 11px; }
.recommendationContent { min-width: 0; align-self: center; }
.recommendationContent .title { font-size: 19px; }
.recommendationFrom { margin-top: 10px; color: var(--muted); font-size: 13px; }
.recommendationFrom strong { color: var(--text); }
.recommendationRating { margin-top: 10px; color: #ffd166; font-weight: 800; }
.recommendationComment { line-height: 1.5; overflow-wrap: anywhere; }
.recommendationContent .addRecommendedMedia { margin-top: 10px; min-height: 44px; }
.recommendationAdded { margin-top: 10px; color: #79d9a5; font-weight: 700; font-size: 13px; }
.unshareRecommendation { margin-top: 8px; }
.recommendationEmpty { padding: 12px 0; }
.progress {
  height: 6px;
  background: #2a3144;
  border-radius: 20px;
  margin-top: 10px;
  overflow: hidden;
}
.progress div { height: 100%; background: var(--red); }
.searchline { display: flex; gap: 10px; margin-bottom: 16px; }
.rawgAddActions { display: flex; flex-wrap: wrap; gap: 8px; }
.rawgAddActions button { flex: 1 1 190px; }
.collectionSearch { max-width: 520px; margin: 0 0 16px; }
.collectionSearch input { width: 100%; }
.statusTabs { display: grid; grid-auto-columns: minmax(0, 1fr); grid-auto-flow: column; margin: 0 0 18px; border-bottom: 1px solid var(--line); }
.statusTab { min-height: 52px; padding: 10px 8px; border: 0; border-bottom: 3px solid transparent; border-radius: 0; background: transparent; color: var(--muted); }
.statusTab.active { border-bottom-color: var(--red); color: var(--text); background: transparent; }
.opinionFilter { display: flex; align-items: center; gap: 10px; max-width: 360px; margin: 0 0 12px; }
.opinionFilter select { flex: 1; }
.opinionBadge { display: inline-block; margin-top: 7px; padding: 3px 8px; border: 1px solid var(--line); border-radius: 999px; color: var(--text); font-size: 12px; }
.mediaCategoryBadge { display: inline-block; margin-top: 6px; padding: 3px 8px; border: 1px solid var(--line); border-radius: 999px; color: #c7d3ed; font-size: 12px; }
.tmdbFilters { margin: -4px 0 16px; padding: 12px 14px; border: 1px solid var(--line); border-radius: 8px; background: var(--panel); }
.tmdbFilters summary { cursor: pointer; font-weight: 700; }
.tmdbFilterGrid { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 10px; margin-top: 12px; }
.tmdbFilterGrid label { display: grid; gap: 5px; color: var(--muted); font-size: 13px; }
.tmdbFilterGrid input, .tmdbFilterGrid select { min-width: 0; width: 100%; }
.catalogCredit { color: var(--muted); font-size: 13px; margin: -6px 0 16px; }
.catalogCredit a { color: var(--text); }
.gamePlatformFilter { max-width: 320px; }
.platformChoices { display: flex; flex-wrap: wrap; gap: 2px 16px; padding: 4px 0 8px; }
.platformChoice { margin: 0 0 8px; color: var(--text); }
.gamePlatformsEditor { margin: 12px 0; padding: 12px; border: 1px solid var(--line); border-radius: 8px; }
.gameOwnedPlatforms { color: #aeb8ce; }
#gameAddDialog { width: min(520px, calc(100vw - 22px)); }
#gameAddDialog .dialogActions { margin-top: 12px; }
#gameAddError { margin: 8px 0; }
.result {
  padding: 14px;
  border: 1px solid var(--line);
  background: var(--panel);
  border-radius: 8px;
  margin-bottom: 10px;
}
.result p { color: var(--muted); }
.searchResult { display: grid; grid-template-columns: 86px minmax(0, 1fr); gap: 14px; align-items: start; }
.searchPoster { width: 86px; aspect-ratio: 2 / 3; overflow: hidden; border-radius: 6px; background: var(--panel-2); }
.searchPosterImage { display: block; width: 100%; height: 100%; object-fit: cover; }
.searchPosterMissing { height: 100%; display: grid; place-items: center; padding: 6px; color: var(--muted); text-align: center; font-size: 11px; }
.searchResultBody { min-width: 0; }
.searchResultBody > strong { display: block; line-height: 1.35; }
.searchResultBody p { display: -webkit-box; overflow: hidden; -webkit-box-orient: vertical; -webkit-line-clamp: 3; line-height: 1.4; }
.searchResultBody > button, .searchResultBody .rawgAddActions { margin-top: 8px; }
.mediaPreviewDialog { width: min(900px, calc(100vw - 24px)); }
.mediaPreviewDialog > .dialogHeader { padding: 12px 18px; border-bottom: 1px solid var(--line); }
.mediaPreviewDialog > .dialogHeader h2 { margin: 0; }
.mediaPreviewBody { max-height: min(68vh, 680px); overflow-y: auto; }
.mediaPreviewDialog > .mediaPreviewBody { padding: 18px; }
.mediaPreviewLayout { display: grid; grid-template-columns: minmax(150px, 230px) minmax(0, 1fr); gap: 22px; align-items: start; }
.previewPoster { aspect-ratio: 2 / 3; overflow: hidden; border-radius: 8px; background: var(--panel-2); }
.previewPosterImage { display: block; width: 100%; height: 100%; object-fit: cover; }
.previewInfo { min-width: 0; }
.previewSynopsis { white-space: pre-line; line-height: 1.55; }
.previewFacts { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 10px 16px; margin: 18px 0; }
.previewFact { display: grid; gap: 3px; min-width: 0; }
.previewFact strong { color: var(--muted); font-size: 12px; }
.previewLinks { display: flex; flex-wrap: wrap; gap: 14px; margin: 16px 0 4px; }
.previewLinks a { color: #a9c8ff; }
.mediaPreviewDialog .dialogActions { display: flex; justify-content: flex-end; flex-wrap: wrap; padding: 12px 18px; border-top: 1px solid var(--line); }
.randomResult {
  margin-top: 18px;
  padding: 22px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--panel);
  font-size: 24px;
  font-weight: 900;
}

dialog {
  width: min(820px, calc(100vw - 22px));
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #0d1018;
  color: var(--text);
  padding: 0;
}
dialog::backdrop { background: rgba(0,0,0,.72); }
#passwordDialog { width: min(480px, calc(100vw - 22px)); }
form { padding: 18px; }
.passwordActions { display: flex; justify-content: flex-end; gap: 10px; margin-top: 12px; }
.dialogHeader, .dialogActions {
  display: grid;
  grid-template-columns: 1fr auto;
  align-items: center;
  gap: 10px;
}
.dialogActions { grid-template-columns: auto 1fr auto auto; margin-top: 16px; }
.formGrid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 12px;
}
label { display: grid; gap: 6px; color: var(--muted); margin-bottom: 12px; }
.checkbox { display: flex; align-items: center; gap: 8px; margin-top: 24px; }
.checkbox input { width: auto; }
.seriesEditor {
  border-top: 1px solid var(--line);
  margin-top: 10px;
  padding-top: 12px;
}
.sectionTitle { font-weight: 900; margin-bottom: 10px; }
.seasonRow {
  display: grid;
  grid-template-columns: 80px 1fr 1fr auto;
  gap: 8px;
  align-items: center;
  margin-bottom: 8px;
}

@media (max-width: 780px) {
  body { display: block; padding-bottom: calc(150px + env(safe-area-inset-bottom)); }
  .sidebar {
    position: fixed;
    z-index: 10;
    bottom: 0;
    top: auto;
    height: auto;
    width: 100%;
    display: grid;
    grid-template-columns: repeat(4, minmax(0, 1fr));
    grid-auto-rows: 40px;
    gap: 4px;
    padding: 7px 8px max(7px, env(safe-area-inset-bottom));
    border-right: 0;
    border-top: 1px solid var(--line);
  }
  .brand { display: none; }
  .sidebar button { text-align: center; font-size: 11px; padding: 6px 2px; margin: 0; }
  .sidebar { overflow-y: auto; }
  .sidebar button { flex-direction: column; justify-content: center; gap: 2px; }
  .navIcon { width: 18px; height: 18px; flex-basis: 18px; }
  main { padding: max(14px, env(safe-area-inset-top)) 14px 14px; }
  /* Keep touch controls in normal flow. Safari can leave a sticky header layer
     over content after scrolling or opening a dialog. */
  .topbar { position: static; z-index: auto; background: var(--bg); padding-bottom: 10px; }
  .topbar { flex-wrap: wrap; }
  #search { flex: 1 1 100%; }
  .topbar button { flex: 1 1 auto; min-width: 0; }
  #search { min-width: 0; }
  #logout { padding-inline: 9px; }
  #profileSwitch, #manageProfiles { padding-inline: 9px; }
  #profileSwitch { max-width: 130px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
  .profileCreateFields { grid-template-columns: 1fr; gap: 0; }
  .recommendationFields { grid-template-columns: 1fr; gap: 0; }
  .recommendationShare { padding: 14px; }
  .recommendationCard { grid-template-columns: 82px minmax(0, 1fr); gap: 11px; padding: 10px; }
  .recommendationContent .title { font-size: 16px; }
  .managedProfile { grid-template-columns: 1fr 1fr; }
  .managedProfile strong { grid-column: 1 / -1; }
  .stats { grid-template-columns: repeat(2, 1fr); }
  .grid { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 12px; }
  .mediaPreviewLayout { grid-template-columns: 100px minmax(0, 1fr); gap: 13px; }
  .previewFacts { grid-template-columns: 1fr; gap: 8px; }
  .formGrid { grid-template-columns: 1fr; }
  .seasonRow { grid-template-columns: 1fr 1fr; }
  dialog {
    max-height: calc(100vh - 24px);
    max-height: calc(100dvh - 24px);
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    overscroll-behavior: contain;
  }
  .dialogActions { grid-template-columns: 1fr 1fr; }
  .dialogActions span { display: none; }
  .dialogActions button { min-height: 44px; }
}
