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

:root {
  --bg:        #0d0d0d;
  --bg2:       #141414;
  --bg3:       #1c1c1c;
  --border:    #2a2a2a;
  --text:      #d4d0c8;
  --muted:     #848484;
  --accent:    #e8c547;
  --accent2:   #4ec9b0;
  --danger:    #f44747;
  --compare:   #e8c547;
  --swap:      #f44747;
  --sorted:    #4ec9b0;
  --pivot:     #c586c0;
  --active:    #569cd6;
  --mono:      'IBM Plex Mono', monospace;
  --sans:      'IBM Plex Sans', sans-serif;
  --ctrl-h:    56px;
  --hdr-h:     44px;
  --nav-h:     38px;
}

html, body {
  width: 100%; height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: var(--sans);
  font-size: 14px;
  overflow: hidden;
  -webkit-font-smoothing: antialiased;
  overscroll-behavior: none;
}

body {
  display: flex;
  flex-direction: column;
  height: 100dvh; /* dvh = accounts for mobile browser chrome */
}

/* ── Header ── */
#hdr {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 16px;
  border-bottom: 1px solid var(--border);
  background: var(--bg2);
  flex-shrink: 0;
  height: var(--hdr-h);
}
#hdr-left { display: flex; align-items: center; gap: 10px; min-width: 0; }
#back-link {
  display: inline-flex; align-items: center; justify-content: center;
  width: 28px; height: 28px; flex-shrink: 0;
  border: 1px solid var(--border); border-radius: 4px;
  color: var(--muted); text-decoration: none; font-family: var(--mono); font-size: 0.9rem;
  transition: color 0.15s, border-color 0.15s;
}
#back-link:hover { color: var(--accent); border-color: var(--accent); }
#logo {
  font-family: var(--mono);
  font-size: 1rem;
  font-weight: 600;
  letter-spacing: 0.18em;
  color: var(--accent);
}
#tagline {
  font-family: var(--mono);
  font-size: 0.6rem;
  letter-spacing: 0.1em;
  color: var(--muted);
  text-transform: uppercase;
}
#hdr-stats { display: flex; gap: 16px; }
.stat { display: flex; flex-direction: column; align-items: flex-end; gap: 1px; }
.stat-val {
  font-family: var(--mono);
  font-size: 0.9rem;
  font-weight: 600;
  color: var(--accent);
  min-width: 48px;
  text-align: right;
}
.stat-lbl {
  font-size: 0.58rem;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--muted);
}

/* ── Algo nav ── */
#algo-nav {
  display: flex;
  gap: 2px;
  padding: 5px 12px;
  background: var(--bg2);
  border-bottom: 1px solid var(--border);
  overflow-x: auto;
  scrollbar-width: none;
  flex-shrink: 0;
  height: var(--nav-h);
  align-items: center;
  -webkit-overflow-scrolling: touch;
}
#algo-nav::-webkit-scrollbar { display: none; }
.algo-btn {
  background: transparent;
  border: 1px solid transparent;
  border-radius: 3px;
  padding: 4px 12px;
  font-family: var(--mono);
  font-size: 0.7rem;
  font-weight: 600;
  letter-spacing: 0.05em;
  color: var(--muted);
  cursor: pointer;
  white-space: nowrap;
  transition: all 0.12s;
  touch-action: manipulation;
  min-height: 28px;
  flex-shrink: 0;
  -webkit-tap-highlight-color: transparent;
}
.algo-btn:hover { color: var(--text); border-color: var(--border); }
.algo-btn.active { color: var(--bg); background: var(--accent); border-color: var(--accent); }

/* ── Main layout ── */
#main {
  display: flex;
  flex: 1;
  min-height: 0;
  overflow: hidden;
  position: relative;
}

/* ── Visualizer ── */
#vis-wrap {
  flex: 1;
  min-width: 0;
  position: relative;
  background: var(--bg);
}
#vis {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  display: block;
}
#done-banner {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  pointer-events: none;
  background: rgba(13,13,13,0.45);
  opacity: 1;
  transition: opacity 0.4s;
}
#done-banner.hidden { opacity: 0; pointer-events: none; }
#done-text {
  font-family: var(--mono);
  font-size: clamp(1.6rem, 6vw, 3rem);
  font-weight: 600;
  letter-spacing: 0.2em;
  color: var(--sorted);
  text-transform: uppercase;
  animation: pulse 1.6s ease-in-out infinite;
}
@keyframes pulse {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.4; }
}

/* ── Info panel — desktop sidebar ── */
#info-panel {
  width: 260px;
  flex-shrink: 0;
  background: var(--bg2);
  border-left: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  padding: 16px 14px;
  gap: 12px;
}
#panel-handle { display: none; }

