/* ==========================================================================
 * CairnKit Precision Foundry Design System — Tokens & Shared Primitives
 * Canonical source for cairnkithq.com and all *.cairnkithq.com product LPs.
 * Zero external CDNs. Zero web font imports. WCAG 2.2 AA in Light & Dark.
 * ========================================================================== */

:root {
  color-scheme: light dark;

  /* Precision Foundry Warm Stone Palette (Default Light) */
  --bg-stone: #F3F0E8;
  --bg-paper: #FAF8F3;
  --bg-basalt: #161615;
  --bg-basalt-card: #20201E;
  --bg-header: rgba(243, 240, 232, 0.88);
  --bg-overlay: rgba(22, 22, 21, 0.56);

  --ink: #161615;
  --ink-mid: #5C5952;
  --ink-faint: #8A867D;
  --ink-muted: #615D55;

  --vermilion: #D94E28;
  --vermilion-text: #B53A18;
  --forest: #246A4E;

  --rule: rgba(22, 22, 21, 0.12);
  --rule-strong: rgba(22, 22, 21, 0.24);

  /* Typography */
  --font-sans: "Inter", -apple-system, BlinkMacSystemFont, "Helvetica Neue", sans-serif;
  --font-mono: ui-monospace, "SF Mono", "JetBrains Mono", SFMono-Regular, Menlo, Consolas, monospace;

  /* Geometry & Layout */
  --radius-sm: 6px;
  --radius-md: 8px;
  --radius-lg: 12px;
  --radius-xl: 16px;
  --container-max: 1152px;
  --header-height: 64px;
  --focus-ring: 2px solid var(--vermilion);
}

:root:has(> head > meta[name="color-scheme"][content="dark"]) {
  color-scheme: dark;
}

:root:has(> head > meta[name="color-scheme"][content="light"]) {
  color-scheme: light;
}

/* Dark-Stone Mode via html.dark or explicit <meta name="color-scheme" content="dark"> */
html.dark,
:root:has(> head > meta[name="color-scheme"][content="dark"]) {
  --bg-stone: #121211;
  --bg-paper: #1B1B19;
  --bg-basalt: #0B0B0A;
  --bg-basalt-card: #242421;
  --bg-header: rgba(18, 18, 17, 0.88);
  --bg-overlay: rgba(5, 5, 5, 0.74);

  --ink: #F3F0E8;
  --ink-mid: #B2AEA4;
  --ink-faint: #737067;
  --ink-muted: #9E9A90;

  --vermilion: #EB5E38;
  --vermilion-text: #F06D49;
  --forest: #3EB887;

  --rule: rgba(243, 240, 232, 0.12);
  --rule-strong: rgba(243, 240, 232, 0.26);
}

/* Respect OS Dark Preference unless user explicitly pinned Light */
@media (prefers-color-scheme: dark) {
  :root:not(.light):not(:has(> head > meta[name="color-scheme"][content="light"])) {
    --bg-stone: #121211;
    --bg-paper: #1B1B19;
    --bg-basalt: #0B0B0A;
    --bg-basalt-card: #242421;
    --bg-header: rgba(18, 18, 17, 0.88);
    --bg-overlay: rgba(5, 5, 5, 0.74);

    --ink: #F3F0E8;
    --ink-mid: #B2AEA4;
    --ink-faint: #737067;
    --ink-muted: #9E9A90;

    --vermilion: #EB5E38;
    --vermilion-text: #F06D49;
    --forest: #3EB887;

    --rule: rgba(243, 240, 232, 0.12);
    --rule-strong: rgba(243, 240, 232, 0.26);
  }
}

/* Base Reset */
*,
*::before,
*::after {
  box-sizing: border-box;
}

html {
  scroll-behavior: smooth;
  -webkit-text-size-adjust: 100%;
}

body {
  margin: 0;
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  font-family: var(--font-sans);
  font-size: 16px;
  line-height: 1.6;
  background-color: var(--bg-stone);
  color: var(--ink);
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  letter-spacing: -0.014em;
}

img,
svg {
  display: block;
  max-width: 100%;
}

button,
input,
select,
textarea {
  font: inherit;
  color: inherit;
}

a {
  color: inherit;
  text-decoration: none;
}

:focus-visible {
  outline: var(--focus-ring);
  outline-offset: 2px;
}

::selection {
  background-color: #D94E28;
  color: #FFFFFF;
}

/* Accessibility Utilities */
.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

.skip-link:focus {
  position: fixed;
  top: 12px;
  left: 12px;
  z-index: 100;
  width: auto;
  height: auto;
  clip: auto;
  margin: 0;
  padding: 10px 16px;
  border-radius: var(--radius-md);
  background-color: var(--ink);
  color: var(--bg-stone);
  font-size: 13px;
  font-weight: 600;
  text-decoration: none;
}

