:root {
  font-family: "IBM Plex Sans", "Segoe UI", system-ui, sans-serif;
  color-scheme: dark;
  --bg:           #060708;
  --panel:        #101114;
  --panel-strong: #15171a;
  --panel-soft:   #1b1d21;
  --text:         #eceef1;
  --text-muted:   #a7abb3;
  --text-subtle:  #868b94;
  --border:       #24262b;
  --border-mid:   #34383f;
  --border-strong:#4b5059;
  --accent-bg:    #e6e9ee;
  --accent-text:  #111316;
  --ok:    #9fe0aa; --ok-bg:   #102417; --ok-bd:   #315f3a;
  --warn:  #ffe08a; --warn-bg: #1a1500; --warn-bd: #6b5a00;
  --bad:   #ffc2ca; --bad-bg:  #251315; --bad-bd:  #5f3137;
  --blue:  #80c8ff; --blue-bg: #0d1f30; --blue-bd: #1e4a70;
  --field-bg: #0d0f12;
  --code-bg: #0e1013;
  --pre-bg: #0c0e11;
  --overlay-bg: rgba(0,0,0,.72);
  --loading-overlay: rgba(6,7,8,.78);
  --shadow: rgba(0,0,0,.55);
}

:root[data-theme="gray"] {
  color-scheme: dark;
  --bg:#303236;--panel:#393b40;--panel-strong:#42454a;--panel-soft:#3f4247;
  --text:#f4f4f5;--text-muted:#c8c9cc;--text-subtle:#b0b3b8;
  --border:#4b4e54;--border-mid:#60646b;--border-strong:#73777f;
  --accent-bg:#f4f4f5;--accent-text:#24262a;
  --ok:#b9dfc0;--ok-bg:#294032;--ok-bd:#486d53;
  --warn:#f0d18b;--warn-bg:#4a3d24;--warn-bd:#756333;
  --bad:#efb5bb;--bad-bg:#4a2c31;--bad-bd:#784951;
  --blue:#bbd6ea;--blue-bg:#2a3c49;--blue-bd:#4f7086;
  --field-bg:#35373b;--code-bg:#35373b;--pre-bg:#2f3135;
  --overlay-bg:rgba(22,23,25,.72);--loading-overlay:rgba(48,50,54,.78);--shadow:rgba(10,10,10,.38);
}

:root[data-theme="light"] {
  color-scheme: light;
  --bg:#f3f5f7;--panel:#fff;--panel-strong:#e8eef3;--panel-soft:#eaf0f4;
  --text:#18222c;--text-muted:#536170;--text-subtle:#606d78;
  --border:#d7dde3;--border-mid:#bac4ce;--border-strong:#9daab5;
  --accent-bg:#24557d;--accent-text:#fff;
  --ok:#27663a;--ok-bg:#e0f1e4;--ok-bd:#a9d1b2;
  --warn:#735c17;--warn-bg:#fff2c7;--warn-bd:#d8c173;
  --bad:#8a343c;--bad-bg:#f7dfe1;--bad-bd:#d6aeb2;
  --blue:#285e86;--blue-bg:#dcecf7;--blue-bd:#a9cadf;
  --field-bg:#fff;--code-bg:#eef2f5;--pre-bg:#eef2f5;
  --overlay-bg:rgba(24,34,44,.32);--loading-overlay:rgba(243,245,247,.84);--shadow:rgba(24,34,44,.2);
}

