/* ==========================================================================
   brand.css — Alan Byers
   A small, dependency-free design system for public-facing work.
   Editorial and quiet. System fonts. Light and dark, following the OS.

   Contents
   1.  Tokens (color, type, space, radius, shadow, layout)
   2.  Dark theme
   3.  Reset and base elements
   4.  Typography and prose
   5.  Layout helpers
   6.  Components (nav, button, card, badge, callout, footer)
   7.  Principle pages (index, entries, anchors)
   8.  Utilities
   9.  Print
   ========================================================================== */

/* 1. Tokens ================================================================ */

:root {
  color-scheme: light dark;

  /* Color — light */
  --color-bg:            #fafaf8;
  --color-surface:       #ffffff;
  --color-sunken:        #f1f0ec;
  --color-text:          #16161a;
  --color-text-muted:    #56565f;
  --color-text-subtle:   #6e6e78;
  --color-border:        #e3e1da;
  --color-border-strong: #c7c5bc;
  --color-border-ui:     #8e8b83; /* interactive boundaries, meets 3:1 */
  --color-accent:        #2f3a8f;
  --color-accent-hover:  #232c6f;
  --color-accent-soft:   #eceefa;
  --color-focus:         #2f3a8f;
  --color-success:       #1f6b45;
  --color-warning:       #8a5a00;
  --color-danger:        #a12a20;
  --color-selection:     #dfe3f7;

  /* Type — system stack, no downloads */
  --font-sans:
    -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue",
    Arial, "Noto Sans", sans-serif, "Apple Color Emoji", "Segoe UI Emoji";
  --font-serif:
    ui-serif, Georgia, Cambria, "Times New Roman", Times, serif;
  --font-mono:
    ui-monospace, SFMono-Regular, "SF Mono", Menlo, Consolas,
    "Liberation Mono", monospace;

  /* Fluid type scale — 1.250 at small sizes, opening up on wide screens */
  --text-xs:   0.78rem;
  --text-sm:   0.875rem;
  --text-base: clamp(1rem, 0.97rem + 0.15vw, 1.0625rem);
  --text-lg:   clamp(1.125rem, 1.08rem + 0.22vw, 1.25rem);
  --text-xl:   clamp(1.3rem, 1.22rem + 0.4vw, 1.5rem);
  --text-2xl:  clamp(1.6rem, 1.44rem + 0.8vw, 2rem);
  --text-3xl:  clamp(2rem, 1.72rem + 1.4vw, 2.75rem);
  --text-4xl:  clamp(2.4rem, 1.95rem + 2.25vw, 3.5rem);

  --leading-tight:  1.18;
  --leading-snug:   1.35;
  --leading-normal: 1.6;
  --leading-prose:  1.7;

  --tracking-tight: -0.018em;
  --tracking-normal: 0;
  --tracking-wide:  0.08em;

  --weight-normal:   400;
  --weight-medium:   500;
  --weight-semibold: 600;
  --weight-bold:     700;

  /* Space — 4px base */
  --space-3xs: 0.25rem;
  --space-2xs: 0.5rem;
  --space-xs:  0.75rem;
  --space-sm:  1rem;
  --space-md:  1.5rem;
  --space-lg:  2rem;
  --space-xl:  3rem;
  --space-2xl: 4.5rem;
  --space-3xl: 7rem;

  /* Radius */
  --radius-sm:   4px;
  --radius-md:   8px;
  --radius-lg:   14px;
  --radius-full: 999px;

  /* Shadow — restrained, never decorative */
  --shadow-sm: 0 1px 2px rgb(22 22 26 / 0.05);
  --shadow-md: 0 2px 4px rgb(22 22 26 / 0.05), 0 8px 24px rgb(22 22 26 / 0.06);

  /* Layout */
  --measure:        68ch;
  --width-prose:    42rem;
  --width-content:  56rem;
  --width-wide:     74rem;
  --gutter:         clamp(1rem, 0.6rem + 2vw, 2.5rem);
  --nav-height:     4rem;

  /* Motion */
  --ease: cubic-bezier(0.2, 0.6, 0.35, 1);
  --duration: 160ms;
}

