:root {
  --bg: #f5f7fb;
  --surface: #ffffff;
  --surface-alt: #f8fafc;
  --surface-hover: #f1f5f9;
  --border: #e3e8ef;
  --border-strong: #d3dae5;
  --text: #172033;
  --muted: #6b7280;
  --subtle: #94a3b8;
  --primary: #2563eb;
  --primary-hover: #1d4ed8;
  --primary-soft: #eaf2ff;
  --success: #16a34a;
  --success-soft: #eaf8ef;
  --warning: #d97706;
  --danger: #dc2626;
  --danger-soft: #fff0f0;
  --shadow: 0 1px 2px rgba(15, 23, 42, .04), 0 4px 14px rgba(15, 23, 42, .04);
  --radius: 12px;
  --radius-sm: 8px;
  font-family: Inter, "Segoe UI", "Microsoft YaHei", system-ui, sans-serif;
  color-scheme: light;
}

:root[data-theme="dark"] {
  --bg: #0f1115;
  --surface: #181b21;
  --surface-alt: #20242c;
  --surface-hover: #252a33;
  --border: #2b3039;
  --border-strong: #3a414d;
  --text: #edf1f7;
  --muted: #a0a8b5;
  --subtle: #77808e;
  --primary: #60a5fa;
  --primary-hover: #93c5fd;
  --primary-soft: #172a46;
  --success: #4ade80;
  --success-soft: #173224;
  --warning: #fbbf24;
  --danger: #f87171;
  --danger-soft: #3a2024;
  --shadow: 0 1px 2px rgba(0, 0, 0, .28), 0 6px 18px rgba(0, 0, 0, .18);
  color-scheme: dark;
}

* { box-sizing: border-box; }

html, body { min-height: 100%; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-size: 14px;
  line-height: 1.5;
  transition: background-color .18s ease, color .18s ease;
}

button, input, textarea, select { font: inherit; }
button { cursor: pointer; }

.hidden { display: none !important; }
.muted { color: var(--muted); }
.eyebrow { display: none; }

.login-shell {
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: 28px;
  position: relative;
}

.login-card {
  width: min(420px, 100%);
  padding: 34px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 16px;
  box-shadow: var(--shadow);
}

.login-card h1 { margin: 20px 0 6px; font-size: 26px; letter-spacing: -.02em; }
.login-card > .muted { margin: 0 0 24px; }
.login-card label, .modal-body label, .setting-card > label, .tier-form label {
  display: grid;
  gap: 7px;
  margin: 0 0 16px;
  color: var(--text);
  font-size: 13px;
  font-weight: 600;
}

.brand-mark {
  width: 44px;
  height: 44px;
  display: inline-grid;
  place-items: center;
  border-radius: 11px;
  background: var(--primary);
  color: #fff;
  font-weight: 800;
  letter-spacing: -.03em;
}

.brand-mark.small { width: 38px; height: 38px; border-radius: 9px; font-size: 13px; }

input, textarea, select {
  width: 100%;
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-sm);
  background: var(--surface);
  color: var(--text);
  outline: none;
  padding: 10px 12px;
  transition: border-color .16s ease, box-shadow .16s ease, background-color .16s ease;
}

input:focus, textarea:focus, select:focus {
  border-color: var(--primary);
  box-shadow: 0 0 0 3px color-mix(in srgb, var(--primary) 16%, transparent);
}

textarea { resize: vertical; min-height: 120px; }

button {
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-sm);
  background: var(--surface);
  color: var(--text);
  padding: 9px 13px;
  font-weight: 600;
  transition: background-color .16s ease, border-color .16s ease, color .16s ease;
}

