/* sandboxd control-plane dashboard.
   One stylesheet, no framework, no build step. Colours are declared once as
   tokens on :root (light) and re-declared under prefers-color-scheme: dark, so
   every rule below is theme-agnostic. */

:root {
  color-scheme: light dark;

  --bg: #f4f5f7;
  --surface: #ffffff;
  --surface-2: #f8f9fb;
  --surface-3: #eef0f4;
  --border: #dfe2e8;
  --border-strong: #c6cbd4;
  --text: #14171c;
  --muted: #626b7a;
  --faint: #8a93a1;

  --accent: #2f6feb;
  --accent-fg: #ffffff;
  --accent-soft: #e6efff;

  --ok: #1a7f4b;
  --ok-soft: #e2f4ea;
  --warn: #9a6200;
  --warn-soft: #fdf1dc;
  --bad: #b42318;
  --bad-soft: #fdeceb;
  --idle: #5a6472;
  --idle-soft: #eceef2;

  --shadow: 0 1px 2px rgba(16, 20, 28, .06), 0 8px 24px rgba(16, 20, 28, .07);
  --radius: 10px;
  --mono: ui-monospace, SFMono-Regular, "SF Mono", Menlo, Consolas, monospace;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1216;
    --surface: #161a20;
    --surface-2: #1b2027;
    --surface-3: #222831;
    --border: #2a313b;
    --border-strong: #3a434f;
    --text: #e7eaef;
    --muted: #99a3b1;
    --faint: #737e8c;

    --accent: #5b8def;
    --accent-fg: #0f1216;
    --accent-soft: #1c2740;

    --ok: #4ec98a;
    --ok-soft: #14291f;
    --warn: #e0a33e;
    --warn-soft: #2c2417;
    --bad: #f0736a;
    --bad-soft: #2e1a19;
    --idle: #8a94a2;
    --idle-soft: #212730;

    --shadow: 0 1px 2px rgba(0, 0, 0, .4), 0 10px 30px rgba(0, 0, 0, .35);
  }
}

* { box-sizing: border-box; }

/* The drawer, the scrim and the modal layer are toggled with the `hidden`
   attribute, which is only a `display: none` rule in the user-agent stylesheet —
   any author rule setting `display` on the same element (`.drawer { display:
   flex }`, `.modal-root { display: grid }`) outranks it and the element stays
   on screen. That is exactly what happened: a full-page translucent modal layer
   and an empty drawer panel rendered over an otherwise working dashboard. This
   rule makes the attribute win regardless of what a class sets. */
[hidden] { display: none !important; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 14px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-font-smoothing: antialiased;
}

h1, h2, h3 { margin: 0; font-weight: 650; letter-spacing: -.01em; }

button, input, select, textarea { font: inherit; color: inherit; }

code, .mono { font-family: var(--mono); font-size: 12.5px; }

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

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

.topbar {
  position: sticky; top: 0; z-index: 20;
  display: flex; align-items: center; justify-content: space-between; gap: 16px;
  padding: 12px 20px;
  background: color-mix(in srgb, var(--surface) 92%, transparent);
  backdrop-filter: blur(8px);
  border-bottom: 1px solid var(--border);
}

.brand { display: flex; align-items: center; gap: 10px; }
.brand-text { display: flex; flex-direction: column; line-height: 1.2; }
.brand-text strong { font-size: 15px; letter-spacing: -.01em; }
.domain { color: var(--faint); font-family: var(--mono); font-size: 11.5px; }

