:root {
  color-scheme: light dark;
  --header-h: 72px; --footer-h: 56px; --page-w: 1280px;
  /* Tokyo Night Light (light mode) */
  --bg: #e6e7ed;             /* editor.background */
  --fg: #343b59;             /* editor.foreground */
  --muted: #606572;          /* muted body text */
  --surface: #ffffff;        /* cards, panels */
  --border: #c1c2c7;         /* light borders */
  --link: #2959aa;           /* textLink.foreground */
  --link-hover: #363c4d;     /* textLink.activeForeground */
  --blockquote: #c1c2c7;     /* blockquote border */
  --code-bg: #dcdee3;        /* editor line highlight-like */
  --code-fg: #343b59;        /* code foreground */
  --shadow: rgba(54,60,77,0.06);
}

@media (prefers-color-scheme: dark) { 
  :root { 
    /* Tokyo Night (dark mode) */
    --bg: #1a1b26;           /* editor.background */
    --fg: #a9b1d6;           /* editor.foreground */
    --muted: #787c99;        /* muted body text */
    --surface: #16161e;      /* panels/surface */
    --border: #101014;       /* dark borders */
    --link: #6183bb;         /* textLink.foreground */
    --link-hover: #7dcfff;   /* textLink.activeForeground */
    --blockquote: #2b2b3b;   /* blockquote border */
    --code-bg: #16161e;      /* code block background */
    --code-fg: #c0caf5;      /* code foreground */
    --shadow: rgba(0,0,0,0.2);
  }
}

/* Explicit overrides via data-theme, take precedence over media query */
:root[data-theme="light"] {
  --bg: #e6e7ed;
  --fg: #343b59;
  --muted: #606572;
  --surface: #ffffff;
  --border: #c1c2c7;
  --link: #2959aa;
  --link-hover: #363c4d;
  --blockquote: #c1c2c7;
  --code-bg: #dcdee3;
  --code-fg: #343b59;
  --shadow: rgba(54,60,77,0.06);
}
:root[data-theme="dark"] {
  --bg: #1a1b26;
  --fg: #a9b1d6;
  --muted: #787c99;
  --surface: #16161e;
  --border: #101014;
  --link: #6183bb;
  --link-hover: #7dcfff;
  --blockquote: #2b2b3b;
  --code-bg: #16161e;
  --code-fg: #c0caf5;
  --shadow: rgba(0,0,0,0.2);
}

