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

:root {
  --bg: #0d0d1a;
  --surface: #161627;
  --surface-2: #1d1d33;
  --border: #2a2a40;
  --accent: #e94560;
  --accent-dim: #b8354d;
  --text: #e8e8f0;
  --muted: #7a7a90;
  --success: #4ecdc4;
  --warning: #f5b942;
}

html, body { height: 100%; }
body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  display: flex;
  flex-direction: column;
}

a { color: inherit; text-decoration: none; }
button {
  background: var(--accent);
  color: #fff;
  border: none;
  padding: 0.45rem 1rem;
  border-radius: 6px;
  font-size: 0.875rem;
  cursor: pointer;
  font-weight: 500;
  transition: background 0.15s;
}
button:hover { background: var(--accent-dim); }
button:disabled { opacity: 0.4; cursor: default; }
button.ghost {
  background: transparent;
  border: 1px solid var(--border);
  color: var(--muted);
}
button.ghost:hover { border-color: var(--accent); color: var(--text); }
button.danger { background: transparent; border: 1px solid var(--accent-dim); color: var(--accent); }
button.danger:hover { background: var(--accent-dim); color: #fff; }

input, select {
  background: var(--surface-2);
  border: 1px solid var(--border);
  color: var(--text);
  padding: 0.45rem 0.7rem;
  border-radius: 6px;
  font-size: 0.875rem;
}
input:focus, select:focus { outline: none; border-color: var(--accent); }

/* --- top bar --- */
header#topbar[hidden] { display: none; }
header#topbar {
  display: flex;
  align-items: center;
  gap: 1.25rem;
  padding: 0.7rem 1.25rem;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 50;
}
header#topbar h1 { font-size: 1.2rem; font-weight: 700; letter-spacing: 0.05em; }
header#topbar h1 a { color: var(--accent); }
header#topbar .spacer { flex: 1; }
#nav { display: flex; gap: 1rem; }
#nav a { color: var(--muted); font-size: 0.9rem; padding: 0.25rem 0; }
#nav a:hover, #nav a.active { color: var(--text); }
#global-search { width: 220px; }
#user-menu { display: flex; align-items: center; gap: 0.6rem; font-size: 0.85rem; color: var(--muted); }
#user-menu b { color: var(--text); }

main#app { --page-pad: 1.5rem; flex: 1; padding: var(--page-pad); max-width: 1500px; width: 100%; margin: 0 auto; }

/* --- login --- */
.login-wrap { display: flex; align-items: center; justify-content: center; min-height: 70vh; }
.login-box {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 2.5rem;
  width: 340px;
  display: flex;
  flex-direction: column;
  gap: 0.9rem;
}
.login-box h1 { color: var(--accent); text-align: center; letter-spacing: 0.08em; margin-bottom: 0.5rem; }
.login-box input { width: 100%; }
.login-box .error { color: var(--accent); font-size: 0.85rem; min-height: 1.2em; }

/* --- rows & grids --- */
.section { margin-bottom: 2.2rem; }
.section > h2 {
  font-size: 1.05rem;
  font-weight: 600;
  margin-bottom: 0.8rem;
  display: flex;
  align-items: baseline;
  gap: 0.75rem;
}
.section > h2 a { color: var(--muted); font-size: 0.8rem; font-weight: 400; }
.section > h2 a:hover { color: var(--accent); }

.card-row {
  display: grid;
  grid-auto-flow: column;
  grid-auto-columns: 150px;
  gap: 0.9rem;
  overflow-x: auto;
  scrollbar-width: none;
  scroll-snap-type: x proximity;
  /* Bleed to the viewport edge so the row cuts at the screen, not
     mid-poster inside the page gutter. */
  margin: 0 calc(-1 * var(--page-pad, 1.5rem));
  padding: 0.25rem var(--page-pad, 1.5rem) 0.5rem;
}
.card-row::-webkit-scrollbar { display: none; }
.card-row > .card { scroll-snap-align: start; }
/* 16:9 rows (continue watching, videos libraries) need wider columns. */
.card-row.wide { grid-auto-columns: 250px; }
.card-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
  gap: 1rem 0.9rem;
}

