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

:root {
  --hh: 48px;
  --bg: #f5f4f0;
  --fg: #111111;
  --border: rgba(0, 0, 0, 0.09);
  --ease: cubic-bezier(0.76, 0, 0.24, 1);
  --dur: 0.3s;
  --strip: 110px; /* strip thickness */
  /* Open-study frame is the white gutter (GAP_OPEN in script.js) */
  --stroke-thin: 2px;                        /* white frame around sub-content */
  --stroke-thin-color: #fff;
}

html, body {
  height: 100%;
  overflow: hidden;
  background: var(--bg);
  color: var(--fg);
  font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
}

body { display: flex; flex-direction: column; }

/* ── Header ─────────────────────────────────────────────── */

header {
  height: var(--hh);
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 22px;
  border-bottom: 1px solid var(--border);
  z-index: 20;
}

.name {
  font-size: 11px;
  font-weight: 500;
  letter-spacing: 0.1em;
  text-transform: uppercase;
}

.mail {
  display: flex; align-items: center; justify-content: center;
  width: 30px; height: 30px; border-radius: 50%;
  color: var(--fg); text-decoration: none;
  opacity: 0.4;
  transition: opacity 0.15s, background 0.15s;
}
.mail:hover { opacity: 1; background: rgba(0,0,0,0.06); }

/* ── Stage ───────────────────────────────────────────────── */

main { flex: 1; overflow: hidden; }

.stage {
  position: relative;
  width: 100%;
  height: calc(100vh - var(--hh));
  background: #fff;            /* shows through the gutters as white frames */
}

/* ── Study cell ──────────────────────────────────────────── */

.study {
  position: absolute;
  overflow: hidden;
  cursor: pointer;
  /* White frame comes from the gutter between cells (the white .stage showing
     through), so it stays visible even over light gallery images. */
  transition:
    left var(--dur) var(--ease),
    top var(--dur) var(--ease),
    width var(--dur) var(--ease),
    height var(--dur) var(--ease);
}

/* Main image */
.study-bg {
  position: absolute;
  inset: 0;
  background-size: cover;
  background-position: center;
  transition: transform var(--dur) var(--ease);
  will-change: transform;
}
.study:not(.is-open):hover .study-bg { transform: scale(1.03); }

/* Opened panel becomes a vertical stack: image (fills) / info / roulette.
   The image shrinks to fill whatever space the chrome leaves — the WHOLE
   artwork shows, with no overlap and no empty voids. */
.study.is-open {
  display: flex;
  flex-direction: column;
  background: #fff;     /* shows through the gaps as the white frame */
  gap: 10px;            /* uniform white border between image / info / roulette (GAP_FLEX) */
}
.study.is-open .study-bg {
  position: relative; inset: auto;
  order: 0;
  flex: 1 1 auto;
  min-height: 0;
  background-size: contain;
  background-repeat: no-repeat;
  background-position: center;
  transform: none !important;
}
.study.is-open .study-overlay,
.study.is-open .study-label { display: none; }
.study.is-open .study-sleeve {
  position: relative !important; inset: auto !important; bottom: auto !important;
  order: 1;
  flex: 0 0 auto;
  transform: none !important; opacity: 1 !important;
  max-height: 26%;
  padding: 8px 14px 9px;       /* compact so the window hugs the image */
}
/* Keep the info tight: title + medium, description capped to a few lines */
.study.is-open .sleeve-desc {
  display: -webkit-box; -webkit-box-orient: vertical; -webkit-line-clamp: 3;
  overflow: hidden;
}
.study.is-open .study-strip {
  position: relative !important; inset: auto !important;
  order: 2;
  flex: 0 0 auto;
  flex-direction: row !important;
  width: 100% !important;
  height: clamp(42px, 10%, 78px) !important;    /* thinner roulette */
  transform: none !important; opacity: 1 !important;
}
.study.is-open .study-strip-img + .study-strip-img {
  border-left: var(--stroke-thin) solid var(--stroke-thin-color) !important;
  border-top: 0 !important;
}

/* Small secondary open panel: FILL (cover) with the same frame weight instead of
   fitting a tiny image — drops the cramped info/roulette and shows a title
   overlay, so it reads as a balanced framed tile. */
