@import url('https://fonts.googleapis.com/css2?family=Playfair+Display:ital,wght@0,400;0,700;1,400&family=Space+Mono:wght@400;700&family=Cormorant+Garamond:ital,wght@0,300;0,600;1,300&display=swap');

:root {
  --ink: #0f0e0a;
  --paper: #f4f0e8;
  --cream: #ede8dc;
  --accent: #c8392b;
  --gold: #c9a84c;
  --muted: #7a7468;
  --stamp-border: #d4cfc4;
  --safe-top: env(safe-area-inset-top);
  --safe-bot: env(safe-area-inset-bottom);
}

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

html, body {
  height: 100%; width: 100%;
  overflow: hidden;
  background: var(--ink);
  font-family: 'Space Mono', monospace;
  color: var(--paper);
  -webkit-font-smoothing: antialiased;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
}

/* ── APP SHELL ── */
#app {
  height: 100dvh;
  display: flex;
  flex-direction: column;
  position: relative;
  overflow: hidden;
}

/* ── SCREENS ── */
.screen {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  opacity: 0;
  pointer-events: none;
  transform: translateY(12px);
  transition: opacity .28s ease, transform .28s ease;
}
.screen.active {
  opacity: 1;
  pointer-events: all;
  transform: translateY(0);
}

/* ── HEADER ── */
.app-header {
  padding: calc(var(--safe-top) + 14px) 20px 12px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  border-bottom: 1px solid rgba(244,240,232,.08);
  flex-shrink: 0;
  background: var(--ink);
  position: relative;
  z-index: 10;
}
.app-title {
  font-family: 'Playfair Display', serif;
  font-size: 22px;
  font-weight: 700;
  letter-spacing: .02em;
  color: var(--paper);
}
.app-title span { color: var(--accent); }
.header-btn {
  background: none; border: none; color: var(--paper);
  cursor: pointer; padding: 6px; display: flex; align-items: center;
  opacity: .8; transition: opacity .2s;
  font-size: 20px;
}
.header-btn:active { opacity: .4; }

/* ── BOTTOM NAV ── */
#bottom-nav {
  position: fixed;
  bottom: 0; left: 0; right: 0;
  padding: 10px 0 calc(var(--safe-bot) + 6px);
  background: rgba(15,14,10,.96);
  backdrop-filter: blur(20px);
  -webkit-backdrop-filter: blur(20px);
  border-top: 1px solid rgba(244,240,232,.1);
  display: flex;
  justify-content: space-around;
  align-items: center;
  z-index: 100;
}
.nav-btn {
  background: none; border: none; color: var(--muted);
  display: flex; flex-direction: column; align-items: center;
  gap: 4px; cursor: pointer; padding: 4px 16px;
  transition: color .2s;
  font-family: 'Space Mono', monospace;
  font-size: 9px;
  letter-spacing: .08em;
  text-transform: uppercase;
}
.nav-btn svg { width: 22px; height: 22px; }
.nav-btn.active { color: var(--paper); }
.nav-btn.active svg { stroke: var(--gold); }

/* ── FAB CAMERA ── */
#fab-camera {
  position: fixed;
  bottom: calc(var(--safe-bot) + 74px);
  left: 50%;
  transform: translateX(-50%);
  width: 64px; height: 64px;
  border-radius: 50%;
  background: var(--accent);
  border: none;
  cursor: pointer;
  display: flex; align-items: center; justify-content: center;
  box-shadow: 0 4px 24px rgba(200,57,43,.5);
  z-index: 200;
  transition: transform .15s ease, box-shadow .15s;
}
#fab-camera:active { transform: translateX(-50%) scale(.92); box-shadow: 0 2px 12px rgba(200,57,43,.4); }
#fab-camera svg { width: 28px; height: 28px; stroke: #fff; }

/* ── GALLERY SCREEN ── */
#screen-gallery {
  background: var(--ink);
  padding-bottom: 150px;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
}
.gallery-scroll {
  padding: 0 16px 20px;
}
.day-group { margin-bottom: 32px; }
.day-label {
  font-family: 'Cormorant Garamond', serif;
  font-size: 13px;
  font-style: italic;
  color: var(--muted);
  letter-spacing: .12em;
  text-transform: uppercase;
  padding: 16px 4px 10px;
  border-bottom: 1px solid rgba(244,240,232,.08);
  margin-bottom: 14px;
}
.stamps-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 10px;
}

