:root {
  --bg: #0e1014;
  --surface: #181b22;
  --surface-2: #20242d;
  --border: #2a2f3a;
  --text: #e6e8ee;
  --muted: #9ba3b4;
  --accent: #ff4d4d;
  --accent-soft: #ff4d4d33;
  --ok: #4ade80;
  --warn: #fbbf24;
  --error: #ef4444;
}

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text);
  font-family: system-ui, -apple-system, "Segoe UI", sans-serif;
  font-size: 15px;
  line-height: 1.4;
}

.app {
  min-height: 100vh;
  /* Column layout: header sits on top, the shell (or watch page) fills
     the rest. This gives the sidebar a full-height column without us
     having to guess the header height in a `calc()`. */
  display: flex;
  flex-direction: column;
}

.boot {
  padding: 2rem;
  color: var(--muted);
}

/* ── Header ──────────────────────────────────────────────────────────── */

.header {
  padding: 1rem 1.5rem;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
}

.brand-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
}

.brand {
  display: flex;
  align-items: center;
  gap: 0.55rem;
}

.brand h1 {
  margin: 0;
  font-size: 1.25rem;
  letter-spacing: 0.01em;
}

.logo {
  display: block;
  height: 1.8rem;
  width: auto;
  /* The viewBox is tight; preserve the rounded-square aspect. */
  flex: 0 0 auto;
}

/* Legacy selector kept harmless in case any markup still uses it. */
.brand-row h1 {
  margin: 0;
  font-size: 1.25rem;
}

.search {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  flex-wrap: wrap;
}

.search input[type="search"] {
  /* Compact, not stretchy — these fields hold short strings and don't
     deserve to span the full row width. */
  flex: 0 1 auto;
  padding: 0.4rem 0.6rem;
  border: 1px solid var(--border);
  border-radius: 4px;
  background: var(--surface-2);
  color: var(--text);
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
}

.search input[type="search"] {
  font-family: inherit;
  width: 320px;
  max-width: 100%;
}

/* Per-result download row: quality dropdown + download button side by side. */
.download-row {
  display: flex;
  align-items: center;
  gap: 0.4rem;
}

.download-row .btn-download {
  flex: 1;
}

.quality-select {
  padding: 0.3rem 0.45rem;
  border: 1px solid var(--border);
  border-radius: 4px;
  background: var(--surface-2);
  color: var(--text);
  font-family: inherit;
  font-size: 0.82rem;
  cursor: pointer;
  /* Keep the dropdown compact — labels are short ("720p", "Best available"). */
  max-width: 8.5rem;
}

.quality-select:disabled {
  cursor: not-allowed;
  color: var(--muted);
}

.search button {
  padding: 0.4rem 0.9rem;
  border: 1px solid var(--border);
  background: var(--accent);
  color: white;
  border-radius: 4px;
  cursor: pointer;
}

.search button:hover {
  filter: brightness(1.1);
}

.search button:disabled,
.search input:disabled {
  background: var(--surface-2);
  color: var(--muted);
  cursor: not-allowed;
}

/* ── Health badge ────────────────────────────────────────────────────── */

.badge {
  padding: 0.2rem 0.55rem;
  border-radius: 999px;
  font-size: 0.8rem;
  border: 1px solid var(--border);
  background: var(--surface-2);
}

.badge-ok {
  color: var(--ok);
  border-color: color-mix(in srgb, var(--ok) 40%, transparent);
}

.badge-error {
  color: var(--error);
  border-color: color-mix(in srgb, var(--error) 40%, transparent);
}

.badge-loading {
  color: var(--warn);
}

.badge-idle {
  color: var(--muted);
}

/* ── App shell ───────────────────────────────────────────────────────── */

.shell {
  display: flex;
  align-items: stretch;
  /* Take whatever vertical space is left under the header so the
     sidebar's right border runs to the bottom of the viewport. */
  flex: 1;
}

.sidebar {
  flex: 0 0 200px;
  background: var(--surface);
  border-right: 1px solid var(--border);
  padding: 1rem 0.5rem;
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
}

