/* ═══════════════════════════════════════════
   OpenSource Arch — Styles
   Design: The Quiet Desk
   ═══════════════════════════════════════════ */

/* --- Tokens --- */

:root {
  /* Warm neutral palette (tinted toward warm brown) */
  --bg:          #f7f4ef;
  --bg-elevated: #fdfbf8;
  --fg:          #2c2a27;
  --fg-muted:    #6e6963;
  --fg-faint:    #9f9890;
  --accent:      #a84832;
  --accent-subtle: #e8d5cf;
  --border:      #d8d3cc;
  --border-strong: #bfb9b1;
  --code-bg:     #1e1c19;
  --code-fg:     #d4cfc8;

  /* Typography */
  --font-display: Georgia, 'Noto Serif SC', serif;
  --font-body:    system-ui, 'Noto Sans SC', sans-serif;
  --font-code:    'SF Mono', 'Fira Code', 'Consolas', monospace;

  /* Spacing scale */
  --sp-xs:  0.25rem;
  --sp-sm:  0.5rem;
  --sp-md:  1rem;
  --sp-lg:  1.75rem;
  --sp-xl:  3rem;
  --sp-2xl: 5rem;
  --sp-3xl: 8rem;

  /* Layout */
  --width-content: 680px;
  --width-wide:    960px;

  /* Motion */
  --ease-out: cubic-bezier(0.25, 1, 0.5, 1);
  --duration: 0.2s;
}

/* --- Reset --- */

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

html {
  font-size: 17px;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

@media (prefers-reduced-motion: no-preference) {
  html {
    scroll-behavior: smooth;
  }
}

body {
  font-family: var(--font-body);
  font-weight: 400;
  line-height: 1.7;
  color: var(--fg);
  background-color: var(--bg);
}

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

/* --- Layout primitives --- */

.page {
  max-width: var(--width-content);
  margin: 0 auto;
  padding: 0 var(--sp-lg);
}

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

/* --- Header --- */

.site-header {
  padding: var(--sp-3xl) 0 var(--sp-xl);
}

.site-header__title {
  font-family: var(--font-display);
  font-weight: 700;
  font-size: clamp(1.6rem, 3.5vw, 2.4rem);
  line-height: 1.15;
  letter-spacing: -0.02em;
  color: var(--fg);
}

.site-header__tagline {
  margin-top: var(--sp-sm);
  font-size: 1rem;
  font-weight: 300;
  color: var(--fg-muted);
  max-width: 38ch;
}

/* --- Divider --- */

.rule {
  border: none;
  border-top: 1px solid var(--border);
  margin: var(--sp-xl) 0;
}

/* --- Index list --- */

.index {
  list-style: none;
  margin-top: var(--sp-lg);
}

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

.index__item:last-child {
  border-bottom: none;
}

.index__link {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: var(--sp-md);
  padding: var(--sp-md) 0;
  text-decoration: none;
  color: var(--fg);
  transition: color var(--duration) var(--ease-out);
}

.index__link:hover {
  color: var(--accent);
}

.index__link:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 4px;
  border-radius: 2px;
}

.index__name {
  font-family: var(--font-display);
  font-weight: 600;
  font-size: 1.15rem;
}

.index__meta {
  font-size: 0.85rem;
  color: var(--fg-faint);
  white-space: nowrap;
}

.index__desc {
  margin-top: var(--sp-xs);
  font-size: 0.92rem;
  color: var(--fg-muted);
  max-width: 55ch;
}

/* --- Article page --- */

.article-header {
  padding: var(--sp-3xl) 0 var(--sp-xl);
}

.article-header__title {
  font-family: var(--font-display);
  font-weight: 700;
  font-size: clamp(1.5rem, 3vw, 2.2rem);
  line-height: 1.2;
  letter-spacing: -0.015em;
}

.article-header__subtitle {
  margin-top: var(--sp-sm);
  font-size: 0.9rem;
  color: var(--fg-faint);
}

/* --- Back link --- */

.back-link {
  display: inline-flex;
  align-items: center;
  gap: var(--sp-xs);
  font-size: 0.88rem;
  font-weight: 500;
  color: var(--fg-muted);
  text-decoration: none;
  margin-bottom: var(--sp-xl);
  transition: color var(--duration) var(--ease-out);
}

.back-link:hover {
  color: var(--accent);
}

.back-link:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 3px;
  border-radius: 2px;
}

.back-link__arrow {
  font-size: 1.1em;
}

/* --- Article body (rendered Markdown) --- */

.article-body {
  padding-bottom: var(--sp-3xl);
}