/* ── STAMP COMPONENT ── */
.stamp-thumb {
  position: relative;
  cursor: pointer;
  transition: transform .2s ease;
}
.stamp-thumb:active { transform: scale(.94); }
.stamp-frame {
  width: 100%;
  aspect-ratio: 3/4;
  position: relative;
  overflow: visible;
}
.stamp-img-wrap {
  width: 100%;
  height: 100%;
  position: relative;
  overflow: hidden;
}
.stamp-img-wrap img {
  width: 100%; height: 100%;
  object-fit: cover;
  display: block;
}

/* Perforated stamp edge via CSS */
.stamp-perforated {
  position: relative;
}
.stamp-perforated::before {
  content: '';
  position: absolute;
  inset: -5px;
  border-radius: 2px;
  background: 
    radial-gradient(circle at 50% 0, var(--ink) 5px, transparent 5px),
    radial-gradient(circle at 50% 100%, var(--ink) 5px, transparent 5px),
    radial-gradient(circle at 0 50%, var(--ink) 5px, transparent 5px),
    radial-gradient(circle at 100% 50%, var(--ink) 5px, transparent 5px);
  background-size: 14px 14px, 14px 14px, 14px 14px, 14px 14px;
  background-position: top center, bottom center, left center, right center;
  background-repeat: repeat-x, repeat-x, repeat-y, repeat-y;
  z-index: 5;
  pointer-events: none;
}
.stamp-border-inner {
  position: absolute;
  inset: 0;
  border: 2px solid rgba(201,168,76,.25);
  pointer-events: none;
  z-index: 4;
}

/* Stamp tags dot */
.stamp-tag-dot {
  position: absolute;
  top: 4px; right: 4px;
  width: 7px; height: 7px;
  border-radius: 50%;
  background: var(--gold);
  z-index: 10;
  display: none;
}
.stamp-thumb.has-tags .stamp-tag-dot { display: block; }

/* ── EMPTY STATE ── */
.empty-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  height: 50vh;
  gap: 16px;
  color: var(--muted);
  text-align: center;
  padding: 40px;
}
.empty-state svg { opacity: .3; }
.empty-state p {
  font-family: 'Cormorant Garamond', serif;
  font-size: 18px;
  font-style: italic;
  line-height: 1.5;
}
.empty-state small {
  font-size: 11px;
  letter-spacing: .1em;
  text-transform: uppercase;
  opacity: .6;
}

/* ── CAMERA SCREEN ── */
#screen-camera {
  background: #000;
  z-index: 300;
}
#video-wrap {
  flex: 1;
  position: relative;
  overflow: hidden;
  display: flex;
  align-items: center;
  justify-content: center;
}
#camera-video {
  width: 100%; height: 100%;
  object-fit: cover;
}

/* Stamp viewfinder overlay */
#viewfinder {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  pointer-events: none;
}
.vf-shade {
  position: absolute;
  inset: 0;
  background: rgba(0,0,0,.55);
}
.vf-stamp {
  position: relative;
  z-index: 2;
  width: min(72vw, 320px);
  aspect-ratio: 3/4;
  background: transparent;
}
.vf-stamp::before {
  content: '';
  position: absolute;
  inset: -8px;
  box-shadow: 0 0 0 100vmax rgba(0,0,0,.55);
  z-index: -1;
}

/* SVG stamp border for viewfinder */
.vf-stamp-svg {
  position: absolute;
  inset: -12px;
  width: calc(100% + 24px);
  height: calc(100% + 24px);
  pointer-events: none;
}

#camera-controls {
  padding: 24px 32px calc(var(--safe-bot) + 20px);
  background: #000;
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-shrink: 0;
}
#shutter-btn {
  width: 72px; height: 72px;
  border-radius: 50%;
  border: 3px solid #fff;
  background: none;
  cursor: pointer;
  display: flex; align-items: center; justify-content: center;
  transition: transform .12s;
  position: relative;
}
#shutter-btn::after {
  content: '';
  width: 58px; height: 58px;
  border-radius: 50%;
  background: #fff;
  transition: transform .12s;
}
#shutter-btn:active::after { transform: scale(.88); }
#flip-btn, #close-camera-btn {
  width: 44px; height: 44px;
  background: rgba(255,255,255,.12);
  border: none; border-radius: 50%;
  color: #fff; cursor: pointer;
  display: flex; align-items: center; justify-content: center;
  backdrop-filter: blur(8px);
}
#flip-btn svg, #close-camera-btn svg { width: 20px; height: 20px; stroke: #fff; }

/* Flash animation */
@keyframes flash {
  0% { opacity: 0; }
  20% { opacity: 1; }
  100% { opacity: 0; }
}
.flash-overlay {
  position: fixed; inset: 0;
  background: #fff;
  pointer-events: none;
  z-index: 9999;
  opacity: 0;
}
.flash-overlay.flashing { animation: flash .4s ease-out forwards; }

