@layer reset, tokens, base, layout, components, utilities;

@layer reset {
  *, *::before, *::after { box-sizing: border-box; }
  html { scroll-behavior: smooth; text-size-adjust: 100%; }
  body, h1, h2, h3, h4, p, figure, blockquote, dl, dd { margin: 0; }
  ul, ol { margin: 0; padding: 0; }
  img, svg { display: block; max-width: 100%; }
  button, input { font: inherit; }
}

@layer tokens {
  :root {
    color-scheme: light;
    --canvas: #f3f6fa;
    --paper: #ffffff;
    --paper-raised: #ffffff;
    --ink: #172033;
    --ink-strong: #0c1321;
    --muted: #5d687a;
    --soft: #eef2f7;
    --line: #d9e1eb;
    --line-strong: #bac6d5;
    --accent: #c53c48;
    --accent-strong: #9f2631;
    --accent-soft: #fce8e9;
    --kotlin: #6557d9;
    --kotlin-soft: #eeecff;
    --shared: #167b83;
    --shared-soft: #e2f5f4;
    --code: #111927;
    --code-ink: #e8edf5;
    --code-muted: #9ca9bc;
    --success: #287a52;
    --warning: #98631b;
    --shadow-sm: 0 1px 2px rgb(12 19 33 / 7%);
    --shadow-md: 0 14px 38px rgb(12 19 33 / 10%);
    --radius-sm: 8px;
    --radius-md: 14px;
    --radius-lg: 22px;
    --header-height: 68px;
    --content-width: 1180px;
    --reading-width: 760px;
    --font-body: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
    --font-code: "SFMono-Regular", "Cascadia Code", "Roboto Mono", ui-monospace, monospace;
  }

  html[data-theme="dark"] {
    color-scheme: dark;
    --canvas: #101722;
    --paper: #171f2c;
    --paper-raised: #1b2533;
    --ink: #e8eef6;
    --ink-strong: #ffffff;
    --muted: #a8b3c3;
    --soft: #202b3a;
    --line: #2d3949;
    --line-strong: #44536a;
    --accent: #ff737c;
    --accent-strong: #ff9299;
    --accent-soft: #3b252d;
    --kotlin: #a295ff;
    --kotlin-soft: #2b2849;
    --shared: #52c9c6;
    --shared-soft: #173c41;
    --code: #0b111b;
    --code-ink: #eef3fb;
    --code-muted: #9aa8bc;
    --success: #66ca96;
    --warning: #e4ad5d;
    --shadow-sm: 0 1px 2px rgb(0 0 0 / 20%);
    --shadow-md: 0 18px 42px rgb(0 0 0 / 28%);
  }
}

