  :root {
    --bg:           #ffffff;
    --bg-subtle:    #f9f9fb;
    --bg-muted:     #f3f4f6;
    --surface:      #ffffff;
    --border:       #e5e7eb;
    --border2:      #d1d5db;
    --accent:       #c026d3;
    --accent-light: #e879f9;
    --accent-dim:   rgba(192,38,211,.08);
    --accent-hover: #a21caf;
    --text:         #111827;
    --text2:        #374151;
    --muted:        #6b7280;
    --muted2:       #9ca3af;
    --online:       #059669;
    --online-dim:   rgba(5,150,105,.08);
    --deleted:      #dc2626;
    --deleted-dim:  rgba(220,38,38,.07);
    --code-bg:      #1e1e2e;
    --code-text:    #cdd6f4;
    --radius:       8px;
    --radius-sm:    5px;
    --shadow-sm:    0 1px 2px rgba(0,0,0,.06);
    --shadow:       0 1px 4px rgba(0,0,0,.08), 0 0 0 1px rgba(0,0,0,.04);
    --font-sans:    system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
    --font-mono:    'SFMono-Regular', Consolas, 'Liberation Mono', monospace;
  }

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

  .sr-only {
    position: absolute;
    width: 1px;
    height: 1px;
    padding: 0;
    margin: -1px;
    overflow: hidden;
    clip: rect(0, 0, 0, 0);
    white-space: nowrap;
    border: 0;
  }

  body {
    background: var(--bg);
    color: var(--text);
    font-family: var(--font-sans);
    font-size: 14px;
    min-height: 100vh;
    line-height: 1.6;
    -webkit-font-smoothing: antialiased;
  }

  /* ── Header ── */
  header {
    position: sticky; top: 0; z-index: 100;
    height: 56px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0 28px;
    background: var(--bg);
    border-bottom: 1px solid var(--border);
    box-shadow: var(--shadow-sm);
  }

  .logo {
    display: flex;
    align-items: center;
    gap: 10px;
    text-decoration: none;
  }

  .logo-icon {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 28px; height: 28px;
    background: var(--accent);
    border-radius: 6px;
    flex-shrink: 0;
  }
  .logo-icon svg { width: 16px; height: 16px; fill: #fff; }

  .logo-text {
    font-weight: 700;
    font-size: .9rem;
    color: var(--text);
    letter-spacing: -.02em;
  }

  .header-right {
    display: flex;
    align-items: center;
    gap: 20px;
  }

  .version-badge {
    font-family: var(--font-sans);
    font-size: .78rem;
    font-weight: 500;
    color: var(--muted);
    background: var(--bg-muted);
    border: 1px solid var(--border);
    border-radius: 6px;
    padding: 3px 10px;
  }

  .status-badge {
    display: flex;
    align-items: center;
    gap: 7px;
    font-size: .75rem;
    color: var(--muted);
    font-family: var(--font-mono);
  }

  .dot {
    width: 7px; height: 7px;
    border-radius: 50%;
    background: var(--border2);
    flex-shrink: 0;
    transition: background .3s;
  }
  .dot.live {
    background: var(--online);
    box-shadow: 0 0 0 3px rgba(5,150,105,.2);
    animation: pulse 2s infinite;
  }
  @keyframes pulse { 0%,100%{opacity:1} 50%{opacity:.4} }

  /* ── Layout ── */
  main {
    max-width: 1200px;
    margin: 0 auto;
    padding: 28px 28px 64px;
  }

  /* ── Toolbar ── */
  .toolbar {
    display: flex;
    align-items: center;
    gap: 8px;
    margin-bottom: 24px;
    flex-wrap: wrap;
  }

  .input-group {
    display: flex;
    align-items: stretch;
    flex: 1;
    min-width: 200px;
    background: var(--bg);
    border: 1px solid var(--border2);
    border-radius: var(--radius-sm);
    overflow: hidden;
    transition: border-color .2s, box-shadow .2s;
  }
  .input-group:focus-within {
    border-color: var(--accent);
    box-shadow: 0 0 0 3px var(--accent-dim);
  }

  .input-label {
    display: flex;
    align-items: center;
    padding: 0 12px;
    background: var(--bg-subtle);
    border-right: 1px solid var(--border);
    font-family: var(--font-mono);
    font-size: .72rem;
    color: var(--muted);
    white-space: nowrap;
    flex-shrink: 0;
  }

  .cluster-input {
    flex: 1;
    background: transparent;
    border: none;
    padding: 9px 13px;
    color: var(--text);
    font-family: var(--font-mono);
    font-size: .83rem;
    outline: none;
  }
  .cluster-input::placeholder { color: var(--muted2); }
  .cluster-input.empty { border-color: var(--deleted) !important; box-shadow: 0 0 0 3px var(--deleted-dim) !important; }
  @keyframes shake {
    0%,100% { transform: translateX(0); }
    20%      { transform: translateX(-5px); }
    40%      { transform: translateX(5px); }
    60%      { transform: translateX(-4px); }
    80%      { transform: translateX(4px); }
  }
  .cluster-input.shake { animation: shake .4s ease; }

  .btn {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    height: 38px;
    padding: 0 16px;
    background: var(--bg);
    border: 1px solid var(--border2);
    border-radius: var(--radius-sm);
    color: var(--text2);
    font-family: var(--font-sans);
    font-size: .8rem;
    font-weight: 500;
    cursor: pointer;
    transition: all .15s;
    white-space: nowrap;
  }
  .btn:hover { border-color: var(--accent); color: var(--accent); background: var(--accent-dim); }
  .btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
  .btn.primary:hover { background: var(--accent-hover); border-color: var(--accent-hover); }
  .btn.watch-active { background: var(--accent-dim); border-color: var(--accent); color: var(--accent); }

  /* ── Stats ── */
  .stats-row {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(140px, 1fr));
    gap: 12px;
    margin-bottom: 24px;
  }

  .stat-card {
    background: var(--bg);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 16px 18px;
    box-shadow: var(--shadow-sm);
    transition: border-color .15s;
  }
  .stat-card:hover { border-color: var(--accent-light); }

  .stat-label {
    font-size: .67rem;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: .1em;
    color: var(--muted);
    margin-bottom: 6px;
  }

  .stat-value {
    font-family: var(--font-mono);
    font-size: 1.7rem;
    font-weight: 700;
    color: var(--accent);
    line-height: 1;
  }
  .stat-value.sm {
    font-size: .9rem;
    padding-top: 4px;
    color: var(--text);
    font-weight: 600;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  /* ── Filter bar ── */
  .filter-bar {
    display: flex;
    align-items: center;
    gap: 10px;
    margin-bottom: 14px;
    flex-wrap: wrap;
  }

  .search-wrap {
    display: flex;
    align-items: center;
    flex: 1;
    min-width: 160px;
    background: var(--bg);
    border: 1px solid var(--border2);
    border-radius: var(--radius-sm);
    overflow: hidden;
    transition: border-color .2s, box-shadow .2s;
  }
  .search-wrap:focus-within {
    border-color: var(--accent);
    box-shadow: 0 0 0 3px var(--accent-dim);
  }

  .search-icon { padding: 0 10px; color: var(--muted2); font-size: .9rem; flex-shrink: 0; }

  .search-input {
    flex: 1;
    background: transparent;
    border: none;
    padding: 8px 10px 8px 0;
    color: var(--text);
    font-family: var(--font-mono);
    font-size: .78rem;
    outline: none;
  }
  .search-input::placeholder { color: var(--muted2); }

  .filter-info {
    font-size: .75rem;
    color: var(--muted);
    font-family: var(--font-mono);
    white-space: nowrap;
  }

  /* ── Error banner ── */
  .error-banner {
    display: none;
    align-items: center;
    gap: 8px;
    background: var(--deleted-dim);
    border: 1px solid rgba(220,38,38,.2);
    border-radius: var(--radius-sm);
    padding: 10px 14px;
    font-size: .8rem;
    color: var(--deleted);
    margin-bottom: 16px;
    font-family: var(--font-mono);
  }

  /* ── Table ── */
  .table-outer {
    background: var(--bg);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    overflow: hidden;
    box-shadow: var(--shadow-sm);
  }

  .json-output {
    margin: 0;
    padding: 16px 18px;
    min-height: 240px;
    max-height: 70vh;
    overflow: auto;
    background: var(--code-bg);
    color: var(--code-text);
    font-family: var(--font-mono);
    font-size: .78rem;
    line-height: 1.5;
    scrollbar-width: thin;
  }

  .affiliate-table {
    width: 100%;
    border-collapse: collapse;
    font-size: .82rem;
  }

  .affiliate-table thead {
    background: var(--bg-subtle);
    border-bottom: 1px solid var(--border);
  }

  .affiliate-table th {
    padding: 10px 14px;
    text-align: left;
    font-size: .67rem;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: .09em;
    color: var(--muted);
    white-space: nowrap;
  }
  .affiliate-table th:first-child { padding-left: 20px; }

  .affiliate-table td {
    padding: 9px 14px;
    border-bottom: 1px solid var(--border);
    vertical-align: middle;
  }
  .affiliate-table td:first-child { padding-left: 20px; }
  .affiliate-table tr:last-child td { border-bottom: none; }

  .affiliate-table tbody tr { transition: background .1s; animation: rowIn .18s ease; }
  @keyframes rowIn { from{opacity:0;transform:translateY(2px)} to{opacity:1;transform:none} }

  .affiliate-table tbody tr:hover   { background: var(--bg-subtle); }
  .affiliate-table tbody tr.deleted { opacity: .45; }

  .id-cell {
    font-family: var(--font-mono);
    font-size: .78rem;
    color: var(--accent);
    font-weight: 500;
    max-width: 220px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    display: block;
  }

  .data-cell {
    font-family: var(--font-mono);
    font-size: .75rem;
    color: var(--muted);
    max-width: 200px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    display: block;
  }

  .ep-tags { display: flex; flex-wrap: wrap; gap: 4px; max-width: 360px; }

  .ep-tag {
    display: inline-block;
    font-family: var(--font-mono);
    font-size: .68rem;
    padding: 2px 8px;
    background: var(--bg-muted);
    border: 1px solid var(--border);
    border-radius: 4px;
    color: var(--text2);
    white-space: nowrap;
    max-width: 200px;
    overflow: hidden;
    text-overflow: ellipsis;
    transition: border-color .1s, color .1s;
  }
  .ep-tag:hover { border-color: var(--accent-light); color: var(--accent); }

  .badge {
    display: inline-flex;
    align-items: center;
    gap: 5px;
    font-family: var(--font-mono);
    font-size: .65rem;
    font-weight: 600;
    padding: 2px 8px;
    border-radius: 4px;
    text-transform: uppercase;
    letter-spacing: .06em;
  }
  .badge-online  { background: var(--online-dim);  color: var(--online);  border: 1px solid rgba(5,150,105,.2); }
  .badge-deleted { background: var(--deleted-dim); color: var(--deleted); border: 1px solid rgba(220,38,38,.2); }

  /* ── Empty state ── */
  .empty-state {
    background: var(--bg-subtle);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 60px 20px;
    text-align: center;
    color: var(--muted);
    font-size: .88rem;
  }
  .empty-icon { font-size: 2.2rem; margin-bottom: 14px; color: var(--muted2); }
  .empty-state strong { color: var(--text2); }

  /* ── Pagination ── */
  .pagination {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-top: 14px;
    font-family: var(--font-mono);
    font-size: .73rem;
    color: var(--muted);
    flex-wrap: wrap;
    gap: 8px;
  }

  .page-btns { display: flex; gap: 3px; flex-wrap: wrap; }

  .page-btn {
    background: var(--bg);
    border: 1px solid var(--border);
    color: var(--muted);
    border-radius: 5px;
    padding: 4px 11px;
    cursor: pointer;
    font-family: var(--font-mono);
    font-size: .73rem;
    transition: all .12s;
  }
  .page-btn:hover:not(:disabled) { border-color: var(--accent); color: var(--accent); background: var(--accent-dim); }
  .page-btn.active-page { background: var(--accent); border-color: var(--accent); color: #fff; font-weight: 600; }
  .page-btn:disabled { opacity: .35; cursor: default; }

  /* ── Event log ── */
  .log-panel {
    margin-top: 28px;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    overflow: hidden;
    box-shadow: var(--shadow-sm);
  }

  .log-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 9px 16px;
    background: var(--bg-subtle);
    border-bottom: 1px solid var(--border);
    font-size: .7rem;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: .1em;
    color: var(--muted);
  }

  .log-body {
    background: var(--code-bg);
    max-height: 150px;
    overflow-y: auto;
    padding: 10px 16px;
    font-family: var(--font-mono);
    font-size: .73rem;
    color: var(--code-text);
    display: flex;
    flex-direction: column-reverse;
    scrollbar-width: thin;
    scrollbar-color: #444 transparent;
  }

  .log-entry {
    padding: 2px 0;
    border-bottom: 1px solid rgba(255,255,255,.04);
    line-height: 1.55;
  }

  .log-ts  { color: var(--accent-light); margin-right: 10px; }
  .log-ok  { color: #a6e3a1; margin-right: 6px; }
  .log-err { color: #f38ba8; margin-right: 6px; }

  /* ── Affiliate detail modal ── */
  .modal-backdrop {
    display: none;
    position: fixed; inset: 0; z-index: 200;
    background: rgba(0,0,0,.35);
    backdrop-filter: blur(2px);
    align-items: center;
    justify-content: center;
    padding: 20px;
  }
  .modal-backdrop.open { display: flex; }

  .modal {
    background: var(--bg);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    box-shadow: 0 20px 60px rgba(0,0,0,.2);
    width: 100%;
    max-width: 640px;
    max-height: 85vh;
    display: flex;
    flex-direction: column;
    animation: modalIn .18s ease;
  }
  @keyframes modalIn { from{opacity:0;transform:translateY(8px)} to{opacity:1;transform:none} }

  .modal-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 16px 20px;
    border-bottom: 1px solid var(--border);
    flex-shrink: 0;
  }

  .modal-title {
    font-size: .8rem;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: .08em;
    color: var(--muted);
  }

  .modal-close {
    background: none;
    border: none;
    cursor: pointer;
    color: var(--muted);
    font-size: 1.2rem;
    line-height: 1;
    padding: 2px 6px;
    border-radius: 4px;
    transition: background .12s, color .12s;
  }
  .modal-close:hover { background: var(--bg-muted); color: var(--text); }

  .modal-body {
    overflow-y: auto;
    padding: 20px;
    flex: 1;
    scrollbar-width: thin;
  }

  /* Full ID hero */
  .modal-id {
    font-family: var(--font-mono);
    font-size: .88rem;
    font-weight: 600;
    color: var(--accent);
    word-break: break-all;
    background: var(--accent-dim);
    border: 1px solid rgba(192,38,211,.15);
    border-radius: var(--radius-sm);
    padding: 12px 14px;
    margin-bottom: 20px;
    line-height: 1.5;
  }

  /* Key-value rows */
  .detail-grid {
    display: grid;
    grid-template-columns: 140px 1fr;
    gap: 0;
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    overflow: hidden;
    margin-bottom: 20px;
  }

  .detail-key {
    padding: 9px 14px;
    background: var(--bg-subtle);
    border-bottom: 1px solid var(--border);
    font-size: .7rem;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: .08em;
    color: var(--muted);
    display: flex;
    align-items: flex-start;
  }
  .detail-key:last-of-type { border-bottom: none; }

  .detail-val {
    padding: 9px 14px;
    background: var(--bg);
    border-bottom: 1px solid var(--border);
    font-family: var(--font-mono);
    font-size: .78rem;
    color: var(--text2);
    word-break: break-all;
    display: flex;
    align-items: flex-start;
    flex-wrap: wrap;
    gap: 4px;
  }
  .detail-val:last-of-type { border-bottom: none; }

  /* Endpoint list inside modal */
  .modal-ep-tag {
    display: inline-block;
    font-family: var(--font-mono);
    font-size: .72rem;
    padding: 3px 9px;
    background: var(--bg-muted);
    border: 1px solid var(--border);
    border-radius: 4px;
    color: var(--text2);
  }

  /* ID link in table */
  .id-link {
    font-family: var(--font-mono);
    font-size: .78rem;
    color: var(--accent);
    font-weight: 500;
    max-width: 220px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    display: block;
    text-decoration: none;
    cursor: pointer;
    transition: color .12s;
  }
  .id-link:hover { color: var(--accent-hover); text-decoration: underline; text-decoration-style: dotted; }
