/* =========================================================================
   Comic Library — design tokens
   Dark-first, calm, dense. Light theme via prefers-color-scheme.
   ========================================================================= */

:root {
  --bg: #101215;
  --bg-elevated: #171a1f;
  --bg-elevated-2: #1e2228;
  --bg-hover: #262b33;
  --border: #2a2f37;
  --border-strong: #3a4048;

  --text: #e8eaed;
  --text-dim: #a7adb6;
  --text-faint: #6b7280;

  --accent: #5b9dff;
  --accent-strong: #7fb2ff;
  --accent-bg: rgba(91, 157, 255, 0.16);
  --accent-border: rgba(91, 157, 255, 0.45);

  --green: #4cc38a;
  --green-bg: rgba(76, 195, 138, 0.16);
  --green-border: rgba(76, 195, 138, 0.4);

  --amber: #e0a940;
  --amber-bg: rgba(224, 169, 64, 0.18);
  --amber-border: rgba(224, 169, 64, 0.45);

  --red: #e5686a;
  --red-bg: rgba(229, 104, 106, 0.16);
  --red-border: rgba(229, 104, 106, 0.4);

  --radius-sm: 6px;
  --radius: 10px;
  --radius-lg: 14px;

  --shadow: 0 4px 16px rgba(0, 0, 0, 0.35);

  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial,
    sans-serif, "Apple Color Emoji", "Segoe UI Emoji";
  --font-mono: ui-monospace, SFMono-Regular, "SF Mono", Menlo, Consolas, monospace;
}

/* The hidden attribute must beat any component display rule. Without this,
   `.btn { display: inline-flex }` wins over the UA stylesheet and a button
   set to hidden stays on screen doing nothing when clicked. */
[hidden] { display: none !important; }


@media (prefers-color-scheme: light) {
  :root {
    --bg: #f5f6f8;
    --bg-elevated: #ffffff;
    --bg-elevated-2: #eef0f3;
    --bg-hover: #e6e8ec;
    --border: #dde1e6;
    --border-strong: #c7ccd3;

    --text: #1a1d21;
    --text-dim: #565d66;
    --text-faint: #8a909a;

    --accent: #2f6fe0;
    --accent-strong: #1f5bc4;
    --accent-bg: rgba(47, 111, 224, 0.1);
    --accent-border: rgba(47, 111, 224, 0.35);

    --green: #1f9d63;
    --green-bg: rgba(31, 157, 99, 0.12);
    --green-border: rgba(31, 157, 99, 0.35);

    --amber: #b5790f;
    --amber-bg: rgba(181, 121, 15, 0.14);
    --amber-border: rgba(181, 121, 15, 0.35);

    --red: #cc3b3e;
    --red-bg: rgba(204, 59, 62, 0.12);
    --red-border: rgba(204, 59, 62, 0.35);

    --shadow: 0 4px 16px rgba(0, 0, 0, 0.08);
  }
}

/* =========================================================================
   Reset & base
   ========================================================================= */

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

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  -webkit-font-smoothing: antialiased;
}

body {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  font-size: 15px;
  line-height: 1.45;
}

a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }

h1, h2, h3 { margin: 0 0 0.5em; font-weight: 600; line-height: 1.25; }
p { margin: 0 0 0.75em; }

svg { display: block; flex-shrink: 0; }

button, input, select, textarea { font-family: inherit; font-size: inherit; color: inherit; }

img { max-width: 100%; display: block; }

::selection { background: var(--accent-bg); }

/* =========================================================================
   Top nav
   ========================================================================= */

.topnav {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 0 16px;
  height: 52px;
  background: var(--bg-elevated);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 40;
}

.topnav-left,
.topnav-right {
  display: flex;
  align-items: center;
  gap: 4px;
  min-width: 0;
}

.brand {
  display: flex;
  align-items: center;
  gap: 8px;
  color: var(--text);
  font-weight: 600;
  padding: 6px 10px 6px 4px;
  margin-right: 4px;
  white-space: nowrap;
}
.brand:hover { text-decoration: none; color: var(--text); }
.brand span { display: none; }
@media (min-width: 560px) {
  .brand span { display: inline; }
}

.navlink {
  color: var(--text-dim);
  padding: 8px 10px;
  border-radius: var(--radius-sm);
  font-size: 14px;
  white-space: nowrap;
}
.navlink:hover { color: var(--text); background: var(--bg-hover); text-decoration: none; }
.navlink.is-active { color: var(--text); background: var(--bg-elevated-2); }

.nav-user {
  color: var(--text-dim);
  font-size: 13px;
  padding: 6px 4px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 140px;
}

