:root,
:root[data-theme="dark"] {
  --bg:           #0b1220;
  --bg-elev:     #111a2e;
  --panel:       #14203a;
  --panel-2:     #1c2c4a;
  --border:      #233557;
  --border-soft: #1a2848;
  --text:        #e7eaf3;
  --text-soft:   #aab4cf;
  --muted:       #6f7da0;
  --accent:      #6ea8ff;
  --accent-soft: #6ea8ff22;
  --good:        #34d399;
  --warn:        #f59e0b;
  --bad:         #f87171;
  --shadow:      0 4px 20px rgba(0, 0, 0, 0.4);
  color-scheme:  dark;
}

:root[data-theme="light"] {
  --bg:           #f3f5fb;
  --bg-elev:     #ffffff;
  --panel:       #ffffff;
  --panel-2:     #f6f8fd;
  --border:      #dfe5f0;
  --border-soft: #ecf0f7;
  --text:        #0f172a;
  --text-soft:   #334155;
  --muted:       #6b7593;
  --accent:      #2563eb;
  --accent-soft: #2563eb15;
  --good:        #16a34a;
  --warn:        #d97706;
  --bad:         #dc2626;
  --shadow:      0 1px 3px rgba(15, 23, 42, 0.08), 0 4px 12px rgba(15, 23, 42, 0.05);
  color-scheme:  light;
}

* { box-sizing: border-box; }
html, body {
  margin: 0; background: var(--bg); color: var(--text);
  font: 14px/1.55 -apple-system, BlinkMacSystemFont, "Segoe UI", "Inter", Roboto, sans-serif;
  transition: background-color .2s ease, color .2s ease;
}
a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }
.muted { color: var(--muted); }
.mono { font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; }

.topbar {
  display: flex; align-items: center; gap: 16px;
  padding: 12px 24px;
  background: var(--bg-elev);
  border-bottom: 1px solid var(--border);
  position: sticky; top: 0; z-index: 10;
  box-shadow: var(--shadow);
}
.topbar .brand { font-weight: 700; font-size: 16px; flex-shrink: 0; }
.topbar .brand a { color: var(--text); }
.topbar nav { margin-left: auto; display: flex; gap: 14px; align-items: center; }

.topbar-controls { display: flex; align-items: center; gap: 12px; }
.window-control { display: flex; align-items: center; gap: 8px; font-size: 13px; }
.window-control select {
  background: var(--panel-2); color: var(--text);
  border: 1px solid var(--border); border-radius: 6px;
  padding: 5px 10px; font: inherit; font-size: 13px; cursor: pointer;
}
.window-control select:focus { outline: none; border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }

.status-pill {
  padding: 4px 10px; border-radius: 999px; font-size: 12px; font-weight: 600;
  background: var(--panel-2); color: var(--muted);
  display: inline-flex; align-items: center; gap: 6px;
  border: 1px solid var(--border-soft);
}
.status-pill::before { content: ''; width: 8px; height: 8px; border-radius: 50%; background: var(--muted); }
.status-pill.live { background: color-mix(in srgb, var(--good) 12%, transparent); color: var(--good); border-color: color-mix(in srgb, var(--good) 30%, transparent); }
.status-pill.live::before { background: var(--good); box-shadow: 0 0 8px var(--good); animation: pulse 2s infinite; }
.status-pill.connecting { color: var(--warn); }
.status-pill.connecting::before { background: var(--warn); }
.status-pill.down { background: color-mix(in srgb, var(--bad) 12%, transparent); color: var(--bad); }
.status-pill.down::before { background: var(--bad); }
@keyframes pulse { 0%, 100% { opacity: 1; } 50% { opacity: 0.5; } }

