/* NewsMag admin: a quiet editor's desk. Cool paper, ink text, one blue-pencil accent.
   Fonts are system fonts (they cover Cyrillic). To use your own, add @font-face rules
   and put the family first in --sans / --serif. */

:root {
    --paper: #f3f4f6;
    --sheet: #ffffff;
    --ink: #14171c;
    --graphite: #566070;
    --rule: #d9dde3;
    --rule-strong: #aab1bd;
    --pencil: #2f4bd0;
    --pencil-ink: #ffffff;
    --pencil-wash: #eceffd;
    --done: #1e7a4f;
    --error: #b3342a;
    --error-wash: #fbeceb;
    --amber: #96610a;
    --slate: #66707f;

    --sans: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", "Noto Sans", Arial, sans-serif;
    --serif: Charter, "Iowan Old Style", "Sitka Text", Cambria, "Noto Serif", Georgia, serif;
    --mono: ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace;
    --radius: 4px;
}

@media (prefers-color-scheme: dark) {
    :root {
        --paper: #111319;
        --sheet: #171a21;
        --ink: #e8eaee;
        --graphite: #99a2b1;
        --rule: #2a2f3a;
        --rule-strong: #485063;
        --pencil: #8ea2ff;
        --pencil-ink: #0e1220;
        --pencil-wash: #1d2236;
        --done: #5cc590;
        --error: #ff8f84;
        --error-wash: #2c1b1b;
        --amber: #e3b05c;
        --slate: #98a1b0;
    }
}

*, *::before, *::after { box-sizing: border-box; }

html { -webkit-text-size-adjust: 100%; }

body {
    margin: 0;
    background: var(--paper);
    color: var(--ink);
    font: 15px/1.5 var(--sans);
}

a { color: var(--pencil); text-underline-offset: 2px; }
a:hover { text-decoration-thickness: 2px; }

h1, h2, h3 { margin: 0; line-height: 1.25; font-weight: 650; }
h1 { font-size: 22px; }
h2 { font-size: 17px; }
h3 { font-size: 15px; }

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

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

.muted { color: var(--graphite); }

/* ---------- top bar ---------- */

.topbar {
    display: flex; align-items: center; gap: 28px;
    padding: 0 20px; height: 52px;
    background: var(--sheet); border-bottom: 1px solid var(--rule);
}
.brand {
    font: 650 20px/1 var(--serif); letter-spacing: -0.01em;
    color: var(--ink); text-decoration: none;
}
.topnav { display: flex; gap: 18px; flex: 1; }
.topnav a {
    color: var(--graphite); text-decoration: none; padding: 15px 0 13px; white-space: nowrap;
    border-bottom: 2px solid transparent;
}
.topnav a:hover { color: var(--ink); }
.topnav a[aria-current="page"] { color: var(--ink); border-bottom-color: var(--pencil); font-weight: 600; }
.signout { display: flex; align-items: center; gap: 14px; margin: 0; }
.who { color: var(--graphite); }

.link-button {
    background: none; border: 0; padding: 0; font: inherit; cursor: pointer;
    color: var(--pencil); text-decoration: underline; text-underline-offset: 2px;
}

/* ---------- layout ---------- */

.wrap { max-width: 1120px; margin: 0 auto; padding: 28px 20px 72px; }
.wrap.wide { max-width: 1440px; }

/* ---------- forms and buttons ---------- */

label { display: block; font-weight: 600; margin-bottom: 6px; }
.field { min-width: 0; }

input[type="text"], input[type="password"], input[type="search"], select, textarea {
    width: 100%; font: inherit; color: var(--ink);
    background: var(--sheet); border: 1px solid var(--rule-strong); border-radius: var(--radius);
    padding: 8px 10px;
}
textarea { resize: vertical; min-height: 5.5em; line-height: 1.45; }
input::placeholder, textarea::placeholder { color: var(--graphite); opacity: 0.8; }

.btn {
    font: inherit; font-weight: 550; cursor: pointer; white-space: nowrap;
    color: var(--ink); background: var(--sheet);
    border: 1px solid var(--rule-strong); border-radius: var(--radius);
    padding: 8px 16px;
}
.btn:hover:not(:disabled) { border-color: var(--ink); }
.btn.primary { background: var(--pencil); border-color: var(--pencil); color: var(--pencil-ink); }
.btn.primary:hover:not(:disabled) { filter: brightness(1.08); border-color: var(--pencil); }
.btn.danger { color: var(--error); }
.btn.small { padding: 4px 10px; font-size: 14px; }
.btn:disabled { opacity: 0.5; cursor: not-allowed; }

.stack > * + * { margin-top: 16px; }

/* ---------- login and error pages ---------- */

.login { max-width: 360px; margin: 14vh auto 0; padding: 0 20px; }
.wordmark { font: 650 30px/1.1 var(--serif); letter-spacing: -0.015em; }
.lede { color: var(--graphite); margin: 6px 0 24px; }
.form-error, .form-note {
    margin: 0 0 18px; padding: 8px 12px;
    border-left: 3px solid var(--error); background: var(--error-wash);
}
.form-note { border-left-color: var(--pencil); background: var(--pencil-wash); }

