/* Modern CSS for Making Games with Python & Pygame */

/* ============================================
   Theme Tokens
   ============================================
   The light palette is defined on a bare :root so it is always present.
   Dark values are declared twice: once behind the OS preference (skipped
   when the reader has explicitly chosen light) and once for the explicit
   data-theme="dark" choice, so the toggle wins in both directions. */

:root {
  --bg: #ffffff;
  --fg: #2c3e50;
  --fg-muted: #718096;
  --heading: #1a202c;
  --h1: #2c5282;
  --h2: #2d3748;
  --h3: #4a5568;
  --accent: #3498db;
  --accent-strong: #2980b9;
  --visited: #8e44ad;
  --rule: #e2e8f0;
  --border: #cbd5e1;
  --surface: #f8fafc;
  --surface-alt: #f7fafc;
  --code-bg: #f7fafc;
  --code-fg: #d73a49;
  --pre-bg: #ffffff;
  --pre-fg: #2c3e50;
  --comment: #6a7d8c;
  --linenum: #9aa5b1;
  --quote-bg: #f0f9ff;
  --promo-bg: #eeeddd;
  --shadow: rgba(0, 0, 0, 0.1);
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #1a202c;
    --fg: #e2e8f0;
    --fg-muted: #a0aec0;
    --heading: #f7fafc;
    --h1: #90cdf4;
    --h2: #f7fafc;
    --h3: #cbd5e1;
    --accent: #60a5fa;
    --accent-strong: #93bbfc;
    --visited: #a78bfa;
    --rule: #4a5568;
    --border: #4a5568;
    --surface: #1f2937;
    --surface-alt: #374151;
    --code-bg: #2d3748;
    --code-fg: #fbbf24;
    --pre-bg: #0f172a;
    --pre-fg: #e2e8f0;
    --comment: #7f9cb0;
    --linenum: #64748b;
    --quote-bg: #1e293b;
    --promo-bg: #2d3748;
    --shadow: rgba(0, 0, 0, 0.4);
  }
}

:root[data-theme="dark"] {
  --bg: #1a202c;
  --fg: #e2e8f0;
  --fg-muted: #a0aec0;
  --heading: #f7fafc;
  --h1: #90cdf4;
  --h2: #f7fafc;
  --h3: #cbd5e1;
  --accent: #60a5fa;
  --accent-strong: #93bbfc;
  --visited: #a78bfa;
  --rule: #4a5568;
  --border: #4a5568;
  --surface: #1f2937;
  --surface-alt: #374151;
  --code-bg: #2d3748;
  --code-fg: #fbbf24;
  --pre-bg: #0f172a;
  --pre-fg: #e2e8f0;
  --comment: #7f9cb0;
  --linenum: #64748b;
  --quote-bg: #1e293b;
  --promo-bg: #2d3748;
  --shadow: rgba(0, 0, 0, 0.4);
}

/* ============================================
   CSS Reset & Base Styles
   ============================================ */

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

html {
  font-size: 16px;
  scroll-behavior: smooth;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

/* ============================================
   Typography & Body Styles
   ============================================ */

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen',
               'Ubuntu', 'Cantarell', 'Helvetica Neue', sans-serif;
  font-size: 1.125rem;
  line-height: 1.75;
  color: var(--fg);
  background-color: var(--bg);
  padding: 2rem 1.5rem;
}

@media (min-width: 768px) {
  body {
    padding: 3rem 2rem;
  }
}

/* Main chapter container, kept to a comfortable measure. */
.chapter {
  max-width: 55rem;
  margin: 0 auto;
}

/* ============================================
   Headings
   ============================================ */

h1, h2, h3, h4, h5, h6 {
  font-family: 'Georgia', 'Times New Roman', serif;
  color: var(--heading);
  margin-top: 2rem;
  margin-bottom: 1rem;
  line-height: 1.3;
  font-weight: 600;
}

