:root {
  color-scheme: dark;
  --bg: #0f1117;
  --surface: #1a1d27;
  --border: #2a2f3d;
  --text: #e8eaef;
  --muted: #9aa3b5;
  --primary: #ff3d3d;
  --primary-hover: #ff5555;
  --success: #3dd68c;
  --error: #ff6b6b;
  --warning: #f5a623;
  --radius: 12px;
  --font: "Segoe UI", system-ui, sans-serif;
}

* { box-sizing: border-box; margin: 0; padding: 0; }

html {
  background: var(--bg);
}

body {
  font-family: var(--font);
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
  min-height: 100dvh;
  line-height: 1.5;
}

.container {
  max-width: 960px;
  margin: 0 auto;
  padding: 1.5rem;
}

.header {
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  padding-top: env(safe-area-inset-top, 0);
}

.header-inner {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding-top: 1rem;
  padding-bottom: 1rem;
}

.logo {
  color: var(--text);
  text-decoration: none;
  font-weight: 700;
  font-size: 1.1rem;
}

.header-user {
  display: flex;
  align-items: center;
  gap: 1rem;
  color: var(--muted);
}

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1.5rem;
  margin-bottom: 1.5rem;
}

.card h2 { margin-bottom: 0.5rem; font-size: 1.25rem; }

.muted { color: var(--muted); font-size: 0.9rem; }

.section-header {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  margin-bottom: 1.25rem;
}

.form { display: flex; flex-direction: column; gap: 1rem; margin-top: 1rem; }

.form-inline { flex-direction: row; flex-wrap: wrap; align-items: center; }

.form label { display: flex; flex-direction: column; gap: 0.35rem; font-size: 0.9rem; }

.search-form {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  align-items: center;
  flex: 1;
  min-width: 220px;
  max-width: 420px;
}

input[type="text"],
input[type="password"],
input[type="url"],
input[type="search"] {
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 8px;
  color: var(--text);
  padding: 0.65rem 0.85rem;
  font-size: 1rem;
  width: 100%;
}

.input-grow { flex: 1; min-width: 180px; }

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 0.65rem 1.1rem;
  border-radius: 8px;
  border: none;
  font-size: 0.95rem;
  cursor: pointer;
  text-decoration: none;
  white-space: nowrap;
}

.btn-primary {
  background: var(--primary);
  color: #fff;
}

.btn-primary:hover { background: var(--primary-hover); }

.btn-ghost {
  background: transparent;
  color: var(--muted);
  border: 1px solid var(--border);
}

.btn-ghost:hover { color: var(--text); border-color: var(--muted); }

.btn-danger {
  background: transparent;
  color: var(--error);
  border: 1px solid var(--error);
}

.btn-danger:hover {
  background: rgba(255, 107, 107, 0.15);
  color: #ff8888;
}

.btn-block { width: 100%; }

.delete-form { display: inline; margin: 0; }

.login-card {
  max-width: 400px;
  margin: 4rem auto;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 2rem;
}

.login-card h1 { margin-bottom: 0.25rem; }

.messages { margin-bottom: 1rem; }

.alert {
  padding: 0.75rem 1rem;
  border-radius: 8px;
  margin-bottom: 0.5rem;
  font-size: 0.9rem;
}

.alert-error { background: rgba(255, 107, 107, 0.15); color: var(--error); }
.alert-success { background: rgba(61, 214, 140, 0.15); color: var(--success); }

.playlists-card h2 {
  margin-bottom: 0.5rem;
}

.playlist-create-form {
  margin-top: 0.75rem;
  margin-bottom: 1rem;
}

.playlist-filters {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  align-items: center;
}

.playlist-chip-wrap {
  display: inline-flex;
  align-items: center;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: var(--bg);
  overflow: hidden;
}

.playlist-chip-wrap.active {
  border-color: var(--primary);
}

.playlist-chip {
  display: inline-flex;
  align-items: center;
  gap: 0.35rem;
  padding: 0.4rem 0.75rem;
  color: var(--text);
  text-decoration: none;
  font-size: 0.9rem;
}

.playlist-chip-wrap > .playlist-chip.active,
a.playlist-chip.active {
  background: rgba(255, 61, 61, 0.15);
  color: var(--primary);
}

.playlist-count {
  font-size: 0.75rem;
  color: var(--muted);
  background: var(--surface);
  padding: 0.1rem 0.4rem;
  border-radius: 999px;
}

.playlist-chip-delete {
  border: none;
  background: transparent;
  color: var(--muted);
  cursor: pointer;
  padding: 0.35rem 0.55rem;
  font-size: 1.1rem;
  line-height: 1;
}

.playlist-chip-delete:hover {
  color: var(--error);
}

.playlist-chip-wrap.active .playlist-chip-delete {
  border-left: 1px solid var(--border);
}

.playlist-filters > .playlist-chip.active {
  display: inline-flex;
  padding: 0.4rem 0.75rem;
  border-radius: 999px;
  border: 1px solid var(--primary);
  background: rgba(255, 61, 61, 0.15);
  color: var(--primary);
  text-decoration: none;
  font-size: 0.9rem;
}

.video-playlist-tags {
  display: flex;
  flex-wrap: wrap;
  gap: 0.35rem;
  margin-bottom: 0.4rem;
}

.playlist-tag {
  font-size: 0.75rem;
  padding: 0.15rem 0.45rem;
  border-radius: 6px;
  background: var(--surface);
  border: 1px solid var(--border);
  color: var(--muted);
  text-decoration: none;
}