/* ---------- submit ---------- */

.submit h1 { margin-bottom: 14px; }
.submit-row {
    display: grid; grid-template-columns: minmax(180px, 360px) auto; gap: 14px;
    align-items: end; margin-top: 12px;
}
.results { list-style: none; margin: 14px 0 0; padding: 0; font-size: 14px; }
.results li { padding: 5px 0 5px 12px; border-left: 3px solid var(--done); margin-top: 6px; overflow-wrap: anywhere; }
.results li.fail { border-left-color: var(--error); }
.results .note { color: var(--graphite); }

/* ---------- jobs list ---------- */

.list { margin-top: 44px; }
.list-head { display: flex; flex-wrap: wrap; align-items: center; gap: 12px 24px; margin-bottom: 14px; }
.list-head h2 { margin-right: 4px; }
.filters { display: flex; flex-wrap: wrap; gap: 4px 18px; flex: 1; }
.filters button {
    font: inherit; background: none; border: 0; padding: 4px 0; cursor: pointer;
    color: var(--graphite); border-bottom: 2px solid transparent;
}
.filters button:hover { color: var(--ink); }
.filters button[aria-pressed="true"] { color: var(--ink); font-weight: 600; border-bottom-color: var(--pencil); }
.count { font-variant-numeric: tabular-nums; color: var(--graphite); font-weight: 400; margin-left: 2px; }
.search { width: 240px; max-width: 100%; }
.sort { display: flex; align-items: center; gap: 8px; font-size: 14px; }
.sort select { width: auto; }

.bulk {
    display: flex; flex-wrap: wrap; align-items: center; gap: 8px 10px;
    padding: 8px 12px; margin-bottom: 10px;
    background: var(--pencil-wash); border: 1px solid var(--rule);
}
.bulk[hidden] { display: none; }
.bulk-count { font-weight: 600; margin-right: 6px; }

table.jobs {
    width: 100%; border-collapse: collapse;
    background: var(--sheet); border: 1px solid var(--rule);
}
.jobs th {
    text-align: left; font-weight: 500; font-size: 13px; color: var(--graphite);
    padding: 9px 12px; border-bottom: 1px solid var(--rule);
}
.jobs td { padding: 12px; vertical-align: top; border-top: 1px solid var(--rule); }
.jobs tbody tr:first-child td { border-top: 0; }
.jobs tbody tr.clickable { cursor: pointer; }
.jobs tbody tr.clickable:hover { background: var(--pencil-wash); }
.col-check { width: 36px; }
.col-status { width: 124px; }
.col-time { width: 140px; text-align: right; }
.col-pub { width: 130px; }
.jobs td.col-pub { font-size: 13px; }
.pub { display: inline-block; margin: 0 4px 3px 0; padding: 1px 7px; border: 1px solid var(--rule-strong); border-radius: 3px; font-weight: 600; white-space: nowrap; }
.pub-live { border-color: var(--ink); color: var(--ink); }
.pub-draft { color: var(--graphite); font-weight: 400; border-style: dashed; }
.pub-none { color: var(--graphite); }
.job-url a.title { display: block; font-weight: 600; margin-bottom: 1px; }
.job-url a.title:hover { color: var(--pencil); }
.jobs td.col-time { color: var(--graphite); font-size: 14px; }
.jobs th.col-time { text-align: right; }

.job-url { overflow-wrap: anywhere; }
.job-url a { color: var(--ink); text-decoration: none; }
.job-url a:hover .host { color: var(--pencil); }
.job-url .host { font-weight: 600; }
.job-url .path { color: var(--graphite); }
.job-url .meta { display: block; margin-top: 3px; font-size: 13px; color: var(--graphite); }
.job-url .meta span + span::before { content: "  /  "; white-space: pre; color: var(--rule-strong); }
.job-url .err { display: block; margin-top: 4px; font-size: 13px; color: var(--error); }

.empty { padding: 32px 0; color: var(--graphite); text-align: center; }
.pager[hidden] { display: none; }
.pager { display: flex; align-items: center; justify-content: center; gap: 16px; margin-top: 18px; color: var(--graphite); }

/* ---------- status marker: shape carries meaning as well as colour ---------- */

.status { display: inline-flex; align-items: center; gap: 8px; white-space: nowrap; font-weight: 550; }
.status::before { content: ""; width: 9px; height: 9px; flex: none; background: currentColor; }
.s-done { color: var(--done); }
.s-error { color: var(--error); }
.s-error::before { transform: rotate(45deg); }
.s-queued, .s-fetching, .s-converting, .s-rewriting { color: var(--amber); }
.s-queued::before { background: transparent; border: 2px solid currentColor; }
.s-fetching::before, .s-converting::before, .s-rewriting::before { border-radius: 50%; animation: pulse 1.4s ease-in-out infinite; }
.s-cancelled { color: var(--slate); }
.s-cancelled::before { background: transparent; border: 2px solid currentColor; border-radius: 50%; }

