/* Colour tokens. Light is the default; dark applies when the OS prefers it
   (unless the user pinned light) or when the user pinned dark via the toggle,
   which sets data-theme on <html>. */
:root {
  color-scheme: light;
  --primary-clr: #cf4a6b;
  --accent-clr: #29a399;
  --danger-clr: #c0392b;
  --nav-bg: var(--primary-clr);
  --bg: #f7f7f9;
  --surface: #ffffff;
  --input-bg: #ffffff;
  --border: #e3e3ea;
  --text: #23232b;
  --muted: #7a7a88;
  --code-bg: #1e1e24;
  --code-text: #e6e6e6;
  --tag-ok-bg: #e3f6f3;
  --tag-ok-text: var(--accent-clr);
  --tag-muted-bg: #eeeeee;
  --warn-bg: #fff4e5;   --warn-border: #ffd9a8;   --warn-text: #8a5300;
  --error-bg: #fdeaec;  --error-border: #f3b6c1;  --error-text: #a01b35;
  --ok-bg: #e3f6f3;     --ok-border: #a8e4dd;     --ok-text: #1d7a72;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --primary-clr: #e0708b;
    --accent-clr: #3cbfb3;
    --danger-clr: #e05a4c;
    --nav-bg: #8f2f48;
    --bg: #15151a;
    --surface: #1f1f26;
    --input-bg: #2a2a33;
    --border: #34343f;
    --text: #e6e6ec;
    --muted: #9a9aab;
    --code-bg: #0f0f13;
    --code-text: #e6e6e6;
    --tag-ok-bg: #173a37;
    --tag-ok-text: #6fd8cd;
    --tag-muted-bg: #2c2c35;
    --warn-bg: #3a2c12;   --warn-border: #6b5020;   --warn-text: #f3c77a;
    --error-bg: #3d1820;  --error-border: #73303e;  --error-text: #f3a3b4;
    --ok-bg: #173a37;     --ok-border: #2b665f;     --ok-text: #8fe3d9;
  }
}

:root[data-theme="dark"] {
  color-scheme: dark;
  --primary-clr: #e0708b;
  --accent-clr: #3cbfb3;
  --danger-clr: #e05a4c;
  --nav-bg: #8f2f48;
  --bg: #15151a;
  --surface: #1f1f26;
  --input-bg: #2a2a33;
  --border: #34343f;
  --text: #e6e6ec;
  --muted: #9a9aab;
  --code-bg: #0f0f13;
  --code-text: #e6e6e6;
  --tag-ok-bg: #173a37;
  --tag-ok-text: #6fd8cd;
  --tag-muted-bg: #2c2c35;
  --warn-bg: #3a2c12;   --warn-border: #6b5020;   --warn-text: #f3c77a;
  --error-bg: #3d1820;  --error-border: #73303e;  --error-text: #f3a3b4;
  --ok-bg: #173a37;     --ok-border: #2b665f;     --ok-text: #8fe3d9;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
}

a { color: var(--primary-clr); text-decoration: none; }
a:hover { text-decoration: underline; }

