/* ── Reset & Variables ─────────────────────────────────────────────────────── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --bg:        #0e0e12;
  --surface:   #16161e;
  --surface2:  #1e1e2a;
  --border:    #2a2a38;
  --text:      #e0e0ee;
  --text-dim:  #8888aa;
  --accent:    #7c6ff7;
  --accent-h:  #9d94ff;
  --success:   #4ade80;
  --error:     #f87171;
  --warn:      #facc15;
  --radius:    6px;
  --panel-w:   clamp(220px, 28vw, 340px);
  --topbar-h:  48px;
}

html, body { height: 100%; }
body {
  font-family: system-ui, -apple-system, sans-serif;
  font-size: 14px;
  background: var(--bg);
  color: var(--text);
  overflow: hidden;
}

/* ── Scrollbar ──────────────────────────────────────────────────────────────── */
::-webkit-scrollbar { width: 6px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--border); border-radius: 3px; }

/* ── Topbar ─────────────────────────────────────────────────────────────────── */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  height: var(--topbar-h);
  padding: 0 16px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  position: fixed;
  top: 0; left: 0; right: 0;
  z-index: 100;
}
.topbar-brand { font-weight: 700; font-size: 16px; letter-spacing: 0.5px; }
.topbar-sub   { color: var(--accent); font-weight: 400; margin-left: 4px; }
.topbar-right { display: flex; gap: 8px; }

/* ── App Layout ─────────────────────────────────────────────────────────────── */
/* Views clear the fixed topbar with padding, not a margin. A top margin
   collapses through <body> and leaves the page taller than the window; the
   scrollbar is hidden, but scrollIntoView() and focus can still scroll that
   range, sliding every panel header under the topbar with no way back. */
.app-layout {
  display: flex;
  height: 100vh;
  padding-top: var(--topbar-h);
  overflow: hidden;
}

/* ── Panels ─────────────────────────────────────────────────────────────────── */
.panel {
  display: flex;
  flex-direction: column;
  border-right: 1px solid var(--border);
  background: var(--surface);
  overflow: hidden;
}
.panel-manga    { width: var(--panel-w); flex-shrink: 0; }
.panel-chapters { flex: 1; min-width: 0; }
.panel-queue    { width: var(--panel-w); flex-shrink: 0; border-right: none; }

.panel-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 14px;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}
.panel-title { font-weight: 600; font-size: 13px; color: var(--text-dim); text-transform: uppercase; letter-spacing: 0.8px; }

.panel-search { padding: 8px 10px; flex-shrink: 0; border-bottom: 1px solid var(--border); }
.panel-search input {
  width: 100%;
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text);
  padding: 6px 10px;
  font-size: 13px;
  outline: none;
}
.panel-search input:focus { border-color: var(--accent); }

/* ── Manga loading bar ──────────────────────────────────────────────────────── */
.manga-loading-bar {
  height: 3px;
  background: var(--surface2);
  flex-shrink: 0;
}
.manga-loading-fill {
  height: 100%;
  background: var(--accent);
  transition: width 0.4s ease;
  width: 0%;
}

/* ── Item Lists ─────────────────────────────────────────────────────────────── */
.item-list {
  flex: 1;
  overflow-y: auto;
  list-style: none;
  padding: 4px 0;
}

.item-list li {
  display: flex;
  align-items: center;
  padding: 8px 14px;
  cursor: pointer;
  border-bottom: 1px solid transparent;
  transition: background 0.1s;
  user-select: none;
}
.item-list li:hover   { background: var(--surface2); }
.item-list li.active  { background: var(--surface2); color: var(--accent-h); }

