/* =========================================================================
   Matshot Media gallery — styles
   Dark surface so the photography carries the page. Layout is mobile-first;
   the action bar is fixed and respects the iOS home-indicator safe area.
   ========================================================================= */

:root {
  --bg: #0c0d10;
  --surface: #16181d;
  --surface-2: #1e2128;
  --line: #2a2e37;
  --text: #f2f4f8;
  --text-dim: #9aa1ae;
  --accent: #3b82f6;
  --accent-press: #2f6fd8;
  --danger: #f0616d;
  --radius: 14px;
  --bar-height: 150px; /* JS keeps this in sync with the measured bar height */
}

* { box-sizing: border-box; }

html { -webkit-text-size-adjust: 100%; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 400 16px/1.5 -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI",
        Roboto, Helvetica, Arial, sans-serif;
  /* Stop rubber-band scrolling from revealing white behind the fixed bar. */
  overscroll-behavior-y: none;
}

/* ------------------------------------------------------------------ *
   Masthead
 * ------------------------------------------------------------------ */
.masthead {
  padding: 22px 16px 14px;
  border-bottom: 1px solid var(--line);
}
.masthead h1 {
  margin: 0;
  font-size: 21px;
  font-weight: 650;
  letter-spacing: -0.02em;
}
.masthead p {
  margin: 3px 0 0;
  font-size: 14px;
  color: var(--text-dim);
}

/* ------------------------------------------------------------------ *
   Notices (insecure origin, manifest failure, etc.)
 * ------------------------------------------------------------------ */
.notice {
  margin: 12px 16px 0;
  padding: 12px 14px;
  border: 1px solid #4a3a1a;
  background: #241d0e;
  color: #f5d99b;
  border-radius: 10px;
  font-size: 14px;
}
.notice[hidden] { display: none; }
.notice code {
  font: 500 13px/1 ui-monospace, SFMono-Regular, Menlo, monospace;
  background: rgba(255, 255, 255, 0.08);
  padding: 2px 5px;
  border-radius: 4px;
}

/* ------------------------------------------------------------------ *
   Grid
 * ------------------------------------------------------------------ */
.grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(min(31vw, 168px), 1fr));
  gap: 4px;
  padding: 4px;
  /* Keep the last row clear of the fixed action bar. */
  padding-bottom: calc(var(--bar-height) + 24px);
  margin: 0;
  list-style: none;
}

.tile {
  position: relative;
  margin: 0;
  aspect-ratio: 1 / 1;
  background: var(--surface-2);
  border-radius: 6px;
  overflow: hidden;
  cursor: pointer;
  /* Suppress the iOS tap flash and text selection on double tap. */
  -webkit-tap-highlight-color: transparent;
  -webkit-user-select: none;
  user-select: none;
}

.tile img {
  display: block;
  width: 100%;
  height: 100%;
  object-fit: cover;
  transition: transform 180ms ease, opacity 180ms ease;
  /* Long-press on iOS must offer the image menu, not a text selection. */
  -webkit-touch-callout: default;
}

.tile:active img { opacity: 0.82; }

/* Selected state: inset the photo and ring it, mirroring iOS Photos. */
.tile[aria-selected="true"] img { transform: scale(0.86); border-radius: 6px; }
.tile[aria-selected="true"]::after {
  content: "";
  position: absolute;
  inset: 0;
  border: 2.5px solid var(--accent);
  border-radius: 6px;
  pointer-events: none;
}

.tile__check {
  position: absolute;
  top: 6px;
  right: 6px;
  width: 24px;
  height: 24px;
  border-radius: 50%;
  background: var(--accent);
  color: #fff;
  display: none;
  align-items: center;
  justify-content: center;
  box-shadow: 0 1px 4px rgba(0, 0, 0, 0.5);
  pointer-events: none;
}
.tile[aria-selected="true"] .tile__check { display: flex; }
.tile__check svg { width: 14px; height: 14px; }

/* Expand affordance — opens a full-resolution preview.
   The button box is a full 44x44 touch target (Apple's minimum), but the
   visible chip is only 26px: twelve heavy circles would otherwise sit on top
   of the photography the grid exists to show. */
.tile__zoom {
  position: absolute;
  bottom: 0;
  right: 0;
  width: 44px;
  height: 44px;
  padding: 0;
  border: 0;
  background: transparent;
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  opacity: 0;
  transition: opacity 150ms ease;
}
.tile__zoom::before {
  content: "";
  position: absolute;
  width: 26px;
  height: 26px;
  border-radius: 50%;
  background: rgba(0, 0, 0, 0.42);
  backdrop-filter: blur(6px);
  -webkit-backdrop-filter: blur(6px);
}
.tile__zoom svg { position: relative; width: 13px; height: 13px; }
/* Touch devices have no hover, so keep the control visible but restrained. */
@media (hover: hover) { .tile:hover .tile__zoom { opacity: 1; } }
@media (hover: none) { .tile__zoom { opacity: 0.6; } }
.tile__zoom:focus-visible { opacity: 1; outline: 2px solid var(--accent); }
/* Once a tile is selected the ring and checkmark own it — drop the clutter. */
.tile[aria-selected="true"] .tile__zoom { opacity: 0; pointer-events: none; }

/* Per-image status: spinner while converting, badge when conversion failed. */
.tile__spinner {
  position: absolute;
  top: 6px;
  left: 6px;
  width: 18px;
  height: 18px;
  border: 2px solid rgba(255, 255, 255, 0.3);
  border-top-color: #fff;
  border-radius: 50%;
  animation: spin 700ms linear infinite;
  display: none;
}
.tile[data-state="loading"] .tile__spinner { display: block; }

