:root {
  --bg: #130c15;
  --bg2: #1a111d;
  --surface: #1f1523;
  --surface2: #2b1d30;
  --line: #3a2a40;
  --text: #f6edf4;
  --muted: #ab95a7;
  --dim: #7b6680;
  --accent: #ec4a82;
  --accent2: #ff8f5a;
  --grad: linear-gradient(135deg, #ec4a82, #ff8f5a);
  --ok: #3fcf8e;
  --warn: #f6b94a;
  --danger: #ef5a5a;
  --l1: #4cc9a6;
  --l2: #f6b94a;
  --l3: #ec4a82;
  --l4: #a874ff;
  --r: 16px;
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-t: env(safe-area-inset-top, 0px);
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; background: var(--bg); color: var(--text); }
body {
  font: 15px/1.45 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  -webkit-tap-highlight-color: transparent;
  -webkit-text-size-adjust: 100%;
  overscroll-behavior: none;
}
a { color: var(--accent2); }
h1, h2, h3 { margin: 0; }
[hidden] { display: none !important; }
svg { display: block; }

/* ---------- Struttura ---------- */
#app { position: fixed; inset: 0; height: var(--app-h, 100dvh); display: flex; flex-direction: column; }
.page { flex: 1; display: flex; flex-direction: column; min-height: 0; min-width: 0; }
header.top {
  display: flex; align-items: center; gap: 6px;
  padding: calc(8px + var(--safe-t)) 10px 8px 16px; min-height: calc(56px + var(--safe-t));
  background: var(--bg); border-bottom: 1px solid var(--line);
}
header.top h1 { flex: 1; font-size: 19px; font-weight: 800; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; letter-spacing: -.01em; }
header.top .back { margin-left: -8px; }
#tools { display: flex; align-items: center; gap: 4px; }
main { flex: 1; overflow-y: auto; min-height: 0; -webkit-overflow-scrolling: touch; }
main.chat-main { overflow: hidden; display: flex; }
.wrap { max-width: 760px; margin: 0 auto; padding: 14px 14px 36px; }

nav.nav { display: flex; background: var(--bg2); border-top: 1px solid var(--line); padding-bottom: var(--safe-b); }
nav.nav a {
  flex: 1; display: flex; flex-direction: column; align-items: center; gap: 3px;
  padding: 8px 0 7px; color: var(--dim); text-decoration: none; font-size: 11px; font-weight: 600; position: relative;
}
nav.nav a svg { width: 24px; height: 24px; }
nav.nav a.on { color: var(--accent); }
.badge {
  position: absolute; top: 3px; left: calc(50% + 5px); font-style: normal;
  background: var(--accent); color: #fff; font-size: 10px; font-weight: 800;
  min-width: 18px; height: 18px; border-radius: 9px; padding: 0 5px;
  display: flex; align-items: center; justify-content: center; border: 2px solid var(--bg2);
}

@media (max-width: 899px) {
  body.in-conv nav.nav { display: none; }
}
@media (min-width: 900px) {
  #app { flex-direction: row-reverse; }
  nav.nav { flex-direction: column; width: 86px; border-top: 0; border-right: 1px solid var(--line); padding: 14px 0; gap: 4px; }
  nav.nav a { flex: none; padding: 12px 0; }
}

/* ---------- Elementi base ---------- */
.btn {
  appearance: none; border: 1px solid var(--line); background: var(--surface2); color: var(--text);
  padding: 10px 16px; border-radius: 12px; font: inherit; font-weight: 700; cursor: pointer;
  display: inline-flex; align-items: center; justify-content: center; gap: 6px; text-decoration: none; line-height: 1.2;
}
.btn svg { width: 18px; height: 18px; }
.btn.primary { background: var(--grad); border-color: transparent; color: #fff; }
.btn.ok { background: var(--ok); border-color: transparent; color: #062317; }
.btn.danger { background: transparent; border-color: #5d2b35; color: var(--danger); }
.btn.ghost { background: transparent; }
.btn.small { padding: 7px 12px; font-size: 13px; border-radius: 10px; }
.btn.block { width: 100%; }
.btn:disabled { opacity: .4; cursor: not-allowed; }
.icon-btn {
  background: none; border: 0; color: var(--muted); width: 42px; height: 42px; border-radius: 50%;
  display: inline-flex; align-items: center; justify-content: center; cursor: pointer; flex: none; padding: 0; font: inherit;
}
.icon-btn svg { width: 24px; height: 24px; }
.icon-btn:active { background: var(--surface2); }
.icon-btn.sm { width: 32px; height: 32px; font-size: 16px; }
.icon-btn.sm svg { width: 18px; height: 18px; }

input, textarea, select {
  width: 100%; background: var(--bg2); border: 1px solid var(--line); color: var(--text);
  border-radius: 12px; padding: 10px 12px; font: inherit; font-size: 16px; outline: none;
}
input:focus, textarea:focus, select:focus { border-color: var(--accent); }
textarea { resize: vertical; min-height: 44px; line-height: 1.4; }
label { display: block; margin-bottom: 12px; font-size: 13px; font-weight: 600; color: var(--muted); }
label input, label textarea, label select { margin-top: 6px; }

.card { background: var(--surface); border: 1px solid var(--line); border-radius: var(--r); padding: 16px; margin-bottom: 12px; }
.card > :last-child { margin-bottom: 0; }
.card p { margin: 0 0 10px; }
a.linkcard { display: block; color: var(--text); text-decoration: none; font-weight: 600; }
h2.sec { font-size: 12px; text-transform: uppercase; letter-spacing: .1em; color: var(--muted); margin: 24px 4px 10px; font-weight: 800; }
h2.sec:first-child { margin-top: 6px; }
.muted { color: var(--muted); }
.dim { color: var(--dim); }
.small { font-size: 13px; }
.ok { color: var(--ok); }
.pos { color: var(--ok); }
.neg { color: var(--danger); }
.empty { text-align: center; color: var(--muted); padding: 22px 12px; }
.loading { text-align: center; color: var(--dim); padding: 30px; }
.alert { background: rgba(239, 90, 90, .12); border: 1px solid rgba(239, 90, 90, .4); color: #ffb3b3; padding: 10px 12px; border-radius: 12px; margin-bottom: 12px; }
.actions { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }
.clamp { display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }

.pill { display: inline-flex; align-items: center; gap: 4px; padding: 4px 10px; border-radius: 999px; font-size: 12px; font-weight: 800; background: var(--surface2); color: var(--muted); }
.pill.l1 { background: rgba(76, 201, 166, .14); color: var(--l1); }
.pill.l2 { background: rgba(246, 185, 74, .14); color: var(--l2); }
.pill.l3 { background: rgba(236, 74, 130, .16); color: var(--l3); }
.pill.l4 { background: rgba(168, 116, 255, .16); color: var(--l4); }
.chip { display: inline-block; font-size: 12px; font-weight: 700; padding: 4px 10px; border-radius: 999px; background: var(--surface2); color: var(--muted); white-space: nowrap; }
.chip.ok { color: var(--ok); background: rgba(63, 207, 142, .12); }
.chip.ko { color: var(--danger); background: rgba(239, 90, 90, .12); }
.chip.wait { color: var(--warn); background: rgba(246, 185, 74, .12); white-space: normal; }

.tabs { display: flex; gap: 6px; overflow-x: auto; padding: 12px 14px 2px; max-width: 760px; margin: 0 auto; scrollbar-width: none; }
.tabs::-webkit-scrollbar { display: none; }
.tabs a {
  white-space: nowrap; padding: 8px 14px; border-radius: 999px; background: var(--surface); color: var(--muted);
  text-decoration: none; font-weight: 700; font-size: 14px; border: 1px solid var(--line);
}
.tabs a.on { background: var(--text); color: var(--bg); border-color: var(--text); }
.seg { display: flex; gap: 6px; margin-bottom: 12px; flex-wrap: wrap; }
.seg button {
  border: 1px solid var(--line); background: var(--surface); color: var(--muted); border-radius: 999px;
  padding: 7px 13px; font: inherit; font-size: 14px; font-weight: 700; cursor: pointer;
}
.seg button.on { background: var(--text); color: var(--bg); border-color: var(--text); }
.seg small { opacity: .7; margin-left: 3px; }

/* ---------- Toast, modali, visore ---------- */
.toast {
  position: fixed; left: 50%; transform: translateX(-50%); bottom: calc(86px + var(--safe-b)); z-index: 90;
  background: var(--text); color: var(--bg); padding: 11px 16px; border-radius: 14px; font-weight: 700;
  max-width: min(92vw, 480px); box-shadow: 0 8px 30px #0008; text-align: center;
}
.toast.err { background: var(--danger); color: #fff; }
.modal-bg { position: fixed; inset: 0; z-index: 80; background: rgba(5, 2, 6, .7); display: flex; align-items: flex-end; justify-content: center; backdrop-filter: blur(3px); }
.modal {
  background: var(--surface); width: 100%; max-width: 560px; border-radius: 22px 22px 0 0;
  padding: 14px 18px calc(18px + var(--safe-b)); max-height: 92vh; overflow-y: auto; border: 1px solid var(--line); border-bottom: 0;
}
.modal-head { display: flex; align-items: center; justify-content: space-between; gap: 10px; margin-bottom: 10px; }
.modal-head h3 { font-size: 18px; }
.modal-body p { margin: 0 0 12px; }
.modal-btns { display: flex; gap: 8px; margin-top: 14px; }
.modal-btns .btn { flex: 1; }
@media (min-width: 600px) {
  .modal-bg { align-items: center; }
  .modal { border-radius: 22px; border-bottom: 1px solid var(--line); padding-bottom: 18px; }
}
.modal .grid { max-height: 52vh; overflow-y: auto; }
.sheet-foot { position: sticky; bottom: 0; padding-top: 12px; background: var(--surface); }
.mprev { display: block; max-width: 100%; max-height: 50vh; margin: 0 auto 12px; border-radius: 12px; }
.viewer { position: fixed; inset: 0; z-index: 95; background: #000; display: flex; align-items: center; justify-content: center; }
.viewer img { max-width: 100%; max-height: 100%; object-fit: contain; }
.viewer .icon-btn { position: absolute; top: calc(10px + var(--safe-t)); right: 10px; color: #fff; background: #0008; }

/* ---------- Chat ---------- */
.chat-split { flex: 1; display: flex; min-height: 0; min-width: 0; }
.sec-list { width: 100%; overflow-y: auto; }
@media (max-width: 899px) {
  .chat-split.has-conv .sec-list { display: none; }
}
@media (min-width: 900px) {
  .sec-list { width: 320px; flex: none; border-right: 1px solid var(--line); }
}
.sec-item { display: flex; gap: 12px; align-items: center; padding: 11px 16px; text-decoration: none; color: var(--text); border-bottom: 1px solid rgba(58, 42, 64, .5); }
.sec-item.on { background: var(--surface); }
.sec-item .av {
  width: 48px; height: 48px; border-radius: 50%; background: var(--grad); color: #fff; flex: none;
  display: flex; align-items: center; justify-content: center; font-weight: 800; font-size: 19px;
}
.sec-item .mid { flex: 1; min-width: 0; }
.sec-item .name { font-weight: 700; }
.sec-item .prev { color: var(--muted); font-size: 14px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.sec-item .right { display: flex; flex-direction: column; align-items: flex-end; gap: 5px; font-size: 12px; color: var(--dim); flex: none; }
.unread { background: var(--accent); color: #fff; border-radius: 999px; font-size: 12px; font-weight: 800; min-width: 22px; height: 22px; display: flex; align-items: center; justify-content: center; padding: 0 7px; }
.sec-add { display: flex; align-items: center; gap: 8px; margin: 14px auto; background: none; border: 1px dashed var(--line); color: var(--muted); padding: 10px 16px; border-radius: 12px; font: inherit; font-weight: 700; cursor: pointer; }
.sec-add svg { width: 18px; height: 18px; }

.conv { flex: 1; display: flex; flex-direction: column; min-width: 0; min-height: 0; background: radial-gradient(ellipse at top, #1d1220 0%, var(--bg) 70%); }
.msgs { flex: 1; overflow-y: auto; padding: 10px 10px 14px; display: flex; flex-direction: column; gap: 3px; -webkit-overflow-scrolling: touch; }
.msgs > :first-child { margin-top: auto; }
.older { text-align: center; color: var(--dim); font-size: 13px; padding: 10px; cursor: pointer; }
.daysep { text-align: center; margin: 10px 0 6px; }
.daysep span { background: var(--surface2); color: var(--muted); font-size: 12px; font-weight: 700; padding: 4px 11px; border-radius: 999px; text-transform: capitalize; }
.msg { display: flex; }
.msg.mine { justify-content: flex-end; }
.bubble {
  max-width: min(80%, 520px); padding: 7px 11px 5px; border-radius: 18px; background: var(--surface2);
  overflow-wrap: anywhere; position: relative;
}
.msg.mine .bubble { background: linear-gradient(135deg, #b42f5c, #c2553a); color: #fff; border-bottom-right-radius: 6px; }
.msg.theirs .bubble { border-bottom-left-radius: 6px; }
.bubble .text { white-space: pre-wrap; }
.bubble .text a { color: inherit; text-decoration: underline; }
.bubble.media { padding: 4px; }
.bubble.media .text { padding: 4px 7px 0; }
.bubble.media .meta { padding: 0 7px 2px; }
.bubble img, .bubble video { display: block; max-width: 100%; max-height: 62vh; min-width: 140px; min-height: 80px; border-radius: 14px; background: #0004; cursor: zoom-in; object-fit: contain; }
.bubble video { cursor: default; width: 100%; }
.bubble audio { display: block; width: 250px; max-width: 100%; height: 42px; margin: 2px 0; }
.bubble.k-voice { padding: 6px 8px 4px; }
.meta { display: flex; justify-content: flex-end; align-items: center; gap: 4px; font-size: 11px; opacity: .7; margin-top: 2px; }
.mmenu { background: none; border: 0; color: inherit; padding: 0 2px; cursor: pointer; font: inherit; font-size: 15px; line-height: 1; opacity: .9; }

.upbar-wrap { position: relative; height: 22px; background: var(--bg2); border-top: 1px solid var(--line); font-size: 12px; color: var(--muted); }
.upbar-wrap .upbar { position: absolute; left: 0; top: 0; bottom: 0; width: 0; background: rgba(236, 74, 130, .25); transition: width .2s; }
.upbar-wrap span { position: relative; padding-left: 12px; line-height: 22px; font-weight: 700; }
.composer { display: flex; align-items: flex-end; gap: 4px; padding: 7px 6px calc(7px + var(--safe-b)); background: var(--bg2); border-top: 1px solid var(--line); }
.composer textarea { flex: 1; resize: none; max-height: 40vh; min-height: 42px; border-radius: 21px; padding: 10px 15px; margin: 0; }
.composer .send { background: var(--grad); color: #fff; }
.composer .send svg { width: 21px; height: 21px; }
.recbar { flex: 1; display: flex; align-items: center; gap: 10px; height: 42px; padding: 0 8px; font-weight: 800; color: var(--danger); }
.recbar .dot { width: 11px; height: 11px; border-radius: 50%; background: var(--danger); animation: pulse 1s infinite; }
@keyframes pulse { 50% { opacity: .25; } }

/* ---------- Griglia meme ---------- */
.grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(96px, 1fr)); gap: 6px; }
.grid.big { grid-template-columns: repeat(auto-fill, minmax(110px, 1fr)); }
.grid > .empty, .grid > .loading { grid-column: 1 / -1; }
.tile { position: relative; aspect-ratio: 1; border-radius: 12px; overflow: hidden; background: var(--surface2); cursor: pointer; border: 3px solid transparent; }
.tile img, .tile video { width: 100%; height: 100%; object-fit: cover; display: block; pointer-events: none; }
.tile .vid { position: absolute; right: 6px; bottom: 4px; font-style: normal; color: #fff; text-shadow: 0 1px 4px #000; font-size: 13px; }
.tile.used img, .tile.used video { opacity: .4; }
.tile.used::after {
  content: '✓ usato'; position: absolute; left: 6px; top: 6px; background: var(--ok); color: #062317;
  font-size: 11px; font-weight: 800; padding: 2px 7px; border-radius: 999px;
}
.tile.sel { border-color: var(--accent); }

/* ---------- Challenge ---------- */
.scorebar { display: flex; align-items: center; gap: 18px; background: var(--surface); border: 1px solid var(--line); border-radius: var(--r); padding: 12px 16px; }
.scorebar > div { display: flex; flex-direction: column; }
.scorebar b { font-size: 22px; line-height: 1.1; }
.scorebar .btn { margin-left: auto; }
.turn-head { display: flex; justify-content: space-between; align-items: center; gap: 10px; margin-bottom: 10px; }
.turn-head .day { font-weight: 800; text-transform: capitalize; font-size: 16px; }
.cd { font-size: 12px; font-weight: 800; color: var(--warn); white-space: nowrap; }
.levels { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; margin-top: 4px; }
.lvl {
  border-radius: 16px; padding: 14px 4px 12px; text-align: center; border: 1px solid; cursor: pointer;
  color: var(--text); font: inherit; font-weight: 700; background: none;
}
.lvl b { display: block; font-size: 24px; line-height: 1.1; }
.lvl span { display: block; font-size: 11px; color: var(--muted); font-weight: 600; margin-top: 3px; }
.lvl.l1 { border-color: rgba(76, 201, 166, .45); background: rgba(76, 201, 166, .08); }
.lvl.l1 b { color: var(--l1); }
.lvl.l2 { border-color: rgba(246, 185, 74, .45); background: rgba(246, 185, 74, .08); }
.lvl.l2 b { color: var(--l2); }
.lvl.l3 { border-color: rgba(236, 74, 130, .5); background: rgba(236, 74, 130, .1); }
.lvl.l3 b { color: var(--l3); }
.lvl:disabled { opacity: .35; cursor: not-allowed; }
.ctext {
  font-size: 17px; line-height: 1.5; white-space: pre-wrap; overflow-wrap: anywhere; margin: 10px 0 14px; padding: 14px 16px;
  border-radius: 14px; background: var(--bg2); border-left: 4px solid var(--accent);
}
.ctext.l1 { border-left-color: var(--l1); }
.ctext.l2 { border-left-color: var(--l2); }
.ctext.l3 { border-left-color: var(--l3); }
.ctext.l4 { border-left-color: var(--l4); }
.card.l4 { border-color: rgba(168, 116, 255, .35); }
.info { font-size: 14px; color: var(--muted); }
.deck-head { display: flex; justify-content: space-between; align-items: center; gap: 10px; margin-bottom: 10px; }
.addform { display: flex; gap: 8px; align-items: flex-end; margin-bottom: 8px; }
.addform textarea { flex: 1; }
.crow { display: flex; gap: 8px; align-items: flex-start; padding: 10px 0; border-top: 1px solid rgba(58, 42, 64, .6); }
.crow .ctxt { flex: 1; white-space: pre-wrap; overflow-wrap: anywhere; }
.crow .cbtn { display: flex; flex: none; }
.hrow { display: flex; justify-content: space-between; align-items: center; gap: 12px; padding: 10px 0; border-top: 1px solid rgba(58, 42, 64, .6); }
.hrow:first-child { border-top: 0; padding-top: 0; }
.hrow:last-child { padding-bottom: 0; }
.ptext { white-space: pre-wrap; font-size: 16px; margin: 4px 0 10px; }

/* ---------- Favori ---------- */
.hero { background: var(--grad); color: #fff; border-radius: 20px; padding: 18px 20px; margin-bottom: 4px; }
.hero .big { font-size: 46px; font-weight: 900; line-height: 1.05; letter-spacing: -.02em; }
.hero .big small { font-size: 20px; margin-left: 2px; }
.hero .small { opacity: .92; }
.slot { display: flex; gap: 12px; align-items: center; padding: 10px 0; border-top: 1px solid rgba(58, 42, 64, .6); }
.card > .slot:first-child, .card > form.slot:first-child { border-top: 0; padding-top: 0; }
.slot .n { width: 28px; height: 28px; border-radius: 50%; background: var(--surface2); display: flex; align-items: center; justify-content: center; font-weight: 800; font-size: 13px; flex: none; }
.slot .st { flex: 1; min-width: 0; overflow-wrap: anywhere; }
.slot textarea { flex: 1; min-height: 40px; }
.reqs { margin-top: 12px; }
.req { display: flex; justify-content: space-between; gap: 10px; align-items: center; padding: 8px 0; border-top: 1px solid rgba(58, 42, 64, .6); }
form.card textarea { margin-bottom: 10px; }

/* ---------- Regole ---------- */
.rules h3 { font-size: 16px; margin: 4px 0 8px; }
.rules ul { margin: 0; padding-left: 20px; }
.rules li { margin-bottom: 7px; }

/* ---------- Login e installazione ---------- */
.login-wrap { min-height: 100dvh; display: flex; align-items: center; justify-content: center; padding: 20px; }
.login { width: 100%; max-width: 360px; text-align: center; }
.login-logo { width: 84px; height: 84px; border-radius: 22px; margin: 6px auto 10px; display: block; }
.login h1 { font-size: 26px; margin-bottom: 18px; }
.login label { text-align: left; }
body.install { overflow: auto; }
.install-wrap { max-width: 620px; margin: 0 auto; padding: 24px 16px 60px; }
.install-wrap h1 { margin-bottom: 18px; }
.install-wrap h2 { font-size: 17px; margin-bottom: 12px; }
.checks { list-style: none; padding: 0; margin: 0 0 8px; }
.checks li { padding: 5px 0; }
.checks .ok { color: var(--ok); }
.checks .ko { color: var(--danger); }
.checks .warn { color: var(--warn); }
fieldset { border: 1px solid var(--line); border-radius: 14px; margin: 0 0 14px; padding: 12px 14px 2px; }
legend { font-weight: 800; padding: 0 6px; }
