:root {
  --bg: #0f1420;
  --bg-elevated: #161d2e;
  --bg-card: #1b2436;
  --border: #2a3550;
  --text: #e8ecf7;
  --text-dim: #9aa5c0;
  --accent: #6c8cff;
  --accent-2: #33d6a6;
  --accent-3: #ff9f5a;
  --danger: #ff6b7a;
  --bar-default: #3d4c73;
  --bar-compare: #ff9f5a;
  --bar-swap: #ff6b7a;
  --bar-sorted: #33d6a6;
  --bar-pivot: #c792ff;
  --radius: 14px;
  --shadow: 0 8px 24px rgba(0,0,0,0.35);
  --nav-h: 60px;
  color-scheme: dark;
}

:root[data-theme="light"] {
  --bg: #f4f6fb;
  --bg-elevated: #ffffff;
  --bg-card: #ffffff;
  --border: #dce2ef;
  --text: #1a2033;
  --text-dim: #5b6480;
  --accent: #3357e0;
  --accent-2: #0f9d76;
  --accent-3: #d8781b;
  --danger: #d63a4b;
  --bar-default: #c7cfe6;
  --bar-compare: #d8781b;
  --bar-swap: #d63a4b;
  --bar-sorted: #0f9d76;
  --bar-pivot: #8b3fd6;
  --shadow: 0 8px 24px rgba(20,30,60,0.08);
  color-scheme: light;
}

@media (prefers-color-scheme: light) {
  :root:not([data-theme="dark"]) {
    --bg: #f4f6fb;
    --bg-elevated: #ffffff;
    --bg-card: #ffffff;
    --border: #dce2ef;
    --text: #1a2033;
    --text-dim: #5b6480;
    --accent: #3357e0;
    --accent-2: #0f9d76;
    --accent-3: #d8781b;
    --danger: #d63a4b;
    --bar-default: #c7cfe6;
    --bar-compare: #d8781b;
    --bar-swap: #d63a4b;
    --bar-sorted: #0f9d76;
    --bar-pivot: #8b3fd6;
    --shadow: 0 8px 24px rgba(20,30,60,0.08);
    color-scheme: light;
  }
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  -webkit-tap-highlight-color: transparent;
}

a { color: inherit; }

button, select, input {
  font-family: inherit;
  color: inherit;
}

#app {
  min-height: 100%;
  padding-bottom: calc(var(--nav-h) + env(safe-area-inset-bottom, 0px) + 12px);
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 14px 16px;
  position: sticky;
  top: 0;
  z-index: 20;
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: blur(10px);
  border-bottom: 1px solid var(--border);
}

.brand {
  display: flex;
  align-items: center;
  gap: 8px;
  font-weight: 700;
  font-size: 1.05rem;
  letter-spacing: -0.01em;
}
.brand .dot { color: var(--accent); }

.icon-btn {
  width: 38px;
  height: 38px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--bg-elevated);
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  font-size: 1.1rem;
}
.icon-btn:active { transform: scale(0.94); }

nav.tabbar {
  position: fixed;
  left: 0; right: 0; bottom: 0;
  display: flex;
  justify-content: space-around;
  background: color-mix(in srgb, var(--bg-elevated) 96%, transparent);
  border-top: 1px solid var(--border);
  padding: 6px 4px calc(6px + env(safe-area-inset-bottom, 0px));
  z-index: 30;
  backdrop-filter: blur(10px);
}

nav.tabbar a {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  text-decoration: none;
  color: var(--text-dim);
  font-size: 0.68rem;
  padding: 6px 2px;
  border-radius: 10px;
}
nav.tabbar a .ic { font-size: 1.25rem; line-height: 1; }
nav.tabbar a.active { color: var(--accent); }

main {
  max-width: 1100px;
  margin: 0 auto;
  padding: 16px;
}

.hero {
  padding: 28px 4px 10px;
}
.hero h1 {
  font-size: clamp(1.6rem, 5vw, 2.4rem);
  margin: 0 0 8px;
  letter-spacing: -0.02em;
}
.hero p {
  color: var(--text-dim);
  font-size: 1rem;
  max-width: 60ch;
  line-height: 1.5;
}

.grid-cards {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(240px, 1fr));
  gap: 14px;
  margin-top: 18px;
}

