:root {
  /* Surfaces — a deep, slightly blue-violet near-black with layered elevation. */
  --bg: #08090d;
  --panel: #131722;
  --panel-2: #1a1f2b;
  --panel-3: #232a39;
  --line: #262d3d;
  --line-soft: #1b2130;
  --text: #f1f3f9;
  --muted: #8b94a7;

  /* Brand — indigo → violet gradient accent. */
  --accent: #818cf8;
  --accent-h: #6d78f0;
  --accent-2: #a78bfa;
  --accent-soft: rgba(129,140,248,.15);
  --grad: linear-gradient(135deg, #6366f1, #8b5cf6);

  --danger: #f87171;
  --ok: #34d399;
  --warn: #fbbf24;

  --radius: 16px;
  --radius-sm: 10px;
  --shadow-sm: 0 1px 2px rgba(0,0,0,.4);
  --shadow-md: 0 8px 26px rgba(0,0,0,.45);
  --shadow-lg: 0 28px 70px rgba(0,0,0,.62);
  --glow: 0 6px 22px rgba(99,102,241,.35);
  --ring: 0 0 0 3px var(--accent-soft);
  --t: .16s ease;
}

* { box-sizing: border-box; }

html { scroll-behavior: smooth; }

body {
  margin: 0;
  font: 14.5px/1.55 "Inter", system-ui, -apple-system, Segoe UI, Roboto, sans-serif;
  background:
    radial-gradient(900px 500px at 12% -10%, rgba(99,102,241,.16), transparent 60%),
    radial-gradient(900px 500px at 100% 0%, rgba(139,92,246,.12), transparent 55%),
    var(--bg);
  background-attachment: fixed;
  color: var(--text);
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
}

/* Custom scrollbars — keep the dark theme cohesive over the OS defaults. */
* { scrollbar-width: thin; scrollbar-color: var(--line) transparent; }
*::-webkit-scrollbar { width: 10px; height: 10px; }
*::-webkit-scrollbar-thumb { background: var(--line); border: 2px solid transparent; background-clip: padding-box; border-radius: 999px; }
*::-webkit-scrollbar-thumb:hover { background: var(--panel-3); background-clip: padding-box; }

:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; border-radius: 4px; }

.hidden { display: none !important; }
.offscreen { position: fixed; top: 0; left: 0; opacity: 0; pointer-events: none; }
.muted { color: var(--muted); }
.center { text-align: center; }
.small { font-size: 12px; }
.grow { flex: 1; }

h1, h2 { letter-spacing: -.02em; }

button {
  font: inherit;
  font-weight: 600;
  cursor: pointer;
  border: 1px solid transparent;
  border-radius: var(--radius-sm);
  padding: 9px 16px;
  background: var(--grad);
  background-size: 140% 140%;
  color: #fff;
  box-shadow: var(--shadow-sm);
  transition: filter var(--t), background-position var(--t), border-color var(--t),
    color var(--t), box-shadow var(--t), transform var(--t);
}
button:hover { filter: brightness(1.07); background-position: 100% 100%; box-shadow: var(--glow); }
button:active { transform: translateY(1px); box-shadow: var(--shadow-sm); }
button:disabled { opacity: .5; cursor: not-allowed; transform: none; filter: grayscale(.3); box-shadow: none; }
button.ghost { background: var(--panel-2); color: var(--muted); border-color: var(--line); box-shadow: none; }
button.ghost:hover { background: var(--panel-3); color: var(--text); border-color: var(--panel-3); filter: none; box-shadow: none; }
button.danger { background: transparent; color: var(--danger); border-color: var(--line); padding: 5px 11px; box-shadow: none; }
button.danger:hover { background: rgba(248,113,113,.12); border-color: rgba(248,113,113,.45); filter: none; box-shadow: none; }

.card {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow-md);
}

