:root {
  color-scheme: light;
  --bg: #eef2f7;
  --surface: #ffffff;
  --surface-2: #f7f9fc;
  --line: #d8e0eb;
  --line-strong: #c5cfdd;
  --text: #182235;
  --muted: #66748a;
  --primary: #2563eb;
  --primary-dark: #1d4ed8;
  --ok: #0f9f6e;
  --warn: #b7791f;
  --danger: #dc2626;
  --shadow: 0 18px 42px rgba(24, 34, 53, 0.08);
}

:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #0a0f1d;
  --surface: #101827;
  --surface-2: #151f32;
  --line: #24324a;
  --line-strong: #33445f;
  --text: #edf4ff;
  --muted: #91a0b8;
  --primary: #4f8cff;
  --primary-dark: #2f6ee8;
  --ok: #34d399;
  --warn: #f5c451;
  --danger: #fb7185;
  --shadow: 0 18px 42px rgba(0, 0, 0, 0.28);
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html { scroll-behavior: smooth; }
body {
  margin: 0;
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", "Microsoft YaHei", sans-serif;
  background:
    radial-gradient(circle at top left, rgba(79, 140, 255, 0.14), transparent 32rem),
    var(--bg);
  color: var(--text);
}

button, input, textarea, select { font: inherit; }
button {
  min-height: 38px;
  border: 1px solid var(--line-strong);
  background: var(--surface);
  color: var(--text);
  border-radius: 7px;
  padding: 0 0.85rem;
  cursor: pointer;
}
button:hover { border-color: #9aa8bb; }
button.primary {
  background: var(--primary);
  border-color: var(--primary);
  color: #fff;
}
button.primary:hover { background: var(--primary-dark); }
button.ghost { background: transparent; }
button.danger { color: var(--danger); }

input, textarea, select {
  width: 100%;
  border: 1px solid var(--line);
  background: var(--surface);
  color: var(--text);
  border-radius: 7px;
  padding: 0.65rem 0.72rem;
  outline: none;
}
input:focus, textarea:focus, select:focus {
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.12);
}
textarea { resize: vertical; }

.login-view {
  min-height: 100vh;
  display: grid;
  place-items: center;
  background:
    linear-gradient(120deg, rgba(37, 99, 235, 0.12), rgba(15, 159, 110, 0.08)),
    var(--bg);
  padding: 1.5rem;
}
.login-panel {
  width: min(410px, 100%);
  display: grid;
  gap: 1rem;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 8px;
  box-shadow: var(--shadow);
  padding: 2rem;
}
.login-panel h1 { margin: 0; font-size: 1.8rem; }
.login-panel p { margin: -0.4rem 0 0.5rem; color: var(--muted); }
.login-divider {
  display: flex;
  align-items: center;
  gap: 0.7rem;
  color: var(--muted);
  font-size: 0.8rem;
}
.login-divider::before, .login-divider::after {
  content: "";
  height: 1px;
  background: var(--line);
  flex: 1;
}
.login-panel label, .form-grid label, .template-grid label {
  display: grid;
  gap: 0.42rem;
}
.login-panel span, .form-grid span, .template-grid span {
  color: var(--muted);
  font-size: 0.82rem;
}
.login-hint {
  color: var(--muted);
  font-size: 0.82rem;
  text-align: center;
}
.wide-button { width: 100%; }
.brand-mark {
  width: 48px;
  height: 48px;
  display: grid;
  place-items: center;
  border-radius: 8px;
  background: linear-gradient(135deg, #1e40af, #0f766e);
  color: #fff;
  font-weight: 800;
}
.brand-mark.small {
  width: 36px;
  height: 36px;
  border-radius: 7px;
  font-size: 0.84rem;
}

.app-shell {
  min-height: 100vh;
  display: grid;
  grid-template-columns: 248px minmax(0, 1fr);
}
.sidebar {
  position: sticky;
  top: 0;
  height: 100vh;
  display: flex;
  flex-direction: column;
  background: #08111f;
  color: #e5edf8;
  border-right: 1px solid #1f2937;
}
.logo-row {
  display: flex;
  gap: 0.75rem;
  align-items: center;
  padding: 1.25rem;
  border-bottom: 1px solid rgba(255, 255, 255, 0.08);
}
.logo-row strong, .logo-row span { display: block; }
.logo-row span {
  margin-top: 0.15rem;
  color: #9ca3af;
  font-size: 0.78rem;
}
.nav-list {
  display: grid;
  gap: 0.2rem;
  padding: 1rem 0.75rem;
}
.nav-list a {
  color: #cbd5e1;
  text-decoration: none;
  padding: 0.72rem 0.8rem;
  border-radius: 7px;
  font-size: 0.94rem;
}
.nav-list a:hover, .nav-list a.active {
  background: rgba(255, 255, 255, 0.08);
  color: #fff;
}
.sidebar-foot {
  margin-top: auto;
  padding: 1rem;
}
.sidebar-foot button {
  width: 100%;
  color: #e5edf8;
  border-color: rgba(255, 255, 255, 0.14);
}

.main {
  min-width: 0;
  padding: 1.45rem;
}
.topbar {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 1rem;
  margin-bottom: 1rem;
}
.topbar h1 {
  margin: 0.1rem 0 0;
  font-size: 1.55rem;
  letter-spacing: 0;
}
.eyebrow {
  color: var(--primary);
  font-weight: 700;
  font-size: 0.78rem;
}
.toolbar, .actions, .row-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
}