.study.is-open.is-fill { gap: 0; }
.study.is-open.is-fill .study-bg { background-size: cover !important; }
.study.is-open.is-fill .study-sleeve,
.study.is-open.is-fill .study-strip { display: none !important; }
.study.is-open.is-fill .study-overlay { display: block; opacity: 0.5; }
.study.is-open.is-fill .study-label { display: flex; opacity: 1; }
/* Title scales with the tile (never bigger than a main panel's) */
.study.is-open.is-fill .study-title { font-size: max(11px, calc(1rem * var(--text-scale, 1))); }
.study.is-open.is-fill .study-sub   { font-size: max(8px, calc(9px * var(--text-scale, 1))); }
/* A small tile only needs a close affordance */
.study.is-open.is-fill .study-fs,
.study.is-open.is-fill .study-move { display: none; }

/* ── Scrollable tile grid (crowded state) ─────────────────────────────── */
.tile-scroll {
  position: absolute;
  z-index: 1;
  overflow-y: auto;
  overflow-x: hidden;
  display: grid;
  gap: 8px;
  align-content: start;
  padding: 8px;
  background: #fff;
  overscroll-behavior: contain;
}
.tile-scroll::-webkit-scrollbar { width: 8px; }
.tile-scroll::-webkit-scrollbar-thumb { background: rgba(0,0,0,0.18); border-radius: 4px; }

.study.tiled {
  position: relative !important;
  left: auto !important; top: auto !important;
  width: auto !important; height: auto !important;
  display: block !important;
  aspect-ratio: 4 / 3;
  overflow: hidden;
  cursor: pointer;
}
.study.tiled .study-bg {
  position: absolute !important; inset: 0 !important;
  background-size: cover !important; transform: none !important;
}
.study.tiled .study-overlay { display: block; position: absolute; inset: 0; opacity: 0.5; }
.study.tiled .study-sleeve,
.study.tiled .study-strip,
.study.tiled .study-fs,
.study.tiled .study-move { display: none !important; }
.study.tiled .study-label { display: flex; opacity: 1; }
.study.tiled .study-title { font-size: 12px; }
.study.tiled .study-sub { font-size: 8px; }

/* Hover dimmer on grid cards (closed only) */
.study::after {
  content: "";
  position: absolute;
  inset: 0;
  z-index: 2;
  background: rgba(0, 0, 0, 0);
  transition: background var(--dur) var(--ease);
  pointer-events: none;
}
.study:not(.is-open):hover::after { background: rgba(0, 0, 0, 0.32); }
.study.is-open::after { display: none; }

/* Gradient overlay */
.study-overlay {
  position: absolute;
  inset: 0;
  background: linear-gradient(to top,
    rgba(0,0,0,0.72) 0%,
    rgba(0,0,0,0.25) 40%,
    rgba(0,0,0,0.03) 100%);
  transition: opacity var(--dur);
}
.study.is-open .study-overlay { opacity: 0; }   /* no dark gradient over the mat */

/* ── Closed-state label ──────────────────────────────────── */

.study-label {
  position: absolute;
  bottom: 0; left: 0; right: 0;
  padding: 14px;
  color: #fff;
  display: flex;
  flex-direction: column;
  gap: 3px;
  transition: opacity var(--dur);
  pointer-events: none;
  z-index: 6;                 /* stay above the hover dimmer */
}
.study.is-open .study-label { opacity: 0; }

/* Guardrail: drop titles from grid cards when many studies are open */
.stage.is-dense .study:not(.is-open) .study-label { opacity: 0; }

/* Guardrail: null the title text on any card rendered too small to read it */
.study.is-tiny .study-label { opacity: 0; }
.study.is-tiny .study-sleeve { display: none; }

/* Guardrail: small / tall-thin grid previews drop their label and read as pure
   thumbnails (computed in place() via LABEL_MIN_*). */
.study.label-min:not(.is-open) .study-label { opacity: 0; }

