/* ============================================
   Rust Memory Visualizer — "Ferris" Theme
   Inspired by Rust's warm orange palette
   ============================================ */

@keyframes flow-right {
  to { stroke-dashoffset: -16; } /* matches dasharray sum (6+10) for seamless loop */
}

@keyframes entry-slide-in {
  from { opacity: 0; transform: translateY(6px); }
  to { opacity: 1; transform: translateY(0); }
}

@keyframes segment-fade-in {
  from { opacity: 0; }
  to { opacity: 1; }
}

@keyframes bg-warmth {
  from { opacity: 0; }
  to { opacity: 1; }
}

:root {
  /* Rust-inspired warm palette */
  --bg-0: #121010;
  --bg-1: #181414;
  --bg-2: #1e1a1a;
  --bg-3: #262020;
  --bg-4: #2e2626;
  --bg-hover: #332b2b;
  --border-0: #2a2222;
  --border-1: #3a2e2e;
  --border-2: #4a3c3c;

  /* Warm text — slightly cream/rust tinted */
  --text-0: #ede8e2;
  --text-1: #b5aea5;
  --text-2: #887f74;
  --text-3: #5e554c;

  /* Segment colors */
  --seg-stack: #4ecdc4;
  --seg-heap: #ff6b6b;
  --seg-rodata: #c3a6ff;
  --seg-data: #ffd93d;
  --seg-bss: #6bcb77;
  --seg-text: #8a8078;

  /* Segment glows */
  --glow-stack: rgba(78, 205, 196, 0.25);
  --glow-heap: rgba(255, 107, 107, 0.25);
  --glow-rodata: rgba(195, 166, 255, 0.25);
  --glow-data: rgba(255, 217, 61, 0.25);
  --glow-bss: rgba(107, 203, 119, 0.25);
  --glow-text: rgba(138, 128, 120, 0.15);

  /* Rust accent — the signature Rust orange */
  --accent: #CE422B;
  --accent-hover: #b5381f;
  --accent-light: #e8563c;
  --danger: #ff6b6b;

  /* Sizing */
  --radius: 6px;
  --radius-sm: 3px;
  --ease: cubic-bezier(0.25, 0.46, 0.45, 0.94);
}

/* ============================================
   Reset
   ============================================ */

*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
html, body { height: 100%; overflow: hidden; }

body {
  font-family: 'DM Sans', -apple-system, system-ui, sans-serif;
  background: var(--bg-0);
  color: var(--text-0);
  display: flex;
  flex-direction: column;
  -webkit-font-smoothing: antialiased;
  /* Subtle warm vignette on the whole page */
  background-image:
    radial-gradient(ellipse at 15% 10%, rgba(206, 66, 43, 0.04) 0%, transparent 50%),
    radial-gradient(ellipse at 85% 90%, rgba(206, 66, 43, 0.03) 0%, transparent 50%);
}

/* ============================================
   Header
   ============================================ */

#app-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 20px;
  height: 52px;
  background: var(--bg-1);
  border-bottom: 1px solid var(--border-0);
  flex-shrink: 0;
  z-index: 100;
}

.header-left { display: flex; align-items: center; }

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

.logo-mark {
  font-family: 'JetBrains Mono', monospace;
  font-size: 18px;
  font-weight: 700;
  color: var(--text-2);
  letter-spacing: -1px;
  line-height: 1;
}

.logo-bracket { color: var(--text-3); }
.logo-ptr { color: var(--accent-light); }

.logo h1 {
  font-family: 'Outfit', sans-serif;
  font-size: 17px;
  font-weight: 700;
  color: var(--text-0);
  letter-spacing: -0.03em;
}

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

.header-btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 7px 14px;
  background: var(--bg-3);
  color: var(--text-1);
  border: 1px solid var(--border-1);
  border-radius: var(--radius);
  font-size: 13px;
  font-family: 'DM Sans', sans-serif;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.15s var(--ease);
  white-space: nowrap;
}

.header-btn:hover {
  background: var(--bg-4);
  color: var(--text-0);
  border-color: var(--border-2);
}

