*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
    body { background: #0a0b0d; color: #e2e8f0; font-family: 'Inter', sans-serif; overflow-x: hidden; }
    .skip-link { position: absolute; top: -100px; left: 0; background: #6366f1; color: #fff; padding: 0.6rem 1rem; border-radius: 0 0 6px 0; z-index: 999; font-weight: 600; font-size: 0.85rem; text-decoration: none; transition: top 0.15s; }
    .skip-link:focus { top: 0; outline: 2px solid #fff; outline-offset: -4px; }
    @media (prefers-reduced-motion: reduce) {
      *, *::before, *::after { animation-duration: 0.01ms !important; transition-duration: 0.01ms !important; }
    }

    #app-container {
      max-width: 1400px;
      margin: 4rem auto;
      height: 80vh;
      min-height: 600px;
      border: 1px solid rgba(255,255,255,0.1);
      border-radius: 16px;
      overflow: hidden;
      box-shadow: 0 40px 100px rgba(0,0,0,0.6);
      position: relative;
      z-index: 10;
    }

    #app { display: flex; height: 100%; width: 100%; }

    /* ── SIDEBAR ── */
    #sidebar {
      width: 320px; min-width: 320px; height: 100%;
      background: rgba(6, 6, 14, 0.95); border-right: 1px solid #18183a;
      display: flex; flex-direction: column;
      position: relative;
    }
    
    #timeline-wrap {
      flex: 1;
      overflow-y: auto;
      padding: 1.5rem 1rem;
      scrollbar-width: none;
    }
    #timeline-wrap::-webkit-scrollbar { display: none; }
    
    .tl-node {
      display: flex; gap: 1rem; position: relative; padding-bottom: 2rem; cursor: pointer;
      opacity: 0.6; transition: opacity 0.3s, transform 0.3s;
    }
    .tl-node:hover { opacity: 1; transform: translateX(5px); }
    .tl-node.active { opacity: 1; }
    
    .tl-line {
      width: 2px; background: #18183a; position: absolute; left: 24px; top: 24px; bottom: 0;
    }
    .tl-node:last-child .tl-line { display: none; }
    
    .tl-year {
      width: 48px; font-family: 'Space Grotesk', monospace; font-size: 0.75rem; color: #818cf8;
      text-align: right; font-weight: 700; flex-shrink: 0; padding-top: 3px;
    }
    
    .tl-dot {
      width: 12px; height: 12px; border-radius: 50%; background: #06060e;
      border: 2px solid #3a3a6a; position: relative; z-index: 2; margin-top: 4px;
      transition: all 0.3s;
    }
    .tl-node:hover .tl-dot { border-color: #fbbf24; background: #fbbf24; box-shadow: 0 0 10px rgba(251,191,36,0.5); }
    .tl-node.active .tl-dot { border-color: #3b82f6; background: #3b82f6; box-shadow: 0 0 15px rgba(59,130,246,0.5); }
    
    .tl-content { flex: 1; }
    .tl-name { font-weight: 600; color: #e2e8f0; font-size: 0.9rem; margin-bottom: 0.25rem; }
    .tl-creator { font-size: 0.7rem; color: #5c5c8a; font-family: 'Space Grotesk', sans-serif; }

    #sidebar::-webkit-scrollbar { width: 4px; }
    #sidebar::-webkit-scrollbar-thumb { background: #2a2a50; border-radius: 2px; }

    .sb-logo {
      padding: 0.85rem 1rem;
      font-family: 'Space Grotesk', sans-serif;
      font-size: 0.82rem; font-weight: 700; color: #818cf8;
      letter-spacing: 0.06em; border-bottom: 1px solid #18183a;
    }

    .sb-section { padding: 0.6rem 0.4rem 0.1rem; }
    .sb-section-title {
      font-size: 0.6rem; font-weight: 600; letter-spacing: 0.14em;
      text-transform: uppercase; color: #3a3a6a;
      padding: 0 0.6rem; margin-bottom: 0.2rem;
    }

    .sb-btn {
      display: block; width: 100%; background: none; border: none; cursor: pointer;
      text-align: left; padding: 0.38rem 0.75rem;
      font-family: 'Inter', sans-serif; font-size: 0.78rem;
      color: #7070a0; border-radius: 6px; transition: all 0.15s;
    }
    .sb-btn:hover { background: #10102a; color: #c0c0e0; }
    .sb-btn.active { background: rgba(99,102,241,0.18); color: #a5b4fc; font-weight: 600; }
    .sb-btn .yr { font-size: 0.62rem; color: #4c4c7a; margin-left: auto; padding-left: 6px; font-family: 'Space Grotesk', sans-serif; flex-shrink: 0; }
    .sb-btn { display: flex; align-items: center; }
    .sb-btn .nm { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

    /* Search */
    .sb-search { padding: 0.55rem 0.75rem 0; position: relative; }
    .sb-search input {
      width: 100%; background: #0a0a1a; border: 1px solid #18183a;
      color: #c0c0e0; padding: 0.42rem 0.65rem 0.42rem 1.85rem;
      border-radius: 8px; font-size: 0.78rem; font-family: 'Inter', sans-serif;
      transition: border-color 0.15s, background 0.15s;
    }
    .sb-search input:focus { outline: none; border-color: rgba(99,102,241,0.5); background: #10102a; }
    .sb-search input::placeholder { color: #4c4c7a; }
    .sb-search-icon { position: absolute; left: 1.4rem; top: 50%; transform: translateY(-50%); color: #4c4c7a; pointer-events: none; font-size: 0.85rem; }
    .sb-search-clear { position: absolute; right: 1.4rem; top: 50%; transform: translateY(-50%); background: none; border: none; color: #4c4c7a; cursor: pointer; font-size: 0.85rem; padding: 2px 5px; border-radius: 4px; display: none; }
    .sb-search-clear:hover { color: #c0c0e0; background: #18183a; }
    .sb-search.has-value .sb-search-clear { display: block; }
    .sb-empty { padding: 1rem 0.95rem; color: #4c4c7a; font-size: 0.72rem; font-style: italic; text-align: center; }

    /* Step button */
    .ctrl-btn.step { background: rgba(167,139,250,0.14); border-color: rgba(167,139,250,0.35); color: #c4b5fd; }
    .ctrl-btn.step:hover { background: rgba(167,139,250,0.28); }
    .ctrl-btn.step:disabled { opacity: 0.35; cursor: not-allowed; transform: none !important; }

    /* Keyboard shortcuts overlay */
    #kbd-overlay {
      position: fixed; inset: 0; background: rgba(0,0,0,0.78); backdrop-filter: blur(8px);
      z-index: 200; display: none; align-items: center; justify-content: center; padding: 1.5rem;
      animation: kbdFade 0.2s ease;
    }
    @keyframes kbdFade { from { opacity: 0; } to { opacity: 1; } }
    #kbd-overlay.show { display: flex; }
    .kbd-card {
      background: linear-gradient(180deg, #0a0a1a, #060614); border: 1px solid #2a2a50;
      border-radius: 14px; padding: 1.6rem 1.8rem; max-width: 460px; width: 100%;
      box-shadow: 0 30px 80px rgba(0,0,0,0.6);
    }
    .kbd-card h3 { font-family: 'Space Grotesk', sans-serif; font-size: 1.1rem; font-weight: 700; color: #fff; margin-bottom: 1rem; display: flex; align-items: center; gap: 0.5rem; }
    .kbd-list { display: flex; flex-direction: column; gap: 0.55rem; margin-bottom: 1.1rem; }
    .kbd-row { display: flex; align-items: center; justify-content: space-between; gap: 1rem; }
    .kbd-desc { font-size: 0.85rem; color: #c0c0e8; }
    .kbd-keys { display: flex; gap: 4px; flex-shrink: 0; }
    kbd {
      font-family: 'JetBrains Mono', monospace; font-size: 0.72rem; font-weight: 600;
      background: #18183a; color: #a5b4fc; padding: 3px 8px; border-radius: 5px;
      border: 1px solid #2a2a50; border-bottom-width: 2px; min-width: 26px; text-align: center;
    }
    .kbd-close { display: block; width: 100%; background: rgba(99,102,241,0.18); border: 1px solid rgba(99,102,241,0.4); color: #a5b4fc; padding: 0.6rem; border-radius: 8px; font-size: 0.78rem; font-weight: 600; cursor: pointer; transition: all 0.15s; }
    .kbd-close:hover { background: rgba(99,102,241,0.3); }
    .kbd-hint {
      position: fixed; bottom: 1rem; left: 50%; transform: translateX(-50%);
      background: rgba(7,7,15,0.92); border: 1px solid #18183a; color: #6c7299;
      font-size: 0.7rem; padding: 0.45rem 1rem; border-radius: 999px;
      z-index: 50; backdrop-filter: blur(6px);
      animation: kbdHintFade 5s ease forwards;
    }
    @keyframes kbdHintFade { 0% { opacity: 0; transform: translate(-50%, 8px); } 8%, 80% { opacity: 1; transform: translate(-50%, 0); } 100% { opacity: 0; transform: translate(-50%, -8px); pointer-events: none; } }

    /* First-time onboarding card */
    #onboard-hint { position: fixed; right: 1.2rem; bottom: 1.2rem; z-index: 180; animation: obIn 0.35s ease-out; }
    @keyframes obIn { from { opacity: 0; transform: translateY(20px); } to { opacity: 1; transform: translateY(0); } }
    .ob-card { background: linear-gradient(180deg, #0a0a1a, #06060e); border: 1px solid rgba(99,102,241,0.4); border-radius: 12px; padding: 1.1rem 1.25rem; max-width: 280px; box-shadow: 0 20px 50px rgba(0,0,0,0.6); }
    .ob-h { font-family: 'Space Grotesk', sans-serif; font-size: 0.85rem; font-weight: 700; color: #a5b4fc; margin-bottom: 0.75rem; letter-spacing: 0.02em; }
    .ob-row { display: flex; align-items: center; gap: 8px; font-size: 0.75rem; color: #c0c0e0; margin-bottom: 0.3rem; }
    .ob-row kbd { font-family: 'JetBrains Mono', monospace; font-size: 0.66rem; background: #18183a; color: #a5b4fc; padding: 1px 6px; border-radius: 4px; border: 1px solid #2a2a50; border-bottom-width: 2px; min-width: 22px; text-align: center; flex-shrink: 0; }
    .ob-close { margin-top: 0.85rem; width: 100%; background: rgba(99,102,241,0.18); border: 1px solid rgba(99,102,241,0.4); color: #a5b4fc; padding: 0.42rem; border-radius: 6px; font-size: 0.72rem; font-weight: 600; cursor: pointer; transition: all 0.15s; font-family: 'Inter', sans-serif; }
    .ob-close:hover { background: rgba(99,102,241,0.3); }

    /* View toggle */
    .view-toggle { display: flex; gap: 4px; padding: 0.55rem 0.75rem 0.4rem; }
    .vt-btn { flex: 1; background: #0a0a1a; border: 1px solid #18183a; color: #5c5c8a; padding: 0.32rem 0.5rem; font-size: 0.66rem; font-weight: 600; letter-spacing: 0.06em; text-transform: uppercase; border-radius: 6px; cursor: pointer; font-family: 'Inter', sans-serif; transition: all 0.15s; }
    .vt-btn:hover { color: #c0c0e0; border-color: #2a2a50; }
    .vt-btn.active { background: rgba(99,102,241,0.18); border-color: rgba(99,102,241,0.4); color: #a5b4fc; }

    /* Era headers in chronological view */
    .era-hdr { font-size: 0.58rem; font-weight: 700; letter-spacing: 0.16em; text-transform: uppercase; color: #4c4c7a; padding: 0.7rem 0.95rem 0.25rem; }
    .era-hdr:first-child { padding-top: 0.4rem; }

    /* Timeline Overlay */
    #timeline-overlay {
      position: absolute; inset: 0; background: rgba(4,4,17,0.95); backdrop-filter: blur(8px);
      z-index: 100; display: none; flex-direction: column; padding: 2rem;
    }
    #timeline-overlay.show { display: flex; }
    .tl-svg-wrap { flex: 1; position: relative; overflow-x: auto; overflow-y: hidden; scrollbar-width: thin; scrollbar-color: #2a2a50 transparent; }
    .tl-svg-wrap::-webkit-scrollbar { height: 6px; }
    .tl-svg-wrap::-webkit-scrollbar-thumb { background: #2a2a50; border-radius: 3px; }
    .tl-node { cursor: pointer; transition: all 0.2s; }
    .tl-node:hover circle { fill: #a5b4fc; filter: drop-shadow(0 0 10px #6366f1); }
    .tl-node:hover text { fill: #fff; }

    /* Info panel additions */
    .info-meta { display: flex; gap: 6px; margin-bottom: 0.55rem; flex-wrap: wrap; }
    .info-year { font-family: 'Space Grotesk', sans-serif; font-size: 0.7rem; font-weight: 700; color: #fbbf24; background: rgba(251,191,36,0.08); border: 1px solid rgba(251,191,36,0.25); padding: 1px 8px; border-radius: 999px; letter-spacing: 0.04em; }
    .info-creator { font-size: 0.7rem; color: #818cf8; background: rgba(99,102,241,0.08); border: 1px solid rgba(99,102,241,0.22); padding: 1px 8px; border-radius: 999px; }
    .algo-history { margin-top: 0.6rem; padding-top: 0.6rem; border-top: 1px dashed #18183a; color: #52527a; font-style: italic; font-size: 0.66rem; line-height: 1.55; }

    /* ── INFO PANEL ── */
    .algo-info {
      margin-top: auto; padding: 0.75rem;
      border-top: 1px solid #18183a; font-size: 0.71rem; color: #5c5c8a;
    }
    .algo-info-name {
      font-family: 'Space Grotesk', sans-serif;
      font-size: 0.84rem; font-weight: 700; color: #a5b4fc; margin-bottom: 0.55rem;
    }
    .cx-row { display: flex; justify-content: space-between; margin-bottom: 0.22rem; }
    .cx-lbl { color: #3a3a6a; }
    .cx-val { color: #c0c0e8; font-family: 'Space Grotesk', sans-serif; font-size: 0.72rem; }
    .algo-desc { margin-top: 0.55rem; color: #4c4c80; line-height: 1.55; font-size: 0.68rem; }

    /* ── MAIN ── */
    #main { flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center; height: 100%; min-width: 0; background: #0a0a1a; padding: 2rem; overflow-y: auto; }
    #canvas-wrap { width: 100%; max-width: 500px; aspect-ratio: 1/1; position: relative; overflow: hidden; border-radius: 16px; box-shadow: 0 20px 50px rgba(0,0,0,0.5); border: 1px solid rgba(255,255,255,0.05); background: #000; flex-shrink: 0; }
    #three-canvas { display: block; width: 100%; height: 100%; }
    #code-panel { width: 100%; max-width: 500px; margin-top: 1.5rem; flex-shrink: 0; }

    /* overlay labels */
    #algo-label {
      position: absolute; top: 0.9rem; left: 50%; transform: translateX(-50%);
      font-family: 'Space Grotesk', sans-serif;
      font-size: 0.88rem; font-weight: 700;
      color: rgba(165,180,252,0.65); pointer-events: none; white-space: nowrap;
    }

    #legend {
      position: absolute; bottom: 0.5rem; right: 0.75rem;
      display: flex; gap: 0.65rem; flex-wrap: wrap; justify-content: flex-end;
    }
    .leg { display: flex; align-items: center; gap: 4px; font-size: 0.62rem; color: #4c4c7a; }
    .leg-dot { width: 8px; height: 8px; border-radius: 2px; flex-shrink: 0; }

    /* ── CODE PANEL (permanent right column) ── */
    #code-panel {
      width: 360px; min-width: 360px; height: 100%;
      background: linear-gradient(180deg, #06060e, #07070f);
      border-left: 1px solid #18183a;
      display: flex; flex-direction: column;
      transition: width 0.28s ease, min-width 0.28s ease;
    }
    #code-panel.collapsed { width: 0; min-width: 0; border-left: none; overflow: hidden; }
    #code-header {
      display: flex; align-items: center; justify-content: space-between;
      padding: 0.7rem 0.95rem; border-bottom: 1px solid #18183a;
      background: #07070f; flex-shrink: 0;
    }
    .ch-left { display: flex; align-items: center; gap: 8px; min-width: 0; }
    #code-header h3 { font-family: 'Space Grotesk', sans-serif; font-size: 0.78rem; font-weight: 700; color: #a5b4fc; letter-spacing: 0.04em; margin: 0; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
    .ch-tag { font-family: 'Space Grotesk', sans-serif; font-size: 0.62rem; font-weight: 700; color: #fbbf24; background: rgba(251,191,36,0.1); border: 1px solid rgba(251,191,36,0.25); padding: 1px 7px; border-radius: 999px; flex-shrink: 0; }
    #code-close, #code-copy { background: none; border: 1px solid #18183a; color: #5c5c8a; font-size: 0.78rem; cursor: pointer; padding: 3px 8px; border-radius: 6px; transition: all 0.15s; min-width: 26px; min-height: 24px; }
    #code-close:hover, #code-copy:hover { color: #e2e8f0; border-color: #2a2a50; }
    #code-copy.ok { color: #60a5fa; border-color: rgba(96,165,250,0.5); }
    .ch-actions { display: flex; gap: 5px; align-items: center; }
    #code-body {
      flex: 1; overflow-y: auto; padding: 0.65rem 0;
      font-family: 'JetBrains Mono', 'Menlo', monospace; font-size: 0.74rem; line-height: 1.75;
      scrollbar-width: thin; scrollbar-color: #2a2a50 transparent;
    }
    #code-body::-webkit-scrollbar { width: 4px; }
    #code-body::-webkit-scrollbar-thumb { background: #2a2a50; border-radius: 2px; }
    .code-line {
      display: flex; padding: 1px 0.95rem; color: #6c7299; transition: all 0.18s;
    }
    .code-line.active {
      background: linear-gradient(90deg, rgba(99,102,241,0.32), rgba(99,102,241,0.04));
      color: #fff;
      border-left: 3px solid #818cf8;
      padding-left: calc(0.95rem - 3px);
      box-shadow: 0 0 18px rgba(99,102,241,0.18) inset;
    }
    .code-line.active .code-num { color: #818cf8; }
    .code-num {
      display: inline-block; width: 22px; flex-shrink: 0; color: #2e3057; text-align: right; padding-right: 10px;
      user-select: none; font-variant-numeric: tabular-nums;
    }
    .code-empty { padding: 1rem; color: #4c4c7a; font-style: italic; font-size: 0.72rem; }

    /* Variable inspector */
    #var-panel {
      border-top: 1px solid #18183a; background: #050511;
      padding: 0.55rem 0.95rem 0.7rem; min-height: 80px; max-height: 32%;
      overflow-y: auto; flex-shrink: 0;
    }
    .vp-title { font-size: 0.6rem; font-weight: 700; letter-spacing: 0.14em; text-transform: uppercase; color: #4c4c7a; margin-bottom: 0.4rem; }
    .vp-list { display: flex; flex-direction: column; gap: 3px; }
    .vp-row { display: flex; gap: 8px; font-size: 0.72rem; font-family: 'JetBrains Mono', monospace; }
    .vp-key { color: #818cf8; min-width: 80px; }
    .vp-val { color: #e2e8f0; word-break: break-all; }
    .vp-empty { font-style: italic; color: #3a3a6a; font-size: 0.66rem; }

    /* Code toggle button (overlaid on canvas) */
    #code-toggle {
      position: absolute; top: 0.85rem; right: 0.85rem; z-index: 11;
      background: rgba(7,7,15,0.85); border: 1px solid #2a2a50;
      color: #a5b4fc; padding: 0.42rem 0.8rem; border-radius: 8px;
      font-size: 0.74rem; font-weight: 600; cursor: pointer;
      font-family: 'Inter', sans-serif; transition: all 0.15s;
      display: none; align-items: center; gap: 6px;
    }
    #code-toggle.show { display: flex; }
    #code-toggle:hover { background: rgba(99,102,241,0.18); border-color: rgba(99,102,241,0.4); }

    @media (max-width: 1200px) {
      #code-panel { width: 320px; min-width: 320px; }
    }
    @media (max-width: 1000px) {
      #code-panel { width: 280px; min-width: 280px; }
      #sidebar { width: 180px; min-width: 180px; }
    }
    @media (max-width: 760px) {
      #code-panel { width: 0; min-width: 0; border-left: none; }
      #code-panel.show-mobile { width: 100%; min-width: 100%; position: absolute; right: 0; top: 0; z-index: 50; border-left: 1px solid #18183a; }
      #code-toggle { display: flex; }
    }

    /* ── CONTROL BAR ── */
    #ctrl-bar {
      height: 68px; min-height: 68px; background: #06060e;
      border-top: 1px solid #18183a;
      display: flex; align-items: center; gap: 0.75rem; padding: 0 1.2rem;
      flex-wrap: nowrap; overflow-x: auto;
    }

    .ctrl-btn {
      background: rgba(99,102,241,0.14); border: 1px solid rgba(99,102,241,0.35);
      color: #a5b4fc; padding: 0.42rem 1rem;
      border-radius: 8px; font-size: 0.76rem; font-weight: 600;
      cursor: pointer; font-family: 'Inter', sans-serif;
      transition: all 0.15s; white-space: nowrap; flex-shrink: 0;
    }
    .ctrl-btn:hover { background: rgba(99,102,241,0.28); transform: translateY(-1px); }
    .ctrl-btn.run  { background: #6366f1; color: #fff; border-color: #6366f1; }
    .ctrl-btn.run:hover { background: #4f52e8; }
    .ctrl-btn.stop { background: rgba(239,68,68,0.14); border-color: rgba(239,68,68,0.35); color: #fca5a5; }

    .speed-wrap { display: flex; align-items: center; gap: 0.45rem; flex-shrink: 0; }
    .speed-lbl { font-size: 0.7rem; color: #4c4c7a; white-space: nowrap; }
    input[type=range] { accent-color: #6366f1; width: 80px; cursor: pointer; }

    #target-wrap {
      display: none; align-items: center; gap: 0.45rem;
      font-size: 0.76rem; color: #4c4c7a; flex-shrink: 0;
    }
    #target-input {
      background: #10102a; border: 1px solid #2a2a50; color: #c0c0e8;
      padding: 0.28rem 0.55rem; border-radius: 6px; font-size: 0.76rem;
      width: 56px; font-family: 'Inter', sans-serif;
    }

    .stats-wrap { margin-left: auto; display: flex; gap: 1.2rem; flex-shrink: 0; }
    .stat { text-align: center; }
    .stat-n {
      font-family: 'Space Grotesk', sans-serif;
      font-size: 1.05rem; font-weight: 700; color: #a5b4fc;
    }
    .stat-l { font-size: 0.58rem; color: #3a3a6a; text-transform: uppercase; letter-spacing: 0.08em; margin-top: 1px; }
