  @import url('https://fonts.googleapis.com/css2?family=JetBrains+Mono:wght@300;400;600&family=Syne:wght@400;700;800&display=swap');

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

  :root {
    --bg: #0a0a0f;
    --surface: #111118;
    --border: #1e1e2e;
    --accent: #7c6af7;
    --accent2: #f76a8c;
    --accent3: #6af7c8;
    --text: #c8c8d8;
    --text-dim: #8888a0;
    --text-bright: #eeeef8;
    --canvas-bg: #12122a;
    --canvas-border: #2a2a4a;
    --node-r: 6;
  }

  body.light {
    --bg: #f0f0f5;
    --surface: #ffffff;
    --border: #d0d0dd;
    --accent: #5b4bd5;
    --accent2: #d44a6a;
    --accent3: #2aab8a;
    --text: #333340;
    --text-dim: #6a6a80;
    --text-bright: #111118;
    --canvas-bg: #e8e8f0;
    --canvas-border: #c0c0d0;
  }

  body {
    background: var(--bg);
    color: var(--text);
    font-family: 'JetBrains Mono', monospace;
    height: 100vh;
    height: 100dvh;
    display: grid;
    grid-template-rows: 44px 1fr;
    grid-template-columns: 1fr 260px;
    overflow: hidden;
  }

  @media (max-width: 600px) {
    body {
      grid-template-columns: 1fr;
      grid-template-rows: auto 1fr;
    }
    header {
      flex-wrap: wrap;
      height: auto;
      padding: 6px 8px;
      gap: 4px;
    }
    header h1 { font-size: 13px; }
    header .subtitle { display: none; }
    .zoom-controls {
      width: 100%;
      flex-wrap: wrap;
      margin-left: 0;
      gap: 3px;
    }
    .zoom-label { display: none; }
    .nudge-control { margin-left: 0; flex: 1; min-width: 0; }
    .nudge-control input[type=range] { flex: 1; min-width: 50px; width: auto; }
    .nudge-control .zoom-label { display: inline; font-size: 9px; }
    .reset-btn { padding: 0 5px; font-size: 9px; }
    .reset-btn { min-width: 0; }
    .sidebar {
      position: fixed !important;
      bottom: 0; left: 0; right: 0;
      width: 100%;
      max-height: 55vh;
      overflow-y: auto;
      background: var(--surface);
      border-top: 2px solid var(--accent);
      border-radius: 14px 14px 0 0;
      z-index: 30;
      padding: 16px;
      box-shadow: 0 -8px 40px rgba(0,0,0,0.6);
      transform: translateY(110%) !important;
      transition: transform 0.25s ease;
      display: flex !important;
      flex-direction: column;
      gap: 12px;
      grid-column: unset !important;
      grid-row: unset !important;
      border-left: none;
    }
    .sidebar.open { transform: translateY(0) !important; }
    .sidebar-backdrop {
      display: none;
      position: fixed;
      inset: 0;
      background: rgba(0,0,0,0.4);
      z-index: 29;
    }
    .sidebar-backdrop.open { display: block; }
    #canvas { grid-column: 1; width: 100%; height: 100%; min-height: 0; }
    .sidebar-toggle { display: flex; }
    .hint, .hint-section { display: none; }
  }

  .sidebar-toggle {
    display: none;
    position: fixed;
    bottom: 16px;
    right: 16px;
    width: 44px;
    height: 44px;
    background: var(--accent);
    border: none;
    border-radius: 50%;
    color: #fff;
    font-size: 18px;
    cursor: pointer;
    align-items: center;
    justify-content: center;
    box-shadow: 0 2px 12px rgba(124,106,247,0.5);
    z-index: 10;
  }

  header {
    grid-column: 1 / -1;
    display: flex;
    align-items: center;
    gap: 20px;
    padding: 0 20px;
    border-bottom: 1px solid var(--border);
    background: var(--surface);
  }

  header h1 {
    font-family: 'Syne', sans-serif;
    font-size: 15px;
    font-weight: 800;
    color: var(--text-bright);
    letter-spacing: 0.05em;
  }

  header .subtitle {
    font-size: 10px;
    color: var(--text-dim);
    letter-spacing: 0.1em;
    text-transform: uppercase;
  }

  .zoom-controls {
    margin-left: auto;
    display: flex;
    align-items: center;
    gap: 6px;
    flex-shrink: 0;
  }

  .zoom-label {
    font-size: 10px;
    color: var(--text-dim);
    text-transform: uppercase;
    letter-spacing: 0.1em;
  }

  /* Stepper: ◀ L3 ▶ */
  .zoom-stepper {
    display: flex;
    align-items: center;
    gap: 0;
    border: 1px solid #2a2a3e;
    border-radius: 4px;
    overflow: hidden;
  }

  .zoom-step-btn {
    width: 26px;
    height: 24px;
    background: var(--border);
    border: none;
    color: var(--text-dim);
    font-family: 'JetBrains Mono', monospace;
    font-size: 13px;
    cursor: pointer;
    transition: all 0.12s;
    display: flex; align-items: center; justify-content: center;
  }
  .zoom-step-btn:hover { background: var(--accent); color: #fff; }
  .zoom-step-btn:disabled { opacity: 0.3; cursor: default; }

  .zoom-current {
    min-width: 38px;
    height: 24px;
    background: var(--accent);
    color: #fff;
    font-family: 'JetBrains Mono', monospace;
    font-size: 10px;
    font-weight: 600;
    display: flex; align-items: center; justify-content: center;
    letter-spacing: 0.05em;
    border-left: 1px solid rgba(255,255,255,0.15);
    border-right: 1px solid rgba(255,255,255,0.15);
  }

  .reset-btn {
    height: 24px;
    padding: 0 8px;
    background: transparent;
    border: 1px solid #2a2a3e;
    border-radius: 3px;
    color: var(--text-dim);
    font-family: 'JetBrains Mono', monospace;
    font-size: 10px;
    cursor: pointer;
    transition: all 0.15s;
    letter-spacing: 0.05em;
  }
  .reset-btn:hover { border-color: var(--accent3); color: var(--accent3); }

  #canvas {
    display: block;
    cursor: grab;
    background: var(--canvas-bg);
    border: 1px solid var(--canvas-border);
    width: 100%;
    height: 100%;
    min-height: 0; /* critical for grid 1fr to work */
    touch-action: none; /* prevent browser zoom/scroll on touch */
  }
  #canvas:active { cursor: grabbing; }

  .sidebar {
    border-left: 1px solid var(--border);
    background: var(--surface);
    overflow-y: auto;
    padding: 16px;
    display: flex;
    flex-direction: column;
    gap: 16px;
    transform: none;
    transition: none;
  }

  .sidebar::-webkit-scrollbar { width: 4px; }
  .sidebar::-webkit-scrollbar-track { background: transparent; }
  .sidebar::-webkit-scrollbar-thumb { background: var(--border); border-radius: 2px; }

  .algo-info {
    font-size: 11px;
    color: var(--text-dim);
    font-family: 'JetBrains Mono', monospace;
    padding: 8px 0;
    line-height: 1.4;
  }

  .hint {
    font-size: 10px;
    color: var(--text-dim);
    line-height: 1.5;
  }

  .panel-title {
    font-size: 9px;
    text-transform: uppercase;
    letter-spacing: 0.15em;
    color: var(--text-dim);
    padding-bottom: 8px;
    border-bottom: 1px solid var(--border);
  }

  .stat-row {
    display: flex;
    justify-content: space-between;
    align-items: baseline;
    font-size: 11px;
    padding: 3px 0;
  }
  .stat-label { color: var(--text-dim); }
  .stat-value { color: var(--text-bright); font-weight: 600; }

  .node-title {
    font-family: 'Syne', sans-serif;
    font-size: 16px;
    font-weight: 700;
    color: var(--text-bright);
    word-break: break-all;
  }

  .node-badge {
    display: inline-block;
    padding: 2px 7px;
    border-radius: 2px;
    font-size: 9px;
    letter-spacing: 0.08em;
    text-transform: uppercase;
    margin-top: 6px;
  }

  .prop-row {
    display: flex;
    flex-direction: column;
    gap: 2px;
    padding: 5px 0;
    border-bottom: 1px solid var(--border);
  }
  .prop-key { font-size: 9px; color: var(--text-dim); text-transform: uppercase; letter-spacing: 0.1em; }
  .prop-val { font-size: 12px; color: var(--text-bright); }

  .minhash-display {
    display: flex;
    flex-wrap: wrap;
    gap: 2px;
    margin-top: 4px;
  }
  .mh-bit {
    width: 10px;
    height: 10px;
    border-radius: 1px;
    transition: background 0.3s;
  }

  .neighbor-list {
    display: flex;
    flex-direction: column;
    gap: 4px;
  }
  .neighbor-item {
    font-size: 11px;
    color: var(--text-dim);
    cursor: pointer;
    padding: 3px 6px;
    border-radius: 2px;
    border: 1px solid transparent;
    transition: all 0.15s;
    display: flex;
    justify-content: space-between;
  }
  .neighbor-item:hover { border-color: var(--border); color: var(--text); }

  .nudge-control {
    display: flex;
    align-items: center;
    gap: 6px;
    margin-left: 12px;
  }

  .nudge-control input[type=range] {
    -webkit-appearance: none;
    width: 80px;
    height: 3px;
    background: var(--border);
    border-radius: 2px;
    outline: none;
  }

  .nudge-control input[type=range]::-webkit-slider-thumb {
    -webkit-appearance: none;
    width: 12px;
    height: 12px;
    border-radius: 50%;
    background: var(--accent3);
    cursor: pointer;
  }

  .nudge-val {
    font-size: 10px;
    color: var(--accent3);
    min-width: 24px;
  }

  @media (max-width: 600px) {
    .nudge-control { margin-left: 0; }
  }
    color: var(--text-dim);
    line-height: 1.6;
    letter-spacing: 0.03em;
  }

  .bucket-id {
    font-size: 10px;
    color: var(--accent);
    font-family: 'JetBrains Mono', monospace;
    word-break: break-all;
    line-height: 1.6;
  }

  .weight-row {
    display: flex;
    align-items: center;
    gap: 6px;
    padding: 3px 0;
  }
  .weight-row input[type=checkbox] {
    -webkit-appearance: none;
    appearance: none;
    width: 12px;
    height: 12px;
    margin: 0;
    flex-shrink: 0;
    border: 1px solid var(--border);
    border-radius: 2px;
    background: transparent;
    cursor: pointer;
    position: relative;
    transition: all 0.12s;
  }
  .weight-row input[type=checkbox]:hover {
    border-color: var(--accent);
  }
  .weight-row input[type=checkbox]:checked {
    background: var(--accent);
    border-color: var(--accent);
  }
  .weight-row input[type=checkbox]:checked::after {
    content: '';
    position: absolute;
    left: 3px;
    top: 1px;
    width: 4px;
    height: 7px;
    border: solid #fff;
    border-width: 0 1.5px 1.5px 0;
    transform: rotate(45deg);
  }
  .weight-label {
    font-size: 10px;
    color: var(--text-dim);
    width: 60px;
    text-transform: uppercase;
    letter-spacing: 0.06em;
    flex-shrink: 0;
  }
  .weight-slider {
    -webkit-appearance: none;
    flex: 1;
    height: 3px;
    background: var(--border);
    border-radius: 2px;
    outline: none;
  }
  .weight-slider::-webkit-slider-thumb {
    -webkit-appearance: none;
    width: 11px; height: 11px;
    border-radius: 50%;
    background: var(--accent3);
    cursor: pointer;
  }
  .weight-val {
    font-size: 10px;
    color: var(--accent3);
    width: 14px;
    text-align: right;
    flex-shrink: 0;
  }
  .preset-row {
    display: flex;
    gap: 4px;
    flex-wrap: wrap;
    margin-bottom: 6px;
  }
  .preset-btn {
    font-family: 'JetBrains Mono', monospace;
    font-size: 9px;
    padding: 3px 7px;
    border-radius: 3px;
    border: 1px solid var(--border);
    background: transparent;
    color: var(--text-dim);
    cursor: pointer;
    letter-spacing: 0.05em;
    text-transform: uppercase;
    transition: all 0.12s;
  }
  .preset-btn:hover { border-color: var(--accent); color: var(--accent); }
  .preset-btn.active { background: var(--accent); border-color: var(--accent); color: #fff; }
    font-size: 10px;
    color: var(--text-dim);
    line-height: 1.7;
    border-left: 2px solid var(--accent);
    padding-left: 8px;
  }

  .loader-screen {
    grid-column: 1 / -1;
    grid-row: 2;
    display: flex;
    align-items: center;
    justify-content: center;
    background: var(--bg);
    z-index: 20;
  }
  .loader-screen.hidden { display: none; }

  .drop-zone {
    border: 2px dashed var(--border);
    border-radius: 12px;
    padding: 48px 40px;
    text-align: center;
    max-width: 520px;
    width: 90%;
    transition: border-color 0.2s, background 0.2s;
  }
  .drop-zone.dragover {
    border-color: var(--accent);
    background: rgba(124,106,247,0.06);
  }
  .drop-title {
    font-family: 'Syne', sans-serif;
    font-size: 22px;
    font-weight: 800;
    color: var(--text-bright);
    margin-bottom: 8px;
  }
  .drop-hint {
    font-size: 11px;
    color: var(--text-dim);
    margin-bottom: 24px;
    line-height: 1.6;
  }
  .file-input-row {
    display: flex;
    gap: 16px;
    justify-content: center;
    margin-bottom: 20px;
    flex-wrap: wrap;
  }
  .file-input-row label {
    font-size: 10px;
    color: var(--text-dim);
    text-transform: uppercase;
    letter-spacing: 0.08em;
    display: flex;
    flex-direction: column;
    gap: 6px;
    align-items: center;
  }
  .file-input-row input[type=file] {
    font-family: 'JetBrains Mono', monospace;
    font-size: 10px;
    color: var(--text);
    width: 190px;
  }
  .file-input-row input[type=file]::file-selector-button {
    font-family: 'JetBrains Mono', monospace;
    font-size: 10px;
    padding: 4px 10px;
    border-radius: 3px;
    border: 1px solid var(--border);
    background: var(--surface);
    color: var(--text);
    cursor: pointer;
    margin-right: 8px;
  }
  .file-input-row input[type=file]::file-selector-button:hover {
    border-color: var(--accent);
    color: var(--accent);
  }
  .load-btn {
    font-family: 'JetBrains Mono', monospace;
    font-size: 11px;
    padding: 8px 28px;
    border-radius: 4px;
    border: 1px solid var(--accent);
    background: var(--accent);
    color: #fff;
    cursor: pointer;
    letter-spacing: 0.06em;
    text-transform: uppercase;
    transition: all 0.15s;
  }
  .load-btn:hover { background: #6a58e0; }
  .load-btn:disabled { opacity: 0.3; cursor: default; }
  .load-status {
    font-size: 10px;
    color: var(--text-dim);
    margin-top: 16px;
    min-height: 18px;
  }
  .load-status.error { color: var(--accent2); }

  .dataset-list {
    display: flex;
    flex-wrap: wrap;
    gap: 6px;
    justify-content: center;
    margin-bottom: 20px;
  }
  .dataset-btn {
    font-family: 'JetBrains Mono', monospace;
    font-size: 9px;
    padding: 4px 8px;
    border-radius: 3px;
    border: 1px solid var(--border);
    background: transparent;
    color: var(--text-dim);
    cursor: pointer;
    letter-spacing: 0.04em;
    transition: all 0.12s;
  }
  .dataset-btn:hover { border-color: var(--accent); color: var(--accent); }
  .dataset-btn:disabled { opacity: 0.3; cursor: default; }


  progress {
    -webkit-appearance: none;
    appearance: none;
    border: none;
    border-radius: 2px;
    overflow: hidden;
  }
  progress::-webkit-progress-bar { background: var(--border); border-radius: 2px; }
  progress::-webkit-progress-value { background: var(--accent); border-radius: 2px; transition: width 0.2s; }
  progress::-moz-progress-bar { background: var(--accent); border-radius: 2px; }

  .node-panel {
    position: fixed;
    top: 44px;
    left: 0;
    width: 300px;
    bottom: 0;
    background: var(--surface);
    border-right: 1px solid var(--border);
    overflow-y: auto;
    padding: 16px;
    display: flex;
    flex-direction: column;
    gap: 12px;
    z-index: 15;
    transform: translateX(-100%);
    transition: transform 0.2s ease;
    box-shadow: 4px 0 20px rgba(0,0,0,0.4);
  }
  .node-panel.open {
    transform: translateX(0);
  }
  .node-panel::-webkit-scrollbar { width: 4px; }
  .node-panel::-webkit-scrollbar-track { background: transparent; }
  .node-panel::-webkit-scrollbar-thumb { background: var(--border); border-radius: 2px; }

  .node-panel-close {
    position: absolute;
    top: 8px;
    right: 8px;
    width: 24px;
    height: 24px;
    background: transparent;
    border: 1px solid var(--border);
    border-radius: 3px;
    color: var(--text-dim);
    font-size: 14px;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.12s;
  }
  .node-panel-close:hover { border-color: var(--accent2); color: var(--accent2); }

  @media (max-width: 600px) {
    .node-panel {
      width: 100%;
      top: auto;
      bottom: 0;
      left: 0;
      right: 0;
      max-height: 55vh;
      border-right: none;
      border-top: 2px solid var(--accent);
      border-radius: 14px 14px 0 0;
      transform: translateY(100%);
      box-shadow: 0 -8px 40px rgba(0,0,0,0.6);
    }
    .node-panel.open { transform: translateY(0); }
  }