.nav-item {
  display: flex;
  align-items: center;
  gap: 0.6rem;
  padding: 0.55rem 0.75rem;
  border: none;
  background: transparent;
  color: var(--muted);
  text-align: left;
  border-radius: 4px;
  cursor: pointer;
  font-size: 0.95rem;
  font-family: inherit;
  /* Anchor-as-nav-item: kill the default link decoration so it reads as
     a nav row, not a link. */
  text-decoration: none;
}

.nav-item:hover {
  background: var(--surface-2);
  color: var(--text);
}

.nav-item-active {
  background: var(--surface-2);
  color: var(--text);
  font-weight: 500;
  /* Left accent bar marks the active page without adding a layout shift. */
  box-shadow: inset 3px 0 0 var(--accent);
}

.nav-icon {
  font-size: 1rem;
  width: 1.25rem;
  display: inline-flex;
  justify-content: center;
}

.content {
  flex: 1;
  min-width: 0;
  padding: 1.5rem;
  max-width: 1200px;
  /* `margin: 0 auto` would centre the panel against the viewport, which
     looks weird when there's a sidebar on the left. Left-align instead. */
}

/* Narrow viewports: collapse the sidebar to a horizontal strip on top. */
@media (max-width: 720px) {
  .shell {
    flex-direction: column;
  }

  .sidebar {
    flex: 0 0 auto;
    flex-direction: row;
    border-right: none;
    border-bottom: 1px solid var(--border);
    padding: 0.5rem;
    gap: 0.5rem;
    overflow-x: auto;
  }

  .nav-item-active {
    /* Bottom accent bar makes more sense in a horizontal nav strip. */
    box-shadow: inset 0 -3px 0 var(--accent);
  }
}

.library-header {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  margin-bottom: 1rem;
}

.library-header h2 {
  margin: 0;
  font-size: 1.1rem;
}

.library-header button {
  padding: 0.3rem 0.75rem;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  border-radius: 4px;
  cursor: pointer;
}

.library-header button:disabled {
  color: var(--muted);
  cursor: not-allowed;
}

.hint {
  color: var(--muted);
}

.error {
  color: var(--error);
}

.count {
  color: var(--muted);
  font-size: 0.85rem;
  margin: 0 0 0.75rem 0;
}

/* ── Video grid ──────────────────────────────────────────────────────── */

.videos {
  list-style: none;
  padding: 0;
  margin: 0;
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(260px, 1fr));
  gap: 1rem;
}

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 6px;
  overflow: hidden;
  cursor: pointer;
  transition: border-color 0.15s ease, transform 0.15s ease;
}

.card:hover {
  border-color: var(--accent);
  transform: translateY(-1px);
}

.thumb {
  width: 100%;
  aspect-ratio: 16 / 9;
  object-fit: cover;
  display: block;
  background: var(--surface-2);
}

.thumb-placeholder {
  display: grid;
  place-items: center;
  color: var(--muted);
  font-size: 2.5rem;
}

.meta {
  padding: 0.6rem 0.75rem 0.75rem;
}

.title {
  margin: 0 0 0.25rem 0;
  font-size: 0.95rem;
  line-height: 1.3;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.channel {
  margin: 0;
  color: var(--muted);
  font-size: 0.85rem;
}

.info {
  margin: 0.25rem 0 0 0;
  color: var(--muted);
  font-size: 0.78rem;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
}

/* ── Search section ──────────────────────────────────────────────────── */

.search-section {
  margin-bottom: 2rem;
}

.search-section h2 {
  margin: 0 0 0.75rem 0;
  font-size: 1.1rem;
  font-weight: 500;
}

.search-section h2 code {
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  color: var(--accent);
}

.results {
  list-style: none;
  padding: 0;
  margin: 0;
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(260px, 1fr));
  gap: 1rem;
}

/* ── Pagination ──────────────────────────────────────────────────────── */

.pagination {
  margin-top: 1.25rem;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 0.75rem;
  flex-wrap: wrap;
}

.page-btn {
  padding: 0.35rem 0.85rem;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  border-radius: 4px;
  cursor: pointer;
  font-size: 0.85rem;
}

.page-btn:hover:not(:disabled) {
  border-color: var(--accent);
}

.page-btn:disabled {
  color: var(--muted);
  cursor: not-allowed;
  opacity: 0.6;
}

.page-indicator {
  color: var(--muted);
  font-size: 0.85rem;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
}

.result-card .meta {
  display: flex;
  flex-direction: column;
  gap: 0.4rem;
}

