/* Layout stylesheet (developer-owned static asset).
   Consumes theme variables from themes/*.css. Penscratch-inspired: a centered
   white content card on a dark page background, serif type, centered ruled
   header. Tune colours/fonts via themes/*.css. */

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

/* --- Base --- */
body {
  margin: 0;
  font-family: var(--font-serif);
  font-size: 17px;
  line-height: 1.65;
  background: var(--color-page);
  color: var(--color-text);
}

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

img {
  max-width: 100%;
  height: auto;
}

h1,
h2,
h3,
h4 {
  font-family: var(--font-serif);
  line-height: 1.25;
  margin: 1.4em 0 0.5em;
  font-weight: 600;
}

h1 {
  font-size: 1.9rem;
}
h2 {
  font-size: 1.45rem;
}
h3 {
  font-size: 1.2rem;
}

code {
  font-family: var(--font-mono);
}

:not(pre) > code {
  background: var(--color-surface);
  padding: 0.1em 0.35em;
  border-radius: 4px;
  font-size: 0.9em;
}

pre.shiki {
  padding: calc(var(--space) * 1.5);
  overflow-x: auto;
  border-radius: var(--radius);
  font-size: 0.9rem;
  line-height: 1.5;
}

blockquote {
  margin: 1.2em 0;
  padding: 0 0 0 calc(var(--space) * 2);
  border-left: 3px solid var(--color-border);
  color: var(--color-muted);
}

/* --- Content card --- */
.site {
  width: 100%;
  max-width: 820px;
  margin: 0 auto;
  background: var(--color-bg);
  padding: 0 calc(var(--space) * 4) calc(var(--space) * 5);
}

/* --- Header (centered) --- */
.site-header {
  text-align: center;
  padding-top: calc(var(--space) * 5);
}

.site-title {
  display: inline-block;
  font-family: var(--font-serif);
  font-size: 2rem;
  color: var(--color-secondary);
  text-decoration: none;
}

.site-tagline {
  margin: var(--space) 0 0;
  color: var(--color-muted);
}

.site-nav {
  margin: calc(var(--space) * 3) 0 calc(var(--space) * 2);
  padding: var(--space) 0;
  border-top: 1px solid var(--color-border);
  border-bottom: 1px solid var(--color-border);
}

.nav-list {
  list-style: none;
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: calc(var(--space) * 3);
  margin: 0;
  padding: 0;
}

.nav-list .nav-list {
  margin-top: var(--space);
  gap: var(--space);
}

.nav-list a {
  font-family: var(--font-sans);
  text-transform: uppercase;
  letter-spacing: 0.12em;
  font-size: 0.72rem;
  text-decoration: none;
  color: var(--color-muted);
}

.nav-list a:hover,
.nav-list a.is-current,
.nav-item.is-active > a {
  color: var(--color-primary);
}

.nav-toggle {
  display: none;
  background: none;
  border: 1px solid var(--color-border);
  border-radius: var(--radius);
  color: var(--color-text);
  font-family: var(--font-sans);
  font-size: 0.85rem;
  padding: calc(var(--space) * 0.75) calc(var(--space) * 1.5);
  cursor: pointer;
}

/* --- Footer controls (search + theme; secondary, seamless) --- */
.footer-controls {
  display: flex;
  justify-content: center;
  align-items: center;
  flex-wrap: wrap;
  gap: calc(var(--space) * 3);
  margin-bottom: calc(var(--space) * 2);
}

.footer-search {
  position: relative;
}

.footer-search .search-input {
  width: 180px;
  padding: calc(var(--space) * 0.5) 0;
  background: transparent;
  border: none;
  border-bottom: 1px solid var(--color-border);
  border-radius: 0;
  color: var(--color-text);
  font-family: var(--font-serif);
  font-size: 0.9rem;
}

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

.footer-search .search-input:focus {
  outline: none;
  border-bottom-color: var(--color-primary);
}

/* Open results upward — the footer sits at the bottom of the page. */
.footer-search .search-results {
  top: auto;
  bottom: 100%;
  margin: 0 0 var(--space);
}

