:root {
  --bg: #0b0b0d;
  --bg-elev: #131316;
  --bg-sheet: #1a1a1e;
  --bg-sheet2: #222226;
  --line: rgba(255,255,255,0.08);
  --text: #f4f2ee;
  --text-dim: #8b8b92;
  --accent: #ef7d3a;
  --accent-2: #d8a06f;
  --danger: #e0594f;
  --ok: #6fae7f;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-left: env(safe-area-inset-left, 0px);
  --safe-right: env(safe-area-inset-right, 0px);
  --bar-h: 60px;
  --font: -apple-system, BlinkMacSystemFont, "SF Pro Display", "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  --radius: 18px;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body {
  margin: 0; padding: 0; height: 100%; width: 100%;
  background: var(--bg); color: var(--text);
  font-family: var(--font);
  overflow: hidden;
  overscroll-behavior: none;
  -webkit-font-smoothing: antialiased;
  touch-action: manipulation;
  user-select: none; -webkit-user-select: none;
  position: fixed;
}
button { font-family: var(--font); cursor: pointer; border: none; background: none; color: inherit; }
#app { height: 100%; width: 100%; }
.hidden { display: none !important; }
.screen { position: absolute; inset: 0; }

/* ---------------- HOME ---------------- */
#home {
  display: flex; align-items: center; justify-content: center;
  background:
    radial-gradient(120% 90% at 50% -10%, #1c1410 0%, #0d0c0e 55%, #0b0b0d 100%);
  padding: calc(var(--safe-top) + 24px) calc(var(--safe-left) + 24px) calc(var(--safe-bottom) + 24px) calc(var(--safe-right) + 24px);
}
.home-inner { text-align: center; width: 100%; max-width: 420px; display: flex; flex-direction: column; gap: 40px; }
.logo-block { display: flex; flex-direction: column; align-items: center; gap: 14px; }
.logo-mark {
  font-size: 64px; font-weight: 700; letter-spacing: 2px;
  background: linear-gradient(180deg, #fff 10%, #e9cfa8 45%, var(--accent) 90%);
  -webkit-background-clip: text; background-clip: text;
  -webkit-text-fill-color: transparent;
  text-shadow: 0 10px 50px rgba(239,125,58,0.25);
}
.logo-tag { font-size: 13px; color: var(--text-dim); letter-spacing: 3px; text-transform: uppercase; }
.home-actions { display: flex; flex-direction: column; gap: 14px; align-items: stretch; }
.btn-import {
  background: linear-gradient(180deg, #ff8f4d, #e97120);
  color: #171410; border-radius: 18px; padding: 18px 24px;
  display: flex; align-items: center; justify-content: center; gap: 12px;
  font-size: 18px; font-weight: 650;
  box-shadow: 0 12px 30px rgba(233,113,32,0.35);
  transition: transform 0.08s ease;
}
.btn-import:active { transform: scale(0.97); }
.btn-import-icon { font-size: 26px; font-weight: 400; }
.btn-secondary {
  border: 1px solid var(--line); border-radius: 16px; padding: 15px 20px;
  font-size: 15px; color: var(--text); background: rgba(255,255,255,0.03);
}
.home-foot p { font-size: 12px; color: #5d5d66; margin: 0; }

/* ---------------- EDITOR ---------------- */
#editor {
  display: flex; flex-direction: column;
  padding: var(--safe-top) 0 var(--safe-bottom);
  background: var(--bg);
}
#editor-top {
  height: calc(var(--bar-h) + 6px);
  display: flex; align-items: center; justify-content: space-between;
  padding: 0 14px; flex-shrink: 0;
}
.brand { font-size: 20px; font-weight: 700; letter-spacing: 1px; }
.brand-letter { color: var(--accent); }
.top-actions { display: flex; align-items: center; gap: 2px; }
.icon-btn {
  width: 42px; height: 42px; border-radius: 12px;
  display: flex; align-items: center; justify-content: center; color: var(--text);
}
.icon-btn svg { width: 22px; height: 22px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
.icon-btn:disabled { opacity: 0.25; }
.export-btn {
  display: flex; align-items: center; justify-content: center;
  background: linear-gradient(180deg, #ff8f4d, #e97120); border-radius: 12px;
  width: 46px; height: 42px; color: #171410; margin-left: 6px;
}
.export-btn svg { width: 22px; height: 22px; fill: none; stroke: currentColor; stroke-width: 2.2; stroke-linecap: round; stroke-linejoin: round; }

/* ---------------- STAGE ---------------- */
#stage {
  flex: 1; position: relative; overflow: hidden; margin: 2px 0 0;
  background:
    linear-gradient(45deg, #111113 25%, transparent 25%, transparent 75%, #111113 75%),
    linear-gradient(45deg, #111113 25%, #0c0c0e 25%, #0c0c0e 75%, #111113 75%);
  background-size: 20px 20px;
  display: flex; align-items: center; justify-content: center;
  touch-action: none;
}
#view-canvas { width: 100%; height: 100%; object-fit: contain; display: block; position: absolute; inset:0; margin:auto; }
#orig-canvas { position: absolute; inset:0; margin:auto; display:block; object-fit: contain; pointer-events:none; opacity:0; transition: opacity 0.12s linear; }

/* Crop overlay */
#crop-overlay { position:absolute; inset:0; pointer-events:none; z-index:6; }
#crop-overlay .crop-frame { position:absolute; border:1.5px solid rgba(255,255,255,0.9); box-shadow:0 0 30px rgba(0,0,0,0.5); touch-action:none; }
.crop-mask { position:absolute; background:rgba(10,10,12,0.55); }
.crop-handle {
  position:absolute; width:28px; height:28px; border-radius:50%; pointer-events:auto; touch-action:none;
  display:flex; align-items:center; justify-content:center; font-size:16px; color:#fff;
}
.crop-handle::before{ content:""; width:8px; height:8px; border:2px solid #fff; position:absolute; }
.crop-handle.nw{left:-14px;top:-14px;} .crop-handle.ne{right:-14px;top:-14px;}
.crop-handle.sw{left:-14px;bottom:-14px;} .crop-handle.se{right:-14px;bottom:-14px;}
.crop-handle.nw::before{ border-left:none; border-top:none; }
.crop-handle.ne::before{ border-right:none; border-top:none; }
.crop-handle.sw::before{ border-left:none; border-bottom:none; }
.crop-handle.se::before{ border-right:none; border-bottom:none; }
#stage-meta { position: absolute; top: 10px; left: 10px; right: 10px; display: flex; gap: 8px; pointer-events: none; }
.meta-chip {
  font-size: 11px; color: #cfcfd6; background: rgba(0,0,0,0.45);
  padding: 4px 9px; border-radius: 8px;
  backdrop-filter: blur(6px); -webkit-backdrop-filter: blur(6px);
}
#meta-zoom { margin-left: auto; }

/* Before / After */
#ba-mode {
  position: absolute; top: 12px; left: 50%; transform: translateX(-50%);
  display: flex; gap: 4px; padding: 3px; border-radius: 12px;
  background: rgba(20,20,24,0.7); backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
}
#ba-mode.hidden { display: none; }
.ba-btn {
  padding: 6px 12px; border-radius: 9px; font-size: 12px; color: var(--text-dim);
  display: flex; align-items: center; gap: 5px;
}
.ba-btn.active { background: #2b2b30; color: var(--text); }
#split-handle {
  position: absolute; top: 0; bottom: 0; width: 2px; left: 50%;
  background: rgba(255,255,255,0.85); transform: translateX(-50%);
  box-shadow: 0 0 12px rgba(0,0,0,0.6);
  z-index: 5; pointer-events: none;
}
#split-handle span {
  position: absolute; top: 50%; left: 50%; transform: translate(-50%,-50%);
  width: 44px; height: 44px; border-radius: 50%; background: #fff;
  display: flex; align-items: center; justify-content: center; color: #333;
  box-shadow: 0 4px 18px rgba(0,0,0,0.5);
}
#split-handle span::before, #split-handle span::after {
  content: ""; display: block; margin: 0 3px;
  border: solid #333; border-width: 0 2px 2px 0; padding: 3px;
}
#split-handle span::before { transform: rotate(135deg); }
#split-handle span::after { transform: rotate(-45deg); }

/* ---------------- BOTTOM BAR ---------------- */
#bottom-bar {
  flex-shrink: 0; background: rgba(18,18,21,0.98);
  border-top: 1px solid var(--line);
  padding: 6px 0 2px;
}
.tool-strip {
  display: flex; overflow-x: auto; gap: 8px;
  padding: 6px 14px; scrollbar-width: none; -webkit-overflow-scrolling: touch;
}
.tool-strip::-webkit-scrollbar { display: none; }
.tool {
  flex-shrink: 0; min-width: 68px; padding: 9px 4px;
  display: flex; flex-direction: column; align-items: center; gap: 6px;
  border-radius: 14px; color: var(--text-dim);
  transition: color 0.15s, background 0.15s;
}
.tool svg { width: 24px; height: 24px; fill: none; stroke: currentColor; stroke-width: 1.7; stroke-linecap: round; stroke-linejoin: round; }
.tool span { font-size: 10.5px; letter-spacing: 0.3px; }
.tool.active { color: var(--accent); background: rgba(239,125,58,0.08); }
.tool .tool-active-dot { width: 4px; height: 4px; border-radius: 50%; background: transparent; }
.tool.modified .tool-active-dot { background: var(--accent); }

/* ---------------- SHEETS ---------------- */
.sheet-layer { position: absolute; inset: 0; z-index: 40; }
.sheet-backdrop {
  position: absolute; inset: 0; background: rgba(0,0,0,0.55);
  backdrop-filter: blur(6px); -webkit-backdrop-filter: blur(6px);
}
.sheet {
  position: absolute; left: 0; right: 0; bottom: 0;
  background: var(--bg-sheet);
  border-radius: 24px 24px 0 0;
  max-height: 78vh;
  display: flex; flex-direction: column;
  padding-bottom: var(--safe-bottom);
  transform: translateY(0);
  transition: transform 0.32s cubic-bezier(0.32, 0.72, 0, 1);
  box-shadow: 0 -20px 60px rgba(0,0,0,0.6);
}
.sheet.taller { max-height: 86vh; }
.sheet-layer.closing .sheet { transform: translateY(110%); }
.sheet-handle {
  width: 40px; height: 4px; border-radius: 3px; background: rgba(255,255,255,0.2);
  margin: 10px auto 6px; flex-shrink: 0;
}
#sheet-body { overflow-y: auto; overscroll-behavior: contain; padding: 0 18px 18px; -webkit-overflow-scrolling: touch; }

/* Sheet header */
.sheet-title {
  display: flex; align-items: center; justify-content: space-between;
  padding: 6px 0 10px;
}
.sheet-title .st-label { font-size: 13px; color: var(--text-dim); }
.st-reset { font-size: 13px; color: var(--accent); padding: 8px 4px; }

/* Sliders */
.slider-group { padding: 4px 0; }
.slider-row {
  display: flex; align-items: center; gap: 12px; padding: 9px 0;
}
.slider-row .s-icon { width: 22px; color: var(--text-dim); font-size: 15px; text-align: center; flex-shrink: 0; }
.slider-row .s-main { flex: 1; }
.slider-row .s-label {
  font-size: 12.5px; color: var(--text-dim); letter-spacing: 0.4px;
  display: flex; justify-content: space-between; align-items: center;
}
.slider-row .s-val {
  font-size: 12.5px; color: var(--text); font-weight: 600; font-variant-numeric: tabular-nums;
}
input[type=range].f-range {
  -webkit-appearance: none; appearance: none; width: 100%; height: 26px;
  background: transparent; margin: 0;
}
input[type=range].f-range::-webkit-slider-runnable-track {
  height: 4px; border-radius: 3px;
  background: linear-gradient(to right, var(--accent) 0%, var(--accent) var(--fill,50%), rgba(255,255,255,0.16) var(--fill,50%));
}
input[type=range].f-range::-webkit-slider-thumb {
  -webkit-appearance: none; appearance: none;
  width: 20px; height: 20px; border-radius: 50%;
  background: #fff; margin-top: -8px;
  box-shadow: 0 2px 8px rgba(0,0,0,0.5);
}
input[type=range].f-range:active::-webkit-slider-thumb { transform: scale(1.1); }

/* Picker rows (temperature fine etc.) */
.picker-grid { display: flex; gap: 6px; }
.picker-cell {
  flex: 1; padding: 12px 6px; border-radius: 12px; text-align: center;
  background: var(--bg-sheet2); color: var(--text-dim);
}
.picker-cell .pc-icon { font-size: 18px; display: block; margin-bottom: 5px; }
.picker-cell .pc-label { font-size: 11px; }
.picker-cell.active { background: rgba(239,125,58,0.16); color: var(--accent); }

/* Segment control */
.segment {
  display: flex; background: var(--bg-sheet2); border-radius: 12px; padding: 3px; overflow-x: auto;
}
.segment::-webkit-scrollbar { display: none; }
.segment .seg {
  flex: 1; padding: 9px 4px; border-radius: 9px; text-align: center;
  font-size: 12.5px; color: var(--text-dim); white-space: nowrap;
}
.segment .seg.active { background: #333338; color: var(--text); }

/* Curve canvas */
.curve-wrap { padding: 8px 0; }
#curve-canvas { width: 100%; height: 220px; border-radius: 14px; background: var(--bg-sheet2); display: block; touch-action: none; }

/* ---------------- FILM PRESETS CAROUSEL ---------------- */
.preset-carousel { display: flex; gap: 14px; overflow-x: auto; padding: 8px 2px 14px; scrollbar-width: none; -webkit-overflow-scrolling: touch; }
.preset-carousel::-webkit-scrollbar { display: none; }
.preset-item {
  flex-shrink: 0; width: 96px; text-align: center;
}
.preset-thumb {
  width: 96px; height: 96px; border-radius: 16px; overflow: hidden;
  border: 2px solid transparent; position: relative;
  background-size: cover; background-position: center;
}
.preset-item.active .preset-thumb { border-color: var(--accent); }
.preset-name { font-size: 11px; color: var(--text-dim); margin-top: 6px; letter-spacing: 0.3px; }
.preset-item.active .preset-name { color: var(--text); }
.preset-badge { position: absolute; top: 6px; right: 6px; font-size: 15px; line-height: 1; }
.grayscale-badge { position:absolute; top:6px; left:6px; font-size:11px; color:#fff; background:rgba(0,0,0,0.5); padding:2px 5px; border-radius:6px; }

/* ---------------- RECIPES ---------------- */
.recipe-header { padding: 4px 0 10px; }
.recipe-actions { display: flex; gap: 8px; padding: 6px 0 12px; }
.recipe-action-btn {
  flex: 1; padding: 12px 6px; border-radius: 12px; background: var(--bg-sheet2);
  font-size: 12.5px; color: var(--text); display: flex; flex-direction: column; align-items: center; gap: 4px;
}
.recipe-action-btn svg { width: 20px; height: 20px; fill:none; stroke: currentColor; stroke-width:1.7; stroke-linecap:round; stroke-linejoin:round; }
.recipe-action-btn.danger { color: var(--danger); }
.recipe-list { display: flex; flex-direction: column; gap: 10px; }
.recipe-card {
  display: flex; align-items: center; gap: 12px; padding: 10px;
  background: var(--bg-sheet2); border-radius: 14px;
}
.recipe-card .rc-thumb { width: 52px; height: 52px; border-radius: 10px; background-size: cover; background-position:center; flex-shrink:0; }
.recipe-card .rc-info { flex: 1; min-width:0; }
.recipe-card .rc-name { font-size: 14px; font-weight:600; white-space: nowrap; overflow:hidden; text-overflow:ellipsis; }
.recipe-card .rc-sub { font-size: 11px; color: var(--text-dim); }
.recipe-card .rc-fav {
  width: 36px; height: 36px; border-radius:50%; display:flex; align-items:center; justify-content:center;
  color: var(--text-dim); font-size:18px;
}
.recipe-card .rc-fav.active { color: var(--accent); }
.recipe-card .rc-apply {
  width: 36px; height: 36px; border-radius:10px; background:#2f2f35;
  display:flex; align-items:center; justify-content:center; color:var(--accent);
}
.recipe-card .rc-apply svg { width:18px; height:18px; fill:none; stroke:currentColor; stroke-width:2; }
.recipe-card.builtin { border: 1px solid rgba(239,125,58,0.25); background: rgba(239,125,58,0.06); }
.fuji-tag {
  display:inline-block; font-size:9px; font-weight:700; letter-spacing:0.5px;
  background: var(--accent); color:#000; border-radius:4px; padding:1px 5px; margin-right:4px;
}

/* Inputs */
.f-text {
  width: 100%; padding: 12px 14px; border-radius: 12px; background: var(--bg-sheet2);
  border: 1px solid var(--line); color: var(--text); font-size: 15px; outline: none;
}

/* ---------------- EXPORT SHEET ---------------- */
.export-preview { border-radius: 16px; overflow: hidden; margin: 6px 0 14px; background:#000; max-height: 300px; display:flex; align-items:center; justify-content:center; }
.export-preview img, .export-preview canvas { max-width:100%; max-height:300px; display:block; }
.export-opt { display:flex; justify-content: space-between; align-items:center; padding: 12px 0; }
.export-opt .eo-label { font-size: 13.5px; color: var(--text); }
.export-opt .eo-val { color: var(--text-dim); font-size: 13px; }
.grid-2 { display:grid; grid-template-columns:1fr 1fr; gap:8px; }
.chip-btn { padding:11px 4px; border-radius:12px; background:var(--bg-sheet2); font-size:13px; text-align:center; }
.chip-btn.active { background:rgba(239,125,58,0.16); color:var(--accent); }

/* ---------------- TOAST / SPINNER ---------------- */
.toast {
  position: absolute; left: 50%; bottom: calc(var(--safe-bottom) + 90px); transform: translateX(-50%) translateY(20px);
  background: rgba(40,40,46,0.95); color: var(--text); padding: 12px 18px; border-radius: 12px;
  font-size: 13.5px; opacity: 0; transition: opacity 0.25s, transform 0.25s; pointer-events: none; z-index: 90;
  backdrop-filter: blur(8px); -webkit-backdrop-filter: blur(8px); box-shadow: 0 8px 30px rgba(0,0,0,0.4);
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }
.spinner {
  position: absolute; inset: 0; z-index: 100;
  background: rgba(8,8,10,0.7); backdrop-filter: blur(4px); -webkit-backdrop-filter: blur(4px);
  display: flex; align-items: center; justify-content: center;
}
.spinner::after {
  content:""; width: 40px; height: 40px; border-radius: 50%;
  border: 3px solid rgba(255,255,255,0.2); border-top-color: var(--accent);
  animation: spin 0.8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

.confirm-bar { display:flex; gap:10px; padding-top: 10px; }
.btn-primary {
  flex:1; background: linear-gradient(180deg,#ff8f4d,#e97120); color:#171410;
  padding: 15px; border-radius: 14px; font-size: 15px; font-weight:650;
}
.btn-ghost { flex:1; background: var(--bg-sheet2); color: var(--text); padding: 15px; border-radius:14px; font-size:15px; }
.btn-ghost.danger { color: var(--danger); }
.disabled { opacity: 0.4; pointer-events: none; }
.group-label {
  font-size:11px; color: var(--text-dim); text-transform: uppercase; letter-spacing:1px;
  padding: 14px 0 6px;
}
.section-divider { height: 1px; background: var(--line); margin: 6px 0; }

/* Desktop adaptation */
@media (min-width: 768px) {
  #stage { border-radius: 14px; margin: 0 14px; }
  .sheet { left: 50%; right: auto; width: 460px; transform: translateX(-50%); border-radius: 24px 24px 24px 24px; margin-bottom: 14px; }
  .sheet-layer.closing .sheet { transform: translateX(-50%) translateY(110%); }
}
