:root {
    --bg: #f4f6fb;
    --card: #fff;
    --ink: #172036;
    --muted: #667085;
    --line: #e4e8f0;
    --navy: #0f1b3d;
    --blue: #2f5bea;
    --blue-soft: #e8eeff;
    --green: #12805c;
    --green-soft: #e3f6ee;
    --amber: #a35b00;
    --amber-soft: #fff3df;
    --red: #c4321c;
    --red-soft: #fde8e4;
    --grey-soft: #eef0f4;
    --radius: 10px;
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
    font: 14px/1.5 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
    color: var(--ink);
    background: var(--bg);
}
a { color: var(--blue); text-decoration: none; }
a:hover { text-decoration: underline; }
h1 { font-size: 22px; margin: 0 0 6px; }
h2 { font-size: 15px; margin: 0 0 12px; }
h3 { font-size: 14px; margin: 0 0 8px; }
p { margin: 0 0 12px; }
code, pre { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 12px; }
pre { background: #0f172a; color: #e2e8f0; padding: 12px; border-radius: 8px; overflow: auto; max-height: 420px; white-space: pre-wrap; word-break: break-word; }
.muted { color: var(--muted); }
.small { font-size: 12px; }
.nowrap { white-space: nowrap; }
.right { text-align: right; }
.mt { margin-top: 16px; }

/* Layout */
.sidebar {
    position: fixed; inset: 0 auto 0 0; width: 220px;
    background: var(--navy); color: #cfd6ea;
    display: flex; flex-direction: column; padding: 18px 12px;
}
.brand { display: flex; gap: 10px; align-items: center; color: #fff; padding: 4px 8px 18px; text-decoration: none !important; }
.brand strong { display: block; font-size: 15px; }
.brand small { display: block; color: #93a0c3; font-size: 11px; }
.brand.dark { color: var(--ink); padding: 0 0 18px; }
.brand.dark small { color: var(--muted); }
.logo { width: 34px; height: 34px; border-radius: 9px; background: var(--blue); color: #fff; display: grid; place-items: center; font-weight: 700; font-size: 13px; }
.sidebar nav { display: flex; flex-direction: column; gap: 2px; }
.sidebar nav a { color: #cfd6ea; padding: 8px 10px; border-radius: 7px; text-decoration: none; }
.sidebar nav a:hover { background: rgba(255,255,255,.06); color: #fff; }
.sidebar nav a.active { background: rgba(255,255,255,.12); color: #fff; font-weight: 600; }
.sidebar .logout { margin-top: auto; padding: 0 8px; }
.sidebar .btn-ghost { color: #cfd6ea; border-color: rgba(255,255,255,.2); }
main { margin-left: 220px; padding: 0 32px 48px; max-width: 1400px; }

.mode { margin: 0 -32px 20px; padding: 9px 32px; font-size: 13px; border-bottom: 1px solid var(--line); }
.mode strong { text-transform: uppercase; letter-spacing: .04em; margin-right: 6px; }
.mode-dry_run { background: var(--amber-soft); color: var(--amber); }
.mode-shadow { background: var(--blue-soft); color: var(--blue); }
.mode-live { background: var(--green-soft); color: var(--green); }

.page-head { display: flex; justify-content: space-between; align-items: flex-start; gap: 16px; margin-bottom: 16px; }
.page-head p { margin: 0; }
.page-head .btn { white-space: nowrap; }
.card { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 18px 20px; margin-bottom: 18px; }
.card > h2:first-child { margin-top: 0; }
.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 18px; }
.grid-2.wide-left { grid-template-columns: 1.7fr 1fr; }
.grid-2 > .card, .grid-2 > div > .card:last-child { margin-bottom: 0; }
ol.steps { margin: 0; padding-left: 20px; }
ol.steps li { margin: 6px 0; }
.grid-2 + .card, .grid-2 + .grid-2 { margin-top: 18px; }
.stats { display: grid; grid-template-columns: repeat(4, 1fr); gap: 14px; margin-bottom: 18px; }
.stats.five { grid-template-columns: repeat(5, 1fr); }
.stat { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 14px 16px; display: block; color: inherit; text-decoration: none !important; }
.stat span { color: var(--muted); font-size: 12px; }
.stat strong { display: block; font-size: 26px; line-height: 1.2; }
.stat.alert strong { color: var(--red); }

/* Flash */
.flash { padding: 10px 14px; border-radius: 8px; margin-bottom: 14px; border: 1px solid transparent; }
.flash-success { background: var(--green-soft); color: var(--green); border-color: #bfe8d6; }
.flash-error { background: var(--red-soft); color: var(--red); border-color: #f6c4ba; }
.flash-info { background: var(--blue-soft); color: var(--blue); }

/* Tables */
.table-wrap { overflow-x: auto; }
table.table { width: 100%; border-collapse: collapse; }
.table th, .table td { padding: 8px 10px; border-bottom: 1px solid var(--line); text-align: left; vertical-align: middle; }
.table th { font-size: 12px; color: var(--muted); font-weight: 600; background: #fafbfd; white-space: nowrap; }
.table tbody tr:hover { background: #fafbff; }
.table tr.off td { color: var(--muted); }
.table td.actions { white-space: nowrap; text-align: right; }
.table a.rowlink { color: inherit; font-weight: 600; }

/* Badges */
.badge { display: inline-block; padding: 2px 8px; border-radius: 999px; font-size: 11.5px; font-weight: 600; white-space: nowrap; }
.badge-green { background: var(--green-soft); color: var(--green); }
.badge-blue { background: var(--blue-soft); color: var(--blue); }
.badge-amber { background: var(--amber-soft); color: var(--amber); }
.badge-red { background: var(--red-soft); color: var(--red); }
.badge-grey { background: var(--grey-soft); color: #4b5563; }
.chip { display: inline-flex; align-items: center; gap: 4px; padding: 3px 4px 3px 10px; border-radius: 999px; background: var(--grey-soft); margin: 0 6px 6px 0; font-size: 12.5px; }
.chip form { display: inline; }
.chip button { border: 0; background: none; color: var(--muted); cursor: pointer; padding: 0 6px; font-size: 14px; line-height: 1; }
.chip button:hover { color: var(--red); }
.table td.sells { min-width: 170px; line-height: 1.9; }
.sells .chip { padding: 1px 8px; margin: 0 4px 0 0; font-size: 11.5px; white-space: nowrap; }

/* Buttons & forms */
.btn { display: inline-flex; align-items: center; justify-content: center; gap: 6px; padding: 8px 14px; border-radius: 8px; border: 1px solid var(--line); background: #fff; color: var(--ink); font: inherit; font-weight: 600; cursor: pointer; text-decoration: none !important; }
.btn:hover { border-color: #c9d0de; }
.btn-primary { background: var(--blue); border-color: var(--blue); color: #fff; }
.btn-primary:hover { background: #2448c7; }
.btn-danger { color: var(--red); }
.btn-danger:hover { background: var(--red-soft); border-color: #f6c4ba; }
.btn-ghost { background: transparent; }
.btn-small { padding: 4px 10px; font-size: 12.5px; }
.btn:disabled, .btn:disabled:hover { opacity: .45; cursor: not-allowed; }
.btn-block { width: 100%; }
.btn-row { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }
.inline { display: inline; }
.field { display: block; margin-bottom: 12px; }
.field > span { display: block; font-size: 12px; font-weight: 600; color: var(--muted); margin-bottom: 4px; }
.field small { display: block; color: var(--muted); font-size: 12px; margin-top: 3px; }
input[type=text], input[type=email], input[type=password], input[type=number], input[type=date], input[type=search], select, textarea {
    width: 100%; padding: 8px 10px; border: 1px solid #d3d9e5; border-radius: 7px; font: inherit; background: #fff; color: var(--ink);
}
input:focus, select:focus, textarea:focus { outline: 2px solid var(--blue-soft); border-color: var(--blue); }
textarea { min-height: 120px; font-family: ui-monospace, Menlo, monospace; font-size: 12px; }
.check { display: flex; align-items: center; gap: 8px; margin-bottom: 10px; }
.check input { width: 16px; height: 16px; }
.form-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 0 14px; }
.form-inline { display: flex; flex-wrap: wrap; gap: 10px; align-items: flex-end; }
.form-inline .field { margin-bottom: 0; }
.form-inline input, .form-inline select { width: auto; min-width: 150px; }

/* On/off switch */
.switch { width: 40px; height: 22px; border-radius: 999px; border: 0; background: #cdd3df; position: relative; cursor: pointer; padding: 0; vertical-align: middle; }
.switch::after { content: ""; position: absolute; top: 3px; left: 3px; width: 16px; height: 16px; border-radius: 50%; background: #fff; transition: left .15s; box-shadow: 0 1px 2px rgba(0,0,0,.2); }
.switch.on { background: var(--green); }
.switch.on::after { left: 21px; }

/* Capacity bar */
.cap { display: flex; align-items: center; gap: 8px; min-width: 120px; }
.cap .bar { flex: 1; height: 6px; background: var(--grey-soft); border-radius: 999px; overflow: hidden; }
.cap .bar i { display: block; height: 100%; background: var(--blue); }
.cap.full .bar i { background: var(--red); }
.cap span { font-size: 12px; color: var(--muted); white-space: nowrap; }

/* Simple horizontal bars */
.bars { list-style: none; margin: 0 0 14px; padding: 0; }
.bars li { display: grid; grid-template-columns: 140px 1fr 36px; gap: 10px; align-items: center; margin-bottom: 6px; font-size: 13px; }
.bars .bar { height: 8px; background: var(--grey-soft); border-radius: 999px; overflow: hidden; }
.bars .bar i { display: block; height: 100%; background: var(--blue); }

/* Tabs / filter chips */
.tabs { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 14px; }
.tabs a { padding: 6px 12px; border-radius: 999px; background: #fff; border: 1px solid var(--line); color: var(--ink); font-size: 13px; text-decoration: none; }
.tabs a.active { background: var(--navy); border-color: var(--navy); color: #fff; }
.tabs a .n { color: var(--muted); margin-left: 4px; }
.tabs a.active .n { color: #b8c2de; }

/* Weights matrix */
table.weights th.course { writing-mode: initial; font-size: 11.5px; white-space: normal; min-width: 72px; max-width: 96px; text-align: center; line-height: 1.25; }
table.weights td { text-align: center; padding: 5px 6px; }
table.weights td:first-child, table.weights th:first-child { text-align: left; white-space: nowrap; }
table.weights input { width: 52px; text-align: center; padding: 5px 4px; }
table.weights input.nz { background: var(--blue-soft); border-color: #b9c8fb; font-weight: 700; }
table.weights tfoot td { font-weight: 700; color: var(--muted); font-size: 12px; background: #fafbfd; }
.sticky-actions { position: sticky; bottom: 0; background: linear-gradient(transparent, var(--card) 30%); padding-top: 14px; margin-top: 6px; }

/* Trace */
ol.trace { list-style: none; margin: 0; padding: 0; }
ol.trace li { position: relative; padding: 6px 0 6px 22px; border-left: 2px solid var(--line); margin-left: 6px; }
ol.trace li::before { content: ""; position: absolute; left: -7px; top: 11px; width: 12px; height: 12px; border-radius: 50%; background: #cbd2e0; border: 2px solid #fff; }
ol.trace li.t-ok::before { background: var(--green); }
ol.trace li.t-warn::before { background: #e59a1a; }
ol.trace li.t-error::before { background: var(--red); }
ol.trace li.t-info::before { background: var(--blue); }
ol.trace .t-step { font-weight: 600; margin-right: 6px; }
ol.trace .t-detail { color: #39425a; word-break: break-word; }

.result-head { display: flex; flex-wrap: wrap; gap: 18px; align-items: center; margin-bottom: 14px; }
.result-head .big { font-size: 18px; font-weight: 700; }
dl.kv { display: grid; grid-template-columns: 150px 1fr; gap: 6px 14px; margin: 0; }
dl.kv dt { color: var(--muted); font-size: 12.5px; }
dl.kv dd { margin: 0; word-break: break-word; }
details summary { cursor: pointer; font-weight: 600; }

.pager { display: flex; gap: 6px; justify-content: flex-end; margin-top: 12px; }
.empty { padding: 24px; text-align: center; color: var(--muted); }
.course-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(280px, 1fr)); gap: 14px; }
.course-grid .card { margin: 0; }

/* Login */
.login-page { display: grid; place-items: center; min-height: 100vh; }
.card.login { width: 360px; padding: 26px; }

@media (max-width: 980px) {
    .sidebar { position: static; width: auto; flex-direction: row; flex-wrap: wrap; align-items: center; }
    .sidebar nav { flex-direction: row; flex-wrap: wrap; }
    .sidebar .logout { margin: 0 0 0 auto; }
    main { margin-left: 0; padding: 0 16px 32px; }
    .mode { margin: 0 -16px 16px; padding: 9px 16px; }
    .grid-2, .grid-2.wide-left, .form-grid { grid-template-columns: 1fr; }
    .stats { grid-template-columns: 1fr 1fr; }
}