/* Login */
.login { min-height: 100vh; display: grid; place-items: center; padding: 20px; }
.login-card { padding: 34px; width: 340px; display: flex; flex-direction: column; gap: 14px; box-shadow: var(--shadow-lg); animation: pop .22s ease both; }
.login-card h1 {
  margin: 0; font-size: 30px; font-weight: 800;
  background: var(--grad); -webkit-background-clip: text; background-clip: text; color: transparent;
}
.login-card p { margin: 0 0 6px; }
.login-card label { display: flex; flex-direction: column; gap: 5px; font-size: 13px; color: var(--muted); }
.login-card input {
  font: inherit; padding: 11px 13px; border-radius: var(--radius-sm);
  border: 1px solid var(--line); background: var(--panel-2); color: var(--text);
  transition: border-color var(--t), box-shadow var(--t);
}
.login-card button { margin-top: 6px; padding: 11px; font-size: 15px; }
.error { color: var(--danger); margin: 0; }

input:focus, select:focus, textarea:focus { outline: none; }
input:focus-visible, select:focus-visible, textarea:focus-visible {
  outline: none; border-color: var(--accent); box-shadow: var(--ring);
}

/* App shell */
.topbar {
  display: flex; align-items: center; gap: 14px;
  padding: 14px 24px; border-bottom: 1px solid var(--line);
  background: rgba(13,16,24,.72); backdrop-filter: blur(14px) saturate(1.4);
  box-shadow: var(--shadow-sm);
  position: sticky; top: 0; z-index: 5;
}
.topbar strong {
  font-size: 17px; font-weight: 800; letter-spacing: -.02em;
  background: var(--grad); -webkit-background-clip: text; background-clip: text; color: transparent;
  display: inline-flex; align-items: center; gap: 9px;
}
.topbar strong::before {
  content: ""; width: 11px; height: 11px; border-radius: 50%;
  background: var(--grad); box-shadow: 0 0 12px rgba(129,140,248,.8); flex: 0 0 auto;
}
main { padding: 28px 24px; display: flex; flex-direction: column; gap: 22px; max-width: 1160px; margin: 0 auto; animation: rise .25s ease both; }

/* Node panel */
#nodes { display: flex; flex-direction: column; gap: 18px; }
.node { padding: 0; overflow: hidden; transition: border-color var(--t), box-shadow var(--t), transform var(--t); }
.node:hover { border-color: var(--panel-3); box-shadow: var(--shadow-lg); transform: translateY(-1px); }
.node-head { display: flex; align-items: center; gap: 12px; padding: 16px 20px; border-bottom: 1px solid var(--line); flex-wrap: wrap; background: linear-gradient(180deg, rgba(99,102,241,.06), transparent); }
.node-head h2 { margin: 0; font-size: 17px; }
.pill { font-size: 11px; font-weight: 600; padding: 3px 10px; border-radius: 999px; border: 1px solid var(--line); color: var(--muted); background: var(--panel-2); white-space: nowrap; text-transform: uppercase; letter-spacing: .03em; }
.pill.online { color: var(--ok); border-color: rgba(52,211,153,.4); background: rgba(52,211,153,.12); }
.pill.stale { color: var(--warn); border-color: rgba(251,191,36,.4); background: rgba(251,191,36,.12); }
.pill.offline { color: var(--muted); background: var(--panel-2); }
.node-meta { color: var(--muted); font-size: 12px; }

/* Remote/Local sliding switch in the node header. */
.switch { display: inline-flex; align-items: center; gap: 8px; cursor: pointer; user-select: none; }
.switch input { position: absolute; opacity: 0; width: 0; height: 0; }
.switch-track {
  position: relative; width: 40px; height: 22px; flex: 0 0 auto;
  border-radius: 999px; background: var(--line); transition: background var(--t);
}
.switch-track::after {
  content: ""; position: absolute; top: 2px; left: 2px; width: 18px; height: 18px;
  border-radius: 50%; background: #fff; box-shadow: var(--shadow-sm); transition: transform var(--t);
}
.switch input:checked + .switch-track { background: var(--accent); }
.switch input:checked + .switch-track::after { transform: translateX(18px); }
.switch input:focus-visible + .switch-track { outline: 2px solid var(--accent); outline-offset: 2px; }
.switch-state { font-size: 12px; color: var(--muted); min-width: 42px; }

