@layer base, layout, components;

@layer base {
  :root {
    color-scheme: dark;
    --bg: #0d1117;
    --panel: #111821;
    --panel-2: #151f2b;
    --text: #d7dee8;
    --muted: #92a0b3;
    --soft: #223044;
    --line: #263548;
    --accent: #7dd3fc;
    --accent-2: #a7f3d0;
    --danger: #fb7185;
    --sidebar: 360px;
    --radius: 8px;
  }

  * { box-sizing: border-box; }

  html {
    scroll-behavior: smooth;
    scroll-padding-top: 24px;
  }

  body {
    margin: 0;
    background: var(--bg);
    color: var(--text);
    font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", Arial, sans-serif;
    line-height: 1.65;
  }

  a { color: var(--accent); text-decoration: none; }
  a:hover { color: var(--accent-2); }

  img {
    display: block;
    max-width: 100%;
    height: auto;
    margin: 1rem auto;
    border: 1px solid var(--line);
    border-radius: 6px;
    background: #05070a;
  }

  table {
    width: 100%;
    border-collapse: collapse;
    margin: 1rem 0;
    overflow: hidden;
  }

  th, td {
    border: 1px solid var(--line);
    padding: .65rem .75rem;
    vertical-align: top;
  }

  th {
    color: #e7f6ff;
    background: #172233;
    font-weight: 700;
  }

  tr:nth-child(even) td { background: rgba(255, 255, 255, .025); }

  code, pre, .monospace {
    font-family: "Cascadia Code", Consolas, "Liberation Mono", monospace;
  }
}

