*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --bg: #f6f3ee;
  --bg-card: #fff;
  --bg-elevated: #ede9e1;
  --text: #1a1814;
  --text-secondary: #6b6560;
  --text-tertiary: #9e9790;
  --accent: #c45d3e;
  --accent-soft: #f0d8cf;
  --border: #e2ded6;
  --font-display: 'DM Serif Display', Georgia, serif;
  --font-body: 'Instrument Sans', -apple-system, BlinkMacSystemFont, sans-serif;
  --radius: 14px;
  --radius-sm: 10px;
  --shadow: 0 1px 3px rgba(26,24,20,0.04), 0 6px 16px rgba(26,24,20,0.06);
  --shadow-lg: 0 4px 12px rgba(26,24,20,0.06), 0 16px 40px rgba(26,24,20,0.1);
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #1a1814;
    --bg-card: #252220;
    --bg-elevated: #2e2a27;
    --text: #f0ece4;
    --text-secondary: #a09a92;
    --text-tertiary: #6b6560;
    --accent: #e07a5f;
    --accent-soft: #3d2a22;
    --border: #36322e;
    --shadow: 0 1px 3px rgba(0,0,0,0.2), 0 6px 16px rgba(0,0,0,0.25);
    --shadow-lg: 0 4px 12px rgba(0,0,0,0.25), 0 16px 40px rgba(0,0,0,0.35);
  }
}

html { height: 100%; }

body {
  font-family: var(--font-body);
  background: var(--bg);
  color: var(--text);
  min-height: 100dvh;
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
  overflow-x: hidden;
}

#app {
  max-width: 540px;
  margin: 0 auto;
  padding: 0 20px;
  padding-bottom: calc(var(--safe-bottom) + 24px);
}

/* ── Header ── */
.header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 20px 0 16px;
  border-bottom: 1px solid var(--border);
  margin-bottom: 20px;
  position: sticky;
  top: 0;
  background: var(--bg);
  z-index: 100;
  padding-top: calc(var(--safe-top) + 20px);
}

.header h1 {
  font-family: var(--font-display);
  font-size: 1.6rem;
  font-weight: 400;
  letter-spacing: -0.01em;
}

.header-subtitle {
  font-size: 0.8rem;
  color: var(--text-tertiary);
  text-transform: uppercase;
  letter-spacing: 0.08em;
  font-weight: 500;
}

.header-right {
  display: flex;
  gap: 8px;
  align-items: center;
}

/* ── Buttons ── */
button {
  font-family: var(--font-body);
  cursor: pointer;
  border: none;
  background: none;
  color: var(--text);
  font-size: 1rem;
}

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 10px 18px;
  border-radius: var(--radius-sm);
  font-weight: 500;
  font-size: 0.9rem;
  transition: all 0.15s ease;
}

.btn-accent {
  background: var(--accent);
  color: #fff;
}

.btn-accent:hover { opacity: 0.9; }

.btn-ghost {
  background: var(--bg-elevated);
  color: var(--text-secondary);
}

.btn-ghost:hover { background: var(--border); }

.btn-icon {
  width: 40px;
  height: 40px;
  padding: 0;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--bg-elevated);
  color: var(--text-secondary);
  transition: all 0.15s ease;
}

.btn-icon:hover {
  background: var(--border);
  color: var(--text);
}

.back-btn {
  width: 36px;
  height: 36px;
  padding: 0;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--text-secondary);
  transition: all 0.15s ease;
}

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

/* ── Search ── */
.search-section {
  margin-bottom: 24px;
}

.search-bar {
  display: flex;
  gap: 8px;
  align-items: center;
}

.search-input {
  flex: 1;
  padding: 12px 16px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--bg-card);
  color: var(--text);
  font-family: var(--font-body);
  font-size: 1rem;
  outline: none;
  transition: border-color 0.15s ease;
}

.search-input:focus {
  border-color: var(--accent);
}

.search-input::placeholder {
  color: var(--text-tertiary);
}

.search-btn {
  width: 48px;
  height: 48px;
  padding: 0;
  border-radius: var(--radius);
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--accent);
  color: #fff;
  flex-shrink: 0;
  transition: opacity 0.15s ease;
}

.search-btn:hover { opacity: 0.9; }

