* { box-sizing: border-box; }

html, body {
  margin: 0;
  min-height: 100%;
  min-height: 100dvh;
  background: #0a0a0f;
  font-family: system-ui, -apple-system, "Segoe UI", sans-serif;
  color: #f4f4f5;
  -webkit-tap-highlight-color: transparent;
}

.app-header {
  padding: max(0.75rem, env(safe-area-inset-top)) 1rem 0.25rem;
  text-align: center;
  position: sticky;
  top: 0;
  z-index: 10;
  background: linear-gradient(180deg, rgba(10,10,15,0.97) 70%, transparent);
}

.app-header h1 {
  font-size: 1.25rem;
  font-weight: 700;
  margin: 0;
  letter-spacing: -0.02em;
}

.app-sub {
  margin: 0.35rem 0 0;
  font-size: 0.78rem;
  color: #a1a1aa;
  line-height: 1.4;
}

.tabs {
  display: flex;
  gap: 0.35rem;
  justify-content: center;
  padding: 0.65rem 1rem 0.35rem;
}

.tabs button {
  flex: 1;
  max-width: 8rem;
  padding: 0.5rem 0.45rem;
  border-radius: 999px;
  border: 1px solid #3f3f46;
  background: #18181b;
  color: #e4e4e7;
  font-size: 0.78rem;
  font-weight: 600;
  cursor: pointer;
}

.tabs button[aria-selected="true"] {
  background: linear-gradient(135deg, #0ea5e9, #6366f1);
  border-color: transparent;
  color: #fff;
}

.panel { display: none; padding: 0 1rem max(1rem, env(safe-area-inset-bottom)); }
.panel.is-active { display: block; }

.error-banner {
  margin: 0.5rem 1rem;
  padding: 0.65rem 0.85rem;
  border-radius: 10px;
  background: rgba(239, 68, 68, 0.15);
  border: 1px solid rgba(239, 68, 68, 0.4);
  color: #fecaca;
  font-size: 0.85rem;
}

.config-card {
  max-width: 520px;
  margin: 0.5rem auto 0;
  padding: 1rem;
  border-radius: 16px;
  background: #18181b;
  border: 1px solid #27272a;
}

.field-label {
  display: block;
  font-size: 0.8rem;
  font-weight: 600;
  color: #d4d4d8;
  margin-bottom: 0.35rem;
}

.field-input, .filter-input {
  width: 100%;
  padding: 0.65rem 0.75rem;
  border-radius: 10px;
  border: 1px solid #3f3f46;
  background: #09090b;
  color: #fafafa;
  font-size: 0.9rem;
}

.field-hint, .fav-hint {
  margin: 0.5rem 0 0;
  font-size: 0.78rem;
  color: #71717a;
  line-height: 1.45;
}

.config-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  margin-top: 1rem;
}

.btn {
  padding: 0.6rem 1rem;
  border-radius: 10px;
  border: none;
  font-size: 0.85rem;
  font-weight: 600;
  cursor: pointer;
}

.btn:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}

.btn--primary {
  background: linear-gradient(135deg, #0ea5e9, #6366f1);
  color: #fff;
}

.btn--secondary {
  background: #27272a;
  color: #e4e4e7;
  border: 1px solid #3f3f46;
}

.status-box {
  margin-top: 0.85rem;
  padding: 0.65rem 0.75rem;
  border-radius: 10px;
  background: rgba(14, 165, 233, 0.1);
  border: 1px solid rgba(14, 165, 233, 0.25);
  font-size: 0.82rem;
  color: #bae6fd;
}

.progress-wrap { margin-top: 0.85rem; }

.progress-bar {
  height: 6px;
  border-radius: 999px;
  background: #27272a;
  overflow: hidden;
}

.progress-bar__fill {
  height: 100%;
  width: 0%;
  background: linear-gradient(90deg, #0ea5e9, #6366f1);
  transition: width 0.2s ease;
}

.progress-label {
  margin: 0.4rem 0 0;
  font-size: 0.78rem;
  color: #a1a1aa;
}

.stats-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 0.65rem;
  margin: 1rem 0 0;
}

.stats-grid div {
  padding: 0.55rem 0.65rem;
  border-radius: 10px;
  background: #09090b;
  border: 1px solid #27272a;
}

.stats-grid dt {
  margin: 0;
  font-size: 0.72rem;
  color: #71717a;
}

.stats-grid dd {
  margin: 0.2rem 0 0;
  font-size: 1.15rem;
  font-weight: 700;
}

.meta-line {
  margin: 0.75rem 0 0;
  font-size: 0.75rem;
  color: #71717a;
}

.filters {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  margin-bottom: 0.65rem;
  align-items: flex-end;
}

.filters__row { min-width: 7rem; }
.filters__row--grow { flex: 1; min-width: 10rem; }

.filter-label {
  display: block;
  font-size: 0.72rem;
  color: #71717a;
  margin-bottom: 0.2rem;
}

.filter-select {
  width: 100%;
  padding: 0.45rem 0.5rem;
  border-radius: 8px;
  border: 1px solid #3f3f46;
  background: #18181b;
  color: #e4e4e7;
  font-size: 0.82rem;
}

.lib-count {
  margin: 0 0 0.5rem;
  font-size: 0.78rem;
  color: #71717a;
}

.lib-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(140px, 1fr));
  gap: 0.65rem;
}