/* Typography Primitives */
.editorial-display {
  letter-spacing: -0.038em;
  line-height: 1.04;
  font-weight: 600;
}

.spec-mono {
  font-variant-numeric: tabular-nums;
  letter-spacing: 0.055em;
}

.code-mono {
  font-family: var(--font-mono);
  font-variant-numeric: tabular-nums;
}

.tabular-num {
  font-variant-numeric: tabular-nums;
}

.text-mid {
  color: var(--ink-mid);
}

.text-muted {
  color: var(--ink-muted);
}

.text-vermilion {
  color: var(--vermilion-text);
}

.text-forest {
  color: var(--forest);
}

/* Layout & Surface Primitives */
.ck-container {
  width: 100%;
  max-width: var(--container-max);
  margin-inline: auto;
  padding-inline: 20px;
}

@media (min-width: 640px) {
  .ck-container {
    padding-inline: 32px;
  }
}

.rule-b {
  border-bottom: 1px solid var(--rule);
}

.rule-t {
  border-top: 1px solid var(--rule);
}

.paper-plate {
  background-color: var(--bg-paper);
  border: 1px solid var(--rule);
}

.basalt-plate {
  background-color: var(--bg-basalt);
  color: #F3F0E8;
}

.dot-grid {
  background-image: radial-gradient(var(--rule-strong) 1px, transparent 1px);
  background-size: 18px 18px;
}

.instrument-plate {
  background-color: var(--bg-paper);
  border: 1px solid var(--rule);
  border-radius: var(--radius-xl);
  padding: 24px;
}

@media (min-width: 640px) {
  .instrument-plate {
    padding: 36px;
  }
}

/* Sticky Warm-Stone Foundry Header */
.foundry-header {
  position: sticky;
  top: 0;
  z-index: 40;
  background-color: var(--bg-header);
  backdrop-filter: blur(14px);
  -webkit-backdrop-filter: blur(14px);
  border-bottom: 1px solid var(--rule);
}

.foundry-header-inner {
  height: var(--header-height);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
}

.foundry-brand {
  display: inline-flex;
  align-items: center;
  gap: 12px;
  color: var(--ink);
  background: transparent;
  border: 0;
  padding: 0;
  cursor: pointer;
  text-decoration: none;
}

.foundry-brand-mark {
  width: 28px;
  height: 28px;
  flex-shrink: 0;
  transition: transform 200ms ease;
}

.foundry-brand:hover .foundry-brand-mark {
  transform: translateY(-1px);
}

.foundry-wordmark {
  font-size: 14px;
  font-weight: 600;
  letter-spacing: 0.14em;
  text-transform: uppercase;
}

.foundry-nav {
  display: none;
  align-items: center;
  gap: 32px;
  font-size: 12px;
  font-weight: 500;
  color: var(--ink-mid);
}

@media (min-width: 768px) {
  .foundry-nav {
    display: flex;
  }
}

.foundry-nav a,
.foundry-nav button {
  background: transparent;
  border: 0;
  padding: 4px 0;
  cursor: pointer;
  color: var(--ink-mid);
  transition: color 160ms ease;
  display: inline-flex;
  align-items: center;
  gap: 6px;
}

.foundry-nav a:hover,
.foundry-nav button:hover,
.foundry-nav a[aria-current="page"] {
  color: var(--ink);
}

.foundry-nav-num {
  color: var(--ink-muted);
}

/* Buttons */
.btn-basalt {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 11px 18px;
  border-radius: var(--radius-md);
  border: 1px solid var(--ink);
  background-color: var(--ink);
  color: var(--bg-stone);
  font-size: 12px;
  font-weight: 600;
  line-height: 1.25;
  cursor: pointer;
  text-decoration: none;
  transition: opacity 160ms ease, transform 160ms ease;
}

.btn-basalt:hover {
  opacity: 0.86;
  text-decoration: none;
}

.btn-outline-stone {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 11px 18px;
  border-radius: var(--radius-md);
  border: 1px solid var(--rule-strong);
  background-color: transparent;
  color: var(--ink);
  font-size: 12px;
  font-weight: 500;
  line-height: 1.25;
  cursor: pointer;
  text-decoration: none;
  transition: background-color 160ms ease, border-color 160ms ease;
}

.btn-outline-stone:hover {
  background-color: var(--bg-paper);
  border-color: var(--ink);
  text-decoration: none;
}

.btn-outline-stone:disabled {
  opacity: 0.4;
  cursor: not-allowed;
}