@keyframes pulse { 50% { opacity: 0.3; } }

/* ---------- job page ---------- */

.crumb { margin: 0 0 10px; font-size: 14px; }
.crumb-row { display: flex; flex-wrap: wrap; align-items: baseline; justify-content: space-between; gap: 6px 16px; margin: 0 0 10px; }
.crumb-row .crumb { margin: 0; }
.next-job { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; }
.next-job select { width: 320px; max-width: 100%; }
.job-head { display: flex; flex-wrap: wrap; align-items: flex-start; justify-content: space-between; gap: 14px 24px; }
.job-title { min-width: 0; flex: 1 1 420px; }
.job-title h1 { overflow-wrap: anywhere; }
.job-title h1 .path { font-weight: 400; color: var(--graphite); }
.job-source { margin: 4px 0 0; font-size: 14px; }
.job-actions { display: flex; flex-wrap: wrap; gap: 8px; }
.job-meta { display: flex; flex-wrap: wrap; align-items: center; gap: 4px 20px; margin: 14px 0 0; color: var(--graphite); font-size: 14px; }
.banner { margin: 14px 0 0; padding: 9px 14px; border-left: 3px solid var(--error); background: var(--error-wash); overflow-wrap: anywhere; }
.banner[hidden] { display: none; }

.job-body { display: grid; grid-template-columns: minmax(0, 1fr) 320px; gap: 22px; align-items: start; margin-top: 22px; }
.job-body .desk { margin-top: 0; }
.job-body > .pictures { margin-top: 0; position: sticky; top: 12px; max-height: calc(100vh - 24px); overflow-y: auto; padding: 14px 16px 18px; background: var(--sheet); border: 1px solid var(--rule); }
.job-body > .pictures[hidden] { display: none; }
.job-body:has(> .pictures[hidden]) { grid-template-columns: minmax(0, 1fr); }
.pictures h2 { margin-top: 0; }
.desk {
    display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
    margin-top: 22px; background: var(--sheet); border: 1px solid var(--rule);
}
.pane { min-width: 0; padding: 0 30px 34px; }
.pane.rewrite { border-left: 1px solid var(--rule); box-shadow: inset 3px 0 0 var(--pencil); }
.pane-head {
    position: sticky; top: 0; z-index: 1;
    display: flex; flex-wrap: wrap; align-items: center; gap: 8px 14px;
    padding: 14px 0 12px; margin-bottom: 18px;
    background: var(--sheet); border-bottom: 1px solid var(--rule);
}
.pane-head h2 { margin-right: auto; }
.pane-tools { display: flex; flex-wrap: wrap; align-items: center; gap: 6px; }
.tools-label { font-size: 13px; color: var(--graphite); }
.pane-tools .seg button { padding: 4px 10px; font-size: 14px; }
.wp-tools { display: inline-flex; flex-wrap: wrap; align-items: center; gap: 6px 10px; margin-left: 6px; }
.wp-tools[hidden], .wp-tools a[hidden] { display: none; }
.wp-tools a { font-size: 14px; }
.pane-head .pane-note { flex-basis: 100%; margin: 0; }
.pane-head .pane-note[hidden] { display: none; }
.version { width: auto; max-width: 260px; padding: 4px 8px; font-size: 14px; }
.version[hidden] { display: none; }
.pane-note { margin: -4px 0 14px; font-size: 13px; color: var(--graphite); }
.pane-note[hidden] { display: none; }

.seg { display: inline-flex; border: 1px solid var(--rule-strong); border-radius: var(--radius); overflow: hidden; }
.seg button {
    font: inherit; font-size: 14px; background: var(--sheet); color: var(--graphite);
    border: 0; padding: 3px 12px; cursor: pointer;
}
.seg button + button { border-left: 1px solid var(--rule-strong); }
.seg button[aria-pressed="true"] { background: var(--pencil); color: var(--pencil-ink); }

.placeholder { color: var(--graphite); max-width: 52ch; }

.prose { font: 17px/1.65 var(--serif); max-width: 68ch; overflow-wrap: anywhere; }
.prose > :first-child { margin-top: 0; }
.prose h1, .prose h2, .prose h3, .prose h4 { font-family: var(--serif); line-height: 1.25; margin: 1.5em 0 0.5em; }
.prose h1 { font-size: 1.55em; }
.prose h2 { font-size: 1.3em; }
.prose h3 { font-size: 1.12em; }
.prose p, .prose ul, .prose ol, .prose pre, .prose table, .prose blockquote { margin: 0 0 1em; }
.prose ul, .prose ol { padding-left: 1.4em; }
.prose li + li { margin-top: 0.25em; }
.prose blockquote { padding-left: 1em; border-left: 3px solid var(--rule-strong); color: var(--graphite); }
.prose code { font: 0.86em var(--mono); background: var(--paper); padding: 0.1em 0.35em; border-radius: 3px; }
.prose pre { overflow-x: auto; padding: 12px 14px; background: var(--paper); border-radius: var(--radius); }
.prose pre code { background: none; padding: 0; }
.prose table { border-collapse: collapse; display: block; overflow-x: auto; }
.prose th, .prose td { border: 1px solid var(--rule); padding: 5px 10px; text-align: left; }
.prose img { max-width: 100%; height: auto; }
.prose hr { border: 0; border-top: 1px solid var(--rule); margin: 1.6em 0; }