/* The label + select read as one underlined control, mirroring the search. */
.theme-field {
  display: inline-flex;
  align-items: center;
  gap: 0.5em;
  padding: calc(var(--space) * 0.5) 0;
  border-bottom: 1px solid var(--color-border);
  font-family: var(--font-sans);
  font-size: 0.72rem;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--color-muted);
  cursor: pointer;
}

.theme-field:hover,
.theme-field:focus-within {
  border-bottom-color: var(--color-primary);
  color: var(--color-primary);
}

.theme-select {
  appearance: none;
  -webkit-appearance: none;
  padding: 0 1.1em 0 0;
  background:
    transparent
    url('data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg" width="10" height="6"><path d="M0 0l5 6 5-6z" fill="%238c8c8c"/></svg>')
    no-repeat right center;
  border: none;
  color: inherit;
  font: inherit;
  letter-spacing: inherit;
  cursor: pointer;
}

/* Caret follows the accent colour on hover/focus. */
.theme-field:hover .theme-select,
.theme-field:focus-within .theme-select {
  background-image: url('data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg" width="10" height="6"><path d="M0 0l5 6 5-6z" fill="%232a7f7f"/></svg>');
}

/* --- Footer social icons --- */
.footer-social {
  display: flex;
  justify-content: center;
  gap: calc(var(--space) * 2);
  margin-bottom: calc(var(--space) * 1.5);
}

.social-link {
  display: inline-flex;
  color: var(--color-muted);
}

.social-link:hover {
  color: var(--color-primary);
}

.social-link svg {
  width: 20px;
  height: 20px;
  fill: currentColor;
}

.footer-credit {
  margin: 0;
}

.footer-credit a {
  color: var(--color-primary);
  text-decoration: none;
}

/* --- NSFW toggle (gallery pages only) --- */
.gallery-nsfw {
  margin: calc(var(--space) * 2) 0;
  text-align: right;
}

.nsfw-toggle {
  background: transparent;
  border: 1px solid var(--color-border);
  border-radius: 999px;
  color: var(--color-muted);
  font-family: var(--font-sans);
  font-size: 0.72rem;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  padding: calc(var(--space) * 0.5) calc(var(--space) * 1.5);
  cursor: pointer;
}

.nsfw-toggle:hover,
.nsfw-toggle[aria-pressed="true"] {
  color: var(--color-primary);
  border-color: var(--color-primary);
}

/* --- Search results dropdown --- */
.search-results {
  position: absolute;
  top: 100%;
  left: 50%;
  transform: translateX(-50%);
  z-index: 900;
  width: min(360px, 90vw);
  margin-top: var(--space);
  text-align: left;
  background: var(--color-bg);
  border: 1px solid var(--color-border);
  border-radius: var(--radius);
  max-height: 60vh;
  overflow-y: auto;
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.15);
}

.search-results ul {
  list-style: none;
  margin: 0;
  padding: 0;
}

.search-result {
  display: block;
  padding: var(--space) calc(var(--space) * 1.5);
  text-decoration: none;
  color: var(--color-text);
  border-bottom: 1px solid var(--color-border);
}

.search-result-title {
  display: block;
  font-weight: 600;
}

.search-result-excerpt {
  display: block;
  font-size: 0.85rem;
  color: var(--color-muted);
}

.search-empty {
  margin: 0;
  padding: var(--space) calc(var(--space) * 1.5);
  color: var(--color-muted);
}

/* --- Banner --- */
.site-banner {
  height: 200px;
  margin-bottom: calc(var(--space) * 2);
  background-color: var(--color-surface);
  background-size: cover;
  background-position: center;
}

/* --- Page layout --- */
.layout {
  display: grid;
  gap: calc(var(--space) * 4);
  grid-template-columns: minmax(0, 1fr);
  padding-top: calc(var(--space) * 2);
}

@media (min-width: 900px) {
  .site:has(.sidebar) {
    max-width: 1080px;
  }

  .layout:has(.sidebar) {
    grid-template-columns: minmax(0, 1fr) 220px;
  }

  .sidebar {
    position: sticky;
    top: calc(var(--space) * 2);
    align-self: start;
  }
}