/* Advertised subnets subsection inside a node card. */
.subnets { border-top: 1px solid var(--line); padding: 14px 20px 6px; }
.subnets-summary { display: flex; align-items: center; gap: 8px; cursor: pointer; list-style-position: outside;
  font-size: 12px; color: var(--muted); text-transform: uppercase; letter-spacing: .05em; font-weight: 600; user-select: none; }
.subnets-summary:hover { color: var(--text); }
.subnets[open] .subnets-summary { margin-bottom: 10px; }
.subnets-summary .small { text-transform: none; letter-spacing: 0; font-weight: 400; }
.subnet-table th, .subnet-table td { padding: 6px 0; }
.subnet-table th:not(:last-child), .subnet-table td:not(:last-child) { padding-right: 18px; }
.subnet-add { gap: 8px; flex-wrap: wrap; }
.subnet-add input, .subnet-add select {
  font: inherit; padding: 8px 11px; border-radius: var(--radius-sm);
  border: 1px solid var(--line); background: var(--panel-2); color: var(--text);
  transition: border-color var(--t), box-shadow var(--t);
}
.pill.mode-masquerade { color: var(--accent); border-color: rgba(129,140,248,.4); background: var(--accent-soft); }
.pill.mode-routed { color: var(--ok); border-color: rgba(52,211,153,.4); background: rgba(52,211,153,.12); }

table { width: 100%; border-collapse: collapse; }
th, td { text-align: left; padding: 11px 20px; border-bottom: 1px solid var(--line-soft); }
th { font-size: 11px; text-transform: uppercase; letter-spacing: .05em; color: var(--muted); font-weight: 600; }
tbody tr { transition: background var(--t); }
tbody tr:hover { background: var(--line-soft); }
tr:last-child td { border-bottom: 0; }
td.num { font-variant-numeric: tabular-nums; }
.mono { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 12px; }

.dot { display: inline-block; width: 9px; height: 9px; border-radius: 50%; margin-right: 7px; background: var(--muted); }
.dot.online { background: var(--ok); box-shadow: 0 0 0 3px rgba(52,211,153,.2); }
.dot.stale { background: var(--warn); box-shadow: 0 0 0 3px rgba(251,191,36,.2); }
.dot.offline { background: var(--muted); }

.add-row { display: flex; gap: 8px; padding: 14px 20px; border-top: 1px solid var(--line); background: var(--panel-2); }
.add-row input {
  font: inherit; padding: 9px 12px; border-radius: var(--radius-sm); flex: 1; max-width: 260px;
  border: 1px solid var(--line); background: var(--panel); color: var(--text);
  transition: border-color var(--t), box-shadow var(--t);
}
.row-actions { display: flex; gap: 6px; justify-content: flex-end; }
.link-btn { background: var(--panel-2); color: var(--accent); padding: 5px 11px; border: 1px solid var(--line); box-shadow: none; }
.link-btn:hover { background: var(--accent-soft); border-color: rgba(129,140,248,.4); filter: none; box-shadow: none; }

