*, *::before, *::after {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
  -webkit-tap-highlight-color: transparent;
}

:root {
  --bg: #000;
  --surface: rgba(30, 30, 30, 0.72);
  --surface-elevated: rgba(44, 44, 46, 0.82);
  --surface-solid: #1c1c1e;
  --surface-secondary: rgba(44, 44, 46, 0.65);
  --surface-tertiary: rgba(58, 58, 60, 0.5);
  --fill: rgba(120, 120, 128, 0.36);
  --fill-light: rgba(120, 120, 128, 0.2);
  --fill-ultralight: rgba(120, 120, 128, 0.08);
  --separator: rgba(84, 84, 88, 0.36);
  --text: #fff;
  --text-secondary: rgba(235, 235, 245, 0.6);
  --text-tertiary: rgba(235, 235, 245, 0.3);
  --accent: #0A84FF;
  --accent-hover: #409CFF;
  --accent-light: rgba(10, 132, 255, 0.15);
  --danger: #FF453A;
  --green: #30D158;
  --orange: #FF9F0A;
  --radius: 12px;
  --radius-lg: 22px;
  --radius-sm: 8px;
  --radius-xs: 6px;
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-top: env(safe-area-inset-top, 0px);
  --spring: cubic-bezier(0.34, 1.56, 0.64, 1);
  --ease-out: cubic-bezier(0.25, 1, 0.5, 1);
  --ease-in-out: cubic-bezier(0.45, 0, 0.55, 1);
}

html, body {
  width: 100%;
  height: 100%;
  overflow: hidden;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, 'SF Pro Display', 'SF Pro Text', system-ui, 'Helvetica Neue', sans-serif;
  font-feature-settings: 'kern' 1, 'liga' 1;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  touch-action: manipulation;
  user-select: none;
  -webkit-user-select: none;
}

/* ===== Screens ===== */
.screen {
  position: fixed;
  inset: 0;
  display: none;
  flex-direction: column;
}
.screen.active {
  display: flex;
  animation: screenIn 0.5s var(--ease-out);
}

@keyframes screenIn {
  from { opacity: 0; transform: scale(0.97); filter: blur(8px); }
  60% { filter: blur(0); }
  to { opacity: 1; transform: scale(1); filter: blur(0); }
}

/* ===== Camera Screen ===== */
#camera-screen {
  background: var(--bg);
}

#camera-feed {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

#camera-controls {
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
  display: flex;
  align-items: center;
  justify-content: space-evenly;
  padding: 32px 44px;
  padding-bottom: calc(32px + var(--safe-bottom));
  background: linear-gradient(transparent 0%, rgba(0,0,0,0.35) 30%, rgba(0,0,0,0.65) 100%);
}

.cam-btn {
  width: 48px;
  height: 48px;
  border: none;
  background: rgba(255,255,255,0.1);
  border-radius: 50%;
  color: white;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  backdrop-filter: blur(30px) saturate(180%);
  -webkit-backdrop-filter: blur(30px) saturate(180%);
  transition: transform 0.3s var(--spring), background 0.2s var(--ease-out);
}
.cam-btn svg {
  width: 22px;
  height: 22px;
  opacity: 0.9;
}
.cam-btn:active {
  transform: scale(0.85);
  background: rgba(255,255,255,0.2);
}

.capture-btn {
  width: 80px;
  height: 80px;
  border: 4px solid rgba(255,255,255,0.85);
  border-radius: 50%;
  background: transparent;
  cursor: pointer;
  position: relative;
  transition: transform 0.2s var(--spring);
}
.capture-btn::after {
  content: '';
  position: absolute;
  inset: 4px;
  border-radius: 50%;
  background: white;
  transition: transform 0.2s var(--spring), opacity 0.15s;
}
.capture-btn:active {
  transform: scale(0.92);
}
.capture-btn:active::after {
  transform: scale(0.78);
  opacity: 0.85;
}

/* ===== Editor Screen ===== */
#editor-screen {
  background: #000;
}

/* Top navigation bar — tight, translucent */
#editor-toolbar {
  display: flex;
  align-items: center;
  gap: 0;
  padding: 4px 6px;
  padding-top: calc(4px + var(--safe-top));
  background: rgba(0, 0, 0, 0.55);
  backdrop-filter: blur(40px) saturate(200%);
  -webkit-backdrop-filter: blur(40px) saturate(200%);
  z-index: 10;
  flex-shrink: 0;
  border-bottom: 0.33px solid rgba(255,255,255,0.06);
}

#tool-buttons {
  display: flex;
  gap: 0;
  flex: 1;
  justify-content: center;
}

