:root {
  --bg: #131319;
  --panel: #1C1D25;
  --panel-hover: #23242E;
  --border: #2E303C;
  --text: #EDEDF2;
  --text-muted: #93949E;
  --accent: #D6A24E;
  --card-min-width: 140px;
  --card-font-scale: 1;
  --grid-columns: 3;
  --error: #E05A5A;
  --error-bg: rgba(224, 90, 90, 0.14);
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
}

.topbar {
  padding: 24px 16px 16px;
  position: sticky;
  top: 0;
  background: var(--bg);
  z-index: 5;
  border-bottom: 1px solid var(--border);
}

.topbar h1 {
  margin: 0 0 14px;
  font-family: Georgia, "Times New Roman", serif;
  font-weight: 600;
  font-size: 1.15rem;
  letter-spacing: 0.02em;
}

.controls {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.controls-row {
  display: flex;
  gap: 10px;
  flex-wrap: nowrap;
}

.controls-row-filters .filter-group {
  flex: 1 1 0;
  min-width: 0;
}

.search-wrapper {
  position: relative;
  flex: 1 1 240px;
  min-width: 0;
}

#search {
  background: var(--panel);
  border: 1px solid var(--border);
  color: var(--text);
  padding: 10px 34px 10px 12px;
  border-radius: 6px;
  font-size: 0.95rem;
  width: 100%;
}

#search:focus {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
}

#search::-webkit-search-cancel-button {
  -webkit-appearance: none;
  appearance: none;
}

.search-clear-btn {
  position: absolute;
  top: 50%;
  right: 6px;
  transform: translateY(-50%);
  background: none;
  border: none;
  color: var(--text-muted);
  font-size: 1.3rem;
  line-height: 1;
  cursor: pointer;
  padding: 4px 6px;
}

.search-clear-btn:hover {
  color: var(--text);
}

.search-clear-btn[hidden] {
  display: none;
}

.sort-group {
  flex-shrink: 0;
  min-width: 60px;
}

.sort-group summary {
  text-align: center;
}

.sort-option {
  padding: 8px 10px;
  font-size: 0.85rem;
  cursor: pointer;
  border-radius: 4px;
}

.sort-option:hover {
  background: var(--panel-hover);
}

.sort-option.active {
  color: var(--accent);
  font-weight: 600;
}

.icon-btn {
  background: var(--panel);
  border: 1px solid var(--border);
  color: var(--text);
  width: 40px;
  padding: 10px 0;
  border-radius: 6px;
  font-size: 1rem;
  cursor: pointer;
  flex-shrink: 0;
}

.icon-btn:hover {
  background: var(--panel-hover);
}

.scroll-top-btn {
  position: fixed;
  bottom: 20px;
  right: 20px;
  width: 44px;
  height: 44px;
  border-radius: 50%;
  background: var(--accent);
  color: var(--bg);
  border: none;
  font-size: 1.2rem;
  font-weight: 700;
  cursor: pointer;
  box-shadow: 0 2px 10px rgba(0,0,0,0.4);
  z-index: 4;
}

.scroll-top-btn[hidden] {
  display: none;
}

.filter-group {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 0;
  position: relative;
}

.filter-group summary {
  padding: 10px 12px;
  cursor: pointer;
  font-size: 0.95rem;
  list-style: none;
  user-select: none;
}

.filter-group summary::-webkit-details-marker {
  display: none;
}

.filter-group summary::after {
  content: "▾";
  float: right;
  color: var(--text-muted);
}

.filter-group[open] summary::after {
  content: "▴";
}

.checkbox-list {
  position: absolute;
  top: calc(100% + 4px);
  left: 0;
  min-width: 220px;
  max-height: 280px;
  overflow-y: auto;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 8px;
  z-index: 6;
}

.checkbox-item {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px 4px;
  font-size: 0.85rem;
  cursor: pointer;
  border-radius: 4px;
}

.checkbox-item:hover {
  background: var(--panel-hover);
}

.checkbox-item input {
  accent-color: var(--accent);
  width: 16px;
  height: 16px;
  flex-shrink: 0;
}

.suggestion-item {
  padding: 8px 10px;
  font-size: 0.85rem;
  cursor: pointer;
  border-radius: 4px;
}

.suggestion-item:hover {
  background: var(--panel-hover);
}

.show-all-row {
  display: flex;
  gap: 6px;
  border-bottom: 1px solid var(--border);
  margin-bottom: 4px;
  padding-bottom: 6px;
}

.show-all-btn {
  flex: 1;
  text-align: center;
  background: none;
  border: 1px solid var(--border);
  border-radius: 4px;
  color: var(--accent);
  padding: 5px 4px;
  font-size: 0.75rem;
  cursor: pointer;
}

