:root {
  --bg: #0f1420;
  --panel: #171d2b;
  --panel-2: #1f2838;
  --line: #2a3345;
  --text: #e7ecf5;
  --muted: #93a0b5;
  --primary: #3b82f6;
  --primary-hover: #2f6fe0;
  --green: #22c55e;
  --amber: #f59e0b;
  --red: #ef4444;
  --grey: #6b7280;
  --radius: 10px;
}

* { box-sizing: border-box; }

/* Ensure the `hidden` attribute always wins over display rules below. */
[hidden] { display: none !important; }

body {
  margin: 0;
  font-family: "Segoe UI", system-ui, -apple-system, sans-serif;
  background: var(--bg);
  color: var(--text);
  font-size: 14px;
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 24px;
  background: var(--panel);
  border-bottom: 1px solid var(--line);
  position: sticky;
  top: 0;
  z-index: 5;
}

.brand { display: flex; align-items: center; gap: 12px; }
.logo {
  font-size: 22px;
  width: 42px; height: 42px;
  display: grid; place-items: center;
  background: var(--panel-2);
  border-radius: var(--radius);
  color: var(--primary);
}
.brand h1 { margin: 0; font-size: 18px; }
.brand .sub { margin: 0; color: var(--muted); font-size: 12px; }

.topbar-actions { display: flex; gap: 10px; }

main { padding: 24px; max-width: 1200px; margin: 0 auto; }

.card {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  overflow: hidden;
}

table { width: 100%; border-collapse: collapse; }
thead th {
  text-align: left;
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: .04em;
  color: var(--muted);
  padding: 12px 16px;
  border-bottom: 1px solid var(--line);
  background: var(--panel-2);
}
tbody td {
  padding: 12px 16px;
  border-bottom: 1px solid var(--line);
  vertical-align: middle;
}
tbody tr:last-child td { border-bottom: none; }
tbody tr:hover { background: rgba(255,255,255,0.02); }
td.empty { text-align: center; color: var(--muted); padding: 32px; }
.mono { font-family: "Cascadia Code", Consolas, monospace; }
.actions-col { text-align: right; }
.row-actions { display: flex; gap: 6px; justify-content: flex-end; }

.badge {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 3px 10px;
  border-radius: 999px;
  font-size: 12px;
  font-weight: 600;
}
.badge::before { content: ""; width: 8px; height: 8px; border-radius: 50%; background: currentColor; }
.badge.connected { color: var(--green); background: rgba(34,197,94,.12); }
.badge.connecting { color: var(--amber); background: rgba(245,158,11,.12); }
.badge.error { color: var(--red); background: rgba(239,68,68,.12); }
.badge.disabled, .badge.stopped { color: var(--grey); background: rgba(107,114,128,.15); }
.badge.unsupported { color: var(--primary); background: rgba(59,130,246,.12); }

