:root {
  --bg: #EEF1F6;
  --surface: #FFFFFF;
  --surface-2: #F6F8FB;
  --ink: #1C2433;
  --muted: #5B6578;
  --line: #D5DBE5;
  --accent: #2F5BEA;
  --accent-ink: #FFFFFF;
  --author: #9A6B00;
  --author-bg: #FBF1D9;
  --community: #0F7A69;
  --community-bg: #DDF3EE;
  --danger: #B42318;
  --radius-lg: 14px;
  --radius-sm: 8px;
  --display: "Bricolage Grotesque", "Segoe UI", system-ui, sans-serif;
  --body: "Figtree", "Segoe UI", system-ui, -apple-system, sans-serif;
  color-scheme: light;
  box-sizing: border-box;
  padding-top: env(safe-area-inset-top, 0px);
  padding-bottom: env(safe-area-inset-bottom, 0px);
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #121821;
    --surface: #1B2330;
    --surface-2: #222C3B;
    --ink: #E6EAF2;
    --muted: #98A3B6;
    --line: #2E3949;
    --accent: #7C9BFF;
    --accent-ink: #0E1420;
    --author: #E9B949;
    --author-bg: #3A2F14;
    --community: #45CDB4;
    --community-bg: #123A33;
    --danger: #FF8A7E;
    color-scheme: dark;
  }
}

/* Tema scuro forzato (opzionale) */
:root[data-theme="dark"] {
    --bg: #121821;
    --surface: #1B2330;
    --surface-2: #222C3B;
    --ink: #E6EAF2;
    --muted: #98A3B6;
    --line: #2E3949;
    --accent: #7C9BFF;
    --accent-ink: #0E1420;
    --author: #E9B949;
    --author-bg: #3A2F14;
    --community: #45CDB4;
    --community-bg: #123A33;
    --danger: #FF8A7E;
    color-scheme: dark;
}

*, *::before, *::after { box-sizing: inherit; }
html { scroll-padding-top: env(safe-area-inset-top, 0px); }
body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font: 400 16px/1.55 var(--body);
  -webkit-font-smoothing: antialiased;
}
img { max-width: 100%; display: block; }
a { color: var(--accent); }
h1, h2, h3 { font-family: var(--display); line-height: 1.15; margin: 0 0 .4em; }
h1 { font-size: clamp(1.9rem, 4vw, 2.6rem); font-weight: 800; letter-spacing: -.02em; }
h2 { font-size: 1.35rem; font-weight: 700; }
p { margin: 0 0 1em; }
:focus-visible { outline: 3px solid var(--accent); outline-offset: 2px; }
[hidden] { display: none !important; }
button, a { -webkit-tap-highlight-color: transparent; }

/* ---------- Barra superiore ---------- */
.topbar {
  position: sticky; top: env(safe-area-inset-top, 0px); z-index: 10;
  background: var(--surface);
  border-bottom: 1px solid var(--line);
}
body::before {
  content: ""; position: fixed; inset: 0 0 auto; z-index: 10;
  height: env(safe-area-inset-top, 0px); background: var(--surface);
}
.topbar-inner {
  max-width: 920px; margin: 0 auto; padding: .7rem 1.2rem;
  display: flex; align-items: center; justify-content: space-between; gap: 1rem;
}
.brand { display: flex; align-items: center; gap: .55rem; text-decoration: none; color: var(--ink); }
.brand-logo { height: 34px; width: auto; display: block; }
.brand-name { font: 700 1.2rem/1 var(--display); }
.nav { display: flex; align-items: center; gap: .5rem; flex-wrap: wrap; justify-content: flex-end; }
.nav .who { color: var(--muted); font-size: .92rem; margin-right: .25rem; }

.container { max-width: 920px; margin: 0 auto; padding: 2rem 1.2rem 4rem; }
.container:focus { outline: none; }

/* ---------- Bottoni e form ---------- */
.btn {
  font: 600 .95rem/1 var(--body);
  border: 1px solid var(--line); background: var(--surface); color: var(--ink);
  padding: .65rem 1rem; border-radius: 999px; cursor: pointer;
  text-decoration: none; display: inline-flex; align-items: center; gap: .4rem;
}
.btn:hover { border-color: var(--muted); }
.btn.primary { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }
.btn.primary:hover { filter: brightness(1.08); }
.btn.danger { color: var(--danger); }
.btn.small { padding: .45rem .75rem; font-size: .85rem; }
.btn.ghost { border-color: transparent; background: transparent; }
.btn:disabled { opacity: .55; cursor: wait; }