.tool-btn {
  width: 44px;
  height: 44px;
  border: none;
  background: transparent;
  color: var(--text-secondary);
  border-radius: var(--radius);
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: color 0.25s var(--ease-out), background 0.25s var(--ease-out), transform 0.3s var(--spring);
  position: relative;
}
.tool-btn svg {
  width: 22px;
  height: 22px;
  transition: transform 0.3s var(--spring);
}
.tool-btn:active {
  transform: scale(0.82);
}
.tool-btn.active {
  color: var(--accent);
  background: var(--accent-light);
}
.tool-btn.active svg {
  transform: scale(1.1);
}

/* Back button — text style like iOS nav */
#btn-back {
  color: var(--accent);
}
#btn-back:active {
  color: var(--accent-hover);
}

/* Save button — filled pill */
.save-btn {
  background: var(--accent);
  color: white !important;
  border-radius: 100px;
  width: auto;
  padding: 0 16px;
  height: 34px;
  font-size: 15px;
  font-weight: 600;
  letter-spacing: -0.02em;
  gap: 5px;
}
.save-btn svg {
  width: 17px;
  height: 17px;
}
.save-btn:active {
  background: var(--accent-hover);
  transform: scale(0.95);
}
.save-btn.active {
  background: var(--accent);
  color: white !important;
}

/* Canvas container */
#editor-canvas-container {
  flex: 1;
  position: relative;
  overflow: hidden;
  display: flex;
  align-items: center;
  justify-content: center;
  touch-action: none;
  background: #000;
}

#editor-canvas {
  max-width: 100%;
  max-height: 100%;
  display: block;
}

#sticker-layer {
  position: absolute;
  inset: 0;
  pointer-events: none;
  overflow: hidden;
}

/* Stickers */
.sticker {
  position: absolute;
  pointer-events: auto;
  touch-action: none;
  cursor: grab;
  display: flex;
  align-items: center;
  justify-content: center;
  transform-origin: center center;
  transition: filter 0.2s;
}
.sticker:active {
  cursor: grabbing;
}
.sticker.emoji {
  font-size: 48px;
  line-height: 1;
  filter: drop-shadow(0 2px 8px rgba(0,0,0,0.3));
}
.sticker.text-sticker {
  font-size: 28px;
  font-weight: 700;
  letter-spacing: -0.02em;
  text-shadow: 0 1px 6px rgba(0,0,0,0.5), 0 4px 16px rgba(0,0,0,0.25);
  white-space: nowrap;
  padding: 4px 10px;
}
.sticker .delete-btn {
  display: none;
  position: absolute;
  top: -14px;
  right: -14px;
  width: 28px;
  height: 28px;
  border-radius: 50%;
  background: rgba(255, 69, 58, 0.9);
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
  color: white;
  border: 2px solid rgba(0,0,0,0.2);
  font-size: 14px;
  font-weight: 600;
  line-height: 1;
  cursor: pointer;
  align-items: center;
  justify-content: center;
  z-index: 5;
  box-shadow: 0 2px 12px rgba(0,0,0,0.4);
  transition: transform 0.3s var(--spring);
}
.sticker .delete-btn:active {
  transform: scale(0.8);
}
.sticker.selected .delete-btn {
  display: flex;
  animation: popIn 0.35s var(--spring);
}
.sticker.selected {
  outline: 2px solid var(--accent);
  outline-offset: 8px;
  border-radius: 6px;
}

@keyframes popIn {
  from { transform: scale(0); opacity: 0; }
  to { transform: scale(1); opacity: 1; }
}

/* ===== Crop overlay ===== */
#crop-overlay {
  position: absolute;
  inset: 0;
  z-index: 5;
}
#crop-overlay::before {
  content: '';
  position: absolute;
  inset: 0;
  background: rgba(0,0,0,0.5);
  pointer-events: none;
}
#crop-box {
  position: absolute;
  border: 1.5px solid rgba(255,255,255,0.9);
  box-shadow: 0 0 0 9999px rgba(0,0,0,0.55);
  z-index: 1;
  touch-action: none;
  transition: border-color 0.2s;
}
/* Rule-of-thirds grid */
#crop-box::before {
  content: '';
  position: absolute;
  inset: 0;
  background:
    linear-gradient(to right, rgba(255,255,255,0.2) 1px, transparent 1px),
    linear-gradient(to bottom, rgba(255,255,255,0.2) 1px, transparent 1px);
  background-size: 33.333% 33.333%;
  pointer-events: none;
}
.crop-handle {
  position: absolute;
  width: 32px;
  height: 32px;
  z-index: 2;
}
.crop-handle::before {
  content: '';
  position: absolute;
  background: white;
  border-radius: 2px;
}
.crop-handle.tl { top: -4px; left: -4px; cursor: nw-resize; }
.crop-handle.tr { top: -4px; right: -4px; cursor: ne-resize; }
.crop-handle.bl { bottom: -4px; left: -4px; cursor: sw-resize; }
.crop-handle.br { bottom: -4px; right: -4px; cursor: se-resize; }

