/* ============================================
   VARIABLES / THEMING
   ============================================ */
:root {
  --font-sans: 'Inter', system-ui, -apple-system, 'Segoe UI', sans-serif;
  --font-mono: 'JetBrains Mono', 'Fira Code', ui-monospace, Menlo, Consolas, monospace;

  /* Light theme */
  --bg: #ffffff;
  --bg-subtle: #f7f8fa;
  --bg-elevated: #ffffff;
  --border: #e5e7eb;
  --border-strong: #d1d5db;
  --text: #14171f;
  --text-muted: #5b6472;
  --text-faint: #8b93a1;
  --primary: #3661f0;
  --primary-hover: #2949c9;
  --primary-subtle: #eaefff;
  --success: #16a34a;
  --success-subtle: #edfbf1;
  --danger: #dc2626;
  --code-bg: #1e1e2e;
  --shadow-sm: 0 1px 2px rgba(16, 24, 40, 0.06);
  --shadow-md: 0 4px 12px rgba(16, 24, 40, 0.08);
  --radius-sm: 6px;
  --radius-md: 10px;
  --radius-lg: 14px;
  --header-h: 60px;
  --sidebar-w: 272px;
  --content-max: 70ch;
}

[data-theme="dark"] {
  --bg: #0d0f14;
  --bg-subtle: #131620;
  --bg-elevated: #171a24;
  --border: #262b38;
  --border-strong: #333949;
  --text: #edeef2;
  --text-muted: #9aa3b2;
  --text-faint: #6b7280;
  --primary: #6b8bff;
  --primary-hover: #85a0ff;
  --primary-subtle: #1a2140;
  --success: #34d399;
  --success-subtle: #0f2a20;
  --danger: #f87171;
  --code-bg: #12141c;
  --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.3);
  --shadow-md: 0 4px 16px rgba(0, 0, 0, 0.4);
}

/* ============================================
   RESET / BASE
   ============================================ */
*, *::before, *::after { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; scroll-behavior: smooth; }

body {
  margin: 0;
  font-family: var(--font-sans);
  background: var(--bg);
  color: var(--text);
  line-height: 1.6;
  -webkit-font-smoothing: antialiased;
  transition: background-color .15s ease, color .15s ease;
}

h1, h2, h3 { font-weight: 600; line-height: 1.25; letter-spacing: -0.01em; margin: 0 0 .5em; }
h1 { font-size: 1.9rem; }
h2 { font-size: 1.2rem; }
h3 { font-size: 1rem; }
p { margin: 0 0 1em; color: var(--text); max-width: var(--content-max); }
ul, ol { max-width: var(--content-max); padding-left: 1.25rem; }
li { margin-bottom: .4em; }
a { color: var(--primary); text-decoration: none; }
a:hover { text-decoration: underline; }
strong { font-weight: 600; }
code { font-family: var(--font-mono); font-size: .85em; background: var(--bg-subtle); padding: .15em .4em; border-radius: 4px; }
kbd { font-family: var(--font-mono); }

.muted { color: var(--text-muted); }
.eyebrow { text-transform: uppercase; font-size: .72rem; font-weight: 600; letter-spacing: .06em; color: var(--primary); margin: 0 0 .4em; }
.lead { font-size: 1.05rem; color: var(--text-muted); }
.section-subtitle { margin-top: 2rem; }

.skip-link {
  position: absolute; left: -999px; top: 0; z-index: 1000;
  background: var(--primary); color: #fff; padding: .6rem 1rem; border-radius: 0 0 8px 0;
}
.skip-link:focus { left: 0; }

:focus-visible { outline: 2px solid var(--primary); outline-offset: 2px; border-radius: 4px; }

/* ============================================
   TOPBAR
   ============================================ */