.logout-form { display: inline-flex; }

/* =========================================================================
   Layout
   ========================================================================= */

.page {
  flex: 1;
  width: 100%;
  max-width: 1100px;
  margin: 0 auto;
  padding: 20px 16px 40px;
}

.site-footer {
  padding: 14px 16px 24px;
  text-align: center;
  color: var(--text-faint);
  font-size: 12.5px;
}

/* =========================================================================
   Buttons
   ========================================================================= */

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  border: 1px solid var(--border-strong);
  background: var(--bg-elevated-2);
  color: var(--text);
  padding: 8px 14px;
  border-radius: var(--radius-sm);
  cursor: pointer;
  font-size: 14px;
  font-weight: 500;
  line-height: 1.2;
  white-space: nowrap;
  transition: background 0.12s ease, border-color 0.12s ease, opacity 0.12s ease;
}
.btn:hover { background: var(--bg-hover); }
.btn:active { transform: translateY(1px); }
.btn:disabled { opacity: 0.45; cursor: not-allowed; }
.btn:disabled:hover { background: var(--bg-elevated-2); }

.btn-primary {
  background: var(--accent);
  border-color: var(--accent);
  color: #06101f;
}
.btn-primary:hover { background: var(--accent-strong); border-color: var(--accent-strong); }

.btn-ghost {
  background: transparent;
  border-color: transparent;
  color: var(--text-dim);
}
.btn-ghost:hover { background: var(--bg-hover); color: var(--text); }

.btn-danger { color: var(--red); border-color: var(--red-border); background: var(--red-bg); }
.btn-danger:hover { background: var(--red-bg); filter: brightness(1.15); }

.btn-sm { padding: 5px 10px; font-size: 13px; }

/* =========================================================================
   Forms
   ========================================================================= */

.field-input {
  background: var(--bg-elevated);
  border: 1px solid var(--border-strong);
  color: var(--text);
  border-radius: var(--radius-sm);
  padding: 8px 10px;
  font-size: 14px;
  min-width: 0;
}
.field-input:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-bg);
}

.field-number { width: 84px; }

/* =========================================================================
   Login page
   ========================================================================= */

.login-shell {
  min-height: calc(100vh - 52px);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
}

.login-card {
  width: 100%;
  max-width: 340px;
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 28px 24px;
  box-shadow: var(--shadow);
}

.login-card h1 {
  font-size: 18px;
  text-align: center;
  margin-bottom: 20px;
}

.login-field { display: flex; flex-direction: column; gap: 6px; margin-bottom: 14px; }
.login-field label { font-size: 13px; color: var(--text-dim); }
.login-field .field-input { width: 100%; padding: 10px 12px; }

.login-error {
  display: flex;
  align-items: center;
  gap: 8px;
  background: var(--red-bg);
  border: 1px solid var(--red-border);
  color: var(--red);
  border-radius: var(--radius-sm);
  padding: 9px 12px;
  font-size: 13.5px;
  margin-bottom: 16px;
}

.login-submit { width: 100%; padding: 10px 14px; margin-top: 4px; }

/* =========================================================================
   Pills / badges
   ========================================================================= */

.pill {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  background: var(--bg-elevated-2);
  border: 1px solid var(--border);
  color: var(--text-dim);
  font-size: 12.5px;
  padding: 3px 9px;
  border-radius: 999px;
  white-space: nowrap;
}

.badge-pct {
  position: absolute;
  top: 6px;
  right: 6px;
  background: rgba(16, 18, 21, 0.82);
  color: #fff;
  font-size: 11.5px;
  font-weight: 600;
  padding: 2px 6px;
  border-radius: 999px;
  border: 1px solid rgba(255, 255, 255, 0.15);
}

/* =========================================================================
   Progress bar
   ========================================================================= */

.progress {
  height: 6px;
  border-radius: 999px;
  background: var(--bg-elevated-2);
  overflow: hidden;
  position: relative;
}
.progress > span {
  display: block;
  height: 100%;
  background: var(--accent);
  border-radius: 999px;
  transition: width 0.25s ease;
}
.progress.is-failed > span { background: var(--red); }

/* =========================================================================
   Empty state
   ========================================================================= */

.empty-state {
  text-align: center;
  padding: 48px 20px;
  color: var(--text-faint);
}
.empty-state svg { margin: 0 auto 12px; opacity: 0.6; }
.empty-state h2 { color: var(--text-dim); font-size: 16px; }
.empty-state p { font-size: 13.5px; }

/* =========================================================================
   Library page
   ========================================================================= */