.crop-handle.tl::before, .crop-handle.br::before { width: 24px; height: 3px; top: 0; left: 0; }
.crop-handle.tl::after, .crop-handle.br::after { content:''; position:absolute; background:white; border-radius:2px; width: 3px; height: 24px; top: 0; left: 0; }
.crop-handle.tr::before, .crop-handle.bl::before { width: 24px; height: 3px; top: 0; right: 0; }
.crop-handle.tr::after, .crop-handle.bl::after { content:''; position:absolute; background:white; border-radius:2px; width: 3px; height: 24px; top: 0; right: 0; }
.crop-handle.bl::before { top: auto; bottom: 0; }
.crop-handle.bl::after { top: auto; bottom: 0; }
.crop-handle.br::before { top: auto; bottom: 0; left: auto; right: 0; }
.crop-handle.br::after { top: auto; bottom: 0; left: auto; right: 0; }

/* ===== Sub-toolbars — Bottom sheets ===== */
.sub-toolbar {
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
  background: rgba(22, 22, 22, 0.75);
  backdrop-filter: blur(50px) saturate(200%);
  -webkit-backdrop-filter: blur(50px) saturate(200%);
  padding: 6px 16px 16px;
  padding-bottom: calc(16px + var(--safe-bottom));
  z-index: 10;
  border-top: 0.33px solid rgba(255,255,255,0.08);
  border-radius: 20px 20px 0 0;
  transition: transform 0.4s var(--ease-out), opacity 0.3s var(--ease-out);
}
.sub-toolbar.hidden {
  display: none;
}

/* Sheet pull indicator */
.sub-toolbar::before {
  content: '';
  display: block;
  width: 36px;
  height: 5px;
  background: rgba(255, 255, 255, 0.18);
  border-radius: 100px;
  margin: 4px auto 14px;
}

/* ===== Draw options ===== */
#draw-options {
  display: flex;
  flex-direction: column;
  gap: 16px;
  align-items: center;
}
#draw-options.hidden {
  display: none;
}

#brush-sizes {
  display: flex;
  gap: 4px;
  align-items: center;
  padding: 4px;
  background: var(--fill-ultralight);
  border-radius: var(--radius);
}
.size-btn {
  width: 44px;
  height: 44px;
  border: none;
  background: transparent;
  border-radius: var(--radius-sm);
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: background 0.2s var(--ease-out), transform 0.3s var(--spring);
}
.size-btn span {
  display: block;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.7);
  transition: transform 0.3s var(--spring), background 0.2s;
}
.size-btn.active {
  background: var(--accent-light);
}
.size-btn.active span {
  background: var(--accent);
  transform: scale(1.2);
}
.size-btn:active {
  transform: scale(0.85);
}

#color-palette, #text-colors {
  display: flex;
  gap: 10px;
  flex-wrap: wrap;
  justify-content: center;
}
.color-btn {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  border: 2.5px solid transparent;
  cursor: pointer;
  transition: transform 0.3s var(--spring), border-color 0.2s, box-shadow 0.2s;
  box-shadow: inset 0 0 0 0.5px rgba(255,255,255,0.12);
}
.color-btn:active {
  transform: scale(0.82);
}
.color-btn.active {
  border-color: var(--text);
  transform: scale(1.22);
  box-shadow: 0 0 0 2px var(--bg), 0 0 0 3.5px rgba(255,255,255,0.8);
}

#btn-undo-draw {
  align-self: flex-end;
  color: var(--text-secondary);
}

/* ===== Emoji picker ===== */
#emoji-grid {
  display: grid;
  grid-template-columns: repeat(7, 1fr);
  gap: 2px;
  max-height: 240px;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  scrollbar-width: none;
}
#emoji-grid::-webkit-scrollbar {
  display: none;
}
#emoji-grid button {
  font-size: 28px;
  line-height: 1;
  padding: 8px;
  border: none;
  background: transparent;
  border-radius: var(--radius-sm);
  cursor: pointer;
  transition: background 0.15s, transform 0.3s var(--spring);
}
#emoji-grid button:active {
  background: var(--fill-light);
  transform: scale(1.25);
}

/* ===== Filter options ===== */
#filter-list {
  display: flex;
  gap: 14px;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  padding: 2px 0 6px;
  scrollbar-width: none;
}
#filter-list::-webkit-scrollbar {
  display: none;
}
.filter-preview {
  flex-shrink: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 7px;
  cursor: pointer;
  border: none;
  background: transparent;
  color: var(--text);
  transition: transform 0.3s var(--spring);
}
.filter-preview:active {
  transform: scale(0.9);
}
.filter-preview canvas {
  width: 72px;
  height: 72px;
  border-radius: var(--radius);
  border: 2.5px solid transparent;
  transition: border-color 0.25s var(--ease-out), box-shadow 0.25s var(--ease-out);
}
.filter-preview.active canvas {
  border-color: var(--accent);
  box-shadow: 0 0 0 1px var(--accent), 0 2px 12px rgba(10, 132, 255, 0.25);
}
.filter-preview span {
  font-size: 11px;
  font-weight: 500;
  color: var(--text-tertiary);
  letter-spacing: 0.02em;
  transition: color 0.2s;
}
.filter-preview.active span {
  color: var(--accent);
  font-weight: 600;
}