.topbar {
  position: sticky; top: 0; z-index: 40;
  height: var(--header-h);
  background: var(--bg-elevated);
  border-bottom: 1px solid var(--border);
}
.topbar__inner {
  height: 100%;
  display: flex; align-items: center; gap: .75rem;
  padding: 0 1rem;
  max-width: 1440px; margin: 0 auto;
}
.brand { display: flex; align-items: center; gap: .5rem; color: var(--text); font-weight: 700; font-size: 1.05rem; flex-shrink: 0; }
.brand:hover { text-decoration: none; }
.brand__mark { font-family: var(--font-mono); color: var(--primary); }

.icon-btn {
  display: inline-flex; align-items: center; justify-content: center;
  width: 36px; height: 36px; border-radius: var(--radius-sm);
  background: transparent; border: 1px solid transparent; color: var(--text-muted);
  cursor: pointer; flex-shrink: 0;
}
.icon-btn:hover { background: var(--bg-subtle); color: var(--text); }

.icon-moon { display: none; }
[data-theme="dark"] .icon-sun { display: none; }
[data-theme="dark"] .icon-moon { display: block; }

.search-wrap { position: relative; flex: 1; max-width: 420px; }
.search-icon { position: absolute; left: 10px; top: 50%; transform: translateY(-50%); color: var(--text-faint); pointer-events: none; }
.search-input {
  width: 100%; height: 36px; padding: 0 2.2rem 0 2.1rem;
  border-radius: var(--radius-sm); border: 1px solid var(--border);
  background: var(--bg-subtle); color: var(--text); font-size: .875rem; font-family: var(--font-sans);
}
.search-input:focus { outline: none; border-color: var(--primary); background: var(--bg); }
.search-kbd {
  position: absolute; right: 8px; top: 50%; transform: translateY(-50%);
  font-size: .7rem; padding: .1em .45em; border-radius: 4px;
  background: var(--bg); border: 1px solid var(--border); color: var(--text-faint);
}

.topbar__actions { display: flex; align-items: center; gap: .5rem; flex-shrink: 0; }

.lang-toggle { display: flex; background: var(--bg-subtle); border-radius: var(--radius-sm); padding: 3px; border: 1px solid var(--border); }
.lang-btn {
  border: none; background: transparent; color: var(--text-muted);
  font-family: var(--font-mono); font-size: .78rem; font-weight: 500;
  padding: .35rem .6rem; border-radius: 5px; cursor: pointer;
}
.lang-btn.is-active { background: var(--bg-elevated); color: var(--primary); box-shadow: var(--shadow-sm); }
.lang-btn:hover:not(.is-active) { color: var(--text); }

/* ============================================
   LAYOUT
   ============================================ */
.layout { display: flex; max-width: 1440px; margin: 0 auto; }

.sidebar {
  width: var(--sidebar-w); flex-shrink: 0;
  border-right: 1px solid var(--border);
  height: calc(100vh - var(--header-h));
  position: sticky; top: var(--header-h);
  overflow-y: auto;
}
.sidebar__nav { padding: 1.25rem 1rem 3rem; }

.sidebar__progress { margin-bottom: 1.25rem; padding: .9rem; border: 1px solid var(--border); border-radius: var(--radius-md); background: var(--bg-subtle); }
.sidebar__progress-label { display: flex; justify-content: space-between; font-size: .78rem; color: var(--text-muted); margin-bottom: .5rem; }
.progress-bar { height: 6px; background: var(--border); border-radius: 99px; overflow: hidden; }
.progress-bar__fill { height: 100%; width: 0%; background: var(--primary); border-radius: 99px; transition: width .25s ease; }

.nav-list { list-style: none; margin: 0; padding: 0; max-width: none; }
.nav-group-label { font-size: .7rem; text-transform: uppercase; letter-spacing: .06em; color: var(--text-faint); font-weight: 600; margin: 1.1rem 0 .35rem .6rem; }
.nav-link {
  display: flex; align-items: center; gap: .5rem;
  padding: .45rem .6rem; border-radius: var(--radius-sm);
  color: var(--text-muted); font-size: .88rem; font-weight: 500;
}
.nav-link:hover { background: var(--bg-subtle); color: var(--text); text-decoration: none; }
.nav-link.is-active { background: var(--primary-subtle); color: var(--primary); }
.nav-link__check { color: var(--success); flex-shrink: 0; }

