:root {
  --bg: #0f1115;
  --surface: #1a1d24;
  --surface-2: #23272f;
  --text: #f4f5f7;
  --text-dim: #9a9ea8;
  --accent: #ff5a5f;
  --keep: #38d39f;
  --archive: #ff5a5f;
  --border: #2c313c;
  --radius: 16px;
  font-size: 16px;
}

* { box-sizing: border-box; }

html, body {
  height: 100%;
  margin: 0;
}

body {
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  overflow: hidden;
}

#app {
  display: flex;
  flex-direction: column;
  height: 100vh;
  height: 100dvh;
}

/* ---------- Topbar ---------- */

.topbar {
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 10px 16px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
  flex-wrap: wrap;
}

.brand {
  font-size: 1.1rem;
  margin: 0;
  white-space: nowrap;
}

.bottom-nav {
  display: flex;
  align-items: center;
  justify-content: space-around;
  gap: 4px;
  padding: 8px 10px calc(8px + env(safe-area-inset-bottom));
  background: var(--surface);
  border-top: 1px solid var(--border);
  flex-shrink: 0;
}

.tab-btn {
  background: transparent;
  border: none;
  color: var(--text-dim);
  width: 46px;
  height: 46px;
  flex-shrink: 0;
  border-radius: 50%;
  font-size: 1.3rem;
  line-height: 1;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
}

.tab-btn:hover { background: var(--surface-2); }

.tab-btn.active {
  background: var(--accent);
}

.topbar-actions {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-left: auto;
}

.photo-count {
  color: var(--text-dim);
  font-size: 0.85rem;
}

.btn {
  border: none;
  border-radius: 10px;
  padding: 9px 16px;
  font-size: 0.9rem;
  cursor: pointer;
  font-weight: 600;
}

.btn.primary {
  background: var(--accent);
  color: white;
}

.btn.primary:hover { filter: brightness(1.1); }

.btn.danger {
  background: transparent;
  color: var(--archive);
  border: 1px solid var(--archive);
}

.btn.secondary {
  background: var(--surface-2);
  color: var(--text);
  border: 1px solid var(--border);
}

.btn.large {
  padding: 14px 28px;
  font-size: 1rem;
  border-radius: 14px;
}

.btn:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

/* ---------- Main / views ---------- */

#main {
  flex: 1;
  overflow: hidden;
  position: relative;
}

.view {
  display: none;
  height: 100%;
  overflow-y: auto;
  padding: 20px;
}

.view.active {
  display: flex;
  flex-direction: column;
  animation: view-in 0.18s ease-out both;
}

@keyframes view-in {
  from { opacity: 0; transform: translateY(6px); }
  to   { opacity: 1; transform: translateY(0); }
}

/* ---------- Welcome ---------- */

.welcome {
  margin: auto;
  max-width: 480px;
  text-align: center;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 14px;
}

.welcome-icon { font-size: 3rem; margin: 0; }
.welcome h2 { margin: 0; font-size: 1.4rem; }
.welcome-sub { color: var(--text-dim); margin: 0; }
.welcome-note { color: var(--text-dim); font-size: 0.8rem; margin-top: 6px; }

/* ---------- Card stack (Sort view) ---------- */

#view-sort {
  align-items: center;
  justify-content: center;
  position: relative;
}

.card-stack {
  position: relative;
  width: min(90vw, 420px);
  height: min(70vh, 560px);
  flex-shrink: 0;
}

.photo-card {
  position: absolute;
  inset: 0;
  border-radius: var(--radius);
  overflow: hidden;
  background: var(--surface-2);
  box-shadow: 0 10px 40px rgba(0,0,0,0.4);
  touch-action: none;
  user-select: none;
  will-change: transform;
  display: flex;
  align-items: center;
  justify-content: center;
}

.photo-card img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  pointer-events: none;
  -webkit-user-drag: none;
  image-orientation: from-image;
}

