:root {
  --bg: #ffffff;
  --fg: #111111;
  --muted: #777777;
  --border: #e4e4e4;
  --border-strong: #111111;
  --accent: #111111;
  --panel-bg: #fafafa;
  --radius: 2px;
  --danger: #c0392b;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  background: var(--bg);
  color: var(--fg);
  font-family: -apple-system, BlinkMacSystemFont, "Helvetica Neue", Arial, sans-serif;
  font-size: 13px;
  -webkit-font-smoothing: antialiased;
}

#app { height: 100vh; display: flex; flex-direction: column; }

/* ---------- Top bar ---------- */
.topbar {
  height: 52px;
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 20px;
  border-bottom: 1px solid var(--border);
}
.brand {
  font-weight: 600;
  font-size: 14px;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  cursor: pointer;
}
.brand span { color: var(--muted); font-weight: 400; text-transform: none; letter-spacing: 0; margin-left: 8px; }

.btn {
  background: var(--fg);
  color: #fff;
  border: 1px solid var(--fg);
  border-radius: var(--radius);
  padding: 8px 14px;
  font-size: 12px;
  cursor: pointer;
  letter-spacing: 0.02em;
}
.btn:hover { opacity: 0.85; }
.btn.secondary { background: transparent; color: var(--fg); }
.btn.ghost { background: transparent; color: var(--muted); border-color: var(--border); }
.btn:disabled { opacity: 0.35; cursor: default; }
.btn-row { display: flex; gap: 8px; align-items: center; }

/* ---------- Dashboard ---------- */
#dashboard { flex: 1; overflow: auto; padding: 32px; }
.dash-header { margin-bottom: 24px; }
.dash-header h1 { font-size: 20px; font-weight: 600; margin: 0 0 4px; }
.dash-header p { color: var(--muted); margin: 0; }

.template-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
  gap: 20px;
}
.template-card {
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
  cursor: pointer;
  background: #fff;
  transition: border-color .15s ease, transform .15s ease;
}
.template-card:hover { border-color: var(--border-strong); }
.template-card .thumb {
  width: 100%;
  aspect-ratio: 1080 / 1350;
  background: #f1f1f1 center/cover no-repeat;
  display: flex; align-items: center; justify-content: center;
  color: var(--muted); font-size: 11px;
}
.template-card .meta { padding: 10px 12px; border-top: 1px solid var(--border); }
.template-card .meta .name { font-weight: 600; font-size: 13px; }
.template-card .meta .sub { color: var(--muted); font-size: 11px; margin-top: 2px; }

.project-list { margin-top: 32px; }
.project-list h2 { font-size: 13px; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); font-weight: 600; margin-bottom: 12px; }
.project-row {
  display: flex; align-items: center; justify-content: space-between;
  padding: 10px 12px; border: 1px solid var(--border); border-radius: var(--radius); margin-bottom: 8px; cursor: pointer;
}
.project-row:hover { border-color: var(--border-strong); }
.project-row .p-name { font-weight: 500; }
.project-row .p-sub { color: var(--muted); font-size: 11px; }

