/* Photo Gallery Mini App. Colours come from Telegram's theme (--tg-theme-*),
   which telegram-web-app.js sets on the page; the fallbacks are for a plain
   browser, where it is only ever previewed. */

:root {
  --fb-bg: #ffffff; --fb-bg2: #f1f1f4; --fb-text: #111114; --fb-hint: #8c8c93;
  --fb-accent: #2c8ef0; --fb-accent-text: #ffffff; --fb-danger: #e5484d;
  --fb-sep: rgba(0, 0, 0, .08);

  --bg: var(--tg-theme-bg-color, var(--fb-bg));
  --bg2: var(--tg-theme-secondary-bg-color, var(--fb-bg2));
  --section: var(--tg-theme-section-bg-color, var(--bg));
  --text: var(--tg-theme-text-color, var(--fb-text));
  --hint: var(--tg-theme-hint-color, var(--fb-hint));
  --sub: var(--tg-theme-subtitle-text-color, var(--hint));
  --accent: var(--tg-theme-button-color, var(--fb-accent));
  --accent-text: var(--tg-theme-button-text-color, var(--fb-accent-text));
  --link: var(--tg-theme-accent-text-color, var(--tg-theme-link-color, var(--accent)));
  --danger: var(--tg-theme-destructive-text-color, var(--fb-danger));
  --sep: var(--tg-theme-section-separator-color, var(--fb-sep));
  --bar: var(--tg-theme-bottom-bar-bg-color, var(--bg));

  --radius: 14px;
  --gap: 10px;
  --pad: 14px;
  --safe-top: calc(var(--tg-safe-area-inset-top, 0px) + var(--tg-content-safe-area-inset-top, 0px));
  --safe-bottom: max(env(safe-area-inset-bottom, 0px), var(--tg-safe-area-inset-bottom, 0px));
  --bar-h: 0px;
}

@media (prefers-color-scheme: dark) {
  :root {
    --fb-bg: #17181c; --fb-bg2: #0f1013; --fb-text: #f2f2f5; --fb-hint: #8d8f98;
    --fb-accent: #4ea4f6; --fb-sep: rgba(255, 255, 255, .09);
  }
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; padding: 0; background: var(--bg); color: var(--text); }
body {
  font: 15px/1.4 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  overscroll-behavior-y: none;
  -webkit-font-smoothing: antialiased;
  touch-action: manipulation;
}
body.locked { overflow: hidden; }
button { font: inherit; color: inherit; }
svg { display: block; }
[hidden] { display: none !important; }

.app {
  min-height: var(--tg-viewport-stable-height, 100vh);
  padding: calc(var(--safe-top) + 6px) var(--pad) calc(var(--safe-bottom) + var(--bar-h) + 24px);
}

/* ---- header ------------------------------------------------------------ */