.show-all-btn:hover {
  color: var(--text);
  background: var(--panel-hover);
}

.count {
  font-family: "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  font-size: 0.8rem;
  font-weight: 400;
  color: var(--text-muted);
}

.grid {
  display: grid;
  grid-template-columns: repeat(var(--grid-columns), 1fr);
  gap: 14px;
  padding: 16px;
  touch-action: pan-y;
}

.grid[hidden] {
  display: none;
}

.card {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 8px;
  overflow: hidden;
  cursor: pointer;
  position: relative;
  transition: transform 0.12s ease, background 0.12s ease;
  display: flex;
  flex-direction: column;
}

.card:hover, .card:focus-visible {
  background: var(--panel-hover);
  transform: translateY(-2px);
}

.card--error {
  background: var(--error-bg);
  border-color: var(--error);
}

.card--error:hover, .card--error:focus-visible {
  background: rgba(224, 90, 90, 0.22);
}

.card-error-flag {
  position: absolute;
  top: 4px;
  right: 4px;
  width: 18px;
  height: 18px;
  border-radius: 50%;
  background: var(--error);
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 0.7rem;
  font-weight: 700;
  z-index: 2;
  box-shadow: 0 0 0 2px rgba(0,0,0,0.4);
}

.badge {
  display: inline-block;
  font-size: 0.65em;
  font-weight: 600;
  color: var(--accent);
  border: 1px solid var(--accent);
  border-radius: 4px;
  padding: 0 4px;
  vertical-align: middle;
}

.view-toggle-btn {
  background: var(--panel);
  border: 1px solid var(--border);
  color: var(--text);
  padding: 10px 14px;
  border-radius: 6px;
  font-size: 0.85rem;
  cursor: pointer;
  flex-shrink: 0;
  white-space: nowrap;
}

.view-toggle-btn:hover {
  background: var(--panel-hover);
}

.list-view {
  padding: 8px 16px;
}

.list-row {
  padding: 12px 8px;
  border-bottom: 1px solid var(--border);
  cursor: pointer;
  font-size: 0.9rem;
}

.list-row:hover, .list-row:focus-visible {
  background: var(--panel-hover);
}

.list-row--error .list-row-title {
  color: var(--error);
}

.list-row-flag {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 16px;
  height: 16px;
  border-radius: 50%;
  background: var(--error);
  color: #fff;
  font-size: 0.65rem;
  font-weight: 700;
  margin-right: 8px;
  flex-shrink: 0;
}

.detail-section--error h3,
.detail-section--error p {
  color: var(--error);
}

.passphrase-card {
  max-width: 360px;
  text-align: center;
}

.passphrase-title {
  font-family: Georgia, "Times New Roman", serif;
  font-size: 1.2rem;
  margin: 0 0 10px;
}

.passphrase-hint {
  color: var(--text-muted);
  font-size: 0.85rem;
  margin: 0 0 18px;
}

#passphrase-input {
  width: 100%;
  background: var(--panel-hover);
  border: 1px solid var(--border);
  color: var(--text);
  padding: 10px 12px;
  border-radius: 6px;
  font-size: 0.95rem;
  margin-bottom: 12px;
}

#passphrase-input:focus {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
}

.passphrase-submit-btn {
  width: 100%;
}

.passphrase-error {
  color: var(--error);
  font-size: 0.85rem;
  margin: 12px 0 0;
}

@media (max-width: 600px) {
  .checkbox-list {
    position: fixed;
    top: var(--topbar-height, 140px);
    left: 16px;
    right: 16px;
    width: auto;
    max-width: none;
    min-width: 0;
    max-height: 60vh;
    overflow-y: auto;
  }
}

@media (max-width: 480px) {
  .controls-row {
    gap: 6px;
  }

  .search-wrapper {
    flex: 1 1 auto;
    min-width: 50px;
  }

  #search {
    padding: 8px 28px 8px 8px;
    font-size: 0.8rem;
  }

  .sort-group {
    min-width: 44px;
  }

  .sort-group summary {
    padding: 8px 4px;
    font-size: 0.7rem;
  }

  .view-toggle-btn {
    padding: 8px 8px;
    font-size: 0.75rem;
  }

  .icon-btn {
    width: 32px;
    padding: 8px 0;
    font-size: 0.8rem;
  }

  .controls-row-filters {
    gap: 6px;
  }

  .filter-group summary {
    padding: 8px 6px;
    font-size: 0.75rem;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }
}

.card img {
  width: 100%;
  aspect-ratio: 2 / 3;
  object-fit: cover;
  background: var(--border);
  display: block;
}