.form { display: grid; gap: 1.1rem; max-width: 560px; }
.field { display: grid; gap: .35rem; }
.field label { font-weight: 600; font-size: .95rem; }
.field .hint { color: var(--muted); font-size: .85rem; }
input[type=text], input[type=password], input[type=url], input[type=number], input[type=search], textarea {
  font: inherit; color: var(--ink); background: var(--surface);
  border: 1px solid var(--line); border-radius: var(--radius-sm);
  padding: .65rem .8rem; width: 100%;
}
textarea { min-height: 6rem; resize: vertical; }
input:focus, textarea:focus { outline: 2px solid var(--accent); outline-offset: 0; border-color: var(--accent); }
.error { color: var(--danger); font-weight: 500; min-height: 1.2em; margin: 0; }
.actions { display: flex; gap: .6rem; flex-wrap: wrap; align-items: center; }
.panel {
  background: var(--surface); border: 1px solid var(--line);
  border-radius: var(--radius-lg); padding: 1.6rem;
}
.auth-panel { max-width: 440px; margin: 2rem auto; }

/* ---------- Home ---------- */
.hero {
  position: relative; margin-bottom: 1.8rem;
  border-radius: var(--radius-lg); overflow: hidden; background: #ECF1F7;
}
.hero-img { width: 100%; height: auto; }
.hero-text {
  position: absolute; left: 4.5%; top: 50%; transform: translateY(-50%);
  width: 42%; color: #1C2433;
}
.hero-text h1 { font-size: clamp(1.7rem, 3.4vw, 2.5rem); margin-bottom: .35em; }
.hero-text p { color: #3A4658; margin-bottom: 1.1em; max-width: 34ch; }
.empty-img { width: min(360px, 100%); height: auto; margin: 0 auto 1.4rem; }

.home-head { display: flex; justify-content: space-between; align-items: end; gap: 1rem; flex-wrap: wrap; margin-bottom: 1.4rem; }
.home-head p { color: var(--muted); margin: 0; max-width: 48ch; }
.search { margin-bottom: 1.4rem; max-width: 420px; }
.ranking-list { list-style: none; margin: 0; padding: 0; display: grid; gap: .75rem; }
.ranking-link {
  display: grid; grid-template-columns: 112px 1fr auto; gap: 1rem; align-items: center;
  padding: .7rem 1.2rem .7rem .7rem; background: var(--surface);
  border: 1px solid var(--line); border-radius: var(--radius-lg);
  text-decoration: none; color: var(--ink);
}
.ranking-link:hover { border-color: var(--accent); }
.rl-cover {
  width: 112px; height: 76px; border-radius: var(--radius-sm); object-fit: cover;
}
.ph {
  display: grid; place-items: center;
  background: hsl(var(--h) 55% 88%); color: hsl(var(--h) 45% 32%);
  font: 800 1.8rem/1 var(--display);
}
.rl-text { display: grid; gap: .3rem; min-width: 0; }
.rl-title { font: 700 1.2rem/1.2 var(--display); }
.ranking-link .meta { color: var(--muted); font-size: .9rem; display: flex; align-items: center; gap: .4rem; }
.ranking-link .count { font: 800 1.6rem/1 var(--display); text-align: right; }
.ranking-link .count small { display: block; font: 500 .75rem/1.2 var(--body); color: var(--muted); }

/* ---------- Avatar ---------- */
.avatar {
  border-radius: 50%; object-fit: cover; flex: none;
  display: inline-grid; place-items: center; vertical-align: middle;
  background: hsl(var(--h, 220) 55% 86%); color: hsl(var(--h, 220) 45% 30%);
  font-family: var(--display); font-weight: 800;
}
.avatar.xs { width: 22px; height: 22px; font-size: .7rem; }
.avatar.sm { width: 32px; height: 32px; font-size: .9rem; }
.avatar.xl { width: 96px; height: 96px; font-size: 2.4rem; }
.me-link { display: inline-flex; align-items: center; gap: .45rem; text-decoration: none; color: var(--ink); font-weight: 600; font-size: .92rem; padding: .2rem .2rem .2rem .4rem; }
.me-link:hover .me-name { text-decoration: underline; }
.profile-head { display: flex; gap: 1.4rem; align-items: center; margin-bottom: 1.8rem; flex-wrap: wrap; }
.profile-head h1 { margin-bottom: .1rem; }
.profile-head .meta { color: var(--muted); margin-bottom: .6rem; }

/* ---------- Selettore immagine ---------- */
.label { font-weight: 600; font-size: .95rem; }
.picker { display: flex; gap: 1rem; align-items: center; flex-wrap: wrap; }
.picker-preview {
  background: var(--surface-2); border: 1px dashed var(--line); border-radius: var(--radius-sm);
  display: grid; place-items: center; overflow: hidden; color: var(--muted); font-size: .85rem; flex: none; text-align: center; padding: .3rem;
}
.picker-cover .picker-preview { width: 200px; aspect-ratio: 16 / 10; }
.picker-avatar .picker-preview { width: 112px; height: 112px; border-radius: 50%; }
.picker-preview:has(img) { padding: 0; }
.picker-preview img { width: 100%; height: 100%; object-fit: cover; }
.picker-side { display: grid; gap: .5rem; }
.hint.error { color: var(--danger); font-weight: 500; }
.rank-cover { width: 100%; max-height: 300px; object-fit: cover; border-radius: var(--radius-lg); margin-bottom: 1.2rem; }
.rank-head .meta { display: flex; align-items: center; gap: .4rem; flex-wrap: wrap; }

.empty { text-align: center; padding: 3rem 1rem; color: var(--muted); }
.empty h2 { color: var(--ink); }

/* ---------- Pagina classifica ---------- */
.rank-head { margin-bottom: 1.5rem; }
.rank-head .meta { color: var(--muted); font-size: .95rem; }
.rank-head .actions { margin-top: .6rem; }
.rank-head .desc { max-width: 65ch; }
.toolbar { display: flex; justify-content: space-between; align-items: center; gap: 1rem; flex-wrap: wrap; margin-bottom: 1rem; }
.seg { display: inline-flex; border: 1px solid var(--line); border-radius: 999px; padding: 3px; background: var(--surface); }
.seg button {
  font: 600 .88rem/1 var(--body); border: 0; background: transparent; color: var(--muted);
  padding: .5rem .9rem; border-radius: 999px; cursor: pointer;
}
.seg button[aria-pressed=true] { background: var(--ink); color: var(--bg); }

.items { list-style: none; margin: 0; padding: 0; display: grid; gap: .8rem; }
.item {
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius-lg);
  overflow: hidden;
}
.item-row {
  display: grid; grid-template-columns: 4.2rem 64px 1fr auto; gap: 1rem; align-items: center;
  padding: .9rem 1.2rem .9rem .6rem; width: 100%;
  background: none; border: 0; color: inherit; text-align: left; cursor: pointer; font: inherit;
}
.pos {
  font: 800 2.6rem/1 var(--display); letter-spacing: -.04em; text-align: center;
  color: var(--muted);
}
.item:nth-child(-n+3) .pos { color: var(--ink); }
.medal { width: 2.4rem; height: auto; margin: 0 auto; }
.thumb {
  width: 64px; height: 64px; border-radius: var(--radius-sm); object-fit: cover;
  font-size: 1.5rem;
}
.item-name { display: block; font: 700 1.15rem/1.2 var(--display); margin-bottom: .15rem; }
.item-title .sub { color: var(--muted); font-size: .85rem; }
.scores { display: flex; gap: .5rem; }
.score {
  min-width: 4.2rem; text-align: center; border-radius: var(--radius-sm); padding: .4rem .5rem;
}
.score b { display: block; font: 800 1.35rem/1 var(--display); }
.score span { font-size: .72rem; font-weight: 600; }
.score.author { background: var(--author-bg); color: var(--author); }
.score.community { background: var(--community-bg); color: var(--community); }
.score.none b { opacity: .5; }