/* Modal */
.modal { position: fixed; inset: 0; background: rgba(4,5,9,.66); backdrop-filter: blur(6px); display: grid; place-items: center; z-index: 20; padding: 20px; animation: fade .16s ease both; }
.modal-box { width: min(720px, 100%); padding: 22px; box-shadow: var(--shadow-lg); animation: pop .22s ease both; max-height: calc(100vh - 40px); overflow: auto; }
.modal-head { display: flex; align-items: center; }
.modal-head h2 { margin: 0; font-size: 18px; flex: 1; }
.conf-grid { display: grid; grid-template-columns: 1fr auto; gap: 18px; margin: 16px 0; }
.conf { background: var(--bg); border: 1px solid var(--line); border-radius: 10px; padding: 14px; margin: 0; overflow: auto; max-height: 320px; white-space: pre; font-family: ui-monospace, monospace; font-size: 12px; }
.qr-wrap { display: flex; flex-direction: column; align-items: center; gap: 8px; }
.qr-wrap #qr { background: #fff; padding: 10px; border-radius: 10px; line-height: 0; }
.modal-actions { display: flex; gap: 8px; justify-content: flex-end; }
@media (max-width: 620px) { .conf-grid { grid-template-columns: 1fr; } }

@keyframes fade { from { opacity: 0; } to { opacity: 1; } }
@keyframes pop { from { opacity: 0; transform: translateY(10px) scale(.98); } to { opacity: 1; transform: none; } }
@keyframes rise { from { opacity: 0; transform: translateY(6px); } to { opacity: 1; transform: none; } }

/* Tabs */
.tabs { display: flex; gap: 4px; margin-left: 18px; }
.tab { background: transparent; color: var(--muted); border: 1px solid transparent; padding: 7px 14px; border-radius: var(--radius-sm); box-shadow: none; font-weight: 600; }
.tab:hover { background: var(--panel-2); color: var(--text); filter: none; box-shadow: none; }
.tab.active { color: var(--text); background: var(--accent-soft); border-color: rgba(129,140,248,.4); filter: none; box-shadow: none; }

/* Proxy status panel */
.proxy-status { padding: 18px 20px; }
.proxy-status-head { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; }
.proxy-status-head h2 { margin: 0; font-size: 18px; }
#proxy-disabled { margin: 10px 0 0; }
#proxy-disabled code, .muted code { background: var(--panel-2); padding: 1px 6px; border-radius: 5px; font-size: 12px; }
tr.row-off td { opacity: 0.5; }
td.cert-error { color: var(--danger); cursor: help; }

/* Route form */
.form-grid { display: flex; flex-direction: column; gap: 14px; margin: 16px 0; }
.form-grid label { display: flex; flex-direction: column; gap: 5px; font-size: 13px; color: var(--muted); }
.form-grid label.check { flex-direction: row; align-items: center; gap: 8px; color: var(--text); }
.form-grid input[type=text], .form-grid input:not([type]), .form-grid input[type=password], .form-grid select {
  font: inherit; padding: 9px 12px; border-radius: var(--radius-sm);
  border: 1px solid var(--line); background: var(--panel-2); color: var(--text);
  transition: border-color var(--t), box-shadow var(--t);
}
.form-grid input[type=checkbox] { width: 17px; height: 17px; accent-color: var(--accent); }
.subfields { display: flex; flex-direction: column; gap: 12px; padding: 14px; border: 1px dashed var(--line); border-radius: 12px; background: rgba(0,0,0,.15); }
.dev-row { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 8px 0; border-bottom: 1px solid var(--line); }
.dev-row:last-child { border-bottom: none; }
.dev-row .muted { word-break: break-word; }

