/* TTD Brain - minimal, high-contrast operations console (Swiss / minimalism, blue + amber). */
:root {
    --primary: #1e40af;
    --on-primary: #ffffff;
    --accent: #b45309;
    --bg: #f8fafc;
    --card: #ffffff;
    --fg: #0f172a;
    --muted: #e9eef6;
    --muted-fg: #475569;
    --border: #cbd5e1;
    --ring: #1e40af;
    --ok: #166534;
    --ok-bg: #dcfce7;
    --warn: #92400e;
    --warn-bg: #fef3c7;
    --bad: #b91c1c;
    --bad-bg: #fee2e2;
    --info-bg: #dbeafe;
    --radius: 10px;
    --font: 'Fira Sans', system-ui, -apple-system, 'Segoe UI', Roboto, sans-serif;
    --mono: 'Fira Code', ui-monospace, SFMono-Regular, Menlo, monospace;
}
@media (prefers-color-scheme: dark) {
    :root:not([data-theme='light']) {
        --primary: #60a5fa;
        --on-primary: #0b1220;
        --accent: #fbbf24;
        --bg: #0b1220;
        --card: #111a2e;
        --fg: #e5edf9;
        --muted: #1a2540;
        --muted-fg: #a5b4cc;
        --border: #2b3a5c;
        --ring: #93c5fd;
        --ok: #86efac;
        --ok-bg: #12301f;
        --warn: #fcd34d;
        --warn-bg: #3a2c0b;
        --bad: #fca5a5;
        --bad-bg: #3b1515;
        --info-bg: #14264d;
    }
}
:root[data-theme='dark'] {
    --primary: #60a5fa;
    --on-primary: #0b1220;
    --accent: #fbbf24;
    --bg: #0b1220;
    --card: #111a2e;
    --fg: #e5edf9;
    --muted: #1a2540;
    --muted-fg: #a5b4cc;
    --border: #2b3a5c;
    --ring: #93c5fd;
    --ok: #86efac;
    --ok-bg: #12301f;
    --warn: #fcd34d;
    --warn-bg: #3a2c0b;
    --bad: #fca5a5;
    --bad-bg: #3b1515;
    --info-bg: #14264d;
}

* { box-sizing: border-box; }
html { font-size: 16px; }
body {
    margin: 0;
    background: var(--bg);
    color: var(--fg);
    font-family: var(--font);
    line-height: 1.5;
    -webkit-font-smoothing: antialiased;
}
h1, h2, h3 { margin: 0; line-height: 1.25; }
h1 { font-size: 1.5rem; font-weight: 600; }
h2 { font-size: 1.125rem; font-weight: 600; }
h3 { font-size: 1rem; font-weight: 600; }
p { margin: 0; }
a { color: var(--primary); }
code, .mono { font-family: var(--mono); font-size: 0.85em; }
:focus-visible { outline: 3px solid var(--ring); outline-offset: 2px; border-radius: 6px; }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
.muted { color: var(--muted-fg); }
.small { font-size: 0.875rem; }

