:root {
  color-scheme: light dark;
  --ink: #222a35;
  --muted: #5c6673;
  --faint: #8a929c;
  --line: #e8e4dc;
  --line-strong: #d6d1c7;
  --accent: #0a5ecb;
  --brand: #ff3300;
  --brand-strong: #d92b00; /* Filled buttons: keeps white text above 4.5:1. */
  --on-brand: #fff;
  --danger: #c2362c;
  --success: #1f7a4d;
  --bg-card: #fff;
  --bg-page: #fbf6ec;
  --bg-hover: #f7f4ee;
  --bg-subtle: #f3efe7;
  --focus: #0a5ecb;
  --shadow: 0 1px 2px rgba(34, 42, 53, 0.04), 0 1px 3px rgba(34, 42, 53, 0.06);

  --toast-bg: #1c1c1e;
  --toast-fg: #fff;
  --error-bg: #fdf3f2;

  --radius: 12px;
  --radius-sm: 8px;
}

/* Explicit dark preference, from the account setting. */
:root[data-theme="light"] { color-scheme: light; }
:root[data-theme="dark"] {
  color-scheme: dark;
  --ink: #f0f0f2;
  --muted: #a3acb8;
  --faint: #7d8691;
  --line: #2c3138;
  --line-strong: #3a4048;
  --accent: #5b9df0;
  --brand: #ff5533;
  --brand-strong: #ff5533;
  --on-brand: #14171b;
  --danger: #ff7a70;
  --success: #5fcf95;
  --bg-card: #1c1f24;
  --bg-page: #14171b;
  --bg-hover: #23272d;
  --bg-subtle: #262a31;
  --focus: #5b9df0;
  --shadow: none;

  --toast-bg: #2c2c30;
  --toast-fg: #f0f0f2;
  --error-bg: #2e1e1d;
}

/* System preference, only when no explicit choice has been made. */
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]):not([data-theme="dark"]) {
    color-scheme: dark;
    --ink: #f0f0f2;
    --muted: #a3acb8;
    --faint: #7d8691;
    --line: #2c3138;
    --line-strong: #3a4048;
    --accent: #5b9df0;
    --brand: #ff5533;
    --brand-strong: #ff5533;
    --on-brand: #14171b;
    --danger: #ff7a70;
    --success: #5fcf95;
    --bg-card: #1c1f24;
    --bg-page: #14171b;
    --bg-hover: #23272d;
    --bg-subtle: #262a31;
    --focus: #5b9df0;
    --shadow: none;

    --toast-bg: #2c2c30;
    --toast-fg: #f0f0f2;
    --error-bg: #2e1e1d;
  }
}

* { box-sizing: border-box; }

body {
  font-family: 'Rubik', -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  font-size: 15px;
  line-height: 1.5;
  background: var(--bg-page);
  color: var(--ink);
  margin: 0;
  padding: 24px 16px 64px;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-rendering: optimizeLegibility;
}

.wrap { max-width: 960px; margin: 0 auto; }
.page-narrow { max-width: 640px; }
.wrap-narrow { max-width: 380px; margin: 12vh auto 0; }

h1 { font-size: 24px; font-weight: 600; line-height: 1.25; margin: 0 0 20px; letter-spacing: -0.01em; }
h2 { font-size: 16px; font-weight: 600; margin: 0 0 4px; }
p { margin: 0 0 12px; }
a { color: var(--accent); }

:focus-visible { outline: 2px solid var(--focus); outline-offset: 2px; }

.sr-only {
  position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px;
  overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; border: 0;
}

/* Header */

.nav {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 12px 24px;
  margin-bottom: 32px;
}
.brand {
  display: inline-flex; align-items: center; gap: 8px;
  font-weight: 700; font-size: 17px; color: var(--ink); text-decoration: none;
}
.brand svg { width: 24px; height: 24px; flex-shrink: 0; color: var(--brand); }
.nav-links { display: flex; align-items: center; gap: 2px; font-size: 14px; }
.nav-links a, .nav-links .link-btn {
  display: inline-block; padding: 6px 12px; border-radius: var(--radius-sm);
  color: var(--muted); text-decoration: none; font-weight: 500;
}
.nav-links a:hover, .nav-links .link-btn:hover { color: var(--ink); background: var(--bg-subtle); }
.nav-links a.active { color: var(--ink); background: var(--bg-subtle); }