.item-body { border-top: 1px solid var(--line); padding: 1.2rem 1.4rem 1.4rem; display: grid; gap: 1.1rem; }
.item-body[hidden] { display: none; }
.item-body .cover { border-radius: var(--radius-sm); max-height: 320px; object-fit: cover; width: 100%; }
.crit-table-wrap { overflow-x: auto; }
.crit-table { width: 100%; border-collapse: collapse; font-size: .95rem; }
.crit-table th { text-align: left; font-weight: 600; color: var(--muted); font-size: .82rem; padding: 0 .5rem .5rem 0; }
.crit-table td { padding: .55rem .5rem .55rem 0; border-top: 1px solid var(--line); vertical-align: middle; }
.crit-table td.name { font-weight: 600; }
.bar { display: flex; align-items: center; gap: .5rem; }
.bar-track { flex: 1; height: 6px; border-radius: 3px; background: var(--surface-2); min-width: 60px; overflow: hidden; }
.bar-fill { height: 100%; border-radius: 3px; }
.bar.author .bar-fill { background: var(--author); }
.bar.community .bar-fill { background: var(--community); }
.bar b { font-variant-numeric: tabular-nums; min-width: 2.2rem; text-align: right; }
.crit-table .votes { color: var(--muted); font-size: .8rem; }
/* Stepper per il voto: − valore + */
.stepper {
  display: inline-flex; align-items: stretch;
  border: 1px solid var(--line); border-radius: 999px; overflow: hidden; background: var(--surface);
}
.stepper button {
  border: 0; background: var(--surface-2); color: var(--ink);
  width: 2.4rem; font: 700 1.15rem/1 var(--body); cursor: pointer;
}
.stepper button:active { background: var(--line); }
.stepper .vote-input {
  width: 3.4rem; border: 0; border-radius: 0; text-align: center;
  padding: .45rem 0; font-weight: 700; font-variant-numeric: tabular-nums;
}
.stepper .vote-input:focus { outline: none; box-shadow: inset 0 0 0 2px var(--accent); }
.note { color: var(--muted); font-size: .9rem; margin: 0; }