.header-btn kbd {
  font-family: 'JetBrains Mono', monospace;
  font-size: 10px;
  color: var(--text-3);
  background: var(--bg-1);
  padding: 2px 5px;
  border-radius: 3px;
  border: 1px solid var(--border-0);
  line-height: 1;
}

.header-btn.primary {
  background: var(--accent);
  color: #fff;
  border-color: var(--accent);
  font-weight: 600;
  text-shadow: 0 1px 2px rgba(0,0,0,0.2);
}

.header-btn.primary:hover {
  background: var(--accent-light);
  border-color: var(--accent-light);
}

.header-btn.primary:active {
  transform: scale(0.97);
}

.header-btn.primary kbd {
  background: rgba(255,255,255,0.15);
  border-color: rgba(255,255,255,0.2);
  color: rgba(255,255,255,0.6);
}

.header-btn.icon-btn {
  width: 34px;
  height: 34px;
  padding: 0;
  display: flex;
  align-items: center;
  justify-content: center;
}

/* Dropdown */

.example-dropdown { position: relative; }

.dropdown-menu {
  position: absolute;
  top: calc(100% + 6px);
  right: 0;
  min-width: 280px;
  background: var(--bg-2);
  border: 1px solid var(--border-1);
  border-radius: var(--radius);
  box-shadow: 0 12px 40px rgba(0, 0, 0, 0.5);
  z-index: 200;
  overflow: hidden;
}

.dropdown-menu.hidden { display: none; }

.dropdown-item {
  display: block;
  width: 100%;
  padding: 10px 14px;
  background: none;
  border: none;
  color: var(--text-1);
  font-size: 13px;
  font-family: 'DM Sans', sans-serif;
  text-align: left;
  cursor: pointer;
  transition: all 0.12s var(--ease);
  border-bottom: 1px solid var(--border-0);
}

.dropdown-item:last-child { border-bottom: none; }
.dropdown-item:hover { background: var(--bg-hover); color: var(--text-0); }

.dropdown-item .item-title {
  font-weight: 600;
  color: var(--text-0);
  font-size: 13px;
}

.dropdown-item .item-desc {
  font-size: 11px;
  color: var(--text-3);
  margin-top: 2px;
}

/* ============================================
   Main Layout
   ============================================ */

#app-main {
  display: flex;
  flex: 1;
  min-height: 0;
  overflow: hidden;
}

/* ============================================
   Editor Panel
   ============================================ */

#editor-panel {
  width: 42%;
  min-width: 300px;
  display: flex;
  flex-direction: column;
  border-right: 1px solid var(--border-0);
}

.panel-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 6px 16px;
  height: 32px;
  background: var(--bg-1);
  border-bottom: 1px solid var(--border-0);
  flex-shrink: 0;
}

.panel-title {
  font-family: 'JetBrains Mono', monospace;
  font-size: 11px;
  font-weight: 500;
  color: var(--text-3);
  text-transform: lowercase;
  letter-spacing: 0.06em;
}

.panel-hint {
  font-family: 'JetBrains Mono', monospace;
  font-size: 10px;
  color: var(--text-3);
  opacity: 0.6;
}

#code-editor {
  flex: 1;
  overflow: hidden;
}

/* CodeMirror overrides — warm Dracula */
#code-editor .CodeMirror {
  height: 100%;
  font-size: 13.5px;
  line-height: 1.65;
  font-family: 'JetBrains Mono', monospace;
  background: var(--bg-0);
  padding-top: 4px;
}

#code-editor .CodeMirror-gutters {
  background: var(--bg-1);
  border-right: 1px solid var(--border-0);
  padding-right: 2px;
}

#code-editor .CodeMirror-linenumber {
  color: var(--text-3);
  font-size: 11px;
  padding: 0 8px 0 4px;
}

#code-editor .CodeMirror-cursor {
  border-left-color: var(--accent-light);
}

#code-editor .CodeMirror-selected {
  background: rgba(206, 66, 43, 0.12) !important;
}