button:hover { background: var(--surface-hover); }
button.primary { background: var(--primary); border-color: var(--primary); color: #fff; }
button.primary:hover { background: var(--primary-hover); border-color: var(--primary-hover); }
button.ghost { background: transparent; }
button.danger, .entity-actions button.danger { color: var(--danger); background: var(--danger-soft); border-color: transparent; }
button.wide { width: 100%; padding-block: 11px; }

.theme-toggle, .icon-button {
  width: 38px;
  height: 38px;
  padding: 0;
  display: inline-grid;
  place-items: center;
  font-size: 17px;
}

.login-theme-toggle { position: absolute; top: 24px; right: 24px; }

.error-line { min-height: 20px; margin: 12px 0 0; color: var(--danger); font-size: 13px; }

.app { min-height: 100vh; }

.topbar {
  height: 68px;
  display: grid;
  grid-template-columns: 240px 1fr auto;
  align-items: center;
  gap: 24px;
  padding: 0 22px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 20;
}

.brand, .account, .top-stats, .top-stats > span { display: flex; align-items: center; }
.brand { gap: 11px; }
.brand div, .account div { display: grid; }
.brand small, .account small { color: var(--muted); font-size: 11px; }
.top-stats { justify-content: center; gap: 8px; }
.top-stats > span { gap: 5px; padding: 6px 10px; background: var(--surface-alt); border-radius: 7px; color: var(--muted); font-size: 12px; }
.top-stats b { color: var(--text); }
.account { justify-content: flex-end; gap: 10px; text-align: right; }

.dot { width: 7px; height: 7px; display: inline-block; border-radius: 50%; background: var(--subtle); }
.dot.online { background: var(--success); }

.workspace { display: grid; grid-template-columns: 240px minmax(0, 1fr); min-height: calc(100vh - 68px); }

.sidebar {
  padding: 20px 14px;
  background: var(--surface);
  border-right: 1px solid var(--border);
  position: sticky;
  top: 68px;
  height: calc(100vh - 68px);
  overflow-y: auto;
}

.sidebar > p {
  margin: 22px 12px 8px;
  color: var(--subtle);
  font-size: 11px;
  font-weight: 700;
}

.sidebar > p:first-of-type { margin-top: 12px; }

.nav-item {
  width: 100%;
  display: flex;
  align-items: center;
  gap: 11px;
  margin: 3px 0;
  padding: 10px 12px;
  border: 0;
  background: transparent;
  color: var(--muted);
  text-align: left;
}

.nav-item span { width: 20px; color: var(--subtle); text-align: center; font-size: 15px; }
.nav-item:hover { background: var(--surface-hover); color: var(--text); }
.nav-item.active { background: var(--primary-soft); color: var(--primary); }
.nav-item.active span { color: var(--primary); }

.content-area { min-width: 0; padding: 24px 26px 32px; }
.page { display: none; }
.page.active { display: block; }

.page-title {
  min-height: 48px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  margin-bottom: 18px;
}

.page-title h1 { margin: 0; font-size: 24px; line-height: 1.2; letter-spacing: -.02em; }
.page-title .muted { font-size: 13px; }

.panel {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  overflow: hidden;
}

.panel-head {
  min-height: 56px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 14px 16px;
  border-bottom: 1px solid var(--border);
}

.panel-head h2, .setting-card h2 { margin: 0; font-size: 16px; }
.left-stack, .right-stack { display: grid; gap: 14px; min-height: 0; }
.left-stack { grid-template-rows: minmax(250px, 1fr) auto; }
.right-stack { grid-template-rows: 1fr 1fr; }

.dashboard {
  min-height: calc(100vh - 158px);
  display: grid;
  grid-template-columns: minmax(250px, 285px) minmax(420px, 1fr) minmax(280px, 330px);
  gap: 14px;
}

.card-list, .table-list { padding: 14px; display: grid; gap: 10px; align-content: start; }
.card-list { overflow: auto; }

.entity-card {
  padding: 14px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--surface);
}

.entity-row { display: flex; align-items: flex-start; justify-content: space-between; gap: 12px; }
.entity-title { font-weight: 700; overflow-wrap: anywhere; }
.entity-title small { color: var(--muted); font-weight: 500; }
.entity-sub { margin-top: 5px; color: var(--muted); font-size: 12px; line-height: 1.65; overflow-wrap: anywhere; }
.entity-actions { display: flex; flex-wrap: wrap; gap: 7px; margin-top: 12px; }
.entity-actions button { padding: 6px 9px; font-size: 12px; }

.permission-dots { display: flex; gap: 3px; flex-shrink: 0; }
.permission-dots i { width: 6px; height: 6px; border-radius: 50%; background: var(--border-strong); }
.permission-dots i.on { background: var(--primary); }

.status-panel { min-height: 220px; }
.metric-grid { padding: 14px; display: grid; grid-template-columns: 1fr 1fr; gap: 9px; }
.metric { padding: 11px; border-radius: 9px; background: var(--surface-alt); }
.metric span { display: block; color: var(--muted); font-size: 11px; }
.metric b { display: block; margin-top: 4px; }
.model-strip { margin: 0 14px 14px; padding: 10px 11px; display: flex; justify-content: space-between; gap: 12px; border-radius: 8px; background: var(--primary-soft); font-size: 12px; }
.model-strip span { color: var(--muted); }
.model-strip b { color: var(--primary); overflow-wrap: anywhere; }

.badge {
  display: inline-flex;
  align-items: center;
  min-height: 24px;
  padding: 3px 8px;
  border-radius: 999px;
  background: var(--primary-soft);
  color: var(--primary);
  font-size: 11px;
  font-weight: 700;
  white-space: nowrap;
}

.badge.good { color: var(--success); background: var(--success-soft); }

.console-panel { display: flex; flex-direction: column; min-width: 0; }
.console-title { flex: none; }
.console-tools { display: flex; align-items: center; flex-wrap: wrap; gap: 8px; }
.console-tools select { width: auto; min-width: 130px; }