/* Layout */
.shell { display: grid; grid-template-columns: 232px 1fr; min-height: 100vh; }
.side {
    background: var(--card);
    border-right: 1px solid var(--border);
    padding: 20px 12px;
    position: sticky;
    top: 0;
    height: 100vh;
    display: flex;
    flex-direction: column;
    gap: 4px;
}
.brand { display: flex; align-items: center; gap: 10px; padding: 4px 10px 16px; font-weight: 700; font-size: 1.125rem; }
.brand svg { color: var(--primary); }
.nav a {
    display: flex;
    align-items: center;
    gap: 10px;
    min-height: 44px;
    padding: 0 12px;
    border-radius: 8px;
    color: var(--fg);
    text-decoration: none;
    font-weight: 500;
    cursor: pointer;
    transition: background 0.15s;
}
.nav a:hover { background: var(--muted); }
.nav a[aria-current='page'] { background: var(--primary); color: var(--on-primary); }
.nav .badge { margin-left: auto; }
.side-foot { margin-top: auto; padding: 8px 10px; }
.main { padding: 28px 32px 64px; max-width: 1180px; width: 100%; min-width: 0; }
.filters { display: grid; grid-template-columns: repeat(auto-fit, minmax(170px, 1fr)); gap: 14px; align-items: end; }
.filters .wide { grid-column: span 2; }
@media (max-width: 520px) { .filters .wide { grid-column: auto; } }
.page-head { display: flex; align-items: flex-start; justify-content: space-between; gap: 16px; flex-wrap: wrap; margin-bottom: 20px; }
.page-head p { margin-top: 4px; }
.stack { display: grid; gap: 16px; }
.grid { display: grid; gap: 16px; }
.grid.cols-4 { grid-template-columns: repeat(4, minmax(0, 1fr)); }
.grid.cols-3 { grid-template-columns: repeat(3, minmax(0, 1fr)); }
.grid.cols-2 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.row { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
.row.between { justify-content: space-between; }

/* Cards */
.card { background: var(--card); border: 1px solid var(--border); border-radius: var(--radius); padding: 18px; }
.card h2 + .muted { margin-top: 2px; }
.card-head { display: flex; justify-content: space-between; align-items: flex-start; gap: 12px; margin-bottom: 14px; }
.stat .num { font-size: 2rem; font-weight: 600; line-height: 1.1; font-variant-numeric: tabular-nums; }
.stat .label { color: var(--muted-fg); font-size: 0.875rem; margin-bottom: 6px; }
.stat .hint { color: var(--muted-fg); font-size: 0.8125rem; margin-top: 4px; }

/* Status banner */
.banner { display: flex; gap: 14px; align-items: center; padding: 16px 18px; border-radius: var(--radius); border: 1px solid var(--border); }
.banner.ok { background: var(--ok-bg); color: var(--ok); border-color: transparent; }
.banner.warn { background: var(--warn-bg); color: var(--warn); border-color: transparent; }
.banner.bad { background: var(--bad-bg); color: var(--bad); border-color: transparent; }
.banner .t { font-weight: 600; font-size: 1.0625rem; }
.banner svg { flex: none; }

/* Buttons & form */
button, .btn {
    font: inherit;
    min-height: 44px;
    padding: 0 16px;
    border-radius: 8px;
    border: 1px solid var(--border);
    background: var(--card);
    color: var(--fg);
    cursor: pointer;
    font-weight: 500;
    display: inline-flex;
    align-items: center;
    gap: 8px;
    transition: background 0.15s, border-color 0.15s;
    text-decoration: none;
}
button:hover:not(:disabled), .btn:hover { background: var(--muted); }
button:disabled { opacity: 0.5; cursor: not-allowed; }
button.primary { background: var(--primary); color: var(--on-primary); border-color: var(--primary); }
button.primary:hover:not(:disabled) { filter: brightness(1.08); background: var(--primary); }
button.danger { color: var(--bad); border-color: var(--bad); }
button.small { min-height: 36px; padding: 0 12px; font-size: 0.875rem; }
button.ghost { border-color: transparent; background: transparent; }
button.icon { padding: 0; width: 44px; justify-content: center; }
input[type='text'], input[type='number'], input[type='search'], input[type='email'], select, textarea {
    font: inherit;
    min-height: 44px;
    padding: 0 12px;
    border-radius: 8px;
    border: 1px solid var(--border);
    background: var(--card);
    color: var(--fg);
    width: 100%;
}
textarea { padding: 10px 12px; min-height: 84px; }
label { font-weight: 500; display: block; margin-bottom: 6px; }
.field { display: grid; gap: 4px; }
.field .help { color: var(--muted-fg); font-size: 0.8125rem; }

/* Toggle */
.switch { display: flex; align-items: center; gap: 12px; cursor: pointer; }
.switch input { position: absolute; opacity: 0; width: 1px; height: 1px; }
.switch .track { width: 48px; height: 28px; border-radius: 999px; background: var(--border); position: relative; transition: background 0.15s; flex: none; }
.switch .track::after { content: ''; position: absolute; top: 3px; left: 3px; width: 22px; height: 22px; border-radius: 50%; background: #fff; transition: transform 0.15s; }
.switch input:checked + .track { background: var(--primary); }
.switch input:checked + .track::after { transform: translateX(20px); }
.switch input:focus-visible + .track { outline: 3px solid var(--ring); outline-offset: 2px; }

/* Chips & badges */
.badge { display: inline-flex; align-items: center; gap: 6px; padding: 2px 10px; border-radius: 999px; font-size: 0.8125rem; font-weight: 600; white-space: nowrap; background: var(--muted); color: var(--fg); }
.badge.ok { background: var(--ok-bg); color: var(--ok); }
.badge.warn { background: var(--warn-bg); color: var(--warn); }
.badge.bad { background: var(--bad-bg); color: var(--bad); }
.badge.info { background: var(--info-bg); color: var(--primary); }
.dot { width: 8px; height: 8px; border-radius: 50%; background: currentColor; display: inline-block; }

/* Table */
.table-wrap { overflow-x: auto; border: 1px solid var(--border); border-radius: var(--radius); background: var(--card); }
table { width: 100%; border-collapse: collapse; }
th, td { text-align: left; padding: 10px 14px; border-bottom: 1px solid var(--border); vertical-align: middle; }
th { font-size: 0.8125rem; text-transform: uppercase; letter-spacing: 0.04em; color: var(--muted-fg); font-weight: 600; background: var(--muted); }
tr:last-child td { border-bottom: 0; }
tbody tr.click { cursor: pointer; }
tbody tr.click:hover { background: var(--muted); }
td .title { max-width: 360px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.pager { display: flex; gap: 10px; align-items: center; justify-content: flex-end; margin-top: 12px; }

/* Bars */
.bars { display: grid; gap: 10px; }
.bar-row { display: grid; grid-template-columns: 150px 1fr 70px; gap: 12px; align-items: center; font-size: 0.9375rem; }
.bar { height: 10px; border-radius: 999px; background: var(--muted); overflow: hidden; }
.bar > i { display: block; height: 100%; background: var(--primary); border-radius: 999px; }
.bar.warn > i { background: var(--accent); }

/* Steps / runs */
details.run { border-top: 1px solid var(--border); padding: 12px 0; }
details.run:first-of-type { border-top: 0; }
details.run summary { cursor: pointer; display: flex; gap: 12px; align-items: center; flex-wrap: wrap; min-height: 32px; }
.steps { margin: 10px 0 0 4px; display: grid; gap: 6px; font-size: 0.9rem; }
.steps .step { display: grid; grid-template-columns: 20px 150px 1fr; gap: 8px; align-items: start; }
.steps pre { margin: 0; white-space: pre-wrap; font-family: var(--mono); font-size: 0.78rem; color: var(--muted-fg); }
.problems { margin: 8px 0 0; padding-left: 18px; color: var(--bad); }

/* Drawer */
dialog { border: 0; padding: 0; background: transparent; color: var(--fg); max-width: none; max-height: none; }
dialog::backdrop { background: rgba(15, 23, 42, 0.5); }
dialog.drawer { margin: 0 0 0 auto; height: 100vh; width: min(560px, 100vw); }
dialog.modal { margin: auto; width: min(480px, calc(100vw - 32px)); }
.sheet { background: var(--card); height: 100%; overflow-y: auto; padding: 22px; display: grid; gap: 16px; align-content: start; }
dialog.modal .sheet { border-radius: var(--radius); height: auto; border: 1px solid var(--border); }
.kv { display: grid; grid-template-columns: 150px 1fr; gap: 6px 12px; font-size: 0.9375rem; }
.kv dt { color: var(--muted-fg); }
.kv dd { margin: 0; word-break: break-word; }
.seg { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
.seg button { justify-content: center; flex-direction: column; min-height: 64px; text-align: center; gap: 2px; padding: 8px; }
.seg button small { color: var(--muted-fg); font-weight: 400; }
.seg button[aria-pressed='true'] { border: 2px solid var(--primary); background: var(--info-bg); }
pre.json { background: var(--muted); padding: 12px; border-radius: 8px; overflow: auto; max-height: 320px; font-family: var(--mono); font-size: 0.78rem; }

/* Toast */
#toasts { position: fixed; right: 16px; bottom: 16px; display: grid; gap: 8px; z-index: 50; }
.toast { background: var(--fg); color: var(--bg); padding: 12px 16px; border-radius: 8px; max-width: 360px; box-shadow: 0 8px 24px rgba(0, 0, 0, 0.25); }
.toast.bad { background: var(--bad); color: #fff; }

.empty { padding: 28px; text-align: center; color: var(--muted-fg); }
.spinner { width: 16px; height: 16px; border: 2px solid currentColor; border-right-color: transparent; border-radius: 50%; display: inline-block; animation: spin 0.8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
@media (prefers-reduced-motion: reduce) {
    * { transition: none !important; animation-duration: 2s !important; }
}

/* Mobile: sidebar becomes a top bar */
.topbar { display: none; }
@media (max-width: 960px) {
    .grid.cols-4 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
    .grid.cols-3 { grid-template-columns: 1fr; }
}
@media (max-width: 760px) {
    .shell { grid-template-columns: 1fr; }
    .side { position: fixed; inset: 0 auto 0 0; width: 260px; z-index: 40; transform: translateX(-100%); transition: transform 0.2s; }
    .side.open { transform: none; box-shadow: 0 0 0 100vmax rgba(15, 23, 42, 0.45); }
    .topbar { display: flex; align-items: center; gap: 10px; padding: 8px 12px; background: var(--card); border-bottom: 1px solid var(--border); position: sticky; top: 0; z-index: 30; }
    .main { padding: 18px 16px 56px; }
    .grid.cols-2, .grid.cols-4 { grid-template-columns: 1fr; }
    .bar-row { grid-template-columns: 110px 1fr 56px; }
    .kv { grid-template-columns: 1fr; }
    .steps .step { grid-template-columns: 20px 1fr; }
    .steps .step pre { grid-column: 2; }
}

.head-row { display: flex; gap: 12px; justify-content: space-between; align-items: flex-start; }

.logrow { display: grid; grid-template-columns: 170px 170px 1fr; gap: 12px; padding: 10px 16px; border-bottom: 1px solid var(--border); align-items: start; }
.logrow:last-child { border-bottom: 0; }
.logrow details { margin-top: 4px; }
@media (max-width: 760px) { .logrow { grid-template-columns: 1fr; gap: 4px; } }