.photo-card.card-broken img { opacity: 0; }
.photo-card.card-broken::after {
  content: attr(data-broken-msg);
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
  font-size: 0.95rem;
  color: var(--text-dim);
  text-align: center;
  white-space: pre-wrap;
}

.photo-card .swipe-tint {
  position: absolute;
  inset: 0;
  opacity: 0;
  pointer-events: none;
}

.photo-card .swipe-edge {
  position: absolute;
  top: 0;
  bottom: 0;
  width: 72px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1.8rem;
  color: rgba(255, 255, 255, 0.55);
  pointer-events: none;
}

.swipe-edge.archive-hint { left: 0; }
.swipe-edge.keep-hint { right: 0; }

.photo-card .stamp {
  position: absolute;
  top: 28px;
  font-weight: 800;
  font-size: 2.1rem;
  padding: 8px 22px;
  border-radius: 10px;
  border: 4px solid;
  text-transform: uppercase;
  letter-spacing: 2px;
  opacity: 0;
  color: white;
  box-shadow: 0 6px 20px rgba(0, 0, 0, 0.5);
  transform: rotate(-18deg);
}

.stamp.keep {
  right: 24px;
  background: rgba(56, 211, 159, 0.9);
  border-color: var(--keep);
}

.stamp.archive {
  left: 24px;
  background: rgba(255, 90, 95, 0.9);
  border-color: var(--archive);
  transform: rotate(18deg);
}

.photo-card .fav-heart {
  position: absolute;
  top: 50%;
  left: 50%;
  width: 120px;
  height: 120px;
  margin: -60px 0 0 -60px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 6rem;
  color: white;
  filter: drop-shadow(0 2px 12px rgba(0,0,0,0.5));
  opacity: 0;
  pointer-events: none;
}

.photo-card .fav-badge {
  position: absolute;
  bottom: 12px;
  right: 12px;
  font-size: 1.4rem;
  filter: drop-shadow(0 2px 4px rgba(0,0,0,0.6));
}

@keyframes heart-pop {
  0%   { opacity: 0; transform: scale(0.4); }
  30%  { opacity: 1; transform: scale(1.15); }
  50%  { opacity: 1; transform: scale(1); }
  100% { opacity: 0; transform: scale(1); }
}

.fav-heart.pop {
  animation: heart-pop 0.7s ease-out;
}

.fav-heart.on { color: var(--accent); }
.fav-heart.off { color: rgba(255,255,255,0.65); }

.sort-controls {
  display: flex;
  gap: 24px;
  margin-top: 24px;
  flex-shrink: 0;
}

.round-btn {
  width: 56px;
  height: 56px;
  border-radius: 50%;
  border: none;
  font-size: 1.4rem;
  cursor: pointer;
  background: var(--surface-2);
  color: var(--text);
  box-shadow: 0 4px 14px rgba(0,0,0,0.3);
}

.round-btn.archive { color: var(--archive); }
.round-btn.keep { color: var(--keep); }
.round-btn.undo { width: 44px; height: 44px; font-size: 1.1rem; align-self: center; color: var(--text-dim); position: relative; }
.round-btn.undo[data-undo-count]::after {
  content: attr(data-undo-count);
  position: absolute;
  top: -4px; right: -4px;
  min-width: 16px; height: 16px;
  background: var(--text-dim);
  color: var(--bg);
  border-radius: 999px;
  font-size: 0.65rem;
  font-weight: 700;
  line-height: 16px;
  text-align: center;
  padding: 0 3px;
}
.round-btn.undo:disabled { opacity: 0.3; cursor: not-allowed; }

.round-btn:hover { filter: brightness(1.2); }
.round-btn:active { transform: scale(0.92); }

.empty-state {
  margin: auto;
  text-align: center;
  color: var(--text-dim);
  font-size: 1.1rem;
}

/* ---------- Grids ---------- */

.photo-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(140px, 1fr));
  gap: 10px;
}

.photo-grid.empty,
.groups-list.empty {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  text-align: center;
}

.photo-tile {
  position: relative;
  aspect-ratio: 1;
  border-radius: 10px;
  overflow: hidden;
  background: var(--surface-2);
  cursor: pointer;
}