.btn {
  padding: 0.35rem 0.7rem;
  border: 1px solid var(--border);
  border-radius: 4px;
  cursor: pointer;
  font-size: 0.85rem;
}

.btn-download {
  background: var(--accent);
  color: white;
  border-color: transparent;
}

.btn-download:hover {
  filter: brightness(1.1);
}

.btn-play {
  background: var(--ok);
  color: #0b1410;
  border-color: transparent;
  font-weight: 500;
  /* `.btn-play` is rendered as an `<a>` now so the click takes the user
     to /video/{id} via modem. Re-flatten the link's default styling so it
     still reads as a button. */
  text-decoration: none;
  display: inline-block;
  text-align: center;
}

.btn-play:hover {
  filter: brightness(1.05);
}

/* Library card anchors wrap the thumbnail and the title. They are
   present in the DOM so modem can intercept the click, but should not
   themselves be visible link decorations. */
.card-link,
.card-link-text {
  display: block;
  color: inherit;
  text-decoration: none;
}

.card-link-text:hover {
  color: var(--accent);
}

.btn-cancel {
  padding: 0.15rem 0.55rem;
  border: 1px solid var(--border);
  background: var(--surface-2);
  color: var(--muted);
  border-radius: 4px;
  cursor: pointer;
  font-size: 0.78rem;
}

.btn-cancel:hover {
  border-color: var(--error);
  color: var(--error);
}

/* ── Download progress ────────────────────────────────────────────────── */

.progress {
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
}

.progress-bar {
  height: 6px;
  background: var(--surface-2);
  border-radius: 3px;
  overflow: hidden;
}

.progress-fill {
  height: 100%;
  background: var(--accent);
  transition: width 0.2s ease;
}

.progress-ready .progress-fill {
  background: var(--ok);
}

.progress-failed .progress-fill {
  background: var(--error);
}

.progress-cancelled .progress-fill {
  /* Muted neutral — cancellation is the user's own doing, not a real error. */
  background: var(--muted);
}

.progress-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 0.78rem;
  color: var(--muted);
}

.progress-label {
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
}

.progress-pct {
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  color: var(--text);
}

.progress-error {
  margin: 0.25rem 0 0 0;
  color: var(--error);
  font-size: 0.78rem;
}

.btn-dismiss {
  border: none;
  background: transparent;
  color: var(--muted);
  cursor: pointer;
  font-size: 0.95rem;
  padding: 0 0.25rem;
  line-height: 1;
}

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

/* ── Delete button on library cards ──────────────────────────────────── */

/* Continue Watching strip (Phase H). Sits above the main library
   grid; self-hides when no entries match a video in the loaded
   library. Reuses .card / .library-card styling, adds a thin
   progress bar overlay on the thumbnail. */
.continue-watching {
  margin-bottom: 1.5rem;
}
.continue-watching-heading {
  margin: 0 0 0.6rem 0;
  font-size: 1rem;
  color: var(--muted);
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.04em;
}
.continue-thumb-wrap {
  position: relative;
  display: block;
}
.continue-thumb-wrap .progress-bar {
  position: absolute;
  bottom: 0;
  left: 0;
  height: 3px;
  background: var(--accent);
  pointer-events: none;
}

.library-card {
  position: relative;
}

.btn-delete {
  position: absolute;
  top: 0.4rem;
  right: 0.4rem;
  border: none;
  background: rgba(0, 0, 0, 0.55);
  color: white;
  width: 28px;
  height: 28px;
  border-radius: 50%;
  cursor: pointer;
  /* Always visible — earlier we hid it behind a hover-only opacity
     trick which broke discoverability on touch devices and made the
     button feel non-responsive even on desktop. A faint baseline
     opacity keeps it from competing with the thumbnail while still
     being immediately tappable. */
  opacity: 0.75;
  transition: opacity 0.15s ease, background 0.15s ease;
  display: grid;
  place-items: center;
  font-size: 0.9rem;
  line-height: 1;
}

.library-card:hover .btn-delete {
  opacity: 1;
}

.btn-delete:hover {
  background: var(--error);
  opacity: 1;
}

/* ── Watch page ──────────────────────────────────────────────────────── */

.watch-page {
  padding: 1rem 1.5rem 2rem;
  max-width: 1200px;
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  gap: 1rem;
}

