:root {
  color-scheme: light dark;
  --bg: #f6f5f3;
  --surface: #ffffff;
  --surface-2: #f0eeea;
  --text: #1b1c1f;
  --muted: #6b6f76;
  --border: #e0ddd7;
  --accent: #6d4aff;
  --accent-text: #ffffff;
  --keep: #16a34a;
  --home: #d97706;
  --toss: #dc2626;
  --danger: #dc2626;
  --radius: 14px;
  --shadow: 0 1px 3px rgb(0 0 0 / 8%);
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #111318;
    --surface: #1a1d24;
    --surface-2: #232733;
    --text: #eef0f4;
    --muted: #9aa1ad;
    --border: #2e3441;
    --accent: #8b6dff;
    --keep: #22c55e;
    --home: #f59e0b;
    --toss: #ef4444;
    --danger: #ef4444;
    --shadow: 0 1px 3px rgb(0 0 0 / 40%);
  }
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.5 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  -webkit-text-size-adjust: 100%;
}

.wrap {
  max-width: 560px;
  margin: 0 auto;
  padding: 16px 16px 48px;
}

/* --- Header / footer --- */
.topbar {
  position: sticky;
  top: 0;
  z-index: 10;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 12px 16px;
  padding-top: max(12px, env(safe-area-inset-top));
  background: color-mix(in srgb, var(--surface) 90%, transparent);
  backdrop-filter: blur(8px);
  border-bottom: 1px solid var(--border);
}
.brand { font-weight: 700; text-decoration: none; color: var(--text); }
.topbar nav { display: flex; align-items: center; gap: 12px; }
.topbar a, .linkbtn { color: var(--muted); font-size: 14px; text-decoration: none; }
.linkbtn { background: none; border: 0; padding: 0; cursor: pointer; font: inherit; font-size: 14px; }

.footer {
  display: flex;
  justify-content: center;
  gap: 20px;
  padding: 24px 16px calc(24px + env(safe-area-inset-bottom));
  border-top: 1px solid var(--border);
  font-size: 14px;
}
.footer a { color: var(--muted); text-decoration: none; }

h1 { font-size: 24px; margin: 8px 0 4px; }
h2 { font-size: 18px; margin: 28px 0 10px; }
p { margin: 8px 0; }
.muted { color: var(--muted); }
.hint { color: var(--muted); font-size: 14px; }
.center { text-align: center; }
.big-code { font-size: 48px; margin: 0; }
.crumb { font-size: 14px; }
.crumb a { color: var(--muted); text-decoration: none; }
.sr-only {
  position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px;
  overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; border: 0;
}

/* --- Card e liste --- */
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 16px;
  margin: 12px 0;
}
.card summary { cursor: pointer; font-weight: 600; }
.list { list-style: none; padding: 0; margin: 0; }
.card__link { display: flex; flex-direction: column; gap: 4px; text-decoration: none; color: inherit; }
.card__title { font-weight: 600; font-size: 17px; }
.meta { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 6px; }
.pill {
  display: inline-block; padding: 2px 10px; border-radius: 999px;
  background: var(--surface-2); color: var(--muted); font-size: 13px;
}
.pill--open { background: color-mix(in srgb, var(--keep) 18%, transparent); color: var(--keep); }
.pill--closed { background: color-mix(in srgb, var(--toss) 15%, transparent); color: var(--toss); }

.row { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; }
.row--between { justify-content: space-between; }
.inline { display: inline; margin: 0; }
.stack { display: flex; flex-direction: column; gap: 8px; margin-top: 12px; }

/* --- Form --- */
label { font-size: 14px; font-weight: 600; }
input[type="text"], input[type="password"], textarea, input[type="file"] {
  width: 100%;
  padding: 12px;
  font: inherit;
  font-size: 16px; /* evita lo zoom automatico su iOS */
  color: var(--text);
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 10px;
}
textarea { resize: vertical; }
input:focus-visible, textarea:focus-visible, button:focus-visible, a:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  width: 100%;
  min-height: 52px;
  padding: 12px 16px;
  margin-top: 8px;
  font: inherit;
  font-weight: 600;
  text-decoration: none;
  text-align: center;
  color: var(--text);
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 12px;
  cursor: pointer;
}
.btn:disabled { opacity: .4; cursor: not-allowed; }
.btn--primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
.btn--ghost { background: transparent; }
.btn--danger { background: transparent; border-color: var(--danger); color: var(--danger); }
.btn--small { width: auto; min-height: 40px; padding: 8px 14px; font-size: 14px; }

/* --- Flash --- */
.flashes { list-style: none; padding: 0; margin: 0 0 12px; }
.flash {
  padding: 10px 14px;
  border-radius: 10px;
  margin-bottom: 8px;
  font-size: 15px;
  border: 1px solid var(--border);
  background: var(--surface);
}
.flash--success { border-color: var(--keep); color: var(--keep); }
.flash--error { border-color: var(--toss); color: var(--toss); }
.flash--warning { border-color: var(--home); color: var(--home); }

/* --- Schermata di voto --- */
.progress { height: 6px; background: var(--surface-2); border-radius: 999px; overflow: hidden; }
.progress__bar { height: 100%; background: var(--accent); transition: width .25s ease; }
.progress__label { font-size: 14px; margin: 6px 0 12px; }

