:root {
  --bg: #0f0f12;
  --surface: #1a1a20;
  --surface-2: #24242c;
  --text: #f2f2f5;
  --muted: #9a9aa6;
  --accent: #7c8cff;
  --accent-press: #6675e6;
  --danger: #ff6b6b;
  --radius: 16px;
  --radius-inner: 10px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
  overscroll-behavior: none;
}

body { touch-action: manipulation; }

.view { min-height: 100%; display: flex; flex-direction: column; }
/* Phone-shaped app: cap width and center on desktop so thumbnails don't balloon. */
#library { width: 100%; max-width: 560px; margin: 0 auto; }
[hidden] { display: none !important; }

/* Buttons */
.btn {
  font: inherit;
  font-weight: 600;
  border: none;
  border-radius: var(--radius);
  padding: 15px 20px;
  cursor: pointer;
  transition: transform .08s ease, background .15s ease, opacity .15s ease;
  min-height: 48px;
}
.btn:active { transform: scale(0.96); }
.btn.primary { background: var(--accent); color: #0b0b12; }
.btn.primary:active { background: var(--accent-press); }
.btn.primary:disabled { opacity: 0.4; cursor: default; transform: none; }
.btn.ghost { background: var(--surface-2); color: var(--text); }
.btn.ghost:active { background: #2e2e38; }

.icon-btn {
  background: none; border: none; color: var(--muted);
  padding: 8px; border-radius: 12px; cursor: pointer;
  min-width: 40px; min-height: 40px; display: grid; place-items: center;
  transition: transform .08s ease, color .15s ease;
}
.icon-btn:active { transform: scale(0.92); color: var(--text); }

/* Onboarding */
#onboard { justify-content: center; padding: calc(28px + var(--safe-top)) 24px calc(28px + var(--safe-bottom)); }
.onboard-inner { max-width: 420px; margin: 0 auto; width: 100%; }
.mark { color: var(--accent); margin-bottom: 18px; }
#onboard h1 { font-size: 34px; margin: 0 0 10px; letter-spacing: -0.02em; }
.lede { color: var(--text); font-size: 17px; line-height: 1.5; margin: 0 0 18px; }
.lede.small { color: var(--muted); font-size: 14px; }
.honesty {
  background: var(--surface); border: 1px solid #2c2c36;
  border-radius: var(--radius); padding: 16px 18px; margin: 0 0 18px;
  font-size: 14px; line-height: 1.55; color: var(--muted);
}
.honesty strong { color: var(--text); }
#startBtn { width: 100%; margin-top: 6px; }

/* Library */
.topbar {
  display: flex; align-items: center; justify-content: space-between;
  padding: calc(16px + var(--safe-top)) 18px 12px;
}
.topbar h2 { font-size: 22px; margin: 0; letter-spacing: -0.01em; }
.grid {
  flex: 1; display: grid; grid-template-columns: repeat(3, 1fr);
  gap: 4px; padding: 4px 8px 8px; align-content: start; overflow-y: auto;
}
.thumb {
  position: relative; aspect-ratio: 1; border-radius: var(--radius-inner);
  overflow: hidden; background: var(--surface); cursor: pointer;
  transition: transform .08s ease;
}
.thumb:active { transform: scale(0.97); }
.thumb img { width: 100%; height: 100%; object-fit: cover; display: block; pointer-events: none; }
.thumb .check {
  position: absolute; top: 7px; right: 7px; width: 24px; height: 24px;
  border-radius: 50%; background: rgba(0,0,0,.45); border: 2px solid #fff;
  display: grid; place-items: center; opacity: 0; transform: scale(.6);
  transition: opacity .12s ease, transform .12s ease;
}
.thumb.selected .check { opacity: 1; transform: scale(1); background: var(--accent); border-color: var(--accent); }
.thumb.selected { outline: 3px solid var(--accent); outline-offset: -3px; }
.thumb .check svg { width: 14px; height: 14px; color: #0b0b12; }

.empty { flex: 1; display: grid; place-content: center; text-align: center; color: var(--muted); gap: 4px; padding: 40px; }
.empty p { margin: 0; }
.small { font-size: 13px; color: var(--muted); }

.bottombar {
  display: flex; gap: 10px; padding: 12px 16px calc(14px + var(--safe-bottom));
  border-top: 1px solid #22222a; background: var(--bg);
}
.bottombar .btn { flex: 1; }

/* Viewer */
.viewer { position: fixed; inset: 0; background: #000; z-index: 20; }
.lock-btn {
  position: absolute; top: calc(12px + var(--safe-top)); left: 12px; z-index: 22;
  width: 44px; height: 44px; border-radius: 50%; border: none;
  background: rgba(0,0,0,.5); color: #fff; cursor: pointer;
  display: grid; place-items: center; backdrop-filter: blur(6px);
  transition: transform .08s ease, background .15s ease;
}
.lock-btn:active { transform: scale(0.92); background: rgba(0,0,0,.7); }
.stage {
  position: absolute; inset: 0; display: flex;
  transition: transform .28s cubic-bezier(.22,.61,.36,1);
  will-change: transform;
}
.stage.dragging { transition: none; }
.slide { min-width: 100%; height: 100%; display: grid; place-items: center; }
.slide img { max-width: 100%; max-height: 100%; object-fit: contain; -webkit-user-select: none; user-select: none; -webkit-user-drag: none; }
.dots { position: absolute; bottom: calc(16px + var(--safe-bottom)); left: 0; right: 0; display: flex; gap: 7px; justify-content: center; z-index: 22; }
.dots span { width: 7px; height: 7px; border-radius: 50%; background: rgba(255,255,255,.35); transition: background .2s ease, transform .2s ease; }
.dots span.on { background: #fff; transform: scale(1.25); }
.dots:only-child, .dots.single { display: none; }

/* PIN modal */
.pin-modal {
  position: fixed; inset: 0; z-index: 40; background: rgba(8,8,10,.72);
  backdrop-filter: blur(10px); display: grid; place-items: center; padding: 20px;
}
.pin-card {
  width: 100%; max-width: 320px; background: var(--surface);
  border-radius: 22px; padding: 26px 22px calc(20px + var(--safe-bottom)); text-align: center;
  border: 1px solid #2c2c36;
}
.pin-title { font-size: 18px; font-weight: 600; margin: 0 0 18px; }
.pin-dots { display: flex; gap: 14px; justify-content: center; margin-bottom: 14px; }
.pin-dots span { width: 13px; height: 13px; border-radius: 50%; background: var(--surface-2); border: 1.5px solid #3a3a46; transition: background .12s ease, transform .12s ease; }
.pin-dots span.filled { background: var(--accent); border-color: var(--accent); transform: scale(1.05); }
.pin-error { color: var(--danger); font-size: 13px; margin: 0 0 8px; height: 16px; }
.pad { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; margin-top: 8px; }
.key {
  font: inherit; font-size: 22px; font-variant-numeric: tabular-nums;
  background: var(--surface-2); color: var(--text); border: none;
  border-radius: 14px; padding: 16px 0; cursor: pointer; min-height: 56px;
  transition: transform .07s ease, background .12s ease;
}
.key:active { transform: scale(0.94); background: #33333d; }
.key.ghostkey { background: none; font-size: 15px; color: var(--muted); }
.key.ghostkey:active { background: var(--surface-2); }

@keyframes shake { 0%,100%{transform:translateX(0)} 20%,60%{transform:translateX(-8px)} 40%,80%{transform:translateX(8px)} }
.pin-card.shake { animation: shake .35s ease; }

@media (prefers-reduced-motion: reduce) {
  * { transition: none !important; animation: none !important; }
}