@layer base {
  body {
    min-width: 320px;
    min-height: 100vh;
    background: var(--canvas);
    color: var(--ink);
    font-family: var(--font-body);
    font-size: 16px;
    line-height: 1.68;
    -webkit-font-smoothing: antialiased;
  }

  a { color: var(--accent-strong); text-decoration-thickness: 1px; text-underline-offset: 3px; }
  a:hover { color: var(--accent); }
  button { color: inherit; }
  ::selection { background: var(--accent); color: #fff; }
  :focus-visible { outline: 3px solid color-mix(in srgb, var(--kotlin) 62%, transparent); outline-offset: 3px; }

  h1, h2, h3, h4 {
    color: var(--ink-strong);
    font-weight: 750;
    letter-spacing: -0.025em;
    line-height: 1.16;
    text-wrap: balance;
  }
  h1 { font-size: clamp(2.5rem, 6vw, 5.25rem); }
  h2 { font-size: clamp(1.7rem, 3vw, 2.45rem); }
  h3 { font-size: clamp(1.15rem, 2vw, 1.4rem); }
  h4 { font-size: 1rem; }
  p { text-wrap: pretty; }
  code, kbd { font-family: var(--font-code); }
  code:not(pre code) {
    border: 1px solid var(--line);
    border-radius: 5px;
    background: var(--soft);
    padding: .1rem .34rem;
    color: var(--ink-strong);
    font-size: .88em;
  }
}

@layer layout {
  .skip-link {
    position: fixed;
    z-index: 1000;
    top: 10px;
    left: 10px;
    transform: translateY(-160%);
    border-radius: var(--radius-sm);
    background: var(--ink-strong);
    padding: 10px 14px;
    color: var(--paper);
    font-weight: 700;
  }
  .skip-link:focus { transform: translateY(0); }

  .site-header {
    position: sticky;
    z-index: 100;
    top: 0;
    height: var(--header-height);
    border-bottom: 1px solid color-mix(in srgb, var(--line) 80%, transparent);
    background: color-mix(in srgb, var(--canvas) 88%, transparent);
    backdrop-filter: blur(18px) saturate(150%);
  }
  .header-inner {
    display: flex;
    width: min(calc(100% - 32px), var(--content-width));
    height: 100%;
    margin-inline: auto;
    align-items: center;
    gap: 28px;
  }
  .brand {
    display: inline-flex;
    flex: 0 0 auto;
    align-items: center;
    gap: 10px;
    color: var(--ink-strong);
    font-weight: 790;
    letter-spacing: -.02em;
    text-decoration: none;
  }
  .brand img { width: 34px; height: 34px; }
  .brand small { color: var(--muted); font-size: .73rem; font-weight: 650; letter-spacing: .04em; text-transform: uppercase; }

  .site-nav { display: flex; margin-left: auto; align-items: center; gap: 4px; }
  .site-nav a {
    border-radius: 8px;
    padding: 8px 11px;
    color: var(--muted);
    font-size: .91rem;
    font-weight: 670;
    text-decoration: none;
    white-space: nowrap;
  }
  .site-nav a:hover, .site-nav a[aria-current="page"] { background: var(--soft); color: var(--ink-strong); }
  .header-actions { display: flex; align-items: center; gap: 6px; }

  .page-shell {
    display: grid;
    grid-template-columns: 190px minmax(0, var(--reading-width)) 190px;
    width: min(calc(100% - 40px), var(--content-width));
    margin: 0 auto;
    gap: 34px;
    align-items: start;
  }
  .page-main { min-width: 0; padding-block: 54px 96px; }
  .side-nav, .on-this-page { position: sticky; top: calc(var(--header-height) + 28px); padding-top: 50px; }
  .side-nav ul, .on-this-page ul { display: grid; gap: 3px; list-style: none; }
  .side-nav .nav-label, .on-this-page .nav-label {
    display: block;
    margin-bottom: 9px;
    color: var(--muted);
    font-size: .72rem;
    font-weight: 800;
    letter-spacing: .11em;
    text-transform: uppercase;
  }
  .side-nav a, .on-this-page a {
    display: block;
    border-left: 2px solid transparent;
    padding: 6px 9px;
    color: var(--muted);
    font-size: .86rem;
    line-height: 1.35;
    text-decoration: none;
  }
  .side-nav a:hover, .side-nav a[aria-current="page"], .on-this-page a.active {
    border-left-color: var(--accent);
    color: var(--ink-strong);
  }

  .home-main { overflow: hidden; }
  .home-section { width: min(calc(100% - 40px), var(--content-width)); margin-inline: auto; padding-block: 88px; }
  .home-section + .home-section { border-top: 1px solid var(--line); }

  .site-footer { border-top: 1px solid var(--line); background: var(--paper); }
  .footer-inner {
    display: grid;
    grid-template-columns: 1.25fr .75fr .75fr;
    width: min(calc(100% - 40px), var(--content-width));
    margin-inline: auto;
    gap: 36px;
    padding-block: 48px 58px;
  }
  .footer-inner p { max-width: 48ch; color: var(--muted); font-size: .92rem; }
  .footer-links { display: grid; align-content: start; gap: 7px; }
  .footer-links strong { margin-bottom: 4px; color: var(--ink-strong); font-size: .8rem; text-transform: uppercase; letter-spacing: .08em; }
  .footer-links a { color: var(--muted); font-size: .91rem; text-decoration: none; }

  @media (max-width: 1040px) {
    .page-shell { grid-template-columns: 176px minmax(0, 1fr); max-width: 920px; }
    .on-this-page { display: none; }
  }

  @media (max-width: 990px) {
    .header-inner { gap: 16px; }
    .brand small { display: none; }
    .repo-label { display: none; }
    .site-nav { gap: 2px; }
    .site-nav a { padding: 8px 9px; }
  }

  @media (max-width: 820px) {
    :root { --header-height: 62px; }
    .header-inner { width: min(calc(100% - 22px), var(--content-width)); gap: 12px; }
    .brand small { display: none; }
    .menu-toggle { display: inline-grid !important; }
    .site-nav {
      position: fixed;
      inset: var(--header-height) 12px auto;
      display: none;
      border: 1px solid var(--line);
      border-radius: var(--radius-md);
      background: var(--paper-raised);
      box-shadow: var(--shadow-md);
      padding: 10px;
    }
    .site-nav.open { display: grid; }
    .site-nav a { padding: 11px 12px; }
    .header-actions { margin-left: auto; }
    .repo-label { display: none; }
    .page-shell { display: block; width: min(calc(100% - 28px), var(--reading-width)); }
    .side-nav { display: none; }
    .page-main { padding-block: 38px 72px; }
    .home-section { width: min(calc(100% - 28px), var(--content-width)); padding-block: 64px; }
    .footer-inner { grid-template-columns: 1fr; width: min(calc(100% - 28px), var(--content-width)); gap: 28px; }
  }
}

@layer components {
  .icon-button, .menu-toggle {
    display: inline-grid;
    width: 38px;
    height: 38px;
    place-items: center;
    border: 1px solid var(--line);
    border-radius: 9px;
    background: var(--paper);
    box-shadow: var(--shadow-sm);
    cursor: pointer;
  }
  .icon-button:hover, .menu-toggle:hover { border-color: var(--line-strong); background: var(--soft); }
  .icon-button svg, .menu-toggle svg { width: 18px; height: 18px; }
  .menu-toggle { display: none; }
  .repo-link { display: inline-flex; align-items: center; gap: 7px; color: var(--muted); font-size: .86rem; font-weight: 700; text-decoration: none; }
  .repo-link svg { width: 18px; height: 18px; }

  .reading-progress { position: fixed; z-index: 120; inset: 0 0 auto; height: 2px; background: transparent; }
  .reading-progress span { display: block; width: 0; height: 100%; background: linear-gradient(90deg, var(--accent), var(--kotlin)); }

  .hero {
    position: relative;
    width: min(calc(100% - 40px), var(--content-width));
    min-height: calc(92vh - var(--header-height));
    margin-inline: auto;
    display: grid;
    grid-template-columns: minmax(0, 1.02fr) minmax(420px, .98fr);
    align-items: center;
    gap: 64px;
    padding-block: 82px;
  }
  .hero::before {
    content: "";
    position: absolute;
    z-index: -1;
    width: 460px;
    height: 460px;
    top: 9%;
    right: -11%;
    border: 1px solid color-mix(in srgb, var(--kotlin) 32%, transparent);
    border-radius: 50%;
    box-shadow: 0 0 0 78px color-mix(in srgb, var(--kotlin) 5%, transparent), 0 0 0 156px color-mix(in srgb, var(--accent) 3%, transparent);
  }
  .hero-copy { max-width: 680px; }
  .hero h1 { margin-block: 18px 24px; max-width: 12ch; }
  .hero h1 em { color: var(--accent); font-style: normal; }
  .hero-lede { max-width: 59ch; color: var(--muted); font-size: clamp(1.05rem, 2vw, 1.24rem); }
  .hero-actions { display: flex; flex-wrap: wrap; gap: 10px; margin-top: 32px; }
  .eyebrow { display: inline-flex; align-items: center; gap: 8px; color: var(--accent-strong); font-size: .76rem; font-weight: 840; letter-spacing: .12em; text-transform: uppercase; }
  .eyebrow::before { content: ""; width: 24px; height: 2px; background: currentColor; }
  .button {
    display: inline-flex;
    min-height: 44px;
    align-items: center;
    justify-content: center;
    gap: 8px;
    border: 1px solid var(--line-strong);
    border-radius: 10px;
    padding: 9px 15px;
    color: var(--ink-strong);
    font-weight: 760;
    text-decoration: none;
  }
  .button:hover { transform: translateY(-1px); border-color: var(--ink); color: var(--ink-strong); }
  .button.primary { border-color: var(--accent); background: var(--accent); color: #fff; box-shadow: 0 8px 24px color-mix(in srgb, var(--accent) 22%, transparent); }
  .button.primary:hover { border-color: var(--accent-strong); background: var(--accent-strong); color: #fff; }
  .button svg { width: 18px; height: 18px; }

  .source-rail {
    position: relative;
    border: 1px solid var(--line);
    border-radius: var(--radius-lg);
    background: var(--paper);
    box-shadow: var(--shadow-md);
    padding: 26px;
  }
  .rail-kicker { display: flex; justify-content: space-between; gap: 16px; margin-bottom: 24px; color: var(--muted); font-family: var(--font-code); font-size: .75rem; }
  .rail-common {
    position: relative;
    z-index: 2;
    border: 1px solid color-mix(in srgb, var(--accent) 45%, var(--line));
    border-radius: 12px;
    background: var(--accent-soft);
    padding: 17px 18px;
  }
  .rail-node strong, .rail-common strong { display: block; color: var(--ink-strong); font-family: var(--font-code); font-size: .88rem; }
  .rail-node span, .rail-common span { display: block; margin-top: 3px; color: var(--muted); font-size: .78rem; }
  .rail-trunk { width: 2px; height: 46px; margin-inline: auto; background: var(--line-strong); }
  .rail-platforms { position: relative; display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; }
  .rail-platforms::before { content: ""; position: absolute; top: -18px; left: 16.66%; right: 16.66%; height: 2px; background: var(--line-strong); }
  .rail-node { position: relative; border: 1px solid var(--line); border-radius: 11px; background: var(--soft); padding: 14px 12px; text-align: center; }
  .rail-node::before { content: ""; position: absolute; width: 2px; height: 18px; left: 50%; bottom: 100%; background: var(--line-strong); }
  .rail-node.ios { border-color: color-mix(in srgb, var(--kotlin) 42%, var(--line)); }
  .rail-node.android { border-color: color-mix(in srgb, var(--shared) 45%, var(--line)); }
  .rail-foot { margin-top: 18px; border-top: 1px dashed var(--line); padding-top: 15px; color: var(--muted); font-size: .8rem; }

  .section-heading { display: grid; grid-template-columns: minmax(0, 1fr) minmax(260px, .7fr); gap: 48px; align-items: end; margin-bottom: 38px; }
  .section-heading h2 { max-width: 15ch; margin-top: 10px; }
  .section-heading p { color: var(--muted); }
  .card-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 15px; }
  .card {
    position: relative;
    border: 1px solid var(--line);
    border-radius: var(--radius-md);
    background: var(--paper);
    box-shadow: var(--shadow-sm);
    padding: 24px;
  }
  .card:hover { border-color: var(--line-strong); box-shadow: var(--shadow-md); transform: translateY(-2px); }
  .card .card-number { display: block; margin-bottom: 28px; color: var(--accent); font-family: var(--font-code); font-size: .76rem; font-weight: 780; }
  .card h3 { margin-bottom: 9px; }
  .card p { color: var(--muted); font-size: .94rem; }
  .card a::after { content: ""; position: absolute; inset: 0; }
  .card a { display: inline-block; margin-top: 18px; font-size: .88rem; font-weight: 750; text-decoration: none; }

  .concept-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; }
  .concept-card { border-left: 3px solid var(--kotlin); background: var(--paper); padding: 23px 24px; }
  .concept-card:nth-child(2n) { border-left-color: var(--shared); }
  .concept-card h3 { margin-bottom: 8px; }
  .concept-card p { color: var(--muted); font-size: .93rem; }
  .concept-card code { display: inline-block; margin-top: 14px; }

  .chapter-header { margin-bottom: 44px; border-bottom: 1px solid var(--line); padding-bottom: 32px; }
  .breadcrumbs { display: flex; flex-wrap: wrap; gap: 7px; margin-bottom: 18px; color: var(--muted); font-size: .82rem; }
  .breadcrumbs a { color: inherit; text-decoration: none; }
  .chapter-header h1 { max-width: 14ch; font-size: clamp(2.35rem, 5vw, 4.25rem); }
  .chapter-header > p { max-width: 62ch; margin-top: 18px; color: var(--muted); font-size: 1.08rem; }
  .chapter-meta { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 22px; }
  .pill { border: 1px solid var(--line); border-radius: 999px; background: var(--paper); padding: 4px 9px; color: var(--muted); font-family: var(--font-code); font-size: .72rem; }

  .doc-section { scroll-margin-top: calc(var(--header-height) + 30px); }
  .doc-section + .doc-section { margin-top: 66px; border-top: 1px solid var(--line); padding-top: 58px; }
  .doc-section h2 { margin-bottom: 18px; }
  .doc-section h3 { margin: 34px 0 12px; scroll-margin-top: calc(var(--header-height) + 24px); }
  .doc-section h4 { margin: 24px 0 8px; }
  .doc-section p + p { margin-top: 14px; }
  .doc-section ul, .doc-section ol { display: grid; gap: 8px; margin: 16px 0 0 1.25rem; }
  .doc-section li::marker { color: var(--accent); font-weight: 800; }
  .doc-section a { font-weight: 620; }

  .callout { margin-block: 24px; border: 1px solid var(--line); border-left: 4px solid var(--shared); border-radius: 0 var(--radius-md) var(--radius-md) 0; background: var(--shared-soft); padding: 18px 20px; }
  .callout.warning { border-left-color: var(--warning); background: color-mix(in srgb, var(--warning) 10%, var(--paper)); }
  .callout.kotlin { border-left-color: var(--kotlin); background: var(--kotlin-soft); }
  .callout strong { display: block; margin-bottom: 4px; color: var(--ink-strong); }
  .callout p { color: var(--muted); font-size: .94rem; }

  .code-block { position: relative; margin-block: 22px; border: 1px solid color-mix(in srgb, var(--line) 45%, transparent); border-radius: var(--radius-md); overflow: hidden; background: var(--code); box-shadow: var(--shadow-sm); }
  .code-label { display: flex; min-height: 38px; align-items: center; justify-content: space-between; border-bottom: 1px solid #293446; padding: 7px 12px 7px 15px; color: var(--code-muted); font-family: var(--font-code); font-size: .72rem; }
  pre { overflow-x: auto; padding: 18px 20px 21px; color: var(--code-ink); font-family: var(--font-code); font-size: .82rem; line-height: 1.66; tab-size: 4; }
  pre code { font-family: inherit; }
  .copy-button { border: 1px solid #3b485c; border-radius: 6px; background: #202a3a; padding: 4px 8px; color: #c5d0df; font-family: var(--font-body); font-size: .7rem; cursor: pointer; }
  .copy-button:hover { background: #2b374a; color: #fff; }
  .token-keyword { color: #cf9cff; }
  .token-type { color: #76d7d0; }
  .token-string { color: #f4c879; }
  .token-comment { color: #8290a5; }

  .file-tree { margin-block: 22px; border: 1px solid var(--line); border-radius: var(--radius-md); background: var(--paper); padding: 20px; color: var(--muted); font-family: var(--font-code); font-size: .82rem; line-height: 1.75; overflow-x: auto; white-space: pre; }
  .file-tree b { color: var(--ink-strong); }
  .file-tree em { color: var(--accent-strong); font-style: normal; }

  .flow-diagram { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 22px; margin-block: 28px; }
  .flow-step { position: relative; border: 1px solid var(--line); border-radius: 11px; background: var(--paper); padding: 16px; }
  .flow-step:not(:last-child)::after { content: "→"; position: absolute; left: calc(100% + 7px); top: 50%; translate: 0 -50%; color: var(--line-strong); font-weight: 800; }
  .flow-step small { display: block; margin-bottom: 4px; color: var(--accent); font-family: var(--font-code); font-size: .68rem; font-weight: 800; text-transform: uppercase; }
  .flow-step strong { color: var(--ink-strong); font-size: .88rem; line-height: 1.35; }

  .comparison { width: 100%; margin-block: 22px; border-collapse: collapse; border: 1px solid var(--line); background: var(--paper); font-size: .88rem; }
  .comparison th, .comparison td { border-bottom: 1px solid var(--line); padding: 12px 14px; text-align: left; vertical-align: top; }
  .comparison th { background: var(--soft); color: var(--ink-strong); font-size: .78rem; letter-spacing: .03em; }
  .comparison td { color: var(--muted); }
  .comparison td:first-child { color: var(--ink-strong); font-family: var(--font-code); font-size: .78rem; }

  .checklist { display: grid; gap: 9px; margin-top: 18px; list-style: none; }
  .checklist li { position: relative; border: 1px solid var(--line); border-radius: 9px; background: var(--paper); padding: 11px 14px 11px 42px; }
  .checklist li::before { content: "✓"; position: absolute; top: 11px; left: 14px; color: var(--success); font-weight: 900; }

  .next-page { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; margin-top: 68px; border-top: 1px solid var(--line); padding-top: 26px; }
  .next-page a { display: grid; border: 1px solid var(--line); border-radius: 11px; background: var(--paper); padding: 16px 18px; text-decoration: none; }
  .next-page a:last-child { text-align: right; }
  .next-page small { color: var(--muted); font-size: .72rem; text-transform: uppercase; letter-spacing: .08em; }
  .next-page strong { margin-top: 3px; color: var(--ink-strong); }

  @media (max-width: 900px) {
    .hero { min-height: auto; grid-template-columns: 1fr; gap: 50px; padding-block: 78px; }
    .hero-copy { max-width: 720px; }
    .hero h1 { max-width: 13ch; }
    .source-rail { max-width: 620px; }
    .section-heading { grid-template-columns: 1fr; gap: 18px; }
    .section-heading h2 { max-width: 18ch; }
  }

  @media (max-width: 680px) {
    .hero { width: min(calc(100% - 28px), var(--content-width)); grid-template-columns: minmax(0, 1fr); padding-block: 58px 70px; }
    .hero::before { width: 260px; height: 260px; }
    .hero h1 { font-size: clamp(2.55rem, 14vw, 4.4rem); }
    .source-rail { padding: 18px; }
    .rail-platforms { grid-template-columns: 1fr; gap: 9px; }
    .rail-platforms::before { display: none; }
    .rail-node::before { display: none; }
    .rail-trunk { height: 20px; }
    .card-grid, .concept-grid { grid-template-columns: 1fr; }
    .flow-diagram { grid-template-columns: 1fr; gap: 10px; }
    .flow-step:not(:last-child)::after { content: "↓"; left: 50%; top: calc(100% + 3px); translate: -50% 0; }
    .comparison { display: block; overflow-x: auto; }
    .next-page { grid-template-columns: 1fr; }
    .next-page a:last-child { text-align: left; }
  }
}

@layer utilities {
  .sr-only { position: absolute !important; width: 1px; height: 1px; padding: 0; margin: -1px; overflow: hidden; clip: rect(0, 0, 0, 0); white-space: nowrap; border: 0; }
  .muted { color: var(--muted); }
  .mono { font-family: var(--font-code); }
  .nowrap { white-space: nowrap; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { scroll-behavior: auto !important; transition-duration: .01ms !important; animation-duration: .01ms !important; }
}

@media print {
  .site-header, .side-nav, .on-this-page, .site-footer, .reading-progress, .copy-button { display: none !important; }
  body { background: #fff; color: #111; }
  .page-shell { display: block; width: 100%; }
  .page-main { padding: 0; }
  .code-block, .card, .callout { break-inside: avoid; box-shadow: none; }
}
