/* === Reset === */
*,
*::before,
*::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

/* === Design Tokens === */
:root {
  --font-body: charter, "Bitstream Charter", "Sitka Text", Cambria, serif;
  --font-heading: system-ui, -apple-system, "Segoe UI", sans-serif;
  --font-mono: ui-monospace, "Cascadia Code", "Fira Code", monospace;
  --color-text: #242424;
  --color-bg: #ffffff;
  --color-accent: #1a8917;
  --color-accent-hover: #0f6e0d;
  --color-muted: #6b6b6b;
  --color-border: #e6e6e6;
  --color-surface: #f7f7f7;
  --max-width: 42rem;
  --spacing: 1.5rem;
}

@media (prefers-color-scheme: dark) {
  :root {
    --color-text: #e0e0e0;
    --color-bg: #121212;
    --color-accent: #4caf50;
    --color-accent-hover: #66bb6a;
    --color-muted: #a0a0a0;
    --color-border: #2a2a2a;
    --color-surface: #1e1e1e;
  }
}

/* === Base === */
html {
  font-size: 112.5%; /* 18px */
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

body {
  font-family: var(--font-body);
  color: var(--color-text);
  background: var(--color-bg);
  line-height: 1.7;
  max-width: var(--max-width);
  margin: 0 auto;
  padding: var(--spacing);
  padding-top: 0;
}

/* === Typography === */
h1, h2, h3, h4 {
  font-family: var(--font-heading);
  font-weight: 700;
  line-height: 1.2;
  letter-spacing: -0.02em;
  margin-top: 2rem;
  margin-bottom: 0.5rem;
}

h1 {
  font-size: clamp(2rem, 5vw, 2.8rem);
  letter-spacing: -0.03em;
  margin-top: 0;
}

h2 {
  font-size: clamp(1.4rem, 3.5vw, 1.8rem);
  margin-top: 2.5rem;
}

h3 {
  font-size: clamp(1.15rem, 2.5vw, 1.35rem);
}

p {
  margin-bottom: 1.25rem;
}

strong {
  font-weight: 700;
}

a {
  color: var(--color-text);
  text-decoration: underline;
  text-decoration-color: var(--color-muted);
  text-underline-offset: 0.2em;
  transition: text-decoration-color 0.15s;
}

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

a:focus-visible {
  outline: 2px solid var(--color-accent);
  outline-offset: 2px;
  border-radius: 2px;
}

blockquote {
  border-left: 3px solid var(--color-text);
  padding-left: 1.25rem;
  margin: 1.75rem 0;
  font-style: italic;
  color: var(--color-muted);
}

blockquote p:last-child {
  margin-bottom: 0;
}

ul, ol {
  margin-bottom: 1.25rem;
  padding-left: 1.5rem;
}

li {
  margin-bottom: 0.35rem;
}

li > ul, li > ol {
  margin-bottom: 0;
  margin-top: 0.35rem;
}

code {
  font-family: var(--font-mono);
  font-size: 0.85em;
  background: var(--color-surface);
  padding: 0.15em 0.4em;
  border-radius: 3px;
}

pre {
  background: var(--color-surface);
  padding: 1.25rem;
  border-radius: 6px;
  overflow-x: auto;
  margin: 1.75rem 0;
  line-height: 1.5;
}

pre code {
  background: none;
  padding: 0;
  font-size: 0.85rem;
}

/* === Syntax Highlighting (Prism) === */
:root {
  --syntax-comment: #6a737d;
  --syntax-keyword: #d73a49;
  --syntax-function: #6f42c1;
  --syntax-string: #032f62;
  --syntax-number: #005cc5;
  --syntax-operator: #d73a49;
  --syntax-class: #6f42c1;
  --syntax-property: #005cc5;
  --syntax-punctuation: #24292e;
  --syntax-tag: #22863a;
  --syntax-attr-name: #6f42c1;
  --syntax-attr-value: #032f62;
  --syntax-inserted: #22863a;
  --syntax-deleted: #b31d28;
  --syntax-changed: #e36209;
}

@media (prefers-color-scheme: dark) {
  :root {
    --syntax-comment: #6a9955;
    --syntax-keyword: #569cd6;
    --syntax-function: #dcdcaa;
    --syntax-string: #ce9178;
    --syntax-number: #b5cea8;
    --syntax-operator: #d4d4d4;
    --syntax-class: #4ec9b0;
    --syntax-property: #9cdcfe;
    --syntax-punctuation: #d4d4d4;
    --syntax-tag: #569cd6;
    --syntax-attr-name: #9cdcfe;
    --syntax-attr-value: #ce9178;
    --syntax-inserted: #b5cea8;
    --syntax-deleted: #ce9178;
    --syntax-changed: #dcdcaa;
  }
}

.token.comment,
.token.prolog,
.token.doctype,
.token.cdata {
  color: var(--syntax-comment);
  font-style: italic;
}

.token.keyword,
.token.control,
.token.directive,
.token.unit {
  color: var(--syntax-keyword);
}

.token.function {
  color: var(--syntax-function);
}

.token.string,
.token.char,
.token.regex,
.token.template-string {
  color: var(--syntax-string);
}

.token.number,
.token.boolean {
  color: var(--syntax-number);
}

.token.operator,
.token.entity,
.token.url {
  color: var(--syntax-operator);
}

.token.class-name,
.token.builtin {
  color: var(--syntax-class);
}

.token.property,
.token.constant,
.token.symbol {
  color: var(--syntax-property);
}

.token.punctuation {
  color: var(--syntax-punctuation);
}

.token.tag {
  color: var(--syntax-tag);
}

.token.attr-name {
  color: var(--syntax-attr-name);
}

.token.attr-value {
  color: var(--syntax-attr-value);
}

.token.inserted {
  color: var(--syntax-inserted);
}

.token.deleted {
  color: var(--syntax-deleted);
}

.token.changed {
  color: var(--syntax-changed);
}

.token.important,
.token.bold {
  font-weight: bold;
}

.token.italic {
  font-style: italic;
}

img {
  max-width: 100%;
  height: auto;
  border-radius: 4px;
  display: block;
}

hr {
  border: none;
  text-align: center;
  margin: 2.5rem 0;
}

hr::before {
  content: "...";
  font-size: 1.5rem;
  letter-spacing: 0.6em;
  color: var(--color-muted);
}

/* === Site Header / Navigation === */
header:first-child nav {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 1.25rem 0;
  border-bottom: 1px solid var(--color-border);
  margin-bottom: 2.5rem;
}

.site-name {
  font-family: var(--font-heading);
  font-weight: 800;
  font-size: 1.3rem;
  text-decoration: none;
  color: var(--color-text);
  letter-spacing: -0.02em;
}

header:first-child nav ul {
  display: flex;
  gap: 1.5rem;
  list-style: none;
  padding: 0;
  margin: 0;
}

header:first-child nav ul li {
  margin: 0;
}

header:first-child nav a {
  font-family: var(--font-heading);
  font-size: 0.9rem;
  text-decoration: none;
  color: var(--color-muted);
  transition: color 0.15s;
}

header:first-child nav a:hover {
  color: var(--color-text);
}

header:first-child nav a[aria-current="page"] {
  color: var(--color-text);
  font-weight: 600;
}

/* === Site Footer === */
body > footer,
footer:last-child {
  margin-top: 4rem;
  padding-top: 1.5rem;
  border-top: 1px solid var(--color-border);
  color: var(--color-muted);
  font-size: 0.85rem;
  font-family: var(--font-heading);
}

/* === Post List (homepage, archives, tags) === */
.post-list {
  list-style: none;
  padding: 0;
}

.post-list li {
  margin-bottom: 2rem;
  padding-bottom: 2rem;
  border-bottom: 1px solid var(--color-border);
}

.post-list li:last-child {
  border-bottom: none;
  margin-bottom: 0;
  padding-bottom: 0;
}

.post-list a {
  font-family: var(--font-heading);
  font-size: 1.25rem;
  font-weight: 700;
  color: var(--color-text);
  text-decoration: none;
  letter-spacing: -0.01em;
  display: block;
  margin-bottom: 0.25rem;
}

.post-list a:hover {
  text-decoration: underline;
  text-underline-offset: 0.15em;
}

.post-list time {
  display: block;
  color: var(--color-muted);
  font-family: var(--font-heading);
  font-size: 0.85rem;
}

.post-list .summary {
  color: var(--color-muted);
  font-size: 0.95rem;
  margin-top: 0.35rem;
}

/* === Article (posts) === */
article header {
  border: none;
  margin-bottom: 2rem;
  padding: 0;
}

article header h1 {
  margin-bottom: 0.75rem;
}

article header time {
  font-family: var(--font-heading);
  color: var(--color-muted);
  font-size: 0.9rem;
}

.reading-time {
  font-family: var(--font-heading);
  color: var(--color-muted);
  font-size: 0.9rem;
}

.reading-time::before {
  content: " · ";
}

.hero-image {
  width: 100%;
  margin: 1.5rem 0 2rem;
  border-radius: 6px;
}

article .tags {
  margin-top: 0.5rem;
}

article .tags a {
  font-family: var(--font-heading);
  font-size: 0.8rem;
  color: var(--color-accent);
  text-decoration: none;
  margin-right: 0.75rem;
}

article .tags a:hover {
  text-decoration: underline;
}

article .e-content {
  margin-bottom: 2rem;
}

article .e-content h2:first-child {
  margin-top: 1.5rem;
}

article > footer {
  margin-top: 2rem;
  padding-top: 1rem;
  border-top: 1px solid var(--color-border);
  font-family: var(--font-heading);
  font-size: 0.85rem;
}

article > footer a {
  color: var(--color-muted);
  font-size: 0.85rem;
}

/* === Notes === */
.note {
  max-width: 36rem;
}

.note .e-content {
  font-size: 1.05rem;
}

.note > footer {
  display: flex;
  gap: 1rem;
  align-items: center;
  flex-wrap: wrap;
}

/* === Notes index === */
.notes-feed li {
  padding: 1.5rem 0;
}

.notes-feed li div {
  margin: 0.5rem 0;
}

/* === 404 === */
.not-found {
  text-align: center;
  padding: 4rem 0;
}

.not-found h1 {
  font-size: 4rem;
  margin-bottom: 0.5rem;
}

.not-found p {
  color: var(--color-muted);
  font-size: 1.1rem;
}

/* === Wikilinks === */
a.wikilink {
  color: var(--color-accent);
  text-decoration-style: dotted;
}

a.wikilink:hover {
  text-decoration-style: solid;
}

/* === Callouts === */
.callout {
  border-left: 4px solid var(--color-accent);
  background: var(--color-surface);
  border-radius: 4px;
  padding: 1rem 1.25rem;
  margin: 1.5rem 0;
}

.callout-title {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  font-family: var(--font-heading);
  font-weight: 700;
  font-size: 0.95rem;
  margin-bottom: 0.5rem;
}

.callout-title-icon svg {
  width: 1.1em;
  height: 1.1em;
  fill: currentColor;
}

.callout-content p:last-child {
  margin-bottom: 0;
}

/* Callout type colours */
.callout[data-callout="note"] {
  border-left-color: #448aff;
}

.callout[data-callout="tip"],
.callout[data-callout="hint"] {
  border-left-color: #00bfa5;
}

.callout[data-callout="warning"],
.callout[data-callout="caution"],
.callout[data-callout="attention"] {
  border-left-color: #ff9100;
}

.callout[data-callout="danger"],
.callout[data-callout="error"] {
  border-left-color: #ff5252;
}

.callout[data-callout="info"] {
  border-left-color: #448aff;
}

.callout[data-callout="success"],
.callout[data-callout="check"],
.callout[data-callout="done"] {
  border-left-color: #00c853;
}

.callout[data-callout="question"],
.callout[data-callout="help"],
.callout[data-callout="faq"] {
  border-left-color: #64dd17;
}

.callout[data-callout="abstract"],
.callout[data-callout="summary"],
.callout[data-callout="tldr"] {
  border-left-color: #00b0ff;
}

.callout[data-callout="quote"],
.callout[data-callout="cite"] {
  border-left-color: var(--color-muted);
}

/* === Comments (Giscus) === */
.comments {
  margin-top: 3rem;
  padding-top: 2rem;
  border-top: 1px solid var(--color-border);
}

/* === Utilities === */
[hidden] {
  display: none !important;
}

::selection {
  background: var(--color-accent);
  color: white;
}

/* === Hero — Space Invader === */
.hero-invader {
  text-align: center;
  padding: 2.5rem 0 2rem;
  margin-bottom: 1.5rem;
  border-bottom: 1px solid var(--color-border);
}

.invader-grid {
  display: inline-grid;
  grid-template-columns: repeat(11, 1fr);
  gap: clamp(2px, 0.4vw, 4px);
  margin: 0 auto 1.25rem;
}

.invader-grid span {
  width: clamp(8px, 2vw, 16px);
  height: clamp(8px, 2vw, 16px);
  border-radius: 2px;
}

.invader-grid .px {
  background: var(--color-accent);
  animation: invader-glow 2s ease-in-out infinite alternate;
}

@keyframes invader-glow {
  0% { opacity: 0.85; }
  100% { opacity: 1; }
}

.hero-text {
  font-family: var(--font-mono);
  font-size: clamp(1.6rem, 5vw, 2.8rem);
  font-weight: 800;
  letter-spacing: 0.15em;
  color: var(--color-text);
  margin: 0;
}

/* === Footer Links === */
footer p:last-child a {
  font-size: 0.85rem;
  color: var(--color-muted);
  text-decoration: none;
  transition: color 0.15s;
}

footer p:last-child a:hover {
  color: var(--color-text);
}

/* === Search Button === */
.search-btn {
  background: none;
  border: none;
  cursor: pointer;
  color: var(--color-muted);
  padding: 0.25rem;
  display: flex;
  align-items: center;
  transition: color 0.15s;
}

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

.search-btn:focus-visible {
  outline: 2px solid var(--color-accent);
  outline-offset: 2px;
  border-radius: 2px;
}

/* === Search Overlay === */
.search-overlay {
  position: fixed;
  inset: 0;
  z-index: 100;
  background: rgba(0, 0, 0, 0.4);
  padding-top: 20vh;
  display: flex;
  justify-content: center;
  align-items: flex-start;
}

.search-panel {
  background: var(--color-bg);
  border: 1px solid var(--color-border);
  border-radius: 12px;
  width: 90vw;
  max-width: 36rem;
  max-height: 60vh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  box-shadow: 0 16px 48px rgba(0, 0, 0, 0.2);
}

.search-input-wrap {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  padding: 0.875rem 1rem;
  border-bottom: 1px solid var(--color-border);
}

.search-icon {
  flex-shrink: 0;
  color: var(--color-muted);
}

.search-input {
  flex: 1;
  border: none;
  background: none;
  font-family: var(--font-heading);
  font-size: 1rem;
  color: var(--color-text);
  outline: none;
}

.search-input::placeholder {
  color: var(--color-muted);
}

/* === Search Results === */
.search-results {
  list-style: none;
  padding: 0.5rem;
  margin: 0;
  overflow-y: auto;
}

.search-results:empty {
  padding: 0;
}

.search-results li {
  margin: 0;
  border-radius: 8px;
}

.search-results li a {
  display: block;
  padding: 0.625rem 0.75rem;
  text-decoration: none;
  border-radius: 8px;
}

.search-results li a:hover,
.search-results li.selected a {
  background: var(--color-surface);
}

.search-result-title {
  display: block;
  font-family: var(--font-heading);
  font-weight: 600;
  font-size: 0.95rem;
  color: var(--color-text);
}

.search-result-excerpt {
  display: block;
  font-size: 0.85rem;
  color: var(--color-muted);
  margin-top: 0.15rem;
  line-height: 1.4;
}

.search-result-excerpt mark {
  background: none;
  color: var(--color-accent);
  font-weight: 700;
}

/* Screen-reader only */
.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;
}