.link-btn {
  background: none; border: none; padding: 0; font: inherit;
  color: var(--muted); cursor: pointer; text-decoration: none;
}
.link-btn:hover { color: var(--ink); }

/* Toast */

.toast {
  position: fixed;
  bottom: 20px;
  right: 20px;
  max-width: calc(100vw - 40px);
  background: var(--toast-bg);
  color: var(--toast-fg);
  padding: 10px 16px;
  border-radius: var(--radius-sm);
  font-size: 14px;
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.18);
  z-index: 1000;
  opacity: 0;
  transform: translateY(8px);
  animation: toast-in 0.25s ease forwards, toast-out 0.3s ease forwards 2.7s;
}
.toast-error { background: var(--danger); color: #fff; }

@keyframes toast-in {
  to { opacity: 1; transform: translateY(0); }
}
@keyframes toast-out {
  to { opacity: 0; transform: translateY(8px); }
}

/* Empty states */

.empty { color: var(--muted); text-align: center; padding: 56px 16px; font-size: 14px; }
.empty-icon { width: 32px; height: 32px; color: var(--faint); margin-bottom: 8px; }
.empty-title { font-size: 16px; font-weight: 500; color: var(--ink); margin: 0 0 4px; }
.empty p { margin: 0; }
.empty .btn { margin-top: 16px; }

/* Tabs: radios first, then the label strip, then the panels, so plain CSS can
   link each checked radio to both its label and its panel. */

.tabs input[type=radio] {
  position: absolute;
  clip: rect(0 0 0 0);
  clip-path: inset(50%);
  width: 1px; height: 1px;
  overflow: hidden;
  white-space: nowrap;
}

.tab-list {
  display: flex;
  gap: 4px;
  border-bottom: 1px solid var(--line-strong);
  margin-bottom: 20px;
  overflow-x: auto;
  scrollbar-width: none;
}
.tab-list::-webkit-scrollbar { display: none; }

.tab-label {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 10px 12px 12px;
  margin-bottom: -1px;
  font-size: 15px;
  font-weight: 500;
  color: var(--muted);
  cursor: pointer;
  user-select: none;
  white-space: nowrap;
  border-bottom: 2px solid transparent;
  transition: color 0.12s ease, border-color 0.12s ease;
}
.tab-label:hover { color: var(--ink); border-bottom-color: var(--line-strong); }

.tab-icon { width: 18px; height: 18px; flex-shrink: 0; }

.tab-count {
  min-width: 22px; padding: 1px 7px; border-radius: 999px;
  font-size: 12px; font-weight: 500; line-height: 18px; text-align: center;
  background: var(--bg-subtle); color: var(--muted);
  font-variant-numeric: tabular-nums;
}
.tab-count.is-alert { background: var(--danger); color: #fff; }

.tab-panel { display: none; }

#tab-new:checked ~ .tab-list [for=tab-new],
#tab-downloaded:checked ~ .tab-list [for=tab-downloaded],
#tab-pending:checked ~ .tab-list [for=tab-pending],
#tab-read:checked ~ .tab-list [for=tab-read],
#tab-archived:checked ~ .tab-list [for=tab-archived],
#tab-favorites:checked ~ .tab-list [for=tab-favorites] {
  color: var(--ink);
  border-bottom-color: var(--brand);
}
#tab-new:checked ~ .tab-list [for=tab-new] .tab-count,
#tab-downloaded:checked ~ .tab-list [for=tab-downloaded] .tab-count,
#tab-pending:checked ~ .tab-list [for=tab-pending] .tab-count,
#tab-read:checked ~ .tab-list [for=tab-read] .tab-count,
#tab-archived:checked ~ .tab-list [for=tab-archived] .tab-count,
#tab-favorites:checked ~ .tab-list [for=tab-favorites] .tab-count {
  background: var(--ink); color: var(--bg-card);
}

#tab-new:focus-visible ~ .tab-list [for=tab-new],
#tab-downloaded:focus-visible ~ .tab-list [for=tab-downloaded],
#tab-pending:focus-visible ~ .tab-list [for=tab-pending],
#tab-read:focus-visible ~ .tab-list [for=tab-read],
#tab-archived:focus-visible ~ .tab-list [for=tab-archived],
#tab-favorites:focus-visible ~ .tab-list [for=tab-favorites] {
  outline: 2px solid var(--focus); outline-offset: -2px; border-radius: var(--radius-sm) var(--radius-sm) 0 0;
}