/* Highlighted lines — colored by segment */
.cm-highlight-line { background: rgba(78, 205, 196, 0.08) !important; }
.cm-highlight-stack { background: rgba(78, 205, 196, 0.1) !important; }
.cm-highlight-heap { background: rgba(255, 107, 107, 0.1) !important; }
.cm-highlight-rodata { background: rgba(195, 166, 255, 0.1) !important; }
.cm-highlight-data { background: rgba(255, 217, 61, 0.1) !important; }
.cm-highlight-bss { background: rgba(107, 203, 119, 0.1) !important; }
.cm-highlight-text { background: rgba(122, 122, 142, 0.08) !important; }

/* ============================================
   Resize Handle
   ============================================ */

.resize-handle {
  width: 5px;
  cursor: col-resize;
  background: transparent;
  transition: background 0.15s;
  flex-shrink: 0;
  z-index: 50;
  position: relative;
}

.resize-handle::after {
  content: '';
  position: absolute;
  left: 2px;
  top: 50%;
  transform: translateY(-50%);
  width: 1px;
  height: 40px;
  background: var(--border-1);
  border-radius: 1px;
  transition: all 0.15s;
}

.resize-handle:hover::after,
.resize-handle.active::after {
  background: var(--accent);
  height: 60px;
  box-shadow: 0 0 8px rgba(206, 66, 43, 0.35);
}

/* Horizontal resize handle (for timeline) */
.resize-handle-h {
  height: 5px;
  cursor: row-resize;
  background: transparent;
  flex-shrink: 0;
  z-index: 50;
  position: relative;
  border-top: 1px solid var(--border-0);
}

.resize-handle-h::after {
  content: '';
  position: absolute;
  top: 2px;
  left: 50%;
  transform: translateX(-50%);
  height: 1px;
  width: 40px;
  background: var(--border-1);
  border-radius: 1px;
  transition: all 0.15s;
}

.resize-handle-h:hover::after,
.resize-handle-h.active::after {
  background: var(--accent);
  width: 60px;
  box-shadow: 0 0 8px rgba(206, 66, 43, 0.35);
}

/* ============================================
   Viz Panel Tabs
   ============================================ */

.viz-tabs {
  display: flex;
  gap: 0;
}

.viz-tab {
  padding: 4px 12px;
  background: none;
  border: none;
  border-bottom: 2px solid transparent;
  color: var(--text-3);
  font-family: 'JetBrains Mono', monospace;
  font-size: 11px;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.15s var(--ease);
  text-transform: lowercase;
  letter-spacing: 0.04em;
  white-space: nowrap;
}

.viz-tab:hover { color: var(--text-1); }

.viz-tab.active {
  color: var(--text-0);
  border-bottom-color: var(--accent);
}

/* Status dot on tab (green = compiled, red = errors, pulsing = loading) */
.viz-tab[data-status="success"]::after,
.viz-tab[data-status="error"]::after,
.viz-tab[data-status="loading"]::after {
  content: '';
  display: inline-block;
  width: 6px;
  height: 6px;
  border-radius: 50%;
  margin-left: 6px;
  vertical-align: middle;
}
.viz-tab[data-status="success"]::after { background: #6bcb77; }
.viz-tab[data-status="error"]::after   { background: var(--danger); }
.viz-tab[data-status="loading"]::after {
  background: var(--text-3);
  animation: pulse 1s ease-in-out infinite;
}

@keyframes pulse {
  0%, 100% { opacity: 0.3; }
  50% { opacity: 1; }
}

/* Tab-specific control groups */
.tab-controls {
  display: flex;
  align-items: center;
  gap: 10px;
}

.tab-controls.hidden { display: none !important; }

/* ============================================
   Visualization Panel
   ============================================ */

#viz-panel {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-width: 300px;
  overflow: hidden;
}

.legend {
  display: flex;
  gap: 14px;
  flex-wrap: wrap;
}

.legend-item {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  font-family: 'JetBrains Mono', monospace;
  font-size: 10px;
  color: var(--text-3);
  cursor: default;
  transition: color 0.15s;
}

.legend-item:hover { color: var(--text-1); }

.legend-item i {
  display: inline-block;
  width: 8px;
  height: 8px;
  border-radius: 2px;
}

.legend-item[data-segment="stack"] i { background: var(--seg-stack); }
.legend-item[data-segment="heap"] i { background: var(--seg-heap); }
.legend-item[data-segment="rodata"] i { background: var(--seg-rodata); }
.legend-item[data-segment="data"] i { background: var(--seg-data); }
.legend-item[data-segment="bss"] i { background: var(--seg-bss); }
.legend-item[data-segment="text"] i { background: var(--seg-text); }