.page-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  flex-wrap: wrap;
  margin-bottom: 18px;
}

.page-header h1 { font-size: 20px; margin: 0; }

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

.filter-bar .field-input { flex: 1; min-width: 140px; }

/* Active downloads panel */

.active-panel {
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 14px 16px;
  margin-bottom: 22px;
}
.active-panel h2 {
  font-size: 13px;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--text-faint);
  margin-bottom: 10px;
}

.job-row {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 9px 0;
  border-top: 1px solid var(--border);
}
.job-row:first-of-type { border-top: none; }

.job-row-main {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  gap: 5px;
}

.job-row-title {
  display: flex;
  align-items: baseline;
  gap: 8px;
  font-size: 13.5px;
  white-space: nowrap;
  overflow: hidden;
}
.job-row-title .series { font-weight: 600; overflow: hidden; text-overflow: ellipsis; }
.job-row-title .chapter { color: var(--text-dim); flex-shrink: 0; }

.job-row-meta {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 12px;
  color: var(--text-faint);
  font-variant-numeric: tabular-nums;
}

.job-row-status {
  font-size: 11px;
  padding: 2px 7px;
  border-radius: 999px;
  white-space: nowrap;
  text-transform: capitalize;
}
.job-row-status.status-queued { background: var(--bg-elevated-2); color: var(--text-dim); }
.job-row-status.status-running { background: var(--accent-bg); color: var(--accent); }
.job-row-status.status-failed { background: var(--red-bg); color: var(--red); }

.job-row-error {
  color: var(--red);
  font-size: 12px;
  margin-top: 2px;
}

.job-row-actions { display: flex; gap: 6px; flex-shrink: 0; }
.icon-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 30px;
  height: 30px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border-strong);
  background: var(--bg-elevated-2);
  color: var(--text-dim);
  cursor: pointer;
}
.icon-btn:hover { background: var(--bg-hover); color: var(--text); }

/* Series grid */

.series-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
  gap: 16px;
}

.series-card {
  display: block;
  color: var(--text);
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
  transition: border-color 0.12s ease, transform 0.12s ease;
}
.series-card:hover { border-color: var(--border-strong); text-decoration: none; }

.series-card-cover {
  position: relative;
  aspect-ratio: 3 / 4;
  background: var(--bg-elevated-2);
  overflow: hidden;
}
.series-card-cover img { width: 100%; height: 100%; object-fit: cover; }

.cover-placeholder {
  width: 100%;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--text-faint);
  background: linear-gradient(160deg, var(--bg-elevated-2), var(--bg-hover));
}

.series-card-body { padding: 9px 10px 11px; }
.series-card-title {
  font-size: 13.5px;
  font-weight: 600;
  overflow: hidden;
  text-overflow: ellipsis;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  margin-bottom: 4px;
}
.series-card-meta { font-size: 12px; color: var(--text-faint); }

/* =========================================================================
   Search page
   ========================================================================= */

.search-form {
  display: flex;
  gap: 8px;
  margin-bottom: 20px;
}
.search-form .field-input { flex: 1; padding: 10px 12px; }

.owned-tag {
  font-size: 11px;
  color: var(--green);
  background: var(--green-bg);
  border: 1px solid var(--green-border);
  padding: 1px 7px;
  border-radius: 999px;
}

