:root {
  --bg: #08111f;
  --bg-2: #0c1729;
  --panel: rgba(14, 24, 41, 0.88);
  --panel-2: rgba(18, 32, 54, 0.92);
  --text: #edf4ff;
  --muted: #9fb1cc;
  --primary: #4da3ff;
  --primary-2: #2f7fe0;
  --danger: #ff6b81;
  --bar: linear-gradient(180deg, #60a5fa 0%, #2563eb 100%);
  --bar-active: linear-gradient(180deg, #ffd166 0%, #f59e0b 100%);
  --bar-swap: linear-gradient(180deg, #fb7185 0%, #e11d48 100%);
  --bar-sorted: linear-gradient(180deg, #34d399 0%, #059669 100%);
  --border: rgba(148, 163, 184, 0.18);
  --shadow: 0 20px 50px rgba(0, 0, 0, 0.35);
  --glow: 0 0 0 1px rgba(77, 163, 255, 0.12), 0 0 24px rgba(77, 163, 255, 0.08);
  --radius-xl: 26px;
}

* { box-sizing: border-box; }
html { scroll-behavior: smooth; }

body {
  margin: 0;
  font-family: "Noto Sans TC", "Microsoft JhengHei", sans-serif;
  color: var(--text);
  background:
    radial-gradient(circle at top left, rgba(77, 163, 255, 0.18), transparent 30%),
    radial-gradient(circle at top right, rgba(139, 92, 246, 0.12), transparent 26%),
    linear-gradient(180deg, var(--bg) 0%, var(--bg-2) 100%);
  min-height: 100vh;
}

a {
  color: inherit;
  text-decoration: none;
}

.wrap {
  width: min(1240px, calc(100% - 28px));
  margin: 20px auto 40px;
  display: grid;
  gap: 20px;
}

.hero,
.panel,
.code-box {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius-xl);
  box-shadow: var(--shadow), var(--glow);
  backdrop-filter: blur(14px);
}

.hero {
  padding: 28px;
  display: grid;
  gap: 18px;
}

.hero-top {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 14px;
}

.badge {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  width: fit-content;
  padding: 8px 12px;
  border-radius: 999px;
  background: rgba(77, 163, 255, 0.12);
  border: 1px solid rgba(77, 163, 255, 0.22);
  color: #cfe5ff;
  font-size: 0.9rem;
  font-weight: 700;
}

.hero-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
}

.anchor-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 11px 16px;
  border-radius: 14px;
  background: rgba(255,255,255,0.05);
  border: 1px solid rgba(255,255,255,0.08);
  font-weight: 700;
}

h1 {
  margin: 0;
  font-size: clamp(2rem, 4.6vw, 3.3rem);
  line-height: 1.08;
}

.sub,
.section-desc,
.section-copy,
.mini-card p,
.mini-card li {
  color: var(--muted);
  line-height: 1.8;
}

.hero-stats,
.stats,
.complexity {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 12px;
}

.hero-stat,
.stat {
  padding: 16px;
  border-radius: 18px;
  background: rgba(255,255,255,0.04);
  border: 1px solid rgba(255,255,255,0.08);
}

.hero-stat small,
.stat small {
  display: block;
  color: var(--muted);
  margin-bottom: 6px;
}

.hero-stat strong,
.stat strong {
  font-size: 1.08rem;
  color: #fff;
}

.grid-main,
.theory-grid {
  display: grid;
  grid-template-columns: 1.2fr 0.8fr;
  gap: 20px;
  align-items: start;
}

.theory-grid {
  grid-template-columns: 1fr 1fr;
}

.panel,
.code-box {
  padding: 22px;
}

.section-head {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  align-items: center;
  gap: 10px;
  margin-bottom: 16px;
}

.section-title {
  margin: 0;
  font-size: 1.15rem;
}

.controls,
.row {
  display: grid;
  gap: 10px;
}

.row-grid-2 {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
}

.row-inline {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
}

label {
  font-weight: 700;
  color: #e5eefc;
}

input[type="text"],
select {
  width: 100%;
  padding: 13px 14px;
  border-radius: 14px;
  border: 1px solid rgba(148, 163, 184, 0.2);
  background: rgba(255,255,255,0.04);
  color: var(--text);
  font-size: 1rem;
  outline: none;
}

input[type="range"] {
  width: 100%;
  accent-color: var(--primary);
}

button {
  border: none;
  border-radius: 14px;
  padding: 12px 16px;
  font-size: 0.98rem;
  font-weight: 800;
  cursor: pointer;
}

.btn-primary {
  background: linear-gradient(135deg, var(--primary) 0%, var(--primary-2) 100%);
  color: white;
}

.btn-light {
  background: rgba(255,255,255,0.06);
  color: #dfeafe;
  border: 1px solid rgba(255,255,255,0.08);
}

.btn-danger {
  background: rgba(255, 107, 129, 0.12);
  color: #ffc4cf;
  border: 1px solid rgba(255, 107, 129, 0.18);
}

.message {
  background: rgba(77, 163, 255, 0.1);
  border: 1px solid rgba(77, 163, 255, 0.22);
  color: #d8eaff;
  padding: 13px 14px;
  border-radius: 14px;
  min-height: 54px;
}

.viz {
  min-height: 390px;
  display: flex;
  align-items: end;
  justify-content: center;
  gap: 8px;
  padding: 18px;
  border-radius: 22px;
  background: linear-gradient(180deg, rgba(255,255,255,0.02) 0%, rgba(77,163,255,0.05) 100%);
  border: 1px solid rgba(148, 163, 184, 0.15);
  overflow: hidden;
}

.bar-wrap {
  flex: 1;
  min-width: 30px;
  max-width: 68px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: end;
  gap: 8px;
}

.bar {
  width: 100%;
  border-radius: 18px 18px 8px 8px;
  background: var(--bar);
  display: flex;
  align-items: start;
  justify-content: center;
  color: #f8fbff;
  font-weight: 800;
  padding-top: 8px;
  transition: height .25s ease, transform .18s ease;
}

.bar.active { background: var(--bar-active); transform: scale(1.04); }
.bar.swap { background: var(--bar-swap); transform: scale(1.05); }
.bar.sorted { background: var(--bar-sorted); }

.bar-index {
  font-size: 0.82rem;
  color: var(--muted);
}

.legend {
  display: flex;
  flex-wrap: wrap;
  gap: 12px;
  color: var(--muted);
  margin-bottom: 14px;
}

.legend span {
  display: inline-flex;
  align-items: center;
  gap: 8px;
}

.dot {
  width: 14px;
  height: 14px;
  border-radius: 50%;
  display: inline-block;
}

.dot-normal { background: #3b82f6; }
.dot-compare { background: #f59e0b; }
.dot-swap { background: #e11d48; }
.dot-sorted { background: #059669; }

.compare-box {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
  margin-bottom: 14px;
}

.num-box {
  padding: 16px;
  border-radius: 18px;
  background: rgba(255,255,255,0.04);
  border: 1px solid rgba(255,255,255,0.08);
  text-align: center;
}

.num-box strong {
  display: block;
  font-size: 1.8rem;
  margin-top: 4px;
}

.steps {
  max-height: 340px;
  overflow: auto;
  padding-right: 4px;
  scroll-behavior: smooth;
}

.step {
  padding: 11px 12px;
  border-radius: 14px;
  background: rgba(255,255,255,0.04);
  border: 1px solid rgba(255,255,255,0.08);
  margin-bottom: 8px;
}

.step.active-step {
  border-color: rgba(77, 163, 255, 0.55);
  background: rgba(77, 163, 255, 0.12);
}

.mini-card {
  margin-top: 14px;
  padding: 18px;
  border-radius: 20px;
  background: var(--panel-2);
  border: 1px solid rgba(255,255,255,0.08);
}

ul.clean {
  margin: 0;
  padding-left: 1.2rem;
}

.code-toolbar {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  margin-bottom: 14px;
}

.tab-btn.active {
  background: linear-gradient(135deg, rgba(77,163,255,0.9), rgba(139,92,246,0.9));
  color: white;
}

.code-interaction {
  display: grid;
  gap: 14px;
}

.code-line-list {
  display: grid;
  gap: 8px;
}

.code-line {
  display: grid;
  grid-template-columns: 64px minmax(0, 1fr);
  align-items: start;
  gap: 14px;
  width: 100%;
  padding: 12px 14px;
  border-radius: 14px;
  background: #0a1323;
  border: 1px solid rgba(148, 163, 184, 0.12);
  color: #dbe8ff;
  cursor: pointer;
  text-align: left;
}

.code-line.active {
  background: rgba(77, 163, 255, 0.14);
  border-color: rgba(77, 163, 255, 0.55);
}

.code-line .num {
  color: #7ea8df;
  font-weight: 800;
  text-align: center;
  padding: 8px 10px;
  border-radius: 10px;
  background: rgba(255,255,255,0.04);
}

.code-line .txt {
  display: block;
  width: 100%;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  white-space: pre;
  overflow-x: auto;
  line-height: 1.7;
  tab-size: 2;
  font-size: 0.98rem;
}

.code-line .txt code {
  font: inherit;
  white-space: inherit;
}

.token-keyword { color: #7dd3fc; }
.token-variable { color: #f8fafc; }
.token-number { color: #f9a8d4; }
.token-operator { color: #c4b5fd; }
.token-brace { color: #fbbf24; }
.token-method { color: #86efac; }
.token-pseudo { color: #e2e8f0; font-weight: 700; }

.code-explain {
  padding: 16px;
  border-radius: 18px;
  background: rgba(255,255,255,0.04);
  border: 1px solid rgba(255,255,255,0.08);
  color: #dce9fd;
  line-height: 1.8;
  min-height: 90px;
}

@media (max-width: 980px) {
  .grid-main,
  .theory-grid {
    grid-template-columns: 1fr;
  }

  .hero-stats,
  .stats,
  .complexity {
    grid-template-columns: repeat(2, 1fr);
  }
}

@media (max-width: 720px) {
  .wrap {
    width: min(100% - 16px, 1240px);
    margin: 12px auto 28px;
  }

  .hero,
  .panel,
  .code-box {
    padding: 18px;
    border-radius: 20px;
  }

  .row-grid-2,
  .compare-box,
  .hero-stats,
  .stats,
  .complexity {
    grid-template-columns: 1fr;
  }

  .row-inline {
    display: grid;
    grid-template-columns: 1fr 1fr;
  }

  .row-inline button:first-child,
  .row-inline button:last-child {
    grid-column: 1 / -1;
  }

  .viz {
    min-height: 310px;
    gap: 5px;
    padding: 12px;
  }

  .bar-wrap {
    min-width: 20px;
  }

  .code-line {
    grid-template-columns: 52px minmax(0, 1fr);
    gap: 10px;
    padding: 10px;
  }

  .code-line .txt {
    font-size: 0.92rem;
  }
}