.item-title { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* ── Selectable list (chapters) ─────────────────────────────────────────────── */
.selectable-list li {
  gap: 10px;
}
.selectable-list input[type="checkbox"] {
  accent-color: var(--accent);
  width: 15px; height: 15px;
  flex-shrink: 0;
  cursor: pointer;
}

/* ── Chapter actions ─────────────────────────────────────────────────────────── */
.chapter-actions { display: flex; gap: 6px; align-items: center; }

/* ── Downloaded badge ───────────────────────────────────────────────────────── */
.ch-downloaded {
  flex-shrink: 0;
  font-size: 11px;
  font-weight: 700;
  color: var(--success);
  margin-left: 4px;
  opacity: 0.85;
}

.is-downloaded .item-title {
  color: var(--text-dim);
}

/* ── Queue list ─────────────────────────────────────────────────────────────── */
.queue-list li {
  flex-direction: column;
  align-items: flex-start;
  gap: 4px;
  padding: 10px 14px;
  cursor: default;
}
.queue-item-title {
  font-size: 12px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  width: 100%;
}
.queue-item-manga { font-size: 11px; color: var(--text-dim); }
.queue-item-bar {
  width: 100%;
  height: 4px;
  background: var(--surface2);
  border-radius: 2px;
  overflow: hidden;
}
.queue-item-bar-fill {
  height: 100%;
  background: var(--accent);
  border-radius: 2px;
  transition: width 0.3s;
}
.queue-item-status {
  font-size: 11px;
  font-weight: 500;
}
.status-pending    { color: var(--text-dim); }
.status-downloading { color: var(--accent); }
.status-complete   { color: var(--success); }
.status-error      { color: var(--error); }
.status-cancelled  { color: var(--text-dim); }

.empty-msg, .loading-msg {
  padding: 24px 14px;
  color: var(--text-dim);
  font-size: 13px;
  text-align: center;
}

/* ── Buttons ─────────────────────────────────────────────────────────────────── */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 7px 14px;
  border-radius: var(--radius);
  border: none;
  cursor: pointer;
  font-size: 13px;
  font-weight: 500;
  transition: opacity 0.15s, background 0.15s;
  white-space: nowrap;
}
.btn:disabled { opacity: 0.4; cursor: not-allowed; }

.btn-primary  { background: var(--accent); color: #fff; }
.btn-primary:hover:not(:disabled) { background: var(--accent-h); }

.btn-secondary { background: var(--surface2); color: var(--text); border: 1px solid var(--border); }
.btn-secondary:hover:not(:disabled) { background: var(--border); }

.btn-ghost { background: transparent; color: var(--text-dim); border: 1px solid transparent; }
.btn-ghost:hover:not(:disabled) { background: var(--surface2); color: var(--text); }

.btn-full { width: 100%; }
.btn-sm   { padding: 4px 10px; font-size: 12px; }

/* ── Login Page ──────────────────────────────────────────────────────────────── */
.login-page {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 100vh;
  overflow: auto;
}
.login-box {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 40px 36px;
  width: 100%;
  max-width: 360px;
}
.login-logo {
  text-align: center;
  margin-bottom: 28px;
}
.logo-text { font-size: 26px; font-weight: 700; letter-spacing: 0.5px; }
.logo-sub  { color: var(--accent); font-size: 16px; margin-left: 6px; }

.field { margin-bottom: 18px; }
.field label {
  display: block;
  margin-bottom: 6px;
  font-size: 13px;
  color: var(--text-dim);
}
.field input {
  width: 100%;
  padding: 9px 12px;
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text);
  font-size: 14px;
  outline: none;
}
.field input:focus { border-color: var(--accent); }

.error-msg {
  color: var(--error);
  font-size: 13px;
  margin-bottom: 12px;
  padding: 8px 12px;
  background: rgba(248,113,113,0.1);
  border-radius: var(--radius);
  border: 1px solid rgba(248,113,113,0.3);
}

.hidden { display: none !important; }

/* ── Topbar nav ─────────────────────────────────────────────────────────────── */
.topbar-nav { display: flex; gap: 4px; }
.nav-btn {
  background: transparent;
  border: none;
  color: var(--text-dim);
  font-size: 13px;
  font-weight: 600;
  letter-spacing: 0.4px;
  padding: 6px 14px;
  border-radius: var(--radius);
  cursor: pointer;
  transition: background 0.15s, color 0.15s;
}
.nav-btn:hover { background: var(--surface2); color: var(--text); }
.nav-btn.is-active { background: var(--surface2); color: var(--accent-h); }

/* ── Metadata card ──────────────────────────────────────────────────────────── */
.meta-status {
  padding: 8px 14px;
  font-size: 12px;
  color: var(--text-dim);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}
.meta-status.is-error { color: var(--warn); }