/* ===== Crop actions — Segmented control ===== */
#crop-actions {
  display: flex;
  align-items: center;
  justify-content: space-between;
}
#crop-actions.hidden {
  display: none;
}
#crop-ratios {
  display: flex;
  gap: 0;
  background: var(--fill-ultralight);
  border-radius: var(--radius-sm);
  padding: 2px;
  position: relative;
}
.ratio-btn {
  padding: 7px 14px;
  border: none;
  border-radius: var(--radius-xs);
  background: transparent;
  color: var(--text-secondary);
  font-size: 13px;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.3s var(--ease-out);
  position: relative;
  z-index: 1;
}
.ratio-btn.active {
  background: rgba(10, 132, 255, 0.9);
  color: white;
  font-weight: 600;
  box-shadow: 0 1px 6px rgba(10, 132, 255, 0.35);
}

.action-btn {
  padding: 8px 20px;
  border: none;
  border-radius: 100px;
  background: var(--fill-light);
  color: var(--text);
  font-size: 15px;
  font-weight: 600;
  cursor: pointer;
  letter-spacing: -0.01em;
  transition: transform 0.3s var(--spring), background 0.15s, opacity 0.15s;
}
.action-btn:active {
  transform: scale(0.92);
  opacity: 0.85;
}
.action-btn.primary {
  background: var(--accent);
  color: white;
  box-shadow: 0 2px 16px rgba(10, 132, 255, 0.3);
}
.action-btn.primary:active {
  background: var(--accent-hover);
}

/* ===== Text options ===== */
#text-options {
  display: flex;
  align-items: center;
  gap: 12px;
  flex-wrap: wrap;
  justify-content: center;
}
#text-options.hidden {
  display: none;
}
#text-input {
  flex: 1;
  min-width: 120px;
  padding: 11px 18px;
  border: none;
  border-radius: 100px;
  background: rgba(118, 118, 128, 0.12);
  color: var(--text);
  font-size: 16px;
  font-weight: 400;
  letter-spacing: -0.01em;
  outline: none;
  transition: box-shadow 0.3s var(--ease-out), background 0.2s;
}
#text-input:focus {
  box-shadow: 0 0 0 2px var(--accent);
  background: rgba(118, 118, 128, 0.18);
}
#text-input::placeholder {
  color: var(--text-tertiary);
}

/* ===== Toast — Apple HUD ===== */
#toast {
  position: fixed;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%) scale(0.8);
  padding: 20px 32px;
  background: rgba(45, 45, 45, 0.92);
  backdrop-filter: blur(40px) saturate(180%);
  -webkit-backdrop-filter: blur(40px) saturate(180%);
  color: white;
  border-radius: var(--radius-lg);
  font-size: 14px;
  font-weight: 600;
  letter-spacing: -0.01em;
  z-index: 100;
  opacity: 0;
  transition: opacity 0.35s var(--ease-out), transform 0.4s var(--spring);
  pointer-events: none;
  box-shadow: 0 8px 40px rgba(0,0,0,0.45);
  text-align: center;
  min-width: 100px;
}
#toast.show {
  opacity: 1;
  transform: translate(-50%, -50%) scale(1);
}

.hidden {
  display: none !important;
}

/* ===== Landscape ===== */
@media (orientation: landscape) and (max-height: 500px) {
  #camera-controls {
    padding: 12px 32px;
  }
  .capture-btn {
    width: 56px;
    height: 56px;
  }
  #editor-toolbar {
    padding: 2px 6px;
  }
  .tool-btn {
    width: 36px;
    height: 36px;
  }
  .sub-toolbar {
    padding: 4px 12px 8px;
  }
  .sub-toolbar::before {
    display: none;
  }
}

/* ===== Save flash — iOS screenshot effect ===== */
@keyframes saveFlash {
  0% { opacity: 0; }
  8% { opacity: 0.9; }
  100% { opacity: 0; }
}

.save-flash {
  position: fixed;
  inset: 0;
  background: white;
  z-index: 200;
  pointer-events: none;
  animation: saveFlash 0.3s ease-out forwards;
}

/* ===== Checkmark toast icon ===== */
.toast-icon {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 10px;
}
.toast-icon svg {
  width: 36px;
  height: 36px;
  flex-shrink: 0;
}
