@import url('https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&display=swap');

:root {
  --bg:         #f1f5f9;
  --surface:    #ffffff;
  --surface-2:  #f8fafc;
  --surface-3:  #f1f5f9;
  --border:     #e2e8f0;
  --border-2:   #cbd5e1;
  --text:       #0f172a;
  --text-muted: #64748b;
  --text-dim:   #94a3b8;
  --accent:     #2563eb;
  --accent-2:   #1d4ed8;
  --green:      #16a34a;
  --green-2:    #15803d;
  --red:        #dc2626;
  --red-2:      #b91c1c;
  --yellow:     #d97706;
  --radius:     12px;
  --radius-sm:  7px;
  --shadow:     0 2px 12px rgba(15,23,42,0.08);
  --shadow-sm:  0 1px 6px rgba(15,23,42,0.06);

  color-scheme: light;
  font-family: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', system-ui, sans-serif;
  background: var(--bg);
  color: var(--text);
  font-size: 14px;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--bg);
  min-height: 100vh;
}

main {
  margin: 0 auto;
  max-width: 1920px;
  padding: 28px 12px 40px;
  min-width: 0;
}

/* ── TOP BAR ────────────────────────────────── */

.topbar {
  display: flex;
  align-items: flex-end;
  justify-content: space-between;
  gap: 20px;
  margin-bottom: 20px;
  padding-bottom: 20px;
  border-bottom: 1px solid var(--border);
}

h1 {
  margin: 0;
  font-size: 26px;
  font-weight: 700;
  letter-spacing: -0.5px;
  color: var(--text);
}

.topbar p {
  margin: 5px 0 0;
  font-size: 13px;
  color: var(--text-muted);
}

#refreshTime {
  font-variant-numeric: tabular-nums;
}

.actions {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
  justify-content: flex-end;
}

/* ── BUTTONS ─────────────────────────────────── */

button {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  border: 1px solid var(--border-2);
  background: var(--surface);
  color: var(--text);
  border-radius: var(--radius-sm);
  padding: 9px 16px;
  font-size: 13px;
  font-weight: 600;
  font-family: inherit;
  cursor: pointer;
  transition: background 0.15s, border-color 0.15s, transform 0.1s, box-shadow 0.15s;
  white-space: nowrap;
}

button:hover {
  background: var(--surface-3);
  border-color: var(--border-2);
  box-shadow: var(--shadow-sm);
}

button:active { transform: scale(0.97); }

button:disabled {
  cursor: default;
  opacity: 0.4;
  transform: none;
  box-shadow: none;
}

#scanBtn {
  background: linear-gradient(135deg, var(--green-2), var(--green));
  border-color: var(--green-2);
  color: #ffffff;
  font-weight: 700;
  box-shadow: 0 2px 8px rgba(22,163,74,0.20);
}

#scanBtn:hover:not(:disabled) {
  background: linear-gradient(135deg, #166534, #16a34a);
  box-shadow: 0 4px 14px rgba(22,163,74,0.30);
}

#scanBtn:disabled {
  background: var(--surface-2);
  border-color: var(--border);
  color: var(--text-muted);
  box-shadow: none;
}

/* ── SEARCH BAR ─────────────────────────────── */

.search-bar {
  align-items: center;
  display: flex;
  gap: 10px;
  margin-bottom: 12px;
}

#searchInput {
  background: var(--surface);
  border: 1.5px solid var(--border-2);
  border-radius: var(--radius);
  color: var(--text);
  font-size: 15px;
  padding: 10px 16px;
  width: 380px;
  transition: border-color 0.15s, box-shadow 0.15s;
}

#searchInput:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(37,99,235,0.12);
  outline: none;
}

#searchInput::placeholder {
  color: var(--text-dim);
}

#searchCount {
  color: var(--text-muted);
  font-size: 13px;
  font-variant-numeric: tabular-nums;
  min-width: 60px;
}

/* ── STATUS LINE ────────────────────────────── */

.status-line {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
  padding: 12px 0 16px;
  min-height: 50px;
}

.pill {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 4px 12px;
  font-size: 12px;
  font-weight: 500;
  background: var(--surface);
  color: var(--text-muted);
  white-space: nowrap;
}

.pill.scanning {
  border-color: #86efac;
  background: #f0fdf4;
  color: var(--green);
}

