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

:root {
  --bg: #0f1117;
  --surface: #1a1d27;
  --surface2: #222535;
  --border: #2d3047;
  --accent: #6c63ff;
  --accent2: #8077ff;
  --accent-glow: rgba(108, 99, 255, 0.18);
  --text: #e8eaf0;
  --muted: #8891a8;
  --r: 14px;
  --r-sm: 10px;
  --bar-h: 80px;
  --safe-b: env(safe-area-inset-bottom, 0px);
}

html { height: 100%; }
body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  min-height: 100%;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior: none;
}

/* HEADER */
.header {
  position: sticky; top: 0; z-index: 50;
  background: rgba(15, 17, 23, 0.9);
  backdrop-filter: blur(14px);
  -webkit-backdrop-filter: blur(14px);
  border-bottom: 1px solid var(--border);
  padding: 14px 16px;
  padding-top: calc(14px + env(safe-area-inset-top, 0px));
}
.logo {
  display: flex; align-items: center; gap: 8px;
  font-size: 1rem; font-weight: 700; color: var(--accent);
}

/* MAIN */
.main {
  max-width: 700px; margin: 0 auto;
  padding: 0 14px 120px;
}

/* HERO */
.hero { padding: 28px 0 20px; text-align: center; }
.hero h1 {
  font-size: clamp(1.35rem, 4.5vw, 2rem);
  font-weight: 800; line-height: 1.25;
  background: linear-gradient(135deg, #fff 40%, var(--accent));
  -webkit-background-clip: text; -webkit-text-fill-color: transparent;
  background-clip: text;
  margin-bottom: 8px;
}
.hero p { color: var(--muted); font-size: 0.9rem; }

/* SEARCH */
.search-form { margin-bottom: 12px; }
.input-wrap {
  display: flex; align-items: center;
  background: var(--surface);
  border: 1.5px solid var(--border);
  border-radius: 50px;
  padding: 6px 6px 6px 14px;
  gap: 8px;
  transition: border-color .2s, box-shadow .2s;
}
.input-wrap:focus-within {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-glow);
}
.input-icon { color: var(--muted); flex-shrink: 0; }
.url-input {
  flex: 1; background: none; border: none; outline: none;
  color: var(--text); font-size: 0.95rem; min-width: 0;
  padding: 6px 0;
}
.url-input::placeholder { color: var(--muted); }
.btn-scan {
  display: flex; align-items: center; justify-content: center; gap: 6px;
  background: var(--accent); color: #fff;
  border: none; border-radius: 40px;
  padding: 10px 20px; font-size: 0.88rem; font-weight: 600;
  cursor: pointer; flex-shrink: 0;
  transition: background .15s, transform .1s;
  min-height: 40px;
}
.btn-scan:active { transform: scale(.96); }
.btn-scan:disabled { opacity: .6; cursor: not-allowed; transform: none; }
.btn-scan:not(:disabled):hover { background: var(--accent2); }

/* ERROR */
.error-box {
  display: flex; align-items: flex-start; gap: 8px;
  background: rgba(239,68,68,.1);
  border: 1px solid rgba(239,68,68,.25);
  color: #f87171; border-radius: var(--r-sm);
  padding: 10px 12px; margin: 8px 0;
  font-size: 0.85rem; line-height: 1.4;
}

/* RESULTS BAR */
.results { margin-top: 4px; }
.results-bar {
  display: flex; flex-direction: column; gap: 10px;
  padding: 12px 0 10px;
  border-bottom: 1px solid var(--border);
  margin-bottom: 14px;
}
.count-label { font-weight: 600; font-size: 0.95rem; }

/* FILTER PILLS */
.filter-pills {
  display: flex; gap: 6px; overflow-x: auto;
  scrollbar-width: none; -ms-overflow-style: none;
  padding-bottom: 2px;
}
.filter-pills::-webkit-scrollbar { display: none; }
.pill {
  flex-shrink: 0;
  background: var(--surface2); color: var(--muted);
  border: 1px solid var(--border); border-radius: 20px;
  padding: 5px 14px; font-size: 0.8rem; cursor: pointer;
  transition: all .15s;
  white-space: nowrap;
}
.pill.active {
  background: var(--accent-glow); border-color: var(--accent);
  color: var(--accent); font-weight: 600;
}

/* IMAGE GRID */
.image-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 10px;
}
@media (min-width: 480px) {
  .image-grid { grid-template-columns: repeat(3, 1fr); }
}
@media (min-width: 640px) {
  .image-grid { grid-template-columns: repeat(4, 1fr); }
}

/* IMAGE CARD */
.img-card {
  background: var(--surface);
  border: 1.5px solid var(--border);
  border-radius: var(--r);
  overflow: hidden;
  cursor: pointer;
  position: relative;
  transition: border-color .15s, transform .15s;
  -webkit-user-select: none; user-select: none;
}
.img-card.selected {
  border-color: var(--accent);
  box-shadow: 0 0 0 2px var(--accent-glow);
}
.img-card:active { transform: scale(.97); }

.img-thumb-wrap {
  width: 100%; aspect-ratio: 1;
  background: var(--surface2);
  display: flex; align-items: center; justify-content: center;
  overflow: hidden; position: relative;
}
.img-thumb {
  width: 100%; height: 100%; object-fit: cover;
  transition: opacity .25s;
}
.img-thumb.loading { opacity: 0; }
.img-placeholder {
  position: absolute; color: var(--border);
  pointer-events: none;
}

/* Checkmark overlay */
.img-check {
  position: absolute; top: 7px; left: 7px;
  width: 24px; height: 24px;
  background: var(--accent); border-radius: 50%;
  display: flex; align-items: center; justify-content: center;
  opacity: 0; transform: scale(.4);
  transition: opacity .15s, transform .15s;
  pointer-events: none; z-index: 2;
}
.img-card.selected .img-check { opacity: 1; transform: scale(1); }

