:root {
    --bg: #0d1117;
    --panel: #141b22;
    --panel-2: #1a232d;
    --border: #263340;
    --border-soft: #1e2933;
    --text: #d8e0e8;
    --muted: #8b99a7;
    --accent: #4682b4;
    --accent-bright: #5f9fd0;
    --accent-dim: rgba(70, 130, 180, 0.14);
    --ok: #4caf7d;
    --warn: #d0904a;
    --fail: #d06060;
    --radius: 8px;
    --font: "Segoe UI", system-ui, -apple-system, sans-serif;
    --mono: "Cascadia Code", Consolas, "SF Mono", 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.55;
}

h1, h2, h3 { font-weight: 600; letter-spacing: -0.01em; }
h1 { font-size: 1.7rem; margin: 0.2rem 0 0.4rem; }
h2 { font-size: 1.15rem; margin: 0; }
h3 { font-size: 1.02rem; margin: 0 0 0.4rem; }

a { color: var(--accent-bright); text-decoration: none; }
a:hover { text-decoration: underline; }

h1:focus { outline: none; }

/* ---- shell ---- */

.shell { display: flex; min-height: 100vh; }

.sidebar {
    width: 230px;
    flex-shrink: 0;
    background: #0a0e13;
    border-right: 1px solid var(--border-soft);
    padding: 1.1rem 0.9rem;
    display: flex;
    flex-direction: column;
    gap: 0.35rem;
    position: sticky;
    top: 0;
    height: 100vh;
    overflow-y: auto;
}

.brand {
    font-size: 1.12rem;
    font-weight: 700;
    color: var(--text);
    margin-bottom: 0.9rem;
    letter-spacing: 0.01em;
}
.brand span { color: var(--accent-bright); font-weight: 500; }
.brand:hover { text-decoration: none; }

.side-label {
    font-size: 0.7rem;
    text-transform: uppercase;
    letter-spacing: 0.09em;
    color: var(--muted);
    margin: 0.7rem 0 0.2rem 0.35rem;
}

.side-nav { display: flex; flex-direction: column; gap: 2px; }

.side-link {
    padding: 0.38rem 0.6rem;
    border-radius: 6px;
    color: var(--text);
    font-size: 0.92rem;
}
.side-link:hover { background: var(--panel-2); text-decoration: none; }
.side-link.active { background: var(--accent-dim); color: var(--accent-bright); }

.side-foot { margin-top: auto; font-size: 0.75rem; padding: 0.5rem 0.35rem 0; }
.side-jobs-link { display: inline-flex; align-items: center; gap: 0.4rem; }
/* .running/.awaiting also set a colour; in the sidebar only the dot should. */
.side-jobs-link.running, .side-jobs-link.awaiting { color: inherit; font-size: inherit; }
.key-ok { color: var(--ok); }
.key-missing { color: var(--warn); }

.content {
    flex: 1;
    padding: 1.6rem 2.2rem 3rem;
    max-width: 1060px;
}

/* ---- common ---- */

.page-head { margin-bottom: 1.3rem; }
.lead { color: var(--muted); margin: 0.2rem 0 0; max-width: 70ch; }
.back { font-size: 0.85rem; color: var(--muted); display: inline-block; margin-bottom: 0.3rem; }

.card-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
    gap: 0.9rem;
    margin: 1.1rem 0;
}

.card {
    background: var(--panel);
    border: 1px solid var(--border-soft);
    border-radius: var(--radius);
    padding: 1rem 1.1rem;
    color: var(--text);
    transition: border-color 0.15s;
}
.card:hover { border-color: var(--accent); text-decoration: none; }

.kit-card .kit-title:hover { text-decoration: none; }
.kit-card .kit-title h3 { color: var(--accent-bright); }
.kit-concept { color: var(--muted); font-size: 0.86rem; margin: 0.35rem 0 0.6rem; }

