:root {
  --bg: #000;
  --fg: #fff;
  --fg-muted: rgba(255, 255, 255, 0.72);
  --panel: rgba(0, 0, 0, 0.55);
  --ok: #34c759;
  --warn: #ffcc00;
  --danger: #ff3b30;
  --gold: #d3af00; /* oro del logo PRIME */
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-right: env(safe-area-inset-right, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-left: env(safe-area-inset-left, 0px);
}

[hidden] { display: none !important; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--fg);
  overflow: hidden;
  overscroll-behavior: none;
  font-family: -apple-system, BlinkMacSystemFont, "SF Pro Text", system-ui, sans-serif;
  -webkit-tap-highlight-color: transparent;
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
}

#app {
  position: fixed;
  inset: 0;
}

/* Anteprima a tutto schermo, non specchiata: identica al video registrato */
#preview {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  object-fit: cover;
  background: var(--bg);
}

/* ---------- Teleprompter ---------- */

/* Subito sotto la Dynamic Island, stretto e centrato: gli occhi restano vicini all'obiettivo */
.prompter {
  position: absolute;
  left: 50%;
  top: calc(max(var(--safe-top), 10px) + 4px);
  transform: translateX(-50%);
  width: calc(var(--prompt-width, 72) * 1%);
  height: calc(var(--prompt-height, 30) * 1%);
  overflow: hidden;
  border-radius: 18px;
  background: rgba(0, 0, 0, 0.55);
  touch-action: none;
  -webkit-mask-image: linear-gradient(to bottom, transparent 0, #000 10px, #000 calc(100% - 24px), transparent 100%);
  mask-image: linear-gradient(to bottom, transparent 0, #000 10px, #000 calc(100% - 24px), transparent 100%);
}
.prompter-text {
  padding: 10px 16px;
  font-size: var(--prompt-size, 30px);
  font-weight: 600;
  line-height: 1.35;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
  text-shadow: 0 1px 2px rgba(0, 0, 0, 0.5);
  will-change: transform;
}

/* ---------- Velocità ---------- */

.speed-bar {
  position: absolute;
  left: calc(16px + var(--safe-left));
  right: calc(16px + var(--safe-right));
  bottom: calc(24px + 80px + 14px + var(--safe-bottom));
  display: flex;
  align-items: center;
  gap: 12px;
  height: 44px;
  padding: 0 16px;
  border-radius: 22px;
  background: var(--panel);
}
.speed-label {
  color: var(--fg-muted);
  font-size: 14px;
  font-weight: 600;
}
.speed-value {
  min-width: 2.5em;
  text-align: right;
  font: 600 15px/1 ui-monospace, "SF Mono", Menlo, monospace;
}

input[type="range"] {
  -webkit-appearance: none;
  appearance: none;
  flex: 1;
  width: 100%;
  height: 44px;
  margin: 0;
  background: transparent;
}
input[type="range"]::-webkit-slider-runnable-track {
  height: 6px;
  border-radius: 3px;
  background: rgba(255, 255, 255, 0.3);
}
input[type="range"]::-webkit-slider-thumb {
  -webkit-appearance: none;
  width: 28px;
  height: 28px;
  margin-top: -11px;
  border-radius: 50%;
  background: var(--fg);
  box-shadow: 0 1px 4px rgba(0, 0, 0, 0.4);
}

/* ---------- Pulsanti laterali ---------- */

.side-buttons {
  position: absolute;
  right: calc(12px + var(--safe-right));
  bottom: calc(24px + 16px + var(--safe-bottom));
  display: flex;
  gap: 10px;
}
.round-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 48px;
  height: 48px;
  padding: 0;
  border: 0;
  border-radius: 50%;
  background: var(--panel);
  color: var(--fg);
}
.round-btn:active { opacity: 0.6; }
body:not([data-state="idle"]) .side-buttons { display: none; }

/* ---------- Pannelli (testo, regolazioni) ---------- */

.sheet {
  position: absolute;
  inset: 0;
  z-index: 5;
}
.sheet-panel {
  position: absolute;
  left: calc(8px + var(--safe-left));
  right: calc(8px + var(--safe-right));
  bottom: calc(8px + var(--safe-bottom));
  padding: 12px 18px 16px;
  border-radius: 22px;
  background: rgba(28, 28, 30, 0.96);
}
.sheet-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 8px;
}
.sheet-header h2 {
  margin: 0;
  font-size: 20px;
}
.btn-link {
  min-height: 44px;
  padding: 0 8px;
  border: 0;
  background: none;
  color: var(--gold);
  font-size: 17px;
  font-weight: 600;
}
.setting {
  display: block;
  margin-top: 6px;
}
.setting span {
  display: flex;
  justify-content: space-between;
  color: var(--fg-muted);
  font-size: 15px;
}
.setting output {
  color: var(--fg);
  font-variant-numeric: tabular-nums;
}