/* 2. Dark theme ============================================================ */

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --color-bg:            #121215;
    --color-surface:       #1a1a1f;
    --color-sunken:        #0d0d10;
    --color-text:          #eceae5;
    --color-text-muted:    #a9a8b2;
    --color-text-subtle:   #8e8d97;
    --color-border:        #2c2c33;
    --color-border-strong: #43434d;
    --color-border-ui:     #72727e;
    --color-accent:        #a8b3f5;
    --color-accent-hover:  #c3cbfb;
    --color-accent-soft:   #1e2140;
    --color-focus:         #a8b3f5;
    --color-success:       #6fd39b;
    --color-warning:       #e0b055;
    --color-danger:        #f08a80;
    --color-selection:     #2a2f56;

    --shadow-sm: 0 1px 2px rgb(0 0 0 / 0.4);
    --shadow-md: 0 2px 4px rgb(0 0 0 / 0.35), 0 8px 24px rgb(0 0 0 / 0.45);
  }
}

:root[data-theme="dark"] {
  --color-bg:            #121215;
  --color-surface:       #1a1a1f;
  --color-sunken:        #0d0d10;
  --color-text:          #eceae5;
  --color-text-muted:    #a9a8b2;
  --color-text-subtle:   #8e8d97;
  --color-border:        #2c2c33;
  --color-border-strong: #43434d;
  --color-border-ui:     #72727e;
  --color-accent:        #a8b3f5;
  --color-accent-hover:  #c3cbfb;
  --color-accent-soft:   #1e2140;
  --color-focus:         #a8b3f5;
  --color-success:       #6fd39b;
  --color-warning:       #e0b055;
  --color-danger:        #f08a80;
  --color-selection:     #2a2f56;

  --shadow-sm: 0 1px 2px rgb(0 0 0 / 0.4);
  --shadow-md: 0 2px 4px rgb(0 0 0 / 0.35), 0 8px 24px rgb(0 0 0 / 0.45);
}

/* 3. Reset and base ======================================================== */

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

* { margin: 0; }

html {
  -webkit-text-size-adjust: 100%;
  scroll-behavior: smooth;
  scroll-padding-top: calc(var(--nav-height) + var(--space-sm));
}

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

body {
  background: var(--color-bg);
  color: var(--color-text);
  font-family: var(--font-sans);
  font-size: var(--text-base);
  line-height: var(--leading-normal);
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
  min-height: 100vh;
}

::selection {
  background: var(--color-selection);
  color: var(--color-text);
}

img,
picture,
svg,
video,
canvas {
  display: block;
  max-width: 100%;
  height: auto;
}

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

/* One focus treatment everywhere. Visible on every interactive element. */
:where(a, button, input, textarea, select, summary, [tabindex]):focus-visible {
  outline: 2px solid var(--color-focus);
  outline-offset: 2px;
  border-radius: var(--radius-sm);
}

.skip-link {
  position: absolute;
  left: var(--space-sm);
  top: var(--space-sm);
  z-index: 100;
  padding: var(--space-2xs) var(--space-sm);
  background: var(--color-surface);
  color: var(--color-text);
  border: 1px solid var(--color-border-strong);
  border-radius: var(--radius-md);
  text-decoration: none;
  transform: translateY(-200%);
}

.skip-link:focus-visible { transform: translateY(0); }

/* 4. Typography ============================================================ */

h1, h2, h3, h4, h5, h6 {
  font-weight: var(--weight-semibold);
  letter-spacing: var(--tracking-tight);
  line-height: var(--leading-tight);
  text-wrap: balance;
}

h1 { font-size: var(--text-4xl); }
h2 { font-size: var(--text-2xl); }
h3 { font-size: var(--text-xl); }
h4 { font-size: var(--text-lg); }
h5, h6 { font-size: var(--text-base); }

p, li { text-wrap: pretty; }

a {
  color: var(--color-accent);
  text-decoration: underline;
  text-underline-offset: 0.18em;
  text-decoration-thickness: 1px;
  text-decoration-color: color-mix(in srgb, currentColor 40%, transparent);
  transition: color var(--duration) var(--ease),
              text-decoration-color var(--duration) var(--ease);
}

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

strong, b { font-weight: var(--weight-semibold); }

small { font-size: var(--text-sm); color: var(--color-text-muted); }