/* Stretched link: the whole kit card navigates like the title does. The card
   also contains child variation links, so a wrapping <a> would nest anchors —
   instead the title link grows a full-card overlay and the children row sits
   above it. */
.kit-card { position: relative; }
.kit-card .kit-title::after { content: ""; position: absolute; inset: 0; }

.children { margin-top: 0.6rem; border-top: 1px solid var(--border-soft); padding-top: 0.5rem; display: flex; flex-direction: column; gap: 2px; position: relative; z-index: 1; }
.child-link { font-size: 0.86rem; }

.chip-row { display: flex; gap: 0.4rem; flex-wrap: wrap; align-items: center; }
.chip {
    font-size: 0.74rem;
    background: var(--panel-2);
    border: 1px solid var(--border-soft);
    border-radius: 999px;
    padding: 0.1rem 0.6rem;
    color: var(--muted);
    white-space: nowrap;
}
.chip.accent { color: var(--accent-bright); border-color: var(--accent); background: var(--accent-dim); }
.chip a { color: var(--accent-bright); }

.panel {
    background: var(--panel);
    border: 1px solid var(--border-soft);
    border-radius: var(--radius);
    padding: 1rem 1.2rem;
    margin: 0.9rem 0;
}

details.panel > summary {
    cursor: pointer;
    font-weight: 600;
    color: var(--accent-bright);
    user-select: none;
}
details.panel[open] > summary { margin-bottom: 0.8rem; }

/* ---- forms ---- */

.form-row { display: flex; gap: 0.6rem; margin: 0.6rem 0; align-items: center; flex-wrap: wrap; }

.input {
    background: var(--bg);
    border: 1px solid var(--border);
    border-radius: 6px;
    color: var(--text);
    padding: 0.5rem 0.7rem;
    font-family: var(--font);
    font-size: 0.92rem;
    width: 100%;
}
.input:focus { outline: none; border-color: var(--accent); }
.input.slim { width: auto; min-width: 220px; }
textarea.input { resize: vertical; }
select.input { appearance: auto; }

