/* ===========================================================
   Sleek dark "pro-tool" theme (Linear / Vercel lineage)
   =========================================================== */
:root {
  --bg: #08090c;
  --panel: #0e0f13;
  --panel-2: #121317;
  --elev: #16181d;
  --hair: rgba(255, 255, 255, 0.08);
  --hair-strong: rgba(255, 255, 255, 0.14);
  --text: #f1f3f7;
  --text-muted: #8b94a3;
  --text-faint: #7a8494;
  --accent: #5b8cff;
  --accent-soft: rgba(91, 140, 255, 0.16);
  --accent-text: #ffffff;

  /* Bar state palette (canvas only) */
  --bar-idle: #3a4150;
  --bar-compare: #ffb020;
  --bar-swap: #ff5d6c;
  --bar-sorted: #2fe39b;
  --bar-target: #9d7bff;

  --r-sm: 8px;
  --r-md: 10px;
  --r-lg: 14px;
  --font-ui: ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, Helvetica, sans-serif;
  --font-mono: ui-monospace, "SF Mono", "JetBrains Mono", "Cascadia Code", Menlo, monospace;
}

[data-theme="light"] {
  --bg: #f7f8fa;
  --panel: #ffffff;
  --panel-2: #fbfcfe;
  --elev: #ffffff;
  --hair: rgba(10, 15, 30, 0.10);
  --hair-strong: rgba(10, 15, 30, 0.16);
  --text: #11141a;
  --text-muted: #5a6472;
  --text-faint: #667085;
  --accent: #2f6bff;
  --accent-soft: rgba(47, 107, 255, 0.12);
  --accent-text: #ffffff;
  --bar-idle: #c4ccd8;
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }

body {
  background-color: var(--bg);
  background-image:
    linear-gradient(var(--hair) 1px, transparent 1px),
    linear-gradient(90deg, var(--hair) 1px, transparent 1px);
  background-size: 44px 44px, 44px 44px;
  background-position: center;
  color: var(--text);
  font-family: var(--font-ui);
  font-size: 14px;
  letter-spacing: -0.005em;
  -webkit-font-smoothing: antialiased;
  overflow: hidden;
}
body::before {
  /* fade the grid toward the edges so it reads as texture, not a table */
  content: "";
  position: fixed; inset: 0;
  background: radial-gradient(120% 120% at 50% 40%, transparent 30%, var(--bg) 90%);
  pointer-events: none;
}

.mono { font-family: var(--font-mono); font-variant-numeric: tabular-nums; }

/* ---------- Layout ---------- */
.app {
  position: relative;
  display: flex;
  flex-direction: column;
  height: 100dvh;
  max-width: 1280px;
  margin: 0 auto;
  padding: clamp(14px, 2.2vh, 22px) clamp(16px, 4vw, 40px);
  gap: clamp(10px, 1.6vh, 16px);
}

/* ---------- Header ---------- */
.header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px 16px;
  flex-wrap: wrap;
}
.title {
  margin: 0;
  font-size: clamp(1rem, 1.5vw, 1.3rem);
  font-weight: 600;
  letter-spacing: -0.03em;
  display: flex;
  align-items: center;
  gap: 10px;
}
.title::before {
  /* small accent tick, instrument flavor */
  content: "";
  flex-shrink: 0;
  width: 9px; height: 9px;
  border-radius: 3px;
  background: var(--accent);
  box-shadow: 0 0 12px var(--accent);
}
.header-meta { display: flex; align-items: center; flex-wrap: wrap; justify-content: flex-end; gap: 10px; }
.algo-name {
  font-family: var(--font-mono);
  font-size: 0.78rem;
  letter-spacing: 0.02em;
  padding: 5px 11px;
  border-radius: 999px;
  background: var(--accent-soft);
  color: var(--accent);
  border: 1px solid color-mix(in srgb, var(--accent) 30%, transparent);
  cursor: pointer;
  transition: filter 140ms ease;
}
.algo-name:hover { filter: brightness(1.15); }
.complexity {
  font-family: var(--font-mono);
  color: var(--text-muted);
  font-size: 0.78rem;
}
.icon-btn {
  background: var(--panel);
  border: 1px solid var(--hair);
  color: var(--text-muted);
  border-radius: 8px;
  width: 32px; height: 32px;
  cursor: pointer;
  font-size: 0.9rem;
  display: grid; place-items: center;
  transition: color 140ms ease, border-color 140ms ease, background 140ms ease;
}
.icon-btn:hover { color: var(--text); border-color: var(--hair-strong); background: var(--elev); }
.sound-toggle.on { color: var(--accent); border-color: color-mix(in srgb, var(--accent) 40%, var(--hair)); }

