:root {
  --bg: #f4f4f6;
  --surface: #ffffff;
  --surface-2: #ececf0;
  --border: #dcdce2;
  --text: #17171c;
  --muted: #6b6b78;
  --accent: #6d4aff;
  --accent-fg: #ffffff;
  --accent-soft: #ece7ff;
  --new: #e5484d;
  --ok: #2f9e6a;
  --warn: #c27c0e;
  --radius: 10px;
  --shadow: 0 1px 2px rgb(0 0 0 / 6%), 0 4px 16px rgb(0 0 0 / 5%);
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #111216;
    --surface: #1a1b21;
    --surface-2: #24252d;
    --border: #2e2f38;
    --text: #ececf1;
    --muted: #9696a3;
    --accent: #8b6dff;
    --accent-fg: #ffffff;
    --accent-soft: #2a2245;
    --new: #ff6369;
    --ok: #4cc38a;
    --warn: #f1a10d;
    --shadow: 0 1px 2px rgb(0 0 0 / 30%), 0 4px 16px rgb(0 0 0 / 25%);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}
a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }
h1 { font-size: 1.5rem; margin: 0 0 1rem; }
h2 { font-size: 1.05rem; margin: 0 0 .75rem; }
code { font-size: .9em; }
[hidden] { display: none !important; }

.container { max-width: 1280px; margin: 0 auto; padding: 1rem 16px 4rem; }
.muted { color: var(--muted); }
.small { font-size: .85rem; }
.center { text-align: center; }
.warn { color: var(--warn); }
.danger { color: var(--new) !important; }

/* ---------------------------------------------------------------- top bar */
.topbar {
  position: sticky; top: 0; z-index: 10;
  display: flex; align-items: center; justify-content: space-between; gap: 1rem;
  padding: .6rem 16px;
  background: color-mix(in srgb, var(--surface) 88%, transparent);
  backdrop-filter: blur(8px);
  border-bottom: 1px solid var(--border);
}
.brand { display: flex; align-items: center; gap: .5rem; font-weight: 700; color: var(--text); }
.brand:hover { text-decoration: none; }
.topbar nav { display: flex; align-items: center; gap: 1rem; }
.topbar nav a { color: var(--muted); font-weight: 500; }
.topbar nav a[aria-current] { color: var(--text); }
.topbar nav form { margin: 0; }
@media (max-width: 520px) { .brand span, .brand { font-size: 0; gap: 0; } .brand img { width: 28px; height: 28px; } }

/* ---------------------------------------------------------------- controls */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: .4rem;
  padding: .5rem .9rem;
  border: 1px solid var(--border); border-radius: 8px;
  background: var(--surface); color: var(--text);
  font: inherit; font-weight: 500; cursor: pointer; white-space: nowrap;
}
.btn:hover { background: var(--surface-2); text-decoration: none; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-fg); }
.btn.primary:hover { filter: brightness(1.08); }
.btn.danger { border-color: var(--new); }
.btn:disabled { opacity: .5; cursor: default; }
.linklike { background: none; border: 0; padding: 0; color: var(--muted); font: inherit; cursor: pointer; }
.linklike:hover { color: var(--text); }
.icon { width: 16px; height: 16px; fill: none; stroke: currentColor; stroke-width: 2.2; stroke-linecap: round; }

input, select, textarea {
  font: inherit; color: var(--text);
  background: var(--surface); border: 1px solid var(--border); border-radius: 8px;
  padding: .5rem .65rem; width: 100%;
}
input:focus, select:focus, textarea:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
input[type=checkbox], input[type=radio] { width: auto; accent-color: var(--accent); }
input.narrow { width: 7rem; }
label { display: grid; gap: .3rem; font-weight: 500; }
label small, .form small { font-weight: 400; color: var(--muted); }
.check, .radio { display: flex; align-items: center; gap: .5rem; font-weight: 400; }
fieldset { border: 1px solid var(--border); border-radius: var(--radius); padding: .75rem 1rem; display: grid; gap: .6rem; margin: 0; }
legend { font-weight: 500; padding: 0 .3rem; }