.btn {
    background: var(--panel-2);
    color: var(--text);
    border: 1px solid var(--border);
    border-radius: 6px;
    padding: 0.5rem 1rem;
    font-size: 0.9rem;
    cursor: pointer;
    transition: background 0.15s, border-color 0.15s;
    font-family: var(--font);
}
.btn:hover:not(:disabled) { border-color: var(--accent); }
.btn:disabled { opacity: 0.45; cursor: not-allowed; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.primary:hover:not(:disabled) { background: var(--accent-bright); }
.btn.small { padding: 0.2rem 0.7rem; font-size: 0.78rem; }

.check { display: inline-flex; align-items: center; gap: 0.3rem; font-size: 0.83rem; color: var(--muted); margin-right: 0.7rem; cursor: pointer; }
.check input { accent-color: var(--accent); }

.ref-row { margin-top: 0.5rem; }
.hint { color: var(--muted); font-size: 0.82rem; }
.hint.warn { color: var(--warn); display: block; margin-top: 0.5rem; }

/* ---- jobs ---- */

.jobs-strip { display: flex; flex-direction: column; gap: 4px; margin: 0.8rem 0; }
.job {
    display: flex;
    align-items: center;
    gap: 0.55rem;
    background: var(--panel);
    border: 1px solid var(--border-soft);
    border-radius: 6px;
    padding: 0.4rem 0.8rem;
    font-size: 0.85rem;
}
.job-title { font-weight: 500; }
.job-detail { color: var(--muted); font-size: 0.8rem; }

.dot { width: 8px; height: 8px; border-radius: 50%; flex-shrink: 0; display: inline-block; }
/* Status colors ride on the status class itself (.job, .job-status chip, the sidebar link). */
.running .dot { background: var(--accent-bright); animation: pulse 1.2s ease-in-out infinite; }
/* Parked on purpose — waiting on a person, not broken and not in flight. */
.awaiting .dot { background: var(--warn); }
.done .dot { background: var(--ok); }
.failed .dot { background: var(--fail); }
.cancelled .dot, .interrupted .dot { background: var(--muted); }

.running { color: var(--accent-bright); font-size: 0.88rem; display: inline-flex; align-items: center; gap: 0.5rem; }
.awaiting { color: var(--warn); font-size: 0.88rem; display: inline-flex; align-items: center; gap: 0.5rem; }

@keyframes pulse { 0%, 100% { opacity: 1; } 50% { opacity: 0.3; } }

/* ---- kit page ---- */

.variation { border-left: 3px solid var(--accent); }
.variation-head { display: flex; justify-content: space-between; align-items: baseline; gap: 1rem; flex-wrap: wrap; }
.descriptor { color: var(--muted); font-style: italic; margin: 0.3rem 0 0.6rem; font-size: 0.9rem; }

.block-head {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin: 0.9rem 0 0.3rem;
    font-size: 0.78rem;
    text-transform: uppercase;
    letter-spacing: 0.08em;
    color: var(--muted);
}

.code {
    background: #0a0f14;
    border: 1px solid var(--border-soft);
    border-radius: 6px;
    padding: 0.7rem 0.9rem;
    font-family: var(--mono);
    font-size: 0.78rem;
    line-height: 1.6;
    overflow-x: auto;
    white-space: pre-wrap;
    word-break: break-word;
    margin: 0;
    color: #b9c6d2;
}
.code.styles { color: #9fc1dd; }

.melody-row { display: flex; align-items: center; gap: 1rem; flex-wrap: wrap; margin-top: 0.3rem; }
.melody-row audio { height: 36px; flex: 1; min-width: 260px; filter: invert(0.88) hue-rotate(180deg) saturate(0.7); }
.melody-links { display: flex; gap: 0.8rem; align-items: center; font-size: 0.84rem; }

/* ---- markdown render ---- */

.md-render { font-size: 0.92rem; }
.md-render h1 { font-size: 1.4rem; border-bottom: 1px solid var(--border-soft); padding-bottom: 0.4rem; }
.md-render h2 { font-size: 1.15rem; margin: 1.4rem 0 0.5rem; color: var(--accent-bright); }
.md-render h3 { margin: 1rem 0 0.4rem; }
.md-render pre {
    background: #0a0f14;
    border: 1px solid var(--border-soft);
    border-radius: 6px;
    padding: 0.7rem 0.9rem;
    overflow-x: auto;
    font-size: 0.78rem;
    line-height: 1.6;
}
.md-render code { font-family: var(--mono); color: #9fc1dd; }
.md-render blockquote { border-left: 3px solid var(--accent); margin: 0.8rem 0; padding: 0.2rem 1rem; color: var(--muted); }
.md-render table { border-collapse: collapse; }
.md-render th, .md-render td { border: 1px solid var(--border-soft); padding: 0.3rem 0.7rem; font-size: 0.85rem; }
.md-render hr { border: none; border-top: 1px solid var(--border-soft); margin: 1.4rem 0; }

/* ---- misc ---- */

.new-project { max-width: 480px; }

.valid.modified:not([type=checkbox]) { outline: 1px solid var(--ok); }
.invalid { outline: 1px solid var(--fail); }
.validation-message { color: var(--fail); }

/* ---- auth ---- */

.auth-shell {
    min-height: 100vh;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 2rem 1rem;
}

.login-card {
    width: 100%;
    max-width: 380px;
    background: var(--panel);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 2rem 2.2rem 2.2rem;
    display: flex;
    flex-direction: column;
    gap: 0.9rem;
}

.login-card .brand { font-size: 1.25rem; font-weight: 600; }
.login-sub { color: var(--muted); font-size: 0.85rem; margin: 0; }

.login-card form { display: flex; flex-direction: column; gap: 0.9rem; }
.login-card label {
    display: flex;
    flex-direction: column;
    gap: 0.3rem;
    font-size: 0.82rem;
    color: var(--muted);
}
.login-card input {
    background: var(--panel-2);
    border: 1px solid var(--border);
    border-radius: 6px;
    color: var(--text);
    padding: 0.55rem 0.7rem;
    font-size: 0.95rem;
    font-family: var(--font);
}
.login-card input:focus { outline: 1px solid var(--accent); }

.login-card .btn-primary {
    background: var(--accent);
    border: none;
    border-radius: 6px;
    color: #eef4f9;
    padding: 0.6rem 0.9rem;
    font-size: 0.95rem;
    cursor: pointer;
    margin-top: 0.3rem;
}
.login-card .btn-primary:hover { background: var(--accent-bright); }

.login-error {
    background: rgba(208, 96, 96, 0.12);
    border: 1px solid var(--fail);
    border-radius: 6px;
    color: var(--fail);
    padding: 0.5rem 0.8rem;
    font-size: 0.84rem;
}

.side-user {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 0.5rem;
    margin-top: 0.6rem;
    padding-top: 0.6rem;
    border-top: 1px solid var(--border-soft);
}
.side-user .user-email {
    color: var(--muted);
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}
.logout-btn {
    background: none;
    border: 1px solid var(--border);
    border-radius: 5px;
    color: var(--muted);
    font-size: 0.72rem;
    padding: 0.2rem 0.55rem;
    cursor: pointer;
    white-space: nowrap;
}
.logout-btn:hover { color: var(--text); border-color: var(--accent); }

/* ---- credits ---- */

.side-balance {
    display: block;
    color: var(--accent-bright);
    font-size: 0.82rem;
    margin-bottom: 0.45rem;
    text-decoration: none;
}
.side-balance:hover { text-decoration: underline; }

.banner {
    border-radius: 6px;
    padding: 0.55rem 0.9rem;
    margin: 0.6rem 0 1rem;
    font-size: 0.88rem;
    border: 1px solid var(--border);
}
.banner.ok { border-color: var(--ok); color: var(--ok); background: rgba(76, 175, 125, 0.08); }
.banner.warn { border-color: var(--warn); color: var(--warn); background: rgba(208, 144, 74, 0.08); }

.balance-hero {
    display: flex;
    align-items: baseline;
    gap: 0.5rem;
    background: var(--panel);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 1rem 1.4rem;
    margin: 0.8rem 0 1.4rem;
}
.balance-hero .amount { font-size: 2rem; font-weight: 600; color: var(--accent-bright); }
.balance-hero .unit { color: var(--muted); }
.balance-hero .costs { margin-left: auto; color: var(--muted); font-size: 0.8rem; }

.packs { display: flex; gap: 1rem; flex-wrap: wrap; margin: 0.8rem 0 0.6rem; }
.pack {
    background: var(--panel);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 1rem 1.2rem;
    min-width: 150px;
    display: flex;
    flex-direction: column;
    gap: 0.25rem;
}
.pack-name { font-weight: 600; }
.pack-credits { color: var(--accent-bright); font-size: 0.95rem; }
.pack-price { color: var(--muted); font-size: 0.85rem; margin-bottom: 0.5rem; }

.credit-table { border-collapse: collapse; margin: 0.6rem 0 1.4rem; width: 100%; max-width: 860px; }
.credit-table th, .credit-table td {
    border-bottom: 1px solid var(--border-soft);
    text-align: left;
    padding: 0.35rem 0.9rem 0.35rem 0;
    font-size: 0.86rem;
}
.credit-table th { color: var(--muted); font-weight: 500; font-size: 0.78rem; }
.credit-table .mono { font-family: var(--mono); font-size: 0.78rem; }
.delta-pos { color: var(--ok); }
.delta-neg { color: var(--fail); }
.gen-status.done { color: var(--ok); }
.gen-status.failed { color: var(--fail); }
.gen-status.running { color: var(--warn); }

.auth-alt { display: block; margin-top: 0.4rem; font-size: 0.84rem; text-align: center; }

/* ---- preset tools ---- */

.presets-select { min-width: 110px; }

.preset-vary {
    margin-top: 0.7rem;
    border-top: 1px solid var(--border-soft);
    padding-top: 0.5rem;
}
.preset-vary > summary {
    cursor: pointer;
    font-size: 0.84rem;
    color: var(--muted);
    user-select: none;
}
.preset-vary > summary:hover { color: var(--accent-bright); }
.preset-vary[open] > summary { margin-bottom: 0.5rem; color: var(--accent-bright); }

/* ---- admin ---- */

.side-admin { display: block; color: var(--muted); font-size: 0.82rem; margin-bottom: 0.45rem; text-decoration: none; }
.side-admin:hover { color: var(--accent-bright); text-decoration: none; }

.status-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(240px, 1fr));
    gap: 0.9rem;
    margin: 1rem 0 1.6rem;
}
.status-card {
    background: var(--panel);
    border: 1px solid var(--border-soft);
    border-radius: var(--radius);
    padding: 0.8rem 1rem;
}
.status-label { font-size: 0.72rem; text-transform: uppercase; letter-spacing: 0.08em; color: var(--muted); }
.status-value { font-size: 0.98rem; margin: 0.25rem 0 0.15rem; }
.status-value.ok { color: var(--ok); }
.status-value.warn { color: var(--warn); }
.status-sub { color: var(--muted); font-size: 0.78rem; }

.detail-cell { max-width: 320px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.gen-status.cancelled { color: var(--muted); }

/* ---- run a job by hand ---- */

.job-manual {
    background: none;
    border: none;
    color: var(--muted);
    cursor: pointer;
    font-size: 0.78rem;
    padding: 0 0.2rem;
    flex-shrink: 0;
    white-space: nowrap;
}
.job-manual:hover { color: var(--accent-bright); text-decoration: none; }
.job-actions { margin-left: auto; display: inline-flex; gap: 0.4rem; align-items: center; }

.chip.job-status { text-transform: capitalize; gap: 0.4rem; display: inline-flex; align-items: center; }

/* ---- jobs management page ---- */

.jobs-toolbar { display: flex; align-items: center; justify-content: space-between; gap: 1rem; margin: 0.4rem 0 0.9rem; flex-wrap: wrap; }
.chip.filter { cursor: pointer; text-transform: capitalize; }
.chip.filter:hover { border-color: var(--accent); }
.chip.filter.active { color: var(--accent-bright); border-color: var(--accent); background: var(--accent-dim); }

.jobs-table .job-cell { display: flex; align-items: center; gap: 0.5rem; max-width: 420px; }
.jobs-table .job-cell a { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.jobs-table .result-link { flex-shrink: 0; font-size: 0.8rem; }
.jobs-table .actions-cell { white-space: nowrap; }
.jobs-table .actions-cell .btn, .jobs-table .actions-cell .job-manual { margin-right: 0.35rem; }
.jobs-table .mono { font-family: var(--mono); font-size: 0.78rem; color: var(--muted); }
.btn.danger:hover:not(:disabled) { border-color: var(--fail); color: var(--fail); }

/* The pasted request is long: cap it so the response box stays on screen. */
.manual-request { max-height: 15rem; overflow-y: auto; }
.manual-response { font-family: var(--mono); font-size: 0.78rem; width: 100%; margin-top: 0.3rem; }

/* ---- by-hand dialog ---- */

.modal-backdrop {
    position: fixed;
    inset: 0;
    background: rgba(5, 8, 11, 0.72);
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 1.5rem;
    z-index: 50;
}

.modal {
    background: var(--panel);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    box-shadow: 0 18px 50px rgba(0, 0, 0, 0.55);
    width: min(760px, 100%);
    /* The request and the paste box both scroll INSIDE the dialog, so the
       footer's Apply button is never pushed off screen. */
    max-height: min(88vh, 900px);
    display: flex;
    flex-direction: column;
}

.modal-head {
    display: flex;
    align-items: flex-start;
    justify-content: space-between;
    gap: 1rem;
    padding: 1rem 1.2rem 0.6rem;
    border-bottom: 1px solid var(--border-soft);
}
.modal-head .hint { margin: 0.2rem 0 0; }

.modal-close {
    background: none;
    border: none;
    color: var(--muted);
    font-size: 1.4rem;
    line-height: 1;
    cursor: pointer;
    padding: 0 0.2rem;
}
.modal-close:hover { color: var(--text); }

.modal-body { padding: 0.4rem 1.2rem 1rem; overflow-y: auto; }

.modal-foot {
    display: flex;
    justify-content: flex-end;
    gap: 0.6rem;
    padding: 0.8rem 1.2rem;
    border-top: 1px solid var(--border-soft);
}

/* Inside the dialog the request gets less room than on the job page. */
.manual-modal .manual-request { max-height: 11rem; }

/* ---- workbench takes ---- */

.take { border-left: 3px solid var(--border); }
.take.active-take { border-left-color: var(--accent); }
.take-head { display: flex; justify-content: space-between; align-items: flex-start; gap: 1rem; flex-wrap: wrap; }
.take-head h3 { margin-bottom: 0.15rem; }
.take-label { margin: 0 0 0.4rem; }
.take-actions { display: flex; gap: 0.5rem; align-items: center; flex-wrap: wrap; }

/* ---- harmony review ---- */

.review {
    border-left: 3px solid var(--warn);
    background: var(--panel-2);
    border-radius: 6px;
    padding: 0.2rem 0.9rem 0.7rem;
    margin-top: 0.8rem;
}
.review .block-head { margin-top: 0.7rem; }
.review-verdict { margin: 0.2rem 0 0.7rem; font-size: 0.9rem; }

.finding { border-top: 1px solid var(--border-soft); padding: 0.55rem 0 0.2rem; }
.finding-head { display: flex; align-items: center; gap: 0.5rem; margin-bottom: 0.25rem; }
.finding-where { font-size: 0.82rem; color: var(--muted); }
.finding-problem { margin: 0 0 0.25rem; font-size: 0.88rem; }
.finding-fix { margin: 0; font-size: 0.88rem; color: var(--accent-bright); }

.chip.sev-minor { color: var(--muted); }
.chip.sev-notable { color: var(--warn); border-color: var(--warn); }
.chip.sev-serious { color: var(--fail); border-color: var(--fail); }

/* ---- generated ticks ---- */

.gen-check { margin-right: 0; }
.chip.gen-none { color: var(--muted); }
.chip.gen-partial { color: var(--accent-bright); border-color: var(--accent); }
.chip.gen-complete { color: var(--ok); border-color: var(--ok); }

/* ---- ai routing switch ---- */

.route-switch { display: flex; align-items: center; gap: 0.35rem; margin-top: 0.6rem; flex-wrap: wrap; }
.route-switch .chip.filter { text-transform: none; }
.route-switch .chip.filter:disabled { opacity: 0.45; cursor: not-allowed; }
.status-sub.note { margin-top: 0.5rem; color: var(--accent-bright); }

/* ---- admin usage bars (P5.2) ---- */
/* Styled div widths, deliberately: no chart library for four tables. */

.credit-table .bar-col { width: 180px; white-space: nowrap; }
.bar {
    display: inline-block;
    height: 0.55rem;
    min-width: 1px;
    background: var(--accent);
    border-radius: 2px;
    vertical-align: middle;
}
.bar-label { color: var(--muted); font-size: 0.72rem; margin-left: 0.4rem; }
.row-quiet td { color: var(--muted); }
.gen-status.grant { color: var(--ok); }
.gen-status.sweep { color: var(--warn); }
.gen-status.route { color: var(--accent-bright); }
