/* FotoWizard — la fotocamera è quieta, la camera oscura è lo spettacolo. */

:root {
  --nero: #000000;
  --carta: #F4F4F0;        /* testo: bianco carta fotografica */
  --alogenuro: #8E8E89;    /* testo secondario */
  --af: #FFD60A;           /* giallo dei punti AF: aggancio e selezione */
  --inattinica: #FF3B1F;   /* luce di sicurezza: solo in camera oscura */
  --velo: rgba(255, 255, 255, .09);
  --ease: cubic-bezier(.2, .8, .2, 1);
  --font: -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --safe-t: env(safe-area-inset-top, 0px);
  --safe-b: env(safe-area-inset-bottom, 0px);
  color-scheme: dark;
}

*, *::before, *::after { box-sizing: border-box; }
html, body { margin: 0; height: 100%; background: var(--nero); color: var(--carta); overscroll-behavior: none; }
body { font: 15px/1.4 var(--font); -webkit-font-smoothing: antialiased; -webkit-user-select: none; user-select: none; }
button { font: inherit; color: inherit; background: none; border: 0; padding: 0; cursor: pointer; -webkit-tap-highlight-color: transparent; }
button:disabled { cursor: default; }
:focus-visible { outline: 2px solid var(--af); outline-offset: 3px; }
[hidden] { display: none !important; }

/* ---------------------------------------------------------------- struttura */

.cam {
  height: 100dvh;
  display: grid;
  grid-template-columns: 100%;
  grid-template-rows: auto auto auto 1fr;
  grid-template-areas: "top" "vf" "lens" "ctrl";
  padding: var(--safe-t) 0 var(--safe-b);
  overflow: hidden;
}

.topbar { grid-area: top; display: flex; align-items: center; justify-content: space-between; height: 52px; padding: 0 8px 0 12px; }
.brand { font-weight: 600; letter-spacing: -.01em; }

.icon-btn { width: 44px; height: 44px; display: grid; place-items: center; border-radius: 50%; }
.icon-btn svg { width: 22px; height: 22px; }

.key-btn { display: flex; align-items: center; gap: 8px; height: 32px; padding: 0 12px; border-radius: 999px; background: var(--velo); font-size: 13px; }
.key-btn .dot { width: 8px; height: 8px; border-radius: 50%; background: var(--alogenuro); transition: background .3s; }
.key-btn[data-on="1"] .dot { background: var(--af); }

/* ---------------------------------------------------------------- mirino */

.viewfinder {
  grid-area: vf;
  justify-self: center;
  position: relative;
  height: min(133.333vw, calc(100dvh - 300px - var(--safe-t) - var(--safe-b)));
  aspect-ratio: 3 / 4;
  overflow: hidden;
  background: var(--nero);
}
.viewfinder video, .viewfinder .still { position: absolute; inset: 0; width: 100%; height: 100%; }
.viewfinder video { object-fit: cover; }
.viewfinder video.mirror { transform: scaleX(-1); }
.viewfinder .still { object-fit: contain; background: var(--nero); }

/* Anteprima fantasma: approssimazioni leggere della lente, solo mentre si sceglie */
.viewfinder :is(video, .still) { transition: filter .35s var(--ease); }
.ghost { position: absolute; inset: 0; pointer-events: none; opacity: 0; transition: opacity .35s var(--ease); }
.cam:is([data-state="live"], [data-state="ready"]) .viewfinder:not([data-lens="originale"]) .ghost { opacity: 1; }

.cam:is([data-state="live"], [data-state="ready"]) .viewfinder[data-lens="inverno"] :is(video, .still) { filter: saturate(.55) brightness(1.08) contrast(.92) hue-rotate(-12deg); }
.viewfinder[data-lens="inverno"] .ghost {
  background:
    var(--snow-near, none) 0 0 / 384px 384px,
    var(--snow-far, none) 0 0 / 256px 256px,
    linear-gradient(rgba(186, 214, 255, .16), rgba(186, 214, 255, .06));
  animation: neve 12s linear infinite;
}
/* due piani a velocità e deriva diverse: parallasse, nessuna griglia percepibile */
@keyframes neve { to { background-position: 96px 768px, -64px 256px, 0 0; } }
@media (prefers-reduced-motion: reduce) { .viewfinder[data-lens="inverno"] .ghost { animation: none; } }