.pill.scanning::before {
  content: '';
  display: inline-block;
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--green);
  box-shadow: 0 0 5px rgba(22,163,74,0.5);
  animation: pulse 1.2s ease-in-out infinite;
}

.pill.bad {
  border-color: #fca5a5;
  background: #fff1f2;
  color: var(--red);
}

@keyframes pulse {
  0%, 100% { opacity: 1; transform: scale(1); }
  50%       { opacity: 0.4; transform: scale(0.75); }
}

/* ── SCAN PROGRESS BAR ──────────────────────── */

#scanProgressWrap {
  margin-bottom: 14px;
}

.sp-header {
  display: flex;
  align-items: center;
  gap: 16px;
  margin-bottom: 7px;
  font-size: 13px;
}

#spPhase {
  color: var(--text);
  font-weight: 700;
  min-width: 80px;
}

#spCounts {
  color: var(--text-muted);
  font-variant-numeric: tabular-nums;
  flex: 1;
}

#spEta {
  color: var(--text-muted);
  font-size: 12px;
  font-variant-numeric: tabular-nums;
  min-width: 80px;
  text-align: right;
}

#spFound {
  color: var(--green);
  font-weight: 700;
  font-variant-numeric: tabular-nums;
}

.sp-bar {
  background: var(--border);
  border-radius: 999px;
  height: 8px;
  overflow: hidden;
  position: relative;
}

.sp-fill {
  background: linear-gradient(90deg, var(--accent-2), var(--accent));
  border-radius: 999px;
  height: 100%;
  min-width: 4px;
  transition: width 0.4s ease;
  position: relative;
}

.sp-fill::after {
  animation: shimmer 1.4s infinite;
  background: linear-gradient(90deg, transparent 0%, rgba(255,255,255,0.45) 50%, transparent 100%);
  border-radius: 999px;
  content: "";
  height: 100%;
  left: 0;
  position: absolute;
  top: 0;
  width: 60%;
}

@keyframes shimmer {
  0%   { transform: translateX(-100%); }
  100% { transform: translateX(200%); }
}

/* ── TABLE ──────────────────────────────────── */

.table-wrap {
  overflow: auto;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  box-shadow: var(--shadow);
}

table {
  width: 100%;
  border-collapse: collapse;
  min-width: 1480px;
}

thead {
  position: sticky;
  top: 0;
  z-index: 2;
}

th, td {
  padding: 8px 9px;
  border-bottom: 1px solid var(--border);
  text-align: left;
  white-space: nowrap;
}

th {
  padding: 0;
  background: var(--surface-2);
  color: var(--text-muted);
  font-size: 11px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  border-bottom: 2px solid var(--border);
}

th:first-child .sort-btn { border-radius: var(--radius) 0 0 0; }
th:last-child  .sort-btn { border-radius: 0 var(--radius) 0 0; }

.sort-btn {
  position: relative;
  width: 100%;
  min-height: 36px;
  border: 0;
  border-radius: 0;
  background: transparent;
  color: inherit;
  padding: 8px 20px 8px 10px;
  font-size: 11px;
  font-weight: 600;
  font-family: inherit;
  text-align: left;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  transition: background 0.12s, color 0.12s;
  cursor: pointer;
}

.sort-btn:hover {
  background: var(--surface-3);
  color: var(--text);
  box-shadow: none;
}

.sort-btn.active {
  background: #eff6ff;
  color: var(--accent);
}

.sort-btn::after {
  content: "";
  position: absolute;
  right: 8px;
  top: 50%;
  transform: translateY(-50%);
  font-size: 10px;
  opacity: 0.5;
}

.sort-btn[data-dir="asc"]::after  { content: "↑"; opacity: 1; color: var(--accent); }
.sort-btn[data-dir="desc"]::after { content: "↓"; opacity: 1; color: var(--accent); }

tbody tr {
  transition: background 0.1s;
}

tbody tr:hover td {
  background: #f8fafc;
}

tbody tr:last-child td { border-bottom: 0; }

/* ── ROW: BER ALERT ─────────────────────────── */

tbody tr.ber-alert td {
  background: #fee2e2;
  border-bottom-color: #fca5a5;
}

tbody tr.ber-alert td:first-child {
  border-left: 4px solid var(--red);
}

tbody tr.ber-alert:hover td {
  background: #fecaca;
}

tbody tr.ber-alert .link-name {
  color: var(--red);
  font-weight: 700;
}