/* Toast */
.toast { position: fixed; bottom: 24px; left: 50%; transform: translateX(-50%); background: var(--panel-3); border: 1px solid var(--line); padding: 12px 18px; border-radius: 12px; z-index: 30; box-shadow: var(--shadow-lg); }
.toast:not(.hidden) { animation: toast-in .24s cubic-bezier(.2,.8,.3,1) both; }
.toast.err { border-color: var(--danger); color: #fecaca; }
@keyframes toast-in { from { opacity: 0; transform: translate(-50%, 14px); } to { opacity: 1; transform: translate(-50%, 0); } }

/* WAF events */
.inline-label { display: flex; align-items: center; gap: 6px; color: var(--muted); }
.pill.blocked { color: #fecaca; border-color: rgba(248,113,113,.5); background: rgba(248,113,113,.14); }
.pill.logged { color: var(--warn); border-color: rgba(251,191,36,.4); background: rgba(251,191,36,.12); }

.waf-stats { display: flex; flex-wrap: wrap; gap: 14px; margin-top: 14px; }
.waf-card { background: linear-gradient(160deg, var(--panel-3), var(--panel-2)); border: 1px solid var(--line); border-radius: 14px; padding: 16px 18px; min-width: 120px; box-shadow: var(--shadow-sm); }
.waf-card.wide { flex: 1; min-width: 220px; }
.waf-card-n { font-size: 30px; font-weight: 800; letter-spacing: -.03em; font-variant-numeric: tabular-nums; line-height: 1.1; }
.waf-card-t { font-size: 11px; color: var(--muted); margin-bottom: 8px; text-transform: uppercase; letter-spacing: .06em; font-weight: 600; }
.waf-list { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 4px; }
.waf-list li { display: flex; gap: 8px; align-items: baseline; }
.waf-list li.clickable { cursor: pointer; border-radius: 6px; margin: 0 -4px; padding: 0 4px; transition: background var(--t); }
.waf-list li.clickable:hover { background: var(--panel-2); }

.waf-filters { display: flex; flex-wrap: wrap; gap: 8px; margin-bottom: 14px; align-items: center; }
.waf-filters input, .waf-filters select {
  font: inherit; padding: 8px 11px; border-radius: var(--radius-sm);
  border: 1px solid var(--line); background: var(--panel-2); color: var(--text);
  transition: border-color var(--t), box-shadow var(--t);
}
.waf-filters input[type=number] { width: 110px; }
.waf-uri { max-width: 360px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; display: inline-block; vertical-align: bottom; }
.waf-row { cursor: pointer; }
.waf-row:hover { background: var(--panel-2); }

.waf-kvs { display: grid; grid-template-columns: max-content 1fr; gap: 4px 16px; margin-bottom: 14px; }
.waf-kv { display: contents; }
.waf-rule { border-top: 1px solid var(--line); padding: 10px 0; display: flex; flex-direction: column; gap: 4px; }

.waf-cats-block { display: flex; flex-direction: column; gap: 6px; }
.waf-cat-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(190px, 1fr)); gap: 6px 12px; }
.waf-cat { display: flex; align-items: center; gap: 8px; color: var(--text); font-size: 13px; }
.waf-cat input[type=checkbox] { width: 17px; height: 17px; accent-color: var(--accent); }

/* Respect users who prefer minimal motion. */
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: .001ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: .001ms !important;
    scroll-behavior: auto !important;
  }
}

/* Reverse-proxy route folders (cosmetic grouping + drag-and-drop) */
.folder-toolbar { display: flex; justify-content: flex-end; margin-bottom: 12px; }
.folder-card { margin-bottom: 12px; overflow: hidden; transition: border-color var(--t), box-shadow var(--t); }
.folder-head {
  display: flex; align-items: center; gap: 10px;
  padding: 12px 16px; cursor: pointer; user-select: none;
  border-bottom: 1px solid var(--line-soft);
}
.folder-card:last-child { margin-bottom: 0; }
.folder-caret { color: var(--muted); width: 12px; font-size: 12px; }
.folder-name { font-weight: 600; }
.folder-count {
  margin-left: auto; min-width: 22px; text-align: center;
  padding: 1px 8px; border-radius: 999px; font-size: 12px;
  color: var(--muted); background: var(--panel-2); border: 1px solid var(--line);
}
.folder-body { padding: 0; }
.folder-empty { padding: 18px 16px; margin: 0; text-align: center; }
/* Drop target highlight while dragging a route over a folder. */
.folder-card.drop-hover { border-color: var(--accent); box-shadow: var(--ring); }
.folder-card.drop-hover .folder-head { background: var(--accent-soft); }
/* Draggable rows. */
tr.route-row { cursor: grab; }
tr.route-row:active { cursor: grabbing; }
tr.dragging { opacity: 0.45; }
