:root {
  --bg: #f6f7fb;
  --card: #ffffff;
  --ink: #161a23;
  --muted: #667085;
  --line: #e4e7ee;
  --soft: #f1f3f8;
  --accent: #2f5bea;
  --accent-ink: #ffffff;
  --accent-soft: #e8eeff;
  --ok: #138a4d;
  --ok-soft: #e5f6ec;
  --warn: #a15c00;
  --warn-soft: #fff4e0;
  --bad: #c0362c;
  --bad-soft: #fdecea;
  --shadow: 0 1px 2px rgba(16, 24, 40, .05), 0 4px 16px rgba(16, 24, 40, .06);
  --radius: 14px;
  --serif: 'Source Serif 4', 'Iowan Old Style', Georgia, serif;
  --sans: Inter, system-ui, -apple-system, 'Segoe UI', Roboto, sans-serif;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1218; --card: #171b23; --ink: #e8ebf2; --muted: #9aa3b5; --line: #2a303c; --soft: #1e232d;
    --accent: #6d8dff; --accent-ink: #0b1020; --accent-soft: #1d2640;
    --ok: #4cc38a; --ok-soft: #12291f; --warn: #f0b35a; --warn-soft: #2e2413; --bad: #ff7a70; --bad-soft: #321a19;
    --shadow: 0 1px 2px rgba(0,0,0,.3), 0 6px 20px rgba(0,0,0,.25);
    color-scheme: dark;
  }
}
* { box-sizing: border-box; }
html, body { margin: 0; }
body { background: var(--bg); color: var(--ink); font: 15px/1.5 var(--sans); -webkit-font-smoothing: antialiased; }
[hidden] { display: none !important; }
button, input, select, textarea { font: inherit; color: inherit; }
.muted { color: var(--muted); }
.tiny { font-size: 12.5px; }
.error { color: var(--bad); min-height: 1.2em; margin: 8px 0 0; font-size: 14px; }

/* ---------- buttons ---------- */
.btn { border: 1px solid var(--line); background: var(--card); padding: 8px 14px; border-radius: 10px; cursor: pointer; font-weight: 500; white-space: nowrap; transition: background .15s, border-color .15s, transform .05s; }
.btn:hover { background: var(--soft); }
.btn:active { transform: translateY(1px); }
.btn:disabled { opacity: .5; cursor: not-allowed; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.btn.primary:hover { filter: brightness(1.07); }
.btn.big { padding: 12px 22px; font-size: 16px; font-weight: 600; }
.btn.wide { width: 100%; }
.btn.ghost { background: transparent; border-color: transparent; }
.btn.ghost:hover { background: var(--soft); }
.btn.icon { padding: 6px 8px; display: inline-flex; }
.btn.small { padding: 4px 10px; font-size: 13px; border-radius: 8px; }
.btn.danger { color: var(--bad); }
.link { background: none; border: 0; color: var(--accent); cursor: pointer; padding: 0; margin-top: 14px; }

/* ---------- auth ---------- */
.auth { min-height: 100vh; display: grid; place-items: center; padding: 16px; }
.auth-card { width: 100%; max-width: 380px; background: var(--card); padding: 28px; border-radius: var(--radius); box-shadow: var(--shadow); text-align: center; }
.auth-card form { text-align: left; display: grid; gap: 12px; margin-top: 16px; }
label { display: grid; gap: 5px; font-size: 13.5px; font-weight: 500; }
input, select, textarea { border: 1px solid var(--line); background: var(--card); border-radius: 9px; padding: 9px 11px; width: 100%; }
input:focus, select:focus, textarea:focus, .btn:focus-visible, .drop:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }
textarea { resize: vertical; line-height: 1.5; }