.sidebar-overlay { display: none; }

.content { flex: 1; min-width: 0; padding: 2rem 2.5rem 5rem; }

.section { scroll-margin-top: calc(var(--header-h) + 1rem); padding-top: .5rem; }
.section + .section, .topic + .topic { margin-top: 4rem; padding-top: 3rem; border-top: 1px solid var(--border); }

/* ============================================
   DASHBOARD
   ============================================ */
.dashboard-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 1rem; margin: 1.5rem 0 2rem; }
.card { border: 1px solid var(--border); border-radius: var(--radius-lg); padding: 1.25rem; background: var(--bg-elevated); }
.card h3 { margin-bottom: .6rem; font-size: .85rem; color: var(--text-muted); text-transform: uppercase; letter-spacing: .03em; }
.card__big-number { font-size: 2.1rem; font-weight: 700; color: var(--primary); margin: 0; }
.dash-list { margin: 0; padding-left: 1.1rem; font-size: .88rem; }
.quick-links { display: flex; flex-direction: column; gap: .5rem; }
.quick-link { font-size: .88rem; font-weight: 500; }

.topic-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(200px, 1fr)); gap: .9rem; }
.topic-card {
  display: block; padding: 1rem 1.1rem; border: 1px solid var(--border); border-radius: var(--radius-md);
  background: var(--bg-elevated); color: var(--text);
}
.topic-card:hover { border-color: var(--primary); text-decoration: none; box-shadow: var(--shadow-sm); }
.topic-card__title { font-weight: 600; font-size: .92rem; margin-bottom: .2rem; }
.topic-card__meta { font-size: .78rem; color: var(--text-faint); }

/* ============================================
   BIG-O TABLE
   ============================================ */
.table-wrap { overflow-x: auto; margin: 1.5rem 0; border: 1px solid var(--border); border-radius: var(--radius-md); }
table { width: 100%; border-collapse: collapse; font-size: .88rem; }
.bigo-table th, .bigo-table td { text-align: left; padding: .7rem .9rem; border-bottom: 1px solid var(--border); vertical-align: top; }
.bigo-table th { background: var(--bg-subtle); font-weight: 600; font-size: .78rem; text-transform: uppercase; letter-spacing: .03em; color: var(--text-muted); }
.bigo-table tr:last-child td { border-bottom: none; }
.bigo-table td:first-child { font-family: var(--font-mono); font-weight: 600; color: var(--primary); white-space: nowrap; }
.bigo-table td:last-child { font-family: var(--font-mono); font-size: .82rem; color: var(--text-muted); }

.howto-list li { margin-bottom: .6rem; }

/* ============================================
   TOPIC ARTICLE
   ============================================ */
.topic__header { display: flex; justify-content: space-between; align-items: flex-start; gap: 1rem; flex-wrap: wrap; }
.topic__category { margin-bottom: .3em; }

.checklist-toggle {
  display: flex; align-items: center; gap: .45rem; font-size: .85rem; font-weight: 500;
  padding: .45rem .8rem; border: 1px solid var(--border); border-radius: 99px; cursor: pointer;
  color: var(--text-muted); background: var(--bg-elevated); flex-shrink: 0; user-select: none;
}
.checklist-toggle:has(.checklist-checkbox:checked) { border-color: var(--success); color: var(--success); background: var(--success-subtle); }
.checklist-checkbox { accent-color: var(--success); width: 15px; height: 15px; }

.complexity-badges { display: flex; gap: .6rem; flex-wrap: wrap; margin: 1rem 0 1.5rem; }
.badge { font-size: .8rem; padding: .35rem .7rem; border-radius: 99px; border: 1px solid var(--border); background: var(--bg-subtle); color: var(--text-muted); }
.badge strong { color: var(--text); font-family: var(--font-mono); font-weight: 600; }

/* ============================================
   CODE TABS + PRISM OVERRIDES
   ============================================ */