.brand-mark {
  width: 26px; height: 26px; border-radius: 8px;
  background: linear-gradient(140deg, var(--accent), color-mix(in srgb, var(--accent) 55%, #9b5bef));
  box-shadow: inset 0 0 0 1px rgba(255, 255, 255, .18);
}

.topbar-right { display: flex; align-items: center; gap: 10px; }
.logout-form { margin: 0; }

.refresh-status { color: var(--faint); font-size: 12px; font-variant-numeric: tabular-nums; }
.refresh-status.is-error { color: var(--bad); }

.toggle { display: inline-flex; align-items: center; gap: 6px; color: var(--muted); font-size: 12.5px; cursor: pointer; }
.toggle input { accent-color: var(--accent); }

/* ---------- buttons ---------- */

.btn {
  display: inline-flex; align-items: center; gap: 6px;
  padding: 7px 13px;
  border: 1px solid var(--border-strong);
  border-radius: 8px;
  background: var(--surface);
  color: var(--text);
  cursor: pointer;
  white-space: nowrap;
  transition: background .12s ease, border-color .12s ease, transform .06s ease;
}
.btn:hover { background: var(--surface-3); }
.btn:active { transform: translateY(1px); }
.btn:focus-visible, .chip:focus-visible, .stat:focus-visible, input:focus-visible, select:focus-visible {
  outline: 2px solid var(--accent); outline-offset: 2px;
}
.btn[disabled] { opacity: .5; cursor: not-allowed; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-fg); }
.btn.primary:hover { filter: brightness(1.07); }
.btn.ghost { background: transparent; border-color: transparent; color: var(--muted); }
.btn.ghost:hover { background: var(--surface-3); color: var(--text); }
.btn.danger { color: var(--bad); border-color: color-mix(in srgb, var(--bad) 45%, var(--border)); }
.btn.danger:hover { background: var(--bad-soft); }
.btn.small { padding: 4px 9px; font-size: 12.5px; border-radius: 7px; }

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

.page { max-width: 1400px; margin: 0 auto; padding: 20px; }

.stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 12px; margin-bottom: 18px; }

.stat {
  display: flex; flex-direction: column; gap: 2px;
  padding: 14px 16px; text-align: left;
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  box-shadow: var(--shadow); cursor: pointer;
  transition: border-color .12s ease, transform .06s ease;
}
.stat:hover { border-color: var(--border-strong); }
.stat.is-active { border-color: var(--accent); box-shadow: var(--shadow), inset 0 0 0 1px var(--accent); }
.stat-value { font-size: 26px; font-weight: 640; letter-spacing: -.02em; font-variant-numeric: tabular-nums; }
.stat-value.ok { color: var(--ok); }
.stat-value.warn { color: var(--warn); }
.stat-value.bad { color: var(--bad); }
.stat-value.muted-value { color: var(--muted); }
.stat-label { color: var(--muted); font-size: 12.5px; }

.toolbar { display: flex; flex-wrap: wrap; align-items: center; gap: 10px; margin-bottom: 14px; }
.search-box { flex: 1 1 260px; min-width: 220px; }
.search-box input {
  width: 100%; padding: 8px 12px;
  background: var(--surface); border: 1px solid var(--border); border-radius: 8px;
}
.search-box input::placeholder { color: var(--faint); }

.filters { display: flex; flex-wrap: wrap; gap: 6px; }
.chip {
  padding: 6px 11px; border-radius: 999px;
  border: 1px solid var(--border); background: var(--surface);
  color: var(--muted); font-size: 12.5px; cursor: pointer;
}
.chip:hover { color: var(--text); border-color: var(--border-strong); }
.chip.is-active { background: var(--accent-soft); border-color: var(--accent); color: var(--accent); font-weight: 600; }

.toolbar-actions { display: flex; gap: 8px; margin-left: auto; }

/* ---------- table ---------- */

/* The table scrolls inside its own box rather than with the page. That is what
   makes the sticky header work at all: overflow on this element makes it the
   scroll container, so a header offset against the viewport (top: 51px, to clear
   the topbar) would instead be pushed 51px down from here and cover the first
   row. Scrolling here, with the header stuck to top: 0, also keeps the stat
   cards and the filter bar on screen while paging through a long estate. */
.table-wrap {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  box-shadow: var(--shadow); overflow: auto;
  max-height: calc(100vh - 270px); min-height: 180px;
}

table.workspaces { width: 100%; border-collapse: collapse; }

.workspaces th {
  position: sticky; top: 0; z-index: 5;
  padding: 10px 14px; text-align: left;
  background: var(--surface-2);
  border-bottom: 1px solid var(--border);
  color: var(--muted); font-size: 11.5px; font-weight: 600;
  text-transform: uppercase; letter-spacing: .05em; white-space: nowrap;
}
.workspaces th.sortable { cursor: pointer; user-select: none; }
.workspaces th.sortable:hover { color: var(--text); }
.workspaces th .sort-arrow { margin-left: 4px; color: var(--accent); }

.workspaces td { padding: 11px 14px; border-bottom: 1px solid var(--border); vertical-align: middle; }
.workspaces tbody tr:last-child td { border-bottom: 0; }
.workspaces tbody tr { cursor: pointer; }
.workspaces tbody tr:hover { background: var(--surface-2); }
.workspaces tbody tr.is-selected { background: var(--accent-soft); }

