:root {
  color-scheme: dark;
  --bg: #191919;
  --surface: #222;
  --surface-2: #292929;
  --border: #424242;
  --border-soft: #333;
  --text: #f5f5f5;
  --muted: #bcbcbc;
  --accent: #79d54d;
  --code: #121212;
  --cx-const: #6fe3c4;
  --cx-log: #59c7e8;
  --cx-linear: #f2c14e;
  --cx-nlogn: #f2924e;
  --cx-quad: #ff8589;
  --cx-other: #bbbde8;
  --cx-pattern: #b1bac9;
  --sans: 'IBM Plex Sans', 'Segoe UI', sans-serif;
  --display: 'Space Grotesk', 'Segoe UI', sans-serif;
  --mono: 'JetBrains Mono', Consolas, monospace;
}

:root[data-theme='light'] {
  color-scheme: light;
  --bg: #fff;
  --surface: #f5f5f5;
  --surface-2: #ececec;
  --border: #d4d4d4;
  --border-soft: #e5e5e5;
  --text: #191919;
  --muted: #5d5d5d;
  --code: #fafafa;
  --cx-const: #00875a;
  --cx-log: #0072e5;
  --cx-linear: #9b6800;
  --cx-nlogn: #cf4b00;
  --cx-quad: #d4002a;
  --cx-other: #7137d8;
  --cx-pattern: #52606d;
}

:root[data-theme='light'] .complexity-legend code,
:root[data-theme='light'] .complexity { font-weight: 600; }

* { box-sizing: border-box; }
html { scroll-behavior: smooth; scroll-padding-top: 84px; }
body {
  margin: 0;
  min-height: 100vh;
  background: var(--bg);
  color: var(--text);
  font: 1rem/1.55 var(--sans);
}
[hidden] { display: none !important; }
button, input { font: inherit; }
button, summary, input[type='color'] { cursor: pointer; }
a { color: var(--accent); text-underline-offset: 3px; }
code { font-family: var(--mono); }
:focus-visible { outline: 3px solid var(--accent); outline-offset: 3px; }
::selection { color: var(--bg); background: var(--accent); }
.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip-path: inset(50%);
  white-space: nowrap;
}
.skip-link {
  position: fixed;
  z-index: 100;
  top: 8px;
  left: 8px;
  padding: 10px;
  background: var(--surface);
  transform: translateY(-150%);
}
.skip-link:focus { transform: none; }

/* Barra superior */
.topbar {
  position: sticky;
  z-index: 70;
  top: 0;
  height: 62px;
  display: grid;
  grid-template-columns: minmax(44px, 1fr) auto minmax(44px, 1fr);
  align-items: center;
  justify-content: space-between;
  padding: 7px clamp(14px, 3vw, 28px);
  border-bottom: 2px solid var(--accent);
  background: var(--bg);
}
.brand {
  min-width: 0;
  max-width: 100%;
  justify-self: center;
  overflow: hidden;
  color: var(--text);
  font: 600 1rem var(--mono);
  text-overflow: ellipsis;
  text-decoration: none;
  white-space: nowrap;
}
.menu-button { justify-self: start; }
.appearance { justify-self: end; }
.menu-button, .close-button {
  min-width: 44px;
  min-height: 44px;
  border: 1px solid var(--border);
  border-radius: 7px;
  background: var(--surface);
  color: var(--text);
}
.appearance { display: flex; align-items: center; gap: 12px; }
.theme-switch {
  display: flex;
  padding: 2px;
  border: 1px solid var(--border);
  border-radius: 8px;
}
.theme-switch button {
  width: 40px;
  height: 40px;
  display: grid;
  place-items: center;
  border: 0;
  border-radius: 5px;
  background: transparent;
  color: var(--muted);
}
.theme-switch svg { width: 26px; height: 26px; }
.theme-switch button[aria-pressed='true'] {
  background: var(--text);
  color: var(--bg);
}
.color-control { display: grid; place-items: center; width: 44px; height: 44px; }
.color-control input {
  width: 36px;
  height: 36px;
  padding: 0;
  overflow: hidden;
  border: 1px solid var(--border);
  border-radius: 50%;
  background: var(--accent);
}
.color-control input::-webkit-color-swatch-wrapper { padding: 0; }
.color-control input::-webkit-color-swatch { border: 0; border-radius: 50%; }
.color-control input::-moz-color-swatch { border: 0; border-radius: 50%; }