.panel { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 1.1rem; box-shadow: var(--shadow); }
.form { display: grid; gap: 1rem; max-width: 560px; }
.inline-form { display: flex; flex-wrap: wrap; align-items: end; gap: .5rem; margin-top: 1rem; }
.inline-form label { display: flex; flex-wrap: wrap; align-items: center; gap: .5rem; font-weight: 400; }
.inline-form input[type=url], .inline-form input[type=file] { flex: 1; min-width: 200px; }
.button-row { display: flex; flex-wrap: wrap; gap: .5rem; }
.button-row form { margin: 0; }

.flash { padding: .65rem .9rem; border-radius: 8px; margin-bottom: 1rem; background: var(--accent-soft); }
.flash-error { background: color-mix(in srgb, var(--new) 16%, var(--surface)); }
.flash-success { background: color-mix(in srgb, var(--ok) 16%, var(--surface)); }

.spinner {
  display: inline-block; width: 14px; height: 14px; vertical-align: -2px;
  border: 2px solid currentColor; border-right-color: transparent; border-radius: 50%;
  animation: spin .7s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ---------------------------------------------------------------- toolbar */
.toolbar { display: grid; gap: .75rem; margin-bottom: 1.25rem; }
.toolbar-row { display: flex; flex-wrap: wrap; align-items: center; gap: .6rem; }
.toolbar-row .btn:last-child { margin-left: auto; }
#refresh-status { color: var(--muted); font-size: .9rem; }
.filters input[type=search] { flex: 1; min-width: 160px; max-width: 320px; }
.filters select { width: auto; }
.tabs { display: flex; gap: .25rem; overflow-x: auto; scrollbar-width: none; border-bottom: 1px solid var(--border); }
.tabs button {
  background: none; border: 0; border-bottom: 2px solid transparent;
  padding: .45rem .7rem; color: var(--muted); font: inherit; font-weight: 500; cursor: pointer; white-space: nowrap;
}
.tabs button span { font-size: .78rem; opacity: .75; }
.tabs button[aria-selected=true] { color: var(--text); border-bottom-color: var(--accent); }

/* ---------------------------------------------------------------- cards */
.grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(320px, 1fr)); gap: .9rem; }
@media (max-width: 400px) { .grid { grid-template-columns: 1fr; } }
.empty { grid-column: 1 / -1; text-align: center; padding: 3rem 1rem; color: var(--muted); }