.log-stream {
  flex: 1;
  min-height: 0;
  padding: 10px 0;
  overflow: auto;
  background: var(--surface-alt);
  font: 12px/1.55 "Cascadia Mono", Consolas, monospace;
}

.log-line { display: grid; grid-template-columns: 80px 88px minmax(0, 1fr); gap: 8px; padding: 5px 14px; }
.log-line:hover { background: var(--surface-hover); }
.log-time { color: var(--subtle); }
.log-cat { color: var(--primary); font-weight: 700; }
.log-cat.Error { color: var(--danger); }
.log-cat.Command { color: var(--warning); }
.log-msg { color: var(--text); white-space: pre-wrap; overflow-wrap: anywhere; }

.empty-card, .empty-state {
  min-height: 100px;
  display: grid;
  place-items: center;
  padding: 20px;
  color: var(--muted);
  text-align: center;
}

.switch, .big-toggle { display: inline-flex; align-items: center; gap: 9px; cursor: pointer; }
.switch input, .big-toggle input, .permission-check input { width: auto; accent-color: var(--primary); }
.switch > span, .big-toggle > span { width: 36px; height: 20px; border-radius: 999px; background: var(--border-strong); position: relative; transition: background .16s ease; }
.switch > span::after, .big-toggle > span::after { content: ""; width: 14px; height: 14px; position: absolute; top: 3px; left: 3px; border-radius: 50%; background: #fff; transition: transform .16s ease; box-shadow: 0 1px 3px rgba(0, 0, 0, .24); }
.switch input, .big-toggle input { position: absolute; opacity: 0; pointer-events: none; }
.switch input:checked + span, .big-toggle input:checked + span { background: var(--primary); }
.switch input:checked + span::after, .big-toggle input:checked + span::after { transform: translateX(16px); }

dialog {
  width: min(560px, calc(100vw - 32px));
  padding: 0;
  border: 1px solid var(--border);
  border-radius: 14px;
  background: var(--surface);
  color: var(--text);
  box-shadow: 0 24px 70px rgba(15, 23, 42, .2);
}

dialog::backdrop { background: rgba(15, 23, 42, .45); }
.modal-head { display: flex; justify-content: space-between; align-items: center; padding: 18px 20px; border-bottom: 1px solid var(--border); }
.modal-head h3 { margin: 0; font-size: 18px; }
.modal-body { padding: 20px; max-height: 62vh; overflow: auto; }
.modal-actions { display: flex; justify-content: flex-end; gap: 9px; padding: 14px 20px; border-top: 1px solid var(--border); }
#modalError { margin: 0 20px; }

.toast-area { position: fixed; right: 22px; bottom: 22px; z-index: 100; display: grid; gap: 8px; }
.toast { min-width: 240px; padding: 11px 14px; border: 1px solid var(--border); border-radius: 9px; background: var(--surface); box-shadow: var(--shadow); }
.toast.error { border-color: color-mix(in srgb, var(--danger) 40%, var(--border)); color: var(--danger); }

@media (max-width: 1240px) {
  .dashboard { grid-template-columns: minmax(230px, 280px) minmax(420px, 1fr); }
  .right-stack { grid-column: 1 / -1; grid-template-columns: 1fr 1fr; grid-template-rows: minmax(240px, auto); }
}

@media (max-width: 900px) {
  .topbar { grid-template-columns: 1fr auto; }
  .top-stats { display: none; }
  .workspace { grid-template-columns: 82px minmax(0, 1fr); }
  .sidebar { padding-inline: 10px; }
  .sidebar > p { display: none; }
  .nav-item { justify-content: center; padding: 11px 8px; font-size: 0; }
  .nav-item span { font-size: 16px; }
  .dashboard { grid-template-columns: 1fr; }
  .right-stack { grid-column: auto; grid-template-columns: 1fr; }
  .left-stack { grid-template-rows: auto; }
}

@media (max-width: 620px) {
  .topbar { height: auto; min-height: 62px; padding: 10px 14px; }
  .brand small, .account > div { display: none; }
  .workspace { display: block; }
  .sidebar { position: sticky; top: 62px; z-index: 15; width: 100%; height: auto; display: flex; overflow-x: auto; border-right: 0; border-bottom: 1px solid var(--border); padding: 7px 10px; }
  .nav-item { width: auto; min-width: 42px; margin: 0 2px; }
  .content-area { padding: 18px 14px 24px; }
  .page-title { align-items: flex-start; flex-direction: column; }
  .dashboard { min-height: auto; }
  .log-line { grid-template-columns: 70px 72px minmax(0, 1fr); padding-inline: 10px; }
  .login-card { padding: 26px; }
}
