@layer reset, base, layout, frame, lightbox;

/* ---------- Schrift ---------- */
@font-face {
  font-family: "TheLudwig";
  font-style: normal;
  font-weight: 300;
  font-display: swap;
  src: url("assets/TheLudwig-alpha-Light.woff2") format("woff2"),
       url("assets/TheLudwig-alpha-Light.woff") format("woff");
}
@font-face {
  font-family: "TheLudwig";
  font-style: normal;
  font-weight: 500;
  font-display: swap;
  src: url("assets/TheLudwig-alpha-Medium.woff2") format("woff2"),
       url("assets/TheLudwig-alpha-Medium.woff") format("woff");
}
@font-face {
  font-family: "TheLudwig";
  font-style: normal;
  font-weight: 600;
  font-display: swap;
  src: url("assets/TheLudwig-alpha-SemiBold.woff2") format("woff2"),
       url("assets/TheLudwig-alpha-SemiBold.woff") format("woff");
}

/* ---------- reset ---------- */
@layer reset {
  *, *::before, *::after {
    box-sizing: border-box;
  }

  :where(body, h1, p, figure) {
    margin: 0;
  }

  :where(img, picture) {
    display: block;
    max-inline-size: 100%;
  }
}

/* ---------- base ---------- */
@layer base {
  :root {
    color-scheme: light dark;
    --wall: light-dark(#cfd0cf, #7f868a);
    --ink: light-dark(#2a2b2d, #15161a);
    --gap: clamp(1.25rem, 3vw, 3rem);
    --page-pad: clamp(1rem, 3vw, 3rem);
    --frame-w: clamp(10px, 1.3vw, 18px);
    /* Schattenfugenrahmen: gemeinsam für Galerie (.work) und Lightbox (.lb-media) */
    --shadow-rest:
      inset 0 1px 0 rgb(255 255 255 / 0.22),
      inset 1px 0 0 rgb(255 255 255 / 0.10),
      inset 0 -1px 0 rgb(0 0 0 / 0.45),
      inset -1px 0 0 rgb(0 0 0 / 0.3),
      1px 2px 2px rgb(0 0 0 / 0.18),
      3px 8px 14px rgb(0 0 0 / 0.22),
      10px 24px 40px -12px rgb(0 0 0 / 0.38);
    --shadow-hover:
      inset 0 1px 0 rgb(255 255 255 / 0.22),
      inset 1px 0 0 rgb(255 255 255 / 0.10),
      inset 0 -1px 0 rgb(0 0 0 / 0.45),
      inset -1px 0 0 rgb(0 0 0 / 0.3),
      1px 3px 3px rgb(0 0 0 / 0.20),
      4px 11px 18px rgb(0 0 0 / 0.24),
      12px 30px 48px -12px rgb(0 0 0 / 0.42);
    --grain: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='260' height='260'%3E%3Cfilter id='n' x='0' y='0' width='100%25' height='100%25'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='.85' numOctaves='3' stitchTiles='stitch'/%3E%3CfeColorMatrix values='0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1.4 -.2'/%3E%3C/filter%3E%3Crect width='100%25' height='100%25' filter='url(%23n)' opacity='.05'/%3E%3C/svg%3E");
  }

  html {
    -webkit-text-size-adjust: none;
    text-size-adjust: none;
  }

  body {
    min-block-size: 100dvh;
    color: var(--ink);
    font-family: "TheLudwig", system-ui, sans-serif;
    font-weight: 300;
    line-height: 1.4;
    /* Galeriewand: Körnung, Vignette (max. 6 %), Telegrau */
    background-color: var(--wall);
    background-image:
      var(--grain),
      radial-gradient(ellipse at 50% 40%, transparent 55%, rgb(0 0 0 / 0.06) 100%);
    background-attachment: scroll, fixed;
  }

  /* Typo-Logik wie in der Lightbox: Titel weiß, Fließtext dunkel */
  h1 {
    font-size: clamp(1.8rem, 4vw, 3rem);
    font-weight: 500;
    letter-spacing: -0.02em;
    line-height: 1;
    color: #fff;
    text-wrap: balance;
  }

  .intro {
    margin-block-start: 0.9rem;
    max-inline-size: 38rem;
    font-size: clamp(1rem, 1.3vw, 1.15rem);
    font-weight: 300;
    line-height: 1.5;
    color: color-mix(in oklch, var(--ink) 75%, black);
    text-wrap: pretty;

    & strong {
      font-weight: 600;
      color: #fff;
    }
  }

  footer {
    padding-block: clamp(2.5rem, 6vw, 5rem) 2rem;
    font-size: 0.8rem;
    font-weight: 300;
    text-align: center;
  }

  :focus-visible {
    outline: 2px solid color-mix(in oklch, var(--ink) 70%, white);
    outline-offset: 3px;
  }
}

/* ---------- layout ---------- */
@layer layout {
  :where(header, main, footer) {
    inline-size: 100%;
    max-inline-size: 1800px;
    margin-inline: auto;
    padding-inline: var(--page-pad);
  }

  header {
    padding-block: clamp(2rem, 6vw, 5rem) clamp(1.5rem, 4vw, 3.5rem);
  }

  .gallery {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(min(100%, 300px), 1fr));
    gap: var(--gap);
    align-items: start;

    /* Nativer Masonry-Entwurf (Chrome/Safari-Syntax) */
    @supports (display: masonry) {
      display: masonry;
      masonry-template-tracks: repeat(auto-fill, minmax(min(100%, 300px), 1fr));
      grid-template-columns: repeat(auto-fill, minmax(min(100%, 300px), 1fr));
    }

    /* Firefox-Syntax */
    @supports (grid-template-rows: masonry) and (not (display: masonry)) {
      grid-template-rows: masonry;
    }

    /* JS-Fallback (gallery.js): Zeilenhöhe 1px, Abstand über margin */
    &.js-masonry {
      grid-auto-rows: 1px;
      row-gap: 0;

      & .work {
        margin-block-end: var(--gap);
      }
    }
  }

  /* Breitformate laufen über zwei Spalten */
  @media (min-width: 700px) {
    .work[data-wide] {
      grid-column: span 2;
      masonry-track: span 2;
    }
  }

  @media (max-width: 699px) {
    :root {
      --frame-w: 10px;
      --page-pad: 1rem;
    }
  }
}

/* ---------- frame ---------- */
@layer frame {
  /* Rahmenleiste: Galerie (.work::before) und Lightbox (.lb-media) */
  .work::before,
  .lb-media {
    border-radius: 2px;
    background-color: var(--frame);
    background-image: linear-gradient(
      135deg,
      oklch(from var(--frame) calc(l + 0.10) c h),
      var(--frame) 35%,
      oklch(from var(--frame) calc(l - 0.08) c h)
    );
    box-shadow: var(--shadow-rest);
  }

  /* Bild in der Fuge: Galerie und Lightbox */
  :is(.work, .lightbox) .canvas {
    position: relative;
    padding: 3px;
    background: #0b0b0c;

    & picture {
      display: block;
    }

    & img {
      display: block;
      inline-size: 100%;
      max-inline-size: 100%;
      block-size: auto;
      aspect-ratio: var(--w) / var(--h);
      image-rendering: auto;
    }

    &::after {
      content: "";
      position: absolute;
      inset: 3px;
      pointer-events: none;
      box-shadow:
        inset 3px 3px 7px rgb(0 0 0 / 0.42),
        inset -1px -1px 2px rgb(255 255 255 / 0.05);
    }
  }

  .work {
    /* Raster: Rahmen (Zeile 1) über dem Bild, Bildunterschrift darunter (Zeile 2) */
    display: grid;
    grid-template-columns: minmax(0, 1fr);

    /* Der Rahmen selbst: liegt hinter dem Bild-Link in derselben Zelle,
       damit die figcaption außerhalb des Rahmens bleibt. */
    &::before {
      content: "";
      grid-area: 1 / 1;
    }

    /* Link ums Bild (ohne JS öffnet er die große Datei) */
    & .open {
      grid-area: 1 / 1;
      display: block;
      color: inherit;
      text-decoration: none;
      cursor: zoom-in;
    }

    & .canvas {
      margin: var(--frame-w);
    }

    & figcaption {
      grid-area: 2 / 1;
      margin-block-start: 0.6rem;
      display: grid;
      gap: 0.1rem;
      color: var(--ink);

      & .title {
        font-size: 1rem;
        font-weight: 500;
      }

      & .technique {
        font-size: 0.85rem;
        font-weight: 300;
        opacity: 0.75;
      }
    }
  }

  @media (hover: hover) and (min-width: 700px) {
    .work:hover {
      translate: 0 -2px;

      &::before {
        box-shadow: var(--shadow-hover);
      }
    }
  }

  @media (hover: hover) and (min-width: 700px) and (prefers-reduced-motion: no-preference) {
    .work {
      transition: translate 300ms ease;

      &::before {
        transition: box-shadow 300ms ease;
      }
    }
  }
}

/* ---------- lightbox ---------- */
@layer lightbox {
  /* Scroll-Lock, solange die Lightbox offen ist */
  :root:has(dialog[open]) {
    overflow: hidden;
  }

  dialog.lightbox {
    --lb-pad: clamp(0.5rem, 1vw, 0.75rem);
    --lb-gap: clamp(0.75rem, 1.5vw, 1.25rem);
    /* Mindestbreite der Textspalte im Layout "side" */
    --lb-text-min: 12rem;
    --lb-close: 2.25rem;
    /* Höhe der Textzeile im Layout "below" */
    --lb-text-h: 5.25rem;
    /* Leiste (2 × --frame-w) plus Fuge (2 × 3px) um das Bild */
    --lb-chrome: calc(2 * var(--frame-w) + 6px);

    inset: 0;
    inline-size: 100vw;
    block-size: 100dvh;
    max-inline-size: none;
    max-block-size: none;
    margin: 0;
    padding: var(--lb-pad);
    border: 0;
    background: transparent;
    color: var(--ink);
    /* vertikales Scrollen bleibt frei, horizontal gehört die Wischgeste dem Script */
    touch-action: pan-y;

    /* display kommt vom UA (none), geöffnet wird es per [open] */
    gap: var(--lb-gap);
    place-items: center;
    place-content: center;

    opacity: 0;
    transition: opacity 250ms ease, overlay 250ms allow-discrete, display 250ms allow-discrete;

    &[open] {
      display: grid;
      opacity: 1;
    }

    @starting-style {
      &[open] {
        opacity: 0;
      }
    }

    &::backdrop {
      /* leicht durchscheinend, damit der Blur die Galerie dahinter noch andeutet */
      background: color-mix(in oklch, color-mix(in oklch, var(--wall) 90%, black) 90%, transparent);
      backdrop-filter: blur(8px);
      opacity: 0;
      transition: opacity 250ms ease, overlay 250ms allow-discrete, display 250ms allow-discrete;
    }

    &[open]::backdrop {
      opacity: 1;
    }

    @starting-style {
      &[open]::backdrop {
        opacity: 0;
      }
    }

    /* Text links, Bild am rechten Browserrand (Bild relativ schmaler als das Fenster).
       Textcontainer so hoch wie das Bild: × oben rechts, Titel mittig, Vor/Zurück unten. */
    &[data-layout="side"] {
      grid-template-columns: minmax(var(--lb-text-min), 1fr) auto;
      grid-template-areas: "text media";
      align-content: center;
      --lb-max-w: calc(100vw - 2 * var(--lb-pad) - var(--lb-text-min) - var(--lb-gap) - var(--lb-chrome));
      --lb-max-h: calc(100dvh - 2 * var(--lb-pad) - var(--lb-chrome));

      & .lb-media {
        justify-self: end;
      }

      & .lb-text {
        display: grid;
        grid-template-rows: auto 1fr auto;
        grid-template-areas: "close" "caption" "nav";
        align-self: stretch;
        justify-self: stretch;
        text-align: start;
      }

      & .lb-caption {
        align-self: center;
        max-inline-size: 28rem;
      }

      & .lb-close {
        justify-self: end;
      }

      & .lb-nav {
        align-self: end;
      }
    }

    /* Bild am oberen Browserrand, Text darunter, so breit wie das Bild:
       links Titel + Technik, rechts × oben und Vor/Zurück unten. Reserve = Abstand + Textblock (--lb-text-h) */
    &[data-layout="below"] {
      grid-template-rows: auto auto;
      grid-template-areas: "media" "text";
      align-content: start;
      --lb-max-w: calc(100vw - 2 * var(--lb-pad) - var(--lb-chrome));
      --lb-max-h: calc(100dvh - 2 * var(--lb-pad) - var(--lb-chrome) - var(--lb-gap) - var(--lb-text-h));

      & .lb-text {
        display: grid;
        grid-template-columns: minmax(0, 1fr) auto;
        grid-template-rows: auto auto;
        grid-template-areas:
          "caption close"
          "caption nav";
        column-gap: 1rem;
        row-gap: 0.5rem;
        /* genau so breit wie das Bild: trägt selbst nichts zur Spaltenbreite bei, füllt sie dann aus */
        inline-size: 0;
        min-inline-size: 100%;
        text-align: start;
      }

      & .lb-caption {
        grid-area: caption;
        align-self: center;
      }

      & .lb-title {
        font-size: clamp(1.1rem, 2vw, 1.6rem);
      }

      & .lb-technique {
        margin-block-start: 0.1rem;
      }

      & .lb-close {
        grid-area: close;
        justify-self: end;
        align-self: start;
      }

      & .lb-nav {
        grid-area: nav;
        justify-self: end;
        align-self: end;
      }
    }
  }

  .lb-media {
    grid-area: media;
    padding: var(--frame-w);

    & .canvas {
      inline-size: fit-content;
    }

    /* Größe ohne Verzerrung und nie über das Original (--w/--h in CSS-px) hinaus:
       Breite = min(Platz, Platz in der Höhe umgerechnet, Originalbreite), Höhe folgt aus aspect-ratio. */
    & img {
      inline-size: min(
        var(--lb-max-w),
        calc(var(--lb-max-h) * var(--w) / var(--h)),
        calc(var(--w) * 1px)
      );
      max-inline-size: none;
      block-size: auto;
    }
  }

  .lb-text {
    grid-area: text;
  }

  .lb-title {
    font-size: clamp(1.4rem, 3vw, 2.4rem);
    font-weight: 500;
    line-height: 1.1;
    color: #fff;
    text-wrap: balance;
  }

  .lb-technique {
    margin-block-start: 0.4rem;
    font-size: clamp(0.95rem, 1.4vw, 1.15rem);
    font-weight: 300;
    color: color-mix(in oklch, var(--ink) 75%, black);
  }

  /* Zeile mit Zurück, Zähler, Weiter */
  .lb-nav {
    display: flex;
    align-items: center;
    gap: 0.5rem;
  }

  .lb-counter {
    min-inline-size: 4.5ch;
    text-align: center;
    font-weight: 300;
    font-variant-numeric: tabular-nums;
    opacity: 0.7;
  }

  .lb-close,
  .lb-prev,
  .lb-next {
    position: static;
    display: inline-grid;
    place-items: center;
    inline-size: var(--lb-close);
    block-size: var(--lb-close);
    padding: 0;
    border: 0;
    border-radius: 50%;
    background: color-mix(in oklch, var(--ink) 12%, transparent);
    color: inherit;
    font-family: "TheLudwig", system-ui, sans-serif;
    font-size: 1.6rem;
    font-weight: 300;
    line-height: 1;
    cursor: pointer;
  }

  .lb-prev,
  .lb-next {
    font-size: 1.8rem;
  }

  @media (max-width: 699px) {
    dialog.lightbox {
      --lb-pad: 0.5rem;
    }
  }

  /* unter 500 px keine Lightbox: Link ohne Wirkung, Hinweis im Intro ausgeblendet */
  @media (max-width: 499px) {
    a.open {
      pointer-events: none;
      cursor: default;
    }

    .intro strong {
      display: none;
    }
  }

  @media (prefers-reduced-motion: reduce) {
    dialog.lightbox,
    dialog.lightbox::backdrop {
      transition: none;
    }
  }
}
