:root {
  --bg: #f5f6f8;
  --surface: #ffffff;
  --surface-2: #eef0f3;
  --border: #d9dde3;
  --text: #1c2129;
  --muted: #667085;
  --accent: #2b6cb0;
  --accent-text: #ffffff;
  --ok: #2f855a;
  --low: #c53030;
  --warn: #b7791f;
  --busy: #2b6cb0;
  --item: #718096;
  --fluid: #3182ce;
  --essentia: #805ad5;
  --radius: 8px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #12151a;
    --surface: #1b1f26;
    --surface-2: #232832;
    --border: #2f3541;
    --text: #e6e8eb;
    --muted: #98a2b3;
    --accent: #4c8fd6;
    --ok: #48bb78;
    --low: #f56565;
    --warn: #ecc94b;
    --busy: #63b3ed;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: 14px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}
h1, h2, h3 { margin: 0 0 0.6rem; font-weight: 600; }
h1 { font-size: 1.4rem; }
h2 { font-size: 1.1rem; margin-top: 0.4rem; }
h3 { font-size: 0.95rem; margin-top: 0.8rem; }
a { color: var(--accent); }
.muted { color: var(--muted); }
.small { font-size: 12px; }
.error { color: var(--low); }
.hidden { display: none !important; }
.spacer { flex: 1; }
.empty { text-align: center; padding: 2rem 0.5rem; color: var(--muted); }

/* Controls */
input, select, textarea, button, .button {
  font: inherit;
  color: inherit;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 0.4rem 0.6rem;
}
input[type="checkbox"] { padding: 0; width: 1rem; height: 1rem; accent-color: var(--accent); }
input[type="number"] { width: 8rem; }
input.narrow { width: 5rem; }
textarea { width: 100%; font-family: ui-monospace, Menlo, Consolas, monospace; font-size: 12px; }
button, .button { cursor: pointer; background: var(--surface-2); white-space: nowrap; }
button:hover:not(:disabled), .button:hover { border-color: var(--accent); }
button:disabled { opacity: 0.5; cursor: default; }
button.primary { background: var(--accent); color: var(--accent-text); border-color: var(--accent); }
button.danger { color: var(--low); }
button.small { padding: 0.15rem 0.45rem; font-size: 12px; }
button.link { background: none; border: none; color: var(--accent); padding: 0; }
button.icon { background: none; border: none; font-size: 1rem; }
label { display: flex; flex-direction: column; gap: 0.2rem; font-size: 13px; }
label.check { flex-direction: row; align-items: center; gap: 0.4rem; }
label.grow { flex: 1; }
fieldset { border: 1px solid var(--border); border-radius: var(--radius); margin: 0.8rem 0 0; }
.row { display: flex; gap: 0.75rem; align-items: flex-end; margin: 0.5rem 0; }
.row.wrap { flex-wrap: wrap; }

/* Layout */
header.top {
  display: flex;
  align-items: center;
  gap: 1.25rem;
  padding: 0 1rem;
  height: 52px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 5;
}
.brand { font-size: 1.05rem; }
nav { display: flex; gap: 0.25rem; flex: 1; overflow-x: auto; }
nav a { color: var(--muted); text-decoration: none; padding: 0.4rem 0.7rem; border-radius: 6px; }
nav a.active { color: var(--text); background: var(--surface-2); font-weight: 600; }
.link-badge { font-size: 12px; padding: 0.2rem 0.6rem; border-radius: 999px; border: 1px solid var(--border); }
.link-badge::before { content: "●"; margin-right: 0.35rem; }
.link-badge.on::before { color: var(--ok); }
.link-badge.off::before { color: var(--low); }
.link-badge.stale::before { color: var(--warn); }
.link-badge.outdated { border-color: var(--warn); color: var(--warn); }
main { padding: 1rem; max-width: 1400px; margin: 0 auto; }
.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 1rem; margin-bottom: 1rem; }
.cards { display: grid; grid-template-columns: repeat(auto-fit, minmax(300px, 1fr)); gap: 1rem; }
.login { max-width: 320px; margin: 15vh auto; display: flex; flex-direction: column; gap: 0.75rem; }
.settings { max-width: 640px; display: flex; flex-direction: column; gap: 0.8rem; }
.toolbar { display: flex; flex-wrap: wrap; gap: 0.5rem; align-items: center; margin-bottom: 0.75rem; }
.toolbar input[type="search"] { min-width: 240px; flex: 0 1 320px; }