code, kbd, samp, pre {
  font-family: var(--font-mono);
  font-size: 0.9em;
}

code {
  background: var(--color-sunken);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-sm);
  padding: 0.1em 0.35em;
}

pre {
  background: var(--color-sunken);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-md);
  padding: var(--space-sm);
  overflow-x: auto;
  line-height: var(--leading-snug);
}

pre code {
  background: none;
  border: 0;
  padding: 0;
}

blockquote {
  border-left: 3px solid var(--color-border-strong);
  padding-left: var(--space-sm);
  color: var(--color-text-muted);
  font-style: normal;
}

hr {
  border: 0;
  border-top: 1px solid var(--color-border);
  margin-block: var(--space-xl);
}

table {
  width: 100%;
  border-collapse: collapse;
  font-size: var(--text-sm);
}

th, td {
  text-align: left;
  padding: var(--space-2xs) var(--space-xs);
  border-bottom: 1px solid var(--color-border);
  vertical-align: top;
}

th {
  font-weight: var(--weight-semibold);
  color: var(--color-text-muted);
  font-size: var(--text-xs);
  letter-spacing: var(--tracking-wide);
  text-transform: uppercase;
}

/* Prose: the reading column. Vertical rhythm handled by flow spacing. */
.prose {
  max-width: var(--measure);
  font-size: var(--text-lg);
  line-height: var(--leading-prose);
  color: var(--color-text);
}

.prose > * + * { margin-top: var(--space-sm); }
.prose > * + :is(h2, h3, h4) { margin-top: var(--space-lg); }
.prose :is(h2, h3, h4) + * { margin-top: var(--space-2xs); }
.prose :is(ul, ol) { padding-left: 1.4em; }
.prose li + li { margin-top: var(--space-3xs); }
.prose figure figcaption {
  font-size: var(--text-sm);
  color: var(--color-text-muted);
  margin-top: var(--space-2xs);
}

/* Small caps label above a heading */
.kicker {
  font-size: var(--text-xs);
  font-weight: var(--weight-semibold);
  letter-spacing: var(--tracking-wide);
  text-transform: uppercase;
  color: var(--color-text-muted);
}

.lede {
  font-size: var(--text-xl);
  line-height: var(--leading-snug);
  color: var(--color-text-muted);
  max-width: var(--measure);
}

.meta {
  font-size: var(--text-sm);
  color: var(--color-text-subtle);
}

/* 5. Layout ================================================================ */

.container {
  width: 100%;
  max-width: var(--width-content);
  margin-inline: auto;
  padding-inline: var(--gutter);
}

.container--prose { max-width: var(--width-prose); }
.container--wide  { max-width: var(--width-wide); }

.section { padding-block: var(--space-2xl); }
.section--tight { padding-block: var(--space-xl); }

.stack > * + * { margin-top: var(--space-sm); }
.stack-lg > * + * { margin-top: var(--space-lg); }

.cluster {
  display: flex;
  flex-wrap: wrap;
  gap: var(--space-2xs);
  align-items: center;
}

.grid {
  display: grid;
  gap: var(--space-md);
  grid-template-columns: repeat(auto-fit, minmax(min(100%, 17rem), 1fr));
}

/* 6. Components ============================================================ */

/* Nav */
.site-nav {
  position: sticky;
  top: 0;
  z-index: 50;
  background: color-mix(in srgb, var(--color-bg) 88%, transparent);
  backdrop-filter: saturate(140%) blur(8px);
  border-bottom: 1px solid var(--color-border);
}

.site-nav__inner {
  max-width: var(--width-wide);
  margin-inline: auto;
  padding: 0 var(--gutter);
  height: var(--nav-height);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-md);
}

.site-nav__brand {
  font-weight: var(--weight-semibold);
  letter-spacing: var(--tracking-tight);
  color: var(--color-text);
  text-decoration: none;
}

.site-nav__links {
  display: flex;
  gap: var(--space-md);
  list-style: none;
  padding: 0;
  font-size: var(--text-sm);
}

.site-nav__links a {
  color: var(--color-text-muted);
  text-decoration: none;
}

.site-nav__links a:hover,
.site-nav__links a[aria-current="page"] { color: var(--color-text); }