/* Índice siempre disponible desde el botón superior */
.overlay {
  position: fixed;
  z-index: 75;
  inset: 0;
  visibility: hidden;
  background: rgb(0 0 0 / 65%);
  opacity: 0;
}
.overlay.open { visibility: visible; opacity: 1; }
.sidebar {
  position: fixed;
  z-index: 80;
  inset: 0 auto 0 0;
  width: min(340px, 90vw);
  padding: 18px;
  overflow-y: auto;
  border-right: 1px solid var(--border);
  background: var(--surface);
  transform: translateX(-100%);
  visibility: hidden;
}
.sidebar.open { visibility: visible; transform: none; }
body.menu-open { overflow: hidden; }
.close-button { width: 100%; margin-bottom: 12px; }
.support-link {
  display: block;
  min-height: 44px;
  padding: 10px 8px;
  border-radius: 5px;
  text-decoration: none;
  color: var(--muted);
}
.home-link { margin-bottom: 12px; }
.support-link:hover, .support-link[aria-current] {
  color: var(--accent);
  background: var(--surface-2);
}
.nav-section h2 {
  margin: 24px 8px 6px;
  color: var(--accent);
  font: 600 .9rem/1.4 var(--display);
}
.nav-section ul { list-style: none; margin: 0; padding: 0; }
.nav-section a {
  display: block;
  min-height: 42px;
  padding: 9px 8px;
  border-radius: 5px;
  color: var(--muted);
  font: .9rem/1.5 var(--sans);
  text-decoration: none;
}
.nav-section a:hover, .nav-section a[aria-current] {
  color: var(--accent);
  background: var(--surface-2);
}

/* Cabecera y búsqueda */
main { width: min(100%, 1120px); margin: 0 auto; padding: 32px clamp(16px, 4vw, 52px) 64px; }
.hero h1 {
  margin: 0;
  font: 600 clamp(1.7rem, 4.3vw, 3.35rem)/1.18 var(--display);
  letter-spacing: -.035em;
}
.hero h1 span { color: var(--accent); }
#contenido:focus, #contenido:focus-visible { outline: 0; }
.complexity-legend {
  display: flex;
  flex-wrap: wrap;
  gap: 10px 22px;
  margin: 18px 0;
}
.complexity-legend code::before { content: '• '; }
.cx-const { color: var(--cx-const); }
.cx-log { color: var(--cx-log); }
.cx-linear { color: var(--cx-linear); }
.cx-nlogn { color: var(--cx-nlogn); }
.cx-quad { color: var(--cx-quad); }
.cx-other { color: var(--cx-other); }
.cx-pattern { color: var(--cx-pattern); }
.search {
  position: relative;
  display: flex;
  align-items: center;
  margin: 0 0 22px;
  background: var(--bg);
}
.search svg {
  position: absolute;
  left: 13px;
  width: 22px;
  fill: none;
  stroke: var(--muted);
  stroke-width: 1.7;
}
.search input {
  width: 100%;
  height: 48px;
  padding: 10px 40px 10px 44px;
  border: 2px solid var(--border);
  border-radius: 10px;
  background: var(--surface);
  color: var(--text);
}
.search:focus-within svg { stroke: var(--accent); }
.search input:focus, .search input:focus-visible { border-color: var(--accent); outline: 0; }
.search input::placeholder { color: var(--muted); }