.card {
  display: flex; flex-direction: column;
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  box-shadow: var(--shadow); overflow: hidden;
  transition: border-color .15s, transform .15s;
}
.card:hover { border-color: color-mix(in srgb, var(--accent) 50%, var(--border)); }
.card.has-new { border-color: color-mix(in srgb, var(--new) 55%, var(--border)); }
.card-main { display: flex; gap: .85rem; padding: .75rem; color: inherit; flex: 1; }
.card-main:hover { text-decoration: none; }
.card-main:hover h2 { color: var(--accent); }
.card-body { min-width: 0; display: flex; flex-direction: column; gap: .3rem; }
.card h2 {
  font-size: 1rem; margin: 0; line-height: 1.3;
  display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden;
}
.progress { margin: 0; color: var(--muted); font-variant-numeric: tabular-nums; }
.progress strong { color: var(--text); }
.progress .sep { opacity: .5; margin: 0 .15rem; }
.progress.big { font-size: 1.1rem; margin-bottom: .9rem; }
.next { margin: auto 0 0; font-size: .88rem; font-weight: 600; color: var(--accent); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.next.done { color: var(--ok); }

.cover { position: relative; flex: none; width: 78px; aspect-ratio: 3 / 4; border-radius: 6px; overflow: hidden; background: var(--surface-2); }
.cover img { width: 100%; height: 100%; object-fit: cover; display: block; }
.cover-ph { display: grid; place-items: center; height: 100%; font-size: 1.8rem; font-weight: 700; color: var(--muted); }
.cover.large { width: 160px; }
.badge {
  position: absolute; top: 4px; right: 4px;
  background: var(--new); color: #fff; font-size: .75rem; font-weight: 700;
  padding: .05rem .4rem; border-radius: 999px;
}
.badge.inline { position: static; margin-left: .4rem; vertical-align: 2px; }

.card-foot {
  display: flex; align-items: center; justify-content: space-between; gap: .5rem;
  padding: .3rem .5rem .3rem .75rem; border-top: 1px solid var(--border); background: var(--surface-2);
}
.card-foot .meta { font-size: .78rem; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.actions { display: flex; gap: .15rem; flex: none; }
.icon-btn {
  display: grid; place-items: center; width: 32px; height: 32px;
  background: none; border: 0; border-radius: 6px; color: var(--muted);
  font-size: 1rem; cursor: pointer;
}
.icon-btn:hover:not(:disabled) { background: var(--surface); color: var(--text); text-decoration: none; }
.icon-btn:disabled { opacity: .35; cursor: default; }
.spin-on-request.htmx-request { animation: spin .8s linear infinite; pointer-events: none; }

/* ---------------------------------------------------------------- detail */
.back { display: inline-block; margin-bottom: 1rem; color: var(--muted); }
.detail-head { display: flex; gap: 1.25rem; margin-bottom: 1.5rem; }
.detail-info { min-width: 0; flex: 1; }
@media (max-width: 600px) { .detail-head { flex-direction: column; } .cover.large { width: 120px; } }
.detail-grid { display: grid; grid-template-columns: minmax(0, 3fr) minmax(0, 2fr); gap: 1rem; align-items: start; }
@media (max-width: 860px) { .detail-grid { grid-template-columns: 1fr; } }
.stack { display: grid; gap: 1rem; }
.chapter-list { max-height: 70vh; overflow-y: auto; margin: 0 -.4rem; }
.chapter-row { display: grid; grid-template-columns: 1fr auto auto; gap: .75rem; align-items: center; padding: .4rem; border-radius: 6px; }
.chapter-row:hover { background: var(--surface-2); }
.chapter-row form { margin: 0; }
.chapter-row.read a { color: var(--muted); }
@media (max-width: 520px) { .chapter-row { grid-template-columns: 1fr auto; } .chapter-row > span { display: none; } }
.sources, .history { list-style: none; padding: 0; margin: 0; display: grid; gap: .75rem; }
.sources a { word-break: break-all; }
.sources form { margin: .25rem 0 0; }

.table { width: 100%; border-collapse: collapse; font-size: .9rem; }
.table th, .table td { text-align: left; padding: .45rem .4rem; border-bottom: 1px solid var(--border); vertical-align: top; }
.table th { color: var(--muted); font-weight: 500; }
details textarea { margin-top: .5rem; font-family: ui-monospace, monospace; font-size: .8rem; }

/* ---------------------------------------------------------------- login / quick */
.login-wrap { min-height: 100vh; display: grid; place-items: center; padding: 16px; }
.login { width: 100%; max-width: 340px; display: grid; gap: 1rem; text-align: center; }
.login label { text-align: left; }
.login h1 { margin: 0; }
.login img { margin: 0 auto; }
.quick { max-width: 480px; margin: 2rem auto; }

/* ---------------------------------------------------------------- toasts */
#toasts { position: fixed; left: 50%; bottom: 1rem; transform: translateX(-50%); display: grid; gap: .5rem; z-index: 50; width: min(92vw, 440px); }
.toast {
  display: flex; align-items: center; justify-content: space-between; gap: 1rem;
  padding: .7rem .9rem; border-radius: 8px;
  background: var(--text); color: var(--bg); box-shadow: var(--shadow);
  animation: toast-in .2s ease-out; transition: opacity .4s, transform .4s;
}
.toast.out { opacity: 0; transform: translateY(6px); }
.toast-error { background: var(--new); color: #fff; }
.toast button { background: none; border: 0; color: var(--accent-soft); font: inherit; font-weight: 700; cursor: pointer; text-transform: uppercase; font-size: .8rem; }
@media (prefers-color-scheme: dark) { .toast button { color: var(--accent); } }
@keyframes toast-in { from { opacity: 0; transform: translateY(8px); } }