.theme-toggle {
  background: transparent; border: 1px solid var(--border);
  color: var(--text-soft); width: 34px; height: 34px;
  border-radius: 8px; display: inline-flex; align-items: center; justify-content: center;
  cursor: pointer; padding: 0;
}
.theme-toggle:hover { background: var(--panel-2); color: var(--text); }
.theme-toggle svg { width: 18px; height: 18px; }
:root[data-theme="dark"] .theme-toggle .icon-sun { display: block; }
:root[data-theme="dark"] .theme-toggle .icon-moon { display: none; }
:root[data-theme="light"] .theme-toggle .icon-sun { display: none; }
:root[data-theme="light"] .theme-toggle .icon-moon { display: block; }

main { padding: 24px; max-width: 1700px; margin: 0 auto; }
.container { padding: 24px; max-width: 1100px; margin: 0 auto; }

.vpn-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(580px, 1fr)); gap: 20px; margin-bottom: 24px; }
.vpn-card {
  background: var(--panel); border: 1px solid var(--border); border-radius: 14px;
  padding: 20px; border-left: 4px solid var(--state-color, var(--muted));
  box-shadow: var(--shadow); transition: border-color .2s ease;
  min-width: 0;  /* important: allow grid items to shrink below content size */
}
.vpn-card header { display: flex; justify-content: space-between; align-items: flex-start; margin-bottom: 12px; gap: 12px; }
.card-title { display: flex; align-items: flex-start; gap: 12px; min-width: 0; flex: 1; }
.card-title .dot { margin-top: 6px; flex-shrink: 0; }
.card-title .title-text { display: flex; flex-direction: column; gap: 2px; min-width: 0; }
.card-title h2 { margin: 0; font-size: 17px; font-weight: 600; line-height: 1.3; word-break: break-word; }
.card-title .conn-id { font-size: 11px; }
.dot { width: 10px; height: 10px; border-radius: 50%; box-shadow: 0 0 10px currentColor; }
.state-badge {
  padding: 5px 12px; border-radius: 6px; font-size: 11px; font-weight: 700;
  letter-spacing: 0.6px; color: white; text-transform: uppercase;
  flex-shrink: 0;
}

.conn-description {
  font-size: 13px; line-height: 1.5; color: var(--text-soft);
  background: var(--panel-2); border-radius: 8px;
  padding: 10px 14px; margin-bottom: 16px;
  border: 1px solid var(--border-soft);
  display: none;
}