/* Button */
.btn {
  display: inline-flex;
  align-items: center;
  gap: var(--space-2xs);
  padding: 0.55em 1.1em;
  border-radius: var(--radius-md);
  border: 1px solid transparent;
  font-size: var(--text-sm);
  font-weight: var(--weight-medium);
  line-height: 1.2;
  text-decoration: none;
  cursor: pointer;
  transition: background-color var(--duration) var(--ease),
              border-color var(--duration) var(--ease),
              color var(--duration) var(--ease);
}

.btn--primary {
  background: var(--color-accent);
  color: var(--color-bg);
  border-color: var(--color-accent);
}

.btn--primary:hover {
  background: var(--color-accent-hover);
  border-color: var(--color-accent-hover);
  color: var(--color-bg);
}

.btn--secondary {
  background: var(--color-surface);
  color: var(--color-text);
  border-color: var(--color-border-ui);
}

.btn--secondary:hover { border-color: var(--color-text-muted); }

.btn--ghost {
  background: transparent;
  color: var(--color-text-muted);
}

.btn--ghost:hover { color: var(--color-text); }

.btn[aria-disabled="true"],
.btn:disabled { opacity: 0.55; cursor: not-allowed; }

/* Card */
.card {
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-lg);
  padding: var(--space-md);
  box-shadow: var(--shadow-sm);
}

.card__title {
  font-size: var(--text-lg);
  margin-bottom: var(--space-3xs);
}

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

a.card {
  display: block;
  text-decoration: none;
  color: inherit;
  transition: border-color var(--duration) var(--ease),
              box-shadow var(--duration) var(--ease);
}

a.card:hover {
  border-color: var(--color-border-strong);
  box-shadow: var(--shadow-md);
}

/* Badge */
.badge {
  display: inline-flex;
  align-items: center;
  gap: var(--space-3xs);
  padding: 0.2em 0.6em;
  border-radius: var(--radius-full);
  border: 1px solid var(--color-border-ui);
  background: var(--color-sunken);
  color: var(--color-text-muted);
  font-size: var(--text-xs);
  font-weight: var(--weight-medium);
  letter-spacing: 0.01em;
}

.badge--accent {
  background: var(--color-accent-soft);
  border-color: color-mix(in srgb, var(--color-accent) 35%, transparent);
  color: var(--color-accent);
}

/* Callout */
.callout {
  border: 1px solid var(--color-border);
  border-left: 3px solid var(--color-border-strong);
  border-radius: var(--radius-md);
  background: var(--color-surface);
  padding: var(--space-sm) var(--space-md);
  font-size: var(--text-base);
}

.callout__label {
  display: block;
  font-size: var(--text-xs);
  font-weight: var(--weight-semibold);
  letter-spacing: var(--tracking-wide);
  text-transform: uppercase;
  color: var(--color-text-muted);
  margin-bottom: var(--space-3xs);
}

.callout--accent  { border-left-color: var(--color-accent); }
.callout--success { border-left-color: var(--color-success); }
.callout--warning { border-left-color: var(--color-warning); }
.callout--danger  { border-left-color: var(--color-danger); }

/* Footer */
.site-footer {
  border-top: 1px solid var(--color-border);
  margin-top: var(--space-3xl);
  padding-block: var(--space-lg) var(--space-xl);
  font-size: var(--text-sm);
  color: var(--color-text-muted);
}

.site-footer__inner {
  max-width: var(--width-wide);
  margin-inline: auto;
  padding-inline: var(--gutter);
  display: flex;
  flex-wrap: wrap;
  gap: var(--space-sm) var(--space-lg);
  justify-content: space-between;
}

/* 7. Principle pages ======================================================= */

.principles-layout {
  max-width: var(--width-wide);
  margin-inline: auto;
  padding-inline: var(--gutter);
  display: grid;
  gap: var(--space-xl);
  grid-template-columns: 1fr;
}

@media (min-width: 60rem) {
  .principles-layout {
    grid-template-columns: 15rem minmax(0, 1fr);
    gap: var(--space-2xl);
  }
}

/* Sticky index */
.principle-index {
  font-size: var(--text-sm);
}