*, *::before, *::after { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { margin: 0; height: 100%; background: var(--bg); color: var(--text); overflow: hidden; }
button, input, textarea, select { font: inherit; }
a { color: inherit; text-decoration: none; }

/* ── Scrollbars (global) ───────────────────── */
* { scrollbar-width: none; }
::-webkit-scrollbar,*::-webkit-scrollbar { width: 0; height: 0; display: none; }

/* ── App shell ─────────────────────────────────── */
.app-shell { height: 100vh; display: flex; flex-direction: column; }
@supports (height: 100dvh) { .app-shell { height: 100dvh; } }
button.mobile-modules-btn, .mobile-sidebar-backdrop { display: none; }

/* ── Topbar ────────────────────────────────────── */
.topbar {
  height: 46px; padding: 0 14px; flex-shrink: 0;
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  border-bottom: 1px solid var(--border);
}
.topbar__path { display: flex; align-items: baseline; gap: 0; font-size: 0.9rem; }
.topbar__brand { font-weight: 700; letter-spacing: 0.04em; }
.topbar__brand:hover { color: var(--text-muted); }
.topbar__sep   { margin: 0 8px; color: var(--border-strong); }
.topbar__crumb { color: var(--text-muted); }
.topbar__actions { display: flex; align-items: center; gap: 8px; }
.theme-toggle { width: 36px; padding: 0; }
.theme-toggle__icon {
  width: 15px; height: 15px; border: 1.5px solid currentColor; border-radius: 50%;
  background: currentColor;
}
:root[data-theme="gray"] .theme-toggle__icon { background: linear-gradient(90deg,currentColor 50%,transparent 50%); }
:root[data-theme="light"] .theme-toggle__icon { background: transparent; box-shadow: inset 0 0 0 2px var(--accent-bg); }
.global-busy {
  display: inline-flex; align-items: center; gap: 7px;
  min-height: 28px; padding: 4px 9px;
  border: 1px solid var(--border-mid); background: var(--panel-strong);
  color: var(--text-muted); font-size: 0.78rem;
}
.global-busy__spin {
  width: 13px; height: 13px; border: 2px solid var(--border-strong);
  border-top-color: var(--text); border-radius: 50%;
  animation: spin .75s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ── Workspace ─────────────────────────────────── */
.workspace {
  flex: 1; min-height: 0;
  display: grid; grid-template-columns: 240px minmax(0, 1fr);
}
.workspace--settings { grid-template-columns: 200px minmax(0, 1fr); }

/* ── Sidebar ───────────────────────────────────── */
.sidebar {
  border-right: 1px solid var(--border); background: var(--panel);
  display: flex; flex-direction: column; min-height: 0; overflow: hidden;
}
.sidebar__head { padding: 10px 12px 6px; border-bottom: 1px solid var(--border); flex-shrink: 0; }
.module-list {
  flex: 1; overflow-y: auto; padding: 6px;
  display: flex; flex-direction: column; gap: 3px;
  scrollbar-width: none;
}
.sidebar__empty { margin: 10px 6px; color: var(--text-subtle); font-size: 0.82rem; }

.module-item {
  display: flex; align-items: center; gap: 8px;
  padding: 7px 10px; border: 1px solid transparent; background: transparent;
  color: var(--text); cursor: pointer; text-align: left; width: 100%; min-width: 0;
}
.module-item:hover { border-color: var(--border); background: var(--panel-strong); }
.module-item--active { border-color: var(--border-mid) !important; background: var(--panel-soft) !important; }

.module-item__dot {
  width: 7px; height: 7px; border-radius: 50%; flex-shrink: 0; background: var(--border-strong);
}
.module-item__dot--active  { background: var(--ok); }
.module-item__dot--paused  { background: var(--warn); }
.module-item__dot--error   { background: var(--bad); }
.module-item__dot--restarting {
  background: var(--blue);
  box-shadow: 0 0 0 0 rgba(128, 200, 255, 0.55);
  animation: module-dot-restart 1s ease-out infinite;
}
@keyframes module-dot-restart {
  70% { box-shadow: 0 0 0 5px rgba(128, 200, 255, 0); }
  100% { box-shadow: 0 0 0 0 rgba(128, 200, 255, 0); }
}

.module-item__name { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: 0.88rem; }
.module-item__ver  { color: var(--text-subtle); font-size: 0.74rem; flex-shrink: 0; }
.module-item__drag { color: var(--border-strong); font-size: 0.7rem; cursor: grab; padding: 0 2px; flex-shrink: 0; line-height: 1; }
.module-item__drag:active { cursor: grabbing; }
.module-item--dragging { opacity: 0.35; border-style: dashed !important; }
.module-item--dragover  { border-color: var(--accent-bg) !important; }

/* ── Content area ──────────────────────────────── */
.content-area { display: flex; flex-direction: column; min-height: 0; overflow: hidden; }

.welcome-screen { flex: 1; display: flex; align-items: center; justify-content: center; }
.welcome-screen__inner { text-align: center; }
.welcome-logo {
  width: 48px; height: 48px; border-radius: 50%;
  border: 2px solid var(--border-mid); margin: 0 auto 16px;
  background: radial-gradient(circle at 35% 35%, var(--panel-soft), var(--panel));
}
.welcome-title { margin: 0 0 6px; font-size: 1rem; font-weight: 600; color: var(--text-muted); }
.welcome-sub   { margin: 0; color: var(--text-subtle); font-size: 0.82rem; }

/* ── Module toolbar + frame ────────────────────── */
.module-view { display: flex; flex-direction: column; flex: 1; min-height: 0; }

.module-toolbar {
  height: 42px; padding: 0 12px; flex-shrink: 0;
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  border-bottom: 1px solid var(--border); background: var(--panel);
}
.module-toolbar__info    { display: flex; align-items: center; gap: 8px; }
.module-toolbar__name    { font-weight: 600; font-size: 0.9rem; }
.module-toolbar__ver     { color: var(--text-subtle); font-size: 0.78rem; }
.module-toolbar__status  { font-size: 0.74rem; padding: 2px 7px; border: 1px solid var(--border-strong); color: var(--text-muted); }
.module-toolbar__status--active { color: var(--ok);   border-color: var(--ok-bd);   background: var(--ok-bg); }
.module-toolbar__status--paused { color: var(--warn); border-color: var(--warn-bd); background: var(--warn-bg); }
.module-toolbar__status--error  { color: var(--bad);  border-color: var(--bad-bd);  background: var(--bad-bg); }
.module-toolbar__actions { display: flex; gap: 6px; }

.module-stage { position: relative; flex: 1; min-height: 0; overflow: hidden; }
.module-update { display: flex; align-items: center; justify-content: space-between; gap: 12px; flex-wrap: wrap; padding: 7px 12px; border-bottom: 1px solid var(--warn-bd); background: var(--warn-bg); color: var(--warn); font-size: .8rem; }
.module-update span { min-width: 0; flex: 1 1 230px; overflow-wrap: anywhere; }
.module-frame { display: block; border: none; width: 100%; height: 100%; background: var(--bg); }
.module-loading {
  position: absolute; inset: 0; z-index: 10;
  display: grid; place-items: center; padding: 18px;
  background: var(--loading-overlay); backdrop-filter: blur(1px);
}
.module-loading__box {
  width: min(430px, 100%); padding: 18px 20px;
  display: grid; grid-template-columns: 26px minmax(0, 1fr); gap: 10px 14px; align-items: center;
  border: 1px solid var(--border-strong); background: var(--panel);
  box-shadow: 0 18px 54px var(--shadow);
}
.module-loading__spin {
  width: 22px; height: 22px; border: 2px solid var(--border-strong);
  border-top-color: var(--text); border-radius: 50%; animation: spin .72s linear infinite;
}
.module-loading__box strong { display: block; font-size: .9rem; font-weight: 600; }
.module-loading__box p { margin: 4px 0 0; color: var(--text-muted); font-size: .8rem; line-height: 1.45; }
.module-loading__actions { grid-column: 1 / -1; display: flex; gap: 8px; padding-top: 4px; }

/* ── Settings sections ─────────────────────────── */
.settings-section { flex: 1; overflow-y: auto; padding: 16px; display: flex; flex-direction: column; gap: 16px; }
.settings-header {
  display: flex; align-items: flex-start; justify-content: space-between; gap: 12px;
}
.settings-header h2 { margin: 0; font-size: 1rem; font-weight: 600; }
.settings-upload-block { display: grid; gap: 10px; }

.env-info.panel { padding: 12px 14px; border: 1px solid var(--border); background: var(--panel); }
.env-info__text { margin: 0; font-size: 0.82rem; color: var(--text-muted); line-height: 1.6; }
.env-info__text strong { color: var(--text); }
.env-info__text code { color: var(--warn); font-family: monospace; font-size: 0.9em; }

.env-keys { display: flex; flex-wrap: wrap; gap: 6px; }
.env-key {
  padding: 3px 8px; border: 1px solid var(--border-mid);
  color: var(--text-muted); font-family: monospace; font-size: 0.8rem;
}

/* ── Buttons ───────────────────────────────────── */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  min-height: 32px; padding: 6px 12px;
  border: 1px solid var(--border-strong); background: var(--panel-strong);
  color: var(--text); cursor: pointer; white-space: nowrap; font-size: 0.84rem;
  transition: background-color .1s ease, border-color .1s ease, transform .1s ease;
}
.btn:not(:disabled):active { transform: translateY(1px); }
.btn:disabled { opacity: 0.35; cursor: default; }
.btn--sm     { min-height: 28px; padding: 4px 10px; font-size: 0.8rem; }
.btn--xs     { min-height: 24px; padding: 2px 7px; font-size: 0.76rem; }
.btn--icon   { width: 28px; min-height: 28px; padding: 0; font-weight: 700; }
.btn--primary { background: var(--accent-bg); color: var(--accent-text); border-color: var(--accent-bg); }
.btn--ghost   { background: transparent; border-color: var(--border); color: var(--text-muted); }
.btn--danger  { background: var(--bad-bg); border-color: var(--bad-bd); color: var(--bad); }

/* ── Forms ─────────────────────────────────────── */
.form-label { display: grid; gap: 4px; color: var(--text-muted); font-size: 0.82rem; }
.form-input {
  width: 100%; min-height: 34px; padding: 6px 9px;
  border: 1px solid var(--border-strong); background: var(--field-bg); color: var(--text);
}
.module-check-list {
  display: grid; gap: 6px; max-height: 220px; overflow: auto;
  padding: 8px; border: 1px solid var(--border); background: var(--field-bg);
}
.check-row {
  min-width: 0; display: grid; grid-template-columns: 16px minmax(0, 1fr) auto;
  align-items: center; gap: 8px; padding: 6px 8px;
  border: 1px solid var(--border); background: var(--panel);
  color: var(--text); font-size: 0.82rem;
}
.check-row--all { grid-template-columns: 16px minmax(0, 1fr); background: var(--panel-strong); }
.check-row input { width: 16px; height: 16px; margin: 0; padding: 0; }
.check-row span { min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.check-row code { color: var(--text-subtle); font-family: "IBM Plex Mono", monospace; font-size: 0.76rem; }
.check-row:has(input:disabled) { opacity: 0.55; }

/* ── Table ─────────────────────────────────────── */
.table-wrap { width: 100%; max-width: 100%; min-width: 0; overflow: auto; }
table { width: 100%; min-width: 720px; border-collapse: collapse; font-size: 0.84rem; }
th, td { padding: 8px 12px; border-bottom: 1px solid var(--border); text-align: left; vertical-align: middle; }
th { color: var(--text-subtle); font-weight: 500; background: var(--panel); position: sticky; top: 0; }
tr:hover td { background: var(--panel-strong); }
td:last-child { white-space: nowrap; }

/* ── Role badges ───────────────────────────────── */
.role-badge { padding: 1px 7px; border: 1px solid var(--border-mid); font-size: 0.75rem; color: var(--text-muted); }
.role-badge--admin  { color: var(--bad);  border-color: var(--bad-bd);  background: var(--bad-bg); }
.role-badge--editor { color: var(--blue); border-color: var(--blue-bd); background: var(--blue-bg); }
.role-badge--viewer { color: var(--text-subtle); }

/* ── Status / misc ─────────────────────────────── */
.eyebrow {
  margin: 0 0 4px; color: var(--text-subtle);
  text-transform: uppercase; letter-spacing: 0.14em; font-size: 0.68rem;
}
.status-line     { margin: 0; color: var(--text-subtle); font-size: 0.8rem; min-height: 18px; }
.status-line--ok  { color: var(--ok); }
.status-line--err { color: var(--bad); }
.panel { background: var(--panel); border: 1px solid var(--border); }

/* ── Overlay / Dialog ──────────────────────────── */
.overlay {
  position: fixed; inset: 0; background: var(--overlay-bg);
  display: grid; place-items: center; padding: 20px; z-index: 100;
}
.dialog {
  width: min(480px, 100%); background: var(--panel);
  border: 1px solid var(--border-strong); box-shadow: 0 24px 80px var(--shadow);
}
.dialog--wide { width: min(760px, 100%); }
.dialog__head {
  display: flex; align-items: center; justify-content: space-between;
  gap: 12px; padding: 14px 16px; border-bottom: 1px solid var(--border);
}
.dialog__head h2 { margin: 0; font-size: 1rem; font-weight: 600; }
.dialog__body { padding: 16px; display: grid; gap: 12px; }
.dialog__body--scroll { overflow-y: auto; max-height: 70vh; }
.dialog__actions { display: flex; justify-content: flex-end; gap: 8px; flex-wrap: wrap; }
.dialog__foot {
  padding: 12px 16px; border-top: 1px solid var(--border);
  display: flex; justify-content: flex-end; gap: 8px;
}
.token-input { font-family: "IBM Plex Mono", monospace; font-size: 0.8rem; }
.token-textarea {
  min-height: 96px; resize: vertical;
  font-family: "IBM Plex Mono", "Consolas", monospace; font-size: 0.8rem; line-height: 1.45;
}

/* ── Upload zone ───────────────────────────────── */
.upload-zone {
  display: flex; align-items: center; justify-content: center;
  min-height: 90px; border: 2px dashed var(--border-mid); background: var(--field-bg);
  color: var(--text-muted); cursor: pointer; font-size: 0.84rem;
  text-align: center; padding: 16px;
}
.upload-zone:hover { border-color: var(--border-strong); }

/* ── Toast notifications ──────────────────────── */
.toast-wrap {
  position: fixed; bottom: 16px; right: 16px;
  display: flex; flex-direction: column; gap: 6px;
  z-index: 9999; pointer-events: none; max-width: 320px;
}
.toast {
  padding: 8px 12px; font-size: 0.8rem; line-height: 1.4;
  border: 1px solid; pointer-events: all;
  animation: toast-in .15s ease; cursor: pointer;
}
@keyframes toast-in {
  from { opacity: 0; transform: translateY(6px); }
  to   { opacity: 1; transform: translateY(0); }
}
.toast--error { background: var(--bad-bg);  border-color: var(--bad-bd);  color: var(--bad);  }
.toast--warn  { background: var(--warn-bg); border-color: var(--warn-bd); color: var(--warn); }
.toast--ok    { background: var(--ok-bg);   border-color: var(--ok-bd);   color: var(--ok);   }

/* ── Login ─────────────────────────────────────── */
.login-body { overflow: auto; }
.login-shell { min-height: 100vh; display: flex; align-items: center; justify-content: center; padding: 24px; }
.login-card { width: min(360px,100%); background: var(--panel); border: 1px solid var(--border-strong); padding: 32px; display: grid; gap: 24px; }
.login-brand { display: flex; align-items: center; gap: 12px; }
.login-brand__mark { width: 32px; height: 32px; border-radius: 50%; border: 2px solid var(--border-mid); background: radial-gradient(circle at 35% 35%, var(--panel-soft), var(--bg)); }
.login-brand strong { display: block; font-weight: 700; letter-spacing: 0.04em; }
.login-brand span   { font-size: 0.74rem; color: var(--text-subtle); }
.login-form { display: grid; gap: 12px; }
.login-form label { display: grid; gap: 5px; font-size: 0.82rem; color: var(--text-muted); }
.login-form input { width: 100%; min-height: 36px; padding: 8px 10px; border: 1px solid var(--border-strong); background: var(--field-bg); color: var(--text); }
.login-error { margin: 0; color: var(--bad); font-size: 0.82rem; }

/* ── Docs content ──────────────────────────────── */
.docs-body { font-size: 0.9rem; line-height: 1.7; color: var(--text-muted); }
.docs-body h2 { color: var(--text); font-size: 1rem; margin: 16px 0 6px; }
.docs-body h3 { color: var(--text-muted); font-size: 0.88rem; margin: 12px 0 4px; }
.docs-body code { background: var(--code-bg); border: 1px solid var(--border); padding: 1px 5px; font-size: 0.85em; color: var(--warn); font-family: "IBM Plex Mono", monospace; }
.docs-body pre { background: var(--pre-bg); border: 1px solid var(--border); padding: 12px; overflow-x: auto; }
.docs-body pre code { background: none; border: none; padding: 0; }
.docs-body table { width: 100%; border-collapse: collapse; margin: 8px 0; }
.docs-body th, .docs-body td { padding: 6px 10px; border: 1px solid var(--border); font-size: 0.82rem; }
.docs-body th { background: var(--panel-strong); color: var(--text-subtle); font-weight: 500; }

/* ── Mobile shell ─────────────────────────────── */
@media (max-width: 760px) {
  .topbar { height: 44px; padding: 0 8px; gap: 6px; }
  .topbar__path { min-width: 0; flex: 1; }
  .topbar__sep, .topbar__crumb { display: none; }
  .topbar__actions { flex-shrink: 0; gap: 4px; }
  button.mobile-modules-btn { display: inline-flex; min-height: 36px; }
  .topbar__actions > a, #themeToggle, #uploadBtn, .topbar__logout {
    width: 36px; min-height: 36px; padding: 0;
  }
  #uploadBtn, .topbar__logout { font-size: 0; }
  #uploadBtn::before { content: "+"; font-size: 18px; line-height: 1; }
  .topbar__logout::before { content: "×"; font-size: 18px; line-height: 1; }
  .global-busy {
    position: fixed; z-index: 95; top: 50px; left: 8px; right: 8px;
    justify-content: center; box-shadow: 0 8px 24px rgba(0,0,0,.55);
  }

  .workspace { display: block; position: relative; min-width: 0; overflow: hidden; }
  .content-area { width: 100%; height: 100%; min-width: 0; }
  .sidebar {
    position: fixed; z-index: 80; top: 44px; bottom: 0; left: 0;
    width: min(86vw, 320px); border-right-color: var(--border-strong);
    transform: translateX(-102%); transition: transform .16s ease;
    box-shadow: 18px 0 42px rgba(0,0,0,.58);
  }
  .workspace--mobile-nav .sidebar { transform: translateX(0); }
  .mobile-sidebar-backdrop {
    position: fixed; z-index: 70; inset: 44px 0 0;
    width: 100%; height: auto; border: 0; padding: 0;
    background: rgba(0,0,0,.68); cursor: pointer;
  }
  .mobile-sidebar-backdrop:not([hidden]) { display: block; }
  .module-list { padding: 8px; }
  .module-item { min-height: 44px; padding: 9px 10px; }

  .module-toolbar {
    min-height: 46px; height: auto; padding: 5px 7px; gap: 5px;
    flex-wrap: wrap;
  }
  .module-toolbar__info { flex: 1 1 150px; min-width: 0; gap: 6px; }
  .module-toolbar__name { min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
  .module-toolbar__ver { display: none; }
  .module-toolbar__actions { margin-left: auto; gap: 4px; }
  .module-toolbar .btn { min-height: 36px; }
  .module-frame { min-height: 0; }

  .overlay { padding: 8px; }
  .dialog { max-height: calc(100dvh - 16px); overflow: hidden; }
  .dialog__body--scroll { max-height: calc(100dvh - 84px); }
  .docs-body pre, .docs-body code { white-space: pre-wrap; overflow-wrap: anywhere; }
  .docs-body pre { overflow-x: hidden; }
}

@media (prefers-reduced-motion: reduce) {
  .sidebar { transition: none; }
  .btn { transition: none; }
  .module-loading__spin, .global-busy__spin { animation-duration: 1.5s; }
}