.card { display: flex; flex-direction: column; gap: 0.4rem; cursor: pointer; }
.card .poster {
  position: relative;
  aspect-ratio: 2 / 3;
  border-radius: 8px;
  overflow: hidden;
  background: var(--surface-2);
  border: 1px solid var(--border);
  transition: transform 0.12s, border-color 0.12s;
}
.card.wide .poster { aspect-ratio: 16 / 9; }
.card:hover .poster { transform: scale(1.03); border-color: var(--accent); }
/* position:relative lifts the img above the absolutely-positioned placeholder,
   so the title text only shows while there is no artwork (error removes the img). */
.card .poster img { width: 100%; height: 100%; object-fit: cover; display: block; position: relative; }
.card .poster .placeholder {
  position: absolute; inset: 0;
  display: flex; align-items: center; justify-content: center;
  color: var(--muted); font-size: 1.05rem; font-weight: 700;
  padding: 0.5rem; text-align: center;
  overflow: hidden;
}
/* Long junk filenames must not paint outside the card. */
.card .poster .placeholder span {
  display: -webkit-box;
  -webkit-line-clamp: 3;
  -webkit-box-orient: vertical;
  overflow: hidden;
  overflow-wrap: anywhere;
}
.card .poster .placeholder small { font-size: 0.75rem; font-weight: 400; }
.card .title { font-size: 0.85rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.card .sub { font-size: 0.75rem; color: var(--muted); }
.card .progress-bar, .ep-row .progress-bar {
  position: absolute; left: 0; right: 0; bottom: 0;
  height: 4px; background: rgba(0,0,0,0.5);
}
.progress-bar > div { height: 100%; background: var(--accent); }
.badge {
  position: absolute; top: 6px; right: 6px;
  background: rgba(0,0,0,0.65);
  border-radius: 4px;
  padding: 0.1rem 0.4rem;
  font-size: 0.7rem;
}
.badge.watched { color: var(--success); }

/* --- library toolbar --- */
.toolbar { display: flex; gap: 0.7rem; align-items: center; margin-bottom: 1.2rem; flex-wrap: wrap; }
.toolbar h2 { font-size: 1.2rem; margin-right: auto; }
.load-more { display: flex; justify-content: center; margin: 1.5rem 0; }

/* --- item detail --- */
.detail-hero {
  position: relative;
  border-radius: 12px;
  overflow: hidden;
  margin-bottom: 1.5rem;
  min-height: 240px;
  background: var(--surface);
  border: 1px solid var(--border);
}
.detail-hero .backdrop {
  position: absolute; inset: 0;
  background-size: cover; background-position: center 20%;
  filter: brightness(0.35);
}
.detail-hero .inner {
  position: relative;
  display: flex;
  gap: 1.5rem;
  padding: 1.8rem;
}
.detail-hero .poster {
  width: 180px; flex-shrink: 0;
  aspect-ratio: 2 / 3;
  border-radius: 8px;
  overflow: hidden;
  background: var(--surface-2);
}
.detail-hero .poster img { width: 100%; height: 100%; object-fit: cover; }
.detail-hero .info { display: flex; flex-direction: column; gap: 0.6rem; min-width: 0; }
.detail-hero .info h2 { font-size: 1.6rem; }
.detail-hero .meta { color: var(--muted); font-size: 0.85rem; display: flex; gap: 0.9rem; flex-wrap: wrap; }
.detail-hero .tagline { color: var(--muted); font-style: italic; font-size: 0.9rem; }
.detail-hero .overview { font-size: 0.92rem; line-height: 1.5; max-width: 60ch; }
.detail-hero .actions { display: flex; gap: 0.7rem; margin-top: auto; padding-top: 0.8rem; flex-wrap: wrap; }
.file-info { font-size: 0.75rem; color: var(--muted); }

/* --- episodes --- */
.season-tabs { display: flex; gap: 0.5rem; margin-bottom: 1rem; flex-wrap: wrap; }
.season-tabs button { background: var(--surface-2); border: 1px solid var(--border); color: var(--muted); }
.season-tabs button.active { background: var(--accent); border-color: var(--accent); color: #fff; }
.ep-list { display: flex; flex-direction: column; gap: 0.6rem; }
.ep-row {
  display: flex; gap: 1rem;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 0.7rem;
  cursor: pointer;
  align-items: center;
}
.ep-row:hover { border-color: var(--accent); }
.ep-row .still {
  position: relative;
  width: 160px; aspect-ratio: 16 / 9; flex-shrink: 0;
  border-radius: 6px; overflow: hidden; background: var(--surface-2);
}
.ep-row .still img { width: 100%; height: 100%; object-fit: cover; }
.ep-row .ep-info { min-width: 0; }
.ep-row .ep-info h4 { font-size: 0.95rem; margin-bottom: 0.25rem; }
.ep-row .ep-info p {
  font-size: 0.8rem; color: var(--muted); line-height: 1.4;
  display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden;
}
.ep-row .ep-check { margin-left: auto; flex-shrink: 0; }

/* --- player --- */
#player-overlay {
  position: fixed; inset: 0; z-index: 100;
  background: #000;
  display: flex; flex-direction: column;
}
#player-overlay video { flex: 1; width: 100%; min-height: 0; }
#player-overlay .player-top {
  position: absolute; top: 0; left: 0; right: 0;
  display: flex; align-items: center; gap: 1rem;
  padding: 0.8rem 1.2rem;
  background: linear-gradient(rgba(0,0,0,0.7), transparent);
  z-index: 2;
  opacity: 0;
  transition: opacity 0.2s;
}
#player-overlay:hover .player-top { opacity: 1; }
#player-overlay .player-top h3 { font-size: 1rem; font-weight: 500; }