/* Tables */
.table-wrap { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); overflow-x: auto; }
table { width: 100%; border-collapse: collapse; }
th, td { text-align: left; padding: 0.45rem 0.6rem; border-bottom: 1px solid var(--border); vertical-align: middle; }
th { font-size: 12px; color: var(--muted); font-weight: 600; background: var(--surface-2); position: sticky; top: 0; }
tbody tr:last-child td { border-bottom: none; }
td.num, th.num { text-align: right; font-variant-numeric: tabular-nums; }
td.check, th.check { width: 2rem; }
td.actions { white-space: nowrap; text-align: right; }
td.actions button { margin-left: 0.25rem; }
tr.status-disabled td { opacity: 0.55; }
.table-foot { display: flex; justify-content: space-between; align-items: center; padding: 0.5rem 0.2rem; }
.pager { display: flex; gap: 0.4rem; }
.bulk-bar { display: flex; gap: 0.5rem; align-items: center; padding: 0.5rem 0.75rem; margin-bottom: 0.5rem; background: var(--surface-2); border-radius: var(--radius); }
.stack-name { display: flex; align-items: center; gap: 0.4rem; font-weight: 500; }
.stock-col { min-width: 220px; }
.stock-cell .stock-text { font-variant-numeric: tabular-nums; }
.bar { position: relative; height: 6px; background: var(--surface-2); border-radius: 3px; overflow: hidden; margin-top: 4px; }
.bar.big { height: 12px; border-radius: 6px; }
.bar .fill { height: 100%; background: var(--ok); }
.bar .fill.low { background: var(--low); }
.bar .min-mark { position: absolute; top: 0; bottom: 0; width: 2px; background: var(--text); opacity: 0.45; }

/* Badges */
.badge { display: inline-block; font-size: 11px; font-weight: 600; padding: 0.1rem 0.5rem; border-radius: 999px; background: var(--surface-2); color: var(--muted); }
.badge.ok, .badge.job-done { background: color-mix(in srgb, var(--ok) 18%, transparent); color: var(--ok); }
.badge.low, .badge.job-failed { background: color-mix(in srgb, var(--low) 18%, transparent); color: var(--low); }
.badge.backoff, .badge.job-canceled { background: color-mix(in srgb, var(--warn) 20%, transparent); color: var(--warn); }
.badge.crafting, .badge.job-running, .badge.job-computing, .badge.job-requested { background: color-mix(in srgb, var(--busy) 18%, transparent); color: var(--busy); }
.type-dot { display: inline-block; width: 8px; height: 8px; border-radius: 50%; background: var(--item); flex: none; }
.type-dot.fluid { background: var(--fluid); }
.type-dot.essentia { background: var(--essentia); }
.stack-icon { width: 16px; height: 16px; flex: none; object-fit: contain; image-rendering: pixelated; }
.stack-icon.lg { width: 32px; height: 32px; }

/* Menus */
details.menu { position: relative; }
details.menu summary { list-style: none; }
details.menu summary::-webkit-details-marker { display: none; }
.menu-items { position: absolute; right: 0; top: 110%; background: var(--surface); border: 1px solid var(--border); border-radius: 6px; z-index: 3; min-width: 110px; }
.menu-items a { display: block; padding: 0.4rem 0.8rem; text-decoration: none; color: var(--text); }
.menu-items a:hover { background: var(--surface-2); }

/* CPUs */
.cpu-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(240px, 1fr)); gap: 0.75rem; margin-bottom: 1.5rem; }
.cpu-card { background: var(--surface); border: 1px solid var(--border); border-left: 4px solid var(--ok); border-radius: var(--radius); padding: 0.75rem; display: flex; flex-direction: column; gap: 0.35rem; }
.cpu-card.busy { border-left-color: var(--busy); }
.cpu-card header { display: flex; justify-content: space-between; align-items: center; }
.cpu-output { display: flex; gap: 0.4rem; align-items: center; }
.cpu-items ul { margin: 0.25rem 0; padding-left: 0; list-style: none; font-size: 12px; max-height: 160px; overflow: auto; }
.cpu-items li { display: flex; gap: 0.35rem; align-items: center; }
.cpu-card button { align-self: flex-start; }