.home-title,
.list-title {
  margin-top: 0;
}

/* --- Article / listing cards (vertical) --- */
.article-list {
  display: flex;
  flex-direction: column;
  gap: calc(var(--space) * 4);
}

.article-card {
  display: flex;
  flex-direction: column;
  gap: var(--space);
  padding-bottom: calc(var(--space) * 3);
  border-bottom: 1px solid var(--color-border);
}

.card-image img {
  display: block;
  width: 100%;
  max-height: 360px;
  object-fit: cover;
  border-radius: var(--radius);
}

.card-title {
  margin: 0;
  font-size: 1.4rem;
}

.card-title a {
  color: var(--color-primary);
  text-decoration: none;
}

.card-meta {
  margin: 0;
  font-size: 0.8rem;
  color: var(--color-muted);
}

.card-kind {
  margin-right: var(--space);
  text-transform: uppercase;
  letter-spacing: 0.05em;
}

.card-excerpt {
  margin: 0;
}

.card-more {
  font-weight: 600;
  text-decoration: none;
}

/* --- Article / page body --- */
.article-header h1 {
  margin-top: 0;
}

.article-meta,
.gallery-meta {
  color: var(--color-muted);
  font-size: 0.85rem;
}

.article-featured,
.gallery-cover {
  display: block;
  width: 100%;
  max-height: 420px;
  object-fit: cover;
  border-radius: var(--radius);
  margin: var(--space) 0;
}

.article-tags {
  display: flex;
  flex-wrap: wrap;
  gap: var(--space);
  margin-top: calc(var(--space) * 3);
}

.tag {
  font-size: 0.85rem;
  text-decoration: none;
  color: var(--color-muted);
}

.two-column {
  display: flex;
  gap: calc(var(--space) * 3);
}

.two-column .column {
  flex: 1;
  min-width: 0;
}

/* --- Table of contents --- */
.toc-title {
  margin: 0 0 var(--space);
  font-family: var(--font-sans);
  font-size: 0.75rem;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--color-muted);
}

.toc-list {
  list-style: none;
  margin: 0;
  padding-left: calc(var(--space) * 1.5);
  font-size: 0.9rem;
}

.toc-list .toc-list {
  padding-left: var(--space);
}

.toc-list a {
  text-decoration: none;
  color: var(--color-muted);
}

.toc-list a:hover {
  color: var(--color-primary);
}

/* --- Gallery detail layouts --- */
/* Shared tile styling. Each layout below overrides image sizing as needed. */
.gallery-item {
  margin: 0;
}

.gallery-item img {
  display: block;
  width: 100%;
  border-radius: var(--radius);
  cursor: pointer;
}

/* Grid: uniform cropped squares (the original look). */
.gallery-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(180px, 1fr));
  gap: var(--space);
  margin-top: calc(var(--space) * 2);
}

.gallery-grid .gallery-item img {
  aspect-ratio: 1 / 1;
  object-fit: cover;
}

/* Justified: full-width rows sharing a height; widths track aspect ratio.
   flex-grow (set inline, ∝ aspect) over a zero basis distributes each row's
   width so all tiles in a row land at the same height — responsively. */
.gallery-justified {
  margin-top: calc(var(--space) * 2);
}

.gallery-row {
  display: flex;
  gap: var(--space);
  margin-bottom: var(--space);
}

.gallery-row > .gallery-cell,
.gallery-row > .gallery-stack {
  flex-basis: 0;
  flex-shrink: 1;
  min-width: 0;
}

/* Stacked cell: two tiles sharing a column inside a justified row. */
.gallery-stack {
  display: flex;
  flex-direction: column;
  gap: var(--space);
}

/* Trailing under-filled row: render at the target height, left-aligned,
   instead of stretching a lone tile across the full width. */
.gallery-row.is-partial {
  flex-wrap: wrap;
}

.gallery-row.is-partial > .gallery-cell,
.gallery-row.is-partial > .gallery-stack {
  flex-grow: 0 !important;
  flex-basis: auto;
  height: var(--row-h);
}