.photo { margin: 0; }
.photo img {
  display: block;
  width: 100%;
  /* Lascia sempre spazio ai tre bottoni: devono stare sopra la piega su iPhone. */
  max-height: 48vh;
  object-fit: contain;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
}
.photo figcaption { margin-top: 10px; font-size: 16px; }
.already { font-size: 14px; color: var(--muted); }

.choices { display: flex; flex-direction: column; gap: 4px; margin-top: 12px; }
.choices .btn { min-height: 56px; font-size: 17px; color: #fff; border: 0; }
.btn--keep { background: var(--keep); }
.btn--home { background: var(--home); }
.btn--toss { background: var(--toss); }
.choices .is-active { box-shadow: 0 0 0 3px var(--text) inset; }

.vote__nav { display: flex; justify-content: space-between; margin-top: 20px; font-size: 14px; }
.vote__nav a { color: var(--muted); }

/* --- Griglia riepilogo --- */
.grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(140px, 1fr));
  gap: 10px;
  list-style: none;
  padding: 0;
}
.tile a { position: relative; display: block; }
.tile img {
  width: 100%;
  aspect-ratio: 3 / 4;
  object-fit: cover;
  border-radius: 10px;
  border: 1px solid var(--border);
}
.tile__badge {
  position: absolute;
  left: 6px;
  bottom: 6px;
  padding: 3px 9px;
  border-radius: 999px;
  font-size: 12px;
  font-weight: 600;
  color: #fff;
}
.badge--keep { background: var(--keep); }
.badge--home { background: var(--home); }
.badge--toss { background: var(--toss); }
.badge--none { background: var(--muted); }

/* --- Risultati --- */
.results { list-style: none; padding: 0; counter-reset: rank; }
.result {
  position: relative;
  display: flex;
  gap: 12px;
  align-items: flex-start;
  padding: 12px;
  margin-bottom: 10px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
}
.result::before {
  counter-increment: rank;
  content: counter(rank);
  position: absolute;
  top: 6px;
  left: 6px;
  min-width: 22px;
  height: 22px;
  display: grid;
  place-items: center;
  padding: 0 5px;
  border-radius: 999px;
  background: var(--text);
  color: var(--surface);
  font-size: 12px;
  font-weight: 700;
}

.result__thumb {
  width: 76px;
  aspect-ratio: 3 / 4;
  object-fit: cover;
  border-radius: 8px;
  flex-shrink: 0;
}
.result__body { flex: 1; min-width: 0; }
.result__caption { margin: 0 0 8px; font-size: 15px; }
.result__counts { display: flex; flex-wrap: wrap; gap: 6px; margin: 8px 0 0; }
.bar { display: flex; height: 10px; border-radius: 999px; overflow: hidden; background: var(--surface-2); }
.bar__seg--keep { background: var(--keep); }
.bar__seg--home { background: var(--home); }
.bar__seg--toss { background: var(--toss); }
.tag { font-size: 12px; padding: 2px 8px; border-radius: 999px; background: var(--surface-2); }
.tag--keep { color: var(--keep); }
.tag--home { color: var(--home); }
.tag--toss { color: var(--toss); }

.table-scroll { overflow-x: auto; -webkit-overflow-scrolling: touch; }
.matrix { border-collapse: collapse; width: 100%; font-size: 14px; }
.matrix th, .matrix td {
  padding: 8px 10px;
  border-bottom: 1px solid var(--border);
  text-align: center;
  white-space: nowrap;
}
.matrix thead th { color: var(--muted); font-weight: 600; }
.matrix tbody th { text-align: left; font-weight: 600; }
.matrix .is-you { background: var(--surface-2); }
.dot {
  display: inline-block;
  width: 14px;
  height: 14px;
  border-radius: 50%;
  vertical-align: middle;
}
.dot--keep { background: var(--keep); }
.dot--home { background: var(--home); }
.dot--toss { background: var(--toss); }
.dot--none { background: transparent; border: 1px solid var(--border); }
.legend { display: flex; flex-wrap: wrap; align-items: center; gap: 6px; font-size: 13px; color: var(--muted); }
.legend .dot { margin-left: 8px; }
.legend .dot:first-child { margin-left: 0; }

/* --- Admin --- */
.jump {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin: 10px 0 4px;
}
.jump a {
  padding: 6px 12px;
  border-radius: 999px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  color: var(--text);
  font-size: 13px;
  font-weight: 600;
  text-decoration: none;
}

/* Le ancore non devono finire sotto la barra superiore appiccicata. */
[id] { scroll-margin-top: 70px; }

.share { display: flex; gap: 8px; align-items: center; margin-top: 6px; }
.share input { flex: 1; min-width: 0; font-size: 14px; }
.share .btn { width: auto; min-height: 44px; margin: 0; white-space: nowrap; }
.photo-row { display: flex; gap: 12px; align-items: flex-start; }
.photo-row__thumb {
  width: 84px;
  aspect-ratio: 3 / 4;
  object-fit: cover;
  border-radius: 8px;
  flex-shrink: 0;
}
.photo-row__body { flex: 1; min-width: 0; }
.danger-zone { border-color: var(--danger); }
.upload-count {
  margin: 4px 0 0;
  padding: 8px 12px;
  border-radius: 10px;
  background: var(--surface-2);
  font-size: 14px;
  font-weight: 600;
}