.playlist-tag:hover {
  color: var(--text);
  border-color: var(--muted);
}

.add-to-playlist-form {
  display: flex;
  flex-wrap: wrap;
  gap: 0.35rem;
  align-items: center;
  margin-top: 0.5rem;
}

.playlist-pick {
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 8px;
  color: var(--text);
  padding: 0.35rem 0.5rem;
  font-size: 0.85rem;
  max-width: 160px;
}

.btn-sm {
  padding: 0.35rem 0.65rem;
  font-size: 0.85rem;
}

.video-total {
  font-size: 0.9rem;
  font-weight: normal;
}

.video-list-sentinel {
  height: 1px;
  margin-top: 0.5rem;
}

.video-list-loading {
  text-align: center;
  padding: 1rem;
  color: var(--muted);
  font-size: 0.9rem;
}

.video-list-loading.hidden,
.video-list-sentinel.hidden {
  display: none;
}

.video-grid { display: flex; flex-direction: column; gap: 1rem; }

.video-card {
  display: flex;
  flex-wrap: wrap;
  align-items: flex-start;
  gap: 1rem;
  padding: 1rem;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 10px;
}

.video-thumb {
  flex-shrink: 0;
  width: 168px;
  aspect-ratio: 16 / 9;
  border-radius: 8px;
  overflow: hidden;
  background: var(--surface);
}

.video-thumb-link {
  display: block;
  width: 100%;
  height: 100%;
  color: inherit;
  text-decoration: none;
}

.video-thumb-link:hover img,
.video-thumb-link:focus-visible img {
  opacity: 0.85;
}

.video-thumb img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
  transition: opacity 0.15s ease;
}

.video-title-link {
  color: var(--text);
  text-decoration: none;
}

.video-title-link:hover,
.video-title-link:focus-visible {
  color: var(--primary);
}

.video-thumb-placeholder {
  width: 100%;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--muted);
  font-size: 1.5rem;
  background: var(--surface);
}

.video-card-body {
  flex: 1;
  min-width: 180px;
}

.video-card-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  align-items: flex-start;
  margin-left: auto;
}

.video-title { font-size: 1rem; margin-bottom: 0.35rem; }

.video-meta { color: var(--muted); font-size: 0.85rem; margin-bottom: 0.5rem; }

.badge {
  display: inline-block;
  font-size: 0.75rem;
  padding: 0.2rem 0.5rem;
  border-radius: 6px;
  font-weight: 600;
}

.badge-pending, .badge-downloading { background: rgba(245, 166, 35, 0.2); color: var(--warning); }
.badge-completed { background: rgba(61, 214, 140, 0.2); color: var(--success); }
.badge-failed { background: rgba(255, 107, 107, 0.2); color: var(--error); }

.error-text { color: var(--error); font-size: 0.8rem; margin-top: 0.5rem; }

.empty-state { padding: 2rem 0; text-align: center; }

.watch-page h1 { font-size: 1.35rem; margin: 1rem 0; }

.back-link {
  color: var(--muted);
  text-decoration: none;
  font-size: 0.9rem;
}

.back-link:hover { color: var(--text); }

.player-wrap {
  background: #000;
  border-radius: var(--radius);
  overflow: hidden;
  aspect-ratio: 16 / 9;
}

.player-wrap video {
  width: 100%;
  height: 100%;
  display: block;
}

.player-tools {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  margin-top: 0.75rem;
}

.player-tools .btn.active {
  border-color: var(--success);
  color: var(--success);
}

.player-hint {
  margin-top: 0.75rem;
  font-size: 0.85rem;
  line-height: 1.45;
}

.player-hint strong {
  color: var(--text);
  font-weight: 600;
}

.btn.hidden {
  display: none;
}

.watch-actions {
  margin-top: 1rem;
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  align-items: center;
}

.install-banner {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 1000;
  padding: 0.75rem 0.75rem calc(0.75rem + env(safe-area-inset-bottom));
  background: linear-gradient(transparent, rgba(15, 17, 23, 0.95) 20%);
}

.install-banner.hidden {
  display: none;
}

.install-banner-inner {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 0.85rem 1rem;
  box-shadow: 0 -4px 24px rgba(0, 0, 0, 0.4);
}

.install-banner-icon {
  flex-shrink: 0;
  border-radius: 10px;
}

.install-banner-text {
  flex: 1;
  min-width: 0;
  font-size: 0.85rem;
}

.install-banner-text strong {
  display: block;
  font-size: 0.95rem;
  margin-bottom: 0.2rem;
}

.install-banner-text p {
  color: var(--muted);
  margin: 0;
  line-height: 1.35;
}

.install-banner-actions {
  display: flex;
  align-items: center;
  gap: 0.35rem;
  flex-shrink: 0;
}

.install-banner-close {
  background: transparent;
  border: none;
  color: var(--muted);
  font-size: 1.5rem;
  line-height: 1;
  cursor: pointer;
  padding: 0.25rem 0.5rem;
}

.install-banner-close:hover {
  color: var(--text);
}

.ios-share {
  display: inline-block;
  padding: 0.1rem 0.35rem;
  background: var(--bg);
  border-radius: 4px;
  font-weight: 600;
  color: var(--text);
}

@media (min-width: 901px) {
  .install-banner { display: none !important; }
}

@media (max-width: 600px) {
  .form-inline { flex-direction: column; align-items: stretch; }
  .video-thumb { width: 100%; max-width: 100%; }
  .video-card-actions { width: 100%; margin-left: 0; }
  .video-card-actions .btn { flex: 1; }
}