/* === Bookmarks === */
.btn-github {
  display: inline-flex;
  align-items: center;
  gap: 0.5rem;
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: 6px;
  padding: 0.6rem 1.2rem;
  font-family: var(--font-heading);
  font-size: 0.95rem;
  font-weight: 600;
  color: var(--color-text);
  text-decoration: none;
  cursor: pointer;
  transition: border-color 0.15s, background 0.15s;
}

.btn-github:hover {
  border-color: var(--color-muted);
  background: var(--color-bg);
  text-decoration: none;
}

.btn-github:focus-visible {
  outline: 2px solid var(--color-accent);
  outline-offset: 2px;
}

.btn-github svg {
  flex-shrink: 0;
}

.bookmarks-signout {
  font-family: var(--font-heading);
  font-size: 0.85rem;
  color: var(--color-muted);
  margin-left: 0.5rem;
}

.bookmark-reactions {
  display: inline-block;
  font-size: 0.85rem;
  margin-left: 0.5rem;
  letter-spacing: 0.1em;
}

.bookmarks-error {
  color: #d32f2f;
}

@media (prefers-color-scheme: dark) {
  .bookmarks-error {
    color: #ef5350;
  }
}

/* === Print === */
@media print {
  .search-overlay,
  .search-btn,
  .hero-invader,
  .btn-github,
  .bookmarks-signout {
    display: none !important;
  }
  body {
    max-width: none;
    padding: 0;
    font-size: 12pt;
  }

  header:first-child nav,
  footer:last-child,
  article > footer {
    display: none;
  }

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

  a[href]::after {
    content: " (" attr(href) ")";
    font-size: 0.8em;
    color: var(--color-muted);
  }
}