/* ── SAVE STAMP SCREEN ── */
#screen-save {
  background: var(--ink);
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding-bottom: calc(var(--safe-bot) + 20px);
  z-index: 400;
}
.save-preview {
  display: flex;
  justify-content: center;
  padding: 24px 40px 20px;
}
.save-stamp-frame {
  width: min(64vw, 260px);
  aspect-ratio: 3/4;
  position: relative;
}
.save-stamp-frame img {
  width: 100%; height: 100%;
  object-fit: cover;
  display: block;
}
.save-form {
  padding: 0 24px;
}
.form-label {
  font-size: 10px;
  letter-spacing: .14em;
  text-transform: uppercase;
  color: var(--muted);
  margin-bottom: 8px;
  display: block;
  font-family: 'Space Mono', monospace;
}
.form-input, .form-textarea {
  width: 100%;
  background: rgba(244,240,232,.06);
  border: 1px solid rgba(244,240,232,.14);
  border-radius: 8px;
  color: var(--paper);
  font-family: 'Cormorant Garamond', serif;
  font-size: 18px;
  padding: 12px 16px;
  outline: none;
  transition: border-color .2s;
  -webkit-appearance: none;
  caret-color: var(--gold);
  margin-bottom: 20px;
}
.form-input:focus, .form-textarea:focus {
  border-color: rgba(201,168,76,.5);
}
.form-textarea { resize: none; height: 80px; font-style: italic; }
.form-input::placeholder, .form-textarea::placeholder { color: var(--muted); font-style: italic; }

/* Tags */
.tags-wrap {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-bottom: 8px;
}
.tag-chip {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  background: rgba(201,168,76,.15);
  border: 1px solid rgba(201,168,76,.3);
  color: var(--gold);
  border-radius: 20px;
  padding: 4px 10px 4px 12px;
  font-size: 12px;
  font-family: 'Space Mono', monospace;
}
.tag-chip button {
  background: none; border: none; color: var(--gold);
  cursor: pointer; padding: 0; line-height: 1; font-size: 14px;
}
.tag-input-row {
  display: flex; gap: 8px; margin-bottom: 20px;
}
.tag-input-row .form-input { margin-bottom: 0; flex: 1; }
.tag-add-btn {
  background: rgba(201,168,76,.2);
  border: 1px solid rgba(201,168,76,.35);
  color: var(--gold);
  border-radius: 8px;
  padding: 0 16px;
  cursor: pointer;
  font-family: 'Space Mono', monospace;
  font-size: 20px;
  line-height: 1;
  flex-shrink: 0;
}