.study-num { display: none; }
.study-title {
  font-size: 1rem; font-weight: 400; line-height: 1.2;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.study-sub {
  font-size: 9px; letter-spacing: 0.09em; text-transform: uppercase;
  opacity: 0.5; white-space: nowrap; overflow: hidden;
}

/* ── Image strip ─────────────────────────────────────────── */

.study-strip {
  position: absolute;
  z-index: 4;
  display: flex;
  overflow: hidden;
  transition: transform var(--dur) var(--ease);
  transition-delay: 0.06s;
}

/* Horizontal (top / bottom) — 18% of panel height */
.study-strip[data-side="top"] {
  flex-direction: row;
  top: 0; left: 0; right: 0;
  height: 18%;
  transform: translateY(-100%);
}
.study-strip[data-side="bottom"] {
  flex-direction: row;
  bottom: 0; left: 0; right: 0;
  height: 18%;
  transform: translateY(100%);
}

/* Vertical (left / right) — 18% of panel width */
.study-strip[data-side="left"] {
  flex-direction: column;
  left: 0; top: 0; bottom: 0;
  width: 18%;
  transform: translateX(-100%);
}
.study-strip[data-side="right"] {
  flex-direction: column;
  right: 0; top: 0; bottom: 0;
  width: 18%;
  transform: translateX(100%);
}

/* Slide in when open */
.study.is-open .study-strip { transform: translate(0, 0); }

.study-strip-img {
  flex: 1;
  min-width: 0; min-height: 0;
  background-size: cover;
  background-position: center;
  transition: opacity 0.15s, transform 0.2s var(--ease);
}
/* Strip thumbnails are clickable (swap into main) once the study is open */
.study.is-open .study-strip-img { cursor: pointer; }
.study.is-open .study-strip-img:hover { opacity: 0.82; }

/* Quick fade when a thumbnail is swapped into the main image */
.study-bg.swap { animation: bg-swap 0.3s var(--ease); }
@keyframes bg-swap {
  0%   { opacity: 0.35; }
  100% { opacity: 1; }
}
/* Hairline dividers between strip images */
.study-strip[data-side="top"] .study-strip-img + .study-strip-img,
.study-strip[data-side="bottom"] .study-strip-img + .study-strip-img {
  border-left: var(--stroke-thin) solid var(--stroke-thin-color);
}
.study-strip[data-side="left"] .study-strip-img + .study-strip-img,
.study-strip[data-side="right"] .study-strip-img + .study-strip-img {
  border-top: var(--stroke-thin) solid var(--stroke-thin-color);
}

/* White frame fully around the strip sub-content so the small thumbnails read
   as separate from the main image. */
.study-strip {
  box-shadow:
    inset 0 0 0 var(--stroke-thin) #fff,
    0 0 0 2px #fff;
}

/* ── Info sleeve ─────────────────────────────────────────── */

.study-sleeve {
  position: absolute;
  bottom: 0; left: 0; right: 0;
  z-index: 5;
  background: #fff;
  padding: 10px 14px 12px;
  transform: translateY(100%);
  opacity: 0;
  transition: transform var(--dur) var(--ease), opacity var(--dur);
  transition-delay: 0.04s;
  /* Cap the sleeve so it fits in the reserved chrome band below the image;
     longer descriptions scroll within the sleeve. */
  max-height: 22%;
  overflow-y: auto;
  overscroll-behavior: contain;
  -webkit-overflow-scrolling: touch;
  /* White stroke fully around the sleeve sub-content */
  box-shadow: inset 0 0 0 var(--stroke-thin) #fff, 0 0 0 2px #fff;
}
/* Slim scrollbar for the sleeve */
.study-sleeve::-webkit-scrollbar { width: 5px; }
.study-sleeve::-webkit-scrollbar-thumb {
  background: rgba(0,0,0,0.16); border-radius: 3px;
}

/* When strip is at bottom, sleeve sits above it (18% = strip height) */
.study[data-strip="bottom"] .study-sleeve {
  bottom: 18%;
}

/* Stacked mode (mobile/portrait): the thumbnail "roulette" always runs full
   width along the bottom, so the main image keeps the entire width.
   (.stage.is-mobile is toggled by stackBands() in script.js.) */
.stage.is-mobile .study-strip {
  flex-direction: row;
  inset: auto 0 0 0;            /* bottom edge, full width */
  width: auto; height: 18%;
  transform: translateY(100%);  /* slides up from the bottom on open */
}
.stage.is-mobile .study.is-open .study-strip { transform: none; }
.stage.is-mobile .study-strip-img + .study-strip-img {
  border-left: var(--stroke-thin) solid var(--stroke-thin-color);
  border-top: 0;
}
/* Sleeve sits just above the bottom roulette */
.stage.is-mobile .study-sleeve { bottom: 18%; }

.study.is-open .study-sleeve { transform: translateY(0); opacity: 1; }

/* Content cascade: title → sub → desc rise in sequence once the panel opens */
.sleeve-title, .sleeve-sub, .sleeve-desc {
  opacity: 0;
  transform: translateY(7px);
  transition: opacity 0.3s var(--ease), transform 0.4s var(--ease);
}
.study.is-open .sleeve-title { transition-delay: 0.14s; opacity: 1; transform: none; }
.study.is-open .sleeve-sub   { transition-delay: 0.19s; opacity: 1; transform: none; }
.study.is-open .sleeve-desc  { transition-delay: 0.24s; opacity: 1; transform: none; }

.sleeve-num { display: none; }
.sleeve-title {
  font-size: max(9px, calc(0.95rem * var(--text-scale, 1)));
  font-weight: 500; line-height: 1.2;
  letter-spacing: -0.01em;
  color: #111;
  margin-bottom: 3px;
}
.sleeve-sub {
  font-size: max(8px, calc(11px * var(--text-scale, 1)));
  font-weight: 300;
  color: #999;
  margin-bottom: 6px;
}
.sleeve-desc {
  font-size: max(8px, calc(11.5px * var(--text-scale, 1)));
  font-weight: 400; line-height: 1.65;
  color: #555;
}

/* ── Close button ────────────────────────────────────────── */

.study-close {
  position: absolute;
  top: 12px; right: 12px;
  width: 28px; height: 28px;
  border-radius: 50%;
  border: 1px solid rgba(255,255,255,0.35);
  background: rgba(0,0,0,0.3);
  color: #fff;
  display: flex; align-items: center; justify-content: center;
  cursor: pointer;
  opacity: 0; pointer-events: none;
  transform: scale(0.6) rotate(-90deg);
  transition: opacity 0.2s, transform 0.25s var(--ease), background 0.15s;
  z-index: 10;
}
.study-close:hover { background: rgba(0,0,0,0.55); }
.study.is-open .study-close {
  opacity: 1; pointer-events: auto;
  transform: scale(1) rotate(0deg);
}

/* ── Full-screen button (top-left, mirrors close) ────────── */

.study-fs {
  position: absolute;
  top: 12px; left: 12px;
  width: 28px; height: 28px;
  border-radius: 50%;
  border: 1px solid rgba(255,255,255,0.35);
  background: rgba(0,0,0,0.3);
  color: #fff;
  display: flex; align-items: center; justify-content: center;
  cursor: pointer;
  opacity: 0; pointer-events: none;
  transform: scale(0.6);
  transition: opacity 0.2s, transform 0.25s var(--ease), background 0.15s;
  z-index: 10;
}
.study-fs:hover { background: rgba(0,0,0,0.55); }
.study.is-open .study-fs {
  opacity: 1; pointer-events: auto;
  transform: scale(1);
}
/* Icon swap: expand by default, collapse while full-screen */
.study-fs .ic-collapse { display: none; }
.study.is-fs .study-fs .ic-expand { display: none; }
.study.is-fs .study-fs .ic-collapse { display: block; }

/* ── Move handle (drag a card onto another to swap) ──────── */

.study-move {
  position: absolute;
  top: 12px; left: 48px;          /* sits right of the full-screen button */
  width: 28px; height: 28px;
  border-radius: 50%;
  border: 1px solid rgba(255,255,255,0.35);
  background: rgba(0,0,0,0.3);
  color: #fff;
  display: flex; align-items: center; justify-content: center;
  cursor: grab;
  touch-action: none;
  opacity: 0; pointer-events: none;
  transform: scale(0.6);
  transition: opacity 0.2s, transform 0.25s var(--ease), background 0.15s;
  z-index: 10;
}
.study-move:hover { background: rgba(0,0,0,0.55); }
.study-move:active { cursor: grabbing; }
.study.is-open .study-move {
  opacity: 1; pointer-events: auto;
  transform: scale(1);
}
.study.is-fs .study-move { display: none; }   /* hidden in full screen */

/* Card being dragged: float it, no transition, let clicks fall through */
.study.is-grabbing {
  transition: none !important;          /* transform/scale is set inline */
  box-shadow: 0 24px 60px -12px rgba(0,0,0,0.5);
  cursor: grabbing;
}
/* Whole grabbed card (buttons included) ignores hit-testing so elementFromPoint
   resolves to the card underneath the cursor. */
.study.is-grabbing,
.study.is-grabbing * { pointer-events: none !important; }

/* Dashed frame left where the dragged card was */
.card-ghost {
  position: absolute;
  z-index: 0;
  border: 2px dashed rgba(0,0,0,0.4);
  background: rgba(0,0,0,0.03);
  pointer-events: none;
}

/* Overlay on the card that will be swapped in */
.study.swap-target::before {
  content: "";
  position: absolute;
  inset: 0;
  z-index: 50;
  background: rgba(15,15,15,0.45);
  box-shadow: inset 0 0 0 2px #fff;
  pointer-events: none;
}

/* Hide resize seams while dragging a card, and stop text selection */
.stage.cards-dragging { user-select: none; }
.stage.cards-dragging .resize-handle { display: none !important; }

/* ── Open state ──────────────────────────────────────────── */

.study.is-open {
  cursor: default;
  /* The wider GAP_OPEN gutter forms the heavier white frame; drop shadow adds lift */
  box-shadow: 0 18px 40px -12px rgba(0,0,0,0.4);
  z-index: 1;
}
.study:focus-visible {
  outline: 2px solid #111;
  outline-offset: -2px;
}
.study.is-open:focus-visible { outline: none; }

/* ── Resize handles (drag the seam between open panels) ───── */

.resize-handle {
  position: absolute;
  z-index: 100000;
  display: none;
  touch-action: none;
}
.resize-handle[data-orient="v"] { cursor: col-resize; }
.resize-handle[data-orient="h"] { cursor: row-resize; }

/* Visible grip line, centred in the hit area; appears on hover/drag */
.resize-handle::before {
  content: "";
  position: absolute;
  background: rgba(0, 0, 0, 0);
  transition: background 0.15s;
}
.resize-handle[data-orient="v"]::before {
  left: 50%; top: 0; bottom: 0; width: 2px; transform: translateX(-50%);
}
.resize-handle[data-orient="h"]::before {
  top: 50%; left: 0; right: 0; height: 2px; transform: translateY(-50%);
}
.resize-handle:hover::before,
.resize-handle.is-active::before { background: rgba(0, 0, 0, 0.4); }

/* While dragging, freeze panel interactions so hover/clicks don't fire */
.stage.is-dragging .study { pointer-events: none; }

/* ── No-transition helper ────────────────────────────────── */

.stage.no-transition .study,
.stage.no-transition .study * {
  transition: none !important;
}

/* ── Mobile: same absolute BSP, but horizontal-only splits ──────── */
/* (split direction is forced in script.js; here just touch ergonomics) */

@media (max-width: 760px) {
  /* Bigger touch targets for the open-panel buttons */
  .study-close, .study-fs, .study-move { width: 36px; height: 36px; }
  .study-fs   { left: 10px; }
  .study-move { left: 54px; }
  .study-close { right: 10px; }

  /* Resize seams are between horizontal bands — make them grabbable & visible */
  .resize-handle[data-orient="h"]::before { background: rgba(0, 0, 0, 0.22); }

  /* Keep the info legible in a short band */
  .study.is-open .sleeve-title { font-size: max(0.95rem, calc(0.95rem * var(--text-scale, 1))); }
  .study.is-open .sleeve-desc  { font-size: max(12px, calc(11.5px * var(--text-scale, 1))); }
}

/* ── Respect reduced-motion preference ───────────────────── */

@media (prefers-reduced-motion: reduce) {
  .study, .study *, .study-bg, .study-strip, .study-sleeve,
  .sleeve-title, .sleeve-sub, .sleeve-desc {
    transition-duration: 0.001s !important;
    transition-delay: 0s !important;
  }
}