.show-more { margin-top: 1rem; display: flex; justify-content: center; }

/* ---------- Editor scheda ---------- */
.crit-editor { display: grid; gap: .6rem; }
.crit-row { display: grid; grid-template-columns: 1fr 9rem 5rem auto; gap: .6rem; align-items: center; }
.crit-row input[type=range] { width: 100%; accent-color: var(--author); }
.crit-row .remove { padding: .45rem .6rem; }
fieldset { border: 1px solid var(--line); border-radius: var(--radius-sm); padding: 1rem; margin: 0; }
legend { font-weight: 600; padding: 0 .3rem; }

/* ---------- Barra in basso (solo telefono) ---------- */
.tabbar { display: none; }
.count-inline { display: none; }

/* ---------- Toast ---------- */
.toast {
  position: fixed; left: 50%; bottom: calc(1.5rem + env(safe-area-inset-bottom, 0px));
  transform: translate(-50%, 1rem); opacity: 0; pointer-events: none;
  background: var(--ink); color: var(--bg); padding: .7rem 1.1rem; border-radius: 999px;
  font-weight: 600; transition: opacity .2s, transform .2s; z-index: 20;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) .ph, :root:not([data-theme="light"]) span.avatar { background: hsl(var(--h, 220) 30% 24%); color: hsl(var(--h, 220) 60% 78%); }
}
:root[data-theme="dark"] .ph, :root[data-theme="dark"] span.avatar { background: hsl(var(--h, 220) 30% 24%); color: hsl(var(--h, 220) 60% 78%); }