/* Stack picker */
.picker { display: flex; flex-direction: column; gap: 0.4rem; }
.picker .row { margin: 0; align-items: center; }
.picker input[type="search"] { flex: 1; }
.picker-results { list-style: none; margin: 0; padding: 0; max-height: 320px; overflow: auto; border: 1px solid var(--border); border-radius: 6px; background: var(--surface); }
.picker-results:empty { display: none; }
.picker-results li { display: grid; grid-template-columns: 32px 1fr auto auto; gap: 0.5rem; align-items: center; min-height: 40px; box-sizing: border-box; padding: 0.25rem 0.6rem; cursor: pointer; }
.picker-results li > :first-child { justify-self: center; }
.picker-results li:hover, .picker-results li.active { background: var(--surface-2); }
.picker-results li.empty { display: block; padding: 0.5rem 0.6rem; text-align: left; cursor: default; }
.picker-loading { display: flex; flex-direction: column; gap: 0.3rem; padding: 0.5rem 0.6rem; border: 1px solid var(--border); border-radius: 6px; background: var(--surface-2); }
.picker-loading[hidden] { display: none; }
.picker-loading .bar { height: 6px; border-radius: 3px; background: var(--border); overflow: hidden; position: relative; }
.picker-loading .bar span { display: block; height: 100%; background: var(--accent); transition: width 0.3s; }
.picker-loading .bar.indeterminate span { width: 30%; position: absolute; animation: picker-slide 1.2s ease-in-out infinite; }
@keyframes picker-slide { from { left: -30%; } to { left: 100%; } }
@media (prefers-reduced-motion: reduce) { .picker-loading .bar.indeterminate span { animation: none; width: 100%; opacity: 0.5; } }
.picker-chosen { padding: 0.5rem 0.6rem; background: var(--surface-2); border-radius: 6px; display: flex; gap: 0.4rem; align-items: center; }
.manual summary { cursor: pointer; color: var(--accent); font-size: 13px; }

/* Dialogs */
dialog { border: 1px solid var(--border); border-radius: 10px; padding: 0; width: min(520px, 94vw); background: var(--surface); color: var(--text); }
dialog.wide { width: min(760px, 96vw); }
dialog::backdrop { background: rgb(0 0 0 / 0.45); }
.dialog-header { display: flex; justify-content: space-between; align-items: center; padding: 0.8rem 1rem; border-bottom: 1px solid var(--border); }
.dialog-header h2 { margin: 0; }
.dialog-body { padding: 0.5rem 1rem 1rem; max-height: 70vh; overflow: auto; }
.dialog-footer { display: flex; justify-content: flex-end; gap: 0.5rem; padding: 0.75rem 1rem; border-top: 1px solid var(--border); }

/* Log */
ol.log { list-style: none; margin: 0 0 0.75rem; padding: 0; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); font-size: 13px; }
ol.log li { display: grid; grid-template-columns: 11rem 3.5rem 1fr; gap: 0.75rem; padding: 0.35rem 0.75rem; border-bottom: 1px solid var(--border); }
ol.log li:last-child { border-bottom: none; }
ol.log time { color: var(--muted); font-variant-numeric: tabular-nums; }
.log-source { color: var(--muted); font-size: 12px; }
.log-warn .log-msg { color: var(--warn); }
.log-error .log-msg { color: var(--low); }
.log-debug .log-msg { color: var(--muted); }
dl.stats { display: grid; grid-template-columns: auto 1fr; gap: 0.3rem 1rem; margin: 0.5rem 0; }
dl.stats dt { color: var(--muted); }
dl.stats dd { margin: 0; font-variant-numeric: tabular-nums; }

/* Toasts */
.toasts { position: fixed; right: 1rem; bottom: 1rem; display: flex; flex-direction: column; gap: 0.5rem; z-index: 50; }
.toast { background: var(--text); color: var(--bg); padding: 0.6rem 0.9rem; border-radius: 6px; box-shadow: 0 4px 14px rgb(0 0 0 / 0.25); max-width: 380px; }
.toast.error { background: var(--low); color: #fff; }

@media (max-width: 720px) {
  header.top { flex-wrap: wrap; height: auto; padding: 0.5rem 0.75rem; gap: 0.5rem; }
  nav { order: 3; flex-basis: 100%; }
  main { padding: 0.75rem; }
  .row { flex-wrap: wrap; }
  ol.log li { grid-template-columns: 1fr; gap: 0.1rem; }
}
.warn { color: var(--warn); }