.gallery-row.is-partial .gallery-item img {
  height: var(--row-h);
  width: auto;
}

.gallery-row.is-partial .gallery-stack .gallery-item img {
  height: auto;
}

/* Masonry: CSS multi-column, natural aspect ratios, no cropping. Reading order
   runs down each column then to the next (matches the lightbox sequence loosely). */
.gallery-masonry {
  column-count: var(--columns, 3);
  column-gap: var(--space);
  margin-top: calc(var(--space) * 2);
}

.gallery-masonry .gallery-item {
  break-inside: avoid;
  margin-bottom: var(--space);
}

/* Mosaic: gapless fixed-column grid. Tiles vary in width (col span, set inline)
   but every row is packed to exactly --columns, so the grid is a perfect, hole-
   free rectangle. Rows are one track tall (clamped, ~square) and cropped. */
.gallery-mosaic {
  display: grid;
  grid-template-columns: repeat(var(--columns, 4), 1fr);
  grid-auto-rows: clamp(110px, 20vw, 210px);
  gap: var(--space);
  margin-top: calc(var(--space) * 2);
}

.gallery-mosaic-cell,
.gallery-mosaic .gallery-item,
.gallery-mosaic .gallery-item a,
.gallery-mosaic .gallery-item img {
  height: 100%;
}

.gallery-mosaic .gallery-item img {
  object-fit: cover;
}

/* --- Gallery index (/gallery/) --- */
.gallery-index {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
  gap: calc(var(--space) * 3) calc(var(--space) * 2);
  margin-top: calc(var(--space) * 2);
}

.gallery-card-image img {
  display: block;
  width: 100%;
  aspect-ratio: 1 / 1;
  object-fit: cover;
  border-radius: var(--radius);
}

.gallery-card-title {
  margin: var(--space) 0 calc(var(--space) * 0.5);
  font-size: 0.95rem;
}

.gallery-card-title a {
  color: var(--color-primary);
  text-decoration: none;
}

.gallery-card-meta {
  margin: 0;
  font-size: 0.8rem;
  color: var(--color-muted);
}

/* --- Pagination --- */
.pagination {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  align-items: center;
  gap: var(--space);
  margin: calc(var(--space) * 5) 0 0;
}

.pagination .page-numbers {
  display: flex;
  flex-wrap: wrap;
  gap: calc(var(--space) * 0.5);
  list-style: none;
  margin: 0;
  padding: 0;
}

.page-link {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.25em;
  min-width: 2.4em;
  height: 2.4em;
  padding: 0 0.7em;
  border: 1px solid var(--color-border);
  border-radius: 6px;
  color: var(--color-muted);
  font-family: var(--font-sans);
  font-size: 0.8rem;
  text-decoration: none;
  transition:
    color 0.15s,
    border-color 0.15s,
    background-color 0.15s;
}

a.page-link:hover {
  color: var(--color-primary);
  border-color: var(--color-primary);
}

.page-link.is-current {
  background: var(--color-primary);
  border-color: var(--color-primary);
  color: #fff;
  font-weight: 600;
}

.page-arrow {
  font-size: 1.1em;
  line-height: 1;
}

.page-ellipsis {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 2.4em;
  height: 2.4em;
  color: var(--color-muted);
}

@media (max-width: 480px) {
  .page-edge-label {
    display: none;
  }
}

/* --- Footer --- */
.site-footer {
  margin-top: calc(var(--space) * 5);
  padding-top: calc(var(--space) * 3);
  border-top: 1px solid var(--color-border);
  color: var(--color-muted);
  font-size: 0.8rem;
  text-align: center;
}

/* --- NSFW (blurred by default; revealed only when opted in) --- */
.is-nsfw img,
.lightbox-image.is-nsfw {
  filter: blur(24px) brightness(0.85);
  transition: filter 0.15s ease;
}

:root[data-nsfw="show"] .is-nsfw img,
:root[data-nsfw="show"] .lightbox-image.is-nsfw {
  filter: none;
}