.cam:is([data-state="live"], [data-state="ready"]) .viewfinder[data-lens="mattoncini"] :is(video, .still) { filter: saturate(1.7) contrast(1.18); }
.viewfinder[data-lens="mattoncini"] .ghost {
  background: radial-gradient(circle at 50% 50%, rgba(255, 255, 255, .28) 0 3.5px, rgba(0, 0, 0, .28) 4.5px 5.5px, transparent 6.5px) 0 0 / 16px 16px;
  mix-blend-mode: overlay;
}

.cam:is([data-state="live"], [data-state="ready"]) .viewfinder[data-lens="diorama"] :is(video, .still) { filter: saturate(1.55) contrast(1.08) brightness(1.04); }
.viewfinder[data-lens="diorama"] .ghost {
  -webkit-backdrop-filter: blur(6px);
  backdrop-filter: blur(6px);
  -webkit-mask-image: linear-gradient(#000 0 18%, transparent 40% 62%, #000 84%);
  mask-image: linear-gradient(#000 0 18%, transparent 40% 62%, #000 84%);
}

.cam:is([data-state="live"], [data-state="ready"]) .viewfinder[data-lens="plastilina"] :is(video, .still) { filter: saturate(1.35) contrast(.88) brightness(1.06) blur(.7px); }
.viewfinder[data-lens="plastilina"] .ghost { background: radial-gradient(ellipse at 50% 40%, rgba(255, 190, 120, .12), rgba(120, 60, 20, .28)); }

.cam:is([data-state="live"], [data-state="ready"]) .viewfinder[data-lens="anni50"] :is(video, .still) { filter: sepia(.32) saturate(1.45) contrast(1.1) hue-rotate(-6deg); }
.viewfinder[data-lens="anni50"] .ghost {
  background: radial-gradient(ellipse at center, transparent 50%, rgba(40, 18, 0, .6)), var(--grain, none);
  background-size: 100% 100%, 128px 128px;
  background-blend-mode: normal, overlay;
  opacity: .85;
}

/* Lettura: scansione e aggancio dei punti AF */
.scan { position: absolute; inset: 0; pointer-events: none; opacity: 0; transition: opacity .3s; }
.cam[data-state="reading"] .scan {
  opacity: 1;
  background: linear-gradient(transparent 42%, rgba(255, 214, 10, .28) 50%, transparent 58%) 0 0 / 100% 220% no-repeat;
  animation: scan 1.5s var(--ease) infinite alternate;
}
@keyframes scan { from { background-position: 0 100%; } to { background-position: 0 0; } }

.af { position: absolute; inset: 0; pointer-events: none; }
.af-box {
  --c: var(--af); --l: 16px;
  position: absolute; left: var(--x); top: var(--y); width: var(--w); height: var(--h);
  background:
    linear-gradient(var(--c), var(--c)) top left / var(--l) 2px no-repeat,
    linear-gradient(var(--c), var(--c)) top left / 2px var(--l) no-repeat,
    linear-gradient(var(--c), var(--c)) top right / var(--l) 2px no-repeat,
    linear-gradient(var(--c), var(--c)) top right / 2px var(--l) no-repeat,
    linear-gradient(var(--c), var(--c)) bottom left / var(--l) 2px no-repeat,
    linear-gradient(var(--c), var(--c)) bottom left / 2px var(--l) no-repeat,
    linear-gradient(var(--c), var(--c)) bottom right / var(--l) 2px no-repeat,
    linear-gradient(var(--c), var(--c)) bottom right / 2px var(--l) no-repeat;
  opacity: 0;
  transform: scale(1.3);
  animation: aggancio .45s var(--ease) var(--d, 0ms) forwards;
}
.af.static .af-box { animation: none; opacity: 1; transform: none; }
@keyframes aggancio { 55% { opacity: 1; transform: scale(.95); } 100% { opacity: 1; transform: scale(1); } }

.af-tag {
  position: absolute; left: 0; bottom: calc(100% + 6px);
  display: flex; gap: 6px; white-space: nowrap;
  padding: 3px 7px; border-radius: 4px;
  background: var(--af); color: #000;
  font-size: 12px; font-weight: 600; font-variant-numeric: tabular-nums;
}
.af-tag b { font-weight: 500; opacity: .62; }
.af-tag.inside { bottom: auto; top: 6px; left: 6px; }
.af-tag.right { left: auto; right: 0; }
.af-tag.inside.right { right: 6px; }

.flash { position: absolute; inset: 0; background: #fff; opacity: 0; pointer-events: none; }
.flash.on { animation: flash .3s ease-out; }
@keyframes flash { from { opacity: .9; } to { opacity: 0; } }

.cam-msg { position: absolute; inset: 0; display: grid; place-content: center; gap: 16px; padding: 32px; text-align: center; background: var(--nero); }
.cam-msg p { margin: 0; max-width: 32ch; color: var(--alogenuro); }
.cam-msg .btn { justify-self: center; }

/* ---------------------------------------------------------------- nastro lenti */

.lenses { grid-area: lens; min-width: 0; transition: opacity .3s; }
.cam:is([data-state="reading"], [data-state="developing"]) .lenses { opacity: .3; pointer-events: none; }
.lenses ol {
  list-style: none; margin: 0;
  padding: 10px calc(50% - 44px);
  display: flex; gap: 2px;
  overflow-x: auto; scroll-snap-type: x mandatory; scrollbar-width: none;
}
.lenses ol::-webkit-scrollbar { display: none; }
.lenses li { scroll-snap-align: center; }
.lens-btn {
  padding: 8px 11px; white-space: nowrap;
  font-size: 13px; font-weight: 600; letter-spacing: .05em; text-transform: uppercase; /* il linguaggio del selettore modalità di una fotocamera */
  color: var(--alogenuro); transition: color .2s;
}
.lens-btn[aria-pressed="true"] { color: var(--af); }

/* ---------------------------------------------------------------- comandi */

.controls {
  grid-area: ctrl; align-self: start;
  display: grid; grid-template-columns: 1fr auto 1fr; align-items: center;
  padding: 10px 28px 20px;
}
.cam[data-state="ready"] .controls { display: none; }

.roll-btn { justify-self: start; width: 48px; height: 48px; border-radius: 8px; overflow: hidden; background: #1d1d1f; border: 1.5px solid rgba(255, 255, 255, .22); }
.roll-btn img { width: 100%; height: 100%; object-fit: cover; display: block; }
.flip { justify-self: end; background: var(--velo); }

.shutter { width: 78px; height: 78px; border-radius: 50%; border: 4px solid var(--carta); display: grid; place-items: center; }
.shutter span { width: 62px; height: 62px; border-radius: 50%; background: var(--carta); transition: transform .15s var(--ease), background-color .25s; }
.cam[data-ai="1"] .shutter span { background: var(--af); }
.shutter:active:not(:disabled) span { transform: scale(.86); }
.shutter:disabled { opacity: .35; }

/* ---------------------------------------------------------------- lettura */

.reading { grid-area: ctrl; align-self: start; min-width: 0; display: flex; flex-direction: column; gap: 12px; padding: 4px 16px 16px; overflow-y: auto; }
.cam:not([data-state="ready"]) .reading { display: none; }
.scene { margin: 0; max-width: 46ch; font-size: 17px; line-height: 1.3; font-weight: 500; }
.facts { list-style: none; margin: 0; padding: 0; display: flex; flex-wrap: wrap; gap: 6px; font-size: 13px; color: var(--alogenuro); }
.facts li { padding: 4px 10px; border-radius: 999px; background: var(--velo); }
.facts .palette { display: flex; align-items: center; gap: 0; padding: 4px 8px; }
.swatch { width: 14px; height: 14px; border-radius: 50%; background: var(--sw); box-shadow: 0 0 0 2px #1a1a1a; }
.swatch + .swatch { margin-left: -3px; }

.intensity { display: grid; grid-template-columns: repeat(3, 1fr); padding: 3px; border-radius: 11px; background: var(--velo); }
.intensity button { padding: 8px 4px; border-radius: 8px; font-size: 13px; font-weight: 500; color: var(--alogenuro); transition: background-color .2s, color .2s; }
.intensity button[aria-checked="true"] { background: var(--carta); color: #000; }

.sheet-actions { display: grid; grid-template-columns: auto 1fr; gap: 10px; }

.btn { height: 48px; padding: 0 18px; border-radius: 12px; font-weight: 600; background: var(--velo); transition: background-color .2s, opacity .2s; }
.btn-primary { background: var(--af); color: #000; }
.btn-primary:disabled { background: var(--velo); color: var(--alogenuro); }

/* ---------------------------------------------------------------- camera oscura */

.darkroom {
  position: fixed; inset: 0; z-index: 20;
  display: grid; grid-template-rows: 1fr auto; gap: 20px;
  padding: calc(var(--safe-t) + 20px) 16px calc(var(--safe-b) + 28px);
  background: var(--nero);
}
.darkroom::before { /* la lampada inattinica sopra la vasca */
  content: ""; position: absolute; inset: 0; pointer-events: none;
  background: radial-gradient(ellipse 80% 50% at 50% -8%, rgba(255, 59, 31, .3), transparent 72%);
  transition: opacity .6s;
}
.darkroom.lights-on::before { opacity: 0; }

.tray { position: relative; min-height: 0; }
.tray img {
  --dev: 0;
  position: absolute; inset: 0; width: 100%; height: 100%; object-fit: contain;
  opacity: calc(.18 + var(--dev) * .82);
  /* sotto la luce inattinica si vede solo il rosso: monocromia rossa che si "carica" col passare dello sviluppo */
  filter: grayscale(1) sepia(1) saturate(8) hue-rotate(-42deg)
          brightness(calc(.5 + var(--dev) * .45)) contrast(calc(.75 + var(--dev) * .45))
          blur(calc((1 - var(--dev)) * 10px));
  transition: filter .4s linear, opacity .4s linear;
}
.tray::after {
  content: ""; position: absolute; inset: 0; pointer-events: none;
  background: var(--grain, none) 0 0 / 128px 128px; mix-blend-mode: screen; opacity: .12;
  animation: grana .4s steps(4) infinite;
}
@keyframes grana { 25% { background-position: 37px 11px; } 50% { background-position: -21px 53px; } 75% { background-position: 64px -40px; } }

.darkroom.lights-on .tray img { filter: none; opacity: 1; transition: filter .9s var(--ease), opacity .3s; }
.darkroom.lights-on .tray::after { opacity: 0; transition: opacity .6s; }

.dark-panel { width: min(520px, 100%); justify-self: center; }
.stages { list-style: none; margin: 0; padding: 0; display: grid; grid-template-columns: repeat(4, 1fr); gap: 6px; }
.stages li { padding-top: 10px; border-top: 3px solid #2b2b2a; font-size: 12px; color: #5d5d5a; transition: color .3s, border-color .3s; }
.stages li[data-s="done"] { color: var(--carta); border-color: var(--carta); }
.stages li[data-s="active"] { color: var(--inattinica); border-color: var(--inattinica); animation: respiro 1.6s ease-in-out infinite; }
@keyframes respiro { 50% { border-color: rgba(255, 59, 31, .35); } }
.dark-meta { display: flex; justify-content: space-between; margin-top: 12px; font-size: 13px; color: var(--alogenuro); font-variant-numeric: tabular-nums; }

/* ---------------------------------------------------------------- risultato */

.result, .roll {
  position: fixed; inset: 0; z-index: 30;
  display: grid; grid-template-rows: auto 1fr auto;
  padding: var(--safe-t) 0 var(--safe-b);
  background: var(--nero);
}
.roll { z-index: 25; grid-template-rows: auto 1fr; }
.result-top { display: grid; grid-template-columns: 44px 1fr 44px; align-items: center; height: 56px; padding: 0 8px; }
.result-title { margin: 0; text-align: center; font-weight: 600; }

.compare { --pos: 50%; position: relative; min-height: 0; touch-action: none; cursor: ew-resize; }
.compare img { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: contain; pointer-events: none; -webkit-user-drag: none; }
.compare .before { clip-path: inset(0 calc(100% - var(--pos)) 0 0); }
.handle { position: absolute; top: 0; bottom: 0; left: var(--pos); width: 44px; margin-left: -22px; display: grid; place-items: center; }
.handle::before { content: ""; position: absolute; top: 0; bottom: 0; left: 50%; width: 2px; margin-left: -1px; background: var(--carta); box-shadow: 0 0 0 1px rgba(0, 0, 0, .3); }
.handle span { position: relative; width: 38px; height: 38px; border-radius: 50%; display: grid; place-items: center; background: var(--carta); color: #000; }
.handle svg { width: 20px; height: 20px; }
.cmp-label { position: absolute; bottom: 14px; padding: 4px 10px; border-radius: 999px; background: rgba(0, 0, 0, .55); font-size: 12px; pointer-events: none; }
.cmp-label.left { left: 14px; }
.cmp-label.right { right: 14px; }
.compare.single { cursor: default; }
.compare.single :is(.before, .handle, .cmp-label) { display: none; }

.result-bar { display: flex; gap: 10px; padding: 14px 16px 18px; }
.result-bar .btn { flex: 1; }

/* ---------------------------------------------------------------- rullino */

.roll-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(112px, 1fr)); gap: 2px; align-content: start; overflow-y: auto; }
.roll-grid button { position: relative; aspect-ratio: 3 / 4; overflow: hidden; background: #151515; }
.roll-grid img { width: 100%; height: 100%; object-fit: cover; display: block; }
.roll-grid .tag { position: absolute; left: 6px; bottom: 6px; padding: 2px 6px; border-radius: 4px; background: rgba(0, 0, 0, .6); font-size: 11px; }
.roll-empty { grid-column: 1 / -1; margin: 30vh auto 0; max-width: 30ch; text-align: center; color: var(--alogenuro); }

/* ---------------------------------------------------------------- dialog e toast */

.key-dialog { width: min(420px, calc(100vw - 32px)); padding: 22px; border: 0; border-radius: 18px; background: #1c1c1e; color: var(--carta); }
.key-dialog::backdrop { background: rgba(0, 0, 0, .6); -webkit-backdrop-filter: blur(4px); backdrop-filter: blur(4px); }
.key-dialog h2 { margin: 0 0 8px; font-size: 20px; }
.key-dialog label { display: block; margin: 16px 0 6px; font-size: 13px; color: var(--alogenuro); }
.key-dialog input { width: 100%; height: 46px; padding: 0 12px; border: 1.5px solid #3a3a3c; border-radius: 10px; background: #000; color: var(--carta); font: 15px var(--font); -webkit-user-select: text; user-select: text; }
.key-dialog input:focus { border-color: var(--af); outline: none; }
.muted { margin: 0; color: var(--alogenuro); }
.small { font-size: 12px; margin-top: 12px; }
.key-state { min-height: 1.4em; margin: 10px 0 0; font-size: 13px; }
.key-state.error { color: #FF6B55; }
.dialog-actions { display: flex; flex-wrap: wrap; justify-content: flex-end; gap: 8px; margin-top: 18px; }

.toast {
  position: fixed; left: 50%; top: calc(var(--safe-t) + 60px); z-index: 50;
  max-width: min(440px, calc(100vw - 32px)); padding: 12px 16px; border-radius: 12px;
  background: #2c2c2e; color: var(--carta); font-size: 14px;
  opacity: 0; transform: translate(-50%, -12px); pointer-events: none;
  transition: opacity .25s, transform .25s var(--ease);
}
.toast.on { opacity: 1; transform: translate(-50%, 0); }

/* ---------------------------------------------------------------- desktop */

@media (min-width: 900px) and (min-aspect-ratio: 1 / 1) {
  .cam {
    grid-template-columns: 1fr 380px;
    grid-template-rows: auto 1fr auto;
    grid-template-areas: "vf top" "vf lens" "vf ctrl";
  }
  .topbar { padding-top: 12px; }
  .viewfinder {
    align-self: center;
    height: min(calc(100dvh - 48px), calc((100vw - 380px - 48px) * .75));
    aspect-ratio: 4 / 3;
    border-radius: 6px;
  }
  .lenses { align-self: end; }
  .controls { padding: 16px 40px 40px; }
  .reading { padding: 8px 24px 32px; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: .01ms !important; animation-iteration-count: 1 !important; transition-duration: .01ms !important; }
  .af-box { opacity: 1; transform: none; }
}