.tile__error {
  position: absolute;
  inset: auto 0 0 0;
  background: var(--danger);
  color: #1a0409;
  font-size: 11px;
  font-weight: 600;
  padding: 4px 6px;
  text-align: center;
  display: none;
}
.tile[data-state="error"] .tile__error { display: block; }
.tile[data-state="error"] img { opacity: 0.45; }

/* Fallback-mode hint: marks a tile that now holds the full-resolution photo. */
.tile__hint {
  position: absolute;
  inset: auto 0 0 0;
  background: rgba(0, 0, 0, 0.72);
  color: #fff;
  font-size: 11px;
  font-weight: 600;
  padding: 5px 6px;
  text-align: center;
  display: none;
}
.tile[data-hint="true"] .tile__hint { display: block; }

@keyframes spin { to { transform: rotate(360deg); } }

/* ------------------------------------------------------------------ *
   Action bar — persistent, always on screen
 * ------------------------------------------------------------------ */
.actionbar {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 20;
  background: rgba(18, 20, 25, 0.93);
  backdrop-filter: saturate(160%) blur(18px);
  -webkit-backdrop-filter: saturate(160%) blur(18px);
  border-top: 1px solid var(--line);
  /* env() keeps the bar clear of the iPhone home indicator. */
  padding: 12px 16px max(12px, env(safe-area-inset-bottom));
}

.actionbar__meta {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  /* The 44px link buttons carry their own breathing room, so this stays tight
     — the bar must not eat the grid on a short phone. */
  margin-bottom: 4px;
}

.count { font-size: 15px; font-weight: 600; }
.count[data-empty="true"] { color: var(--text-dim); font-weight: 500; }

.actionbar__links { display: flex; gap: 6px; }

.linkbtn {
  /* min-height meets the 44px touch-target minimum; the negative inline
     margin keeps the enlarged box from pushing the row wider than it looks. */
  display: inline-flex;
  align-items: center;
  min-height: 44px;
  background: none;
  border: 0;
  color: var(--accent);
  font: inherit;
  font-size: 15px;
  padding: 6px 8px;
  border-radius: 8px;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}
.linkbtn:disabled { color: var(--text-dim); opacity: 0.5; cursor: default; }
.linkbtn:not(:disabled):active { background: var(--surface-2); }

.btn-primary {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 9px;
  width: 100%;
  min-height: 50px;
  padding: 14px 18px;
  border: 0;
  border-radius: var(--radius);
  background: var(--accent);
  color: #fff;
  font: inherit;
  font-size: 17px;
  font-weight: 600;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
  transition: background 120ms ease, opacity 120ms ease;
}
.btn-primary:disabled { background: var(--surface-2); color: var(--text-dim); cursor: default; }
.btn-primary:not(:disabled):active { background: var(--accent-press); }

/* Button spinner shown while blobs are fetched and converted to Files. */
.btn-primary__spinner {
  width: 17px;
  height: 17px;
  border: 2px solid rgba(255, 255, 255, 0.35);
  border-top-color: #fff;
  border-radius: 50%;
  animation: spin 700ms linear infinite;
  display: none;
  flex: none;
}
.btn-primary[data-busy="true"] .btn-primary__spinner { display: block; }

/* The always-visible instruction line (spec section 3, placement option a). */
.microcopy {
  margin: 10px 2px 0;
  font-size: 13px;
  line-height: 1.45;
  color: var(--text-dim);
  text-align: center;
}
.microcopy strong { color: var(--text); font-weight: 600; }

.status {
  margin: 8px 2px 0;
  min-height: 18px;
  font-size: 13px;
  text-align: center;
  color: var(--text-dim);
}
.status[data-tone="warn"] { color: #f5c26b; }
.status[data-tone="error"] { color: var(--danger); }

/* Fallback panel that *replaces* the save button on unsupported browsers. */
.fallback {
  border: 1px solid var(--line);
  background: var(--surface);
  border-radius: var(--radius);
  padding: 14px 16px;
  font-size: 14px;
  line-height: 1.5;
  color: var(--text);
}
.fallback strong { display: block; margin-bottom: 4px; font-weight: 650; }

/* Short viewports (iPhone SE, folded phones, landscape): claw back vertical
   space so the fixed bar leaves a usable amount of grid on screen. */
@media (max-height: 720px) {
  .masthead { padding: 14px 16px 10px; }
  .masthead h1 { font-size: 19px; }
  .actionbar { padding-top: 8px; }
  .btn-primary { min-height: 46px; padding: 12px 16px; font-size: 16px; }
  .microcopy { margin-top: 8px; font-size: 12.5px; }
  .status { margin-top: 6px; }
}

/* ------------------------------------------------------------------ *
   Lightbox
 * ------------------------------------------------------------------ */
.lightbox {
  position: fixed;
  inset: 0;
  z-index: 50;
  background: rgba(0, 0, 0, 0.96);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: env(safe-area-inset-top) 0 env(safe-area-inset-bottom);
}
.lightbox[hidden] { display: none; }
.lightbox img { max-width: 100%; max-height: 100%; object-fit: contain; }
.lightbox__close {
  position: absolute;
  top: max(14px, env(safe-area-inset-top));
  right: 14px;
  width: 38px;
  height: 38px;
  border: 0;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.16);
  color: #fff;
  font-size: 20px;
  line-height: 1;
  cursor: pointer;
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
  }
}