#viz-container {
  flex: 1;
  overflow: auto;
  padding: 20px;
  position: relative;
  background: var(--bg-0);
}

/* Visualization background — warm Rust-inspired atmosphere */
#viz-grid-bg {
  position: absolute;
  inset: 0;
  pointer-events: none;
  opacity: 0;
  animation: bg-warmth 0.8s 0.2s var(--ease) forwards;
  /* Subtle crosshatch pattern + warm gradient glow */
  background:
    /* Faint warm glow in the center where the action is */
    radial-gradient(ellipse at 30% 35%, rgba(206, 66, 43, 0.035) 0%, transparent 60%),
    radial-gradient(ellipse at 75% 25%, rgba(78, 205, 196, 0.02) 0%, transparent 50%),
    /* Fine diagonal lines — engineering blueprint feel, not dots */
    repeating-linear-gradient(
      135deg,
      transparent,
      transparent 30px,
      rgba(206, 66, 43, 0.02) 30px,
      rgba(206, 66, 43, 0.02) 31px
    ),
    repeating-linear-gradient(
      45deg,
      transparent,
      transparent 30px,
      rgba(206, 66, 43, 0.015) 30px,
      rgba(206, 66, 43, 0.015) 31px
    );
}

#viz-placeholder {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  height: 100%;
  gap: 16px;
  color: var(--text-3);
  text-align: center;
  position: relative;
  z-index: 1;
}

.placeholder-icon {
  font-family: 'JetBrains Mono', monospace;
  font-size: 42px;
  font-weight: 700;
  color: var(--border-2);
  letter-spacing: -2px;
  line-height: 1;
}

.placeholder-icon .ptr-symbol {
  color: var(--accent-light);
  opacity: 0.5;
}

#viz-placeholder p {
  font-family: 'DM Sans', sans-serif;
  font-size: 14px;
  color: var(--text-3);
}

#viz-placeholder p.sub {
  font-size: 12px;
  color: var(--text-3);
  opacity: 0.5;
}

#viz-placeholder kbd {
  display: inline-block;
  padding: 2px 7px;
  font-family: 'JetBrains Mono', monospace;
  font-size: 10px;
  background: var(--bg-2);
  border: 1px solid var(--border-1);
  border-radius: 3px;
  color: var(--text-2);
}

#viz-content.hidden { display: none; }

/* Draggable segment headers */
.mem-segment-group { cursor: grab; }
.mem-segment-group:active { cursor: grabbing; }
.mem-entry-group { cursor: pointer; }

#memory-svg {
  width: 100%;
  min-height: 400px;
  touch-action: none; /* prevent scroll while dragging segments on mobile */
  position: relative;
  z-index: 1;
}

/* ============================================
   SVG Memory Elements
   ============================================ */

.mem-segment-bg {
  transition: opacity 0.2s;
}

.mem-entry-bg {
  cursor: pointer;
  transition: all 0.15s var(--ease);
}

.mem-entry-bg:hover {
  filter: brightness(1.4);
}

.mem-entry-bg.highlighted {
  filter: brightness(1.6);
}

/* Connection arrows — the star of the show */
.mem-connection-base {
  fill: none;
  stroke-width: 2;
  stroke-linecap: round;
  opacity: 0.35;
  transition: opacity 0.2s var(--ease);
}

.mem-connection-flow {
  fill: none;
  stroke-width: 2;
  stroke-linecap: round;
  stroke-dasharray: 6 10;
  opacity: 0.5;
  will-change: stroke-dashoffset; /* GPU-accelerate the animation */
}

.mem-connection-flow.animated {
  animation: flow-right 0.8s linear infinite;
}

.mem-connection-base.highlighted {
  opacity: 0.8;
  stroke-width: 2.5;
}

.mem-connection-flow.highlighted {
  opacity: 0.9;
  stroke-width: 2.5;
}

/* Pointer dots on entries */
.ptr-dot {
  transition: all 0.15s var(--ease);
  cursor: pointer;
}