.top { display: flex; align-items: center; gap: 8px; min-height: 52px; padding: 4px 0 8px; }
.titles { flex: 1; min-width: 0; }
.top h1 { margin: 0; font-size: 22px; line-height: 1.2; font-weight: 700; letter-spacing: -.01em; }
.subtitle { margin: 2px 0 0; color: var(--sub); font-size: 14px; }
.nav-back, .icon-btn {
  flex: none; width: 38px; height: 38px; border: 0; border-radius: 50%;
  background: var(--bg2); display: grid; place-items: center; cursor: pointer;
}
.nav-back svg, .icon-btn svg { width: 20px; height: 20px; fill: none; stroke: var(--text); stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.icon-btn svg rect { fill: var(--text); stroke: none; opacity: .85; }
.icon-btn.on { background: var(--accent); }
.icon-btn.on svg rect { fill: var(--accent-text); }

.notice {
  margin: 0 0 12px; padding: 11px 13px; border-radius: 12px;
  background: var(--bg2); color: var(--text); font-size: 14px;
}
.notice b { font-weight: 600; }
.notice.good { background: var(--bg2); box-shadow: inset 3px 0 0 #34c759; }

/* ---- the picking bar ---------------------------------------------------- */

.pickbar {
  position: sticky; top: 0; z-index: 5;
  margin: 0 calc(-1 * var(--pad)) 12px; padding: 8px var(--pad) 10px;
  background: var(--bg);
  border-bottom: 1px solid var(--sep);
}
@supports (background: color-mix(in srgb, red 50%, blue)) and ((backdrop-filter: blur(1px)) or (-webkit-backdrop-filter: blur(1px))) {
  .pickbar {
    background: color-mix(in srgb, var(--bg) 88%, transparent);
    -webkit-backdrop-filter: saturate(1.6) blur(14px); backdrop-filter: saturate(1.6) blur(14px);
  }
}
.pickbar-row { display: flex; align-items: baseline; justify-content: space-between; margin-bottom: 7px; }
.pick-count { font-size: 15px; font-weight: 600; }
.pick-count .of { color: var(--hint); font-weight: 400; }
.text-btn { border: 0; background: none; color: var(--link); padding: 2px 0 2px 12px; cursor: pointer; font-size: 15px; }
.text-btn:disabled { color: var(--hint); opacity: .6; }
.head-btn { flex: none; padding: 8px 4px 8px 10px; font-weight: 500; white-space: nowrap; }
.meter { height: 5px; border-radius: 3px; background: var(--bg2); overflow: hidden; }
.meter span { display: block; height: 100%; width: 0; border-radius: 3px; background: var(--accent); transition: width .25s ease; }
.pickbar.full .meter span { background: #34c759; }

/* ---- the grid ----------------------------------------------------------- */

.grid {
  display: grid; gap: var(--gap);
  grid-template-columns: repeat(auto-fill, minmax(148px, 1fr));
}
.grid.dense { --gap: 3px; grid-template-columns: repeat(auto-fill, minmax(96px, 1fr)); margin: 0 calc(-1 * var(--pad) + 3px); }
.tile {
  position: relative; cursor: pointer; outline: none;
  -webkit-user-select: none; user-select: none; -webkit-touch-callout: none;
  content-visibility: auto; contain-intrinsic-size: 150px 200px;
}
.ph {
  position: relative; width: 100%; padding-top: 100%; overflow: hidden;
  border-radius: var(--radius); background: var(--bg2);
  transition: transform .18s ease, border-radius .18s ease;
}
.grid.dense .ph { border-radius: 4px; }
.ph img {
  position: absolute; inset: 0; width: 100%; height: 100%; object-fit: cover;
  opacity: 0; transition: opacity .25s ease; pointer-events: none; -webkit-user-drag: none;
}
.ph.loaded img { opacity: 1; }
.ph::after { /* shimmer while loading */
  content: ""; position: absolute; inset: 0;
  background: linear-gradient(100deg, transparent 20%, rgba(255,255,255,.18) 50%, transparent 80%);
  background-size: 200% 100%; animation: shimmer 1.2s linear infinite;
}
.ph.loaded::after, .ph.broken::after { display: none; }
.ph.broken { background: var(--bg2) center / 34% no-repeat url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='%23999' stroke-width='1.6'%3E%3Crect x='3' y='5' width='18' height='14' rx='2'/%3E%3Cpath d='M3 15l5-5 4 4 3-3 6 6'/%3E%3C/svg%3E"); }
@keyframes shimmer { from { background-position: 150% 0; } to { background-position: -50% 0; } }

.cap {
  margin: 6px 2px 0; font-size: 13px; line-height: 1.3; color: var(--text);
  display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden;
  overflow-wrap: anywhere; min-height: 2.6em;
}
.cap.none { color: var(--hint); font-style: italic; }
.grid.dense .cap { display: none; }

.tile:focus-visible .ph { box-shadow: 0 0 0 3px var(--accent); }

/* the round mark in the corner while picking */
.mark {
  position: absolute; top: 7px; right: 7px; width: 28px; height: 28px; border-radius: 50%;
  display: none; place-items: center; font-size: 14px; font-weight: 700;
  color: #fff; background: rgba(0, 0, 0, .22); border: 2px solid rgba(255, 255, 255, .95);
  box-shadow: 0 1px 4px rgba(0, 0, 0, .25); transition: transform .15s ease;
}
.picking .mark { display: grid; }
.grid.dense .mark { width: 24px; height: 24px; top: 5px; right: 5px; font-size: 12px; }
.tile.picked .mark { background: var(--accent); color: var(--accent-text); border-color: var(--accent-text); transform: scale(1.06); }
.tile.picked .ph { transform: scale(.9); border-radius: calc(var(--radius) + 4px); box-shadow: 0 0 0 3px var(--accent); }
.grid.dense .tile.picked .ph { transform: scale(.86); }
/* Select mode, to delete several: the same marks, in the danger colour, with a tick. */
.selecting .tile.picked .mark { background: var(--danger); border-color: #fff; color: #fff; }
.selecting .tile.picked .ph { box-shadow: 0 0 0 3px var(--danger); }
.tile.dim .ph { opacity: .45; }
.tile.dim .cap { opacity: .55; }
.tile.nope { animation: nope .32s ease; }
@keyframes nope { 0%, 100% { transform: translateX(0); } 25% { transform: translateX(-5px); } 75% { transform: translateX(5px); } }

/* "Sent" tag on photos in the selection already with the admins */
.sent-tag {
  position: absolute; left: 7px; top: 7px; display: none;
  padding: 2px 8px 2px 6px; border-radius: 10px; font-size: 11.5px; font-weight: 600;
  color: #fff; background: rgba(0, 0, 0, .5);
  -webkit-backdrop-filter: blur(6px); backdrop-filter: blur(6px);
}
.sent-tag::before { content: "✓ "; }
.tile.is-sent .sent-tag { display: block; }
.grid.dense .sent-tag { font-size: 10px; padding: 1px 6px 1px 4px; left: 4px; top: 4px; }

/* the magnifier, to look at a photo while picking */
.zoom {
  position: absolute; left: 6px; bottom: calc(2.6em + 18px); width: 30px; height: 30px;
  border: 0; border-radius: 50%; padding: 0; display: none; place-items: center; cursor: pointer;
  background: rgba(0, 0, 0, .42); -webkit-backdrop-filter: blur(6px); backdrop-filter: blur(6px);
}
.zoom svg { width: 16px; height: 16px; fill: none; stroke: #fff; stroke-width: 2.2; stroke-linecap: round; stroke-linejoin: round; }
.picking .zoom { display: grid; }
.grid.dense .zoom { bottom: 5px; left: 5px; width: 26px; height: 26px; }

.skeleton .ph::after { display: block; }
.skeleton .cap { background: var(--bg2); border-radius: 6px; color: transparent; width: 70%; min-height: 1em; }

/* ---- panels: empty, done, problem -------------------------------------- */

.panel { text-align: center; padding: 48px 18px 24px; }
.panel h2 { margin: 14px 0 6px; font-size: 20px; }
.panel p { margin: 0 auto; max-width: 320px; color: var(--hint); }
.empty-art svg { width: 84px; height: 84px; margin: 0 auto; fill: none; stroke: var(--hint); stroke-width: 2.4; stroke-linejoin: round; stroke-linecap: round; opacity: .8; }
.done-badge {
  width: 78px; height: 78px; margin: 0 auto; border-radius: 50%; display: grid; place-items: center;
  background: #34c759; animation: pop .45s cubic-bezier(.2, 1.4, .4, 1);
}
.done-badge svg { width: 42px; height: 42px; fill: none; stroke: #fff; stroke-width: 2.6; stroke-linecap: round; stroke-linejoin: round; }
@keyframes pop { from { transform: scale(.4); opacity: 0; } to { transform: scale(1); opacity: 1; } }
.problem .btn { margin-top: 18px; }

/* ---- review -------------------------------------------------------------- */

.review-list { list-style: none; margin: 0; padding: 0; background: var(--section); border-radius: var(--radius); overflow: hidden; }
.review-list li {
  display: flex; align-items: center; gap: 12px; padding: 9px 12px; cursor: pointer;
  border-bottom: 1px solid var(--sep);
}
.review-list li:last-child { border-bottom: 0; }
.review-num {
  flex: none; width: 24px; height: 24px; border-radius: 50%; display: grid; place-items: center;
  font-size: 13px; font-weight: 700; background: var(--accent); color: var(--accent-text);
}
.review-thumb { flex: none; width: 58px; height: 58px; border-radius: 10px; object-fit: cover; background: var(--bg2); }
.review-cap { flex: 1; min-width: 0; font-size: 14px; overflow-wrap: anywhere;
  display: -webkit-box; -webkit-line-clamp: 3; -webkit-box-orient: vertical; overflow: hidden; }
.review-cap.none { color: var(--hint); font-style: italic; }
.review-note { color: var(--hint); font-size: 13.5px; margin: 12px 4px 0; }

/* ---- buttons ------------------------------------------------------------- */

.btn {
  border: 0; border-radius: 12px; padding: 12px 22px; cursor: pointer; font-weight: 600;
  background: var(--accent); color: var(--accent-text);
}
.btn.ghost { background: var(--bg2); color: var(--text); }
.btn:disabled { opacity: .45; cursor: default; }
.bottombar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 20;
  padding: 10px var(--pad) calc(10px + var(--safe-bottom));
  background: var(--bar); border-top: 1px solid var(--sep);
}
.btn.main { width: 100%; padding: 14px; font-size: 16px; }
.btn.main.danger { background: var(--danger); color: #fff; }
.btn.busy { position: relative; color: transparent; }
.btn.busy::after {
  content: ""; position: absolute; left: 50%; top: 50%; width: 18px; height: 18px; margin: -9px 0 0 -9px;
  border-radius: 50%; border: 2px solid var(--accent-text); border-right-color: transparent;
  animation: spin .7s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ---- the full view ------------------------------------------------------- */

.viewer {
  position: fixed; inset: 0; z-index: 30; display: flex; flex-direction: column;
  background: #000; color: #fff;
  padding-top: var(--safe-top); padding-bottom: var(--safe-bottom);
  animation: fade .18s ease;
}
@keyframes fade { from { opacity: 0; } to { opacity: 1; } }
.viewer-top { display: flex; align-items: center; justify-content: space-between; padding: 8px 10px; }
.viewer-btn { width: 38px; height: 38px; border: 0; border-radius: 50%; display: grid; place-items: center; background: rgba(255,255,255,.12); cursor: pointer; }
.viewer-btn.ghost { background: none; }
.viewer-btn svg { width: 18px; height: 18px; fill: none; stroke: #fff; stroke-width: 2.2; stroke-linecap: round; }
.viewer-count { font-size: 14px; color: rgba(255, 255, 255, .75); font-variant-numeric: tabular-nums; }
/* touch-action none: with pan-y the browser takes a vertical drag (and cancels the
   pointer), so swipe-down-to-close never fired on phones. Nothing here scrolls. */
.viewer-stage { position: relative; flex: 1; min-height: 0; display: flex; align-items: center; justify-content: center; overflow: hidden; touch-action: none; }
/* Pictures take no touches: on Android a long press on an <img> opens Telegram's
   own "open in browser / copy link" menu. Swipes land on the stage instead. */
.viewer-stage img {
  max-width: 100%; max-height: 100%; object-fit: contain; -webkit-user-drag: none; pointer-events: none;
  user-select: none; -webkit-touch-callout: none; transition: transform .2s ease, opacity .2s ease;
}
.review-thumb, .sheet-thumb { pointer-events: none; -webkit-touch-callout: none; }
.viewer-stage img.swiping { transition: none; }
.viewer-nav {
  position: absolute; top: 50%; margin-top: -22px; width: 44px; height: 44px; border: 0; border-radius: 50%;
  display: grid; place-items: center; background: rgba(0, 0, 0, .35); cursor: pointer;
}
.viewer-nav svg { width: 22px; height: 22px; fill: none; stroke: #fff; stroke-width: 2.4; stroke-linecap: round; stroke-linejoin: round; }
.viewer-nav.prev { left: 10px; }
.viewer-nav.next { right: 10px; }
@media (hover: none) { .viewer-nav { display: none; } }
/* Only the caption scrolls; the buttons below it always show, however long it is. */
.viewer-info { padding: 10px 16px 2px; max-height: 32%; overflow-y: auto; overscroll-behavior: contain; }
.viewer-caption { margin: 0; font-size: 15.5px; line-height: 1.45; white-space: pre-wrap; overflow-wrap: anywhere; }
.viewer-caption.none { color: rgba(255, 255, 255, .55); font-style: italic; }
.viewer-meta { margin: 6px 0 6px; font-size: 12.5px; color: rgba(255, 255, 255, .55); }
.viewer-actions { display: flex; gap: 8px; flex-wrap: wrap; padding: 8px 16px 14px; flex: none; }
.pill {
  border: 0; border-radius: 20px; padding: 9px 15px; cursor: pointer; font-size: 14.5px; font-weight: 600;
  background: rgba(255, 255, 255, .14); color: #fff;
}
.pill.accent { background: var(--accent); color: var(--accent-text); }
.pill.accent.on { background: #fff; color: #000; }
.pill.danger { color: #ff8f8f; }
.pill:disabled { opacity: .45; }

/* ---- the caption editor -------------------------------------------------- */

.sheet {
  position: fixed; inset: 0; z-index: 40; background: rgba(0, 0, 0, .45);
  display: flex; align-items: flex-start; justify-content: center;
  padding: calc(var(--safe-top) + 12px) 12px 12px; animation: fade .15s ease;
}
.sheet-card {
  width: 100%; max-width: 560px; background: var(--section); color: var(--text);
  border-radius: 18px; padding: 14px; box-shadow: 0 12px 40px rgba(0, 0, 0, .3);
}
.sheet-head { display: flex; align-items: center; gap: 10px; margin-bottom: 10px; }
.sheet-thumb { width: 40px; height: 40px; border-radius: 8px; object-fit: cover; background: var(--bg2); }
.sheet-head h2 { flex: 1; margin: 0; font-size: 17px; }
.cap-count { font-size: 12.5px; color: var(--hint); font-variant-numeric: tabular-nums; }
.cap-count.over { color: var(--danger); font-weight: 600; }
.sheet textarea {
  width: 100%; resize: vertical; min-height: 96px; max-height: 40vh;
  border: 1px solid var(--sep); border-radius: 12px; padding: 10px 12px;
  background: var(--bg); color: var(--text); font: inherit; font-size: 16px; line-height: 1.4;
}
.sheet textarea:focus { outline: none; border-color: var(--accent); }
.sheet-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 12px; }

/* ---- toast --------------------------------------------------------------- */

.toast {
  position: fixed; left: 50%; z-index: 50; transform: translateX(-50%);
  bottom: calc(var(--safe-bottom) + var(--bar-h) + 16px);
  display: flex; align-items: center; gap: 14px; max-width: calc(100% - 28px);
  padding: 11px 16px; border-radius: 12px; font-size: 14px;
  background: rgba(30, 30, 34, .94); color: #fff; box-shadow: 0 6px 24px rgba(0, 0, 0, .25);
  animation: rise .2s ease;
}
@keyframes rise { from { opacity: 0; transform: translate(-50%, 8px); } to { opacity: 1; transform: translate(-50%, 0); } }
/* Over the full view the toast goes to the top, clear of Remove, Pick and Undo's neighbours. */
.viewing .toast { top: calc(var(--safe-top) + 62px); bottom: auto; }
.toast-action { border: 0; background: none; color: #7cc0ff; font-weight: 700; cursor: pointer; padding: 0; }

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: .01ms !important; transition-duration: .01ms !important; }
}
@media (min-width: 720px) {
  .app { max-width: 980px; margin: 0 auto; }
  .grid { grid-template-columns: repeat(auto-fill, minmax(180px, 1fr)); }
}