/* ---------- Canvas ---------- */
.canvas-wrap {
  flex: 1;
  min-height: 0;
  position: relative;
  background: var(--panel);
  border: 1px solid var(--hair);
  border-radius: var(--r-lg);
  overflow: hidden;
}
#canvas { display: block; width: 100%; height: 100%; cursor: zoom-in; }
.canvas-wrap.expanded #canvas { cursor: grab; touch-action: none; }
.canvas-wrap.grabbing #canvas { cursor: grabbing; }

/* Fullscreen: cover the whole window, hide everything else */
.canvas-wrap.fullscreen {
  position: fixed;
  inset: 0;
  z-index: 65;
  border: none;
  border-radius: 0;
  background: var(--bg);
}
.fs-close { position: absolute; top: 14px; right: 14px; z-index: 2; }
.fs-close[hidden] { display: none; }

.zoom-hint {
  position: absolute;
  left: 50%;
  bottom: 16px;
  transform: translateX(-50%);
  font-family: var(--font-mono);
  font-size: 0.72rem;
  color: var(--text-muted);
  background: color-mix(in srgb, var(--panel) 90%, transparent);
  border: 1px solid var(--hair);
  border-radius: 999px;
  padding: 6px 14px;
  pointer-events: none;
}
.zoom-hint[hidden] { display: none; }

/* ---------- Controls ---------- */
.controls {
  display: flex;
  flex-direction: column;
  gap: 14px;
  background: var(--panel);
  border: 1px solid var(--hair);
  border-radius: var(--r-lg);
  padding: 14px 15px;
}
.controls-row {
  display: flex;
  align-items: flex-end;
  flex-wrap: wrap;
  gap: 12px 14px;
}
.controls-actions { justify-content: flex-end; gap: 9px; }
.control { display: flex; flex-direction: column; gap: 6px; }
.control-grow { flex: 1; min-width: 170px; }
.control label {
  font-family: var(--font-mono);
  font-size: 0.66rem;
  text-transform: uppercase;
  letter-spacing: 0.12em;
  color: var(--text-faint);
}

select, input[type="text"], input[type="number"] {
  background: var(--bg);
  border: 1px solid var(--hair);
  color: var(--text);
  border-radius: var(--r-sm);
  padding: 9px 11px;
  font-size: 0.86rem;
  font-family: var(--font-ui);
  width: 100%;
  transition: border-color 140ms ease, box-shadow 140ms ease;
}
select:hover, input:hover { border-color: var(--hair-strong); }
input[type="text"], input[type="number"] { min-width: 120px; }

.input-row { display: flex; gap: 8px; align-items: stretch; }
.input-row input { flex: 1; }
.btn-sm { padding: 8px 12px; font-size: 0.82rem; white-space: nowrap; }

