/* The hidden attribute must always win, even over display:flex rules below
   (otherwise e.g. the selection bar shows on the dashboard with "0 blocks"). */
[hidden] { display: none !important; }

:root {
  --accent: #1a4f9c;
  --accent-hover: #17457f;
  --accent-soft: #e8f0fb;
  --bg: #eef1f6;
  --card: #ffffff;
  --text: #1e2430;
  --muted: #6b7280;
  --border: #e1e6ee;
  --border-strong: #cdd5e1;
  --ok: #12855a;
  --ok-soft: #e3f5ec;
  --warn: #b7791f;
  --warn-soft: #fbf1dd;
  --err: #c0392b;
  --err-soft: #fbe6e3;
  --info: #1a4f9c;
  --info-soft: #e8f0fb;
  --radius: 12px;
  --shadow: 0 1px 2px rgba(16, 24, 40, .04), 0 4px 16px rgba(16, 24, 40, .06);
  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  --mono: "Segoe UI Mono", "Cascadia Code", Consolas, monospace;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  font-size: 15px;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
}

h1, h2, h3 { margin: 0; font-weight: 650; letter-spacing: -0.01em; }

/* ---------------- top bar ---------------- */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px 28px;
  background: var(--card);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 20;
}
.brand { display: flex; align-items: center; gap: 14px; }
.brand-mark {
  font-size: 26px;
  background: var(--accent-soft);
  width: 46px; height: 46px;
  display: grid; place-items: center;
  border-radius: 12px;
}
.topbar h1 { font-size: 18px; }
.tagline { margin: 2px 0 0; color: var(--muted); font-size: 13px; }

main { max-width: 1080px; margin: 0 auto; padding: 26px 24px 60px; }

/* ---------------- cards ---------------- */
.card {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}
.uploader { padding: 18px; margin-bottom: 26px; }