.photo-tile img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  image-orientation: from-image;
}

/* Item 7: hint the browser that offscreen tiles can skip layout+paint */
.photo-tile {
  content-visibility: auto;
  contain-intrinsic-size: 0 140px;
}

/* Item 3: broken / HEIC-unsupported tile state */
.photo-tile.broken img { display: none; }
.photo-tile.broken::after {
  content: attr(data-broken-msg);
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 8px;
  font-size: 0.7rem;
  color: var(--text-dim);
  text-align: center;
  white-space: pre-wrap;
}

.photo-tile .tile-badges {
  position: absolute;
  top: 6px;
  right: 6px;
  display: flex;
  gap: 4px;
  font-size: 1rem;
}

.photo-tile .tile-badges span {
  filter: drop-shadow(0 1px 3px rgba(0,0,0,0.7));
}

.photo-tile .badge-archive {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 22px;
  height: 22px;
  border-radius: 50%;
  background: rgba(220, 53, 53, 0.85);
  color: #fff;
  filter: drop-shadow(0 1px 3px rgba(0,0,0,0.5));
}

/* ── Select-mode tile overlay ── */
.photo-tile .tile-check-wrap {
  display: none;
  position: absolute;
  inset: 0;
  pointer-events: none;
}

.photo-grid.select-mode .photo-tile .tile-check-wrap {
  display: block;
}

.photo-tile .tile-check {
  position: absolute;
  top: 6px;
  left: 6px;
  width: 20px;
  height: 20px;
  border-radius: 50%;
  border: 2px solid rgba(255,255,255,0.85);
  background: rgba(0,0,0,0.25);
}

.photo-tile.tile-selected .tile-check-wrap::before {
  content: '';
  position: absolute;
  inset: 0;
  background: rgba(61,214,140,0.2);
}

.photo-tile.tile-selected .tile-check {
  background: #3dd68c;
  border-color: #3dd68c;
}

.photo-tile.tile-selected .tile-check::after {
  content: '';
  position: absolute;
  top: 3px;
  left: 5px;
  width: 6px;
  height: 10px;
  border: 2px solid #000;
  border-top: none;
  border-left: none;
  transform: rotate(45deg);
}

/* ── Select action bar ── */
.select-bar {
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 16px;
  background: var(--card-bg);
  border-top: 1px solid var(--border);
  z-index: 50;
  gap: 12px;
}

.select-bar-cancel {
  font-size: 14px;
  color: var(--text-dim);
  background: none;
  border: none;
  cursor: pointer;
  padding: 4px 0;
}

.select-bar-count {
  font-size: 14px;
  font-weight: 500;
  color: var(--text);
  flex: 1;
  text-align: center;
}

.select-bar-compare {
  font-size: 14px;
  font-weight: 600;
  color: #3dd68c;
  background: none;
  border: 1.5px solid #3dd68c;
  border-radius: 8px;
  padding: 6px 14px;
  cursor: pointer;
}

.select-bar-compare.disabled {
  color: var(--text-dim);
  border-color: var(--border);
  cursor: default;
}

#view-library {
  position: relative;
}

.photo-tile .tile-zoom-btn {
  position: absolute;
  bottom: 6px;
  right: 6px;
  width: 26px;
  height: 26px;
  border-radius: 50%;
  border: none;
  background: rgba(0, 0, 0, 0.45);
  color: white;
  font-size: 0.9rem;
  line-height: 1;
  cursor: pointer;
}

.photo-tile .tile-zoom-btn:hover {
  background: rgba(0, 0, 0, 0.7);
}

.list-toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 6px;
  color: var(--text-dim);
  font-size: 0.9rem;
  flex-wrap: wrap;
}

.toolbar-btns {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}

#archive-delete-note {
  margin: 0 0 14px;
  text-align: left;
}

.text-dim {
  color: var(--text-dim);
}

/* ---------- Threshold slider ---------- */

.threshold-row {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 2px 14px;
}