.ws-name { display: flex; flex-direction: column; gap: 2px; }
.ws-name .primary-line { display: flex; align-items: center; gap: 7px; font-weight: 600; }
.ws-name .id { color: var(--faint); font-family: var(--mono); font-size: 11.5px; }
.ws-tag {
  padding: 1px 6px; border-radius: 5px; font-size: 10.5px; font-weight: 600;
  text-transform: uppercase; letter-spacing: .03em;
  background: var(--surface-3); color: var(--muted);
}

.status { display: inline-flex; align-items: center; gap: 7px; white-space: nowrap; }
.dot { width: 8px; height: 8px; border-radius: 50%; flex: none; background: var(--idle); }
.dot.ok { background: var(--ok); }
.dot.warn { background: var(--warn); }
.dot.bad { background: var(--bad); }
.dot.busy { background: var(--accent); animation: pulse 1.4s ease-in-out infinite; }
@keyframes pulse { 0%, 100% { opacity: 1; } 50% { opacity: .3; } }

.image-cell { display: flex; align-items: center; gap: 7px; }
.image-ref { font-family: var(--mono); font-size: 12px; color: var(--muted); max-width: 320px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.badge {
  padding: 2px 7px; border-radius: 999px; font-size: 11px; font-weight: 600; white-space: nowrap;
}
.badge.ok { background: var(--ok-soft); color: var(--ok); }
.badge.warn { background: var(--warn-soft); color: var(--warn); }
.badge.bad { background: var(--bad-soft); color: var(--bad); }
.badge.idle { background: var(--idle-soft); color: var(--idle); }

.time-cell { color: var(--muted); white-space: nowrap; font-variant-numeric: tabular-nums; }
.col-actions { width: 1%; }
.row-actions { display: flex; gap: 6px; justify-content: flex-end; }

.empty { padding: 40px 20px; text-align: center; color: var(--muted); }

/* ---------- drawer ---------- */

.scrim { position: fixed; inset: 0; background: rgba(8, 11, 16, .45); z-index: 30; }

.drawer {
  position: fixed; top: 0; right: 0; bottom: 0; z-index: 31;
  width: min(520px, 100vw);
  display: flex; flex-direction: column;
  background: var(--surface); border-left: 1px solid var(--border);
  box-shadow: -12px 0 40px rgba(8, 11, 16, .18);
  overflow-y: auto;
}

.drawer-head {
  position: sticky; top: 0;
  display: flex; align-items: flex-start; justify-content: space-between; gap: 12px;
  padding: 18px 20px 14px;
  background: var(--surface); border-bottom: 1px solid var(--border);
}
.drawer-head h2 { font-size: 17px; }
.drawer-head .sub { color: var(--muted); font-size: 12.5px; margin-top: 3px; }
.drawer-body { padding: 16px 20px 28px; display: flex; flex-direction: column; gap: 18px; }

.section-title {
  color: var(--faint); font-size: 11px; font-weight: 650;
  text-transform: uppercase; letter-spacing: .06em; margin-bottom: 8px;
}

.kv { display: grid; grid-template-columns: 130px 1fr; gap: 8px 12px; align-items: baseline; }
.kv dt { color: var(--muted); font-size: 12.5px; }
.kv dd { margin: 0; overflow-wrap: anywhere; }

.copy-field { display: flex; align-items: center; gap: 8px; }
.copy-field .value { font-family: var(--mono); font-size: 12.5px; overflow-wrap: anywhere; }

.action-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(140px, 1fr)); gap: 8px; }
.action-grid .btn { justify-content: center; }

.link-row { display: flex; flex-wrap: wrap; gap: 8px; }
a.btn { text-decoration: none; }

/* ---------- modal ---------- */

.modal-root { position: fixed; inset: 0; z-index: 40; display: grid; place-items: center; padding: 20px; background: rgba(8, 11, 16, .5); }