/* --- Footnotes --- */
.footnote-popover {
  position: fixed;
  z-index: 1100;
  max-width: 320px;
  padding: var(--space) calc(var(--space) * 1.5);
  background: var(--color-bg);
  color: var(--color-text);
  border: 1px solid var(--color-border);
  border-radius: var(--radius);
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.2);
  font-size: 0.9rem;
}

.footnote-content p {
  margin: 0;
}

.footnote-close {
  display: none;
}

.footnote-backdrop {
  position: fixed;
  inset: 0;
  z-index: 1099;
  background: rgba(0, 0, 0, 0.4);
}

.footnote-popover.is-sheet {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  top: auto;
  width: 100%;
  max-width: none;
  border: none;
  border-top: 1px solid var(--color-border);
  border-radius: var(--radius) var(--radius) 0 0;
  box-shadow: 0 -4px 20px rgba(0, 0, 0, 0.3);
  padding: calc(var(--space) * 2) calc(var(--space) * 2) calc(var(--space) * 4);
  font-size: 1rem;
  animation: footnote-slide-up 0.15s ease-out;
}

.footnote-popover.is-sheet .footnote-close {
  display: block;
  position: absolute;
  top: var(--space);
  right: var(--space);
  background: none;
  border: none;
  font-size: 1.5rem;
  line-height: 1;
  color: var(--color-text);
  cursor: pointer;
}

@keyframes footnote-slide-up {
  from {
    transform: translateY(100%);
  }
  to {
    transform: translateY(0);
  }
}

/* --- Lightbox --- */
.lightbox {
  position: fixed;
  inset: 0;
  z-index: 1000;
  display: none;
  align-items: center;
  justify-content: center;
  background: rgba(0, 0, 0, 0.9);
  touch-action: pan-y;
}

.lightbox.is-open {
  display: flex;
}

.lightbox-image {
  max-width: 92vw;
  max-height: 88vh;
  object-fit: contain;
}

.lightbox-close,
.lightbox-prev,
.lightbox-next {
  position: absolute;
  /* Above the image: an NSFW-blurred image's filter creates a stacking context
     that would otherwise paint over the earlier-in-DOM prev button. */
  z-index: 2;
  background: rgba(0, 0, 0, 0.4);
  color: #fff;
  border: none;
  cursor: pointer;
  font-size: 2rem;
  line-height: 1;
  padding: calc(var(--space) * 1.5);
  border-radius: var(--radius);
}

.lightbox-close {
  top: var(--space);
  right: var(--space);
}

.lightbox-prev {
  left: var(--space);
  top: 50%;
  transform: translateY(-50%);
}

.lightbox-next {
  right: var(--space);
  top: 50%;
  transform: translateY(-50%);
}

.lightbox-counter {
  position: absolute;
  bottom: var(--space);
  left: 50%;
  transform: translateX(-50%);
  color: #fff;
  font-size: 0.9rem;
}

/* --- Tablet --- */
@media (max-width: 700px) {
  .two-column {
    flex-direction: column;
    gap: var(--space);
  }
}

/* --- Mobile --- */
@media (max-width: 640px) {
  body {
    font-size: 16px;
  }

  .site {
    padding: 0 calc(var(--space) * 2) calc(var(--space) * 4);
  }

  .site-banner {
    height: 140px;
  }

  h1 {
    font-size: 1.6rem;
  }

  .card-title {
    font-size: 1.25rem;
  }

  /* Collapsible primary nav. */
  .nav-toggle {
    display: inline-block;
  }

  .site-nav-list {
    display: none;
    margin-top: var(--space);
  }

  .site-nav-list.is-open {
    display: block;
  }

  .nav-list {
    flex-direction: column;
    gap: var(--space);
  }

  .gallery-grid {
    grid-template-columns: repeat(auto-fill, minmax(120px, 1fr));
  }

  /* Narrow screens: fewer masonry columns. The mosaic column count is kept as
     authored — its rows are packed to that exact count, so re-columning here
     would reintroduce gaps. Only the tile height is reduced. */
  .gallery-masonry {
    column-count: 2;
  }

  .gallery-mosaic {
    grid-auto-rows: clamp(80px, 22vw, 160px);
  }
}