.meta-card {
  display: flex;
  gap: 14px;
  padding: 14px;
  border-bottom: 1px solid var(--border);
  background: var(--surface2);
  flex-shrink: 0;
  max-height: 44vh;
  overflow-y: auto;
}
.meta-cover-wrap { flex-shrink: 0; width: 92px; }
.meta-cover {
  width: 92px;
  border-radius: var(--radius);
  display: block;
  background: var(--border);
  aspect-ratio: 2 / 3;
  object-fit: cover;
}
.meta-body { flex: 1; min-width: 0; }
.meta-head { display: flex; align-items: flex-start; justify-content: space-between; gap: 10px; }
.meta-head-text { min-width: 0; }
.meta-head-actions { display: flex; align-items: center; gap: 6px; flex-shrink: 0; }
.meta-title {
  font-size: 15px;
  font-weight: 650;
  line-height: 1.3;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.meta-native { font-size: 12px; color: var(--text-dim); margin-top: 2px; }

.meta-flag {
  font-size: 10px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  padding: 3px 7px;
  border-radius: 10px;
  white-space: nowrap;
}
.meta-flag.is-warn   { background: rgba(250,204,21,0.15); color: var(--warn); }
.meta-flag.is-manual { background: rgba(124,111,247,0.18); color: var(--accent-h); }

.meta-stats {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-top: 8px;
}
.meta-stat {
  font-size: 11px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 4px;
  padding: 2px 7px;
  color: var(--text-dim);
  white-space: nowrap;
}
.meta-stat b { color: var(--text); font-weight: 600; }
.meta-stat.is-score b { color: var(--success); }

.meta-tags { display: flex; flex-wrap: wrap; gap: 5px; margin-top: 8px; }
.meta-tag {
  font-size: 11px;
  padding: 2px 8px;
  border-radius: 10px;
  background: rgba(124,111,247,0.12);
  color: var(--accent-h);
  white-space: nowrap;
}

.meta-desc-wrap { margin-top: 8px; }
.meta-desc {
  font-size: 12.5px;
  line-height: 1.55;
  color: var(--text-dim);
  white-space: pre-wrap;
}
.meta-desc.is-clamped {
  display: -webkit-box;
  -webkit-line-clamp: 3;
  -webkit-box-orient: vertical;
  overflow: hidden;
}
.meta-more {
  background: none;
  border: none;
  color: var(--accent);
  font-size: 11.5px;
  cursor: pointer;
  padding: 3px 0 0;
}
.meta-more:hover { color: var(--accent-h); }

.meta-links { display: flex; flex-wrap: wrap; gap: 10px; margin-top: 9px; }
.meta-links a {
  font-size: 11.5px;
  color: var(--text-dim);
  text-decoration: none;
  border-bottom: 1px dotted var(--border);
}
.meta-links a:hover { color: var(--accent-h); border-bottom-color: var(--accent); }

/* ── Discover view ──────────────────────────────────────────────────────────── */
.discover {
  height: 100vh;
  padding-top: var(--topbar-h); /* padding, not margin: see .app-layout */
  display: flex;
  flex-direction: column;
  overflow: hidden;
}
.discover-bar {
  display: flex;
  align-items: center;
  gap: 12px;
  flex-wrap: wrap;
  padding: 10px 16px;
  border-bottom: 1px solid var(--border);
  background: var(--surface);
  flex-shrink: 0;
}
.seg {
  display: flex;
  gap: 2px;
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 2px;
}
.seg-scroll { overflow-x: auto; max-width: 100%; scrollbar-width: none; }
.seg-scroll::-webkit-scrollbar { display: none; }
.seg-btn {
  background: transparent;
  border: none;
  color: var(--text-dim);
  font-size: 12px;
  font-weight: 500;
  padding: 5px 11px;
  border-radius: 4px;
  cursor: pointer;
  white-space: nowrap;
  text-transform: capitalize;
  transition: background 0.12s, color 0.12s;
}
.seg-btn:hover { color: var(--text); }
.seg-btn.is-active { background: var(--accent); color: #fff; }

.discover-search { flex: 1; min-width: 180px; }
.discover-search input {
  width: 100%;
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text);
  padding: 6px 10px;
  font-size: 13px;
  outline: none;
}
.discover-search input:focus { border-color: var(--accent); }

.card-grid {
  flex: 1;
  overflow-y: auto;
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
  /* Cards clip their overflow, so their automatic minimum size is zero and
     auto rows will happily divide the container height between however many
     rows exist — squeezing every card flatter as more are loaded, until the
     title and date are clipped away. max-content sizes rows to the card and
     lets the grid scroll instead. */
  grid-auto-rows: max-content;
  gap: 16px;
  padding: 16px;
  align-content: start;
}
.card {
  cursor: pointer;
  border-radius: var(--radius);
  overflow: hidden;
  background: var(--surface);
  border: 1px solid var(--border);
  transition: transform 0.12s, border-color 0.12s;
  display: flex;
  flex-direction: column;
}
.card:hover { transform: translateY(-2px); border-color: var(--accent); }
.card-cover-wrap { position: relative; background: var(--surface2); aspect-ratio: 2 / 3; }
.card-cover { width: 100%; height: 100%; object-fit: cover; display: block; }
.card-score {
  position: absolute;
  top: 6px; right: 6px;
  background: rgba(14,14,18,0.85);
  color: var(--success);
  font-size: 11px;
  font-weight: 700;
  padding: 2px 6px;
  border-radius: 4px;
}
.card-rank {
  position: absolute;
  top: 6px; left: 6px;
  background: rgba(14,14,18,0.85);
  color: var(--accent-h);
  font-size: 11px;
  font-weight: 700;
  padding: 2px 6px;
  border-radius: 4px;
}
/* The leaders of a ranking earn a warmer badge. */
.card-rank.is-podium { color: #fbbf24; }
.card-body { padding: 8px 9px 10px; }
.card-title {
  font-size: 12.5px;
  font-weight: 550;
  line-height: 1.35;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}
.card-sub { font-size: 11px; color: var(--text-dim); margin-top: 3px; }

.discover-footer { padding: 0 16px 18px; text-align: center; flex-shrink: 0; }

/* ── Modal ──────────────────────────────────────────────────────────────────── */
.modal {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.65);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 200;
  padding: 20px;
}
.modal-box {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  width: 100%;
  max-width: 620px;
  max-height: 82vh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}
.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 13px 16px;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}
.modal-title { font-weight: 600; font-size: 14px; }
.modal-close {
  background: none;
  border: none;
  color: var(--text-dim);
  font-size: 22px;
  line-height: 1;
  cursor: pointer;
  padding: 0 4px;
}
.modal-close:hover { color: var(--text); }
.modal-body { padding: 14px 16px 18px; overflow-y: auto; }