h1 {
  font-size: 2.5rem;
  margin-top: 0;
  padding-bottom: 1rem;
  border-bottom: 3px solid var(--accent);
  color: var(--h1);
}

h2 {
  font-size: 2rem;
  margin-top: 3rem;
  padding-top: 1rem;
  color: var(--h2);
  border-top: 1px solid var(--rule);
}

h3 {
  font-size: 1.5rem;
  color: var(--h3);
}

h4 {
  font-size: 1.25rem;
  color: var(--h3);
}

/* ============================================
   Paragraphs & Text
   ============================================ */

p {
  margin-bottom: 1.5rem;
}

/* ============================================
   Code Blocks & Inline Code
   ============================================ */

code {
  font-family: 'Consolas', 'Monaco', 'Courier New', monospace;
  background-color: var(--code-bg);
  padding: 0.125rem 0.375rem;
  border-radius: 0.25rem;
  font-size: 0.95em;
  color: var(--code-fg);
  border: 1px solid var(--rule);
}

pre {
  background-color: var(--pre-bg);
  color: var(--pre-fg);
  padding: 1.5rem;
  border-radius: 0.5rem;
  overflow-x: auto;
  margin: 1.5rem 0;
  font-size: 0.95rem;
  line-height: 1.5;
  box-shadow: 0 4px 6px var(--shadow);
  border: 1px solid var(--rule);
}

pre code {
  font-family: 'Consolas', 'Monaco', 'Courier New', monospace;
  background-color: transparent;
  color: inherit;
  padding: 0;
  border: none;
  font-size: inherit;
}

/* The numbered program listings the reader is meant to type in. */
pre.listing {
  border-left: 4px solid var(--accent);
}

/* Interactive shell transcripts and short snippets. */
pre.example {
  background-color: var(--surface);
}

/* Program output. */
pre.output {
  background-color: var(--surface);
  border-left: 4px solid var(--fg-muted);
}

/* Comments inside listings stay legible but recede. */
pre .comment {
  color: var(--comment);
  font-style: italic;
}

/* ============================================
   Tables
   ============================================ */

table {
  width: 100%;
  border-collapse: collapse;
  margin: 2rem 0;
  background-color: var(--bg);
  box-shadow: 0 1px 3px var(--shadow);
  border-radius: 0.5rem;
  overflow: hidden;
}

thead {
  background-color: var(--surface);
  color: var(--fg);
}

th {
  padding: 1rem;
  text-align: left;
  font-weight: 600;
  font-size: 0.95rem;
}

td {
  padding: 1rem;
  border-bottom: 1px solid var(--rule);
  vertical-align: top;
}

tbody tr:last-child td {
  border-bottom: none;
}

tbody tr:hover {
  background-color: var(--surface-alt);
  transition: background-color 0.2s ease;
}

tbody tr:nth-child(even) {
  background-color: var(--surface);
}

/* Wide tables scroll inside their own box rather than the page. */
.table-wrap {
  overflow-x: auto;
  margin: 2rem 0;
}

.table-wrap table {
  margin: 0;
}

/* ============================================
   Lists
   ============================================ */

ul, ol {
  margin: 1.5rem 0;
  padding-left: 2rem;
}

li {
  margin-bottom: 0.5rem;
  line-height: 1.8;
}

li::marker {
  color: var(--accent);
}

li ul, li ol {
  margin-top: 0.5rem;
  margin-bottom: 0.5rem;
}

/* ============================================
   Links
   ============================================ */

a {
  color: var(--accent);
  text-decoration: none;
  border-bottom: 2px solid transparent;
  transition: all 0.3s ease;
  font-weight: 500;
}

a:hover {
  color: var(--accent-strong);
  border-bottom-color: var(--accent-strong);
}

a:visited {
  color: var(--visited);
}

/* ============================================
   Images & Figures
   ============================================ */

img {
  max-width: 100%;
  height: auto;
  display: block;
  margin: 2rem auto;
}

figure {
  margin: 2rem 0;
  text-align: center;
}

figure img {
  margin: 0 auto;
}