/* --- admin --- */
.admin-tabs { display: flex; gap: 0.5rem; margin-bottom: 1.5rem; }
.admin-tabs button { background: var(--surface-2); border: 1px solid var(--border); color: var(--muted); }
.admin-tabs button.active { background: var(--accent); border-color: var(--accent); color: #fff; }
table { width: 100%; border-collapse: collapse; font-size: 0.85rem; }
th, td { text-align: left; padding: 0.55rem 0.7rem; border-bottom: 1px solid var(--border); }
th { color: var(--muted); font-weight: 500; font-size: 0.75rem; text-transform: uppercase; letter-spacing: 0.05em; }
td .bar {
  background: var(--surface-2); border-radius: 4px; height: 8px; width: 140px; overflow: hidden;
}
td .bar > div { height: 100%; background: var(--accent); }
.form-row { display: flex; gap: 0.7rem; margin: 1rem 0; flex-wrap: wrap; align-items: center; }
.pill {
  display: inline-block; padding: 0.1rem 0.5rem; border-radius: 10px;
  font-size: 0.72rem; border: 1px solid var(--border); color: var(--muted);
}
.pill.ready { color: var(--success); border-color: var(--success); }
.pill.failed { color: var(--accent); border-color: var(--accent); }
.pill.transcoding, .pill.queued { color: var(--warning); border-color: var(--warning); }
.lib-pills { display: flex; flex-wrap: wrap; gap: 0.35rem; align-items: center; }
button.lib-pill {
  background: transparent;
  border: 1px solid var(--border);
  color: var(--muted);
  border-radius: 10px;
  padding: 0.15rem 0.6rem;
  font-size: 0.75rem;
  font-weight: 400;
}
button.lib-pill:hover { border-color: var(--accent); color: var(--text); background: transparent; }
button.lib-pill.active { background: var(--accent); border-color: var(--accent); color: #fff; }
button.lib-pill.active:hover { background: var(--accent-dim); }
.muted { color: var(--muted); }
.empty { color: var(--muted); text-align: center; padding: 3rem 0; }

/* --- search results dropdown --- */
#search-results {
  position: absolute;
  top: 100%; right: 0;
  width: 380px; max-height: 60vh; overflow-y: auto;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 0 0 10px 10px;
  z-index: 60;
}
#search-results .sr-row { display: flex; gap: 0.7rem; padding: 0.55rem 0.8rem; cursor: pointer; align-items: center; }
#search-results .sr-row:hover { background: var(--surface-2); }
#search-results .sr-row img { width: 34px; height: 51px; object-fit: cover; border-radius: 4px; background: var(--surface-2); }
#search-results .sr-row .muted { font-size: 0.75rem; }

@media (max-width: 700px) {
  main#app { --page-pad: 1rem; padding: var(--page-pad); }
  .detail-hero .poster { width: 110px; }
  .detail-hero .inner { padding: 1rem; gap: 1rem; }
  #global-search { width: 130px; }
  .card-grid { grid-template-columns: repeat(auto-fill, minmax(110px, 1fr)); }
  .card-row { grid-auto-columns: 110px; }
  .ep-row .still { width: 110px; }
}