.ptr-dot:hover {
  r: 6;
}

.ptr-dot.highlighted {
  r: 6;
}

/* Entry appear animation */
.mem-entry-group {
  opacity: 0;
  animation: entry-slide-in 0.25s var(--ease) forwards;
}

/* Segment appear */
.mem-segment-group {
  opacity: 0;
  animation: segment-fade-in 0.35s var(--ease) forwards;
}

/* ============================================
   Assembly View
   ============================================ */

#asm-content {
  position: relative;
  z-index: 1;
}

#asm-content.hidden { display: none; }

#asm-container {
  font-family: 'JetBrains Mono', monospace;
  font-size: 12px;
  line-height: 1.6;
}

.asm-lines { padding: 8px 0; }

.asm-line {
  display: flex;
  align-items: baseline;
  padding: 1px 12px;
  transition: background 0.1s;
  border-left: 2px solid transparent;
}

.asm-line:hover { background: var(--bg-hover) !important; }

.asm-line.highlighted {
  background: rgba(206, 66, 43, 0.12) !important;
  border-left-color: var(--accent);
}

.asm-linenum {
  width: 36px;
  text-align: right;
  color: var(--text-3);
  font-size: 10px;
  opacity: 0.5;
  flex-shrink: 0;
  padding-right: 8px;
  user-select: none;
}

.asm-src {
  width: 32px;
  text-align: right;
  color: var(--text-3);
  font-size: 9px;
  flex-shrink: 0;
  padding-right: 10px;
  user-select: none;
  opacity: 0.6;
}

.asm-text {
  flex: 1;
  white-space: pre;
  color: var(--text-1);
}