header.topnav {
  background: var(--nav-bg);
  color: #fff;
  padding: 0.75rem 1.25rem;
  display: flex;
  align-items: center;
  gap: 1.5rem;
}
header.topnav .brand { font-weight: 700; font-size: 1.1rem; }
header.topnav nav { display: flex; gap: 1.25rem; }
header.topnav nav a { color: #fff; opacity: 0.9; }
header.topnav nav a:hover { opacity: 1; }
header.topnav .spacer { flex: 1; }
header.topnav .nav-user { color: #fff; opacity: 0.85; font-size: 0.9rem; }
header.topnav .logout-form { margin: 0; display: flex; }
header.topnav .logout-link {
  color: #fff;
  background: transparent;
  border: 1px solid rgba(255, 255, 255, 0.6);
  border-radius: 6px;
  padding: 0.3rem 0.8rem;
  font-size: 0.9rem;
  font-family: inherit;
  cursor: pointer;
}
header.topnav .logout-link:hover {
  background: rgba(255, 255, 255, 0.15);
  text-decoration: none;
}

main {
  max-width: var(--main-max-width, 1100px);
  margin: 1.5rem auto;
  padding: 0 1.25rem;
  position: relative;
}

/* Per-page default widths. A width dragged by the user (--main-max-width) wins. */
main.main-wide { max-width: var(--main-max-width, 99%); }
main.main-narrow { max-width: var(--main-max-width, max(40%, 520px)); }

#main-resizer {
  position: absolute;
  top: 0;
  right: 0;
  width: 12px;
  height: 100%;
  cursor: col-resize;
  z-index: 5;
}
/* The handle is sticky so it stays in the middle of the viewport. Centered in the
   whole page (top: 50%) it ends up far below the fold on a long table. */
#main-resizer::before {
  content: "";
  display: block;
  position: sticky;
  top: calc(50vh - 28px);
  margin: 0 3px 0 auto;
  width: 4px;
  height: 56px;
  background: var(--primary-clr);
  border-radius: 2px;
  transition: background 0.15s;
}
#main-resizer:hover::before, body.resizing-main #main-resizer::before { background: var(--accent-clr); }
body.resizing-main { cursor: col-resize; user-select: none; }
@media (max-width: 700px) { #main-resizer { display: none; } }

h1, h2, h3 { color: var(--text); }
h1 { font-size: 1.5rem; }

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 1rem 1.25rem;
  margin-bottom: 1rem;
}

table { width: 100%; border-collapse: collapse; }
th, td { text-align: left; padding: 0.5rem 0.6rem; border-bottom: 1px solid var(--border); }
th { color: var(--muted); font-size: 0.8rem; text-transform: uppercase; letter-spacing: 0.03em; }

.btn {
  display: inline-block;
  background: var(--primary-clr);
  color: #fff;
  border: none;
  border-radius: 6px;
  padding: 0.4rem 0.8rem;
  cursor: pointer;
  font-size: 0.9rem;
}
.btn:hover { filter: brightness(1.05); text-decoration: none; }
.btn.accent { background: var(--accent-clr); }
.btn.ghost { background: transparent; color: var(--primary-clr); border: 1px solid var(--primary-clr); }
.btn.danger { background: var(--danger-clr); }
.btn.small { padding: 0.2rem 0.5rem; font-size: 0.8rem; }

input, select, textarea {
  font: inherit;
  padding: 0.4rem 0.5rem;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--input-bg);
  color: var(--text);
}
textarea { width: 100%; min-height: 360px; font-family: ui-monospace, monospace; }
label { font-size: 0.85rem; color: var(--muted); display: block; margin-bottom: 0.2rem; }

.tag { display: inline-block; padding: 0.1rem 0.45rem; border-radius: 999px; font-size: 0.72rem; }
.tag.ok { background: var(--tag-ok-bg); color: var(--tag-ok-text); }
.tag.muted { background: var(--tag-muted-bg); color: var(--muted); }

.badge-ok { color: var(--accent-clr); font-weight: 600; }
.badge-fail { color: var(--primary-clr); font-weight: 600; }

.banner { padding: 0.7rem 1rem; border-radius: 8px; margin-bottom: 1rem; }
.banner.warn { background: var(--warn-bg); border: 1px solid var(--warn-border); color: var(--warn-text); }
.banner.error { background: var(--error-bg); border: 1px solid var(--error-border); color: var(--error-text); }
.banner.ok { background: var(--ok-bg); border: 1px solid var(--ok-border); color: var(--ok-text); }

.filters { display: flex; flex-wrap: wrap; gap: 0.75rem; align-items: end; }
.filters > div { display: flex; flex-direction: column; }

.row-actions { display: flex; gap: 0.4rem; align-items: center; }
.inline-form { display: inline; }