@layer layout {
  .site-shell {
    min-height: 100vh;
    display: grid;
    grid-template-columns: var(--sidebar) minmax(0, 1fr);
  }

  .sidebar {
    position: sticky;
    top: 0;
    height: 100vh;
    overflow: auto;
    padding: 22px 18px;
    border-right: 1px solid var(--line);
    background: linear-gradient(180deg, #111821 0%, #0c1119 100%);
  }

  .content {
    width: min(100%, 1080px);
    padding: 40px clamp(20px, 4vw, 56px);
  }

  .page-hero {
    margin-bottom: 28px;
    padding-bottom: 24px;
    border-bottom: 1px solid var(--line);
  }

  .page-hero h1 {
    max-width: 820px;
    margin: 0;
    font-size: clamp(2rem, 4vw, 3.4rem);
    line-height: 1.08;
    letter-spacing: 0;
  }

  .page-hero p {
    max-width: 760px;
    color: var(--muted);
    font-size: 1rem;
  }

  @media (max-width: 980px) {
    .site-shell { display: block; }
    .sidebar {
      position: fixed;
      inset: 0 auto 0 0;
      z-index: 30;
      width: min(88vw, var(--sidebar));
      transform: translateX(-105%);
      transition: transform .22s ease;
      box-shadow: 20px 0 50px rgba(0, 0, 0, .35);
    }
    body.sidebar-open .sidebar { transform: translateX(0); }
    .content { padding-top: 78px; }
  }
}

@layer components {
  .sidebar-toggle {
    position: fixed;
    top: 16px;
    left: 16px;
    z-index: 40;
    display: none;
    border: 1px solid var(--line);
    border-radius: 999px;
    background: #172233;
    color: var(--text);
    padding: .65rem .9rem;
    font-weight: 700;
    cursor: pointer;
  }

  .brand {
    display: flex;
    align-items: center;
    gap: .75rem;
    color: var(--text);
    margin-bottom: 20px;
  }

  .brand-mark {
    display: grid;
    place-items: center;
    width: 42px;
    height: 42px;
    border: 1px solid #27506a;
    border-radius: 8px;
    background: #0b2635;
    color: var(--accent);
    font-weight: 800;
  }

  .brand small {
    display: block;
    color: var(--muted);
    font-size: .8rem;
  }

  .language-switcher {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 6px;
    align-items: center;
    margin: -6px 0 18px;
  }

  .language-label {
    grid-column: 1 / -1;
    color: var(--muted);
    font-size: .72rem;
    font-weight: 700;
    letter-spacing: .08em;
    text-transform: uppercase;
  }

  .language-switcher a {
    display: grid;
    place-items: center;
    min-height: 34px;
    border: 1px solid var(--line);
    border-radius: 8px;
    background: #0b111a;
    color: var(--muted);
    font-size: .84rem;
    font-weight: 700;
  }

  .language-switcher a:hover,
  .language-switcher a.active {
    border-color: #367da0;
    background: #112032;
    color: var(--text);
  }

  .search-box {
    display: grid;
    gap: .4rem;
    margin-bottom: 16px;
    color: var(--muted);
    font-size: .78rem;
    text-transform: uppercase;
    letter-spacing: .08em;
  }

  .search-box input {
    width: 100%;
    border: 1px solid var(--line);
    border-radius: 8px;
    background: #0b111a;
    color: var(--text);
    padding: .7rem .8rem;
    outline: none;
  }

  .search-box input:focus {
    border-color: #2f86b3;
    box-shadow: 0 0 0 3px rgba(125, 211, 252, .12);
  }

  .toc-group {
    border-top: 1px solid var(--line);
    padding: .45rem 0;
  }

  .toc-group summary {
    cursor: pointer;
    color: #e6edf5;
    font-weight: 750;
    padding: .45rem .3rem;
    border-radius: 6px;
  }

  .toc-group summary:hover { background: rgba(125, 211, 252, .08); }
  .toc-group ul { list-style: none; margin: .2rem 0 .4rem; padding: 0; }

  .toc-link {
    display: grid;
    grid-template-columns: 3.25rem minmax(0, 1fr);
    gap: .5rem;
    align-items: baseline;
    padding: .38rem .45rem;
    border-radius: 6px;
    color: var(--muted);
    font-size: .92rem;
    line-height: 1.25;
  }

  .toc-link:hover,
  .toc-link.active {
    color: var(--text);
    background: rgba(125, 211, 252, .08);
  }

  .toc-number {
    color: var(--accent);
    font-family: "Cascadia Code", Consolas, monospace;
    font-size: .78rem;
  }

  .eyebrow {
    margin: 0 0 .75rem;
    color: var(--accent-2);
    text-transform: uppercase;
    letter-spacing: .12em;
    font-weight: 800;
    font-size: .76rem;
  }

  .doc-article {
    padding: 28px 0;
    border-bottom: 1px solid var(--line);
  }

  .article-header {
    display: grid;
    grid-template-columns: auto minmax(0, 1fr) auto;
    gap: .75rem;
    align-items: center;
    margin-bottom: 16px;
  }

  .article-number {
    border: 1px solid #2e5870;
    border-radius: 6px;
    background: #0b2635;
    color: var(--accent);
    padding: .18rem .45rem;
    font-family: "Cascadia Code", Consolas, monospace;
    font-size: .85rem;
  }

  .article-header h2 {
    margin: 0;
    font-size: clamp(1.45rem, 2.4vw, 2rem);
    line-height: 1.2;
    letter-spacing: 0;
  }

  .article-body {
    color: #cbd5e1;
    overflow-wrap: anywhere;
  }

  .article-body p { margin: 0 0 1rem; text-indent: 1.5em; }
  .article-body p:first-child,
  .article-body h3 + p,
  .article-body h4 + p { text-indent: 0; }
  .article-body ul, .article-body ol { padding-left: 1.4rem; }
  .article-body h3 { color: #f3f7fb; margin-top: 1.8rem; }
  .article-body h4 { color: #dce8f5; margin-top: 1.25rem; }
  .article-body .float-left { float: left; margin: .35rem 1rem 1rem 0; }
  .article-body .float-right { float: right; margin: .35rem 0 1rem 1rem; }
  .article-body .clear-left { clear: left; }
  .article-body .clear-right { clear: right; }
  .article-body .layout-block { margin: 1rem 0; }
  .text-blue, .text-green, .text-purple, .text-orange, .text-brown { color: inherit; }

  .modern-note {
    margin: 1rem 0 1.25rem;
    border: 1px solid #335d7a;
    border-left: 4px solid var(--accent);
    border-radius: 8px;
    background: rgba(125, 211, 252, .08);
    color: #dce8f5;
    padding: .85rem 1rem;
  }

  .modern-note strong {
    color: #f3f7fb;
  }

  .modern-note ul {
    margin: .55rem 0 0;
  }

  .article-figure {
    margin: 1rem 0 1.25rem;
    border: 1px solid var(--line);
    border-radius: 8px;
    overflow: hidden;
    background: rgba(255, 255, 255, .025);
  }

  .article-figure img {
    display: block;
    max-width: 100%;
    height: auto;
  }

  .article-figure figcaption {
    padding: .55rem .7rem;
    color: var(--muted);
    font-size: .86rem;
  }

  .tutorial-images {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(min(100%, 260px), 1fr));
    gap: .9rem;
    margin: 1rem 0 1.35rem;
  }

  .tutorial-images figure {
    margin: 0;
    border: 1px solid var(--line);
    border-radius: 8px;
    overflow: hidden;
    background: rgba(255, 255, 255, .025);
  }

  .tutorial-images img {
    display: block;
    width: 100%;
    height: auto;
  }

  .tutorial-images figcaption {
    padding: .55rem .7rem;
    color: var(--muted);
    font-size: .86rem;
  }

  .entity-index {
    display: flex;
    flex-wrap: wrap;
    gap: .45rem;
    margin: 1rem 0 1.25rem;
  }

  .entity-index a {
    border: 1px solid var(--line);
    border-radius: 6px;
    padding: .28rem .55rem;
    background: rgba(255, 255, 255, .03);
    color: #d7ecff;
    font-family: "Cascadia Code", Consolas, monospace;
    font-size: .84rem;
  }

  .entity-reference {
    margin: 1rem 0;
    border: 1px solid var(--line);
    border-radius: 8px;
    overflow: hidden;
    background: rgba(255, 255, 255, .018);
  }

  .entity-ref-title,
  .entity-ref-subhead,
  .entity-ref-detail {
    padding: .65rem .75rem;
  }

  .entity-ref-title {
    background: #172233;
    color: #e7f6ff;
    font-weight: 700;
  }

  .entity-ref-subhead {
    border-top: 1px solid var(--line);
    border-bottom: 1px solid var(--line);
    background: rgba(125, 211, 252, .06);
    color: var(--accent-2);
    font-weight: 700;
  }

  .entity-ref-detail + .entity-ref-detail {
    border-top: 1px solid rgba(38, 53, 72, .72);
  }

  .article-body a[data-local-preview] {
    text-decoration: underline;
    text-decoration-style: dotted;
    text-decoration-thickness: 1px;
    text-underline-offset: .2em;
  }


  .chapter-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(240px, 1fr));
    gap: 12px;
  }

  .chapter-card {
    min-height: 96px;
    display: grid;
    align-content: space-between;
    border: 1px solid var(--line);
    border-radius: 8px;
    background: var(--panel);
    padding: 16px;
    color: var(--text);
  }

  .chapter-card:hover {
    border-color: #367da0;
    background: var(--panel-2);
  }

  .chapter-card small { color: var(--muted); }

  @media (max-width: 980px) {
    .sidebar-toggle { display: block; }
  }

  @media (max-width: 640px) {
    .article-header {
      grid-template-columns: 1fr;
      align-items: start;
    }
    .article-body .float-left,
    .article-body .float-right {
      float: none;
      margin: 1rem auto;
    }
  }
}