/* ── Toggle ── */
.toggle-bar {
  display: flex;
  gap: 4px;
  background: var(--bg-elevated);
  border-radius: var(--radius-sm);
  padding: 4px;
  margin-bottom: 20px;
}

.toggle-btn {
  flex: 1;
  padding: 8px 12px;
  border-radius: 8px;
  font-size: 0.85rem;
  font-weight: 500;
  color: var(--text-secondary);
  transition: all 0.2s ease;
  text-align: center;
}

.toggle-btn.active {
  background: var(--bg-card);
  color: var(--text);
  box-shadow: var(--shadow);
}

/* ── Results grid ── */
.results-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 14px;
}

.result-card {
  cursor: pointer;
  transition: transform 0.15s ease;
}

.result-card:active {
  transform: scale(0.97);
}

.poster-frame {
  position: relative;
  aspect-ratio: 2/3;
  border-radius: var(--radius);
  overflow: hidden;
  background: var(--bg-elevated);
  box-shadow: var(--shadow);
}

.poster-frame img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
}

.poster-fallback {
  width: 100%;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--text-tertiary);
}

.badge-type {
  position: absolute;
  top: 8px;
  left: 8px;
  background: rgba(0,0,0,0.65);
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
  color: #fff;
  font-size: 0.65rem;
  font-weight: 600;
  padding: 3px 7px;
  border-radius: 6px;
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

.badge-rating {
  position: absolute;
  bottom: 8px;
  left: 8px;
  background: rgba(0,0,0,0.65);
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
  color: #f5c518;
  font-size: 0.75rem;
  font-weight: 600;
  padding: 3px 8px;
  border-radius: 6px;
}

.result-title {
  font-size: 0.85rem;
  font-weight: 500;
  margin-top: 8px;
  line-height: 1.3;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.result-year {
  font-size: 0.75rem;
  color: var(--text-tertiary);
  margin-top: 2px;
}

.result-match {
  font-size: 0.7rem;
  color: var(--accent);
  margin-top: 3px;
  line-height: 1.3;
}

.result-match span {
  color: var(--text-tertiary);
}

/* ── Loading ── */
.loading {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 60px 20px;
  color: var(--text-tertiary);
  gap: 12px;
}

.spinner {
  width: 28px;
  height: 28px;
  border: 3px solid var(--border);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}

@keyframes spin { to { transform: rotate(360deg); } }

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

.empty-state svg {
  margin-bottom: 12px;
  opacity: 0.5;
}

.empty-state p {
  font-size: 0.9rem;
  line-height: 1.5;
}

/* ── Detail header (edge-to-edge) ── */
.detail-header {
  display: flex;
  align-items: center;
  padding: 16px 0;
  position: sticky;
  top: 0;
  background: var(--bg);
  z-index: 100;
  padding-top: calc(var(--safe-top) + 16px);
  width: calc(100% + 40px);
  margin: 0 -20px;
  padding-left: 20px;
  padding-right: 20px;
}

/* ── Detail screen ── */
.detail-screen {
  animation: fadeIn 0.3s ease-out;
}

@keyframes fadeIn {
  from { opacity: 0; transform: translateY(8px); }
  to { opacity: 1; transform: translateY(0); }
}

.detail-hero {
  position: relative;
  width: calc(100% + 40px);
  margin: 0 -20px;
  aspect-ratio: 16/9;
  overflow: hidden;
  background: var(--bg-elevated);
}

.detail-hero img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
}

.detail-hero-gradient {
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
  height: 50%;
  background: linear-gradient(transparent, var(--bg));
}

.detail-body {
  margin-top: -30px;
  position: relative;
  z-index: 1;
}

.detail-no-hero .detail-body {
  margin-top: 0;
}

.detail-poster-row {
  display: flex;
  gap: 16px;
  margin-bottom: 16px;
}

.detail-poster {
  width: 120px;
  flex-shrink: 0;
  aspect-ratio: 2/3;
  border-radius: var(--radius);
  overflow: hidden;
  box-shadow: var(--shadow-lg);
  background: var(--bg-elevated);
}

.detail-poster img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
}

.detail-info {
  flex: 1;
  display: flex;
  flex-direction: column;
  justify-content: flex-end;
  padding-bottom: 4px;
}

.detail-title {
  font-family: var(--font-display);
  font-size: 1.4rem;
  font-weight: 400;
  line-height: 1.2;
  margin-bottom: 6px;
}