#tab-new:checked ~ .tab-panels #panel-new,
#tab-downloaded:checked ~ .tab-panels #panel-downloaded,
#tab-pending:checked ~ .tab-panels #panel-pending,
#tab-read:checked ~ .tab-panels #panel-read,
#tab-archived:checked ~ .tab-panels #panel-archived,
#tab-favorites:checked ~ .tab-panels #panel-favorites {
  display: block;
}

/* Lists */

.list {
  background: var(--bg-card);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  overflow: hidden;
  margin-bottom: 28px;
}
.item {
  border-bottom: 1px solid var(--line);
  padding: 16px 20px;
  display: flex;
  align-items: center;
  gap: 16px;
  transition: background 0.12s ease;
}
.item:last-child { border-bottom: none; }
.item:hover { background: var(--bg-hover); }
.item.is-error { align-items: flex-start; }

.item-thumb {
  width: 44px; height: 44px; border-radius: 10px; flex-shrink: 0;
  display: flex; align-items: center; justify-content: center;
  background: var(--bg-subtle); color: var(--faint);
  overflow: hidden;
}
.item-thumb svg { width: 22px; height: 22px; }
.source-logo-img { width: 100%; height: 100%; object-fit: cover; }

.info { flex: 1; min-width: 0; }
.title {
  font-size: 17px; font-weight: 500; line-height: 1.35; color: var(--ink);
  overflow-wrap: anywhere;
}
.subinfo {
  display: flex; flex-wrap: wrap; align-items: center; column-gap: 6px; row-gap: 2px;
  font-size: 14px; color: var(--muted); margin-top: 3px;
}
.subinfo .sep { color: var(--faint); }
.external-link {
  display: inline-flex; align-items: center; gap: 2px;
  color: var(--muted); text-decoration: none;
}
.external-link:hover { color: var(--accent); text-decoration: underline; }
.external-link svg { width: 14px; height: 14px; opacity: 0.7; }

.chips { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 6px; }
.chip {
  font-size: 13px; line-height: 20px; padding: 1px 8px; border-radius: 6px;
  background: var(--bg-subtle); color: var(--muted);
}

.badge {
  display: inline-flex; align-items: center; gap: 4px;
  font-size: 12px; font-weight: 500; line-height: 18px;
  padding: 1px 8px; border-radius: 999px;
  background: var(--bg-subtle); color: var(--muted);
}
.badge.is-error { background: var(--error-bg); color: var(--danger); }
.badge.is-pending::before {
  content: ""; width: 6px; height: 6px; border-radius: 50%; background: currentColor;
  animation: pulse 1.4s ease-in-out infinite;
}
@keyframes pulse { 50% { opacity: 0.3; } }

.error-message {
  margin: 10px 0 0; font-size: 12px; line-height: 1.5; color: var(--danger);
  background: var(--error-bg); border-radius: var(--radius-sm); padding: 10px 12px;
  white-space: pre-wrap; overflow-wrap: anywhere; max-height: 120px; overflow-y: auto;
}

/* Row actions */

.actions { display: flex; gap: 2px; flex-shrink: 0; flex-wrap: wrap; align-items: center; justify-content: flex-end; }
.actions-divider { width: 1px; height: 20px; align-self: center; background: var(--line-strong); margin: 0 6px; }

/* With a real mouse, keep rows calm and reveal actions on hover or keyboard focus.
   Touch screens have no hover, so there the actions are always shown. */
@media (hover: hover) and (pointer: fine) {
  .item .actions > * { opacity: 0; transition: opacity 0.12s ease; }
  .item:hover .actions > *, .item:focus-within .actions > * { opacity: 1; }
}

.btn-icon {
  display: inline-flex; align-items: center; justify-content: center;
  width: 36px; height: 36px; padding: 0; border-radius: var(--radius-sm); border: none;
  background: transparent; color: var(--muted); cursor: pointer; text-decoration: none;
  transition: background 0.12s ease, color 0.12s ease;
}
.btn-icon svg { width: 20px; height: 20px; }
.btn-icon:hover { background: var(--bg-subtle); color: var(--ink); }
.btn-icon.danger:hover { color: var(--danger); background: var(--error-bg); }
.btn-icon.favorite:hover { color: var(--brand); }
.btn-icon.favorite.is-active { color: var(--brand); }