.grid { display: grid; gap: 1rem; }
.stats-grid {
  grid-template-columns: repeat(4, minmax(0, 1fr));
  margin-bottom: 1rem;
}
.stat-card, .panel, .metric {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 8px;
}
.stat-card {
  min-height: 126px;
  padding: 1.1rem;
  display: grid;
  align-content: space-between;
  border-left: 4px solid transparent;
}
.stat-card span, .stat-card small {
  color: var(--muted);
  font-size: 0.82rem;
}
.stat-card strong {
  display: block;
  margin: 0.4rem 0;
  font-size: 1.45rem;
  word-break: break-word;
}
.stat-card.warn {
  border-color: rgba(245, 196, 81, 0.45);
  background: rgba(245, 196, 81, 0.1);
}
.stat-card.warn strong { color: var(--warn); }
.tone-ok, .stat-card.tone-ok {
  border-color: color-mix(in srgb, var(--ok) 42%, var(--line));
}
.tone-ok strong, .tone-ok .value { color: var(--ok); }
.tone-warn, .stat-card.tone-warn {
  border-color: color-mix(in srgb, var(--warn) 48%, var(--line));
}
.tone-warn strong, .tone-warn .value { color: var(--warn); }
.tone-bad, .stat-card.tone-bad {
  border-color: color-mix(in srgb, var(--danger) 48%, var(--line));
}
.tone-bad strong, .tone-bad .value { color: var(--danger); }

.panel {
  margin-bottom: 1rem;
  padding: 1rem;
  box-shadow: var(--shadow);
}
.compact-panel { max-width: 780px; }
.panel-head {
  display: flex;
  justify-content: space-between;
  gap: 1rem;
  align-items: flex-start;
  margin-bottom: 1rem;
}
.panel-head h2 {
  margin: 0;
  font-size: 1.08rem;
}
.panel-head p {
  margin: 0.35rem 0 0;
  color: var(--muted);
  font-size: 0.9rem;
}
.badge {
  display: inline-flex;
  align-items: center;
  min-height: 28px;
  padding: 0 0.65rem;
  border-radius: 999px;
  color: var(--ok);
  background: color-mix(in srgb, var(--ok) 14%, transparent);
  border: 1px solid color-mix(in srgb, var(--ok) 34%, transparent);
  white-space: nowrap;
  font-size: 0.8rem;
}
.badge.soft {
  color: var(--primary);
  background: color-mix(in srgb, var(--primary) 14%, transparent);
  border-color: color-mix(in srgb, var(--primary) 34%, transparent);
}

.form-grid {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 0.8rem;
}
.form-grid.dense {
  grid-template-columns: repeat(6, minmax(0, 1fr));
}
.form-grid .wide { grid-column: 1 / -1; }