#algo-name-wrap { display: flex; flex-direction: column; gap: 7px; }
#algo-title {
  font-family: var(--mono);
  font-size: 0.92rem;
  font-weight: 600;
  color: var(--accent);
  letter-spacing: 0.04em;
}
#complexity-row { display: flex; flex-wrap: wrap; gap: 4px; }
.badge {
  font-family: var(--mono);
  font-size: 0.62rem;
  padding: 2px 6px;
  border-radius: 2px;
  background: var(--bg3);
  border: 1px solid var(--border);
  color: var(--muted);
  white-space: nowrap;
}
.badge::before { color: var(--accent2); margin-right: 3px; }
#badge-best::before  { content: 'Ω'; }
#badge-avg::before   { content: 'Θ'; }
#badge-worst::before { content: 'O'; }
#badge-space::before { content: 'S'; }

#algo-desc {
  font-size: 0.75rem;
  line-height: 1.65;
  color: #8a8a8a;
  flex-shrink: 0;
  overflow-y: auto;
  max-height: 120px;
}
#step-log-wrap {
  display: flex;
  flex-direction: column;
  gap: 5px;
  flex: 1;
  min-height: 0;
}
#step-log-label {
  font-family: var(--mono);
  font-size: 0.58rem;
  letter-spacing: 0.12em;
  color: var(--muted);
  border-bottom: 1px solid var(--border);
  padding-bottom: 4px;
}
#step-log {
  font-family: var(--mono);
  font-size: 0.72rem;
  line-height: 1.55;
  color: var(--text);
  overflow-y: auto;
  flex: 1;
  scrollbar-width: thin;
  scrollbar-color: var(--border) transparent;
  display: flex;
  flex-direction: column;
  gap: 3px;
  -webkit-overflow-scrolling: touch;
}
.log-entry {
  padding: 4px 7px;
  border-radius: 2px;
  border-left: 2px solid transparent;
  word-break: break-word;
}
.log-entry.compare { border-left-color: var(--compare); background: rgba(232,197,71,0.06); }
.log-entry.swap    { border-left-color: var(--swap);    background: rgba(244,71,71,0.06); }
.log-entry.sorted  { border-left-color: var(--sorted);  background: rgba(78,201,176,0.06); }
.log-entry.info    { border-left-color: var(--active);  background: rgba(86,156,214,0.06); }
.log-entry.pivot   { border-left-color: var(--pivot);   background: rgba(197,134,192,0.06); }

/* ── Controls footer ── */
#controls {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 0 16px;
  background: var(--bg2);
  border-top: 1px solid var(--border);
  flex-shrink: 0;
  height: var(--ctrl-h);
}
#ctrl-left, #ctrl-right {
  display: flex;
  align-items: center;
  gap: 12px;
  flex: 1;
}
#ctrl-right { justify-content: flex-end; }
.ctrl-group { display: flex; align-items: center; gap: 7px; }
.ctrl-label {
  font-family: var(--mono);
  font-size: 0.58rem;
  letter-spacing: 0.1em;
  color: var(--muted);
  white-space: nowrap;
}
.ctrl-val {
  font-family: var(--mono);
  font-size: 0.72rem;
  color: var(--accent);
  min-width: 20px;
  text-align: right;
}

input[type="range"] {
  -webkit-appearance: none;
  appearance: none;
  width: 80px;
  height: 2px;
  background: var(--border);
  outline: none;
  cursor: pointer;
  touch-action: pan-x;
  flex-shrink: 0;
}
input[type="range"]::-webkit-slider-thumb {
  -webkit-appearance: none;
  width: 18px; height: 18px;
  border-radius: 50%;
  background: var(--accent);
  cursor: pointer;
}
input[type="range"]::-moz-range-thumb {
  width: 18px; height: 18px;
  border-radius: 50%;
  background: var(--accent);
  border: none;
  cursor: pointer;
}

select#sel-input {
  background: var(--bg3);
  border: 1px solid var(--border);
  border-radius: 3px;
  color: var(--text);
  font-family: var(--mono);
  font-size: 0.68rem;
  padding: 5px 8px;
  cursor: pointer;
  outline: none;
  appearance: none;
  -webkit-appearance: none;
  min-height: 32px;
  touch-action: manipulation;
}
select#sel-input option { background: var(--bg2); }