.code-tabs { margin: 1rem 0 2rem; border: 1px solid var(--border); border-radius: var(--radius-md); overflow: hidden; max-width: var(--content-max); }
.code-tabs__nav { display: flex; background: var(--bg-subtle); border-bottom: 1px solid var(--border); }
.code-tab-btn {
  border: none; background: transparent; padding: .6rem 1rem; font-family: var(--font-mono);
  font-size: .78rem; color: var(--text-muted); cursor: pointer; border-bottom: 2px solid transparent;
}
.code-tab-btn.is-active { color: var(--primary); border-bottom-color: var(--primary); }

.code-panel { display: none; position: relative; }
.code-panel.is-active { display: block; }

.code-panel pre { margin: 0; border-radius: 0 !important; font-size: .82rem !important; max-height: 480px; }
.code-panel pre[class*="language-"] { background: var(--code-bg) !important; }

.copy-btn {
  position: absolute; top: .5rem; right: .5rem;
  font-size: .72rem; padding: .3rem .6rem; border-radius: 6px;
  background: rgba(255,255,255,.08); color: #d4d4d8; border: 1px solid rgba(255,255,255,.14);
  cursor: pointer; font-family: var(--font-sans);
}
.copy-btn:hover { background: rgba(255,255,255,.16); }
.copy-btn.is-copied { color: #34d399; border-color: #34d399; }

/* ============================================
   PATTERNS
   ============================================ */
.patterns-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(260px, 1fr)); gap: 1rem; margin-top: 1.5rem; }
.pattern-card { border: 1px solid var(--border); border-radius: var(--radius-lg); padding: 1.1rem 1.2rem; background: var(--bg-elevated); }
.pattern-card h3 { color: var(--primary); margin-bottom: .5rem; }
.pattern-card p { font-size: .88rem; margin-bottom: .6rem; }
.pattern-card__signals { display: flex; flex-wrap: wrap; gap: .35rem; }
.pattern-signal { font-size: .72rem; font-family: var(--font-mono); background: var(--bg-subtle); border: 1px solid var(--border); padding: .15rem .5rem; border-radius: 6px; color: var(--text-muted); }

/* ============================================
   FLASHCARDS
   ============================================ */