.detail-meta {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-bottom: 8px;
}

.meta-tag {
  font-size: 0.75rem;
  font-weight: 500;
  color: var(--text-secondary);
  background: var(--bg-elevated);
  padding: 4px 10px;
  border-radius: 20px;
}

.detail-rating {
  font-size: 0.9rem;
  font-weight: 600;
  color: #f5c518;
}

.detail-rating span {
  color: var(--text-tertiary);
  font-weight: 400;
  font-size: 0.8rem;
}

/* ── Add to Shelf button ── */
.add-shelf-btn {
  width: 100%;
  margin-top: 16px;
  padding: 12px 18px;
  font-size: 0.9rem;
}

/* ── Sections ── */
.section {
  margin-top: 24px;
}

.section-title {
  font-family: var(--font-display);
  font-size: 1.1rem;
  font-weight: 400;
  margin-bottom: 12px;
}

.overview-text {
  font-size: 0.9rem;
  line-height: 1.6;
  color: var(--text-secondary);
}

/* ── Credits ── */
.credits-group {
  margin-bottom: 16px;
}

.credits-label {
  font-size: 0.75rem;
  color: var(--text-tertiary);
  text-transform: uppercase;
  letter-spacing: 0.06em;
  font-weight: 500;
  margin-bottom: 10px;
}

.credits-row {
  display: flex;
  gap: 12px;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  scrollbar-width: none;
  padding-bottom: 4px;
}

.credits-row::-webkit-scrollbar { display: none; }

.credit-person {
  flex-shrink: 0;
  width: 72px;
  text-align: center;
}

.credit-photo {
  width: 56px;
  height: 56px;
  border-radius: 50%;
  overflow: hidden;
  margin: 0 auto 6px;
  background: var(--bg-elevated);
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--text-tertiary);
}

.credit-photo img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
}

.credit-name {
  font-size: 0.7rem;
  font-weight: 500;
  line-height: 1.2;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.credit-role {
  font-size: 0.65rem;
  color: var(--text-tertiary);
  line-height: 1.2;
  margin-top: 1px;
  display: -webkit-box;
  -webkit-line-clamp: 1;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

/* ── Providers ── */
.providers-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.provider-chip {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 14px;
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  cursor: pointer;
  transition: background 0.15s ease;
  flex-wrap: wrap;
}

.provider-chip:hover {
  background: var(--bg-elevated);
}

.provider-logo {
  width: 32px;
  height: 32px;
  border-radius: 8px;
  object-fit: cover;
  flex-shrink: 0;
}

.provider-name {
  font-size: 0.85rem;
  font-weight: 500;
  flex: 1;
}

.provider-count {
  font-size: 0.75rem;
  color: var(--text-tertiary);
  font-weight: 500;
}

.provider-countries {
  display: none;
  width: 100%;
  padding-top: 8px;
  border-top: 1px solid var(--border);
  margin-top: 4px;
  flex-wrap: wrap;
  gap: 4px;
}

.provider-chip.expanded .provider-countries {
  display: flex;
}

.country-flag {
  font-size: 1.2rem;
  cursor: default;
  position: relative;
}

.country-flag[title]:hover::after {
  content: attr(title);
  position: absolute;
  bottom: 100%;
  left: 50%;
  transform: translateX(-50%);
  background: var(--text);
  color: var(--bg);
  font-size: 0.7rem;
  padding: 3px 8px;
  border-radius: 6px;
  white-space: nowrap;
  pointer-events: none;
  z-index: 10;
}

.no-providers {
  font-size: 0.85rem;
  color: var(--text-tertiary);
  font-style: italic;
  padding: 16px 0;
}

/* ── Trending / Popular ── */
.trending-section {
  margin-top: 8px;
}

.trending-label {
  font-size: 0.8rem;
  color: var(--text-tertiary);
  text-transform: uppercase;
  letter-spacing: 0.06em;
  font-weight: 500;
  margin-bottom: 14px;
}

/* ── Toast ── */
#toast {
  position: fixed;
  bottom: calc(var(--safe-bottom) + 24px);
  left: 50%;
  transform: translateX(-50%) translateY(100px);
  background: var(--text);
  color: var(--bg);
  padding: 12px 20px;
  border-radius: var(--radius);
  font-size: 0.85rem;
  font-weight: 500;
  z-index: 1000;
  opacity: 0;
  transition: all 0.3s ease;
  max-width: 90%;
  text-align: center;
}

#toast.show {
  opacity: 1;
  transform: translateX(-50%) translateY(0);
}