@media (min-width: 60rem) {
  .principle-index {
    position: sticky;
    top: calc(var(--nav-height) + var(--space-md));
    align-self: start;
    max-height: calc(100vh - var(--nav-height) - var(--space-xl));
    overflow-y: auto;
  }
}

.principle-index__title {
  font-size: var(--text-xs);
  font-weight: var(--weight-semibold);
  letter-spacing: var(--tracking-wide);
  text-transform: uppercase;
  color: var(--color-text-muted);
  margin-bottom: var(--space-2xs);
}

.principle-index__list {
  list-style: none;
  padding: 0;
  display: grid;
  gap: var(--space-3xs);
  counter-reset: idx;
}

.principle-index__list a {
  display: grid;
  grid-template-columns: 1.6rem 1fr;
  gap: var(--space-3xs);
  padding: var(--space-3xs) var(--space-3xs) var(--space-3xs) 0;
  color: var(--color-text-muted);
  text-decoration: none;
  border-radius: var(--radius-sm);
  line-height: var(--leading-snug);
}

.principle-index__list a::before {
  counter-increment: idx;
  content: counter(idx, decimal-leading-zero);
  font-family: var(--font-mono);
  font-size: var(--text-xs);
  color: var(--color-text-subtle);
}

.principle-index__list a:hover,
.principle-index__list a[aria-current="true"] {
  color: var(--color-text);
}

/* Entries */
.principle-list { counter-reset: principle; }

.principle {
  counter-increment: principle;
  padding-block: var(--space-xl);
  border-top: 1px solid var(--color-border);
  scroll-margin-top: calc(var(--nav-height) + var(--space-md));
}

.principle:first-child { border-top: 0; padding-top: var(--space-md); }

.principle__number {
  font-family: var(--font-mono);
  font-size: var(--text-sm);
  color: var(--color-text-subtle);
  display: block;
  margin-bottom: var(--space-2xs);
}

.principle__number::before { content: counter(principle, decimal-leading-zero); }

.principle__title {
  font-size: var(--text-2xl);
  max-width: 24ch;
}

.principle__summary {
  font-size: var(--text-lg);
  line-height: var(--leading-snug);
  color: var(--color-text-muted);
  max-width: var(--measure);
  margin-top: var(--space-2xs);
}

.principle__body {
  max-width: var(--measure);
  margin-top: var(--space-sm);
  line-height: var(--leading-prose);
}

.principle__body > * + * { margin-top: var(--space-sm); }

.principle__example {
  margin-top: var(--space-md);
  max-width: var(--measure);
}

/* Anchor link that appears on hover or keyboard focus */
.anchor {
  margin-left: 0.4em;
  font-size: 0.7em;
  color: var(--color-text-subtle);
  text-decoration: none;
  opacity: 0;
  transition: opacity var(--duration) var(--ease);
}

:is(h1, h2, h3, .principle__title):hover .anchor,
.anchor:focus-visible { opacity: 1; }

/* Form controls */
input:where([type="text"], [type="email"], [type="search"], [type="url"]),
textarea,
select {
  width: 100%;
  padding: 0.55em 0.7em;
  background: var(--color-surface);
  color: var(--color-text);
  border: 1px solid var(--color-border-ui);
  border-radius: var(--radius-md);
}

input::placeholder,
textarea::placeholder { color: var(--color-text-subtle); }

label {
  display: inline-block;
  font-size: var(--text-sm);
  font-weight: var(--weight-medium);
  margin-bottom: var(--space-3xs);
}

/* 8. Utilities ============================================================= */

.visually-hidden {
  position: absolute !important;
  width: 1px; height: 1px;
  padding: 0; margin: -1px;
  overflow: hidden;
  clip-path: inset(50%);
  white-space: nowrap;
}

.text-muted  { color: var(--color-text-muted); }
.text-subtle { color: var(--color-text-subtle); }
.text-center { text-align: center; }
.mono        { font-family: var(--font-mono); }
.measure     { max-width: var(--measure); }
.no-underline { text-decoration: none; }

/* 9. Print ================================================================= */

@media print {
  :root { --color-bg: #fff; --color-text: #000; }
  .site-nav, .principle-index, .skip-link { display: none; }
  body { font-size: 11pt; }
  a::after { content: " (" attr(href) ")"; font-size: 0.85em; }
}
