:root {
  --bg: #0f1115;
  --surface: #181b22;
  --surface-2: #222631;
  --text: #eceef3;
  --muted: #9aa1b2;
  --accent: #6ea8fe;
  --danger: #ff6b6b;
  --ok: #4cc38a;
  --gap: 3px;
  --top: calc(56px + env(safe-area-inset-top));
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; background: var(--bg); color: var(--text); }
body {
  font: 15px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  min-height: 100vh;
  padding-bottom: env(safe-area-inset-bottom);
}
a { color: inherit; text-decoration: none; }
svg { width: 24px; height: 24px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }

/* Barra superiore */
.topbar {
  position: sticky; top: 0; z-index: 10;
  display: flex; align-items: center; gap: 8px;
  height: var(--top);
  padding: env(safe-area-inset-top) 8px 0;
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: blur(12px);
  border-bottom: 1px solid var(--surface-2);
}
.titlebox { flex: 1; min-width: 0; }
.titlebox h1 { margin: 0; font-size: 18px; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.crumbs { font-size: 12px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.iconbtn {
  display: inline-grid; place-items: center;
  width: 44px; height: 44px; flex: none;
  border: 0; border-radius: 50%; background: transparent; color: var(--text);
  cursor: pointer;
}
.iconbtn:active { background: var(--surface-2); }
.logo { color: var(--accent); }

main { padding: 8px 0 24px; }

/* Messaggi */
.messages { padding: 8px 12px 0; }
.msg { padding: 10px 12px; border-radius: 10px; background: var(--surface); margin-bottom: 6px; }
.msg-error { border-left: 3px solid var(--danger); }
.msg-success { border-left: 3px solid var(--ok); }

/* Cartelle */
.folders {
  display: grid; gap: 12px;
  grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
  padding: 4px 12px 16px;
}
@media (max-width: 400px) { .folders { grid-template-columns: repeat(2, 1fr); } }
.folder { display: flex; flex-direction: column; gap: 6px; min-width: 0; }
.folder-cover {
  aspect-ratio: 1; border-radius: 14px; overflow: hidden;
  background: var(--surface); display: grid; place-items: center; color: var(--muted);
}
.folder-cover img { width: 100%; height: 100%; object-fit: cover; display: block; }
.folder-cover svg { width: 48px; height: 48px; }
.folder-name { font-size: 14px; font-weight: 500; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; padding: 0 2px; }

/* Griglia foto */
.count { margin: 0; padding: 0 12px 8px; color: var(--muted); font-size: 13px; }
.grid {
  display: grid; gap: var(--gap);
  grid-template-columns: repeat(auto-fill, minmax(110px, 1fr));
}
@media (max-width: 480px) { .grid { grid-template-columns: repeat(3, 1fr); } }
.tile {
  position: relative; aspect-ratio: 1; padding: 0; border: 0;
  background: var(--surface); cursor: pointer; overflow: hidden;
}
.tile img { width: 100%; height: 100%; object-fit: cover; display: block; }
.tile img.broken { visibility: hidden; }
.play {
  position: absolute; right: 6px; bottom: 6px;
  width: 26px; height: 26px; border-radius: 50%;
  display: grid; place-items: center;
  background: rgba(0, 0, 0, .55); color: #fff;
}
.play svg { width: 14px; height: 14px; fill: currentColor; stroke: none; }
.empty { text-align: center; color: var(--muted); padding: 48px 16px; }

/* Visualizzatore */
.viewer {
  position: fixed; inset: 0; z-index: 100;
  background: #000; touch-action: none; user-select: none;
}
.viewer[hidden] { display: none; }
.viewer-stage { position: absolute; inset: 0; overflow: hidden; }
.slide {
  position: absolute; inset: 0;
  display: grid; place-items: center;
  will-change: transform;
}
.slide img, .slide video {
  max-width: 100%; max-height: 100%;
  object-fit: contain; display: block;
}
.slide img { transform-origin: center center; transition: transform .2s ease; }
.slide .loading { position: absolute; color: var(--muted); font-size: 13px; }
.slide .novideo { color: var(--muted); text-align: center; padding: 24px; }
.slide .novideo a { color: var(--accent); }
.viewer-bar {
  position: absolute; left: 0; right: 0;
  display: flex; align-items: center; gap: 8px;
  padding: 6px 8px; color: #fff;
  transition: opacity .2s;
}
.viewer-top { top: 0; padding-top: calc(6px + env(safe-area-inset-top)); background: linear-gradient(rgba(0,0,0,.6), transparent); }
.viewer-bottom { bottom: 0; justify-content: center; padding-bottom: calc(10px + env(safe-area-inset-bottom)); font-size: 13px; color: #ccc; background: linear-gradient(transparent, rgba(0,0,0,.5)); }
.viewer-name { flex: 1; min-width: 0; font-size: 14px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.viewer .iconbtn { color: #fff; }
.viewer .iconbtn:active { background: rgba(255,255,255,.15); }
.nav {
  position: absolute; top: 50%; transform: translateY(-50%);
  width: 48px; height: 48px; border-radius: 50%; border: 0;
  background: rgba(0,0,0,.4); color: #fff; cursor: pointer;
  display: grid; place-items: center;
}
.nav-prev { left: 12px; }
.nav-next { right: 12px; }
.viewer.chrome-hidden .viewer-bar, .viewer.chrome-hidden .nav { opacity: 0; pointer-events: none; }
@media (hover: none) { .nav { display: none; } }

/* Pannelli / form */
.panel {
  margin: 12px; padding: 16px;
  background: var(--surface); border-radius: 14px;
}
.panel h2 { margin: 0 0 8px; font-size: 16px; }
.panel p { margin: 0 0 12px; color: var(--muted); }
.panel form { display: flex; flex-direction: column; gap: 8px; }
.panel label { font-weight: 500; }
.panel small { color: var(--muted); }
.panel input[type=text], .panel input[type=password] {
  width: 100%; padding: 12px; font-size: 16px;
  color: var(--text); background: var(--bg);
  border: 1px solid var(--surface-2); border-radius: 10px;
}
.panel input:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
.error { color: var(--danger); font-size: 14px; }
.btn {
  margin-top: 6px; padding: 12px 16px; font-size: 15px; font-weight: 600;
  border: 0; border-radius: 10px; cursor: pointer;
  background: var(--accent); color: #0b1020;
}
.btn-secondary { background: var(--surface-2); color: var(--text); }
.login { max-width: 400px; margin: 48px auto; }

/* Account e amministrazione */
.muted { color: var(--muted); }
.small { font-size: 12px; }
.mt { margin-top: 8px; }
code { font: 13px ui-monospace, monospace; color: var(--text); background: var(--surface-2); padding: 1px 5px; border-radius: 5px; word-break: break-all; }
a.btn { display: inline-block; text-align: center; }
.btn-danger { background: var(--danger); color: #1a0505; }
.actions { display: flex; flex-wrap: wrap; gap: 8px; }
.actions form { display: contents; }
.actions .btn { margin-top: 0; }
.link { color: var(--accent); font-size: 14px; }
.field { display: flex; flex-direction: column; gap: 4px; margin-bottom: 6px; }
.field ul { margin: 2px 0 0; padding-left: 18px; color: var(--muted); font-size: 12px; }
.field-check label { display: flex; align-items: center; gap: 10px; font-weight: 500; }
.field-check input { width: 20px; height: 20px; accent-color: var(--accent); }
.panel input[type=email] {
  width: 100%; padding: 12px; font-size: 16px;
  color: var(--text); background: var(--bg);
  border: 1px solid var(--surface-2); border-radius: 10px;
}
.badge {
  display: inline-block; margin-left: 4px; padding: 1px 8px; border-radius: 99px;
  font-size: 11px; font-weight: 600; vertical-align: middle;
  background: color-mix(in srgb, var(--accent) 25%, transparent); color: var(--accent);
}
.badge-off { background: var(--surface-2); color: var(--muted); }
.badge-danger { background: color-mix(in srgb, var(--danger) 25%, transparent); color: var(--danger); }
.badge-warn { background: color-mix(in srgb, #f5b74e 25%, transparent); color: #f5b74e; }

.list { list-style: none; margin: 0; padding: 4px 12px; display: flex; flex-direction: column; gap: 8px; }
.list a { display: block; padding: 12px 14px; background: var(--surface); border-radius: 12px; }
.list a:active { background: var(--surface-2); }
.list-sub { margin-top: 4px; font-size: 13px; color: var(--muted); }

.log { list-style: none; margin: 0 0 10px; padding: 0; }
.log li { display: flex; gap: 10px; padding: 8px 0; border-bottom: 1px solid var(--surface-2); }
.log li:last-child { border-bottom: 0; }
.log .ua { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 80vw; }
.dot { flex: none; width: 10px; height: 10px; margin-top: 5px; border-radius: 50%; }
.dot-ok { background: var(--ok); }
.dot-bad { background: var(--danger); }
.dot-warn { background: #f5b74e; }

.filters { display: flex; gap: 8px; padding: 4px 12px; }
.filters select {
  flex: 1; min-width: 0; padding: 10px; font-size: 15px;
  color: var(--text); background: var(--surface); border: 1px solid var(--surface-2); border-radius: 10px;
}
.pager { display: flex; align-items: center; justify-content: center; gap: 12px; padding: 0 12px 16px; }

/* Stato miniature */
.progress { height: 8px; border-radius: 99px; background: var(--surface-2); overflow: hidden; margin-bottom: 12px; }
.progress-bar { height: 100%; background: var(--accent); transition: width .4s; }
.stats { display: grid; grid-template-columns: repeat(auto-fill, minmax(140px, 1fr)); gap: 10px; margin: 0 0 12px; }
.stats dt { font-size: 12px; color: var(--muted); }
.stats dd { margin: 2px 0 0; font-size: 16px; font-weight: 600; }
.panel input[type=number] {
  width: 100%; padding: 12px; font-size: 16px;
  color: var(--text); background: var(--bg);
  border: 1px solid var(--surface-2); border-radius: 10px;
}
.btn:disabled { opacity: .5; cursor: default; }

/* Selettore cartelle */
.picker-open { align-self: flex-start; margin-top: 4px; }
.picker-head { display: flex; align-items: center; gap: 6px; margin-bottom: 8px; }
.picker-path { flex: 1; min-width: 0; }
.picker-list { list-style: none; margin: 0 0 8px; padding: 0; max-height: 50vh; overflow-y: auto; }
.picker-list li { border-bottom: 1px solid var(--surface-2); }
.picker-list li.muted { padding: 10px 4px; }
.picker-list button {
  width: 100%; text-align: left; padding: 12px 4px; font-size: 15px;
  background: none; border: 0; color: var(--text); cursor: pointer;
}
.picker-list button::before { content: "📁 "; }
.picker-list button:active { background: var(--surface-2); }
