:root {
  --bg: #f4f6f9;
  --surface: #ffffff;
  --text: #0f172a;
  --muted: #64748b;
  --border: #e2e8f0;
  --primary: #2563eb;
  --primary-hover: #1d4ed8;
  --primary-soft: #eff6ff;
  --danger: #dc2626;
  --danger-soft: #fef2f2;
  --ok: #059669;
  --ok-soft: #ecfdf5;
  --warn: #d97706;
  --low: #0284c7;
  --high: #e11d48;
  --radius: 12px;
  --shadow: 0 1px 2px rgba(15, 23, 42, 0.04), 0 4px 16px rgba(15, 23, 42, 0.04);
  --font: "Inter", system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}

*, *::before, *::after { box-sizing: border-box; }

html { font-size: 15px; }

body {
  margin: 0;
  font-family: var(--font);
  background: var(--bg);
  color: var(--text);
  line-height: 1.5;
  min-height: 100vh;
  -webkit-font-smoothing: antialiased;
}

a { color: var(--primary); text-decoration: none; }
a:hover { text-decoration: underline; }

/* Header */
.top {
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  padding: 0.85rem 1.5rem;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  position: sticky;
  top: 0;
  z-index: 50;
}
.top h1 {
  margin: 0;
  font-size: 1.1rem;
  font-weight: 650;
  letter-spacing: -0.02em;
}
.top .muted { margin: 0.1rem 0 0; font-size: 0.85rem; color: var(--muted); }
.top-actions { display: flex; gap: 0.35rem; align-items: center; }
.top-actions a {
  color: var(--muted);
  font-size: 0.9rem;
  font-weight: 500;
  padding: 0.4rem 0.75rem;
  border-radius: 8px;
  text-decoration: none;
}
.top-actions a:hover { background: var(--bg); color: var(--text); text-decoration: none; }

main {
  max-width: 1080px;
  margin: 0 auto;
  padding: 1.5rem 1.25rem 3rem;
}

/* Cards */
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1.25rem;
  box-shadow: var(--shadow);
}
.card h2 {
  margin: 0 0 1rem;
  font-size: 0.95rem;
  font-weight: 650;
  color: var(--text);
}

.muted { color: var(--muted); font-size: 0.9rem; }

/* Stats */
.stats {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 0.85rem;
  margin-bottom: 1.25rem;
}
@media (max-width: 900px) {
  .stats { grid-template-columns: repeat(2, 1fr); }
}
@media (max-width: 520px) {
  .stats { grid-template-columns: 1fr; }
}
.stat {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1rem 1.15rem;
  box-shadow: var(--shadow);
}
.stat .lbl {
  font-size: 0.75rem;
  font-weight: 600;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.04em;
}
.stat .val {
  margin-top: 0.35rem;
  font-size: 1.5rem;
  font-weight: 700;
  letter-spacing: -0.03em;
  font-variant-numeric: tabular-nums;
}
.stat .val small {
  font-size: 0.8rem;
  font-weight: 500;
  color: var(--muted);
}
.stat.highlight {
  border-color: #bfdbfe;
  background: var(--primary-soft);
}

.grid-2 {
  display: grid;
  grid-template-columns: 1fr 1.2fr;
  gap: 1rem;
}
@media (max-width: 800px) {
  .grid-2 { grid-template-columns: 1fr; }
}

/* Forms */
label {
  display: block;
  font-size: 0.8rem;
  font-weight: 600;
  color: var(--muted);
  margin: 0.65rem 0 0.3rem;
}
label:first-child { margin-top: 0; }

input, select, textarea {
  width: 100%;
  padding: 0.55rem 0.75rem;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: #fff;
  color: var(--text);
  font: inherit;
  transition: border-color 0.15s, box-shadow 0.15s;
}
input:focus, select:focus, textarea:focus {
  outline: none;
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.15);
}