/* Assembly syntax highlighting */
.asm-label     { color: var(--accent-light); font-weight: 600; }
.asm-mnemonic  { color: #c3a6ff; }
.asm-register  { color: #4ecdc4; }
.asm-number    { color: #ffd93d; }
.asm-directive { color: #6bcb77; }
.asm-comment   { color: var(--text-3); font-style: italic; }

/* Assembly errors */
.asm-errors { padding: 20px; }

.asm-errors-title {
  font-family: 'Outfit', sans-serif;
  font-size: 14px;
  font-weight: 600;
  color: var(--danger);
  margin-bottom: 4px;
}

.asm-errors-desc {
  font-size: 12px;
  color: var(--text-2);
  margin-bottom: 14px;
}

.asm-error-line {
  font-family: 'JetBrains Mono', monospace;
  font-size: 11px;
  line-height: 1.6;
  padding: 1px 0;
  white-space: pre-wrap;
  word-break: break-word;
}

.asm-error-line.asm-is-error { color: var(--danger); }
.asm-error-line.asm-is-warn  { color: #ffd93d; }
.asm-error-line.asm-is-note  { color: var(--text-2); }

/* Assembly warnings banner */
.asm-warnings {
  padding: 6px 12px;
  background: rgba(255, 217, 61, 0.08);
  border-bottom: 1px solid rgba(255, 217, 61, 0.15);
  color: #ffd93d;
  font-family: 'JetBrains Mono', monospace;
  font-size: 11px;
  cursor: help;
}

/* Assembly loading / placeholder */
.asm-loading {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  height: 200px;
  color: var(--text-3);
  font-family: 'JetBrains Mono', monospace;
  font-size: 12px;
}

.asm-spinner {
  width: 16px;
  height: 16px;
  border: 2px solid var(--border-1);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}

@keyframes spin { to { transform: rotate(360deg); } }

.asm-placeholder {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  height: 200px;
  gap: 10px;
  color: var(--text-3);
  font-family: 'JetBrains Mono', monospace;
  font-size: 12px;
  text-align: center;
}

.asm-placeholder-icon {
  font-size: 28px;
  font-weight: 700;
  color: var(--border-2);
  letter-spacing: 2px;
  text-transform: uppercase;
}

.asm-placeholder-sub {
  font-size: 11px;
  opacity: 0.5;
}

/* Attribution */
.asm-attribution {
  padding: 8px 12px;
  font-family: 'JetBrains Mono', monospace;
  font-size: 9px;
  color: var(--text-3);
  opacity: 0.4;
  text-align: right;
}

.asm-attribution a {
  color: var(--text-2);
  text-decoration: none;
}

.asm-attribution a:hover {
  color: var(--accent-light);
  text-decoration: underline;
}

/* Compile status badge */
.compile-status {
  font-family: 'JetBrains Mono', monospace;
  font-size: 10px;
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 2px 8px;
  border-radius: var(--radius-sm);
  white-space: nowrap;
}

.status-loading { color: var(--text-3); }

.status-success {
  color: #6bcb77;
  background: rgba(107, 203, 119, 0.1);
}

.status-error {
  color: var(--danger);
  background: rgba(255, 107, 107, 0.1);
  cursor: help;
}

.status-offline {
  color: var(--text-3);
  background: var(--bg-3);
}

.status-spinner {
  display: inline-block;
  width: 10px;
  height: 10px;
  border: 1.5px solid var(--border-1);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}

/* Optimization level select */
.asm-opt-select {
  font-family: 'JetBrains Mono', monospace;
  font-size: 10px;
  padding: 2px 6px;
  background: var(--bg-3);
  color: var(--text-1);
  border: 1px solid var(--border-1);
  border-radius: var(--radius-sm);
  cursor: pointer;
  outline: none;
}

.asm-opt-select:hover { border-color: var(--border-2); }
.asm-opt-select:focus { border-color: var(--accent); }

/* ============================================
   Timeline Panel
   ============================================ */

#timeline-panel {
  height: 170px;
  min-height: 60px;
  max-height: 50vh;
  display: flex;
  flex-direction: column;
  flex-shrink: 0;
  background: var(--bg-1);
}

.timeline-controls {
  display: flex;
  align-items: center;
  gap: 4px;
}

.timeline-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 26px;
  height: 26px;
  background: var(--bg-3);
  border: 1px solid var(--border-0);
  border-radius: var(--radius-sm);
  color: var(--text-2);
  cursor: pointer;
  transition: all 0.12s;
}

.timeline-btn:hover:not(:disabled) {
  background: var(--bg-4);
  color: var(--text-0);
}

.timeline-btn:disabled {
  opacity: 0.2;
  cursor: not-allowed;
}

.timeline-btn.playing {
  background: var(--accent);
  color: #1a1a1a;
  border-color: var(--accent);
}

#timeline-step-display {
  font-family: 'JetBrains Mono', monospace;
  font-size: 11px;
  color: var(--text-3);
  min-width: 50px;
  text-align: center;
  font-variant-numeric: tabular-nums;
}

#timeline-entries {
  flex: 1;
  overflow-y: auto;
  padding: 6px 12px;
}

.timeline-placeholder {
  font-family: 'JetBrains Mono', monospace;
  color: var(--text-3);
  font-size: 11px;
  padding: 12px 0;
  opacity: 0.5;
}

.timeline-entry {
  display: flex;
  align-items: flex-start;
  gap: 8px;
  padding: 5px 8px;
  border-radius: var(--radius-sm);
  font-size: 12px;
  line-height: 1.5;
  color: var(--text-2);
  cursor: pointer;
  transition: all 0.12s;
  font-family: 'JetBrains Mono', monospace;
  border-left: 2px solid transparent;
}

.timeline-entry:hover {
  background: var(--bg-hover);
  color: var(--text-0);
}

.timeline-entry.active {
  background: var(--bg-3);
  color: var(--text-0);
}

.timeline-entry.active[data-segment="stack"] { border-left-color: var(--seg-stack); }
.timeline-entry.active[data-segment="heap"] { border-left-color: var(--seg-heap); }
.timeline-entry.active[data-segment="rodata"] { border-left-color: var(--seg-rodata); }
.timeline-entry.active[data-segment="data"] { border-left-color: var(--seg-data); }
.timeline-entry.active[data-segment="bss"] { border-left-color: var(--seg-bss); }
.timeline-entry.active[data-segment="text"] { border-left-color: var(--seg-text); }

.timeline-step-num {
  flex-shrink: 0;
  width: 20px;
  color: var(--text-3);
  text-align: right;
  font-size: 10px;
  opacity: 0.5;
}

.timeline-action { flex: 1; font-size: 11px; }

.timeline-segment-tag {
  display: inline-block;
  padding: 1px 5px;
  border-radius: 2px;
  font-size: 9px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  margin-left: 4px;
  vertical-align: middle;
}

.tag-stack { background: rgba(78, 205, 196, 0.12); color: var(--seg-stack); }
.tag-heap { background: rgba(255, 107, 107, 0.12); color: var(--seg-heap); }
.tag-rodata { background: rgba(195, 166, 255, 0.12); color: var(--seg-rodata); }
.tag-data { background: rgba(255, 217, 61, 0.12); color: var(--seg-data); }
.tag-bss { background: rgba(107, 203, 119, 0.12); color: var(--seg-bss); }
.tag-text { background: rgba(122, 122, 142, 0.1); color: var(--seg-text); }
.tag-drop { background: rgba(255, 107, 107, 0.12); color: var(--danger); }

/* ============================================
   Tooltip
   ============================================ */

#tooltip {
  position: fixed;
  z-index: 1000;
  max-width: 340px;
  background: var(--bg-2);
  border: 1px solid var(--border-1);
  border-radius: var(--radius);
  box-shadow: 0 12px 48px rgba(0, 0, 0, 0.6);
  pointer-events: none;
  transition: opacity 0.12s;
}