.table-wrap { overflow: auto; }
table {
  width: 100%;
  border-collapse: collapse;
  min-width: 760px;
}
th, td {
  padding: 0.78rem 0.7rem;
  border-top: 1px solid var(--line);
  text-align: left;
  vertical-align: middle;
}
th {
  color: var(--muted);
  background: var(--surface-2);
  font-size: 0.78rem;
  font-weight: 700;
}
td small {
  display: block;
  color: var(--muted);
  margin-top: 0.18rem;
}
.status {
  font-weight: 700;
  font-size: 0.84rem;
}
.status.ok { color: var(--ok); }
.status.bad { color: var(--danger); }
.status.unknown { color: var(--warn); }
.empty {
  padding: 1rem;
  color: var(--muted);
  border: 1px dashed var(--line-strong);
  border-radius: 8px;
  background: var(--surface-2);
}

.monitor-grid {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 0.8rem;
}
.metric {
  padding: 0.9rem;
  background: var(--surface-2);
  border-left: 3px solid transparent;
}
.metric .label {
  color: var(--muted);
  font-size: 0.78rem;
}
.metric .value {
  margin-top: 0.35rem;
  font-size: 1.05rem;
  font-weight: 700;
  word-break: break-word;
}
.chart-panel {
  grid-column: 1 / -1;
  display: grid;
  grid-template-columns: 180px minmax(0, 1fr);
  gap: 1.2rem;
  align-items: center;
  padding: 1rem;
  background: var(--surface-2);
  border: 1px solid var(--line);
  border-left: 4px solid var(--ok);
  border-radius: 8px;
}
.chart-title {
  color: var(--muted);
  font-size: 0.82rem;
  margin-bottom: 0.8rem;
}
.donut {
  --value: 0;
  --tone: var(--ok);
  width: 136px;
  height: 136px;
  border-radius: 50%;
  display: grid;
  place-items: center;
  background:
    radial-gradient(circle closest-side, var(--surface-2) 70%, transparent 72%),
    conic-gradient(var(--tone) calc(var(--value) * 1%), color-mix(in srgb, var(--line) 78%, transparent) 0);
}
.tone-warn .donut { --tone: var(--warn); }
.tone-bad .donut { --tone: var(--danger); }
.donut span {
  font-size: 1.5rem;
  font-weight: 800;
  color: var(--text);
}
.chart-bars {
  display: grid;
  gap: 0.72rem;
}
.bar-row {
  display: grid;
  grid-template-columns: 56px minmax(0, 1fr) 104px;
  gap: 0.75rem;
  align-items: center;
}
.bar-row span {
  color: var(--muted);
  font-size: 0.82rem;
}
.bar-row strong {
  text-align: right;
  font-size: 0.92rem;
}
.bar {
  height: 10px;
  overflow: hidden;
  border-radius: 999px;
  background: color-mix(in srgb, var(--line) 78%, transparent);
}
.bar i {
  display: block;
  height: 100%;
  width: 0;
  border-radius: inherit;
  background: var(--ok);
}
.tone-warn .bar i { background: var(--warn); }
.tone-bad .bar i { background: var(--danger); }
.bar i.remain { background: var(--primary); }
.bar i.upload { background: #38bdf8; }
.bar i.download { background: #a78bfa; }

.template-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 0.8rem;
}
textarea[readonly] {
  min-height: 28rem;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 0.84rem;
  background: #0f172a;
  color: #dbeafe;
}

:root[data-theme="light"] textarea[readonly] {
  background: #0f172a;
  color: #dbeafe;
}

.toast {
  position: fixed;
  right: 1rem;
  bottom: 1rem;
  max-width: min(360px, calc(100vw - 2rem));
  padding: 0.75rem 0.9rem;
  border-radius: 8px;
  background: #111827;
  color: #fff;
  box-shadow: var(--shadow);
  z-index: 20;
}
.toast.error { background: var(--danger); }

@media (max-width: 1180px) {
  .stats-grid, .monitor-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .form-grid, .form-grid.dense, .template-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
}

@media (max-width: 820px) {
  .app-shell { grid-template-columns: 1fr; }
  .sidebar {
    position: static;
    height: auto;
  }
  .nav-list {
    grid-template-columns: repeat(3, minmax(0, 1fr));
  }
  .topbar {
    flex-direction: column;
  }
  .stats-grid, .monitor-grid, .form-grid, .form-grid.dense, .template-grid {
    grid-template-columns: 1fr;
  }
  .chart-panel {
    grid-template-columns: 1fr;
  }
  .bar-row {
    grid-template-columns: 48px minmax(0, 1fr);
  }
  .bar-row strong {
    grid-column: 2;
    text-align: left;
  }
}