.raw { margin: 0; font: 13px/1.6 var(--mono); white-space: pre-wrap; overflow-wrap: anywhere; }

/* ---------- activity ---------- */

.activity { margin-top: 28px; }
.activity summary { cursor: pointer; font-weight: 600; padding: 6px 0; }
.activity-grid { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1.2fr); gap: 28px; margin-top: 12px; }
.activity h3 { margin-bottom: 10px; }
.timeline { list-style: none; margin: 0; padding: 0; }
.timeline li { display: grid; grid-template-columns: 8.5em 1fr; gap: 12px; padding: 7px 0; border-top: 1px solid var(--rule); font-size: 14px; }
.timeline li:first-child { border-top: 0; }
.timeline time { color: var(--graphite); font-variant-numeric: tabular-nums; }
.timeline .msg { overflow-wrap: anywhere; }
table.attempts { width: 100%; border-collapse: collapse; font-size: 14px; }
.attempts th { text-align: left; font-weight: 500; color: var(--graphite); padding: 6px 10px 6px 0; border-bottom: 1px solid var(--rule); }
.attempts td { padding: 7px 10px 7px 0; border-top: 1px solid var(--rule); vertical-align: top; overflow-wrap: anywhere; }
.attempts tbody tr:first-child td { border-top: 0; }
.attempts .fail { color: var(--error); }

/* ---------- catalog: writers, personas, models ---------- */

.catalog-head { display: flex; flex-wrap: wrap; align-items: flex-start; justify-content: space-between; gap: 12px 24px; margin-bottom: 22px; }
.catalog-head .lede { margin: 6px 0 0; max-width: 62ch; }
.catalog { display: grid; grid-template-columns: minmax(280px, 380px) minmax(0, 1fr); gap: 32px; align-items: start; }
.items { list-style: none; margin: 0; padding: 0; border-top: 1px solid var(--rule); }
.items li { border-bottom: 1px solid var(--rule); }
.item {
    display: grid; grid-template-columns: minmax(0, 1fr) auto; gap: 2px 12px; width: 100%;
    padding: 11px 12px; text-align: left; font: inherit; color: var(--ink);
    background: none; border: 0; border-left: 3px solid transparent; cursor: pointer;
}
.item:hover { background: var(--pencil-wash); }
.item[aria-current="true"] { background: var(--pencil-wash); border-left-color: var(--pencil); }
.item-title { font-weight: 600; overflow-wrap: anywhere; }
.item-meta { grid-column: 1 / -1; font-size: 13px; color: var(--graphite); overflow-wrap: anywhere; }
.item-note { grid-column: 1 / -1; font-size: 13px; color: var(--amber); }
.item .status { font-size: 14px; }

.editor {
    min-width: 0; padding: 22px 26px 26px;
    background: var(--sheet); border: 1px solid var(--rule); border-radius: var(--radius);
    box-shadow: inset 3px 0 0 var(--pencil);
}
.editor[hidden], .tester[hidden], [hidden].fld-error { display: none; }
.editor h2 { font-size: 18px; }
.fld > label { margin-bottom: 5px; }
.fld-hint { margin: 5px 0 0; font-size: 13px; color: var(--graphite); max-width: 62ch; }
.fld-error { margin: 5px 0 0; font-size: 13px; color: var(--error); }
input[aria-invalid="true"], select[aria-invalid="true"], textarea[aria-invalid="true"] { border-color: var(--error); }
input[readonly] { color: var(--graphite); background: var(--paper); }
.check { display: flex; align-items: center; gap: 10px; margin: 0; font-weight: 500; cursor: pointer; }
.check input { width: 16px; height: 16px; margin: 0; accent-color: var(--pencil); }
textarea.mono, .mono { font-family: var(--mono); font-size: 13px; }
.editor-actions { display: flex; flex-wrap: wrap; align-items: center; gap: 10px; }
.editor-actions .danger { margin-left: auto; }
.tester { margin-top: 30px; padding-top: 22px; border-top: 1px solid var(--rule); }
.tester > p { margin: 4px 0 14px; }
.tester-result { padding-top: 6px; }
.tester-meta { margin: 0 0 10px; font-size: 13px; color: var(--graphite); }
.tester-result .prose { padding: 14px 18px; background: var(--paper); border-radius: var(--radius); margin-bottom: 8px; }

/* ---------- providers and selection rules ---------- */