/* ---------- archivio ---------- */
.archive-section { margin-top: 32px; max-width: 640px; }
.archive-section h2 { font-size: 13px; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); font-weight: 600; margin-bottom: 12px; }
.storage-categories { border: 1px solid var(--border); border-radius: var(--radius); overflow: hidden; }
.storage-row {
  display: flex; align-items: center; gap: 12px;
  padding: 9px 12px; font-size: 12px; border-bottom: 1px solid var(--border);
}
.storage-row:last-child { border-bottom: none; }
.storage-row .s-label { flex: 0 0 140px; font-weight: 500; }
.storage-row .s-bar-track { flex: 1; height: 6px; background: #f0f0f0; border-radius: 3px; overflow: hidden; }
.storage-row .s-bar-fill { height: 100%; background: var(--fg); border-radius: 3px; }
.storage-row .s-size { flex: 0 0 90px; text-align: right; color: var(--muted); font-variant-numeric: tabular-nums; }
.storage-row .s-count { flex: 0 0 70px; text-align: right; color: var(--muted); font-size: 10.5px; }
.storage-total { margin-top: 10px; font-size: 11.5px; color: var(--muted); }
.storage-actions { margin-top: 12px; }

/* ---------- Editor ---------- */
#editor { flex: 1; display: none; min-height: 0; flex-direction: column; }
#editor.active { display: flex; }

.editor-body { flex: 1; display: flex; min-height: 0; }

.col-fields {
  width: 320px;
  flex: 0 0 320px;
  border-right: 1px solid var(--border);
  overflow-y: auto;
  padding: 20px;
  background: var(--panel-bg);
}
.col-fields h3 { font-size: 11px; text-transform: uppercase; letter-spacing: .05em; color: var(--muted); margin: 18px 0 8px; }
.col-fields h3:first-child { margin-top: 0; }

.field { margin-bottom: 14px; }
.field label { display: block; font-size: 11.5px; font-weight: 600; margin-bottom: 5px; }
.field .hint { color: var(--muted); font-size: 10.5px; margin-top: 3px; }
.field .hint.warn { color: var(--danger); }
textarea, input[type=text] {
  width: 100%;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 7px 8px;
  font-family: inherit;
  font-size: 12.5px;
  resize: vertical;
  background: #fff;
  color: var(--fg);
}
textarea:focus, input[type=text]:focus { outline: none; border-color: var(--border-strong); }

.image-drop {
  border: 1px dashed var(--border);
  border-radius: var(--radius);
  padding: 10px;
  text-align: center;
  color: var(--muted);
  font-size: 11px;
  cursor: pointer;
  background: #fff;
  position: relative;
  overflow: hidden;
  min-height: 70px;
  display: flex; align-items: center; justify-content: center;
}
.image-drop.has-image { padding: 0; }
.image-drop img { width: 100%; display: block; }
.image-drop.drag-over { border-color: var(--border-strong); background: #f3f3f3; }
.image-drop .remove-img {
  position: absolute; top: 4px; right: 4px; background: rgba(0,0,0,.65); color: #fff;
  border: none; border-radius: 50%; width: 18px; height: 18px; font-size: 11px; cursor: pointer; line-height: 1;
}
.crop-controls { display: flex; align-items: center; gap: 6px; margin-top: 6px; }
.crop-controls input[type=range] { flex: 1; }
.crop-controls .axis-label { font-size: 10px; color: var(--muted); width: 14px; }

.col-preview {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  background: #efefef;
  overflow: auto;
  position: relative;
}
.preview-frame {
  position: relative;
  display: inline-block;
  box-shadow: 0 2px 24px rgba(0,0,0,.12);
  background: #fff repeating-conic-gradient(#f3f3f3 0% 25%, #fff 0% 50%) 50%/16px 16px;
  max-height: calc(100vh - 230px);
  max-width: 92%;
  line-height: 0;
}
.preview-frame img { display: block; max-height: calc(100vh - 230px); max-width: 100%; user-select: none; -webkit-user-drag: none; }
.preview-empty { color: var(--muted); font-size: 12px; text-align: center; line-height: 1.4; }
.preview-loading {
  position: absolute; top: 12px; right: 12px; font-size: 11px; color: var(--muted);
  background: #fff; border: 1px solid var(--border); padding: 4px 10px; border-radius: var(--radius);
  z-index: 20;
}
.preview-warnings {
  position: absolute; left: 12px; bottom: 12px; right: 12px;
  display: flex; flex-direction: column; gap: 4px;
  z-index: 15;
}
.warn-pill {
  background: #fff3e0; border: 1px solid #e6a23c; color: #7a4a00;
  font-size: 11px; padding: 6px 10px; border-radius: var(--radius);
}

/* ---------- editor visuale: overlay interattivo sul PNG renderizzato ---------- */
#overlay-layer {
  position: absolute; inset: 0;
  pointer-events: none; /* solo i box/maniglie dentro sono interattivi */
}
.ov-box {
  position: absolute;
  pointer-events: auto;
  cursor: pointer;
  box-sizing: border-box;
  border: 1px solid transparent;
  touch-action: none; /* i gesti (pan/resize/move) sono gestiti via Pointer Events, non lo scroll nativo */
}
/* A riposo il box non disegna NULLA sopra il PNG reale (niente sfondo, niente contenuto
   visibile): solo un contorno sottile al passaggio del mouse o da selezionato. È la
   condizione che evita che una foto appena sostituita copra testo/loghi nell'anteprima. */
.ov-box:hover { border-color: rgba(17,17,17,.35); }
.ov-box.selected { border: 1.5px solid #111; }
.ov-box.image.crop-mode { border: 1.5px dashed #1a6fd6; cursor: grab; }
.ov-box.image.crop-mode:active { cursor: grabbing; }

.ov-content {
  position: absolute; inset: 0; display: none; align-items: center; justify-content: center;
  text-align: center; overflow: hidden; pointer-events: none;
  background: rgba(255,255,255,.88); color: #111; font-weight: 700;
  font-family: -apple-system, "Helvetica Neue", Arial, sans-serif;
  padding: 2px; box-sizing: border-box; line-height: 1.1; word-break: break-word;
}
/* Il contenuto testuale dell'overlay si vede SOLO quando serve davvero a non far sparire
   un testo che nel documento reale sta sopra una foto in modalità ritaglio attiva — mai a
   riposo (dove il PNG reale, già composto correttamente da DesignCraft, è sufficiente). */
.ov-box.crop-guard .ov-content { display: flex; }
.ov-box.editing .ov-content { display: none; }

.ov-missing-mark {
  position: absolute; inset: 0; display: none; align-items: center; justify-content: center;
  font-size: 22px; color: rgba(17,17,17,.3); pointer-events: none;
}
.ov-box.missing-image .ov-missing-mark { display: flex; }

.ov-crop-badge {
  position: absolute; top: -20px; left: 0; display: none;
  background: #1a6fd6; color: #fff; font-size: 10px; font-weight: 600;
  padding: 2px 7px; border-radius: 2px; letter-spacing: .02em; pointer-events: none;
}
.ov-box.crop-mode .ov-crop-badge { display: block; }

.ov-handle {
  position: absolute; width: 10px; height: 10px; background: #111; border: 1.5px solid #fff;
  border-radius: 50%; z-index: 2; display: none; touch-action: none;
}
.ov-box.selected:not(.crop-mode) .ov-handle { display: block; }
.ov-handle.nw { left: -5px; top: -5px; cursor: nwse-resize; }
.ov-handle.ne { right: -5px; top: -5px; cursor: nesw-resize; }
.ov-handle.sw { left: -5px; bottom: -5px; cursor: nesw-resize; }
.ov-handle.se { right: -5px; bottom: -5px; cursor: nwse-resize; }

.ov-edit-textarea {
  position: absolute; inset: 0; width: 100%; height: 100%;
  resize: none; border: 1.5px solid #111; outline: none; background: rgba(255,255,255,.95);
  color: #111; font-family: inherit; padding: 2px; box-sizing: border-box;
  line-height: 1.15; z-index: 4;
}
.ov-zoom-controls {
  position: absolute; bottom: 4px; right: 4px; display: none; gap: 2px; z-index: 3;
}
.ov-box.crop-mode .ov-zoom-controls { display: flex; }
.ov-zoom-controls button {
  width: 22px; height: 22px; border: 1px solid rgba(255,255,255,.6); background: rgba(17,17,17,.75);
  color: #fff; font-size: 14px; line-height: 1; cursor: pointer; border-radius: 2px; touch-action: none;
}

/* ---------- pannello proprietà ---------- */
.property-panel {
  position: absolute; top: 12px; left: 12px; width: 220px;
  background: #fff; border: 1px solid var(--border); border-radius: var(--radius);
  box-shadow: 0 4px 18px rgba(0,0,0,.14); z-index: 25; font-size: 11.5px;
}
.pp-header {
  display: flex; align-items: center; justify-content: space-between;
  padding: 8px 10px; border-bottom: 1px solid var(--border); font-weight: 600;
}
.pp-close { border: none; background: none; cursor: pointer; font-size: 15px; color: var(--muted); line-height: 1; }
.pp-body { padding: 10px; display: flex; flex-direction: column; gap: 8px; }
.pp-row { display: flex; align-items: center; gap: 6px; }
.pp-row label { flex: 0 0 60px; color: var(--muted); font-size: 10.5px; }
.pp-row input[type=number] {
  flex: 1; width: 100%; border: 1px solid var(--border); border-radius: 2px; padding: 4px 6px; font-size: 11.5px;
}
.pp-align-group { display: flex; gap: 2px; flex: 1; }
.pp-align-group button {
  flex: 1; border: 1px solid var(--border); background: #fff; padding: 4px 0; cursor: pointer; font-size: 11px;
}
.pp-align-group button.active { background: #111; color: #fff; border-color: #111; }
.pp-actions { padding: 8px 10px; border-top: 1px solid var(--border); }
.pp-actions .btn { width: 100%; }
.pp-action-btn { width: 100%; margin-bottom: 2px; }

.field.sidebar-highlight { outline: 1.5px solid #111; outline-offset: 2px; border-radius: 2px; }

/* ---------- bottom slide strip ---------- */
.slide-strip {
  flex: 0 0 auto;
  border-top: 1px solid var(--border);
  padding: 10px 16px;
  display: flex;
  align-items: center;
  gap: 10px;
  overflow-x: auto;
  background: #fff;
}
.slide-thumb {
  flex: 0 0 auto;
  width: 54px;
  aspect-ratio: 1080/1350;
  border: 2px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
  cursor: pointer;
  position: relative;
  background: #f1f1f1 center/cover no-repeat;
}
.slide-thumb.active { border-color: var(--border-strong); }
.slide-thumb .idx { position: absolute; bottom: 2px; left: 3px; font-size: 9px; background: rgba(255,255,255,.85); padding: 0 3px; }
.slide-thumb .del {
  position: absolute; top: 2px; right: 2px; background: rgba(0,0,0,.6); color: #fff; border: none;
  width: 14px; height: 14px; font-size: 9px; border-radius: 50%; cursor: pointer; display: none;
}
.slide-thumb:hover .del { display: block; }
.slide-add {
  flex: 0 0 auto; width: 54px; aspect-ratio: 1080/1350; border: 1px dashed var(--border);
  display: flex; align-items: center; justify-content: center; font-size: 18px; color: var(--muted);
  cursor: pointer; border-radius: var(--radius);
}
.slide-add:hover { border-color: var(--border-strong); color: var(--fg); }

.editor-topbar { display: flex; align-items: center; justify-content: space-between; padding: 10px 20px; border-bottom: 1px solid var(--border); }
.editor-topbar .title-group { display: flex; align-items: center; gap: 10px; }
.editor-topbar input.proj-name { border: none; font-size: 14px; font-weight: 600; background: transparent; }
.editor-topbar input.proj-name:focus { outline: none; border-bottom: 1px solid var(--border-strong); }
.save-indicator { font-size: 10.5px; color: var(--muted); }

.modal-backdrop {
  position: fixed; inset: 0; background: rgba(0,0,0,.4); display: none; align-items: center; justify-content: center; z-index: 50;
}
.modal-backdrop.active { display: flex; }
.modal {
  background: #fff; border-radius: var(--radius); padding: 24px; width: 320px;
}
.modal h3 { margin: 0 0 14px; font-size: 14px; }
.modal .btn-row { margin-top: 16px; justify-content: flex-end; }

.hidden { display: none !important; }

/* ---------- adattamenti iPad / touch (PWA) ---------- */
#boot-modal.active { display: flex; }

body { padding-top: env(safe-area-inset-top); padding-bottom: env(safe-area-inset-bottom); }
.btn { touch-action: manipulation; min-height: 34px; }
input, textarea, button { touch-action: manipulation; }

/* maniglie e controlli più grandi per un tocco preciso con il dito (le coordinate restano in
   punti documento: solo l'area di presa visiva/tattile cresce). */
.ov-handle { width: 22px; height: 22px; }
.ov-handle.nw { left: -11px; top: -11px; }
.ov-handle.ne { right: -11px; top: -11px; }
.ov-handle.sw { left: -11px; bottom: -11px; }
.ov-handle.se { right: -11px; bottom: -11px; }
.ov-zoom-controls button { width: 32px; height: 32px; font-size: 18px; }

@media (max-width: 900px) {
  .editor-body { flex-direction: column; }
  .col-fields { width: 100%; flex: 0 0 auto; max-height: 38vh; border-right: none; border-bottom: 1px solid var(--border); }
  .col-preview { flex: 1; }
  .preview-frame, .preview-frame img { max-height: calc(100vh - 460px); }
  .topbar { flex-wrap: wrap; height: auto; padding: 8px 12px; gap: 6px; }
  .btn-row { flex-wrap: wrap; }
}

#offline-status-box .storage-total.ok { color: #1a7a3c; }
#offline-status-box .storage-total.warn { color: var(--danger); }