.file-chip {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  background: var(--bg);
  border: 1px solid var(--hair);
  border-radius: var(--r-sm);
  padding: 0 4px 0 11px;
  max-width: 200px;
}
.file-chip[hidden] { display: none; }
.file-chip-name {
  font-size: 0.8rem;
  color: var(--text);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.file-remove {
  background: transparent;
  border: none;
  color: var(--text-muted);
  cursor: pointer;
  font-size: 0.78rem;
  line-height: 1;
  padding: 6px;
  border-radius: 6px;
  transition: color 140ms ease, background 140ms ease;
}
.file-remove:hover { color: var(--bar-swap); background: var(--elev); }

/* ---------- Onboarding ---------- */
.onboard {
  position: fixed; inset: 0;
  z-index: 70;
  display: grid;
  place-items: center;
  padding: 20px;
  overflow-y: auto;
  background: rgba(0, 0, 0, 0.6);
  backdrop-filter: blur(4px);
  -webkit-backdrop-filter: blur(4px);
}
.onboard[hidden] { display: none; }
.onboard-card {
  width: min(560px, 96vw);
  background: var(--panel);
  border: 1px solid var(--hair);
  border-radius: var(--r-lg);
  padding: 26px;
  box-shadow: 0 36px 90px -28px rgba(0, 0, 0, 0.8);
}
.onboard-head h2 { margin: 0 0 6px; font-size: 1.32rem; font-weight: 650; letter-spacing: -0.02em; }
.onboard-head p { margin: 0 0 20px; color: var(--text-muted); font-size: 0.9rem; line-height: 1.5; }
.tips { list-style: none; margin: 0 0 22px; padding: 0; display: grid; gap: 13px; }
.tips li { display: flex; gap: 12px; align-items: flex-start; }
.tip-key {
  flex-shrink: 0;
  font-family: var(--font-mono);
  font-size: 0.64rem;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--accent);
  background: var(--accent-soft);
  border: 1px solid color-mix(in srgb, var(--accent) 30%, transparent);
  border-radius: 6px;
  padding: 5px 0;
  width: 56px;
  text-align: center;
}
.tips strong { display: block; font-size: 0.92rem; margin-bottom: 2px; }
.tips p { margin: 0; color: var(--text-muted); font-size: 0.85rem; line-height: 1.5; }
.onboard-subhead {
  font-family: var(--font-mono);
  font-size: 0.66rem;
  text-transform: uppercase;
  letter-spacing: 0.12em;
  color: var(--text-faint);
  margin: 0 0 10px;
}
.shortcuts { width: 100%; border-collapse: collapse; margin: 0 0 22px; font-size: 0.85rem; }
.shortcuts td { padding: 7px 0; border-bottom: 1px solid var(--hair); color: var(--text-muted); }
.shortcuts tr:last-child td { border-bottom: none; }
.shortcuts td:first-child { width: 130px; white-space: nowrap; }
.onboard-actions { display: flex; justify-content: flex-end; }

/* ---------- Custom values modal ---------- */
.modal-backdrop {
  position: fixed; inset: 0;
  background: rgba(0, 0, 0, 0.55);
  z-index: 55;
}
.modal-backdrop[hidden] { display: none; }
.modal {
  position: fixed;
  top: 50%; left: 50%;
  transform: translate(-50%, -50%);
  z-index: 56;
  width: min(440px, 92vw);
  background: var(--panel);
  border: 1px solid var(--hair);
  border-radius: var(--r-lg);
  padding: 18px;
  display: flex;
  flex-direction: column;
  gap: 12px;
  box-shadow: 0 30px 80px -24px rgba(0, 0, 0, 0.75);
}
.modal[hidden] { display: none; }
.modal-head { display: flex; align-items: center; justify-content: space-between; }
.modal-head h2 { margin: 0; font-size: 1.05rem; font-weight: 600; letter-spacing: -0.01em; }
.modal-hint { margin: 0; font-size: 0.82rem; color: var(--text-muted); line-height: 1.55; }
.modal input { width: 100%; }
.modal-actions { display: flex; justify-content: flex-end; gap: 9px; }
kbd {
  font-family: var(--font-mono);
  font-size: 0.72rem;
  padding: 1px 6px;
  border: 1px solid var(--hair);
  border-bottom-width: 2px;
  border-radius: 5px;
  background: var(--bg);
  color: var(--text);
}

/* ---------- Drag-and-drop overlay ---------- */
.drop-overlay {
  position: fixed;
  inset: 0;
  z-index: 60;
  display: grid;
  place-items: center;
  background: color-mix(in srgb, var(--bg) 80%, transparent);
  backdrop-filter: blur(4px);
  -webkit-backdrop-filter: blur(4px);
  pointer-events: none;
}
.drop-overlay[hidden] { display: none; }
.drop-overlay-inner {
  border: 2px dashed var(--accent);
  border-radius: var(--r-lg);
  padding: 44px 60px;
  text-align: center;
  background: var(--panel);
  box-shadow: 0 24px 70px -24px rgba(0, 0, 0, 0.7);
}
.drop-icon { font-size: 2.4rem; line-height: 1; color: var(--accent); }
.drop-overlay-inner p { margin: 14px 0 4px; font-size: 1.05rem; color: var(--text); }
.drop-overlay-inner span { font-family: var(--font-mono); font-size: 0.78rem; color: var(--text-faint); }