.btn {
  border: 1px solid transparent;
  border-radius: 8px;
  padding: 8px 14px;
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
  color: var(--text);
  background: var(--panel-2);
}
.btn:hover { filter: brightness(1.1); }
.btn.primary { background: var(--primary); color: #fff; }
.btn.primary:hover { background: var(--primary-hover); }
.btn.ghost { background: transparent; border-color: var(--line); color: var(--muted); }
.btn.ghost:hover { color: var(--text); }
.btn.small { padding: 5px 10px; font-size: 12px; }
.btn.danger { color: var(--red); border-color: rgba(239,68,68,.4); background: transparent; }
.btn.danger:hover { background: rgba(239,68,68,.12); }
/* Links styled as buttons should match native button text rendering. */
a.btn { display: inline-block; font-family: Arial, sans-serif; text-decoration: none; }

.icon-btn {
  background: transparent; border: none; color: var(--muted);
  font-size: 22px; cursor: pointer; line-height: 1;
}
.icon-btn:hover { color: var(--text); }

.banner {
  padding: 12px 16px;
  border-radius: var(--radius);
  margin-bottom: 16px;
  font-weight: 500;
}
.banner.warn { background: rgba(245,158,11,.14); color: var(--amber); border: 1px solid rgba(245,158,11,.3); }
.banner.ok { background: rgba(34,197,94,.14); color: var(--green); border: 1px solid rgba(34,197,94,.3); }
.banner.error { background: rgba(239,68,68,.14); color: var(--red); border: 1px solid rgba(239,68,68,.3); }

/* Modal */
.modal-backdrop {
  position: fixed; inset: 0;
  background: rgba(0,0,0,.55);
  display: grid; place-items: center;
  z-index: 20; padding: 20px;
}
.modal {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 14px;
  width: 100%; max-width: 720px;
  max-height: 90vh; display: flex; flex-direction: column;
}
.wide-modal { max-width: 1000px; }
.modal-head {
  display: flex; align-items: center; justify-content: space-between;
  padding: 18px 22px; border-bottom: 1px solid var(--line);
}
.modal-head h1, .modal-head h2 { margin: 0; font-size: 17px; }
.modal-body { padding: 22px; overflow-y: auto; }

.grid { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; }
.field { display: flex; flex-direction: column; gap: 6px; }
.field > span { font-size: 12px; color: var(--muted); }
.field > span em { font-style: normal; opacity: .7; }
.field input, .field select, .field textarea {
  background: var(--panel-2);
  border: 1px solid var(--line);
  border-radius: 8px;
  color: var(--text);
  padding: 9px 11px;
  font-size: 14px;
}
.field input:focus, .field select:focus, .field textarea:focus { outline: none; border-color: var(--primary); }
.field textarea { resize: vertical; font: inherit; }
.field.checkbox { flex-direction: row; align-items: center; gap: 8px; }
.field.checkbox input { width: 16px; height: 16px; }
.field.checkbox span { color: var(--text); font-size: 14px; }

.creds { border: 1px solid var(--line); border-radius: 10px; padding: 16px; margin-top: 18px; }
.creds legend { padding: 0 8px; color: var(--muted); font-size: 12px; text-transform: uppercase; letter-spacing: .04em; }

.modal-foot { display: flex; align-items: center; gap: 10px; margin-top: 22px; }
.modal-foot .spacer { flex: 1; }

.form-error {
  background: rgba(239,68,68,.14); color: var(--red);
  border: 1px solid rgba(239,68,68,.3);
  border-radius: 8px; padding: 10px 12px; margin-bottom: 16px;
}

.tabbar { display: flex; gap: 8px; margin-bottom: 16px; }
.rule-tab.active { color: var(--text); border-color: var(--primary); }
.rules-layout { display: grid; grid-template-columns: 1.5fr 1fr; gap: 24px; }
.rules-layout h3, .firewall-section h3 { margin: 0 0 12px; font-size: 14px; }
.number-rule {
  display: grid; grid-template-columns: 24px minmax(140px, 1fr) minmax(140px, 1fr) auto auto auto;
  align-items: end; gap: 8px; margin-bottom: 10px;
}
.number-rule .field { min-width: 0; }
.rule-order { padding-bottom: 10px; color: var(--muted); text-align: center; }
.test-output {
  min-height: 64px; max-height: 180px; overflow: auto; padding: 12px;
  background: #0b0e16; border: 1px solid var(--line); border-radius: 8px;
  white-space: pre-wrap; word-break: break-word;
}
.firewall-section { margin-bottom: 22px; }
.firewall-section .checkbox { margin: 10px 0; }
.table-scroll { overflow-x: auto; }
.manual-rule { display: grid; grid-template-columns: 110px 100px 100px minmax(160px, 1fr) auto; align-items: end; gap: 10px; margin-bottom: 10px; }
.manual-rule .field { flex-direction: row; align-items: center; }
.manual-rule .field:not(.checkbox) { flex-direction: column; align-items: stretch; }
.firewall-add { align-items: end; margin-top: 14px; }
.muted { color: var(--muted); font-size: 12px; }

.creds-actions { display: flex; align-items: center; gap: 12px; margin-top: 14px; }
.sr-only {
  position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px;
  overflow: hidden; clip-path: inset(50%); white-space: nowrap; border: 0;
}

/* Native <dialog> panels (opened with showModal) share the .modal look. */
.dialog-panel {
  padding: 0; color: var(--text);
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 14px;
  width: calc(100% - 40px); max-width: 760px; max-height: 90vh;
}
.dialog-panel[open] { display: flex; flex-direction: column; }
.dialog-panel:modal { overscroll-behavior: contain; }
.dialog-panel::backdrop {
  background: rgba(0,0,0,.55);
  overflow: hidden; overscroll-behavior: contain;
}
.agents-toolbar { display: flex; flex-wrap: wrap; align-items: center; gap: 10px; margin-bottom: 14px; }
.agents-table td { padding: 6px 8px; }
.agents-table th { padding: 10px 8px; }
.agents-table td:last-child { width: 1%; white-space: nowrap; text-align: right; }
.agents-table input {
  width: 100%; box-sizing: border-box;
  background: var(--panel-2); border: 1px solid var(--line); border-radius: 8px;
  color: var(--text); padding: 8px 10px; font-size: 14px;
}
.agents-table input:focus { outline: none; border-color: var(--primary); }
.agents-table input[aria-invalid="true"] { border-color: var(--red); }

/* Admin dialog */
.dialog-panel.wide { max-width: 980px; }
.admin-tabs {
  display: flex; gap: 4px; margin: -4px 0 18px;
  border-bottom: 1px solid var(--line);
  overflow-x: auto; scrollbar-width: thin;
}
.admin-tabs [role="tab"] {
  position: relative; flex: none; white-space: nowrap;
  padding: 10px 16px; background: none; border: 0; border-radius: 8px 8px 0 0;
  color: var(--muted); font: inherit; font-size: 14px; cursor: pointer;
}
.admin-tabs [role="tab"]:hover { color: var(--text); background: var(--panel-2); }
.admin-tabs [role="tab"]:focus-visible { outline: 2px solid var(--primary); outline-offset: -2px; }
.admin-tabs [role="tab"][aria-selected="true"] { color: var(--text); font-weight: 600; }
/* Selected tab is marked by an underline, not colour alone. */
.admin-tabs [role="tab"][aria-selected="true"]::after {
  content: ""; position: absolute; inset: auto 8px 0; height: 3px;
  border-radius: 3px 3px 0 0; background: var(--primary);
}
.panel-inner { padding: 2px 0; }
.panel-inner .modal-foot { padding-left: 0; padding-right: 0; }
.section-title { margin: 0 0 12px; font-size: 14px; }
.details { display: grid; grid-template-columns: max-content 1fr; gap: 6px 16px; margin: 0; font-size: 14px; }
.details dt { color: var(--muted); }
.details dd { margin: 0; overflow-wrap: anywhere; }
.warning-list { margin: 12px 0 0; padding-left: 20px; color: var(--amber); font-size: 13px; }
.warning-list:empty { display: none; }
.field textarea.mono, .panel-inner textarea { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 12px; }
.email-conn .row-actions { justify-content: flex-start; margin-top: 12px; }
.checkbox-group { border: 0; padding: 0; margin: 14px 0 0; }
.checkbox-group legend { padding: 0; margin-bottom: 4px; color: var(--muted); font-size: 12px; }
.checkbox-group .checkbox { margin: 8px 0; }
.inline-field { flex-direction: row; align-items: center; gap: 8px; margin-left: auto; }
.inline-field input { min-width: 220px; }
#adminNotice { margin: 0 0 16px; }

/* Login */
.login-wrap { min-height: 100vh; display: grid; place-items: center; padding: 20px; }
.login-card { background: var(--panel); border: 1px solid var(--line); border-radius: 14px; padding: 32px; width: 100%; max-width: 360px; }
.login-card h1 { margin: 0 0 6px; font-size: 20px; }
.login-card p { margin: 0 0 20px; color: var(--muted); }
.login-card .field { margin-bottom: 16px; }

@media (max-width: 640px) {
  .grid { grid-template-columns: 1fr; }
  .rules-layout { grid-template-columns: 1fr; }
  .number-rule { grid-template-columns: 24px 1fr; }
  .number-rule .btn { grid-row: auto; }
  .topbar { align-items: flex-start; gap: 12px; flex-direction: column; }
  .topbar-actions { flex-wrap: wrap; }
}