.threshold-label {
  color: var(--text-dim);
  font-size: 0.82rem;
  white-space: nowrap;
  flex-shrink: 0;
}

.threshold-slider {
  flex: 1;
  accent-color: var(--accent);
  cursor: pointer;
}

/* ---------- Groups ---------- */

.groups-list {
  display: flex;
  flex-direction: column;
  gap: 18px;
}

.group-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px;
  cursor: pointer;
}

.group-card h3 {
  margin: 0 0 10px;
  font-size: 0.95rem;
  color: var(--text-dim);
  font-weight: 500;
}

.group-thumbs {
  display: flex;
  gap: 8px;
  overflow-x: auto;
}

.group-thumbs img {
  width: 90px;
  height: 90px;
  object-fit: cover;
  border-radius: 8px;
  flex-shrink: 0;
  image-orientation: from-image;
}

/* ---------- Compare modal ---------- */

.modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.7);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 50;
  padding: 20px;
}

.compare-modal {
  background: var(--surface);
  border-radius: var(--radius);
  padding: 20px;
  max-width: 900px;
  width: 100%;
  max-height: 90vh;
  overflow-y: auto;
}

.compare-modal h2 {
  margin-top: 0;
  font-size: 1.1rem;
}

.compare-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(160px, 1fr));
  gap: 12px;
}

.compare-item {
  position: relative;
  border-radius: 12px;
  overflow: hidden;
  border: 3px solid transparent;
  cursor: pointer;
  background: var(--surface-2);
}

.compare-item.best {
  border-color: var(--keep);
}

.compare-item img {
  width: 100%;
  aspect-ratio: 1;
  object-fit: cover;
  display: block;
}

.compare-meta {
  display: flex;
  justify-content: space-between;
  padding: 5px 6px;
  font-size: 0.7rem;
  color: var(--text-dim);
  gap: 4px;
}

.compare-item .best-badge {
  position: absolute;
  top: 6px;
  left: 6px;
  background: var(--keep);
  color: #04231a;
  font-size: 0.7rem;
  font-weight: 700;
  padding: 2px 8px;
  border-radius: 999px;
}

.compare-actions {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
  margin-top: 16px;
}

/* ---------- Compare slider modal ---------- */

.cs-modal {
  background: var(--surface);
  border-radius: var(--radius);
  width: min(92vw, 480px);
  height: 90dvh;
  overflow: hidden;
  display: flex;
  flex-direction: column;
}

.cs-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 16px 12px;
  font-size: 0.95rem;
  font-weight: 600;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}

.cs-close-btn {
  background: transparent;
  border: none;
  color: var(--text-dim);
  font-size: 1rem;
  cursor: pointer;
  padding: 2px 6px;
  border-radius: 6px;
}

.cs-close-btn:hover { background: var(--surface-2); }

.cs-arena {
  position: relative;
  width: 100%;
  flex: 1;
  min-height: 0;
  background: #000;
  overflow: hidden;
  touch-action: none;
  cursor: ew-resize;
}

.cs-side {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: #000;
}

.cs-right { clip-path: inset(0 0 0 50%); }

.cs-img {
  width: 100%;
  height: 100%;
  object-fit: contain;
  pointer-events: none;
  user-select: none;
  -webkit-user-drag: none;
  image-orientation: from-image;
}

.cs-divider {
  position: absolute;
  top: 0; bottom: 0;
  left: 50%;
  width: 2px;
  background: white;
  transform: translateX(-50%);
  pointer-events: none;
  display: flex;
  align-items: center;
  justify-content: center;
}