/* Expand button */
.img-expand {
  position: absolute; bottom: 7px; right: 7px;
  width: 30px; height: 30px;
  background: rgba(0,0,0,.55); border: none; border-radius: 8px;
  color: #fff; display: flex; align-items: center; justify-content: center;
  cursor: pointer; z-index: 2;
  opacity: 0; transition: opacity .15s;
}
@media (hover: hover) {
  .img-card:hover .img-expand { opacity: 1; }
}
/* Always visible on touch */
@media (hover: none) {
  .img-expand { opacity: 1; }
}

.img-info {
  padding: 8px 10px;
}
.img-name {
  font-size: 0.72rem; color: var(--text);
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
  margin-bottom: 2px;
}
.img-meta { display: flex; align-items: center; gap: 6px; }
.img-badge {
  font-size: 0.65rem; font-weight: 700; text-transform: uppercase;
  background: var(--surface2); color: var(--muted);
  padding: 1px 5px; border-radius: 4px;
}
.img-size { font-size: 0.68rem; color: var(--muted); }

/* NO MATCH */
.no-match {
  text-align: center; padding: 40px 20px;
  color: var(--muted);
  display: flex; flex-direction: column; align-items: center; gap: 10px;
  font-size: 0.9rem;
}

/* BOTTOM ACTION BAR */
.action-bar {
  position: fixed; bottom: 0; left: 0; right: 0; z-index: 100;
  background: rgba(26, 29, 39, 0.97);
  backdrop-filter: blur(16px); -webkit-backdrop-filter: blur(16px);
  border-top: 1px solid var(--border);
  padding: 12px 16px;
  padding-bottom: calc(12px + var(--safe-b));
}
.action-bar-inner {
  max-width: 700px; margin: 0 auto;
  display: flex; align-items: center; justify-content: space-between;
  gap: 12px;
}
.selection-info {
  display: flex; align-items: center; gap: 10px; flex-wrap: wrap;
  font-size: 0.85rem;
}
.link-btn {
  background: none; border: none; color: var(--accent);
  font-size: 0.82rem; cursor: pointer; padding: 0;
  text-decoration: underline;
}
.action-btns { display: flex; gap: 8px; flex-shrink: 0; }
.btn-action {
  display: flex; align-items: center; gap: 6px;
  border: none; border-radius: var(--r-sm);
  padding: 10px 16px; font-size: 0.85rem; font-weight: 600;
  cursor: pointer; transition: opacity .15s;
  min-height: 44px;
}
.btn-action:active { opacity: .7; }
.btn-share {
  background: var(--surface2); color: var(--text);
  border: 1px solid var(--border);
}
.btn-download {
  background: var(--accent); color: #fff;
}

/* LIGHTBOX */
.lightbox {
  position: fixed; inset: 0; z-index: 200;
  display: flex; align-items: flex-end; justify-content: center;
}
@media (min-width: 600px) {
  .lightbox { align-items: center; }
}
.lb-backdrop {
  position: absolute; inset: 0;
  background: rgba(0,0,0,.85);
  backdrop-filter: blur(4px); -webkit-backdrop-filter: blur(4px);
}
.lb-box {
  position: relative; z-index: 1;
  width: 100%; max-width: 580px;
  background: var(--surface); border-radius: var(--r) var(--r) 0 0;
  overflow: hidden;
  display: flex; flex-direction: column;
  max-height: 92vh;
}
@media (min-width: 600px) {
  .lb-box { border-radius: var(--r); max-height: 88vh; }
}
.lb-top {
  display: flex; align-items: center; justify-content: space-between;
  padding: 12px 14px;
  border-bottom: 1px solid var(--border);
}
.lb-name { font-size: 0.82rem; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.lb-close {
  background: var(--surface2); border: none; border-radius: 8px;
  color: var(--text); width: 32px; height: 32px;
  display: flex; align-items: center; justify-content: center;
  cursor: pointer; flex-shrink: 0;
}
.lb-img-wrap {
  flex: 1; overflow: auto;
  display: flex; align-items: center; justify-content: center;
  padding: 16px;
  min-height: 200px;
}
#lbImg {
  max-width: 100%; max-height: 65vh;
  object-fit: contain; border-radius: 6px;
  display: block;
}
.lb-actions {
  display: flex; gap: 10px;
  padding: 12px 14px;
  padding-bottom: calc(12px + var(--safe-b));
  border-top: 1px solid var(--border);
}
.lb-btn {
  flex: 1; display: flex; align-items: center; justify-content: center; gap: 7px;
  background: var(--surface2); color: var(--text);
  border: 1px solid var(--border); border-radius: var(--r-sm);
  padding: 11px; font-size: 0.88rem; font-weight: 600;
  cursor: pointer; text-decoration: none;
  transition: opacity .15s; min-height: 44px;
}
.lb-btn:active { opacity: .7; }
.lb-btn-primary {
  background: var(--accent); color: #fff; border-color: var(--accent);
}

/* TOAST */
.toast {
  position: fixed; bottom: calc(var(--bar-h) + 12px + var(--safe-b)); left: 50%;
  transform: translateX(-50%);
  background: var(--surface2); border: 1px solid var(--border);
  border-radius: var(--r); padding: 10px 18px;
  display: flex; align-items: center; gap: 8px;
  font-size: 0.85rem; font-weight: 500;
  box-shadow: 0 8px 32px rgba(0,0,0,.5);
  white-space: nowrap; z-index: 300;
}

/* UTILS */
.hidden { display: none !important; }
.spin-anim { animation: spin .75s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