/* Buttons */

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  height: 38px; padding: 0 16px; border-radius: var(--radius-sm);
  border: 1px solid var(--line-strong); background: var(--bg-card); color: var(--ink);
  font: inherit; font-size: 14px; font-weight: 500; line-height: 1;
  cursor: pointer; text-decoration: none; white-space: nowrap;
  transition: background 0.12s ease, border-color 0.12s ease, filter 0.12s ease;
}
.btn:hover { background: var(--bg-hover); border-color: var(--faint); }
.btn.primary { background: var(--brand-strong); border-color: var(--brand-strong); color: var(--on-brand); }
.btn.primary:hover { filter: brightness(0.93); }
.btn.danger { color: var(--danger); }
.btn.danger:hover { background: var(--error-bg); border-color: var(--danger); }
.btn.block { width: 100%; }

form.inline { display: inline; }

/* Cards & settings */

.card {
  background: var(--bg-card);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 24px;
  margin-bottom: 16px;
}
.card-header { margin-bottom: 16px; }
.card-header p { margin: 0; }
.muted { color: var(--muted); font-size: 14px; }

.details { margin: 0; display: grid; grid-template-columns: max-content 1fr; gap: 8px 24px; font-size: 15px; }
.details dt { color: var(--muted); }
.details dd { margin: 0; overflow-wrap: anywhere; }

/* Forms */

.edit-form, .login-card form { display: flex; flex-direction: column; gap: 18px; }
.field { display: flex; flex-direction: column; gap: 6px; }
.field > label, .field-label {
  font-size: 14px; color: var(--ink); font-weight: 500;
}
.edit-form input[type=text], .edit-form input[type=password], .edit-form textarea, .edit-form select,
.login-card input[type=text], .login-card input[type=password], .token-row input {
  width: 100%; height: 40px; padding: 0 12px; font-size: 15px;
  border: 1px solid var(--line-strong); border-radius: var(--radius-sm);
  background: var(--bg-card); color: var(--ink); font-family: inherit;
  transition: border-color 0.12s ease, box-shadow 0.12s ease;
}
.edit-form textarea { height: auto; padding: 10px 12px; resize: vertical; min-height: 96px; line-height: 1.5; }
.edit-form input:focus, .edit-form textarea:focus, .login-card input:focus, .token-row input:focus,
.source-select-trigger:focus-visible {
  outline: none; border-color: var(--focus);
  box-shadow: 0 0 0 3px color-mix(in srgb, var(--focus) 25%, transparent);
}
.edit-form input[readonly] { background: var(--bg-subtle); color: var(--muted); }
.edit-form input[type=file] { font-size: 14px; color: var(--muted); }
.edit-form input[type=file]::file-selector-button {
  font: inherit; font-size: 14px; font-weight: 500; margin-right: 12px;
  height: 34px; padding: 0 12px; border-radius: var(--radius-sm);
  border: 1px solid var(--line-strong); background: var(--bg-card); color: var(--ink); cursor: pointer;
}
.field-hint { font-size: 13px; color: var(--muted); margin: 0; }
.read-status.is-busy { color: var(--ink); }
.read-status.is-ok { color: var(--success); }
.read-status.is-warn { color: var(--ink); }
.read-status.is-error { color: var(--danger); }

.check { display: inline-flex; align-items: center; gap: 8px; font-size: 14px; cursor: pointer; }
.check input { width: 16px; height: 16px; margin: 0; accent-color: var(--brand-strong); }

.logo-preview { display: flex; align-items: center; gap: 12px; margin-bottom: 4px; }
.logo-preview img {
  width: 44px; height: 44px; border-radius: 10px; object-fit: cover; border: 1px solid var(--line);
}

.form-actions {
  display: flex; flex-wrap: wrap; gap: 8px; align-items: center;
  padding-top: 18px; margin-top: 2px; border-top: 1px solid var(--line);
}

.field-label-row { display: flex; align-items: baseline; justify-content: space-between; gap: 12px; }
.text-link {
  background: none; border: none; padding: 0; font: inherit; font-size: 14px; font-weight: 500;
  color: var(--accent); cursor: pointer;
}
.text-link:hover { text-decoration: underline; }

/* Modal (native <dialog>) */

.modal {
  width: min(480px, calc(100vw - 32px));
  max-height: calc(100vh - 32px);
  padding: 0; /* padding lives on the form, so clicks on the dialog itself mean the backdrop */
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--bg-card);
  color: var(--ink);
  box-shadow: 0 24px 64px rgba(0, 0, 0, 0.28);
}
.modal::backdrop { background: rgba(20, 23, 27, 0.5); }
.modal > form { padding: 24px; }
.modal-header { display: flex; align-items: center; justify-content: space-between; margin: -6px -10px -6px 0; }
.modal-header h2 { margin: 0; font-size: 18px; }