.cs-handle {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  background: white;
  color: #111;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}

.cs-label {
  position: absolute;
  bottom: 10px;
  background: rgba(0,0,0,0.55);
  color: rgba(255,255,255,0.9);
  font-size: 0.7rem;
  padding: 3px 8px;
  border-radius: 6px;
  pointer-events: none;
  white-space: nowrap;
}

.cs-label-l { left: 10px; }
.cs-label-r { right: 10px; }

.cs-thumb-strip-wrap {
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}

.cs-thumb-hint {
  font-size: 11px;
  color: var(--text-dim);
  text-align: center;
  margin: 6px 0 0;
  padding: 0 14px;
}

.cs-thumb-strip {
  display: flex;
  gap: 6px;
  padding: 8px 14px 10px;
  overflow-x: auto;
}

.cs-thumb {
  position: relative;
  width: 52px;
  height: 52px;
  border-radius: 8px;
  overflow: hidden;
  border: 2px solid transparent;
  padding: 0;
  cursor: pointer;
  flex-shrink: 0;
  background: var(--surface-2);
}

.cs-thumb.cs-active-left  { border-color: var(--keep); }
.cs-thumb.cs-active-right { border-color: var(--accent); }

.cs-thumb img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  image-orientation: from-image;
}

.cs-thumb-label {
  position: absolute;
  bottom: 2px; left: 3px;
  font-size: 0.6rem;
  font-weight: 700;
  color: white;
  background: var(--keep);
  border-radius: 3px;
  padding: 0 3px;
  line-height: 1.4;
}

.cs-thumb-label-r { background: var(--accent); left: auto; right: 3px; }

.cs-actions {
  display: flex;
  gap: 8px;
  padding: 12px 14px 14px;
  flex-shrink: 0;
}

.cs-btn {
  flex: 1;
  padding: 10px 8px;
  border-radius: 10px;
  border: none;
  font-size: 0.88rem;
  font-weight: 600;
  cursor: pointer;
}

.cs-btn-keep    { background: rgba(56,211,159,0.15); color: var(--keep); }
.cs-btn-keep:hover { background: rgba(56,211,159,0.25); }
.cs-btn-neutral { background: var(--surface-2); color: var(--text); border: 1px solid var(--border); }
.cs-btn-neutral:hover { background: var(--surface-2); filter: brightness(1.1); }

/* ---------- Photo viewer ---------- */

.viewer-backdrop {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(6, 7, 10, 0.95);
  z-index: 100;
  flex-direction: column;
}

.viewer-backdrop.open {
  display: flex;
}

.viewer-toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 12px 16px;
  flex-shrink: 0;
}

.viewer-filename {
  color: var(--text-dim);
  font-size: 0.85rem;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.viewer-stage {
  position: relative;
  flex: 1;
  overflow: hidden;
  display: flex;
  align-items: center;
  justify-content: center;
  touch-action: none;
}

.viewer-img {
  max-width: 92%;
  max-height: 92%;
  object-fit: contain;
  transform-origin: center center;
  will-change: transform;
  user-select: none;
  pointer-events: none;
  -webkit-user-drag: none;
}

.viewer-nav-btn {
  position: absolute;
  top: 50%;
  transform: translateY(-50%);
  width: 44px;
  height: 44px;
  border-radius: 50%;
  border: none;
  background: rgba(255, 255, 255, 0.12);
  color: white;
  font-size: 1.6rem;
  line-height: 1;
  cursor: pointer;
}

.viewer-nav-btn:hover { background: rgba(255, 255, 255, 0.22); }
.viewer-nav-btn:disabled { opacity: 0.25; cursor: default; }

.viewer-prev { left: 12px; }
.viewer-next { right: 12px; }

.viewer-zoom-controls {
  display: flex;
  justify-content: center;
  gap: 16px;
  padding: 14px;
  flex-shrink: 0;
}

/* ---------- Loading overlay ---------- */

.loading-overlay {
  position: fixed;
  inset: 0;
  z-index: 150;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 14px;
  background: rgba(6, 7, 10, 0.85);
  color: var(--text-dim);
  font-size: 0.95rem;
}

.spinner {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  border: 4px solid var(--surface-2);
  border-top-color: var(--accent);
  animation: spin 0.8s linear infinite;
}

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

/* ---------- Misc ---------- */

.hidden,
[hidden] { display: none !important; }

@media (max-width: 560px) {
  .topbar { padding: 8px 10px; }
  .brand { font-size: 0.95rem; }
}