.flashcard-controls { display: flex; justify-content: space-between; align-items: center; margin: 1.5rem 0 1rem; flex-wrap: wrap; gap: .75rem; }
.flashcard-buttons { display: flex; gap: .5rem; }
.btn {
  font-family: var(--font-sans); font-size: .85rem; font-weight: 600; cursor: pointer;
  padding: .55rem 1.1rem; border-radius: var(--radius-sm); border: 1px solid transparent;
}
.btn--ghost { background: var(--bg-elevated); border-color: var(--border); color: var(--text); }
.btn--ghost:hover { border-color: var(--primary); color: var(--primary); }
.btn--primary { background: var(--primary); color: #fff; }
.btn--primary:hover { background: var(--primary-hover); }

.flashcard-stage { display: flex; justify-content: center; padding: 1rem 0 2.5rem; perspective: 1200px; }
.flashcard {
  width: 100%; max-width: 560px; min-height: 260px;
  border: none; background: transparent; cursor: pointer; padding: 0;
  font-family: inherit;
}
.flashcard__inner {
  position: relative; width: 100%; min-height: 260px;
  transform-style: preserve-3d; transition: transform .45s cubic-bezier(.4,.2,.2,1);
}
.flashcard.is-flipped .flashcard__inner { transform: rotateY(180deg); }

.flashcard__face {
  position: absolute; inset: 0; backface-visibility: hidden;
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  padding: 2rem; border-radius: var(--radius-lg); border: 1px solid var(--border);
  background: var(--bg-elevated); box-shadow: var(--shadow-md); text-align: center;
}
.flashcard__face--back { transform: rotateY(180deg); background: var(--primary-subtle); border-color: var(--primary); }
.flashcard__tag { font-size: .72rem; text-transform: uppercase; letter-spacing: .06em; color: var(--primary); font-weight: 700; margin-bottom: .8rem; }
.flashcard__face p { font-size: 1.05rem; font-weight: 500; max-width: 42ch; margin: 0; color: var(--text); }
.flashcard__hint { margin-top: 1.2rem; font-size: .75rem; color: var(--text-faint); }

/* ============================================
   CHEATSHEET
   ============================================ */
.cheatsheet-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(280px, 1fr)); gap: .9rem; margin-top: 1.5rem; }
.cheatsheet-item { border: 1px solid var(--border); border-radius: var(--radius-md); padding: .9rem 1rem; background: var(--bg-elevated); }
.cheatsheet-item__label { font-size: .8rem; font-weight: 600; margin-bottom: .5rem; color: var(--text-muted); }
.cheatsheet-item pre { margin: 0; background: var(--code-bg); border-radius: 6px; padding: .6rem .7rem; overflow-x: auto; }
.cheatsheet-item code { background: transparent; padding: 0; font-size: .78rem; color: #e4e4e7; white-space: pre; }

/* ============================================
   SEARCH RESULTS
   ============================================ */
.search-results__meta { color: var(--text-muted); font-size: .85rem; margin-bottom: 1rem; }
.search-results__list { list-style: none; padding: 0; display: flex; flex-direction: column; gap: .6rem; }
.search-result {
  display: block; padding: .9rem 1.1rem; border: 1px solid var(--border); border-radius: var(--radius-md);
  background: var(--bg-elevated); color: var(--text);
}
.search-result:hover { border-color: var(--primary); text-decoration: none; }
.search-result__title { font-weight: 600; margin-bottom: .2rem; }
.search-result__category { font-size: .78rem; color: var(--primary); }
.search-result__snippet { font-size: .82rem; color: var(--text-muted); margin-top: .3rem; }
.search-result__snippet mark { background: var(--primary-subtle); color: var(--primary); padding: 0 .15em; border-radius: 3px; }

/* ============================================
   FOOTER
   ============================================ */
.site-footer { border-top: 1px solid var(--border); padding: 2rem 2.5rem; text-align: center; }
.site-footer p { margin: 0; font-size: .8rem; color: var(--text-faint); max-width: none; }

/* ============================================
   RESPONSIVE
   ============================================ */
@media (max-width: 1024px) {
  .dashboard-grid { grid-template-columns: 1fr 1fr; }
}

@media (max-width: 900px) {
  .search-wrap { display: none; }

  .sidebar {
    position: fixed; top: var(--header-h); left: 0; bottom: 0;
    width: min(84vw, 320px); z-index: 50;
    background: var(--bg-elevated);
    transform: translateX(-100%);
    transition: transform .2s ease;
    height: calc(100vh - var(--header-h));
  }
  .sidebar.is-open { transform: translateX(0); box-shadow: var(--shadow-md); }

  .sidebar-overlay {
    display: block; position: fixed; inset: var(--header-h) 0 0 0; z-index: 45;
    background: rgba(0,0,0,.4); opacity: 0; pointer-events: none; transition: opacity .2s ease;
  }
  .sidebar-overlay.is-visible { opacity: 1; pointer-events: auto; }

  .content { padding: 1.5rem 1.25rem 4rem; }
  .dashboard-grid { grid-template-columns: 1fr; }
  h1 { font-size: 1.5rem; }
  .lang-toggle { display: none; }
}

@media (max-width: 600px) {
  .topbar__inner { padding: 0 .75rem; gap: .5rem; }
  .brand__text { display: none; }
  .content { padding: 1.25rem 1rem 3rem; }
  .flashcard__face { padding: 1.25rem; }
  .flashcard__face p { font-size: .95rem; }
  .topic__header { flex-direction: column; }
}

/* Print styles for cheatsheet */
@media print {
  .topbar, .sidebar, .sidebar-overlay, .site-footer, #dashboard, #big-o, #categories-container, #patterns, #flashcards { display: none !important; }
  .content { padding: 0; }
  body { background: #fff; color: #000; }
}