.card {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 18px;
  text-decoration: none;
  display: flex;
  flex-direction: column;
  gap: 8px;
  box-shadow: var(--shadow);
  transition: transform 0.15s ease, border-color 0.15s ease;
}
.card:active { transform: scale(0.98); }
.card .emoji { font-size: 1.8rem; }
.card h3 { margin: 0; font-size: 1.05rem; }
.card p { margin: 0; color: var(--text-dim); font-size: 0.88rem; line-height: 1.4; }
.card .tags { display: flex; gap: 6px; flex-wrap: wrap; margin-top: 4px; }
.tag {
  font-size: 0.68rem;
  padding: 3px 8px;
  border-radius: 999px;
  background: color-mix(in srgb, var(--accent) 16%, transparent);
  color: var(--accent);
  border: 1px solid color-mix(in srgb, var(--accent) 30%, transparent);
  white-space: nowrap;
}

.section-header {
  margin-bottom: 14px;
}
.section-header h2 {
  margin: 0 0 4px;
  font-size: 1.4rem;
  letter-spacing: -0.01em;
}
.section-header p {
  margin: 0;
  color: var(--text-dim);
  font-size: 0.92rem;
  line-height: 1.5;
}

.panel {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px;
  margin-bottom: 16px;
  box-shadow: var(--shadow);
}

.controls-row {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  align-items: center;
  margin-bottom: 12px;
}

.btn {
  border: 1px solid var(--border);
  background: var(--bg-elevated);
  color: var(--text);
  padding: 9px 14px;
  border-radius: 10px;
  font-size: 0.88rem;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  white-space: nowrap;
}
.btn:active { transform: scale(0.96); }
.btn.primary {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
  font-weight: 600;
}
.btn.ghost { background: transparent; }
.btn:disabled { opacity: 0.45; cursor: not-allowed; }

.select, select.select {
  border: 1px solid var(--border);
  background: var(--bg-elevated);
  color: var(--text);
  padding: 9px 12px;
  border-radius: 10px;
  font-size: 0.88rem;
}

.slider-group {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 0.8rem;
  color: var(--text-dim);
}
.slider-group input[type=range] { width: 110px; accent-color: var(--accent); }

.viz-box {
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 10px;
  overflow: hidden;
}

.bars-wrap {
  display: flex;
  align-items: flex-end;
  gap: 2px;
  height: 220px;
  width: 100%;
}
.bar {
  flex: 1;
  background: var(--bar-default);
  border-radius: 3px 3px 0 0;
  position: relative;
  transition: background-color 0.15s ease;
  min-width: 2px;
}
.bar.compare { background: var(--bar-compare); }
.bar.swap { background: var(--bar-swap); }
.bar.sorted { background: var(--bar-sorted); }
.bar.pivot { background: var(--bar-pivot); }
.bar .val {
  position: absolute;
  bottom: 100%;
  left: 50%;
  transform: translateX(-50%);
  font-size: 0.6rem;
  color: var(--text-dim);
  margin-bottom: 2px;
  white-space: nowrap;
}

.status-line {
  margin-top: 10px;
  font-size: 0.85rem;
  color: var(--text-dim);
  min-height: 1.4em;
}
.status-line strong { color: var(--text); }

.legend {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  margin-top: 10px;
  font-size: 0.75rem;
  color: var(--text-dim);
}
.legend span { display: inline-flex; align-items: center; gap: 5px; }
.swatch { width: 10px; height: 10px; border-radius: 3px; display: inline-block; }

table.info-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.85rem;
}
table.info-table th, table.info-table td {
  text-align: left;
  padding: 8px 10px;
  border-bottom: 1px solid var(--border);
}
table.info-table th { color: var(--text-dim); font-weight: 600; }
table.info-table td.mono, table.info-table th.mono { font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; }
table.info-table tr.active-row { background: color-mix(in srgb, var(--accent) 10%, transparent); }

pre.code-block {
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 12px 14px;
  font-size: 0.78rem;
  line-height: 1.6;
  overflow-x: auto;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}
pre.code-block .line { display: block; padding: 1px 4px; border-radius: 4px; }
pre.code-block .line.active { background: color-mix(in srgb, var(--accent) 22%, transparent); color: var(--text); }

.two-col {
  display: grid;
  grid-template-columns: 1fr;
  gap: 14px;
  min-width: 0;
}
.two-col > * { min-width: 0; }
@media (min-width: 760px) {
  .two-col { grid-template-columns: 1.3fr 1fr; }
}