.modal {
  width: min(560px, 100%); max-height: 88vh; overflow-y: auto;
  background: var(--surface); border: 1px solid var(--border);
  border-radius: 14px; box-shadow: var(--shadow);
}
.modal-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 18px 20px 12px; }
.modal-head h2 { font-size: 16px; }
.modal-body { padding: 4px 20px 8px; display: flex; flex-direction: column; gap: 14px; }
.modal-body p { margin: 0; color: var(--muted); }
.modal-foot { display: flex; justify-content: flex-end; gap: 8px; padding: 14px 20px 18px; }

.field { display: flex; flex-direction: column; gap: 5px; }
.field label { font-size: 12.5px; color: var(--muted); }
.field input, .field select {
  padding: 8px 11px; background: var(--surface-2);
  border: 1px solid var(--border); border-radius: 8px;
}
.field .hint { font-size: 11.5px; color: var(--faint); }
.field-row { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }

.checkbox { display: flex; align-items: center; gap: 8px; color: var(--muted); font-size: 13px; }
.checkbox input { accent-color: var(--accent); }

details.advanced summary { cursor: pointer; color: var(--muted); font-size: 12.5px; margin-bottom: 10px; }
details.advanced[open] summary { margin-bottom: 12px; }
details.advanced .field + .field, details.advanced .field-row + .field { margin-top: 12px; }

.modal-error { color: var(--bad); background: var(--bad-soft); padding: 9px 12px; border-radius: 8px; font-size: 13px; }
.modal-note { background: var(--surface-2); border: 1px solid var(--border); border-radius: 8px; padding: 10px 12px; font-size: 12.5px; color: var(--muted); }
.id-list { max-height: 180px; overflow-y: auto; font-family: var(--mono); font-size: 12px; color: var(--muted); }

/* ---------- toasts ---------- */

.toasts { position: fixed; right: 16px; bottom: 16px; z-index: 60; display: flex; flex-direction: column; gap: 8px; max-width: min(420px, calc(100vw - 32px)); }

.toast {
  display: flex; gap: 10px; align-items: flex-start;
  padding: 11px 13px; border-radius: 10px;
  background: var(--surface); border: 1px solid var(--border);
  border-left: 3px solid var(--accent);
  box-shadow: var(--shadow);
  animation: toast-in .16s ease-out;
}
.toast.ok { border-left-color: var(--ok); }
.toast.bad { border-left-color: var(--bad); }
.toast .toast-body { flex: 1; }
.toast .toast-title { font-weight: 600; margin-bottom: 2px; }
.toast .toast-text { color: var(--muted); font-size: 12.5px; overflow-wrap: anywhere; }
@keyframes toast-in { from { opacity: 0; transform: translateY(6px); } to { opacity: 1; transform: none; } }

/* ---------- login ---------- */

.login-body { display: grid; place-items: center; min-height: 100vh; padding: 20px; }

.login-card {
  width: min(400px, 100%);
  background: var(--surface); border: 1px solid var(--border);
  border-radius: 14px; box-shadow: var(--shadow); padding: 26px;
}
.login-brand { display: flex; align-items: center; gap: 12px; margin-bottom: 22px; }
.login-brand h1 { font-size: 19px; }
.login-domain { margin: 2px 0 0; color: var(--faint); font-family: var(--mono); font-size: 12px; }
.login-form { display: flex; flex-direction: column; gap: 8px; }
.login-form label { font-size: 12.5px; color: var(--muted); }
.login-form input {
  padding: 9px 12px; background: var(--surface-2);
  border: 1px solid var(--border); border-radius: 8px;
}
.login-form button {
  margin-top: 6px; padding: 9px 14px;
  background: var(--accent); border: 1px solid var(--accent); color: var(--accent-fg);
  border-radius: 8px; font-weight: 600; cursor: pointer;
}
.login-form button:hover { filter: brightness(1.07); }
.login-error { margin: 0 0 14px; padding: 9px 12px; border-radius: 8px; background: var(--bad-soft); color: var(--bad); font-size: 13px; }
.login-hint { margin: 18px 0 0; color: var(--faint); font-size: 12px; line-height: 1.5; }
.login-hint code { color: var(--muted); }

@media (max-width: 720px) {
  .topbar { flex-wrap: wrap; }
  .toolbar-actions { margin-left: 0; width: 100%; }
  /* Short viewports lose more to a fixed-height box than they gain from a
     sticky header, so the table goes back into the page flow. */
  .table-wrap { max-height: none; }
  .kv { grid-template-columns: 1fr; gap: 2px 0; }
  .kv dd { margin-bottom: 8px; }
}
