:root {
  --bg: #16171a;
  --panel: #1f2125;
  --panel-2: #272a2f;
  --line: #34373d;
  --text: #ececec;
  --muted: #9a9ea6;
  --accent: #e8a64b;
  --accent-ink: #1a1307;
  --danger: #e5625b;
  --radius: 10px;
  --top: env(safe-area-inset-top, 0px);
  --bottom: env(safe-area-inset-bottom, 0px);
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; background: var(--bg); color: var(--text); font: 15px/1.4 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif; -webkit-tap-highlight-color: transparent; }
#app { min-height: 100%; display: flex; flex-direction: column; }
button, input, select, textarea { font: inherit; color: inherit; }
.hidden { display: none !important; }
.spacer { flex: 1; }
.muted { color: var(--muted); }
.small { font-size: 13px; }
.print-only { display: none; }

/* ---------- Pulsanti e campi ---------- */
.btn { border: 1px solid var(--line); background: var(--panel-2); border-radius: 8px; padding: 8px 12px; cursor: pointer; white-space: nowrap; line-height: 1.1; }
.btn:hover { border-color: #4a4e56; }
.btn:active { transform: translateY(1px); }
.btn.primary { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); font-weight: 600; }
.btn.danger { color: var(--danger); }
.btn.ghost { background: transparent; }
.btn.icon { padding: 8px 10px; min-width: 36px; }
.btn.sm { padding: 5px 9px; font-size: 13px; }
.btn.big { padding: 12px 20px; font-size: 16px; }
.btn.block { display: block; width: 100%; text-align: left; margin-bottom: 8px; white-space: normal; }
.input, .select { width: 100%; background: var(--bg); border: 1px solid var(--line); border-radius: 8px; padding: 8px 10px; outline: none; }
.select { width: auto; padding: 6px 8px; }
.input:focus, .select:focus { border-color: var(--accent); }
textarea.input { resize: none; overflow: hidden; min-height: 38px; }