#tooltip.hidden { opacity: 0; display: none; }

.tooltip-header {
  padding: 10px 14px;
  font-size: 13px;
  font-weight: 600;
  border-bottom: 1px solid var(--border-0);
  font-family: 'JetBrains Mono', monospace;
}

.tooltip-body {
  padding: 10px 14px;
  font-size: 12px;
  color: var(--text-1);
  line-height: 1.6;
  font-family: 'DM Sans', sans-serif;
}

.tooltip-body .tip-row {
  display: flex;
  justify-content: space-between;
  gap: 16px;
  padding: 2px 0;
}

.tooltip-body .tip-label { color: var(--text-3); font-size: 11px; }
.tooltip-body .tip-value { color: var(--text-0); font-family: 'JetBrains Mono', monospace; font-size: 11px; }

.tooltip-body .tip-reason {
  margin-top: 8px;
  padding-top: 8px;
  border-top: 1px solid var(--border-0);
  color: var(--text-2);
  font-size: 11px;
  line-height: 1.5;
}

/* ============================================
   Modal
   ============================================ */

.modal {
  position: fixed;
  inset: 0;
  z-index: 500;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(0, 0, 0, 0.65);
  backdrop-filter: blur(6px);
}

.modal.hidden { display: none; }

.modal-content {
  width: 90%;
  max-width: 520px;
  max-height: 80vh;
  overflow-y: auto;
  background: var(--bg-1);
  border: 1px solid var(--border-1);
  border-radius: var(--radius);
  box-shadow: 0 24px 80px rgba(0, 0, 0, 0.5);
}

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

.modal-header h2 {
  font-family: 'Outfit', sans-serif;
  font-size: 18px;
  font-weight: 700;
  letter-spacing: -0.02em;
}

.modal-close {
  width: 30px;
  height: 30px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: none;
  border: none;
  color: var(--text-3);
  font-size: 20px;
  cursor: pointer;
  border-radius: var(--radius-sm);
  transition: all 0.12s;
}

.modal-close:hover { background: var(--bg-hover); color: var(--text-0); }

.modal-body {
  padding: 20px;
  font-size: 13px;
  line-height: 1.7;
  color: var(--text-1);
}

.modal-body h3 {
  font-family: 'Outfit', sans-serif;
  font-size: 13px;
  font-weight: 600;
  color: var(--text-0);
  margin: 18px 0 6px;
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

.modal-body h3:first-child { margin-top: 0; }

.modal-body table { width: 100%; border-collapse: collapse; margin: 6px 0; }

.modal-body td {
  padding: 5px 8px;
  border-bottom: 1px solid var(--border-0);
  font-size: 12px;
}

.modal-body td:first-child { white-space: nowrap; color: var(--text-0); }

.modal-body kbd {
  display: inline-block;
  padding: 1px 5px;
  font-family: 'JetBrains Mono', monospace;
  font-size: 10px;
  background: var(--bg-3);
  border: 1px solid var(--border-1);
  border-radius: 3px;
  color: var(--text-2);
}

.modal-body ul { padding-left: 18px; }
.modal-body li { margin: 3px 0; font-size: 12px; }

.seg-stack { color: var(--seg-stack); }
.seg-heap { color: var(--seg-heap); }
.seg-rodata { color: var(--seg-rodata); }
.seg-data { color: var(--seg-data); }
.seg-bss { color: var(--seg-bss); }
.seg-text { color: var(--seg-text); }

/* ============================================
   Scrollbar — minimal
   ============================================ */

::-webkit-scrollbar { width: 6px; height: 6px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--border-1); border-radius: 3px; }
::-webkit-scrollbar-thumb:hover { background: var(--text-3); }