.facts { display: grid; grid-template-columns: max-content minmax(0, 1fr); gap: 4px 18px; margin: 0; font-size: 14px; }
.facts dt { color: var(--graphite); }
.facts dd { margin: 0; overflow-wrap: anywhere; }
.rules-box { margin-top: 30px; padding-top: 22px; border-top: 1px solid var(--rule); }
.rules-box > p { margin: 4px 0 14px; }
.rule-list { list-style: none; margin: 0 0 14px; padding: 0; border-top: 1px solid var(--rule); }
.rule-row { display: flex; flex-wrap: wrap; align-items: baseline; justify-content: space-between; gap: 4px 16px; padding: 10px 0; border-bottom: 1px solid var(--rule); }
.rule-text { min-width: 0; flex: 1 1 320px; }
.rule-code { margin-left: 10px; font: 12px var(--mono); color: var(--graphite); }
.rule-tools { display: flex; gap: 14px; }
.rule-form { margin: 0 0 14px; padding: 14px 16px; background: var(--paper); border-radius: var(--radius); }
.rule-form[hidden], .rules-box [hidden] { display: none; }
.rule-form h3 { margin-bottom: 10px; }
.rule-fields { display: grid; grid-template-columns: repeat(auto-fit, minmax(210px, 1fr)); gap: 12px 16px; margin-bottom: 12px; }
.rule-fields .fld[hidden] { display: none; }

.default-note { margin: 0 0 20px; padding: 8px 12px; border-left: 3px solid var(--pencil); background: var(--pencil-wash); max-width: 90ch; }
.default-note:empty { display: none; }
.default-note.bad { border-left-color: var(--error); background: var(--error-wash); }
.catalog-rules { grid-template-columns: minmax(300px, 1fr) minmax(0, 1.1fr); }
.side { min-width: 0; display: grid; gap: 24px; }
.side .editor[hidden] { display: none; }
.rule-items { counter-reset: none; }
.rule-item { display: grid; grid-template-columns: 2.2em minmax(0, 1fr) auto; align-items: start; }
.rule-item .item { padding-left: 4px; }
.ordinal { padding-top: 12px; text-align: center; font: 600 14px var(--mono); color: var(--graphite); font-variant-numeric: tabular-nums; }
.move { display: flex; flex-direction: column; gap: 4px; padding: 8px 8px 8px 0; }
fieldset.conditions { margin: 0; padding: 14px 16px 16px; border: 1px solid var(--rule); border-radius: var(--radius); min-width: 0; }
fieldset.conditions legend { padding: 0 6px; font-weight: 600; }
fieldset.conditions > p { margin: 0 0 12px; font-size: 14px; }
.cond-row { margin-bottom: 14px; }
.cond-inputs { display: grid; grid-template-columns: minmax(135px, 0.9fr) minmax(130px, 1fr) minmax(110px, 1.2fr) auto; gap: 8px; align-items: center; }
.cond-row .fld-hint, .cond-row .fld-error { margin-top: 4px; }
.tester-panel h2 + p { margin: 4px 0 14px; }
.trace { margin-top: 20px; padding-top: 16px; border-top: 1px solid var(--rule); }
.trace-context { margin: 0 0 6px; font-size: 14px; color: var(--graphite); }
.trace-answer { margin: 0 0 14px; font-size: 16px; }
.trace-answer.bad { color: var(--error); }
.trace-list { list-style: none; margin: 0; padding: 0; border-top: 1px solid var(--rule); }
.trace-group { padding: 10px 0; border-bottom: 1px solid var(--rule); }
.trace-head { display: grid; grid-template-columns: 2.2em minmax(0, 1fr) auto; align-items: baseline; gap: 0 4px; }
.trace-head .ordinal { padding-top: 0; }
.trace-title { overflow-wrap: anywhere; font-weight: 550; }
.trace-chosen { background: var(--pencil-wash); margin: 0 -10px; padding: 10px; }
.trace-after_choice, .trace-off, .trace-matched_but_later { color: var(--graphite); }
.trace-conds { list-style: none; margin: 6px 0 0 2.4em; padding: 0; font-size: 14px; }
.trace-conds li { padding: 2px 0; }
.trace-conds .status { font-size: 13px; margin-right: 6px; }
.trace-group .item-note { margin: 4px 0 0 2.4em; }
.trace-default { margin: 14px 0 0; font-size: 14px; }

/* ---------- dialog and toast ---------- */

dialog {
    width: calc(100% - 32px); max-width: 440px; padding: 22px 24px;
    color: var(--ink); background: var(--sheet);
    border: 1px solid var(--rule-strong); border-radius: 6px;
}
dialog::backdrop { background: rgba(10, 12, 16, 0.5); }
dialog p { margin: 8px 0 0; color: var(--graphite); }
.dialog-actions { display: flex; justify-content: flex-end; gap: 10px; }

.toast {
    position: fixed; left: 50%; bottom: 24px; z-index: 20;
    max-width: calc(100% - 32px); padding: 10px 16px;
    transform: translateX(-50%);
    color: var(--paper); background: var(--ink); border-radius: var(--radius);
    opacity: 0; pointer-events: none; transition: opacity 0.15s;
}
.toast.show { opacity: 1; }

/* ---------- small screens ---------- */

@media (max-width: 1180px) {
    .job-body { grid-template-columns: minmax(0, 1fr); }
    .job-body > .pictures { position: static; max-height: none; order: -1; }
}