.btn-paper-on-basalt {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 11px 18px;
  border-radius: var(--radius-md);
  border: 1px solid #F3F0E8;
  background-color: #F3F0E8;
  color: #161615;
  font-size: 12px;
  font-weight: 600;
  line-height: 1.25;
  cursor: pointer;
  text-decoration: none;
  transition: opacity 160ms ease;
}

.btn-paper-on-basalt:hover {
  opacity: 0.9;
  text-decoration: none;
}

.btn-outline-on-basalt {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 11px 18px;
  border-radius: var(--radius-md);
  border: 1px solid rgba(243, 240, 232, 0.28);
  background-color: transparent;
  color: #F3F0E8;
  font-size: 12px;
  font-weight: 500;
  line-height: 1.25;
  cursor: pointer;
  text-decoration: none;
  transition: background-color 160ms ease, border-color 160ms ease;
}

.btn-outline-on-basalt:hover {
  background-color: rgba(255, 255, 255, 0.08);
  border-color: #F3F0E8;
  text-decoration: none;
}

.btn-sm {
  padding: 6px 12px;
  font-size: 11px;
  border-radius: var(--radius-sm);
}

.btn-upper {
  text-transform: uppercase;
  letter-spacing: 0.06em;
}

.vermilion-dot {
  width: 6px;
  height: 6px;
  border-radius: 9999px;
  background-color: var(--vermilion);
  flex-shrink: 0;
  display: inline-block;
}

.vermilion-dot-lg {
  width: 8px;
  height: 8px;
}

/* Dark-Stone Mode Toggle Button */
.dark-toggle-btn {
  width: 34px;
  height: 34px;
  padding: 0;
  border-radius: var(--radius-sm);
  border: 1px solid var(--rule-strong);
  background-color: transparent;
  color: var(--ink);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  flex-shrink: 0;
  transition: background-color 160ms ease, border-color 160ms ease;
}

.dark-toggle-btn:hover {
  background-color: var(--bg-paper);
  border-color: var(--ink);
}

.dark-toggle-pip {
  width: 10px;
  height: 10px;
  border-radius: 9999px;
  background-color: currentColor;
  display: block;
}

.theme-option-light {
  display: none;
}

.theme-option-dark {
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

html.dark .theme-option-light,
:root:has(> head > meta[name="color-scheme"][content="dark"]) .theme-option-light {
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

html.dark .theme-option-dark,
:root:has(> head > meta[name="color-scheme"][content="dark"]) .theme-option-dark {
  display: none;
}

@media (prefers-color-scheme: dark) {
  :root:not(.light):not(:has(> head > meta[name="color-scheme"][content="light"])) .theme-option-light {
    display: inline-flex;
    align-items: center;
    justify-content: center;
  }
  :root:not(.light):not(:has(> head > meta[name="color-scheme"][content="light"])) .theme-option-dark {
    display: none;
  }
}

/* External Arrow SVG Helper */
.ext-icon {
  width: 11px;
  height: 11px;
  display: inline-block;
  flex-shrink: 0;
}

/* Accessible Native <dialog> Spec Sheet Modal */
dialog.foundry-dialog {
  width: min(640px, calc(100vw - 32px));
  max-height: 88vh;
  margin: auto;
  padding: 0;
  border: 1px solid var(--rule-strong);
  border-radius: var(--radius-xl);
  background-color: var(--bg-paper);
  color: var(--ink);
  box-shadow: 0 28px 64px -18px rgba(0, 0, 0, 0.36);
  overflow: auto;
}

dialog.foundry-dialog:modal {
  overscroll-behavior: contain;
}

dialog.foundry-dialog::backdrop {
  background-color: var(--bg-overlay);
  backdrop-filter: blur(6px);
  -webkit-backdrop-filter: blur(6px);
  overflow: hidden;
  overscroll-behavior: contain;
}

/* Unified Foundry Footer */
.foundry-footer {
  margin-top: auto;
  border-top: 1px solid var(--rule);
  background-color: var(--bg-stone);
}

.foundry-footer-inner {
  padding-block: 32px;
  display: flex;
  flex-direction: column;
  gap: 16px;
  font-size: 12px;
  color: var(--ink-mid);
}

@media (min-width: 640px) {
  .foundry-footer-inner {
    flex-direction: row;
    align-items: center;
    justify-content: space-between;
  }
}

.foundry-footer-links {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 20px;
  list-style: none;
  margin: 0;
  padding: 0;
}

.foundry-footer-links a {
  color: var(--ink-mid);
  text-transform: uppercase;
  transition: color 160ms ease;
}

.foundry-footer-links a:hover,
.foundry-footer-links a[aria-current="page"] {
  color: var(--ink);
  text-decoration: underline;
}

.foundry-footer-mail {
  color: var(--ink);
  text-decoration: underline;
  text-underline-offset: 2px;
}

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