.select-wrap { position: relative; }
.select-wrap::after {
  content: "▾";
  position: absolute; right: 11px; top: 50%;
  transform: translateY(-50%);
  pointer-events: none; color: var(--text-faint); font-size: 0.72rem;
}
select { appearance: none; padding-right: 28px; cursor: pointer; }

input[type="range"] { width: 100%; accent-color: var(--accent); cursor: pointer; height: 24px; }

:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
input:focus-visible, select:focus-visible {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-soft);
}

/* Segmented toggle */
.segmented {
  display: inline-flex;
  background: var(--bg);
  border: 1px solid var(--hair);
  border-radius: var(--r-sm);
  padding: 3px; gap: 2px;
}
.seg {
  background: transparent; border: none;
  color: var(--text-muted);
  font-family: var(--font-mono);
  font-size: 0.76rem; letter-spacing: 0.04em;
  padding: 7px 15px; border-radius: 6px;
  cursor: pointer;
  transition: background 140ms ease, color 140ms ease;
}
.seg:hover { color: var(--text); }
.seg.active { background: var(--accent); color: var(--accent-text); }

/* Buttons */
.btn {
  border-radius: var(--r-sm);
  padding: 10px 18px;
  font-size: 0.85rem;
  font-weight: 550;
  letter-spacing: 0.01em;
  cursor: pointer;
  border: 1px solid var(--hair);
  transition: background 140ms ease, transform 70ms ease, border-color 140ms ease, color 140ms ease;
}
.btn:active { transform: translateY(1px); }
.btn-primary {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--accent-text);
  box-shadow: 0 0 0 1px var(--accent), 0 6px 22px -10px var(--accent);
}
.btn-primary:hover { filter: brightness(1.08); }
.btn-secondary { background: var(--elev); color: var(--text); }
.btn-secondary:hover { background: var(--panel-2); border-color: var(--hair-strong); }
.btn-ghost { background: transparent; color: var(--text-muted); }
.btn-ghost:hover { background: var(--elev); color: var(--text); }
.btn:disabled { opacity: 0.38; cursor: not-allowed; transform: none; box-shadow: none; filter: none; }

/* ---------- Status ---------- */
.status {
  display: flex; align-items: center; gap: 14px; flex-wrap: wrap;
  font-family: var(--font-mono);
  font-size: 0.76rem;
  color: var(--text-faint);
  padding: 0 4px; min-height: 16px;
}
.status-msg { color: var(--text-muted); }
.status-msg.error { color: var(--bar-swap); }
#time-label { margin-left: auto; color: var(--text-muted); }
.credit { color: var(--text-faint); }
.credit a {
  color: var(--text-muted);
  text-decoration: none;
  border-bottom: 1px solid transparent;
  transition: color 140ms ease, border-color 140ms ease;
}
.credit a:hover { color: var(--accent); border-bottom-color: var(--accent); }

/* ---------- Info drawer ---------- */
.drawer-backdrop {
  position: fixed; inset: 0;
  background: rgba(0, 0, 0, 0.55);
  z-index: 40;
  opacity: 0;
  transition: opacity 220ms ease;
}
.drawer-backdrop.show { opacity: 1; }

.drawer {
  position: fixed;
  top: 0; right: 0;
  height: 100dvh;
  width: min(420px, 92vw);
  background: var(--panel);
  border-left: 1px solid var(--hair);
  z-index: 50;
  display: flex;
  flex-direction: column;
  transform: translateX(100%);
  transition: transform 240ms cubic-bezier(0.16, 1, 0.3, 1);
}
.drawer.open { transform: translateX(0); }

.drawer-head {
  display: flex; align-items: center; justify-content: space-between;
  padding: 16px 18px;
  border-bottom: 1px solid var(--hair);
  flex-shrink: 0;
}
.drawer-title { margin: 0; font-size: 1.05rem; font-weight: 600; letter-spacing: -0.01em; }
.drawer-body { padding: 18px; overflow-y: auto; }

.drawer-body .summary {
  font-size: 0.95rem;
  color: var(--text);
  margin: 0 0 14px;
  line-height: 1.5;
}
.drawer-body h3 {
  font-family: var(--font-mono);
  font-size: 0.66rem;
  text-transform: uppercase;
  letter-spacing: 0.12em;
  color: var(--text-faint);
  margin: 20px 0 8px;
}
.drawer-body p { margin: 0; color: var(--text-muted); line-height: 1.6; font-size: 0.9rem; }