@media (max-width: 960px) {
    .catalog, .catalog-rules { grid-template-columns: minmax(0, 1fr); gap: 22px; }
    .desk { grid-template-columns: minmax(0, 1fr); }
    .pane.rewrite { border-left: 0; border-top: 1px solid var(--rule); box-shadow: inset 0 3px 0 var(--pencil); }
    .pane { padding: 0 18px 28px; }
    .activity-grid { grid-template-columns: minmax(0, 1fr); }
}

@media (max-width: 640px) {
    .topbar { flex-wrap: wrap; height: auto; gap: 0 12px; padding: 10px 14px 0; }
    .signout { margin-left: auto; }
    .topnav { order: 3; flex: 1 0 100%; flex-wrap: wrap; gap: 0 16px; margin-top: 4px; }
    .topnav a { padding: 8px 0 7px; }
    .editor { padding: 18px 16px 22px; }
    .cond-inputs { grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); }
    .cond-inputs input { grid-column: 1 / -1; }
    .trace-head { grid-template-columns: 2.2em minmax(0, 1fr); }
    .trace-head .status { grid-column: 2; }
    .who { display: none; }
    .wrap { padding: 20px 14px 56px; }
    .submit-row { grid-template-columns: minmax(0, 1fr); }
    .submit-row .btn { width: 100%; }
    .search { width: 100%; }
    .col-status { width: 96px; }
    .jobs .col-time { display: none; }
    .col-pub { width: 84px; }
    .jobs td, .jobs th { padding: 10px 8px; }
    .timeline li { grid-template-columns: 1fr; gap: 0; }
}

@media (prefers-reduced-motion: reduce) {
    .s-fetching::before, .s-converting::before, .s-rewriting::before { animation: none; }
    .toast { transition: none; }
}

/* ---------- dashboard ---------- */

.dash-section { margin-top: 36px; min-width: 0; }
.dash-section h2 { margin-bottom: 10px; }
.dash-head { display: flex; flex-wrap: wrap; align-items: baseline; justify-content: space-between; gap: 6px 24px; margin-bottom: 10px; }
.dash-head h2 { margin-bottom: 0; }
.small-note { margin: -4px 0 10px; font-size: 13px; }

.worker-box { padding: 14px 18px; background: var(--sheet); border: 1px solid var(--rule); border-radius: var(--radius); }
.worker-line { display: flex; flex-wrap: wrap; align-items: baseline; gap: 4px 14px; }
.worker-line .status { font-weight: 650; }
.worker-line .when { color: var(--graphite); font-size: 14px; }
.worker-help { margin: 8px 0 0; font-size: 14px; color: var(--graphite); }
.worker-help code { font: 13px var(--mono); background: var(--paper); padding: 2px 6px; border-radius: var(--radius); user-select: all; }
.worker-help[hidden], .worker-more[hidden] { display: none; }
.worker-more { margin-top: 10px; font-size: 14px; }
.worker-more summary, .table-alt summary { cursor: pointer; color: var(--graphite); }
#worker-alert { margin-top: 12px; }

.figures { display: grid; grid-template-columns: repeat(auto-fit, minmax(128px, 1fr)); margin-top: 18px; border-top: 1px solid var(--rule); border-bottom: 1px solid var(--rule); }
.fig { padding: 14px 16px 12px 0; }
.fig + .fig { padding-left: 16px; border-left: 1px solid var(--rule); }
.fig-num { display: block; font: 650 28px/1.1 var(--serif); font-variant-numeric: tabular-nums; }
.fig-num.bad { color: var(--error); }
.fig-label { display: block; margin-top: 4px; font-size: 13px; color: var(--graphite); }

.legend { display: flex; flex-wrap: wrap; gap: 4px 16px; margin: 0; padding: 0; list-style: none; font-size: 13px; color: var(--graphite); }
.legend li::before { content: ""; display: inline-block; width: 10px; height: 10px; margin-right: 6px; background: var(--swatch, var(--slate)); }
.legend .k-done { --swatch: var(--done); }
.legend .k-error { --swatch: var(--error); }
.legend .k-cancelled { --swatch: var(--slate); }
.legend .k-other { --swatch: var(--amber); }

.chart { position: relative; padding: 4px 0 0 34px; }
.plot { position: relative; height: 190px; border-bottom: 1px solid var(--rule-strong); }
.gridline { position: absolute; left: 0; right: 0; height: 0; border-top: 1px dashed var(--rule); }
.gridline span { position: absolute; left: -34px; top: -9px; width: 28px; text-align: right; font-size: 12px; color: var(--graphite); font-variant-numeric: tabular-nums; }
.bars { position: absolute; inset: 0; display: flex; align-items: stretch; gap: 3px; }
.bar { flex: 1 1 0; min-width: 0; display: flex; flex-direction: column-reverse; }
.bar:hover { background: var(--pencil-wash); }
.seg-done, .seg-error, .seg-cancelled, .seg-other { display: block; width: 100%; }
.seg-done { background: var(--done); }
.seg-error { background: var(--error); }
.seg-cancelled { background: var(--slate); }
.seg-other { background: var(--amber); }
.axis { display: flex; justify-content: space-between; margin-top: 6px; font-size: 12px; color: var(--graphite); }