.table-scroll { overflow-x: auto; }

.numline {
  display: flex;
  flex-wrap: wrap;
  gap: 4px;
  padding: 6px;
}
.num-cell {
  min-width: 34px;
  height: 34px;
  flex: 1 1 34px;
  max-width: 52px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--bar-default);
  border-radius: 6px;
  font-size: 0.78rem;
  font-weight: 600;
  position: relative;
  transition: background-color 0.15s ease, opacity 0.15s ease;
}
.num-cell .ptr {
  position: absolute;
  top: 100%;
  left: 50%;
  transform: translateX(-50%);
  font-size: 0.6rem;
  color: var(--text-dim);
  white-space: nowrap;
  margin-top: 2px;
}
.num-cell.eliminated { opacity: 0.3; }
.num-cell.mid { background: var(--bar-pivot); color: #fff; }
.num-cell.current { background: var(--bar-compare); }
.num-cell.found { background: var(--bar-sorted); color: #fff; }

.grid-viz {
  display: grid;
  gap: 2px;
  touch-action: none;
  user-select: none;
}
.cell {
  aspect-ratio: 1;
  background: var(--bar-default);
  border-radius: 2px;
}
.cell.wall { background: #3a3f4d; }
.cell.mud { background: #8a5a2b; }
.cell.current { background: var(--accent); }
.cell.start { background: var(--accent-2); }
.cell.end { background: var(--danger); }
.cell.visited { background: color-mix(in srgb, var(--accent) 55%, var(--bg-elevated)); }
.cell.frontier { background: var(--accent-3); }
.cell.path { background: var(--accent-2); }

.chip-row { display: flex; gap: 8px; flex-wrap: wrap; margin-bottom: 10px; }
.chip {
  padding: 7px 12px;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: var(--bg-elevated);
  font-size: 0.82rem;
  cursor: pointer;
}
.chip.active { background: var(--accent); border-color: var(--accent); color: #fff; font-weight: 600; }

.stat-row { display: flex; gap: 14px; flex-wrap: wrap; margin-top: 10px; }
.stat {
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 10px 14px;
  min-width: 100px;
}
.stat .num { font-size: 1.3rem; font-weight: 700; }
.stat .lbl { font-size: 0.72rem; color: var(--text-dim); }

.tree-wrap { overflow-x: auto; padding: 10px 0; }
svg.tree-svg text { fill: var(--text-dim); font-size: 10px; font-family: ui-monospace, monospace; }
svg.tree-svg circle { fill: var(--bg-elevated); stroke: var(--border); stroke-width: 1.5; }
svg.tree-svg circle.visiting { stroke: var(--accent-3); fill: color-mix(in srgb, var(--accent-3) 25%, var(--bg-elevated)); }
svg.tree-svg circle.done { stroke: var(--accent-2); }
svg.tree-svg circle.cached { stroke: var(--bar-pivot); fill: color-mix(in srgb, var(--bar-pivot) 25%, var(--bg-elevated)); }
svg.tree-svg line { stroke: var(--border); stroke-width: 1.5; }

.callout {
  border-left: 3px solid var(--accent);
  background: color-mix(in srgb, var(--accent) 8%, transparent);
  padding: 10px 14px;
  border-radius: 0 8px 8px 0;
  font-size: 0.86rem;
  line-height: 1.5;
  margin: 10px 0;
}

.bigo-chart-wrap { overflow-x: auto; }
svg.bigo-svg text { fill: var(--text-dim); font-size: 10px; }

@media (min-width: 900px) {
  #app { padding-bottom: 24px; }
  nav.tabbar { display: none; }
  .topbar .nav-links { display: flex; gap: 4px; }
  .nav-links a {
    text-decoration: none;
    color: var(--text-dim);
    padding: 8px 12px;
    border-radius: 8px;
    font-size: 0.9rem;
  }
  .nav-links a.active { color: var(--accent); background: color-mix(in srgb, var(--accent) 12%, transparent); font-weight: 600; }
}
@media (max-width: 899px) {
  .topbar .nav-links { display: none; }
}

.hidden { display: none !important; }

::-webkit-scrollbar { height: 8px; width: 8px; }
::-webkit-scrollbar-thumb { background: var(--border); border-radius: 4px; }