.save-actions {
  display: flex; gap: 12px; padding: 20px 24px 0;
}
.btn-primary, .btn-secondary {
  flex: 1; padding: 16px;
  border-radius: 10px;
  font-family: 'Space Mono', monospace;
  font-size: 12px;
  letter-spacing: .1em;
  text-transform: uppercase;
  cursor: pointer;
  border: none;
  transition: opacity .2s, transform .15s;
}
.btn-primary:active, .btn-secondary:active { transform: scale(.96); opacity: .8; }
.btn-primary { background: var(--accent); color: #fff; }
.btn-secondary { background: rgba(244,240,232,.08); color: var(--muted); border: 1px solid rgba(244,240,232,.12); }

/* ── COLLECTIONS SCREEN ── */
#screen-collections {
  background: var(--ink);
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding-bottom: 150px;
}
.collections-list {
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 14px;
}
.collection-card {
  background: rgba(244,240,232,.04);
  border: 1px solid rgba(244,240,232,.1);
  border-radius: 12px;
  padding: 16px;
  cursor: pointer;
  transition: background .2s;
  display: flex;
  align-items: center;
  gap: 16px;
}
.collection-card:active { background: rgba(244,240,232,.08); }
.collection-preview {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 3px;
  width: 70px; height: 70px;
  border-radius: 8px;
  overflow: hidden;
  flex-shrink: 0;
}
.collection-preview img {
  width: 100%; height: 100%; object-fit: cover;
}
.collection-preview-empty {
  width: 70px; height: 70px;
  background: rgba(244,240,232,.06);
  border-radius: 8px;
  display: flex; align-items: center; justify-content: center;
  flex-shrink: 0;
  font-size: 24px;
}
.collection-info { flex: 1; min-width: 0; }
.collection-name {
  font-family: 'Playfair Display', serif;
  font-size: 17px;
  font-weight: 700;
  margin-bottom: 4px;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.collection-meta {
  font-size: 11px;
  color: var(--muted);
  letter-spacing: .08em;
}
.btn-new-collection {
  margin: 4px 16px 16px;
  width: calc(100% - 32px);
  padding: 14px;
  background: none;
  border: 1px dashed rgba(244,240,232,.2);
  border-radius: 10px;
  color: var(--muted);
  font-family: 'Space Mono', monospace;
  font-size: 11px;
  letter-spacing: .1em;
  text-transform: uppercase;
  cursor: pointer;
  transition: border-color .2s, color .2s;
}
.btn-new-collection:active { border-color: var(--gold); color: var(--gold); }

/* ── STAMP DETAIL ── */
#screen-detail {
  background: var(--ink);
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding-bottom: calc(var(--safe-bot) + 20px);
  z-index: 250;
}
.detail-stamp-wrap {
  display: flex;
  justify-content: center;
  padding: 24px 40px;
}
.detail-stamp-frame {
  width: min(70vw, 280px);
  aspect-ratio: 3/4;
  position: relative;
}
.detail-stamp-frame img {
  width: 100%; height: 100%; object-fit: cover; display: block;
}
.detail-info { padding: 0 24px; }
.detail-date {
  font-size: 11px;
  color: var(--muted);
  letter-spacing: .12em;
  text-transform: uppercase;
  margin-bottom: 10px;
  font-family: 'Space Mono', monospace;
}
.detail-caption {
  font-family: 'Cormorant Garamond', serif;
  font-size: 20px;
  font-style: italic;
  line-height: 1.5;
  margin-bottom: 16px;
  color: var(--cream);
}
.detail-tags {
  display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 24px;
}
.detail-tag {
  background: rgba(201,168,76,.12);
  border: 1px solid rgba(201,168,76,.25);
  color: var(--gold);
  border-radius: 20px;
  padding: 4px 12px;
  font-size: 11px;
  font-family: 'Space Mono', monospace;
}
.detail-actions {
  display: flex; gap: 10px; padding: 0 24px;
}
.btn-danger {
  flex: 1; padding: 14px;
  border-radius: 10px;
  border: 1px solid rgba(200,57,43,.3);
  background: rgba(200,57,43,.1);
  color: var(--accent);
  font-family: 'Space Mono', monospace;
  font-size: 11px;
  letter-spacing: .1em;
  text-transform: uppercase;
  cursor: pointer;
}

/* ── SEARCH / FILTER BAR ── */
.search-bar {
  padding: 10px 16px 2px;
  display: flex;
  gap: 8px;
  flex-shrink: 0;
}
.search-input {
  flex: 1;
  background: rgba(244,240,232,.06);
  border: 1px solid rgba(244,240,232,.1);
  border-radius: 24px;
  color: var(--paper);
  font-family: 'Space Mono', monospace;
  font-size: 13px;
  padding: 9px 16px;
  outline: none;
  caret-color: var(--gold);
  -webkit-appearance: none;
}
.search-input::placeholder { color: var(--muted); }

/* ── MODAL ── */
.modal-overlay {
  position: fixed; inset: 0;
  background: rgba(0,0,0,.7);
  z-index: 500;
  display: flex; align-items: flex-end;
  opacity: 0; pointer-events: none;
  transition: opacity .25s;
}
.modal-overlay.open { opacity: 1; pointer-events: all; }
.modal-sheet {
  background: #1a1915;
  border-radius: 20px 20px 0 0;
  padding: 24px 24px calc(var(--safe-bot) + 24px);
  width: 100%;
  transform: translateY(100%);
  transition: transform .3s cubic-bezier(.22,1,.36,1);
}
.modal-overlay.open .modal-sheet { transform: translateY(0); }
.modal-title {
  font-family: 'Playfair Display', serif;
  font-size: 20px;
  margin-bottom: 20px;
}
.modal-handle {
  width: 36px; height: 4px;
  background: rgba(244,240,232,.2);
  border-radius: 2px;
  margin: -8px auto 20px;
}

/* ── TOAST ── */
.toast {
  position: fixed;
  bottom: calc(var(--safe-bot) + 90px);
  left: 50%; transform: translateX(-50%) translateY(20px);
  background: rgba(30,29,24,.95);
  border: 1px solid rgba(244,240,232,.12);
  border-radius: 24px;
  padding: 10px 20px;
  font-size: 13px;
  color: var(--paper);
  backdrop-filter: blur(12px);
  z-index: 1000;
  opacity: 0;
  transition: opacity .2s, transform .2s;
  white-space: nowrap;
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }

/* ── SCROLLBAR ── */
::-webkit-scrollbar { width: 3px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: rgba(244,240,232,.15); border-radius: 2px; }

/* ── COLLECTION DETAIL ── */
#screen-collection-detail {
  background: var(--ink);
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding-bottom: 150px;
  z-index: 300;
}