.btn-back {
  align-self: flex-start;
  padding: 0.35rem 0.8rem;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  border-radius: 4px;
  cursor: pointer;
  font-size: 0.9rem;
}

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

.watch-video {
  width: 100%;
  max-height: 80vh;
  background: black;
  border-radius: 6px;
  display: block;
}

.watch-meta {
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
}

.watch-title {
  margin: 0;
  font-size: 1.25rem;
  line-height: 1.3;
}
ound route ─────────────────────────────────────────────────── */

.not-found h2 {
  margin: 0 0 0.5rem 0;
}

.not-found a {
  color: var(--accent);
}

/* ── Sync status badge (Phase E) ─────────────────────────────────────── */

.sync-badge {
  margin-top: auto;
  padding: 0.75rem;
  border-radius: 0.5rem;
  font-size: 0.8rem;
  line-height: 1.3;
}
.sync-badge .sync-state {
  font-weight: 600;
}
.sync-badge .sync-hint {
  color: #aaa;
  margin-top: 0.15rem;
  word-break: break-word;
}
.sync-anon { background: #2a2a2a; }
.sync-loading { background: #2a2f3a; }
.sync-ok { background: #1f3a1f; }
.sync-ok .sync-hint { color: #8fd98f; }
.sync-err { background: #3a1f1f; }
.sync-err .sync-hint { color: #ef8585; }

/* The sidebar is `aside` with `nav-item`s as direct children. We rely on
   flex column + the sync badge's `margin-top: auto` to pin the badge to
   the bottom. Make sure the sidebar lays out as a column. */
.sidebar {
  display: flex;
  flex-direction: column;
}

/* ── Account page (Phase E) ─────────────────────────────────────────── */

.account-page {
  max-width: 44rem;
  display: flex;
  flex-direction: column;
  gap: 1rem;
}
.account-title { margin: 0; }
.account-blurb { color: #aaa; margin: 0; }
.account-anon, .account-signed-in {
  display: flex;
  flex-direction: column;
  gap: 1rem;
}
.account-card {
  background: #222;
  padding: 1rem 1.25rem;
  border-radius: 0.5rem;
  display: flex;
  flex-direction: column;
  gap: 0.6rem;
}
.account-card h3 {
  margin: 0;
  font-size: 1.05rem;
}
/* Header row inside a card: title on the left, status/badge on the
   right. Used by the Proxy card to put the health badge next to the
   "Proxy" heading. */
.account-card-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
  flex-wrap: wrap;
}
.account-card p { margin: 0; color: #bbb; }
.account-card .hint { color: #888; font-size: 0.85rem; }
.account-input {
  width: 100%;
  max-width: 26rem;
  padding: 0.45rem 0.6rem;
  font-family: ui-monospace, monospace;
  font-size: 0.9rem;
  background: #1a1a1a;
  color: #e5e5e5;
  border: 1px solid #333;
  border-radius: 0.35rem;
}
.account-actions {
  display: flex;
  gap: 0.5rem;
  flex-wrap: wrap;
}
.btn-primary { background: var(--accent); color: #fff; }
.btn-danger { background: #4a1f1f; color: #ef8585; }
.account-code-display code {
  background: #111;
  padding: 0.15rem 0.4rem;
  border-radius: 0.3rem;
  font-size: 0.95rem;
}

/* ── Channel page ────────────────────────────────────────────────────── */

.channel-page {
  display: flex;
  flex-direction: column;
  gap: 1rem;
}

/* Banner sits flush at the top of the page; CSS background lets us
   crop YouTube's wide banner art to the available width without
   distorting it. Falls back to an empty box when missing. */
.channel-banner {
  width: 100%;
  height: 180px;
  background-size: cover;
  background-position: center;
  background-color: var(--surface-2);
  border-radius: 0.5rem;
}

.channel-banner-empty {
  background: linear-gradient(135deg, var(--surface-2), var(--surface));
}

.channel-header {
  display: flex;
  align-items: center;
  gap: 1rem;
  flex-wrap: wrap;
}

.channel-avatar {
  width: 88px;
  height: 88px;
  border-radius: 50%;
  object-fit: cover;
  background: var(--surface-2);
  flex: 0 0 auto;
}

.channel-avatar-placeholder {
  display: grid;
  place-items: center;
  font-size: 2rem;
  color: var(--muted);
}

.channel-header-meta {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  gap: 0.15rem;
}

.channel-name {
  margin: 0;
  font-size: 1.4rem;
  line-height: 1.2;
}

.channel-handle {
  color: var(--muted);
  font-size: 0.9rem;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
}

.channel-subs {
  color: var(--muted);
  font-size: 0.85rem;
}

.channel-description {
  color: var(--muted);
  font-size: 0.9rem;
  line-height: 1.45;
  white-space: pre-wrap;
  /* Keep descriptions from blowing out the page; YouTube descriptions
     can be huge. Clamp to a few lines; we can add a Read-more later. */
  display: -webkit-box;
  -webkit-line-clamp: 6;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.btn-subscribe {
  /* Already styled via .btn-primary; this is here for future tweaks. */
  font-weight: 500;
}

.btn-subscribed {
  /* "Already subscribed" state — visually quieter than the call-to-action
     so it doesn't keep screaming at the user once the deed is done. */
  background: var(--surface-2);
  color: var(--ok);
  border: 1px solid color-mix(in srgb, var(--ok) 40%, transparent);
  cursor: pointer;
}

.btn-subscribed:hover {
  border-color: var(--error);
  color: var(--error);
}

.channel-videos-heading {
  margin: 0 0 0.5rem 0;
  font-size: 1rem;
  font-weight: 500;
  color: var(--muted);
}

/* ── Channel hits inside a search response ───────────────────────────── */

.channel-hits {
  margin-bottom: 1.5rem;
}

.channel-hits-heading {
  margin: 0 0 0.5rem 0;
  font-size: 0.9rem;
  font-weight: 500;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

.channel-hits-list {
  list-style: none;
  padding: 0;
  margin: 0;
  display: grid;
  /* Wider min so the card fits avatar + ~20 chars of name comfortably. */
  grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
  gap: 0.6rem;
}

.channel-hit {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 6px;
  overflow: hidden;
  transition: border-color 0.15s ease;
}

.channel-hit:hover {
  border-color: var(--accent);
}

.channel-hit-link {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  padding: 0.6rem 0.75rem;
  color: inherit;
  text-decoration: none;
}

.channel-hit-avatar {
  width: 48px;
  height: 48px;
  border-radius: 50%;
  object-fit: cover;
  background: var(--surface-2);
  flex: 0 0 auto;
}

.channel-hit-avatar-placeholder {
  display: grid;
  place-items: center;
  font-size: 1.4rem;
  color: var(--muted);
}

.channel-hit-meta {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  gap: 0.1rem;
}

.channel-hit-name {
  font-weight: 500;
  font-size: 0.95rem;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.channel-hit-handle {
  color: var(--muted);
  font-size: 0.8rem;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
}

.channel-hit-subs {
  color: var(--muted);
  font-size: 0.75rem;
}

/* Channel-name links inside video cards / watch page metadata. Subtle
   underline-on-hover so the link affordance is discoverable without
   bashing the eye on every card. */
.channel-link {
  color: inherit;
  text-decoration: none;
}

.channel-link:hover {
  color: var(--accent);
  text-decoration: underline;
}

/* Banner that pops up immediately after signup with the freshly
   generated code. Visually distinct so the user does not miss it. */
.fresh-code-banner {
  background: #3a3320;
  border: 1px solid #6b5b2e;
  padding: 1rem 1.25rem;
  border-radius: 0.5rem;
  display: flex;
  flex-direction: column;
  gap: 0.6rem;
}
.fresh-code-banner h3 { margin: 0; }
.fresh-code-banner p { margin: 0; }
.fresh-code-value code {
  display: block;
  background: #111;
  padding: 0.6rem 0.8rem;
  border-radius: 0.35rem;
  font-size: 1rem;
  word-break: break-all;
}

/* ── Subscriptions feed page ─────────────────────────────────────────── */

.subscriptions-section {
  display: flex;
  flex-direction: column;
  gap: 1rem;
}

.subs-header {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 1rem;
}

.subs-header h2 {
  margin: 0;
}

.subs-empty {
  background: var(--surface);
  border: 1px dashed var(--border);
  border-radius: 0.5rem;
  padding: 1.25rem 1.5rem;
}