/* Source picker */

.source-select { position: relative; }
.source-select-trigger {
  width: 100%; height: 40px; display: flex; align-items: center; gap: 10px;
  padding: 0 12px; font-size: 15px; font-family: inherit; text-align: left;
  border: 1px solid var(--line-strong); border-radius: var(--radius-sm); background: var(--bg-card); color: var(--ink);
  cursor: pointer;
}
.source-select-trigger:hover { border-color: var(--faint); }
.source-select-trigger[aria-expanded="true"] { border-color: var(--focus); }
.source-select-label { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.source-select-chevron { width: 16px; height: 16px; flex-shrink: 0; color: var(--muted); }

.source-select-icon {
  width: 22px; height: 22px; flex-shrink: 0; border-radius: 6px; overflow: hidden;
  display: flex; align-items: center; justify-content: center;
  background: var(--bg-subtle); color: var(--muted);
}
.source-select-icon:empty { background: transparent; }
.source-select-icon img { width: 100%; height: 100%; object-fit: cover; }
.source-select-icon svg { width: 14px; height: 14px; }

.source-select-options {
  position: absolute; z-index: 10; top: calc(100% + 4px); left: 0; right: 0;
  max-height: 280px; overflow-y: auto; margin: 0; padding: 4px; list-style: none;
  background: var(--bg-card); border: 1px solid var(--line-strong); border-radius: var(--radius-sm);
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.12);
}
.source-select-option {
  display: flex; align-items: center; gap: 10px;
  padding: 8px; border-radius: 6px; cursor: pointer; font-size: 15px;
}
.source-select-option:hover, .source-select-option:focus { background: var(--bg-hover); outline: none; }
.source-select-option.is-selected { background: var(--bg-subtle); font-weight: 500; }

/* Page header */

.section-header {
  display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: 12px;
  margin-bottom: 20px;
}
.section-header h1 { margin: 0; }
.page-subtitle { margin: 4px 0 0; }
.back-link {
  display: inline-flex; align-items: center; gap: 4px; margin-bottom: 12px;
  font-size: 14px; color: var(--muted); text-decoration: none;
}
.back-link:hover { color: var(--ink); }
.back-link svg { width: 16px; height: 16px; }

/* Account */

.token-row { display: flex; gap: 8px; align-items: center; margin-bottom: 12px; }
.token-row input {
  flex: 1; min-width: 0; font-size: 14px;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  background: var(--bg-subtle);
}

.segmented {
  display: inline-flex; padding: 3px; gap: 2px; border-radius: 10px;
  background: var(--bg-subtle); border: 1px solid var(--line);
}
.segmented input { position: absolute; opacity: 0; width: 1px; height: 1px; }
.segmented label {
  padding: 6px 16px; border-radius: 7px; font-size: 14px; font-weight: 500;
  color: var(--muted); cursor: pointer; transition: background 0.12s ease, color 0.12s ease;
}
.segmented label:hover { color: var(--ink); }
.segmented input:checked + label { background: var(--bg-card); color: var(--ink); box-shadow: var(--shadow), 0 0 0 1px var(--line); }
.segmented input:focus-visible + label { outline: 2px solid var(--focus); outline-offset: 1px; }

/* Login */

.login-brand { display: flex; flex-direction: column; align-items: center; gap: 12px; margin-bottom: 24px; text-align: center; }
.login-brand svg { width: 40px; height: 40px; color: var(--brand); }
.login-brand h1 { margin: 0; font-size: 22px; }
.login-card { padding: 28px; }

/* Small screens */

@media (max-width: 640px) {
  body { padding-top: 16px; }
  h1 { font-size: 22px; }
  .nav { margin-bottom: 20px; }
  .nav-links { margin-left: -12px; }
  .item { flex-wrap: wrap; align-items: flex-start; padding: 14px 16px; gap: 12px 14px; }
  .item-thumb { width: 40px; height: 40px; }
  .title { font-size: 16px; }
  .item .actions { width: 100%; justify-content: flex-start; padding-left: 46px; margin: -6px 0 -6px -8px; }
  .card { padding: 20px; }
  .tab-label { padding-left: 10px; padding-right: 10px; }
  .tab-label .tab-icon { display: none; }
}