/* ============================================
   Utility
   ============================================ */

.hidden { display: none !important; }

/* ============================================
   Responsive
   ============================================ */

/* ============================================
   Mobile — complete responsive overhaul
   ============================================ */

@media (max-width: 768px) {
  /* Header: compress for narrow screens */
  #app-header {
    padding: 0 10px;
    height: 44px;
    gap: 6px;
  }

  .logo-mark { font-size: 14px; }
  .logo h1 { font-size: 14px; }
  .logo { gap: 6px; }

  .header-btn { padding: 5px 8px; font-size: 11px; }
  .header-btn kbd { display: none; } /* hide keyboard hints on touch */
  .header-btn.icon-btn { width: 30px; height: 30px; }

  .panel-hint { display: none; }
  #reset-layout-btn { display: none; }

  .viz-tabs { gap: 0; }
  .viz-tab { font-size: 10px; padding: 3px 8px; }

  .asm-opt-select { font-size: 9px; }
  .compile-status { font-size: 9px; padding: 2px 5px; }

  .asm-line { padding: 1px 8px; }
  .asm-linenum { width: 28px; font-size: 9px; }
  .asm-src { width: 26px; font-size: 8px; }
  .asm-text { font-size: 11px; }
  #asm-container { font-size: 11px; }

  /* Stack panels vertically */
  #app-main { flex-direction: column; }

  #editor-panel {
    width: 100% !important; /* override inline resize styles */
    height: 35%;
    min-height: 120px;
    min-width: unset;
    border-right: none;
    border-bottom: 1px solid var(--border-0);
  }

  .resize-handle { display: none; }

  #viz-panel {
    flex: 1;
    min-width: unset;
    min-height: 120px;
  }

  .panel-header { padding: 4px 10px; height: 28px; }
  .panel-title { font-size: 10px; }

  .legend { gap: 6px; }
  .legend-item { font-size: 9px; gap: 3px; }
  .legend-item i { width: 6px; height: 6px; }

  #viz-container { padding: 10px; }

  /* Timeline: shorter on mobile */
  .resize-handle-h { display: none; }
  #timeline-panel {
    height: 120px !important;
    min-height: 60px;
  }
  #timeline-entries { padding: 4px 8px; }
  .timeline-entry { font-size: 10px; padding: 4px 6px; gap: 6px; }
  .timeline-segment-tag { font-size: 8px; padding: 1px 3px; }
  .timeline-step-num { width: 16px; font-size: 9px; }

  /* CodeMirror: smaller font on mobile */
  #code-editor .CodeMirror {
    font-size: 12px;
    line-height: 1.5;
  }

  /* Tooltip: full-width on mobile */
  #tooltip {
    max-width: calc(100vw - 20px);
    left: 10px !important;
    right: 10px;
  }

  /* Modal: tighter on mobile */
  .modal-content { width: 95%; max-height: 85vh; }
  .modal-body { padding: 14px; font-size: 12px; }

  /* Dropdown: full-width on mobile */
  .dropdown-menu {
    position: fixed;
    left: 10px;
    right: 10px;
    top: 50px;
    min-width: unset;
    width: auto;
  }
}

@media (max-width: 480px) {
  /* Very small phones */
  .logo-mark { display: none; }
  .legend { display: none; } /* too cramped, users use tooltip instead */

  #editor-panel { height: 30%; }

  #app-header { height: 40px; }
  .header-btn { padding: 4px 6px; font-size: 10px; }
}