button, .btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.4rem;
  padding: 0.55rem 1rem;
  border: none;
  border-radius: 8px;
  background: var(--primary);
  color: #fff;
  font: inherit;
  font-weight: 600;
  font-size: 0.9rem;
  cursor: pointer;
  text-decoration: none;
  transition: background 0.15s;
}
button:hover, .btn:hover { background: var(--primary-hover); text-decoration: none; color: #fff; }
button.secondary, .btn.secondary {
  background: #fff;
  color: var(--text);
  border: 1px solid var(--border);
}
button.secondary:hover, .btn.secondary:hover { background: var(--bg); color: var(--text); }

.form button { width: 100%; margin-top: 1rem; }

.row { display: flex; gap: 0.5rem; }
.row.between { justify-content: space-between; align-items: center; flex-wrap: wrap; gap: 0.75rem; }
.row > * { flex: 1; min-width: 0; }

/* Table */
.table-wrap { overflow-x: auto; }
table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.9rem;
  font-variant-numeric: tabular-nums;
}
th, td {
  text-align: left;
  padding: 0.7rem 0.5rem;
  border-bottom: 1px solid var(--border);
  vertical-align: middle;
}
th {
  font-size: 0.72rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--muted);
}
tbody tr:hover { background: #f8fafc; }

.bg-chip {
  display: inline-flex;
  align-items: center;
  padding: 0.2rem 0.55rem;
  border-radius: 6px;
  font-weight: 650;
  font-size: 0.88rem;
}
.bg-chip.low  { background: #e0f2fe; color: var(--low); }
.bg-chip.ok   { background: var(--ok-soft); color: var(--ok); }
.bg-chip.high { background: #ffe4e6; color: var(--high); }

.ctx-pill {
  display: inline-block;
  padding: 0.15rem 0.5rem;
  border-radius: 6px;
  font-size: 0.78rem;
  background: #f1f5f9;
  color: var(--muted);
  text-transform: capitalize;
}

a.danger {
  color: var(--danger);
  font-weight: 600;
  font-size: 0.85rem;
  text-decoration: none;
  padding: 0.25rem 0.45rem;
  border-radius: 6px;
}
a.danger:hover { background: var(--danger-soft); text-decoration: none; }

.inline { margin: 0; }
.inline select { width: auto; min-width: 6.5rem; }

code.block, pre.code {
  display: block;
  background: #f8fafc;
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 0.75rem 0.9rem;
  overflow-x: auto;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 0.8rem;
  margin: 0.4rem 0 0.85rem;
  color: #334155;
}

/* Auth */
body.auth {
  display: grid;
  place-items: center;
  padding: 1.5rem;
  background: linear-gradient(180deg, #eff6ff 0%, var(--bg) 45%);
}
.auth-card {
  width: min(400px, 100%);
  padding: 1.75rem;
}
.auth-card h1 {
  margin: 0 0 0.25rem;
  font-size: 1.35rem;
  letter-spacing: -0.02em;
}
.auth-card .muted { margin: 0 0 1.25rem; }
.auth-card button { width: 100%; margin-top: 1.1rem; }
.alert {
  background: var(--danger-soft);
  color: #991b1b;
  border: 1px solid #fecaca;
  border-radius: 8px;
  padding: 0.6rem 0.75rem;
  margin-bottom: 1rem;
  font-size: 0.9rem;
}

.footer-note {
  text-align: center;
  color: var(--muted);
  font-size: 0.8rem;
  margin-top: 2rem;
}

/* Toast */
.toast-stack {
  position: fixed;
  top: 1rem;
  right: 1rem;
  z-index: 9999;
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
  max-width: min(360px, calc(100vw - 2rem));
  pointer-events: none;
}
.toast-item {
  pointer-events: auto;
  background: #0f172a;
  color: #f8fafc;
  padding: 0.75rem 1rem;
  border-radius: 10px;
  box-shadow: 0 10px 30px rgba(15, 23, 42, 0.2);
  font-size: 0.9rem;
  font-weight: 500;
  display: flex;
  align-items: flex-start;
  gap: 0.65rem;
  animation: toast-in 0.25s ease;
}
.toast-item.success { background: #065f46; }
.toast-item.error { background: #9f1239; }
.toast-item.info { background: #1e3a5f; }
.toast-item .toast-x {
  margin-left: auto;
  background: transparent;
  border: 0;
  color: inherit;
  opacity: 0.7;
  cursor: pointer;
  padding: 0;
  font-size: 1.1rem;
  line-height: 1;
  width: auto;
}
.toast-item .toast-x:hover { opacity: 1; background: transparent; }
@keyframes toast-in {
  from { opacity: 0; transform: translateY(-8px); }
  to { opacity: 1; transform: translateY(0); }
}

#chart { max-height: 220px; }