/* ---------- Barra in alto ---------- */
.topbar { display: flex; align-items: center; gap: 10px; padding: calc(10px + var(--top)) 14px 10px; background: var(--panel); border-bottom: 1px solid var(--line); position: sticky; top: 0; z-index: 20; }
.brand { display: flex; align-items: center; gap: 10px; font-size: 17px; }
.title-input { background: transparent; border: 1px solid transparent; border-radius: 8px; padding: 6px 8px; font-size: 17px; font-weight: 600; min-width: 0; flex: 0 1 380px; }
.title-input:hover, .title-input:focus { border-color: var(--line); outline: none; }
.status { font-size: 12px; color: var(--muted); white-space: nowrap; }
.status::before { content: ""; display: inline-block; width: 7px; height: 7px; border-radius: 50%; background: #5bbf73; margin-right: 6px; vertical-align: 1px; }
.status[data-s="pending"]::before, .status[data-s="saving"]::before { background: var(--accent); }
.status[data-s="error"]::before { background: var(--danger); }
.toolbar { display: flex; align-items: center; gap: 8px; padding: 8px 14px; background: var(--panel); border-bottom: 1px solid var(--line); overflow-x: auto; scrollbar-width: none; position: sticky; top: calc(57px + var(--top)); z-index: 19; }
.toolbar::-webkit-scrollbar { display: none; }
.inline { display: flex; align-items: center; gap: 6px; color: var(--muted); font-size: 13px; white-space: nowrap; }
.total { font-size: 13px; white-space: nowrap; }

/* ---------- Logo ---------- */
.logo { display: grid; grid-template-columns: 1fr 1fr; gap: 6px; width: 64px; height: 64px; margin: 0 auto 16px; }
.logo span { border-radius: 6px; background: var(--accent); }
.logo span:nth-child(2) { background: #d9d4cc; }
.logo span:nth-child(3) { background: #6f8fb3; }
.logo span:nth-child(4) { background: #3b3e44; }
.logo.sm { width: 24px; height: 24px; gap: 3px; margin: 0; }
.logo.sm span { border-radius: 3px; }

/* ---------- Login ---------- */
.login { flex: 1; display: grid; place-items: center; padding: 24px; }
.login-card { text-align: center; max-width: 360px; }
.login-card h1 { margin: 0 0 6px; font-size: 28px; }
.login-card p { color: var(--muted); margin: 0 0 24px; }
.login-card .err { color: var(--danger); }

/* ---------- Home ---------- */
.home { padding: 18px 14px calc(30px + var(--bottom)); max-width: 1200px; width: 100%; margin: 0 auto; }
.home-actions { display: flex; gap: 10px; flex-wrap: wrap; margin-bottom: 18px; }
.boards { display: grid; grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); gap: 14px; }
.tile { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); overflow: hidden; cursor: pointer; transition: border-color .15s; }
.tile:hover { border-color: #4a4e56; }
.tile-cover { aspect-ratio: 16 / 10; background: var(--panel-2) center / cover no-repeat; position: relative; }
.tile-type { position: absolute; left: 8px; top: 8px; font-size: 11px; background: rgba(0,0,0,.55); padding: 3px 7px; border-radius: 20px; }
.tile-info { display: flex; align-items: center; gap: 8px; padding: 10px 10px 10px 12px; }
.tile-info > div { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.tile-info b { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.tile-info small { color: var(--muted); }
.empty { grid-column: 1 / -1; text-align: center; padding: 60px 20px; color: var(--muted); }
.empty h2 { color: var(--text); margin: 0 0 6px; }
.loading { flex: 1; display: grid; place-items: center; }

/* ---------- Tela moodboard ---------- */
.board-body { flex: 1; display: flex; flex-direction: column; min-height: 0; position: relative; }
.canvas { position: relative; flex: 1; overflow: hidden; touch-action: none; outline: none; height: calc(100dvh - 114px - var(--top)); background-color: #121316; background-image: radial-gradient(#2a2d33 1px, transparent 1px); user-select: none; -webkit-user-select: none; cursor: grab; }
.canvas:active { cursor: grabbing; }
.canvas.dropping { box-shadow: inset 0 0 0 3px var(--accent); }
.world { position: absolute; left: 0; top: 0; transform-origin: 0 0; }
.canvas-hint { position: absolute; inset: auto 0 calc(24px + var(--bottom)) 0; text-align: center; color: var(--muted); font-size: 13px; padding: 0 20px; pointer-events: none; }

.card { position: absolute; border-radius: 8px; box-shadow: 0 2px 10px rgba(0,0,0,.35); cursor: move; }
.card.sel { outline: 2px solid var(--accent); outline-offset: 2px; }
.card-image { background: var(--panel-2); overflow: hidden; }
.card-image img { width: 100%; height: 100%; object-fit: cover; display: block; pointer-events: none; }
.card-image.remote::after { content: "link"; position: absolute; right: 6px; top: 6px; font-size: 10px; background: rgba(0,0,0,.6); padding: 2px 6px; border-radius: 10px; }
.card-image.broken { background: repeating-linear-gradient(45deg, #2a2c31 0 8px, #24262a 8px 16px); }
.card .caption { position: absolute; left: 0; right: 0; bottom: 0; padding: 18px 10px 8px; font-size: 13px; background: linear-gradient(transparent, rgba(0,0,0,.75)); }
.card-note { color: #1d1d1d; padding: 12px 14px; min-height: 60px; }
.note-text { white-space: pre-wrap; word-break: break-word; font-size: 15px; outline: none; min-height: 1.4em; }
.card.editing { cursor: text; }
.card.editing .note-text { user-select: text; -webkit-user-select: text; }
.card-link { background: var(--panel-2); border: 1px solid var(--line); padding: 10px 12px; }
.link-host { font-size: 12px; color: var(--accent); }
.link-url { font-size: 13px; word-break: break-all; }
.resize { position: absolute; right: -6px; bottom: -6px; width: 18px; height: 18px; border-radius: 50%; background: var(--accent); border: 3px solid var(--bg); cursor: nwse-resize; opacity: 0; }
.card.sel .resize { opacity: 1; }
.card.pending img { opacity: .55; }

.selbar { position: absolute; z-index: 10; display: flex; flex-wrap: wrap; align-items: center; gap: 6px; background: var(--panel); border: 1px solid var(--line); border-radius: 10px; padding: 6px; box-shadow: 0 6px 20px rgba(0,0,0,.45); max-width: calc(100% - 16px); }
.swatch { width: 22px; height: 22px; border-radius: 50%; border: 2px solid transparent; cursor: pointer; padding: 0; }
.swatch.on { border-color: var(--accent); }

/* ---------- Storyboard ---------- */
.sb-wrap { padding: 16px 14px calc(40px + var(--bottom)); }
.sb-grid { --ar: 16 / 9; display: grid; grid-template-columns: repeat(auto-fill, minmax(280px, 1fr)); gap: 14px; max-width: 1600px; margin: 0 auto; }
.sb-grid[data-aspect="9:16"], .sb-grid[data-aspect="4:5"] { grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); }
.frame { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); display: flex; flex-direction: column; overflow: hidden; }
.frame.over { border-color: var(--accent); box-shadow: 0 0 0 2px var(--accent); }
.frame.dragging { opacity: .4; }
.frame-head { display: flex; align-items: center; gap: 6px; padding: 8px; }
.handle { cursor: grab; color: var(--muted); padding: 2px 4px; font-size: 16px; user-select: none; }
.num { background: var(--accent); color: var(--accent-ink); border-radius: 6px; min-width: 24px; text-align: center; padding: 1px 6px; font-size: 13px; }
.frame-head { padding-bottom: 2px; }
.frame-title { margin: 0 8px 8px; background: transparent; border: 1px solid transparent; border-radius: 6px; padding: 5px 6px; font-weight: 600; font-size: 15px; min-width: 0; }
.frame-title:focus { border-color: var(--line); outline: none; }
.frame-actions { display: flex; gap: 2px; }
.frame-actions .btn { padding: 3px 7px; min-width: 0; background: transparent; border-color: transparent; }
.frame-actions .btn:hover { border-color: var(--line); }
.frame-media { aspect-ratio: var(--ar); background: #121316; position: relative; cursor: pointer; overflow: hidden; border-top: 1px solid var(--line); border-bottom: 1px solid var(--line); }
.frame-media img { width: 100%; height: 100%; object-fit: cover; display: block; }
.frame-media.pending img { opacity: .55; }
.media-empty { position: absolute; inset: 0; display: grid; place-content: center; text-align: center; color: var(--muted); font-size: 13px; border: 1px dashed var(--line); margin: 8px; border-radius: 8px; }
.media-x { position: absolute; top: 6px; right: 6px; width: 26px; height: 26px; border-radius: 50%; border: 0; background: rgba(0,0,0,.6); cursor: pointer; font-size: 16px; line-height: 1; }
.media-src { position: absolute; left: 6px; bottom: 6px; font-size: 11px; background: rgba(0,0,0,.6); padding: 2px 7px; border-radius: 10px; color: var(--text); text-decoration: none; }
.frame-fields { display: flex; flex-wrap: wrap; gap: 8px; padding: 10px; }
.field { display: flex; flex-direction: column; gap: 3px; width: 100%; }
.field.half { width: calc(33.33% - 6px); min-width: 80px; flex: 1; }
.field > span { font-size: 11px; color: var(--muted); text-transform: uppercase; letter-spacing: .04em; }
.field .input { padding: 6px 8px; font-size: 14px; }
.frame.add { display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 6px; min-height: 220px; border-style: dashed; color: var(--muted); cursor: pointer; background: transparent; }
.frame.add span { font-size: 34px; line-height: 1; }

/* ---------- Spinner, toast, modali ---------- */
.spinner { position: absolute; left: 50%; top: 50%; width: 26px; height: 26px; margin: -13px; border-radius: 50%; border: 3px solid rgba(255,255,255,.25); border-top-color: var(--accent); animation: spin .8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
.toast { position: fixed; left: 50%; bottom: calc(20px + var(--bottom)); transform: translate(-50%, 20px); background: #f2f2f2; color: #141414; padding: 10px 16px; border-radius: 10px; font-size: 14px; opacity: 0; pointer-events: none; transition: .2s; z-index: 100; max-width: calc(100% - 32px); text-align: center; }
.toast.show { opacity: 1; transform: translate(-50%, 0); }
.reconnect { position: fixed; left: 12px; right: 12px; bottom: calc(12px + var(--bottom)); background: var(--panel); border: 1px solid var(--accent); border-radius: 12px; padding: 12px; display: flex; gap: 12px; align-items: center; justify-content: space-between; z-index: 90; font-size: 14px; }
.modal-bg { position: fixed; inset: 0; background: rgba(0,0,0,.6); display: grid; place-items: center; z-index: 80; padding: 16px; }
.modal { background: var(--panel); border: 1px solid var(--line); border-radius: 14px; padding: 18px; width: min(460px, 100%); max-height: 85dvh; overflow: auto; }
.modal h2 { margin: 0 0 12px; font-size: 18px; }
.modal-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 16px; }
.settings label { display: block; font-size: 12px; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); margin: 12px 0 6px; }
.settings .row { display: flex; gap: 8px; flex-wrap: wrap; margin-top: 10px; }
.pick-list { margin-top: 10px; }
.lightbox { position: fixed; inset: 0; background: rgba(0,0,0,.9); z-index: 95; display: flex; flex-direction: column; align-items: center; justify-content: center; padding: 20px; cursor: zoom-out; }
.lightbox img { max-width: 100%; max-height: 85dvh; object-fit: contain; }
.lightbox p { margin-top: 12px; }

@media (max-width: 640px) {
  .topbar { gap: 6px; padding-left: 8px; padding-right: 8px; }
  .title-input { font-size: 16px; }
  .status { font-size: 0; }
  .status::before { margin: 0; }
  .sb-grid { grid-template-columns: 1fr; }
  .sb-grid[data-aspect="9:16"], .sb-grid[data-aspect="4:5"] { grid-template-columns: 1fr 1fr; }
  .field.half { min-width: 70px; }
  .sb-grid[data-aspect="9:16"] .field.half, .sb-grid[data-aspect="4:5"] .field.half { flex-basis: 100%; }
  .sb-grid[data-aspect="9:16"] .frame-actions .btn, .sb-grid[data-aspect="4:5"] .frame-actions .btn { padding: 3px 5px; }
}

/* ---------- Stampa / PDF storyboard ---------- */
@media print {
  @page { size: A4 landscape; margin: 12mm; }
  html, body { background: #fff; color: #111; }
  .topbar, .toolbar, .frame.add, .frame-actions, .handle, .media-x, .media-src, .toast, .reconnect { display: none !important; }
  .print-only { display: block; font-size: 20px; margin: 0 0 10px; }
  .sb-wrap { padding: 0; }
  .sb-grid { grid-template-columns: repeat(3, 1fr) !important; gap: 8px; }
  .sb-grid[data-aspect="9:16"], .sb-grid[data-aspect="4:5"] { grid-template-columns: repeat(4, 1fr) !important; }
  .frame { break-inside: avoid; border-color: #ccc; background: #fff; }
  .frame-media { background: #eee; border-color: #ccc; }
  .media-empty { display: none; }
  .num { background: #111; color: #fff; }
  .frame-title { margin: 0 6px 4px; }
  .frame-title, .input { color: #111; border: 0 !important; background: transparent; padding: 0; }
  .field > span { color: #666; }
  .input:placeholder-shown { display: none; }
}