/* ── SLA panel — fixed layout ── */
.sla-panel {
  background: var(--bg);
  border: 1px solid var(--border-soft);
  border-radius: 10px;
  padding: 16px;
  margin-bottom: 18px;
}
.sla-main {
  display: flex; align-items: baseline; gap: 10px;
  margin-bottom: 10px; flex-wrap: wrap;
}
.sla-pct {
  font-size: 28px; font-weight: 700; font-feature-settings: "tnum";
  color: var(--good); letter-spacing: -0.5px; line-height: 1;
}
.sla-label { font-size: 12px; }
.sla-bar {
  height: 6px; background: var(--panel-2); border-radius: 999px;
  overflow: hidden; margin-bottom: 14px;
}
.sla-bar-fill {
  height: 100%; background: var(--good);
  border-radius: 999px;
  transition: width .4s ease, background-color .3s ease;
  width: 0%;
}
/* Three-column SLA stats — proper grid with internal dividers */
.sla-stats {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
  gap: 0;
}
.sla-stats > div {
  display: flex; flex-direction: column; gap: 4px;
  padding: 0 14px;
  min-width: 0;  /* allow content to wrap */
}
.sla-stats > div:first-child { padding-left: 0; }
.sla-stats > div:not(:first-child) { border-left: 1px solid var(--border-soft); }
.sla-stats > div:last-child { padding-right: 0; }
.sla-stats label {
  font-size: 10px; color: var(--muted); text-transform: uppercase;
  letter-spacing: 0.6px; font-weight: 600;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.sla-stats span {
  font-size: 16px; font-weight: 600; color: var(--text);
  font-feature-settings: "tnum";
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
  line-height: 1.3;
}

.grid-stats { display: grid; grid-template-columns: 1fr 1fr; gap: 14px 24px; margin-bottom: 18px; }
.grid-stats > div { display: flex; flex-direction: column; gap: 3px; min-width: 0; }
.grid-stats label { font-size: 10px; color: var(--muted); text-transform: uppercase; letter-spacing: 0.6px; font-weight: 600; }
.grid-stats span { font-size: 14px; color: var(--text); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.grid-stats .ike-age { font-size: 12px; }

.charts { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; margin-bottom: 12px; }
.chart-wrap { height: 150px; background: var(--bg); border: 1px solid var(--border-soft); border-radius: 10px; padding: 8px; min-width: 0; }

.state-history { margin-top: 8px; border-top: 1px solid var(--border-soft); padding-top: 14px; }
.state-history summary { cursor: pointer; color: var(--muted); font-size: 13px; user-select: none; }
.state-history summary:hover { color: var(--text-soft); }
.history-list { margin-top: 12px; display: flex; flex-direction: column; gap: 6px; max-height: 220px; overflow: auto; }
.history-row { display: flex; align-items: center; gap: 8px; font-size: 12px; padding: 4px 0; }
.state-pill { padding: 2px 8px; border-radius: 4px; color: white; font-size: 11px; font-weight: 600; }

.panel { background: var(--panel); border: 1px solid var(--border); border-radius: 14px; padding: 20px; margin-bottom: 24px; box-shadow: var(--shadow); }
.panel h2 { margin: 0 0 16px; font-size: 16px; font-weight: 600; }

.alert-feed { display: flex; flex-direction: column; gap: 6px; max-height: 420px; overflow: auto; }
.alert-row {
  display: grid; grid-template-columns: 170px 90px minmax(160px, 240px) 1fr;
  align-items: center; gap: 12px;
  padding: 10px 14px; background: var(--bg);
  border: 1px solid var(--border-soft); border-radius: 8px;
  border-left: 3px solid var(--muted); font-size: 13px;
  min-width: 0;
}
.alert-row.is-reminder { background: color-mix(in srgb, var(--warn) 6%, var(--bg)); }
.alert-time { font-size: 12px; }
.alert-sev { padding: 3px 8px; border-radius: 4px; color: white; font-size: 11px; font-weight: 700; text-align: center; letter-spacing: 0.4px; white-space: nowrap; }
.alert-conn { color: var(--accent); font-size: 13px; font-weight: 500; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.alert-msg { overflow: hidden; text-overflow: ellipsis; }
.alert-empty { color: var(--muted); padding: 24px; text-align: center; font-size: 13px; }
.reminder-tag {
  display: inline-block; padding: 1px 6px; border-radius: 4px;
  background: var(--warn); color: white; font-size: 10px; font-weight: 700;
  letter-spacing: 0.4px; margin-left: 6px; vertical-align: middle;
}

.centered { display: flex; align-items: center; justify-content: center; min-height: 100vh; padding: 16px; }
.login-card {
  background: var(--panel); border: 1px solid var(--border); border-radius: 14px;
  padding: 36px 32px; width: 380px; max-width: 100%;
  display: flex; flex-direction: column; gap: 16px; box-shadow: var(--shadow);
}
.login-card h1 { margin: 0; font-size: 22px; font-weight: 600; }
.login-card label, .user-form label { display: flex; flex-direction: column; gap: 6px; font-size: 13px; color: var(--muted); }
.login-card input, .user-form input, .user-form textarea, .client-form input, .client-form textarea {
  padding: 10px 12px; background: var(--bg); border: 1px solid var(--border);
  border-radius: 8px; color: var(--text); font: inherit; font-size: 14px;
  transition: border-color .15s ease;
}
.login-card input:focus, .user-form input:focus, .user-form textarea:focus,
.client-form input:focus, .client-form textarea:focus {
  outline: none; border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft);
}
button { padding: 10px 16px; background: var(--accent); color: white; border: none; border-radius: 8px;
  font-size: 14px; font-weight: 600; cursor: pointer; transition: filter .15s ease, transform .05s ease; }
button:hover { filter: brightness(1.08); }
button:active { transform: translateY(1px); }
button.danger { background: var(--bad); }
button.ghost { background: transparent; border: 1px solid var(--border); color: var(--text-soft); }
button.ghost:hover { background: var(--panel-2); color: var(--text); }
.error { background: color-mix(in srgb, var(--bad) 15%, transparent); color: var(--bad); padding: 10px 12px; border-radius: 8px; font-size: 13px; border: 1px solid color-mix(in srgb, var(--bad) 30%, transparent); }

.users-table { width: 100%; border-collapse: collapse; }
.users-table th, .users-table td { padding: 10px 12px; text-align: left; border-bottom: 1px solid var(--border-soft); }
.users-table th { color: var(--muted); font-size: 11px; text-transform: uppercase; letter-spacing: 0.6px; font-weight: 600; }
.users-table form { display: inline; margin: 0; }
.user-form { display: flex; flex-direction: column; gap: 12px; max-width: 380px; }
.user-form .checkbox { flex-direction: row; align-items: center; gap: 8px; color: var(--text-soft); }

.client-form {
  display: block; padding: 16px; margin: 0 0 14px 0;
  background: var(--bg); border: 1px solid var(--border-soft);
  border-radius: 10px;
}
.client-form-header {
  display: flex; align-items: center; gap: 14px; flex-wrap: wrap;
  margin-bottom: 14px; padding-bottom: 12px; border-bottom: 1px solid var(--border-soft);
}
.client-id {
  font-family: ui-monospace, Menlo, Consolas, monospace;
  background: var(--panel-2); padding: 4px 10px; border-radius: 6px;
  font-size: 13px; color: var(--text);
}
.client-meta { font-size: 12px; }
.client-form-body { display: flex; flex-direction: column; gap: 12px; }
.client-form-body label { display: flex; flex-direction: column; gap: 6px; font-size: 12px; color: var(--muted); }
.client-form-body textarea { resize: vertical; min-height: 50px; font-family: inherit; line-height: 1.5; }
.client-form-actions { display: flex; gap: 8px; }

.peer-wrap { display: inline-flex; align-items: center; gap: 6px; }
.peer-toggle { background: transparent; border: none; padding: 0; color: var(--muted); cursor: pointer; font-size: 12px; display: inline-flex; align-items: center; }
.peer-toggle:hover { color: var(--text-soft); }
.peer-toggle svg { width: 14px; height: 14px; }

.empty-state { text-align: center; padding: 60px 20px; color: var(--muted); }
.empty-state h2 { color: var(--text); margin-bottom: 8px; }
.empty-state code { background: var(--panel-2); padding: 2px 6px; border-radius: 4px; font-size: 13px; }

@media (max-width: 768px) {
  .vpn-grid { grid-template-columns: 1fr; }
  .charts { grid-template-columns: 1fr; }
  .grid-stats { grid-template-columns: 1fr; }
  /* Stack SLA stats vertically on narrow screens with horizontal dividers */
  .sla-stats { grid-template-columns: 1fr; }
  .sla-stats > div { padding: 10px 0; border-left: none !important; border-top: 1px solid var(--border-soft); }
  .sla-stats > div:first-child { padding-top: 0; border-top: none; }
  .sla-stats > div:last-child { padding-bottom: 0; }
  .alert-row { grid-template-columns: 1fr; gap: 4px; }
  main { padding: 12px; }
  .topbar { padding: 10px 14px; gap: 8px; flex-wrap: wrap; }
  .topbar nav { gap: 8px; }
  .topbar nav a { font-size: 13px; }
  .topbar-controls { width: 100%; order: 3; }
}