pre.output {
  background: var(--code-bg); color: var(--code-text); padding: 0.8rem; border-radius: 8px;
  overflow: auto; max-height: 360px; white-space: pre-wrap; font-size: 0.82rem;
}

.scrappers-layout { display: flex; gap: 0.5rem; align-items: flex-start; }
.scrappers-main { flex: 1; min-width: 0; }
.run-sidebar { width: 360px; flex-shrink: 0; position: sticky; top: 1rem; }
.run-sidebar #run-output { min-height: 120px; max-height: 75vh; resize: vertical; }

.run-resizer {
  flex: 0 0 6px;
  align-self: stretch;
  min-height: 120px;
  margin: 0 0.25rem;
  background: var(--border);
  border-radius: 3px;
  cursor: col-resize;
  position: sticky;
  top: 1rem;
}
.run-resizer:hover { background: var(--primary-clr); }
.scrappers-layout.resizing { cursor: col-resize; user-select: none; }

@media (max-width: 900px) {
  .scrappers-layout { flex-direction: column; }
  .run-sidebar { width: 100% !important; position: static; }
  .run-resizer { display: none; }
}

.preview-img { max-width: 100%; height: auto; border: 1px solid var(--border); border-radius: 6px; }
/* Scraped pages render with their own (light) styles, so keep the frame white. */
.preview-frame { width: 100%; height: 70vh; border: 1px solid var(--border); border-radius: 6px; background: #fff; }
.preview-text {
  background: var(--code-bg); color: var(--code-text); padding: 0.9rem; border-radius: 8px;
  overflow: auto; max-height: 70vh; white-space: pre-wrap; word-break: break-word;
  font-family: ui-monospace, monospace; font-size: 0.82rem; margin: 0;
}

.login-wrap { max-width: 360px; margin: 6rem auto; }
.login-wrap .card { padding: 1.5rem; }
.login-wrap h1 { text-align: center; color: var(--primary-clr); }
.field { margin-bottom: 0.9rem; }
.field input { width: 100%; }

/* ── Theme toggle ───────────────────────────────────────────────────────── */
.theme-toggle {
  color: #fff;
  background: transparent;
  border: 1px solid rgba(255, 255, 255, 0.6);
  border-radius: 6px;
  padding: 0.3rem 0.7rem;
  font: inherit;
  font-size: 0.85rem;
  cursor: pointer;
}
.theme-toggle:hover { background: rgba(255, 255, 255, 0.15); }
.login-wrap .theme-toggle {
  color: var(--muted);
  border-color: var(--border);
  position: fixed;
  top: 1rem;
  right: 1rem;
}

/* ── Scrape runs / builder ──────────────────────────────────────────────── */
.tag.changed { background: var(--warn-bg); color: var(--warn-text); }
.tag.failed { background: var(--error-bg); color: var(--error-text); }
pre.diff .add { color: #7ee2a8; }
pre.diff .del { color: #ff9aa9; }
pre.diff .hunk { color: #8ab4ff; }
.hint { color: var(--muted); font-size: 0.85rem; margin: 0 0 0.8rem; }
.stack { display: flex; flex-direction: column; gap: 0.8rem; }
.stack .field input, .stack .field textarea { width: 100%; }
.stack textarea { min-height: 90px; font-family: inherit; }
.kv { display: grid; grid-template-columns: max-content 1fr; gap: 0.3rem 1rem; }
.kv dt { color: var(--muted); font-size: 0.85rem; }
.kv dd { margin: 0; word-break: break-all; }

.output-head { display: flex; align-items: center; justify-content: space-between; gap: 0.5rem; }
.output-head h3 { margin: 0.2rem 0; }
.output-tools { display: flex; gap: 0.3rem; flex-shrink: 0; }

/* Database browser: columns share the width, long values are cut. */
.db-grid { table-layout: fixed; }
.db-grid th, .db-grid td { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.db-grid th a { color: inherit; }
.db-grid td.db-actions { overflow: visible; }
.db-grid .db-actions .btn, .db-grid .db-actions form { margin-right: 0.2rem; }
.db-id { min-width: 2.2rem; font-variant-numeric: tabular-nums; }

.db-overlay { position: fixed; inset: 0; background: rgba(0, 0, 0, 0.35); z-index: 40; }
.db-panel {
  position: fixed; top: 0; right: 0; bottom: 0; z-index: 41;
  width: min(560px, 100vw);
  background: var(--surface); color: var(--text);
  border-left: 1px solid var(--border);
  box-shadow: -8px 0 24px rgba(0, 0, 0, 0.25);
  display: flex; flex-direction: column;
}
.db-overlay[hidden], .db-panel[hidden] { display: none; }
.db-panel-head { display: flex; align-items: center; justify-content: space-between; gap: 0.5rem; padding: 0.8rem 1rem; border-bottom: 1px solid var(--border); }
.db-panel-head h3 { margin: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.db-panel-form { flex: 1; overflow: auto; padding: 1rem; }
.db-panel-form input, .db-panel-form textarea { width: 100%; box-sizing: border-box; min-height: 0; }
.db-panel-form textarea { font-family: ui-monospace, monospace; font-size: 0.82rem; resize: vertical; }
.db-panel-foot { display: flex; gap: 0.5rem; padding: 0.8rem 1rem; border-top: 1px solid var(--border); }

.db-confirm { background: var(--surface); color: var(--text); border: 1px solid var(--border); border-radius: 10px; padding: 1.2rem 1.4rem; width: min(420px, 92vw); }
.db-confirm::backdrop { background: rgba(0, 0, 0, 0.5); }
.db-confirm p { margin-top: 0; margin-bottom: 1.4rem; }
/* Far apart on purpose: Delete must not be one slip of the mouse away from Cancel. */
.db-confirm .row-actions { display: flex; justify-content: space-between; gap: 3rem; margin: 0; }

.db-date-row { display: flex; gap: 0.5rem; align-items: center; }
.db-date-row input { flex: 1; width: auto; }
.db-date-row .btn { flex-shrink: 0; }

/* Runs list: fixed columns so a long summary can never push the table out of its card. */
.runs-grid { table-layout: fixed; }
.runs-grid td { vertical-align: middle; }
.run-when { white-space: nowrap; font-variant-numeric: tabular-nums; }
.run-when span { display: block; color: var(--muted); font-size: 0.8rem; }
.run-scrapper { overflow: hidden; }
.run-scrapper span { display: block; color: var(--muted); font-size: 0.75rem; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.run-scrapper code { display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.runs-grid .tag { white-space: nowrap; }
.run-summary {
  display: -webkit-box; -webkit-box-orient: vertical; -webkit-line-clamp: 2; line-clamp: 2;
  overflow: hidden; overflow-wrap: anywhere; line-height: 1.35;
}
.run-actions { text-align: right; }
/* "first run" is neutral, not a muted grey that disappears on the dark surface. */
.tag.first { background: transparent; color: var(--text); border: 1px solid var(--border); }

/* Report an issue: header button, menu, dialog, picker, countdown, toast */
.feedback-btn {
  display: inline-flex; align-items: center; justify-content: center;
  width: 2rem; height: 2rem; padding: 0;
  color: #fff; background: transparent; border: 1px solid rgba(255, 255, 255, 0.6);
  border-radius: 6px; cursor: pointer;
}
.feedback-btn:hover { background: rgba(255, 255, 255, 0.15); }
.feedback-btn:disabled { opacity: 0.5; cursor: progress; }
.feedback-menu {
  position: fixed; z-index: 1000; min-width: 15rem; padding: 0.3rem;
  background: var(--surface); color: var(--text);
  border: 1px solid var(--border); border-radius: 8px; box-shadow: 0 6px 24px rgba(0, 0, 0, 0.25);
}
.feedback-menu[hidden] { display: none; }
.feedback-menu hr { border: 0; border-top: 1px solid var(--border); margin: 0.3rem 0; }
.feedback-menu button {
  display: block; width: 100%; text-align: left; padding: 0.5rem 0.7rem;
  background: transparent; color: inherit; border: 0; border-radius: 6px; font: inherit; cursor: pointer;
}
.feedback-menu button[hidden] { display: none; }
.feedback-menu button:hover, .feedback-menu button:focus-visible { background: var(--bg); outline: none; }
.feedback-dialog {
  width: min(34rem, 92vw); padding: 1.2rem;
  background: var(--surface); color: var(--text);
  border: 1px solid var(--border); border-radius: 8px;
}
.feedback-dialog::backdrop { background: rgba(0, 0, 0, 0.45); }
.feedback-dialog h3 { margin: 0; }
.feedback-dialog .field { margin: 0; }
.feedback-dialog label { display: block; margin-bottom: 0.25rem; }
.feedback-dialog input:not([type=radio]), .feedback-dialog textarea { width: 100%; box-sizing: border-box; }
.feedback-dialog .hint { display: block; margin: 0.3rem 0 0; }
.feedback-dialog .row-actions { justify-content: flex-end; }
.feedback-kind { display: flex; gap: 1.2rem; }
.feedback-kind label { display: inline-flex; align-items: center; gap: 0.35rem; margin: 0; }
.feedback-preview { margin: 0; border: 1px solid var(--border); border-radius: 8px; overflow: hidden; }
.feedback-preview[hidden], #feedback-add-shot[hidden], #feedback-error[hidden] { display: none; }
.feedback-preview img { display: block; width: 100%; max-height: 260px; object-fit: contain; background: var(--bg); }
.feedback-preview figcaption { display: flex; justify-content: flex-end; gap: 0.25rem; padding: 0.25rem; }
#feedback-add-shot { align-self: flex-start; }
.feedback-toast {
  position: fixed; z-index: 2000; left: 50%; bottom: 1.2rem; transform: translateX(-50%);
  max-width: 90vw; padding: 0.6rem 1rem; border-radius: 8px;
  background: var(--code-bg, #1e1e24); color: #fff; box-shadow: 0 6px 24px rgba(0, 0, 0, 0.3);
}
.feedback-toast[hidden] { display: none; }
.feedback-pill {
  position: fixed; inset: auto auto 16px 16px; z-index: 2147483646;
  display: flex; align-items: center; gap: 8px; padding: 4px 4px 4px 12px;
  background: var(--surface); color: var(--text); border-radius: 8px;
  box-shadow: 0 6px 24px rgba(0, 0, 0, 0.3); font: 500 0.875rem system-ui, sans-serif; pointer-events: none;
}
.feedback-pill button {
  pointer-events: auto; border: 0; border-radius: 6px; padding: 4px 8px;
  background: transparent; color: var(--primary-clr); font: inherit; cursor: pointer;
}
.feedback-picker {
  position: fixed; inset: 0; width: 100vw; height: 100vh; padding: 0; z-index: 2147483646;
  cursor: crosshair; background: rgba(0, 0, 0, 0.45); touch-action: none; user-select: none;
}
.feedback-picker.is-dragging { background: transparent; }
.feedback-picker__hint {
  position: fixed; top: 16px; left: 50%; transform: translateX(-50%); padding: 6px 12px; border-radius: 8px;
  background: var(--surface); color: var(--text); box-shadow: 0 6px 24px rgba(0, 0, 0, 0.3);
  font: 500 0.875rem system-ui, sans-serif; pointer-events: none;
}
.feedback-picker__box {
  position: fixed; display: none; outline: 2px solid var(--accent-clr);
  box-shadow: 0 0 0 9999px rgba(0, 0, 0, 0.45); background: transparent; pointer-events: none;
}
