/* ==========================================================
   Contrast: stylesheet
   Order: tokens -> base -> layout -> components -> preferences
   ========================================================== */

/* 1. Box model */
*,
*::before,
*::after {
  box-sizing: border-box;
}

/* 2. Design tokens, named by role (lecture 6, 8).
      light-dark() picks the first value in light mode, the second in dark mode.
      Dark is deep violet, not an inversion: lighter, less saturated accents. */
:root {
  color-scheme: light dark;

  --c-text:        light-dark(#1b1033, #f3ecff);
  --c-muted:       light-dark(#4c4268, #c4b5e0);
  --c-bg:          light-dark(#fbf5ff, #170f2b);
  --c-surface:     light-dark(#ffffff, #231a3d);
  --c-surface-alt: light-dark(#f1e6ff, #2e2250);
  --c-brand:       light-dark(#6d28d9, #c4a6ff);   /* dominant colour */
  --c-accent:      light-dark(#be185d, #ff8cc6);   /* the one accent  */
  --c-success:     light-dark(#047857, #34d399);   /* state: pass     */
  --c-border:      light-dark(#7c6ca6, #8f7fc0);   /* >= 3:1 for controls */
  --c-on-brand:    light-dark(#ffffff, #170f2b);   /* text on a brand fill */
  --c-shadow:      light-dark(rgb(109 40 217 / 0.18), rgb(0 0 0 / 0.5));

  --f-sans: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;

  /* Body stays at 1rem; only display sizes are fluid */
  --font-lead: clamp(1.125rem, 1rem + 0.6vw, 1.4rem);
  --font-h1:   clamp(2.25rem, 1.5rem + 4vw, 4rem);
  --font-h2:   clamp(1.5rem, 1.2rem + 1.5vw, 2.25rem);
  --font-h3:   clamp(1.15rem, 1.05rem + 0.6vw, 1.4rem);

  --space-1: 0.5rem;
  --space-2: 1rem;
  --space-3: 1.5rem;
  --space-4: 2.5rem;
  --radius:  0.75rem;
}

/* 3. Base */
html {
  scroll-behavior: smooth;
  scroll-padding-top: 5rem;   /* anchors stay clear of the sticky nav */
}

body {
  margin: 0;
  font-family: var(--f-sans);
  font-size: 1rem;
  line-height: 1.6;
  color: var(--c-text);
  background-color: var(--c-bg);
}

p,
li,
blockquote {
  max-width: 65ch;       /* readable line length */
  text-wrap: pretty;
}

h1, h2, h3 {
  text-wrap: balance;
  line-height: 1.15;
}

h1 {
  font-size: var(--font-h1);
  font-weight: 800;
  letter-spacing: -0.03em;
  margin: 0 0 var(--space-1);
}

h2 {
  font-size: var(--font-h2);
  font-weight: 700;
  margin: var(--space-4) 0 var(--space-1);
  padding-bottom: 0.3rem;
  border-bottom: 3px solid var(--c-accent);
  display: inline-block;
  color: var(--c-brand);
}

h3 {
  font-size: var(--font-h3);
  margin-block: 0 var(--space-1);
}

a {
  color: var(--c-brand);
  text-decoration: underline;
  text-underline-offset: 0.25em;
}

a:hover {
  color: var(--c-accent);
}

:focus-visible {
  outline: 3px solid var(--c-accent);
  outline-offset: 3px;
}

img {
  max-width: 100%;
  height: auto;
  display: block;
}

/* 4. Layout */
header,
nav,
main,
footer {
  width: 100%;
  max-width: 75rem;
  margin-inline: auto;
  padding: var(--space-2);
}

main {
  container-type: inline-size;   /* lets components react to the page column */
  container-name: page;
}

.lead {
  font-size: var(--font-lead);
  max-width: 100%;
}

.meta {
  color: var(--c-muted);
  font-size: 0.9375rem;
}

/* 5. Skip link */
.skip-link {
  position: absolute;
  top: -10rem;
  left: var(--space-2);
  background: var(--c-brand);
  color: var(--c-on-brand);
  padding: var(--space-1) var(--space-2);
  border-radius: 0.25rem;
  font-weight: 700;
  z-index: 100;
}

.skip-link:focus {
  top: var(--space-2);
  color: var(--c-on-brand);
}

/* 6. Header: dominant violet to the accent. White text stays above 6:1 on both ends. */
.site-header {
  max-width: none;
  background: linear-gradient(135deg, #5b21b6, #be185d);
  color: #ffffff;
  padding-block: var(--space-4);
  border-radius: 0 0 1.5rem 1.5rem;
  box-shadow: 0 15px 30px -10px var(--c-shadow);
}

.site-header > * {
  max-width: 75rem;
  margin-inline: auto;
}

.site-header h1,
.site-header .lead {
  color: #ffffff;
}

/* 7. Navigation: few links, always visible (lecture 4) */
nav {
  position: sticky;
  top: 0;
  z-index: 10;
  background: var(--c-surface);
  border-bottom: 2px solid var(--c-border);
}

nav ul {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-wrap: wrap;
  gap: var(--space-1) var(--space-2);
}

nav a {
  display: inline-block;
  padding: 0.5rem 0.75rem;
  border-radius: 0.375rem;
  font-weight: 600;
  text-decoration: none;
}

nav a:hover {
  background: var(--c-surface-alt);
  text-decoration: underline;
}

nav a[aria-current="page"] {
  background: var(--c-surface-alt);
  box-shadow: inset 0 -3px 0 var(--c-accent);
}

/* 8. Cards: Grid without breakpoints, so the browser decides the columns */
.cards {
  display: grid;
  gap: var(--space-2);
  grid-template-columns: repeat(auto-fit, minmax(min(100%, 15rem), 1fr));
  margin-top: var(--space-2);
}

.card {
  background: var(--c-surface);
  border: 1px solid var(--c-border);
  border-top: 5px solid var(--c-brand);
  border-radius: var(--radius);
  padding: var(--space-3);
  box-shadow: 0 10px 25px -8px var(--c-shadow);
  transition: transform 0.25s ease, box-shadow 0.25s ease;
}

.card:nth-child(3n+2) { border-top-color: var(--c-accent); }
.card:nth-child(3n+3) { border-top-color: var(--c-success); }

.card:hover {
  transform: translateY(-4px);
  box-shadow: 0 20px 35px -10px var(--c-shadow);
}

.card h3 {
  color: var(--c-brand);
}

.card > :last-child {
  margin-bottom: 0;
}

/* Container query: when the page column is wide, the first card of a group spans two columns */
@container page (min-width: 60em) {
  .cards--feature > .card:first-child {
    grid-column: span 2;
  }
}

blockquote {
  margin: 0;
  padding-left: var(--space-2);
  border-left: 4px solid var(--c-accent);
  font-style: italic;
}

/* 9. Figures and images */
figure {
  margin: var(--space-3) 0;
  padding: var(--space-2);
  background: var(--c-surface);
  border: 1px solid var(--c-border);
  border-radius: var(--radius);
}

figure img {
  width: 100%;
  border-radius: 0.375rem;
}

figcaption {
  margin-top: var(--space-1);
  color: var(--c-muted);
  font-size: 0.9375rem;
}

/* Pass swatches: real text on real colour, ratio written out */
.swatches {
  display: grid;
  gap: var(--space-2);
  grid-template-columns: repeat(auto-fit, minmax(min(100%, 14rem), 1fr));
}

.swatch {
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
  padding: var(--space-3);
  border-radius: 0.625rem;
  color: #ffffff;
}

.swatch strong {
  font-size: clamp(2.5rem, 2rem + 3vw, 4rem);
  line-height: 1;
}

.swatch--brand   { background: #6d28d9; }   /* 7.10:1 with white */
.swatch--success { background: #047857; }   /* 5.48:1 with white */
.swatch--accent  { background: #be185d; }   /* 6.04:1 with white */

.ratio {
  font-weight: 700;
}

/* 10. Buttons, with every state (lecture 8, slide 18) */
.btn {
  font: inherit;
  font-weight: 700;
  padding: 0.625rem 1.125rem;
  min-height: 2.75rem;
  color: var(--c-on-brand);
  background: var(--c-brand);
  border: 2px solid var(--c-brand);
  border-radius: 0.5rem;
  cursor: pointer;
}

.btn:hover {
  background: var(--c-accent);
  border-color: var(--c-accent);
}

.btn:active {
  transform: translateY(1px);
}

.btn:disabled {
  cursor: not-allowed;
  color: var(--c-muted);
  background: transparent;
  border-style: dashed;
  border-color: var(--c-border);
}

/* 11. Contrast tester (JavaScript only adds behaviour on top of the content) */
.tester {
  display: grid;
  gap: var(--space-2);
  padding: var(--space-3);
  background: var(--c-surface);
  border: 1px solid var(--c-border);
  border-radius: var(--radius);
}

.tester[hidden] {
  display: none;
}

.tester-fields {
  display: grid;
  gap: var(--space-2);
  grid-template-columns: repeat(auto-fit, minmax(min(100%, 10rem), 1fr));
}

.tester label {
  display: grid;
  gap: 0.25rem;
  font-weight: 600;
}

.tester input[type="color"] {
  width: 100%;
  height: 3rem;
  padding: 0.125rem;
  background: var(--c-surface);
  border: 2px solid var(--c-border);
  border-radius: 0.5rem;
  cursor: pointer;
}

.tester-sample {
  padding: var(--space-3);
  border-radius: 0.5rem;
  border: 1px solid var(--c-border);
  font-size: 1.25rem;
  font-weight: 700;
}

.tester-result {
  margin: 0;
  font-weight: 700;
}

/* 12. Aside, footer */
aside {
  margin-top: var(--space-3);
  padding: var(--space-3);
  background: var(--c-surface-alt);
  border-left: 6px solid var(--c-accent);
  border-radius: 0 var(--radius) var(--radius) 0;
}

footer {
  margin-top: var(--space-4);
  text-align: center;
  color: var(--c-muted);
  border-top: 2px solid var(--c-border);
}

/* Style guide page */
.token-list {
  display: grid;
  gap: var(--space-1);
  grid-template-columns: repeat(auto-fit, minmax(min(100%, 12rem), 1fr));
  padding: 0;
  list-style: none;
}

.token {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  padding: 0.5rem;
  background: var(--c-surface);
  border: 1px solid var(--c-border);
  border-radius: 0.5rem;
  max-width: none;
}

.token i {
  flex: none;
  width: 2.5rem;
  height: 2.5rem;
  border-radius: 0.375rem;
  border: 1px solid var(--c-border);
}

.token code {
  font-size: 0.875rem;
}

/* ==========================================================
   User preferences (lecture 1, 6)
   ========================================================== */
@media (prefers-reduced-motion: reduce) {
  html { scroll-behavior: auto; }
  *, *::before, *::after {
    animation: none !important;
    transition: none !important;
  }
  .card:hover { transform: none; }
}

@media (prefers-contrast: more) {
  :root {
    --c-text:   light-dark(#000000, #ffffff);
    --c-muted:  light-dark(#1b1033, #f3ecff);
    --c-border: light-dark(#000000, #ffffff);
  }
}

@media (forced-colors: active) {
  .card,
  .swatch,
  .btn,
  .tester-sample {
    border: 1px solid CanvasText;
  }
}