/* ---------------- dropzone ---------------- */
.dropzone {
  border: 2px dashed var(--border-strong);
  border-radius: 10px;
  padding: 34px 20px;
  text-align: center;
  cursor: pointer;
  transition: border-color .15s, background .15s;
  outline: none;
}
.dropzone:hover, .dropzone:focus-visible { border-color: var(--accent); background: #fafcff; }
.dropzone.dragover { border-color: var(--accent); background: var(--accent-soft); }
.dz-icon { font-size: 30px; }
.dz-title { font-size: 16px; font-weight: 600; margin: 8px 0 4px; }
.dz-sub { color: var(--muted); margin: 0 0 4px; }
.dz-hint { color: var(--muted); font-size: 12.5px; margin: 8px 0 0; opacity: .85; }
.link { color: var(--accent); font-weight: 600; }

.upload-feedback { margin-top: 12px; }
.upload-feedback .up-row {
  display: flex; align-items: center; gap: 10px;
  font-size: 13.5px; color: var(--muted); padding: 4px 2px;
}

/* ---------------- section head ---------------- */
.section-head {
  display: flex; align-items: center; justify-content: space-between;
  margin: 4px 2px 12px;
}
.section-head h2 { font-size: 16px; }
.section-head-actions { display: flex; gap: 8px; align-items: center; }
.search {
  border: 1px solid var(--border-strong);
  border-radius: 8px;
  padding: 7px 11px;
  font: inherit; font-size: 13.5px;
  width: 200px;
  background: var(--card);
}
.search:focus { outline: 2px solid var(--accent-soft); border-color: var(--accent); }

/* ---------------- table ---------------- */
.jobs-table { width: 100%; border-collapse: collapse; }
.jobs-table thead th {
  text-align: left;
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: .04em;
  color: var(--muted);
  font-weight: 600;
  padding: 12px 16px;
  border-bottom: 1px solid var(--border);
}
.jobs-table tbody td {
  padding: 13px 16px;
  border-bottom: 1px solid var(--border);
  vertical-align: middle;
}
.jobs-table tbody tr:last-child td { border-bottom: none; }
.jobs-table tbody tr:hover { background: #f8fafd; }
.col-pages, .col-status { width: 150px; }
.col-date { width: 130px; }
.col-actions { width: 190px; text-align: right; }
.job-name { font-weight: 600; display: flex; align-items: center; gap: 9px; }
.job-kind {
  font-size: 11px; color: var(--muted); font-weight: 500;
  background: var(--bg); border-radius: 5px; padding: 1px 6px;
}
.date-cell { color: var(--muted); font-size: 13px; }

/* ---------------- status badges + progress ---------------- */
.badge {
  display: inline-flex; align-items: center; gap: 6px;
  font-size: 12.5px; font-weight: 600;
  padding: 3px 9px; border-radius: 20px;
}
.badge::before { content: ""; width: 7px; height: 7px; border-radius: 50%; background: currentColor; }
.badge-queued { color: var(--muted); background: var(--bg); }
.badge-processing { color: var(--info); background: var(--info-soft); }
.badge-done { color: var(--ok); background: var(--ok-soft); }
.badge-error { color: var(--err); background: var(--err-soft); }
.progress-wrap { margin-top: 7px; height: 5px; background: var(--bg); border-radius: 4px; overflow: hidden; }
.progress-bar { height: 100%; background: var(--accent); width: 0; transition: width .4s ease; }
.status-msg { font-size: 11.5px; color: var(--muted); margin-top: 5px; display: block; max-width: 220px; }

/* ---------------- buttons ---------------- */
.btn {
  font: inherit; font-size: 13.5px; font-weight: 600;
  border: 1px solid transparent; border-radius: 8px;
  padding: 8px 14px; cursor: pointer;
  transition: background .15s, border-color .15s, color .15s;
  white-space: nowrap;
}
.btn-primary { background: var(--accent); color: #fff; }
.btn-primary:hover { background: var(--accent-hover); }
.btn-secondary { background: var(--card); color: var(--text); border-color: var(--border-strong); }
.btn-secondary:hover { border-color: var(--accent); color: var(--accent); }
.btn-ghost { background: transparent; color: var(--muted); border-color: transparent; }
.btn-ghost:hover { background: var(--bg); color: var(--text); }
.btn-sm { padding: 6px 10px; font-size: 12.5px; }
.btn-danger-ghost { background: transparent; color: var(--err); border-color: transparent; }
.btn-danger-ghost:hover { background: var(--err-soft); }
.row-actions { display: flex; gap: 4px; justify-content: flex-end; align-items: center; }
.btn[disabled] { opacity: .5; cursor: default; }

/* ---------------- dropdown ---------------- */
.dropdown { position: relative; display: inline-block; }
.dropdown-menu {
  position: absolute; right: 0; top: calc(100% + 6px);
  background: var(--card); border: 1px solid var(--border);
  border-radius: 10px; box-shadow: var(--shadow);
  min-width: 170px; padding: 6px; display: none; z-index: 30;
}
.dropdown.open .dropdown-menu { display: block; }
.dropdown-menu a {
  display: block; padding: 9px 11px; border-radius: 7px;
  color: var(--text); text-decoration: none; font-size: 13.5px;
}
.dropdown-menu a:hover { background: var(--accent-soft); color: var(--accent); }

/* ---------------- empty state ---------------- */
.empty-state { padding: 44px 20px; text-align: center; color: var(--muted); }

/* ---------------- chips ---------------- */
.chip {
  font-size: 12px; font-weight: 600; padding: 5px 11px;
  border-radius: 20px; display: inline-flex; align-items: center; gap: 6px;
}
.chip::before { content: ""; width: 7px; height: 7px; border-radius: 50%; background: currentColor; }
.chip-muted { color: var(--muted); background: var(--bg); }
.chip-ok { color: var(--ok); background: var(--ok-soft); }
.chip-warn { color: var(--warn); background: var(--warn-soft); }

/* ---------------- result view ---------------- */
.result-head {
  display: flex; align-items: center; justify-content: space-between;
  margin-bottom: 14px; gap: 12px; flex-wrap: wrap;
}
.result-actions { display: flex; gap: 8px; align-items: center; }
.result-meta { margin: 4px 2px 18px; }
.result-meta h2 { font-size: 20px; }
.view-toggle { margin: 0 2px 16px; }
.switch { display: inline-flex; align-items: center; gap: 8px; font-size: 13.5px; color: var(--muted); cursor: pointer; }
.switch input { width: 16px; height: 16px; accent-color: var(--accent); }

.pages-container { display: flex; flex-direction: column; gap: 18px; }
.page-card {
  background: var(--card); border: 1px solid var(--border);
  border-radius: var(--radius); box-shadow: var(--shadow); overflow: hidden;
}
.page-card-head {
  display: flex; align-items: center; justify-content: space-between;
  padding: 12px 18px; border-bottom: 1px solid var(--border);
  background: #fbfcfe;
}
.page-card-head h3 { font-size: 14px; color: var(--accent); }
.page-body { display: grid; grid-template-columns: 1fr; }
.page-body.with-image { grid-template-columns: minmax(0,1fr) minmax(0,1fr); }
.page-image-pane { border-right: 1px solid var(--border); padding: 14px; background: #f6f8fb; display: none; }
.page-body.with-image .page-image-pane { display: block; }
.page-image-pane img { width: 100%; border-radius: 8px; border: 1px solid var(--border); }
.page-text {
  padding: 18px 20px;
  white-space: pre-wrap;
  word-wrap: break-word;
  font-size: 14.5px;
  line-height: 1.62;
  max-height: 620px;
  overflow-y: auto;
}
.page-text.empty { color: var(--muted); font-style: italic; }

/* ---------------- selectable text blocks ---------------- */
.selection-help {
  margin: 0 2px 14px;
  padding: 10px 14px;
  background: var(--accent-soft);
  border: 1px solid #d3e2f7;
  border-radius: 9px;
  font-size: 13px;
  color: #24486f;
}
.selection-help kbd {
  font: inherit; font-size: 11.5px; font-weight: 600;
  background: var(--card); border: 1px solid var(--border-strong);
  border-bottom-width: 2px; border-radius: 5px; padding: 1px 5px;
}
.page-head-actions { display: flex; gap: 6px; align-items: center; }

.page-blocks { padding: 10px 12px; max-height: 620px; overflow-y: auto; }
.block {
  position: relative;
  display: flex; gap: 10px; align-items: flex-start;
  padding: 9px 11px 9px 9px;
  border: 1px solid transparent;
  border-radius: 8px;
  transition: background .12s, border-color .12s;
}
/* Text must be freely selectable — dragging across it is the whole point. */
.block .block-text { user-select: text; cursor: text; }
.block:hover { border-color: var(--border); }
.block.selected { background: var(--accent-soft); border-color: #b9d2f2; }

/* Native drag-selection, before it is marked */
.block-text::selection { background: rgba(26, 79, 156, .22); }
.block-text ::selection { background: rgba(26, 79, 156, .22); }

/* Marked passages — translucent highlighter pen */
mark.hl {
  background: rgba(255, 202, 40, .42);
  box-shadow: 0 0 0 1px rgba(214, 158, 0, .28);
  border-radius: 3px;
  color: inherit;
  padding: 1px 0;
  cursor: pointer;
  transition: background .12s;
}
mark.hl:hover { background: rgba(255, 138, 128, .48); }
mark.hl:hover::after { content: " ✕"; font-size: 10px; color: #8a3b32; font-weight: 700; }

/* Floating "add this" button that follows the selection */
.add-sel {
  position: fixed; z-index: 48;
  font: inherit; font-size: 13px; font-weight: 700;
  background: var(--accent); color: #fff;
  border: none; border-radius: 8px;
  padding: 7px 13px; cursor: pointer;
  box-shadow: 0 6px 20px rgba(16, 24, 40, .3);
}
.add-sel:hover { background: var(--accent-hover); }
.add-sel[hidden] { display: none; }
.kbd-plus {
  display: inline-block; background: var(--accent); color: #fff;
  border-radius: 5px; padding: 0 6px; font-weight: 700;
}
.block-check {
  flex: none; margin-top: 3px;
  width: 15px; height: 15px; accent-color: var(--accent); cursor: pointer;
}
.block-text { flex: 1; min-width: 0; }

/* ---------------- rendered Markdown (blocks + article panes) ---------------- */
.rich {
  white-space: normal; word-wrap: break-word;
  font-size: 14.5px; line-height: 1.62;
}
.rich > :first-child { margin-top: 0; }
.rich > :last-child  { margin-bottom: 0; }
.rich p { margin: 0 0 10px; }
.rich h1, .rich h2, .rich h3, .rich h4, .rich h5, .rich h6 {
  margin: 14px 0 7px; line-height: 1.3; color: var(--accent); font-weight: 700;
}
.rich h1 { font-size: 20px; }
.rich h2 { font-size: 17px; }
.rich h3 { font-size: 15.5px; }
.rich h4, .rich h5, .rich h6 { font-size: 14.5px; }
.rich ul, .rich ol { margin: 0 0 10px; padding-left: 22px; }
.rich li { margin-bottom: 5px; }
.rich strong { font-weight: 700; }
.rich em { font-style: italic; }
.rich code {
  font-family: var(--mono); font-size: 12.5px;
  background: var(--bg); padding: 1px 5px; border-radius: 4px;
}
.rich table {
  border-collapse: collapse; width: 100%; margin: 0 0 10px; font-size: 13px;
  display: block; overflow-x: auto;
}
.rich th, .rich td {
  border: 1px solid var(--border-strong); padding: 6px 9px;
  text-align: left; vertical-align: top;
}
.rich th { background: var(--bg); font-weight: 700; }

/* ---------------- selection bar ---------------- */
.selection-bar {
  position: fixed; bottom: 18px; left: 50%; transform: translateX(-50%);
  display: flex; align-items: center; gap: 18px;
  background: #1e2430; color: #fff;
  padding: 10px 12px 10px 20px; border-radius: 12px;
  box-shadow: 0 10px 34px rgba(0,0,0,.28);
  z-index: 45;
}
.sel-count { font-size: 13.5px; white-space: nowrap; }
.sel-count strong { font-size: 15px; }
.sel-actions { display: flex; gap: 6px; }
.selection-bar .btn-ghost { color: #c7cdd8; }
.selection-bar .btn-ghost:hover { background: rgba(255,255,255,.12); color: #fff; }
.selection-bar .btn-secondary { background: transparent; color: #fff; border-color: rgba(255,255,255,.28); }
.selection-bar .btn-secondary:hover { background: rgba(255,255,255,.12); color: #fff; border-color: rgba(255,255,255,.5); }

/* ---------------- article view ---------------- */
.compare-grid { display: grid; grid-template-columns: minmax(0,1.15fr) minmax(0,.85fr); gap: 18px; }
.compare-pane { overflow: hidden; }
.compare-pane h3 {
  font-size: 13px; padding: 12px 18px; margin: 0;
  border-bottom: 1px solid var(--border); background: #fbfcfe; color: var(--accent);
  display: flex; align-items: baseline; gap: 10px; justify-content: space-between;
}
.pane-note { font-size: 11.5px; color: var(--muted); font-weight: 500; }
#article-body { white-space: normal; }
#article-body h1 { font-size: 21px; margin: 4px 0 12px; }
#article-body h2 { font-size: 17px; margin: 20px 0 8px; }
#article-body h3 { font-size: 15px; margin: 16px 0 6px; }
#article-body p { margin: 0 0 13px; }
#article-body ul, #article-body ol { margin: 0 0 13px; padding-left: 22px; }
#article-body li { margin-bottom: 5px; }
.muted { color: var(--muted); }
.article-status {
  display: flex; align-items: center; gap: 10px;
  margin: 0 2px 16px; padding: 11px 15px;
  background: var(--info-soft); border: 1px solid #d3e2f7;
  border-radius: 9px; font-size: 13.5px; color: #24486f;
}
.article-status.error { background: var(--err-soft); border-color: #f3cdc7; color: var(--err); }
.article-status .btn { margin-left: auto; flex: none; }

/* ---------------- dialogs ---------------- */
.modal-backdrop {
  position: fixed; inset: 0; z-index: 60;
  background: rgba(20, 26, 38, .48);
  display: flex; align-items: center; justify-content: center;
  padding: 20px;
}
.modal-backdrop[hidden] { display: none; }
.modal {
  background: var(--card); border-radius: 14px;
  box-shadow: 0 20px 60px rgba(0,0,0,.3);
  width: 100%; max-width: 520px; padding: 24px;
  max-height: 90vh; overflow-y: auto;
}
.modal h2 { font-size: 18px; margin-bottom: 6px; }
.modal-sub { color: var(--muted); font-size: 13.5px; margin: 0 0 18px; }
.modal-note { color: var(--muted); font-size: 12.5px; line-height: 1.55; margin: 14px 0 0; }
.modal-note code { font-family: var(--mono); font-size: 11.5px; background: var(--bg); padding: 1px 5px; border-radius: 4px; }
.modal-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 22px; }

.field { display: block; margin-bottom: 14px; }
.field > span { display: block; font-size: 13px; font-weight: 600; margin-bottom: 6px; }
.field > span em { font-weight: 500; font-style: normal; color: var(--muted); }
.field input, .field textarea, .field select {
  width: 100%; font: inherit; font-size: 14px;
  border: 1px solid var(--border-strong); border-radius: 8px;
  padding: 9px 11px; background: var(--card); color: var(--text);
  resize: vertical;
}
.field select { cursor: pointer; }
.field input:focus, .field textarea:focus, .field select:focus {
  outline: 2px solid var(--accent-soft); border-color: var(--accent);
}
.modal-rule { border: none; border-top: 1px solid var(--border); margin: 20px 0 18px; }
.modal-h3 { font-size: 14px; margin: 0 0 12px; }
.settings-advanced { margin-top: 16px; }
.settings-advanced summary {
  cursor: pointer; color: var(--muted); font-size: 13px;
  padding: 6px 0; user-select: none;
}
.settings-advanced summary:hover { color: var(--text); }
.settings-advanced[open] summary { margin-bottom: 12px; }
.pw-reveal {
  font-family: var(--mono); background: var(--bg);
  padding: 2px 8px; border-radius: 6px;
}
.warn-box {
  background: var(--warn-soft); border: 1px solid #eddcb7;
  color: #7a5410; border-radius: 9px;
  padding: 11px 14px; font-size: 13px; margin-bottom: 16px;
}
.link-btn {
  background: none; border: none; padding: 0; font: inherit; font-weight: 700;
  color: var(--accent); cursor: pointer; text-decoration: underline;
}

/* ---------------- toast ---------------- */
.toast {
  position: fixed; bottom: 24px; left: 50%; transform: translateX(-50%);
  background: #1e2430; color: #fff; padding: 11px 18px; border-radius: 9px;
  font-size: 13.5px; font-weight: 500; box-shadow: 0 8px 30px rgba(0,0,0,.24);
  z-index: 50; opacity: 0; transition: opacity .2s, transform .2s;
  pointer-events: none;
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(-4px); }
.toast.toast-error { background: var(--err); }

/* ---------------- footer ---------------- */
.footer { text-align: center; color: var(--muted); font-size: 12.5px; padding: 20px; }

/* ---------------- misc ---------------- */
.spinner {
  width: 14px; height: 14px; border: 2px solid var(--border-strong);
  border-top-color: var(--accent); border-radius: 50%;
  display: inline-block; animation: spin .7s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

@media (max-width: 720px) {
  .col-date, .col-pages { display: none; }
  .page-body.with-image { grid-template-columns: 1fr; }
  .page-body.with-image .page-image-pane { border-right: none; border-bottom: 1px solid var(--border); }
  .result-head { flex-direction: column; align-items: stretch; }
  .compare-grid { grid-template-columns: 1fr; }
  .selection-bar { left: 12px; right: 12px; transform: none; justify-content: space-between; padding-left: 14px; }
}