.table-alt { margin-top: 10px; font-size: 14px; }

table.plain { width: 100%; border-collapse: collapse; font-size: 14px; margin-top: 8px; }
table.plain th { text-align: left; font-weight: 600; color: var(--graphite); padding: 6px 12px 6px 0; border-bottom: 1px solid var(--rule-strong); }
table.plain td { padding: 8px 12px 8px 0; border-top: 1px solid var(--rule); vertical-align: middle; }
table.plain tbody tr:first-child td { border-top: 0; }
table.plain .num { text-align: right; font-variant-numeric: tabular-nums; }
table.plain th.num { text-align: right; }
table.plain td:last-child, table.plain th:last-child { padding-right: 0; }
.cell-sub { display: block; font-size: 12px; color: var(--graphite); }
.rate { display: flex; align-items: center; gap: 8px; min-width: 110px; }
.rate b { font-weight: 600; min-width: 3.2em; text-align: right; font-variant-numeric: tabular-nums; }
.meter { flex: 1; height: 6px; min-width: 40px; background: var(--rule); }
.meter span { display: block; height: 100%; background: var(--error); }

.dash-cols { display: grid; grid-template-columns: minmax(0, 3fr) minmax(0, 2fr); gap: 0 40px; align-items: start; }
.plain-list { list-style: none; margin: 0; padding: 0; font-size: 14px; }
.plain-list li { display: flex; justify-content: space-between; gap: 16px; padding: 8px 0; border-top: 1px solid var(--rule); }
.plain-list li:first-child { border-top: 0; }
.plain-list .n { font-variant-numeric: tabular-nums; font-weight: 600; }
.plain-list .who-fails { color: var(--graphite); }

.fail-list { list-style: none; margin: 0; padding: 0; font-size: 14px; }
.fail-list li { display: grid; grid-template-columns: minmax(0, 1fr) auto; gap: 2px 20px; padding: 10px 0; border-top: 1px solid var(--rule); }
.fail-list li:first-child { border-top: 0; }
.fail-list a { color: var(--ink); text-decoration: none; overflow-wrap: anywhere; }
.fail-list a:hover .host { color: var(--pencil); }
.fail-list .host { font-weight: 600; }
.fail-list .path { color: var(--graphite); }
.fail-list .msg { grid-column: 1 / -1; color: var(--error); overflow-wrap: anywhere; }
.fail-list .when { color: var(--graphite); white-space: nowrap; }

.cleanup { max-width: 640px; }
.cleanup-form { display: flex; flex-wrap: wrap; align-items: flex-end; gap: 12px; margin-top: 14px; }
.cleanup-form .fld { width: 150px; }
.cleanup-result { margin-top: 14px; padding: 10px 14px; border-left: 3px solid var(--pencil); background: var(--pencil-wash); }
.cleanup-result[hidden] { display: none; }
.cleanup-result p { margin: 0 0 10px; }
.cleanup-result button[hidden] { display: none; }
.cleanup-result button { margin-bottom: 2px; }
.cleanup p.muted { margin: 12px 0 0; font-size: 14px; }

@media (max-width: 860px) {
    .dash-cols { grid-template-columns: minmax(0, 1fr); }
    .figures { grid-template-columns: repeat(2, minmax(0, 1fr)); }
    .fig:nth-child(odd) { padding-left: 0; border-left: 0; }
    .fig:nth-child(n+3) { border-top: 1px solid var(--rule); }
}
@media (max-width: 560px) {
    .plot { height: 150px; }
    .bars { gap: 1px; }
    table.plain th, table.plain td { padding-right: 8px; }
}

/* Pictures: a side panel next to the text */
.pictures { margin-top: 28px; }
.pic-form { display: grid; grid-template-columns: minmax(0, 1fr); gap: 12px; align-items: start; margin: 12px 0 18px; }
.pic-form[hidden] { display: none; }
.pic-form .pic-actions { grid-column: 1 / -1; display: flex; flex-wrap: wrap; align-items: center; gap: 8px 14px; }
.pic-grid { list-style: none; margin: 0; padding: 0; display: grid; grid-template-columns: minmax(0, 1fr); gap: 14px; }
.pic-card { background: var(--sheet); border: 1px solid var(--rule); border-radius: var(--radius); padding: 10px; display: flex; flex-direction: column; gap: 8px; min-width: 0; }
.pic-frame { background: var(--paper); border-radius: var(--radius); min-height: 120px; display: flex; align-items: center; justify-content: center; overflow: hidden; }
.pic-frame img { display: block; max-width: 100%; height: auto; }
.pic-state { margin: 0; padding: 16px; text-align: center; color: var(--graphite); }
.pic-state.error { color: var(--error); }
.pic-meta { margin: 0; font-size: 13px; color: var(--graphite); }
.pic-prompt-view summary { cursor: pointer; font-size: 13px; }
.pic-prompt-view p { margin: 6px 0 0; font-size: 13px; white-space: pre-wrap; overflow-wrap: anywhere; }
.pic-tools { display: flex; flex-wrap: wrap; gap: 6px; margin-top: auto; }
.test-picture { display: block; max-width: 100%; height: auto; border: 1px solid var(--rule); border-radius: var(--radius); }
.test-prompt { font-size: 13px; color: var(--graphite); white-space: pre-wrap; overflow-wrap: anywhere; }
@media (max-width: 720px) { .pic-form { grid-template-columns: minmax(0, 1fr); } }
.pic-card.cover { border-color: var(--pencil); box-shadow: 0 0 0 1px var(--pencil); }
.pic-badge { margin: 0; font-size: 13px; font-weight: 600; color: var(--pencil); }
@media (max-width: 1180px) { .pic-grid { grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); } }