.article-body h1 {
  display: none;
}

.article-body h2 {
  font-family: var(--font-display);
  font-weight: 700;
  font-size: 1.5rem;
  line-height: 1.25;
  margin-top: var(--sp-2xl);
  margin-bottom: var(--sp-md);
  color: var(--fg);
}

.article-body h3 {
  font-family: var(--font-display);
  font-weight: 600;
  font-size: 1.15rem;
  line-height: 1.3;
  margin-top: var(--sp-xl);
  margin-bottom: var(--sp-sm);
  color: var(--fg);
}

.article-body h4 {
  font-family: var(--font-body);
  font-weight: 600;
  font-size: 1rem;
  margin-top: var(--sp-lg);
  margin-bottom: var(--sp-xs);
}

.article-body p {
  margin-bottom: var(--sp-md);
}

.article-body a {
  color: var(--accent);
  text-decoration: underline;
  text-underline-offset: 2px;
  text-decoration-thickness: 1px;
  transition: color var(--duration) var(--ease-out);
}

.article-body a:hover {
  color: var(--fg);
  text-decoration-thickness: 2px;
}

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

.article-body ul,
.article-body ol {
  margin-bottom: var(--sp-md);
  padding-left: var(--sp-lg);
}

.article-body li {
  margin-bottom: var(--sp-xs);
}

.article-body blockquote {
  border-left: 1px solid var(--border-strong);
  padding-left: var(--sp-lg);
  margin: var(--sp-lg) 0;
  color: var(--fg-muted);
  font-style: italic;
}

.article-body code {
  font-family: var(--font-code);
  font-size: 0.88em;
  background: var(--border);
  padding: 0.15em 0.4em;
  border-radius: 3px;
}

.article-body pre {
  background: var(--code-bg);
  color: var(--code-fg);
  padding: var(--sp-lg);
  border-radius: 6px;
  overflow-x: auto;
  margin: var(--sp-lg) 0;
  line-height: 1.5;
}

.article-body pre code {
  background: none;
  padding: 0;
  font-size: 0.85rem;
  color: inherit;
}

.article-body table {
  width: 100%;
  border-collapse: collapse;
  margin: var(--sp-lg) 0;
  font-size: 0.9rem;
}

.article-body th,
.article-body td {
  text-align: left;
  padding: var(--sp-sm) var(--sp-md);
  border-bottom: 1px solid var(--border);
}

.article-body th {
  font-weight: 600;
  color: var(--fg);
  font-size: 0.85rem;
  text-transform: uppercase;
  letter-spacing: 0.03em;
}

.article-body td {
  color: var(--fg-muted);
}

.article-body img {
  max-width: 100%;
  border-radius: 3px;
}

.article-body hr {
  border: none;
  border-top: 1px solid var(--border);
  margin: var(--sp-xl) 0;
}

/* Mermaid diagrams */
.article-body pre.mermaid {
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  color: var(--fg);
  padding: var(--sp-lg);
  margin: var(--sp-lg) 0;
  border-radius: var(--rounded-md, 6px);
  cursor: zoom-in;
  overflow-x: auto;
  max-width: calc(100vw - 2 * var(--sp-lg));
  transition: border-color var(--duration) var(--ease-out);
}

.article-body pre.mermaid:hover {
  border-color: var(--border-strong);
}

.article-body pre.mermaid:focus-within {
  outline: none;
}

.article-body pre.mermaid svg {
  max-width: none;
  height: auto;
}

.article-body pre.mermaid[data-loading]::after {
  content: 'Loading diagram…';
  display: block;
  color: var(--fg-faint);
  font-size: 0.85rem;
  font-family: var(--font-body);
  padding: var(--sp-md);
  text-align: center;
}

/* Fullscreen overlay */
.mermaid-fullscreen {
  position: fixed;
  inset: 0;
  z-index: 9999;
  background: oklch(0.97 0.005 60 / 0.96);
  backdrop-filter: blur(4px);
  display: flex;
  flex-direction: column;
  cursor: default;
  animation: mermaid-fade-in 0.2s var(--ease-out);
}

@keyframes mermaid-fade-in {
  from { opacity: 0; }
  to   { opacity: 1; }
}

.mermaid-fullscreen__toolbar {
  display: flex;
  align-items: center;
  gap: var(--sp-sm);
  padding: var(--sp-sm) var(--sp-md);
  flex-shrink: 0;
  z-index: 1;
}

.mermaid-toolbar__group {
  display: flex;
  align-items: center;
  gap: 2px;
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 2px;
}