tbody tr.ber-alert .link-name::before {
  color: var(--red);
  content: '⚠ ';
  font-size: 13px;
  font-weight: 900;
  opacity: 1;
}

/* ── TABLE CELLS ────────────────────────────── */

.link-name {
  font-weight: 600;
  font-size: 13px;
}

.link-title-wrap {
  display: inline-flex;
  align-items: center;
  gap: 6px;
}

.ip-link, .link-title {
  color: var(--accent);
  text-decoration: none;
  font-family: 'SFMono-Regular', Consolas, 'Courier New', monospace;
  font-size: 12.5px;
}

.ip-link:hover, .link-title:hover {
  color: var(--accent-2);
  text-decoration: underline;
  text-decoration-color: var(--accent);
}

.ip-link.ok { color: var(--green); }
.ip-link.bad { color: var(--red); }

.unit-tooltip {
  position: fixed;
  z-index: 1000;
  max-width: min(520px, calc(100vw - 20px));
  max-height: min(620px, calc(100vh - 20px));
  overflow: auto;
  border: 1px solid var(--border-2);
  border-radius: 8px;
  background: #0f172a;
  color: #f8fafc;
  box-shadow: 0 14px 36px rgba(15,23,42,0.26);
  padding: 10px 12px;
  font-family: 'SFMono-Regular', Consolas, 'Courier New', monospace;
  font-size: 12px;
  line-height: 1.55;
  white-space: pre-wrap;
  pointer-events: none;
}

.unit-tooltip[hidden] {
  display: none;
}

.metric {
  font-weight: 600;
  font-size: 13px;
}

.muted {
  color: var(--text-dim);
}

.ok  { color: var(--green); }
.bad { color: var(--red); }

.edit-link-name {
  border: 1px solid var(--border);
  border-radius: 4px;
  background: transparent;
  color: var(--text-muted);
  padding: 2px 7px;
  font-size: 10px;
  font-weight: 600;
  font-family: inherit;
  letter-spacing: 0.03em;
  opacity: 0;
  transition: opacity 0.15s, background 0.15s;
}

tr:hover .edit-link-name { opacity: 1; }

.edit-link-name:hover {
  background: var(--surface-3);
  color: var(--text);
  box-shadow: none;
}

/* ── DIVIDER BETWEEN A/B SIDES ──────────────── */

td.side-divider, th.side-divider {
  border-left: 2px solid var(--border-2);
}

/* ── STATS ──────────────────────────────────── */

.stats-wrap {
  margin-top: 16px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  box-shadow: var(--shadow-sm);
  padding: 18px;
}

h2 {
  margin: 0 0 14px;
  color: var(--text-muted);
  font-size: 11px;
  font-weight: 600;
  letter-spacing: 0.1em;
  text-transform: uppercase;
}

.type-stats {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
  gap: 8px;
}

.stat-vendor {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  min-height: 32px;
  grid-column: 1 / -1;
  padding: 6px 12px 4px;
  border-bottom: 1px solid var(--border);
  margin-bottom: 2px;
}

.stat-vendor span {
  color: var(--text);
  font-size: 12px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.05em;
}

.stat-vendor strong {
  color: var(--text);
  font-size: 16px;
  font-weight: 800;
  flex-shrink: 0;
}

.stat-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  min-height: 36px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 8px 12px;
  background: var(--surface-2);
  font-size: 13px;
  transition: border-color 0.15s, background 0.15s;
}

.stat-row:hover {
  background: var(--surface-3);
  border-color: var(--border-2);
}

.stat-row span {
  color: var(--text-muted);
  font-size: 12px;
  overflow: hidden;
  text-overflow: ellipsis;
}

.stat-row strong {
  color: var(--accent);
  font-size: 15px;
  font-weight: 700;
  flex-shrink: 0;
}

/* ── SCAN RESULTS ───────────────────────────── */

#scanResults {
  margin-bottom: 18px;
}

.scan-summary {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
}

.scan-summary summary {
  align-items: center;
  cursor: pointer;
  display: flex;
  gap: 12px;
  justify-content: space-between;
  list-style: none;
  padding: 12px 16px;
  user-select: none;
}

.scan-summary summary::-webkit-details-marker { display: none; }

.scan-summary summary::before {
  color: var(--accent);
  content: "▶";
  display: inline-block;
  font-size: 10px;
  transition: transform 0.15s;
}

.scan-summary[open] summary::before {
  transform: rotate(90deg);
}