/* Editing a version by hand */
.editor-form { display: grid; gap: 12px; }
.desk .pane.source .pane-body { max-height: min(78vh, 960px); overflow-y: auto; padding: 4px 16px 4px 0; border-bottom: 1px solid var(--rule); }
.desk .pane.source .pane-body:focus-visible { outline: 2px solid var(--pencil); outline-offset: 2px; }
.desk.editing .pane.source .pane-body { height: min(78vh, 960px); }
.desk.editing .editor-form textarea { height: calc(min(78vh, 960px) - 150px); min-height: 280px; resize: vertical; }
.editor-form textarea { min-height: 340px; font: 15px/1.5 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; }
.editor-form .editor-hint { margin: 0; font-size: 13px; color: var(--graphite); }
.editor-form .editor-actions { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; }

@media (max-width: 960px) { .desk .pane.source .pane-body { max-height: 46vh; } .desk.editing .pane.source .pane-body { height: 36vh; } .desk.editing .editor-form textarea { height: 46vh; } }

/* ---- tags (M22) ---- */
.tag-box { margin: 14px 0 0; max-width: 760px; }
.tag-box label { display: block; font-weight: 600; margin-bottom: 4px; }
.tag-row { display: flex; gap: 8px; align-items: center; }
.tag-row input[type="text"], .tag-row select { flex: 1 1 auto; min-width: 0; }
.tag-chips { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 8px; }
.tag-chips:empty { display: none; }
.chip { font: inherit; font-size: 13px; padding: 3px 10px; border: 1px solid var(--rule-strong); border-radius: 999px; background: var(--sheet); color: var(--ink); cursor: pointer; }
.chip:hover { border-color: var(--ink); }
.chip.on { background: var(--ink); color: var(--sheet); border-color: var(--ink); }
.chip.more { border-style: dashed; color: var(--graphite); }
.tag-box .muted { margin: 6px 0 0; font-size: 13px; }
.bulk-tags { flex-basis: 100%; display: flex; flex-wrap: wrap; align-items: center; gap: 8px; margin-top: 8px; }
.bulk-tags input[type="text"] { flex: 1 1 220px; min-width: 0; }
.bulk-tags .tag-chips { flex-basis: 100%; margin-top: 0; }
.job-url .tags { display: flex; flex-wrap: wrap; gap: 4px; margin: 3px 0 0; }
.tag { font-size: 12px; padding: 0 7px; border: 1px solid var(--rule); border-radius: 999px; color: var(--graphite); }
.tag-tools { margin: 18px 0 22px; max-width: 760px; display: grid; gap: 10px; }
.tag-list { list-style: none; margin: 0; padding: 0; display: grid; grid-template-columns: repeat(auto-fill, minmax(260px, 1fr)); gap: 8px; }
.tag-item { display: flex; align-items: baseline; gap: 10px; padding: 8px 12px; border: 1px solid var(--rule); background: var(--sheet); }
.tag-name { font-weight: 600; margin-right: auto; overflow-wrap: anywhere; }
.tag-uses { font-size: 13px; color: var(--graphite); }
.tag-row[hidden], .bulk-tags[hidden], .tag-box[hidden], .tag-chips[hidden], #import-note[hidden], #import-target[hidden] { display: none; }
.version-foot { padding: 18px 20px 28px; text-align: center; font-size: 12px; color: var(--graphite); }

/* ---- automatic mode (M23) ---- */
.auto-field { align-self: end; }
.auto-field .check { display: flex; align-items: center; gap: 6px; font-weight: 600; margin: 0; }
.auto-badge { display: inline-block; margin: 3px 6px 0 0; padding: 0 7px; font-size: 12px; font-weight: 600; border: 1px solid var(--rule-strong); border-radius: 999px; color: var(--ink); }
.auto-badge.auto-done { color: #176b3a; border-color: #176b3a; }
.auto-badge.auto-failed { color: #b3261e; border-color: #b3261e; }
.auto-box { margin: 12px 0 0; max-width: 760px; padding: 10px 14px; border: 1px solid var(--rule-strong); background: var(--sheet); }
.auto-box.stopped { border-color: #b3261e; }
.auto-box p { margin: 0 0 8px; }
.auto-actions { display: flex; gap: 8px; flex-wrap: wrap; }
.auto-box[hidden], .auto-field[hidden] { display: none; }