/* ---------- brand / topbar ---------- */
.brand { display: flex; align-items: center; gap: 10px; font-weight: 700; letter-spacing: -.01em; }
.brand.big { justify-content: center; font-size: 19px; }
.logo { width: 28px; height: 28px; border-radius: 8px; background: var(--accent) url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 32 32'%3E%3Cpath d='M8 9h7a3 3 0 0 1 3 3v12a2.5 2.5 0 0 0-2.5-2.5H8zM24 9h-4a3 3 0 0 0-3 3v12a2.5 2.5 0 0 1 2.5-2.5H24z' fill='white'/%3E%3C/svg%3E") center/100% no-repeat; flex: none; }
.topbar { position: sticky; top: 0; z-index: 10; display: flex; justify-content: space-between; align-items: center; padding: 10px 20px; background: color-mix(in srgb, var(--bg) 88%, transparent); backdrop-filter: blur(8px); border-bottom: 1px solid var(--line); }
.top-actions { display: flex; align-items: center; gap: 2px; position: relative; }
.menu { position: absolute; right: 0; top: calc(100% + 6px); background: var(--card); border: 1px solid var(--line); border-radius: 10px; box-shadow: var(--shadow); min-width: 190px; padding: 6px; z-index: 20; }
.menu button { display: block; width: 100%; text-align: left; background: none; border: 0; padding: 8px 10px; border-radius: 7px; cursor: pointer; }
.menu button:hover { background: var(--soft); }
.menu-email { padding: 6px 10px 8px; font-size: 13px; color: var(--muted); border-bottom: 1px solid var(--line); margin-bottom: 4px; overflow: hidden; text-overflow: ellipsis; }

/* ---------- layout ---------- */
.wrap { max-width: 900px; margin: 0 auto; padding: 22px 16px 80px; display: grid; gap: 18px; }
.card { background: var(--card); border-radius: var(--radius); box-shadow: var(--shadow); padding: 22px; min-width: 0; }
.card-head { display: flex; align-items: center; gap: 12px; margin-bottom: 4px; }
.title-input { border: 0; padding: 4px 0; font-weight: 600; font-size: 15px; background: transparent; border-bottom: 1px dashed transparent; border-radius: 0; }
.title-input:hover, .title-input:focus { border-bottom-color: var(--line); outline: none; }
.save-state { font-size: 12.5px; color: var(--muted); white-space: nowrap; }
.h1 { font-size: 26px; letter-spacing: -.02em; margin: 6px 0 14px; }
.h2 { font-size: 20px; letter-spacing: -.01em; margin: 0; }

/* ---------- drop zone ---------- */
.drop { border: 2px dashed var(--line); border-radius: var(--radius); padding: 28px 16px; text-align: center; color: var(--muted); transition: border-color .15s, background .15s; }
.drop.over { border-color: var(--accent); background: var(--accent-soft); }
.drop svg { color: var(--accent); }
.drop-title { font-size: 18px; font-weight: 600; color: var(--ink); margin: 6px 0 0; }
.drop p { margin: 4px 0; }
.drop .btn { margin: 4px 0 10px; }

/* ---------- thumbnails ---------- */
.thumbs { display: grid; grid-template-columns: repeat(auto-fill, minmax(118px, 1fr)); gap: 12px; margin-top: 16px; }
.thumb { position: relative; border-radius: 10px; overflow: hidden; background: var(--soft); border: 1px solid var(--line); aspect-ratio: 3 / 4; cursor: zoom-in; }
.thumb img { width: 100%; height: 100%; object-fit: cover; display: block; }
.thumb .rm { position: absolute; top: 5px; right: 5px; width: 24px; height: 24px; border-radius: 50%; border: 0; background: rgba(0,0,0,.6); color: #fff; cursor: pointer; font-size: 15px; line-height: 1; }
.thumb .cap { position: absolute; left: 0; right: 0; bottom: 0; padding: 18px 7px 6px; background: linear-gradient(transparent, rgba(0,0,0,.7)); color: #fff; font-size: 12px; font-weight: 500; }
.thumb .badge { position: absolute; top: 6px; left: 6px; font-size: 11px; font-weight: 600; padding: 2px 7px; border-radius: 99px; background: var(--card); color: var(--ink); }
.thumb .badge.bad { background: var(--bad); color: #fff; }
.thumb .badge.warn { background: var(--warn); color: #fff; }
.thumb .badge.ok { background: var(--ok); color: #fff; }
.thumb.busy::after { content: ''; position: absolute; inset: 0; background: color-mix(in srgb, var(--card) 55%, transparent); }
.thumb.busy .spin { position: absolute; z-index: 1; left: 50%; top: 50%; margin: -12px; }
.thumb.flag { outline: 2px solid var(--bad); outline-offset: -2px; }
.thumb.dup { opacity: .55; }

/* ---------- notices ---------- */
.notice-list { display: grid; gap: 8px; margin-top: 14px; }
.notice-list:empty { display: none; }
.notice { padding: 10px 12px; border-radius: 10px; font-size: 14px; display: flex; gap: 8px; align-items: flex-start; }
.notice.error { background: var(--bad-soft); color: var(--bad); margin: 0; }
.notice.warn { background: var(--warn-soft); color: var(--warn); }
.notice.info { background: var(--accent-soft); color: var(--accent); }
.notice.ok { background: var(--ok-soft); color: var(--ok); }

/* ---------- options ---------- */
.options { margin-top: 18px; border: 1px solid var(--line); border-radius: 12px; padding: 0 14px; }
.options summary { cursor: pointer; padding: 12px 0; font-weight: 600; list-style: none; }
.options summary::-webkit-details-marker { display: none; }
.options summary::before { content: '▸'; display: inline-block; width: 16px; transition: transform .15s; }
.options[open] summary::before { transform: rotate(90deg); }
.options summary .muted { font-weight: 400; font-size: 13px; margin-left: 6px; }
.opt-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); gap: 12px 16px; padding-bottom: 16px; }
label.check { display: flex; align-items: center; gap: 8px; align-self: end; padding-bottom: 8px; }
label.check input { width: auto; }
.gen-row { display: flex; align-items: center; gap: 14px; margin-top: 18px; flex-wrap: wrap; }

/* ---------- progress ---------- */
.stages { list-style: none; padding: 0; margin: 0 0 14px; display: grid; gap: 8px; }
.stages li { display: flex; align-items: center; gap: 10px; color: var(--muted); }
.stages li.active { color: var(--ink); font-weight: 600; }
.stages li.done { color: var(--ok); }
.stages li .dot { width: 22px; height: 22px; display: grid; place-items: center; flex: none; }
.stages li .dot::before { content: ''; width: 8px; height: 8px; border-radius: 50%; background: var(--line); }
.stages li.done .dot::before { content: '✓'; width: auto; height: auto; background: none; font-weight: 700; }
.stages li.active .dot::before { display: none; }
.spin { width: 20px; height: 20px; border: 2.5px solid var(--accent-soft); border-top-color: var(--accent); border-radius: 50%; animation: spin .8s linear infinite; display: inline-block; }
@keyframes spin { to { transform: rotate(360deg); } }
.bar { height: 6px; background: var(--soft); border-radius: 99px; overflow: hidden; }
.bar-fill { height: 100%; width: 0; background: var(--accent); border-radius: 99px; transition: width .4s; }

/* ---------- notes ---------- */
.notes-toolbar { display: flex; align-items: center; justify-content: space-between; gap: 12px; flex-wrap: wrap; margin-bottom: 8px; }
.tb-actions { display: flex; gap: 8px; flex-wrap: wrap; }
.dl { position: relative; }
.chapter-h { font-family: var(--serif); font-size: 23px; margin: 22px 0 6px; padding-bottom: 6px; border-bottom: 1px solid var(--line); }
.chapter-src { font-size: 12px; font-family: var(--sans); color: var(--muted); font-weight: 400; margin-left: 6px; }
.group-h { font-family: var(--serif); font-size: 19px; margin: 18px 0 4px; }
.sec { border: 1px solid var(--line); border-radius: 12px; margin: 10px 0; background: var(--card); }
.sec.l2 { margin-left: 18px; }
.sec.l3 { margin-left: 36px; }
.sec > summary { list-style: none; cursor: pointer; padding: 12px 14px; display: flex; align-items: baseline; gap: 10px; }
.sec > summary::-webkit-details-marker { display: none; }
.sec > summary::before { content: '▸'; color: var(--muted); transition: transform .15s; flex: none; }
.sec[open] > summary::before { transform: rotate(90deg); }
.sec-title { font-family: var(--serif); font-weight: 600; font-size: 17.5px; flex: 1; }
.sec-title.intro { font-style: italic; color: var(--muted); font-weight: 400; }
.sec-pages { font-size: 12.5px; color: var(--muted); white-space: nowrap; }
.sec-body { padding: 0 16px 14px 36px; }
.summary { font-family: var(--serif); font-size: 17px; line-height: 1.65; white-space: pre-wrap; margin: 0; }
.summary blockquote, .sep-quote { margin: 8px 0 0; padding-left: 12px; border-left: 3px solid var(--line); color: var(--muted); }
.summary q { quotes: none; }
.summary .qt { background: var(--accent-soft); border-radius: 3px; padding: 0 1px; }
.edit-area { width: 100%; font-family: var(--serif); font-size: 16.5px; line-height: 1.6; min-height: 120px; }
.sec-actions { display: flex; gap: 6px; flex-wrap: wrap; margin-top: 10px; }
.flags { display: grid; gap: 6px; margin-top: 10px; }
.flag { font-size: 13px; padding: 6px 10px; border-radius: 8px; background: var(--warn-soft); color: var(--warn); }
.flag.error { background: var(--bad-soft); color: var(--bad); }
.sec.working { opacity: .6; pointer-events: none; }
.edited-tag { font-size: 11px; font-weight: 600; color: var(--accent); background: var(--accent-soft); padding: 1px 7px; border-radius: 99px; }

/* ---------- qc ---------- */
.qc { margin-top: 18px; border-top: 1px solid var(--line); padding-top: 12px; }
.qc summary { cursor: pointer; font-weight: 600; }
.qc-list { list-style: none; padding: 0; margin: 10px 0 0; display: grid; gap: 6px; }
.qc-list li { display: grid; grid-template-columns: 22px 1fr; gap: 4px 8px; font-size: 14px; }
.qc-list li .st { font-weight: 700; }
.qc-list li.pass .st { color: var(--ok); }
.qc-list li.warn .st { color: var(--warn); }
.qc-list li.fail .st { color: var(--bad); }
.qc-list li .d { grid-column: 2; color: var(--muted); font-size: 13px; }
.pill { font-size: 12px; font-weight: 600; padding: 2px 8px; border-radius: 99px; margin-left: 6px; }
.pill.ok { background: var(--ok-soft); color: var(--ok); }
.pill.warn { background: var(--warn-soft); color: var(--warn); }

/* ---------- dialogs ---------- */
dialog { border: 0; border-radius: var(--radius); padding: 0; background: var(--card); color: var(--ink); box-shadow: 0 20px 60px rgba(0,0,0,.3); width: min(520px, calc(100vw - 32px)); }
dialog::backdrop { background: rgba(10, 14, 24, .5); }
dialog form, .dlg-body { padding: 22px; display: grid; gap: 12px; }
dialog h3 { margin: 0; font-size: 19px; }
dialog p { margin: 0; }
.wide-dlg { width: min(720px, calc(100vw - 32px)); }
.dlg-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 6px; }
.confirm-item { display: grid; grid-template-columns: 110px 1fr; gap: 12px; align-items: center; padding: 10px; border: 1px solid var(--line); border-radius: 10px; }
.confirm-item img { width: 110px; height: 140px; object-fit: cover; border-radius: 8px; cursor: zoom-in; }
.confirm-item .ci-text { display: grid; gap: 6px; font-size: 14px; }
.style-top { display: flex; gap: 8px; }
.style-top select { flex: 1; }
.row { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; }
.profile-view { display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: 8px; }
.profile-view:empty { display: none; }
.pv { background: var(--soft); border-radius: 10px; padding: 9px 11px; font-size: 13.5px; }
.pv b { display: block; font-size: 11.5px; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); margin-bottom: 2px; }
.pv.full { grid-column: 1 / -1; }
.search { margin-bottom: 4px; }
.history { list-style: none; padding: 0; margin: 0; display: grid; gap: 8px; max-height: 60vh; overflow: auto; }
.history li { display: flex; align-items: center; gap: 10px; padding: 10px 12px; border: 1px solid var(--line); border-radius: 10px; }
.history li .h-main { flex: 1; min-width: 0; cursor: pointer; }
.history li .h-name { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.history li .h-meta { font-size: 12.5px; color: var(--muted); }
.history .empty { justify-content: center; color: var(--muted); border-style: dashed; }
.view-dlg { width: min(900px, calc(100vw - 32px)); padding: 12px; }
.view-dlg img { width: 100%; max-height: 75vh; object-fit: contain; display: block; background: var(--soft); border-radius: 8px; }
.view-dlg .view-info { font-size: 13px; color: var(--muted); padding: 8px 2px; }
.view-dlg .btn { float: right; }

.toast { position: fixed; left: 50%; bottom: 24px; transform: translate(-50%, 30px); background: var(--ink); color: var(--bg); padding: 10px 16px; border-radius: 10px; font-size: 14px; opacity: 0; pointer-events: none; transition: opacity .2s, transform .2s; z-index: 50; max-width: calc(100vw - 32px); }
.toast.show { opacity: 1; transform: translate(-50%, 0); }

@media (max-width: 640px) {
  .topbar { padding: 8px 12px; }
  .brand-text { display: none; }
  .card { padding: 16px; }
  .h1 { font-size: 22px; }
  .sec-body { padding: 0 12px 12px 14px; }
  .sec.l2, .sec.l3 { margin-left: 8px; }
  .thumbs { grid-template-columns: repeat(auto-fill, minmax(96px, 1fr)); }
  .top-actions .btn.ghost:not(.icon) { padding: 8px 8px; font-size: 14px; }
  #newBtn { display: none; }
}