.lib-card-wrap {
  position: relative;
}

.lib-card {
  position: relative;
  display: block;
  border: none;
  padding: 0;
  border-radius: 12px;
  overflow: hidden;
  background: #18181b;
  cursor: pointer;
  text-align: left;
  color: inherit;
  width: 100%;
  -webkit-user-select: none;
  user-select: none;
}

.lib-card:active {
  transform: scale(0.98);
  opacity: 0.92;
}

.lib-card:focus-visible {
  outline: 2px solid #6366f1;
  outline-offset: 2px;
}

.lib-card__poster {
  aspect-ratio: 2/3;
  background: #27272a;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
}

.lib-card__poster img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.lib-card__ph {
  font-size: 0.72rem;
  color: #52525b;
  padding: 0.5rem;
  text-align: center;
}

.lib-card__body { padding: 0.45rem 0.5rem 0.55rem; }

.lib-card__title {
  margin: 0;
  font-size: 0.78rem;
  font-weight: 600;
  line-height: 1.25;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.lib-card__meta {
  margin: 0.25rem 0 0;
  display: flex;
  align-items: center;
  gap: 0.35rem;
  font-size: 0.7rem;
  color: #a1a1aa;
}

.badge-type {
  padding: 0.1rem 0.35rem;
  border-radius: 4px;
  font-size: 0.62rem;
  font-weight: 700;
  text-transform: uppercase;
}

.badge-type--movie { background: #312e81; color: #c7d2fe; }
.badge-type--tv { background: #831843; color: #fbcfe8; }

.rating {
  display: inline-flex;
  align-items: center;
  gap: 0.15rem;
  font-weight: 700;
  font-size: 0.72rem;
}

.rating--high { color: #4ade80; }
.rating--mid { color: #facc15; }
.rating--low { color: #f87171; }
.rating--na { color: #71717a; }

.lib-card-wrap .lib-card__fav {
  position: absolute;
  top: 0.35rem;
  right: 0.35rem;
  width: 2rem;
  height: 2rem;
  border-radius: 999px;
  border: none;
  background: rgba(0,0,0,0.55);
  color: #fff;
  font-size: 1rem;
  cursor: pointer;
  z-index: 2;
}

.lib-card__fav.is-on { color: #fbbf24; }

.empty-state {
  text-align: center;
  color: #71717a;
  font-size: 0.88rem;
  padding: 2rem 1rem;
}

.modal {
  border: none;
  padding: 0;
  background: transparent;
  max-width: min(640px, 96vw);
  width: 100%;
}

.modal:not([open]) {
  display: none;
}

.modal[open] {
  display: block;
  position: fixed;
  inset: 0;
  margin: auto;
  z-index: 1000;
}

.modal::backdrop {
  background: rgba(0,0,0,0.75);
}

.modal__inner {
  position: relative;
  background: #18181b;
  border-radius: 16px;
  border: 1px solid #3f3f46;
  padding: 1rem;
  max-height: 85vh;
  overflow-y: auto;
}

.modal__close {
  position: absolute;
  top: 0.5rem;
  right: 0.5rem;
  width: 2rem;
  height: 2rem;
  border: none;
  border-radius: 999px;
  background: #27272a;
  color: #fff;
  font-size: 1.25rem;
  cursor: pointer;
  z-index: 2;
}

.modal-hero {
  border-radius: 12px;
  overflow: hidden;
  margin-bottom: 0.75rem;
  aspect-ratio: 16/9;
  background: #27272a;
}

.modal-hero img { width: 100%; height: 100%; object-fit: cover; }

.modal-title {
  margin: 0 0 0.35rem;
  font-size: 1.1rem;
}

.modal-overview {
  margin: 0.5rem 0;
  font-size: 0.85rem;
  color: #d4d4d8;
  line-height: 1.55;
  max-height: 12rem;
  overflow-y: auto;
}

.modal-meta {
  margin: 0.25rem 0 0;
  font-size: 0.78rem;
  color: #a1a1aa;
}

.modal-loading {
  text-align: center;
  color: #a1a1aa;
  padding: 2rem 1rem;
  font-size: 0.88rem;
}

.modal-trailer {
  margin: 0.75rem 0;
}

.modal-trailer__label {
  margin: 0 0 0.35rem;
  font-size: 0.75rem;
  color: #a1a1aa;
  font-weight: 600;
}

.modal-trailer__frame {
  position: relative;
  aspect-ratio: 16 / 9;
  border-radius: 10px;
  overflow: hidden;
  background: #000;
}

.modal-trailer__frame iframe {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  border: 0;
}

.btn--compact {
  padding: 0.45rem 0.65rem;
  font-size: 0.75rem;
  white-space: nowrap;
}

.filters__row .btn--compact {
  margin-top: 1.15rem;
}

.modal-fav-btn.is-on {
  color: #fbbf24;
  border-color: #fbbf24;
}

.modal-actions {
  display: flex;
  gap: 0.5rem;
  margin-top: 0.75rem;
}

.modal-actions .btn { flex: 1; text-align: center; text-decoration: none; }

.player-modal {
  border: none;
  padding: 0;
  background: transparent;
  max-width: min(960px, 98vw);
  width: 100%;
}

.player-modal:not([open]) {
  display: none;
}

.player-modal[open] {
  display: block;
  position: fixed;
  inset: 0;
  margin: auto;
  z-index: 1100;
}

.player-modal::backdrop {
  background: rgba(0, 0, 0, 0.92);
}

.player-modal__inner {
  background: #09090b;
  border-radius: 16px;
  border: 1px solid #3f3f46;
  overflow: hidden;
}

.player-modal__header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
  padding: 0.65rem 0.85rem;
  background: #18181b;
  border-bottom: 1px solid #27272a;
}

.player-modal__title {
  margin: 0;
  font-size: 0.95rem;
  font-weight: 600;
  line-height: 1.3;
  flex: 1;
  min-width: 0;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.player-modal__close {
  flex-shrink: 0;
  width: 2rem;
  height: 2rem;
  border: none;
  border-radius: 999px;
  background: #27272a;
  color: #fff;
  font-size: 1.25rem;
  cursor: pointer;
}

.player-modal__video-wrap {
  background: #000;
  aspect-ratio: 16 / 9;
  display: flex;
  align-items: center;
  justify-content: center;
}

#player-video {
  width: 100%;
  height: 100%;
  max-height: min(72vh, 540px);
  background: #000;
  object-fit: contain;
}

.player-status {
  margin: 0;
  padding: 0.55rem 0.85rem;
  font-size: 0.78rem;
  color: #a1a1aa;
  background: #18181b;
}

.player-status.is-error {
  color: #fca5a5;
  background: rgba(239, 68, 68, 0.12);
}

.player-modal__footer {
  padding: 0.65rem 0.85rem 0.85rem;
  background: #18181b;
}

.player-modal__footer .btn {
  display: block;
  width: 100%;
  text-align: center;
  text-decoration: none;
}

.lib-more {
  display: block;
  width: 100%;
  max-width: 320px;
  margin: 0.75rem auto 0;
}

@media (min-width: 640px) {
  .stats-grid { grid-template-columns: repeat(4, 1fr); }
  .lib-grid { grid-template-columns: repeat(auto-fill, minmax(155px, 1fr)); }
}