/* ---------- Mobile ---------- */
@media (max-width: 820px) {
  .hero { background: var(--surface); border: 1px solid var(--line); }
  .hero-img { aspect-ratio: 16 / 9; object-fit: cover; object-position: right; }
  .hero-text { position: static; transform: none; width: auto; padding: 1.2rem 1.2rem 1.4rem; color: var(--ink); }
  .hero-text p { color: var(--muted); }
}
@media (max-width: 640px) {
  body { font-size: 15.5px; }
  input, textarea, .stepper .vote-input { font-size: 16px; } /* evita lo zoom automatico su iPhone */
  .container { padding: 1.2rem 1rem calc(5.5rem + env(safe-area-inset-bottom, 0px)); }
  .topbar-inner { padding: .55rem 1rem; }
  .hide-mobile { display: none !important; }
  .brand-name { display: inline; font-size: 1.1rem; }
  .me-link .me-name { display: none; }
  .me-link .avatar.sm { width: 36px; height: 36px; }
  .panel { padding: 1.2rem; border-radius: var(--radius-lg); }
  .btn { padding: .75rem 1.1rem; }
  .btn.small { padding: .6rem .9rem; font-size: .9rem; }

  /* Barra di navigazione in basso */
  .tabbar {
    display: flex; position: fixed; left: 0; right: 0; bottom: 0; z-index: 15;
    background: var(--surface); border-top: 1px solid var(--line);
    padding-bottom: env(safe-area-inset-bottom, 0px);
  }
  .tabbar a {
    flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 3px;
    min-height: 58px; padding: .35rem 0; text-decoration: none;
    color: var(--muted); font-size: .72rem; font-weight: 600;
  }
  .tabbar .tab-icon { display: grid; place-items: center; width: 28px; height: 28px; }
  .tabbar svg { width: 23px; height: 23px; }
  .tabbar a[aria-current=page] { color: var(--accent); }
  .tabbar a.tab-main .tab-icon { background: var(--accent); color: var(--accent-ink); border-radius: 50%; width: 34px; height: 34px; margin-top: -4px; }
  .tabbar a.tab-main svg { width: 20px; height: 20px; }
  .toast { bottom: calc(72px + env(safe-area-inset-bottom, 0px)); max-width: calc(100% - 2rem); text-align: center; }

  /* Home */
  .hero-text h1 { font-size: 1.6rem; }
  .search { max-width: none; }
  .ranking-link { grid-template-columns: 76px 1fr; gap: .8rem; padding: .6rem .8rem .6rem .6rem; }
  .rl-cover { width: 76px; height: 60px; font-size: 1.4rem; }
  .rl-title { font-size: 1.05rem; }
  .ranking-link .count { display: none; }
  .count-inline { display: inline; }
  .ranking-link .meta { font-size: .82rem; align-items: flex-start; }
  .ranking-link .meta .avatar { margin-top: 1px; }

  /* Pagina classifica */
  .rank-cover { max-height: 200px; margin-bottom: .9rem; }
  .rank-head h1 { font-size: 1.7rem; }
  .toolbar { gap: .6rem; }
  .seg { width: 100%; }
  .seg button { flex: 1; padding: .65rem .5rem; }
  .item-row { grid-template-columns: 2.4rem 52px 1fr; gap: .2rem .7rem; padding: .7rem .8rem .7rem .4rem; }
  .pos { font-size: 1.7rem; }
  .medal { width: 1.8rem; }
  .thumb { width: 52px; height: 52px; font-size: 1.2rem; }
  .item-name { font-size: 1.02rem; }
  .scores { grid-column: 3; gap: .35rem; margin-top: .35rem; }
  .score { min-width: 0; padding: .25rem .5rem; display: flex; align-items: baseline; gap: .3rem; }
  .score b { display: inline; font-size: 1.05rem; }
  .score span { font-size: .68rem; }
  .item-body { padding: 1rem; }
  .item-body .cover { max-height: 220px; }
  .show-more .btn { width: 100%; justify-content: center; }

  /* Tabella voti trasformata in schede */
  .crit-table thead { display: none; }
  .crit-table, .crit-table tbody { display: block; }
  .crit-table tr {
    display: grid; grid-template-columns: 1fr 1fr; gap: .5rem .9rem;
    padding: .8rem 0; border-top: 1px solid var(--line);
  }
  .crit-table td { display: block; border: 0; padding: 0; }
  .crit-table td.name { grid-column: 1 / -1; font-size: 1rem; }
  .crit-table td[data-label]::before {
    content: attr(data-label); display: block; color: var(--muted); font-size: .74rem; font-weight: 600; margin-bottom: .2rem;
  }
  .crit-table td.vote { grid-column: 1 / -1; display: flex; align-items: center; justify-content: space-between; }
  .crit-table td.vote::before { margin: 0; font-size: .85rem; color: var(--ink); }
  .stepper button { width: 2.9rem; font-size: 1.3rem; }
  .stepper .vote-input { width: 3.6rem; padding: .6rem 0; }
  .vote-form .actions .btn { width: 100%; justify-content: center; }

  /* Form */
  .crit-row { grid-template-columns: 1fr 4.5rem auto; }
  .crit-row input[type=range] { grid-column: 1 / -1; grid-row: 2; height: 28px; }
  .picker { flex-wrap: nowrap; align-items: flex-start; }
  .picker-cover .picker-preview { width: 120px; }
  .picker-avatar .picker-preview { width: 88px; height: 88px; }
  .picker .actions { flex-direction: column; align-items: stretch; gap: .4rem; }
  .form > .actions .btn.primary { flex: 1 1 100%; justify-content: center; }

  /* Profilo autore */
  .profile-head { gap: 1rem; }
  .avatar.xl { width: 72px; height: 72px; font-size: 1.8rem; }
}
@media (prefers-reduced-motion: reduce) {
  * { transition: none !important; animation: none !important; }
}