.mermaid-toolbar__btn {
  width: 32px;
  height: 32px;
  border: none;
  border-radius: 3px;
  background: transparent;
  color: var(--fg-muted);
  font-size: 0.9rem;
  font-family: var(--font-body);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: color var(--duration) var(--ease-out),
              background var(--duration) var(--ease-out);
}

.mermaid-toolbar__btn:hover {
  color: var(--fg);
  background: var(--border);
}

.mermaid-toolbar__btn:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: -2px;
}

.mermaid-toolbar__zoom {
  min-width: 42px;
  text-align: center;
  font-size: 0.78rem;
  color: var(--fg-faint);
  font-family: var(--font-code);
  user-select: none;
}

.mermaid-fullscreen__close {
  width: 36px;
  height: 36px;
  margin-left: auto;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--bg-elevated);
  color: var(--fg-muted);
  font-size: 1.1rem;
  line-height: 1;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: color var(--duration) var(--ease-out),
              border-color var(--duration) var(--ease-out);
}

.mermaid-fullscreen__close:hover {
  color: var(--accent);
  border-color: var(--accent);
}

.mermaid-fullscreen__close:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

.mermaid-fullscreen__diagram {
  width: 100%;
  flex: 1;
  overflow: hidden;
  display: flex;
  align-items: center;
  justify-content: center;
  -webkit-overflow-scrolling: touch;
}

.mermaid-fullscreen__diagram svg {
  max-width: none;
  height: auto;
  display: block;
}



/* --- Footer --- */

.site-footer {
  border-top: 1px solid var(--border);
  padding: var(--sp-xl) 0;
  margin-top: var(--sp-3xl);
  font-size: 0.85rem;
  color: var(--fg-faint);
}

.site-footer a {
  color: var(--fg-muted);
  text-decoration: none;
  transition: color var(--duration) var(--ease-out);
}

.site-footer a:hover {
  color: var(--accent);
}

.site-footer a:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 3px;
  border-radius: 2px;
}

/* --- Reduced motion --- */

@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    transition-duration: 0s !important;
  }
}

/* --- Responsive --- */

/* Tablet and below */
@media (max-width: 767px) {
  /* Tighter spacing scale for small screens */
  :root {
    --sp-2xl: 3rem;
    --sp-3xl: 4.5rem;
  }

  html {
    font-size: 15.5px;
  }

  /* Less page edge padding on mobile */
  .page {
    padding-left: var(--sp-md);
    padding-right: var(--sp-md);
  }

  /* --- Header --- */

  .site-header {
    padding-top: var(--sp-2xl);
    padding-bottom: var(--sp-lg);
  }

  .article-header {
    padding-top: var(--sp-xl);
    padding-bottom: var(--sp-lg);
  }

  .article-header__title {
    font-size: clamp(1.3rem, 6vw, 1.8rem);
  }

  /* --- Index list --- */

  .index__link {
    flex-direction: column;
    gap: var(--sp-xs);
  }

  .index__desc {
    max-width: none;
  }

  /* --- Article body --- */

  .article-body h2 {
    font-size: 1.3rem;
    margin-top: var(--sp-xl);
  }

  .article-body h3 {
    font-size: 1.05rem;
    margin-top: var(--sp-lg);
  }

  .article-body blockquote {
    padding-left: var(--sp-md);
    margin-left: 0;
    margin-right: 0;
  }

  .article-body pre {
    padding: var(--sp-sm) 0.75rem;
    font-size: 0.8rem;
  }

  /* Tables: horizontal scroll */
  .table-scroll {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    margin-left: calc(-1 * var(--sp-md));
    margin-right: calc(-1 * var(--sp-md));
    padding: 0 var(--sp-md);
  }

  .table-scroll table {
    min-width: 400px;
  }

  /* --- Footer --- */

  .site-footer {
    margin-top: var(--sp-2xl);
    padding-top: var(--sp-lg);
    padding-bottom: var(--sp-lg);
  }

  /* --- Back link --- */

  .back-link {
    margin-bottom: var(--sp-lg);
  }

  /* --- Mermaid inline --- */

  .article-body pre.mermaid {
    max-width: none;
    margin-left: calc(-1 * var(--sp-md));
    margin-right: calc(-1 * var(--sp-md));
    padding-left: var(--sp-md);
    padding-right: var(--sp-md);
    border-radius: 0;
  }

  /* Fullscreen overlay: larger touch targets */
  .mermaid-toolbar__btn {
    width: 40px;
    height: 40px;
  }

  .mermaid-fullscreen__close {
    width: 44px;
    height: 44px;
  }
}