/* ---------- Schermata iniziale: script ---------- */

.home {
  position: absolute;
  inset: 0;
  z-index: 6;
  display: flex;
  flex-direction: column;
  gap: 12px;
  padding: calc(max(var(--safe-top), 12px) + 8px) calc(16px + var(--safe-right))
           calc(16px + var(--safe-bottom)) calc(16px + var(--safe-left));
  background: var(--bg);
}
.home-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
}
.brand {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 6px;
  margin: 0;
}
.brand-logo {
  display: block;
  height: 40px;
  width: auto;
}
.brand-name {
  color: var(--gold);
  font-size: 13px;
  font-weight: 600;
  letter-spacing: 0.32em;
  text-transform: uppercase;
}
.script-tabs {
  display: flex;
  flex-shrink: 0;
  gap: 8px;
  overflow-x: auto;
  scrollbar-width: none;
  -webkit-overflow-scrolling: touch;
}
.script-tabs::-webkit-scrollbar { display: none; }
.script-tab {
  flex-shrink: 0;
  max-width: 60vw;
  min-height: 40px;
  padding: 0 16px;
  overflow: hidden;
  border: 0;
  border-radius: 20px;
  background: #1c1c1e;
  color: var(--fg-muted);
  font-size: 15px;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.script-tab[aria-selected="true"] {
  background: var(--fg);
  color: var(--bg);
  font-weight: 600;
}
/* Campi: almeno 16px, sotto questa soglia iOS ingrandisce la pagina al tocco */
.script-title,
.script-text {
  box-sizing: border-box;
  width: 100%;
  color: var(--fg);
  -webkit-user-select: text;
  user-select: text;
}
.script-title {
  padding: 8px 0;
  border: 0;
  border-bottom: 1px solid #3a3a3c;
  border-radius: 0;
  background: transparent;
  font: 600 20px/1.3 -apple-system, BlinkMacSystemFont, system-ui, sans-serif;
  outline: none;
}
.script-title:focus { border-bottom-color: var(--gold); }
.script-text {
  flex: 1;
  min-height: 0;
  padding: 12px;
  border: 1px solid #3a3a3c;
  border-radius: 14px;
  background: #0d0d0d;
  font: 17px/1.5 -apple-system, BlinkMacSystemFont, system-ui, sans-serif;
  resize: none;
  outline: none;
}
.script-text:focus { border-color: var(--gold); }
.script-stats {
  margin: -4px 0 0;
  color: var(--fg-muted);
  font-size: 13px;
}
.home-actions {
  display: flex;
  gap: 12px;
}
.home-actions .btn-secondary { flex: 1; }
.home-actions .btn-primary { flex: 2; min-width: 0; }

/* ---------- Controlli di registrazione ---------- */

.controls {
  position: absolute;
  left: 0;
  right: 0;
  bottom: calc(24px + var(--safe-bottom));
  display: flex;
  justify-content: center;
  pointer-events: none;
}

.record {
  position: relative;
  width: 80px;
  height: 80px;
  padding: 0;
  border: 4px solid var(--fg);
  border-radius: 50%;
  background: rgba(0, 0, 0, 0.25);
  pointer-events: auto;
}
.record::after {
  content: "";
  position: absolute;
  inset: 6px;
  border-radius: 50%;
  background: var(--danger);
  transition: inset 0.2s ease, border-radius 0.2s ease, background 0.2s ease;
}
.record:disabled { opacity: 0.4; }
.record:active::after { opacity: 0.75; }
body[data-state="countdown"] .record::after,
body[data-state="recording"] .record::after {
  inset: 22px;
  border-radius: 8px;
}
body[data-state="countdown"] .record::after { background: var(--fg); }

/* Durante la registrazione il timer prende il posto dell'indicatore, accanto al pulsante */
.timer {
  position: absolute;
  left: calc(12px + var(--safe-left));
  bottom: calc(24px + 24px + var(--safe-bottom));
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px 14px;
  border-radius: 18px;
  background: var(--panel);
  font: 600 17px/1.2 ui-monospace, "SF Mono", Menlo, monospace;
  font-variant-numeric: tabular-nums;
}
.rec-dot {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background: var(--danger);
  animation: blink 1s steps(1) infinite;
}
@keyframes blink { 50% { opacity: 0.2; } }

.countdown {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 160px;
  font-weight: 700;
  text-shadow: 0 4px 24px rgba(0, 0, 0, 0.6);
  pointer-events: none;
}
.countdown.tick { animation: pop 1s ease-out; }
@keyframes pop {
  from { transform: scale(1.4); opacity: 0; }
  20% { transform: scale(1); opacity: 1; }
  to { transform: scale(0.9); opacity: 0.6; }
}

/* ---------- Anteprima della registrazione ---------- */

.review {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  gap: 12px;
  padding: calc(12px + var(--safe-top)) calc(16px + var(--safe-right))
           calc(16px + var(--safe-bottom)) calc(16px + var(--safe-left));
  background: var(--bg);
}
.review video {
  flex: 1;
  min-height: 0;
  width: 100%;
  object-fit: contain;
  border-radius: 12px;
  background: #111;
}
.review-info {
  margin: 0;
  color: var(--fg-muted);
  font: 13px/1.4 ui-monospace, "SF Mono", Menlo, monospace;
  text-align: center;
  white-space: pre-line;
}
.review-actions {
  display: flex;
  gap: 12px;
}
.review-actions button { flex: 1; }

.btn-secondary {
  min-height: 56px;
  padding: 0 24px;
  border: 2px solid rgba(255, 255, 255, 0.35);
  border-radius: 28px;
  background: transparent;
  color: var(--fg);
  font-size: 17px;
  font-weight: 600;
}
.btn-secondary:active { opacity: 0.7; }

.toast {
  position: absolute;
  left: 50%;
  top: 45%;
  transform: translate(-50%, -50%);
  width: max-content;
  max-width: calc(100% - 32px);
  padding: 12px 18px;
  border-radius: 14px;
  background: rgba(30, 30, 30, 0.92);
  color: var(--fg);
  font-size: 15px;
  line-height: 1.35;
  text-align: center;
  z-index: 10;
}

.wake-video {
  position: fixed;
  left: 0;
  top: 0;
  width: 1px;
  height: 1px;
  opacity: 0;
  pointer-events: none;
}

/* ---------- Indicatore risoluzione ---------- */

.info {
  position: absolute;
  left: calc(12px + var(--safe-left));
  /* centrato in verticale accanto al pulsante di registrazione */
  bottom: calc(24px + 18px + var(--safe-bottom));
  padding: 6px 10px;
  border: 0;
  border-radius: 8px;
  background: var(--panel);
  color: var(--fg-muted);
  font: 600 12px/1.3 ui-monospace, "SF Mono", Menlo, monospace;
  letter-spacing: 0.02em;
  text-align: left;
  white-space: pre;
}
.info::before {
  content: "";
  display: inline-block;
  width: 7px;
  height: 7px;
  margin-right: 6px;
  border-radius: 50%;
  background: var(--fg-muted);
  vertical-align: 1px;
}
body:not([data-state="idle"]) .info { display: none; }
.info.ok::before { background: var(--ok); }
.info.warn::before { background: var(--warn); }

.info-details {
  position: absolute;
  left: calc(12px + var(--safe-left));
  right: calc(12px + var(--safe-right));
  bottom: calc(24px + 80px + 14px + 44px + 12px + var(--safe-bottom));
  margin: 0;
  padding: 10px 12px;
  border-radius: 10px;
  background: rgba(0, 0, 0, 0.75);
  color: var(--fg);
  font: 12px/1.45 ui-monospace, "SF Mono", Menlo, monospace;
  white-space: pre-wrap;
}

.message {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: calc(24px + var(--safe-top)) calc(24px + var(--safe-right))
           calc(24px + var(--safe-bottom)) calc(24px + var(--safe-left));
  background: rgba(0, 0, 0, 0.85);
}
.message-box {
  max-width: 360px;
  text-align: center;
}
.message-box h1 {
  margin: 0 0 12px;
  font-size: 22px;
}
.message-box p {
  margin: 0 0 24px;
  color: var(--fg-muted);
  font-size: 16px;
  line-height: 1.45;
  white-space: pre-line;
}

.btn-primary {
  min-width: 160px;
  min-height: 56px;
  padding: 0 24px;
  border: 0;
  border-radius: 28px;
  background: var(--fg);
  color: var(--bg);
  font-size: 17px;
  font-weight: 600;
}
.btn-primary:active { opacity: 0.7; }

/* Avviso per il browser interno delle app social */
.message.inapp {
  z-index: 8;
  background: var(--bg);
}
.inapp-icon {
  display: block;
  margin: 0 auto 20px;
  border-radius: 16px;
}
.inapp-actions {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 12px;
}
.inapp-actions .btn-primary { width: 100%; max-width: 280px; }