.result-meta-line {
  font-size: 12px;
  color: var(--text-faint);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

/* =========================================================================
   Series page
   ========================================================================= */

.series-header {
  display: flex;
  gap: 18px;
  margin-bottom: 20px;
}

.series-header-cover {
  width: 96px;
  flex-shrink: 0;
  aspect-ratio: 3 / 4;
  border-radius: var(--radius);
  overflow: hidden;
  background: var(--bg-elevated-2);
}
.series-header-cover img { width: 100%; height: 100%; object-fit: cover; }

.series-header-info { min-width: 0; display: flex; flex-direction: column; justify-content: center; gap: 8px; }
.series-header-info h1 { font-size: 19px; word-break: break-word; }
.series-header-pills { display: flex; gap: 6px; flex-wrap: wrap; }

/* Toolbar */

.toolbar {
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 12px 14px;
  margin-bottom: 18px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.toolbar-row {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
}

.toolbar-label { font-size: 13px; color: var(--text-dim); white-space: nowrap; }

.toolbar-range { display: flex; align-items: center; gap: 6px; flex-wrap: wrap; }

.toolbar-spacer { flex: 1; }

.toolbar-buttons { display: flex; gap: 6px; }

.checkbox-row {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 13.5px;
  color: var(--text-dim);
  cursor: pointer;
  user-select: none;
}
.checkbox-row input { width: 16px; height: 16px; accent-color: var(--accent); }

/* Chapter blocks */

.chapter-block {
  border: 1px solid var(--border);
  border-radius: var(--radius);
  margin-bottom: 8px;
  overflow: hidden;
  background: var(--bg-elevated);
}

.chapter-block-header {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 12px;
  cursor: pointer;
  background: transparent;
  border: none;
  width: 100%;
  text-align: left;
  color: var(--text);
}
.chapter-block-header:hover { background: var(--bg-hover); }

.chapter-block-chevron {
  color: var(--text-faint);
  transition: transform 0.15s ease;
  flex-shrink: 0;
}
.chapter-block.is-open .chapter-block-chevron { transform: rotate(90deg); }

.chapter-block-range {
  font-weight: 600;
  font-size: 13.5px;
  white-space: nowrap;
  min-width: 74px;
}

.chapter-block-bar {
  flex: 1;
  height: 6px;
  border-radius: 999px;
  background: var(--bg-elevated-2);
  overflow: hidden;
  min-width: 40px;
}
.chapter-block-bar > span {
  display: block;
  height: 100%;
  background: var(--green);
  border-radius: 999px;
}

.chapter-block-count {
  font-size: 12px;
  color: var(--text-faint);
  white-space: nowrap;
  min-width: 44px;
  text-align: right;
  font-variant-numeric: tabular-nums;
}

.chapter-block-selected {
  font-size: 11.5px;
  color: var(--accent);
  background: var(--accent-bg);
  border: 1px solid var(--accent-border);
  padding: 1px 8px;
  border-radius: 999px;
  white-space: nowrap;
}

.chapter-block-body {
  display: none;
  padding: 10px 12px 14px;
  border-top: 1px solid var(--border);
}
.chapter-block.is-open .chapter-block-body { display: block; }

.chapter-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(52px, 1fr));
  gap: 6px;
}

.chapter-chip {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 34px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border-strong);
  background: var(--bg-elevated-2);
  color: var(--text-dim);
  font-size: 12.5px;
  font-variant-numeric: tabular-nums;
  cursor: pointer;
  padding: 0 4px;
  user-select: none;
}
.chapter-chip:hover { border-color: var(--text-faint); }

.chapter-chip.is-owned {
  background: var(--green-bg);
  border-color: var(--green-border);
  color: var(--green);
}
.chapter-chip.is-selected {
  background: var(--accent-bg);
  border-color: var(--accent);
  color: var(--accent-strong);
  font-weight: 600;
}
.chapter-chip.is-locked {
  background: var(--amber-bg);
  border-color: var(--amber-border);
  color: var(--amber);
  cursor: not-allowed;
}

/* Sticky footer bar for series page */

.queue-bar {
  position: sticky;
  bottom: 0;
  z-index: 30;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  flex-wrap: wrap;
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 12px 16px;
  margin-top: 18px;
}

.queue-bar-summary { font-size: 13.5px; color: var(--text-dim); }
.queue-bar-summary strong { color: var(--text); font-weight: 600; }

.queue-feedback {
  font-size: 13px;
  color: var(--text-dim);
  margin-top: 8px;
}
.queue-feedback.is-error { color: var(--red); }
.queue-feedback.is-success { color: var(--green); }

/* =========================================================================
   Jobs page
   ========================================================================= */

.jobs-section { margin-bottom: 26px; }
.jobs-section h2 {
  font-size: 13px;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--text-faint);
  margin-bottom: 10px;
}
.jobs-list {
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 4px 16px;
}

/* =========================================================================
   Responsive
   ========================================================================= */

@media (max-width: 480px) {
  .page { padding: 14px 12px 32px; }
  .series-header { gap: 12px; }
  .series-header-cover { width: 76px; }
  .series-header-info h1 { font-size: 17px; }
  .toolbar-range { width: 100%; }
  .field-number { width: 68px; }
  .queue-bar { flex-direction: column; align-items: stretch; }
  .queue-bar .btn { width: 100%; }
  .series-grid { grid-template-columns: repeat(auto-fill, minmax(120px, 1fr)); gap: 10px; }
}

/* =========================================================================
   Jobs page -- inline empty states, small extras
   ========================================================================= */

.jobs-empty {
  color: var(--text-faint);
  font-size: 13px;
  padding: 14px 0;
  text-align: center;
}

.series-card-title .owned-tag {
  margin-left: 6px;
  vertical-align: middle;
}

.jobs-section-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}
.jobs-section-head h2 { margin-bottom: 0; }