/* ── Transport buttons ── */
#ctrl-center {
  display: flex;
  align-items: center;
  gap: 5px;
  flex-shrink: 0;
}
.ctrl-btn {
  background: var(--bg3);
  border: 1px solid var(--border);
  border-radius: 4px;
  color: var(--text);
  font-size: 0.78rem;
  width: 36px; height: 36px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background 0.12s, color 0.12s;
  touch-action: manipulation;
  flex-shrink: 0;
  -webkit-tap-highlight-color: transparent;
  user-select: none;
}
.ctrl-btn:hover { background: var(--border); color: var(--accent); }
.ctrl-btn:active { transform: scale(0.93); }
.ctrl-btn.primary {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--bg);
  width: 44px; height: 44px;
  font-size: 1rem;
}
.ctrl-btn.primary:hover { background: #f0d060; }
.ctrl-btn.ghost {
  background: transparent;
  border-color: var(--border);
  color: var(--muted);
}
.ctrl-btn.ghost:hover { color: var(--text); }

/* ── Progress ── */
#progress-wrap {
  display: flex;
  align-items: center;
  gap: 7px;
}
#progress-bar {
  width: 70px; height: 3px;
  background: var(--border);
  border-radius: 2px;
  overflow: hidden;
}
#progress-fill {
  height: 100%;
  background: var(--accent);
  width: 0%;
  transition: width 0.08s;
  border-radius: 2px;
}
#progress-text {
  font-family: var(--mono);
  font-size: 0.62rem;
  color: var(--muted);
  white-space: nowrap;
}

/* Mobile extra row — hidden on desktop */
#ctrl-mobile-extra { display: none; }

/* ════════════════════════════════════════
   MOBILE STYLES  (≤ 700px)
════════════════════════════════════════ */
@media (max-width: 700px) {

  /* Tighten header */
  #hdr { padding: 0 12px; }
  #tagline { display: none; }
  .stat-val { font-size: 0.8rem; min-width: 36px; }
  #hdr-stats { gap: 12px; }

  /* Algo nav — bigger tap targets */
  #algo-nav { padding: 4px 8px; height: auto; min-height: 38px; }
  .algo-btn { padding: 6px 10px; min-height: 30px; }

  /* Main: canvas full width, no sidebar */
  #main { flex-direction: column; }
  #info-panel {
    /* Slide-up bottom sheet */
    position: fixed;
    bottom: 0; left: 0; right: 0;
    width: 100%;
    height: 60vh;
    max-height: 60vh;
    border-left: none;
    border-top: 1px solid var(--border);
    border-radius: 14px 14px 0 0;
    z-index: 200;
    padding: 0 14px 16px;
    gap: 10px;
    transform: translateY(calc(100% - 0px));
    transition: transform 0.3s cubic-bezier(0.4,0,0.2,1);
    box-shadow: 0 -8px 32px rgba(0,0,0,0.5);
  }
  #info-panel.open { transform: translateY(0); }

  #panel-handle {
    display: flex;
    justify-content: center;
    padding: 10px 0 6px;
    cursor: pointer;
    touch-action: none;
    flex-shrink: 0;
  }
  #panel-handle-bar {
    width: 36px; height: 4px;
    background: var(--border);
    border-radius: 2px;
  }

  /* Controls footer: stack into 2 rows */
  #controls {
    height: auto;
    flex-wrap: wrap;
    padding: 8px 12px;
    gap: 6px;
    row-gap: 6px;
  }
  #ctrl-left { display: none; }       /* hide desktop sliders */
  /* Input type + progress were hidden on mobile, leaving no way to pick
     Reversed / Nearly sorted; show them as a compact full-width row */
  #ctrl-right { display: flex; width: 100%; order: 3; justify-content: space-between; gap: 10px; }
  #ctrl-right #progress-bar { width: 60px; }

  #ctrl-center { flex: 1; justify-content: center; }
  .ctrl-btn { width: 40px; height: 40px; }
  .ctrl-btn.primary { width: 48px; height: 48px; font-size: 1.1rem; }

  /* Mobile extra row — shown on mobile */
  #ctrl-mobile-extra {
    display: flex;
    align-items: center;
    gap: 10px;
    width: 100%;
    padding-top: 4px;
    border-top: 1px solid var(--border);
  }
  #ctrl-mobile-extra .ctrl-group { flex: 1; }
  #ctrl-mobile-extra input[type="range"] { width: 100%; flex: 1; }
}

@media (max-width: 380px) {
  .algo-btn { padding: 5px 8px; font-size: 0.65rem; }
  .stat-val { min-width: 28px; font-size: 0.75rem; }
}
/* ── Keyboard focus ── */
.algo-btn:focus-visible, .ctrl-btn:focus-visible, #back-link:focus-visible,
#sel-input:focus-visible, input[type="range"]:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

/* ── Reduced motion ── */
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { transition-duration: 0.01ms !important; animation-duration: 0.01ms !important; scroll-behavior: auto !important; }
}

/* ── Narrow phones (≤ 380px): keep header within the viewport ── */
@media (max-width: 380px) {
  #hdr { gap: 8px; }
  #hdr-stats { gap: 8px; }
}