*, *::before, *::after { box-sizing: border-box; }
html { scroll-behavior: smooth; }
body { font-family: Inter, system-ui, -apple-system, Segoe UI, Roboto, Helvetica, Arial, sans-serif; line-height: 1.7; background: var(--bg); color: var(--fg); margin: 0; padding-bottom: var(--footer-h); }
.container { max-width: var(--page-w); margin: 2.5rem auto; padding: 0 1rem; display: grid; grid-template-columns: 280px 1fr; gap: 24px; }
@media (max-width: 960px) { .container { display: block; } }
.container.no-aside { display: block; }
.doc-list { list-style: none; padding-left: 0; display: grid; gap: 14px; }
.doc-list h3 { margin: 0 0 .25rem; font-size: 1.05rem; }
.doc-list .meta { color: var(--muted); font-size: .95rem; }
.card { background: var(--surface); border: 1px solid var(--border); border-radius: 16px; padding: 2rem 1.5rem; box-shadow: 0 1px 2px var(--shadow); }
h1, h2, h3 { line-height: 1.25; color: var(--fg); }
h1 { font-size: clamp(1.8rem, 2vw + 1rem, 2.4rem); margin: 0 0 1rem; }
h2 { font-size: clamp(1.4rem, 1.2vw + 1rem, 1.8rem); margin-top: 2.2rem; margin-bottom: 0.8rem; }
h3 { font-size: 1.2rem; margin-top: 1.6rem; margin-bottom: 0.6rem; }
p { margin: 1rem 0; }
ul { padding-left: 1.2rem; }
ul ul { margin-top: .25rem; }
a { color: var(--link); text-decoration: underline; text-underline-offset: 3px; }
a:hover { color: var(--link-hover); }
blockquote { border-left: 4px solid var(--blockquote); padding-left: 1rem; color: var(--muted); margin-left: 0; }
pre { background: var(--code-bg); color: var(--code-fg); padding: 1rem; overflow: auto; border-radius: 12px; }
code { font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", "Courier New", monospace; font-size: .95em; }
img { max-width: 100%; height: auto; border-radius: 12px; }
header { position: sticky; top: 0; z-index: 20; background: var(--surface); border-bottom: 1px solid var(--border); }
footer { position: fixed; left: 0; right: 0; bottom: 0; z-index: 20; background: var(--surface); border-top: 1px solid var(--border); }
.topbar { max-width: var(--page-w); margin: 0 auto; padding: .9rem 1rem; display: flex; align-items: center; justify-content: space-between; min-height: 72px; flex-wrap: wrap; row-gap: .3rem; }
.brand { font-weight: 700; letter-spacing: -.01em; }
.actions { display: flex; align-items: center; gap: .85rem; }
.actions a { color: var(--link); text-decoration: none; padding: .25rem .4rem; border-radius: 6px; }
.actions a:hover { text-decoration: underline; text-underline-offset: 3px; }
.theme-toggle { border: 1px solid var(--border); background: var(--surface); color: var(--fg); border-radius: 8px; padding: .35rem .55rem; cursor: pointer; font-size: 14px; line-height: 1; }
.theme-toggle:hover { filter: brightness(1.05); }
.toc { position: sticky; top: 88px; background: var(--surface); border: 1px solid var(--border); border-radius: 14px; padding: 1rem; box-shadow: 0 1px 2px var(--shadow); }
.toc-title { font-weight: 600; color: var(--fg); margin-bottom: .5rem; }
.toc ul { list-style: none; padding-left: 0; margin: 0; display: flex; flex-direction: column; gap: .35rem; }
.toc a { color: var(--muted); text-decoration: none; }
.toc a:hover { color: var(--link); text-decoration: underline; text-underline-offset: 3px; }
h1[id], h2[id], h3[id], h4[id] { scroll-margin-top: 88px; }
@media (max-width: 960px) { .toc { position: static; top: auto; } }
table { width: 100%; border-collapse: collapse; margin: 1rem 0; }
thead th { background: var(--bg); color: var(--fg); }
th, td { border: 1px solid var(--border); padding: .6rem .5rem; vertical-align: top; text-align: left; }
td { overflow-wrap: anywhere; }
.foot { max-width: var(--page-w); margin: 0 auto; padding: .75rem 1rem; color: var(--muted); font-size: 12px; display: grid; grid-template-columns: 1fr auto 1fr; align-items: center; min-height: var(--footer-h); column-gap: 12px; }
.foot .right { justify-self: end; }
.foot .center { justify-self: center; text-align: center; }

/* Print styles */
@media print {
  @page { margin: 12mm; }
  :root {
    --bg: #ffffff;
    --fg: #000000;
    --muted: #333333;
    --surface: #ffffff;
    --border: #000000;
    --link: #000000;
    --link-hover: #000000;
    --blockquote: #999999;
    --code-bg: #ffffff;
    --code-fg: #000000;
    --shadow: transparent;
  }
  body { background: #fff; color: #000; padding-bottom: 0; }
  header, footer, aside, .toc, .actions, .theme-toggle { display: none !important; }
  .container { display: block; max-width: 100%; margin: 0; padding: 0; }
  .card { border: 0; box-shadow: none; background: #fff; padding: 0; border-radius: 0; }
  a, a:visited { color: #000; text-decoration: underline; }
  a[href^="http"]::after { content: " (" attr(href) ")"; font-weight: normal; }
  a[href^="#"]::after { content: ""; }
  pre, code { background: #fff; color: #000; }
  img, pre, blockquote, table { break-inside: avoid; page-break-inside: avoid; }
  h1, h2, h3, h4 { break-after: avoid; page-break-after: avoid; page-break-inside: avoid; }
  /* Keep headings with the immediate following block */
  .keep-with-next { break-inside: avoid; page-break-inside: avoid; }
  p { orphans: 3; widows: 3; }
  /* Gentle nudge to avoid break before the element right after a heading */
  h1 + *, h2 + *, h3 + *, h4 + * { break-before: avoid; page-break-before: avoid; }
  table { border-collapse: collapse; }
  th, td { border: 1px solid #000; }
}