.modal-search { display: flex; gap: 8px; margin-bottom: 12px; }
.modal-search input {
  flex: 1;
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text);
  padding: 8px 11px;
  font-size: 13px;
  outline: none;
}
.modal-search input:focus { border-color: var(--accent); }

.result-list { list-style: none; display: flex; flex-direction: column; gap: 8px; }
.result-item {
  display: flex;
  gap: 11px;
  padding: 9px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface2);
  align-items: center;
}
.result-item.is-current { border-color: var(--accent); }
.result-cover {
  width: 44px;
  aspect-ratio: 2 / 3;
  object-fit: cover;
  border-radius: 4px;
  flex-shrink: 0;
  background: var(--border);
}
.result-info { flex: 1; min-width: 0; }
.result-title {
  font-size: 13px;
  font-weight: 550;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.result-sub { font-size: 11px; color: var(--text-dim); margin-top: 3px; }
.result-actions { flex-shrink: 0; }

.modal-note { font-size: 12px; color: var(--text-dim); margin-bottom: 10px; line-height: 1.5; }
.modal-note.is-error { color: var(--warn); }

.detail-head { display: flex; gap: 14px; margin-bottom: 12px; }
.detail-cover { width: 110px; border-radius: var(--radius); flex-shrink: 0; background: var(--border); }
.detail-actions { display: flex; gap: 8px; flex-wrap: wrap; margin-top: 12px; }

/* ── Toast ──────────────────────────────────────────────────────────────────── */
.toast {
  position: fixed;
  left: 50%;
  bottom: 26px;
  transform: translateX(-50%);
  background: var(--surface2);
  border: 1px solid var(--border);
  color: var(--text);
  padding: 10px 18px;
  border-radius: 20px;
  font-size: 13px;
  z-index: 300;
  box-shadow: 0 6px 20px rgba(0,0,0,0.4);
  max-width: 80vw;
  text-align: center;
}
.toast.is-error { border-color: rgba(248,113,113,0.5); color: var(--error); }

/* ── Narrow screens ─────────────────────────────────────────────────────────── */
@media (max-width: 860px) {
  .meta-card { max-height: 52vh; }
  .card-grid { grid-template-columns: repeat(auto-fill, minmax(120px, 1fr)); gap: 12px; padding: 12px; }
}

.favourite-filters, .favourite-controls {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px;
  padding: 10px 14px;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}
.favourite-filters [aria-pressed="true"] { color: var(--accent-h); border-color: var(--accent); }
.favourite-controls label { font-size: 12px; display: flex; align-items: center; gap: 6px; }
.favourite-controls input { accent-color: var(--accent); }
.favourite-controls small { width: 100%; color: var(--text-dim); font-size: 11px; }
#favouriteStatus { color: var(--text-dim); font-size: 12px; overflow-wrap: anywhere; }
.panel-chapters > .panel-header { flex-wrap: wrap; gap: 8px; }