.card .no-poster {
  width: 100%;
  aspect-ratio: 2 / 3;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
  color: var(--text-muted);
  font-size: calc(0.75rem * var(--card-font-scale));
  text-align: center;
  padding: 8px;
}

.card-body {
  padding: 8px 10px 10px;
}

.grid.hide-meta .card-meta {
  display: none;
}

.grid.hide-title .card-title,
.grid.hide-title .card-meta {
  display: none;
}

.grid.hide-title .card-body {
  padding: 0;
}

.card-title {
  font-size: calc(0.85rem * var(--card-font-scale));
  font-weight: 600;
  line-height: 1.25;
  margin: 0 0 4px;
}

.card-meta {
  font-size: calc(0.75rem * var(--card-font-scale));
  color: var(--text-muted);
  display: flex;
  justify-content: space-between;
}

.empty-state {
  text-align: center;
  color: var(--text-muted);
  padding: 60px 16px;
}

.overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.7);
  display: flex;
  align-items: flex-start;
  justify-content: center;
  padding: 24px 16px;
  overflow-y: auto;
  z-index: 10;
}

.overlay[hidden] {
  display: none;
}

.detail-card {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 10px;
  max-width: 640px;
  width: 100%;
  position: relative;
  padding: 24px;
}

.close-btn {
  position: absolute;
  top: 8px;
  right: 8px;
  background: none;
  border: none;
  color: var(--text-muted);
  font-size: 3.2rem;
  line-height: 1;
  cursor: pointer;
  padding: 0;
  z-index: 3;
}

.close-btn:hover { color: var(--text); }

.detail-header {
  margin-bottom: 16px;
}

.detail-visual-row {
  display: flex;
  flex-wrap: nowrap;
  align-items: stretch;
  gap: 12px;
  margin-bottom: 16px;
}

.detail-actions {
  flex: 1 1 0;
  min-width: 90px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 12px;
}

.detail-random-btn {
  width: 84px;
  height: 84px;
  padding: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 2.1rem;
  line-height: 1;
  border-radius: 50%;
}

.detail-back-btn {
  width: 48px;
  height: 48px;
  padding: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1.2rem;
  line-height: 1;
  border-radius: 50%;
}

.detail-info {
  min-width: 0;
}

.detail-header img {
  height: 320px;
  width: auto;
  max-width: min(75%, calc(100% - 102px));
  object-fit: contain;
  border-radius: 6px;
  flex-shrink: 1;
  cursor: zoom-in;
}

.detail-no-poster {
  height: 320px;
  width: 213px;
  max-width: calc(100% - 102px);
  flex-shrink: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--panel-hover);
  border: 1px solid var(--border);
  border-radius: 6px;
  color: var(--text-muted);
  font-size: 0.8rem;
  text-align: center;
  padding: 8px;
}

.image-fullscreen {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.92);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
  cursor: zoom-out;
  z-index: 20;
}

.image-fullscreen[hidden] {
  display: none;
}

.image-fullscreen img {
  max-width: 100%;
  max-height: 100%;
  object-fit: contain;
  border-radius: 6px;
}

.detail-title {
  font-family: Georgia, "Times New Roman", serif;
  font-size: 1.15rem;
  margin: 0 0 6px;
}

.detail-sub {
  color: var(--text-muted);
  font-size: 0.85rem;
  margin: 0 0 10px;
}

.ratings {
  display: flex;
  gap: 14px;
  font-size: 0.85rem;
  margin-bottom: 8px;
}

.ratings span strong {
  color: var(--accent);
}

.genre-tags {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-top: 8px;
}

.genre-tag {
  font-size: 0.7rem;
  border: 1px solid var(--border);
  border-radius: 4px;
  padding: 2px 8px;
  color: var(--text-muted);
}

.detail-section {
  margin-top: 14px;
}

.detail-section h3 {
  font-size: 0.8rem;
  text-transform: none;
  color: var(--text-muted);
  margin: 0 0 4px;
  font-weight: 600;
}

.detail-section p {
  margin: 0;
  line-height: 1.5;
  font-size: 0.85rem;
}

@media (max-width: 480px) {
  .detail-header img {
    height: 240px;
    max-width: min(70%, calc(100% - 82px));
  }
  .detail-no-poster {
    height: 240px;
    width: 160px;
    max-width: calc(100% - 82px);
  }
  .detail-actions {
    min-width: 70px;
    gap: 8px;
  }
  .detail-random-btn {
    width: 64px;
    height: 64px;
    font-size: 1.6rem;
  }
  .detail-back-btn {
    width: 40px;
    height: 40px;
    font-size: 1rem;
  }
}