figcaption {
  margin-top: 0.75rem;
  font-size: 0.95rem;
  color: var(--fg-muted);
}

/* Side-by-side figures (the before/after diagrams). */
.figrow {
  display: flex;
  flex-wrap: wrap;
  gap: 1.5rem;
  justify-content: center;
  margin: 2rem 0;
}

.figrow figure {
  flex: 1 1 16rem;
  margin: 0;
}

/* ============================================
   Utility Classes
   ============================================ */

.text-center {
  text-align: center;
}

blockquote {
  margin: 2rem 0;
  padding: 1rem 1.5rem;
  background-color: var(--quote-bg);
  border-left: 4px solid var(--accent);
  color: var(--fg);
  border-radius: 0.25rem;
}

/* ============================================
   Light / Dark Mode Switch
   ============================================ */

.theme-switch {
  max-width: 55rem;
  margin: 0 auto 1.5rem auto;
  display: flex;
  justify-content: flex-end;
}

.theme-toggle {
  font: inherit;
  font-size: 0.875rem;
  line-height: 1;
  display: inline-flex;
  align-items: center;
  gap: 0.4rem;
  padding: 0.4rem 0.75rem;
  color: var(--fg);
  background-color: var(--surface);
  border: 1px solid var(--border);
  border-radius: 999px;
  cursor: pointer;
  transition: background-color 0.2s ease, border-color 0.2s ease;
}

.theme-toggle:hover {
  background-color: var(--surface-alt);
  border-color: var(--accent);
}

.theme-toggle .theme-icon {
  font-size: 1rem;
}

/* ============================================
   Other Books Promo
   ============================================ */

.otherbooks {
  background-color: var(--promo-bg);
  font-family: inherit;
  font-size: 0.95rem;
  padding: 1rem;
  margin: 0 auto 2rem auto;
  max-width: 55rem;
  border-radius: 0.5rem;
  text-align: center;
}

.otherbooks img.cover {
  display: inline-block;
  height: 150px;
  width: auto;
  margin: 0.25rem;
  border: 1px solid #000;
  vertical-align: bottom;
}

/* ============================================
   Responsive Design
   ============================================ */

@media (max-width: 768px) {
  body {
    font-size: 1rem;
  }

  h1 {
    font-size: 2rem;
  }

  h2 {
    font-size: 1.5rem;
  }

  h3 {
    font-size: 1.25rem;
  }

  pre {
    padding: 1rem;
    font-size: 0.8125rem;
  }

  table {
    font-size: 0.875rem;
  }

  th, td {
    padding: 0.75rem 0.5rem;
  }

  .otherbooks img.cover {
    height: 110px;
  }
}

/* ============================================
   Print Styles
   ============================================ */

@media print {
  body {
    color: #000;
    background: #fff;
    font-size: 12pt;
    line-height: 1.5;
  }

  .otherbooks, .theme-switch {
    display: none;
  }

  h1, h2, h3, h4, h5, h6 {
    page-break-after: avoid;
    color: #000;
  }

  pre, blockquote, figure {
    page-break-inside: avoid;
  }

  a {
    color: #000;
    text-decoration: underline;
  }

  a[href^="http"]:after {
    content: " (" attr(href) ")";
  }

  pre {
    border: 1px solid #999;
    background: #f5f5f5;
    box-shadow: none;
  }
}

/* ============================================
   Animations & Transitions
   ============================================ */

@keyframes fadeIn {
  from {
    opacity: 0;
    transform: translateY(10px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.chapter > * {
  animation: fadeIn 0.6s ease-out;
}

@media (prefers-reduced-motion: reduce) {
  html {
    scroll-behavior: auto;
  }

  .chapter > * {
    animation: none;
  }

  a, .theme-toggle, tbody tr {
    transition: none;
  }
}

/* ============================================
   Accessibility Improvements
   ============================================ */

*:focus-visible {
  outline: 3px solid var(--accent);
  outline-offset: 2px;
}