.scan-summary:hover summary {
  background: var(--surface-2);
}

.scan-summary-title {
  color: var(--text);
  font-size: 13px;
  font-weight: 600;
}

.scan-summary-meta {
  color: var(--text-muted);
  font-size: 12px;
  margin-left: auto;
}

.scan-summary[open] .scan-list {
  animation: scan-reveal 0.35s ease;
}

@keyframes scan-reveal {
  from { opacity: 0; transform: translateY(-6px); }
  to   { opacity: 1; transform: translateY(0); }
}

.scan-list {
  border-top: 1px solid var(--border);
  display: grid;
  gap: 1px;
  grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
  padding: 10px;
  background: var(--border);
}

.scan-link {
  align-items: baseline;
  background: var(--surface);
  border-radius: var(--radius-sm);
  display: flex;
  flex-direction: column;
  gap: 2px;
  padding: 8px 12px;
}

.scan-link-name {
  color: var(--text);
  font-size: 13px;
  font-weight: 600;
}

.scan-link-ips {
  color: var(--text-muted);
  font-family: ui-monospace, monospace;
  font-size: 11px;
}

/* ── SCAN GROUP ─────────────────────────────── */

.scan-group {
  display: flex;
  align-items: stretch;
  gap: 0;
}

#scanTarget {
  border: 1px solid var(--border-2);
  border-right: none;
  border-radius: var(--radius-sm) 0 0 var(--radius-sm);
  color: var(--text);
  font-family: ui-monospace, Consolas, monospace;
  font-size: 12px;
  padding: 8px 12px;
  width: 200px;
  background: var(--surface);
  transition: border-color 0.15s, box-shadow 0.15s;
}

#scanTarget::placeholder {
  color: var(--text-dim);
  font-family: inherit;
}

#scanTarget:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(37,99,235,0.12);
  outline: none;
  z-index: 1;
  position: relative;
}

.scan-group #scanBtn {
  border-radius: 0 var(--radius-sm) var(--radius-sm) 0;
}

/* ── LINK EDIT DIALOG ───────────────────────── */

#linkEditDialog {
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: 0 8px 40px rgba(15,23,42,0.18);
  padding: 0;
  width: min(400px, calc(100vw - 32px));
}

#linkEditDialog::backdrop {
  background: rgba(15,23,42,0.35);
}

#linkEditForm {
  display: grid;
  gap: 16px;
  padding: 20px;
}

.le-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.le-head h3 {
  font-size: 15px;
  font-weight: 700;
  margin: 0;
  color: var(--text);
}

.le-close {
  background: transparent;
  border: none;
  color: var(--text-muted);
  font-size: 16px;
  padding: 4px 6px;
  box-shadow: none;
}

.le-close:hover { background: var(--surface-3); color: var(--text); box-shadow: none; }

.le-body {
  display: grid;
  gap: 10px;
}

.le-body label {
  display: grid;
  gap: 5px;
  font-size: 12px;
  font-weight: 600;
  color: var(--text-muted);
}

.le-body input {
  border: 1px solid var(--border-2);
  border-radius: var(--radius-sm);
  color: var(--text);
  font-family: ui-monospace, monospace;
  font-size: 13px;
  padding: 8px 10px;
}

.le-body input:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(37,99,235,0.12);
  outline: none;
}

.le-hint {
  color: var(--text-muted);
  font-size: 11px;
  margin: 0;
}

.le-actions {
  display: flex;
  gap: 8px;
  justify-content: flex-end;
}

.btn-primary {
  background: var(--accent);
  border-color: var(--accent);
  color: white;
  font-weight: 700;
}

.btn-primary:hover {
  background: var(--accent-2);
  box-shadow: 0 2px 8px rgba(37,99,235,0.25);
}

/* ── SCROLLBAR ──────────────────────────────── */

::-webkit-scrollbar { width: 7px; height: 7px; }
::-webkit-scrollbar-track { background: var(--surface-2); }
::-webkit-scrollbar-thumb { background: var(--border-2); border-radius: 4px; }
::-webkit-scrollbar-thumb:hover { background: var(--text-dim); }

/* ── RESPONSIVE ─────────────────────────────── */

@media (max-width: 800px) {
  main { width: min(100% - 14px, 1760px); padding: 16px 0 32px; }
  .topbar { align-items: flex-start; flex-direction: column; }
  .actions { justify-content: flex-start; }
  h1 { font-size: 22px; }
}