.cx-table { width: 100%; border-collapse: collapse; font-size: 0.85rem; }
.cx-table td { padding: 6px 0; border-bottom: 1px solid var(--hair); }
.cx-table td:first-child { color: var(--text-muted); }
.cx-table td:last-child { text-align: right; font-family: var(--font-mono); color: var(--text); }
.cx-table tr:last-child td { border-bottom: none; }

.props { display: flex; flex-wrap: wrap; gap: 8px; }
.prop {
  font-family: var(--font-mono);
  font-size: 0.74rem;
  padding: 4px 10px;
  border-radius: 999px;
  border: 1px solid var(--hair);
  color: var(--text-muted);
  background: var(--bg);
}

.legend { display: flex; flex-direction: column; gap: 9px; }
.legend-item { display: flex; align-items: center; gap: 10px; font-size: 0.85rem; color: var(--text-muted); }
.legend-swatch { width: 14px; height: 14px; border-radius: 4px; flex-shrink: 0; }
.legend-swatch.s-compare { background: var(--bar-compare); }
.legend-swatch.s-swap { background: var(--bar-swap); }
.legend-swatch.s-sorted { background: var(--bar-sorted); }
.legend-swatch.s-target { background: var(--bar-target); }

.pseudo {
  margin: 0;
  background: var(--bg);
  border: 1px solid var(--hair);
  border-radius: var(--r-sm);
  padding: 12px 14px;
  font-family: var(--font-mono);
  font-size: 0.78rem;
  line-height: 1.6;
  color: var(--text);
  white-space: pre;
  overflow-x: auto;
}

/* ---------- Export menu ---------- */
.export { position: relative; }
.export-menu {
  position: absolute;
  top: calc(100% + 6px);
  right: 0;
  z-index: 30;
  background: var(--panel);
  border: 1px solid var(--hair);
  border-radius: var(--r-sm);
  padding: 5px;
  min-width: 180px;
  display: flex;
  flex-direction: column;
  box-shadow: 0 14px 40px -16px rgba(0, 0, 0, 0.7);
}
.export-menu[hidden] { display: none; }
.export-menu button {
  background: transparent;
  border: none;
  color: var(--text);
  text-align: left;
  font-size: 0.84rem;
  padding: 9px 11px;
  border-radius: 6px;
  cursor: pointer;
  transition: background 140ms ease;
}
.export-menu button:hover { background: var(--elev); }

/* ---------- Responsive ---------- */
@media (max-width: 768px) {
  body { overflow-x: hidden; overflow-y: auto; }
  /* Page scrolls instead of locking the viewport; tighter gutters reclaim width */
  .app {
    height: auto;
    min-height: 100dvh;
    padding: 14px 14px 22px;
    gap: 12px;
  }

  /* Canvas takes a fixed, reachable slice instead of eating the whole screen
     so the controls sit above the fold rather than being pushed far below. */
  .canvas-wrap {
    flex: none;
    height: clamp(240px, 40vh, 380px);
    min-height: 0;
  }

  /* Header: let the meta chips/icons wrap and read left-to-right when they do */
  .header { gap: 8px 12px; }
  .header-meta { justify-content: flex-start; width: 100%; gap: 8px; }

  /* One control per row, each filling the column */
  .controls-row:not(.controls-actions) { flex-direction: column; align-items: stretch; gap: 12px; }
  .controls-row:not(.controls-actions) .control,
  .controls-row:not(.controls-actions) .control-grow { width: 100%; min-width: 0; }

  /* Actions wrap cleanly instead of overflowing the right edge.
     Run (primary) gets its own full-width row as the main action. */
  .controls-actions { flex-wrap: wrap; gap: 8px; }
  .controls-actions .btn {
    flex: 1 1 calc(50% - 4px);
    min-width: 0;
    min-height: 44px;
  }
  .controls-actions .btn-primary { flex-basis: 100%; }

  /* Long mono footer text shouldn't force a horizontal scroll */
  .status { gap: 6px 14px; }
  #time-label { margin-left: 0; }
}

@media (max-width: 380px) {
  .title { font-size: 0.95rem; }
  .controls-actions .btn { flex-basis: 100%; }
}
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { transition: none !important; }
}