/* Base común y ayudas opcionales */
.common-base {
  margin: 0 0 24px;
  padding: 14px 16px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface);
}
.common-base h2 { margin: 0 0 10px; font: 600 1rem var(--display); }
.common-base pre {
  margin: 0;
  padding: 12px;
  overflow-x: auto;
  border: 1px solid var(--border-soft);
  border-radius: 6px;
  background: var(--code);
  font-size: .86rem;
  line-height: 1.55;
}
.support { margin-bottom: 40px; border-block: 1px solid var(--border-soft); }
.support > details + details { border-top: 1px solid var(--border-soft); }
.support summary { min-height: 44px; padding: 11px 0; color: var(--muted); }
.support summary::marker { color: var(--accent); }
.learning-body { padding: 0 12px 14px; }
.path-list { padding-left: 24px; }
.path-list li { margin-bottom: 12px; }
.path-list p { margin: 4px 0; color: var(--muted); }
.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; }
th, td { padding: 10px 12px; text-align: left; border-bottom: 1px solid var(--border-soft); }
th:first-child { width: 62%; }
td { color: var(--muted); }

/* Resultados */
.search-results { margin: -8px 0 24px; border-bottom: 1px solid var(--border); }
.search-results p { color: var(--muted); font-size: .9rem; }
.search-results ul { max-height: 360px; margin: 0; padding: 0; overflow-y: auto; list-style: none; }
.search-results a {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 4px 14px;
  min-height: 52px;
  padding: 10px 8px;
  border-top: 1px solid var(--border-soft);
  text-decoration: none;
}
.result-name { min-width: 0; overflow-wrap: anywhere; color: var(--text); }

/* Catálogo */
.category { margin-bottom: 56px; }
.category > h2 {
  margin: 0 0 28px;
  padding-bottom: 10px;
  border-bottom: 2px solid var(--accent);
  font: 600 1.5rem var(--display);
  text-align: center;
}
.structure { margin-bottom: 48px; scroll-margin-top: 140px; }
.structure > header h3 { margin: 0; font: 600 1.16rem var(--display); }
.structure > header p { margin: 8px 0 12px; color: var(--muted); }
.code-block {
  margin: 10px 0;
  overflow: hidden;
  border: 1px solid var(--border);
  border-radius: 7px;
  background: var(--code);
}
.code-block > span {
  display: block;
  padding: 7px 12px;
  border-bottom: 1px solid var(--border-soft);
  color: var(--muted);
  font-size: .78rem;
}
.code-block pre { margin: 0; padding: 12px; overflow-x: auto; font-size: .87rem; line-height: 1.6; }
.note { margin: 8px 0; color: var(--muted); font-size: .94rem; }
.methods { display: flex; flex-direction: column; gap: 8px; margin-top: 16px; }
.method {
  border: 1px solid var(--border);
  border-radius: 7px;
  background: var(--surface);
  scroll-margin-top: 140px;
}
.method[open] { border-color: var(--accent); }
.method summary {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 12px;
  padding: 12px 14px;
  list-style-position: inside;
}
.method summary > span { min-width: 0; }
.method-name { overflow-wrap: anywhere; }
.method small { display: block; margin: 3px 0 0 20px; color: var(--muted); font-size: .92rem; }
.complexity { align-self: start; white-space: nowrap; text-align: right; }
.method-body { padding: 2px 14px 12px; border-top: 1px solid var(--border-soft); }
.anchor { display: block; scroll-margin-top: 140px; }
footer { padding-top: 24px; border-top: 1px solid var(--border-soft); color: var(--muted); font-size: .88rem; text-align: center; }

@media (max-width: 620px) {
  main { padding-top: 24px; }
  .topbar {
    grid-template-columns: auto minmax(0, 1fr) auto;
    column-gap: 10px;
    padding-inline: 14px;
  }
  .hero h1 { font-size: clamp(1.65rem, 9vw, 2.2rem); }
  .appearance { gap: 8px; }
  .brand { font-size: .86rem; }
  th, td { padding-inline: 7px; }
}

@media (max-width: 360px) {
  .topbar { column-gap: 6px; padding-inline: 10px; }
  .brand { font-size: .78rem; }
  .appearance { gap: 4px; }
  .theme-switch button { width: 34px; height: 38px; }
  .theme-switch svg { width: 23px; height: 23px; }
  .color-control { width: 36px; height: 40px; }
  .color-control input { width: 30px; height: 30px; }
}

@media (prefers-reduced-motion: reduce) {
  html { scroll-behavior: auto; }
}