/* ── Update banner ── */
.update-banner {
  position: fixed;
  top: calc(var(--safe-top) + 8px);
  left: 50%;
  transform: translateX(-50%);
  background: var(--accent);
  color: #fff;
  padding: 10px 20px;
  border-radius: var(--radius);
  font-size: 0.85rem;
  font-weight: 500;
  z-index: 1000;
  cursor: pointer;
  box-shadow: var(--shadow-lg);
  animation: fadeIn 0.3s ease-out;
}

/* ── Pagination ── */
.pagination {
  display: flex;
  justify-content: center;
  gap: 8px;
  margin-top: 24px;
  padding-bottom: 8px;
}

.page-btn {
  padding: 8px 14px;
  border-radius: var(--radius-sm);
  font-size: 0.85rem;
  font-weight: 500;
  background: var(--bg-elevated);
  color: var(--text-secondary);
  transition: all 0.15s ease;
}

.page-btn:hover {
  background: var(--border);
  color: var(--text);
}

.page-btn.active {
  background: var(--accent);
  color: #fff;
}

.page-info {
  display: flex;
  align-items: center;
  font-size: 0.8rem;
  color: var(--text-tertiary);
  padding: 0 8px;
}

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

.genre-tag {
  font-size: 0.75rem;
  color: var(--accent);
  background: var(--accent-soft);
  padding: 4px 10px;
  border-radius: 20px;
  font-weight: 500;
}

/* ── Watchlist nav badge ── */
.watchlist-nav-btn {
  position: relative;
}

.wl-badge {
  position: absolute;
  top: -2px;
  right: -2px;
  background: var(--accent);
  color: #fff;
  font-size: 0.6rem;
  font-weight: 700;
  min-width: 16px;
  height: 16px;
  border-radius: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0 4px;
  line-height: 1;
  pointer-events: none;
}

/* ── Detail header actions ── */
.detail-header {
  justify-content: space-between;
}

.detail-header-actions {
  display: flex;
  gap: 8px;
  align-items: center;
}

.wl-toggle-btn {
  transition: all 0.2s ease;
}

.wl-toggle-btn.in-watchlist {
  color: var(--accent);
  background: var(--accent-soft);
}

.wl-toggle-btn:active {
  transform: scale(0.85);
}

.watched-toggle-btn {
  transition: all 0.2s ease;
}

.watched-toggle-btn.is-watched {
  color: #4caf50;
  background: rgba(76, 175, 80, 0.12);
}

.watched-toggle-btn:active {
  transform: scale(0.85);
}

/* ── Watchlist status filter bar ── */
.wl-status-bar {
  margin-bottom: 20px;
}

/* ── Watchlist card extras ── */
.wl-card {
  position: relative;
}

.wl-remove-btn {
  position: absolute;
  top: 6px;
  right: 6px;
  width: 26px;
  height: 26px;
  border-radius: 50%;
  background: rgba(0,0,0,0.6);
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
  display: flex;
  align-items: center;
  justify-content: center;
  color: #fff;
  z-index: 5;
  opacity: 0;
  transition: opacity 0.2s ease;
  cursor: pointer;
  border: none;
  padding: 0;
}

.wl-card:hover .wl-remove-btn,
.wl-card:active .wl-remove-btn {
  opacity: 1;
}

/* Always show on touch devices */
@media (hover: none) {
  .wl-remove-btn {
    opacity: 1;
  }
}

.badge-watched {
  position: absolute;
  top: 8px;
  right: 8px;
  width: 22px;
  height: 22px;
  border-radius: 50%;
  background: #4caf50;
  color: #fff;
  font-size: 0.7rem;
  font-weight: 700;
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 3;
}

/* Position watched badge at bottom-right on watchlist cards */
.wl-card .badge-watched {
  top: auto;
  bottom: 8px;
  right: 8px;
}

/* ── Responsive ── */
@media (max-width: 360px) {
  .results-grid { gap: 10px; }
  .detail-title { font-size: 1.2rem; }
  .detail-poster { width: 100px; }
}
