/* BotBar Mini App — mobile first.
   I colori seguono il tema Telegram dell'utente, con fallback per il browser. */
:root {
  --bg:        var(--tg-theme-bg-color, #17212b);
  --surface:   var(--tg-theme-secondary-bg-color, #232e3c);
  --text:      var(--tg-theme-text-color, #ffffff);
  --muted:     var(--tg-theme-hint-color, #8b9aa8);
  --accent:    var(--tg-theme-button-color, #3390ec);
  --on-accent: var(--tg-theme-button-text-color, #ffffff);
  --line: rgba(128,150,170,.20);
  --ok: #4bb34b; --warn: #e5a33d; --bad: #e8524f;
  --radius: 14px;
  --pad: 16px;
  --safe-b: env(safe-area-inset-bottom, 0px);
}
@media (prefers-color-scheme: light) {
  :root:not([data-tg]) { --bg:#f4f4f7; --surface:#ffffff; --text:#0f1419; --muted:#707b87; --line:rgba(0,0,0,.10); }
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin:0; padding:0; }
body {
  background: var(--bg); color: var(--text);
  font: 16px/1.45 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, system-ui, sans-serif;
  overscroll-behavior-y: contain;
}
.hidden { display: none !important; }
button { font: inherit; color: inherit; border: 0; background: none; cursor: pointer; }
h1 { font-size: 22px; font-weight: 700; margin: 0; letter-spacing: -.02em; }
h2 { font-size: 13px; font-weight: 600; margin: 0; }
h3 { font-size: 17px; font-weight: 700; margin: 0; }

/* ---------- boot ---------- */
.boot { position: fixed; inset: 0; display: grid; place-items: center; background: var(--bg); z-index: 100; }
.spinner { width: 30px; height: 30px; border: 3px solid var(--line); border-top-color: var(--accent);
           border-radius: 50%; animation: spin .7s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

/* ---------- layout ---------- */
.screen { padding: 8px var(--pad) 0; padding-bottom: calc(76px + var(--safe-b)); max-width: 640px; margin: 0 auto; }
.topbar { display: flex; align-items: flex-start; justify-content: space-between; gap: 12px; padding: 12px 0 16px; }
.sub { margin: 2px 0 0; color: var(--muted); font-size: 13px; }
.section-title { color: var(--muted); text-transform: uppercase; letter-spacing: .06em; margin: 22px 0 10px; }
.note { color: var(--muted); font-size: 12.5px; line-height: 1.5; margin: 14px 2px 0; }
.err { color: var(--bad); font-size: 14px; margin: 10px 0 0; }

.card { background: var(--surface); border-radius: var(--radius); padding: 14px; }
.card + .card { margin-top: 10px; }
.card-head { display: flex; justify-content: space-between; align-items: center; font-size: 13px; color: var(--muted); }
.card-head strong { font-size: 18px; color: var(--text); font-variant-numeric: tabular-nums; }
.card-text { margin: 0 0 12px; color: var(--muted); font-size: 14px; }

/* ---------- bottoni ---------- */
.btn-primary, .btn-secondary, .btn-ghost {
  display: block; width: 100%; padding: 14px; border-radius: 12px;
  font-size: 16px; font-weight: 600; text-align: center; transition: opacity .15s, transform .1s;
}
.btn-primary { background: var(--accent); color: var(--on-accent); }
.btn-secondary { background: var(--surface); color: var(--accent); }
.btn-ghost { background: none; color: var(--muted); font-weight: 500; border: 1px dashed var(--line); margin-top: 12px; }
.btn-primary:active, .btn-secondary:active, .btn-ghost:active { transform: scale(.985); opacity: .85; }
.btn-primary[disabled] { opacity: .45; }
.btn-pay { margin-top: 4px; }
.btn-danger { background: var(--bad); color: #fff; }
.chip-btn { background: var(--surface); border-radius: 10px; padding: 9px 12px; font-size: 18px; line-height: 1; }

/* ---------- selettore giorno ---------- */
.daypicker { display: flex; gap: 8px; overflow-x: auto; padding: 2px 0 12px; scrollbar-width: none; }
.daypicker::-webkit-scrollbar { display: none; }
.day-chip { flex: 0 0 auto; background: var(--surface); border-radius: 11px; padding: 9px 13px;
            font-size: 13px; font-weight: 600; white-space: nowrap; }
.day-chip small { display: block; color: var(--muted); font-weight: 400; font-size: 11px; }
.day-chip.on { background: var(--accent); color: var(--on-accent); }
.day-chip.on small { color: inherit; opacity: .8; }

/* ---------- riepilogo di oggi ---------- */
.today-box { margin-bottom: 4px; }
.today-list { list-style: none; margin: 10px 0 0; padding: 0; }
.today-list li { display: flex; align-items: center; gap: 10px; padding: 7px 0; border-top: 1px solid var(--line); font-size: 15px; }
.today-list li span.t { color: var(--muted); font-size: 12px; font-variant-numeric: tabular-nums; }
.today-list li b { margin-left: auto; font-weight: 600; font-variant-numeric: tabular-nums; }
.del-x { color: var(--muted); font-size: 17px; padding: 2px 4px; line-height: 1; }

/* ---------- griglia articoli ---------- */
.item-grid { display: grid; grid-template-columns: repeat(2, 1fr); gap: 10px; }
.item {
  position: relative; background: var(--surface); border-radius: var(--radius);
  padding: 14px 12px; text-align: left; min-height: 74px;
  display: flex; flex-direction: column; justify-content: center; transition: transform .1s;
}
.item:active { transform: scale(.96); }
.item .name { font-weight: 600; font-size: 15px; line-height: 1.2; }
.item .price { color: var(--muted); font-size: 13px; margin-top: 3px; font-variant-numeric: tabular-nums; }
.item.qty-on { background: var(--accent); color: var(--on-accent); }
.item.qty-on .price { color: inherit; opacity: .85; }
.item .badge {
  position: absolute; top: 8px; right: 8px; min-width: 24px; height: 24px; padding: 0 6px;
  background: rgba(0,0,0,.28); border-radius: 12px; font-size: 13px; font-weight: 700;
  display: grid; place-items: center;
}
.item .minus {
  position: absolute; bottom: 6px; right: 6px; width: 30px; height: 30px;
  border-radius: 9px; background: rgba(0,0,0,.22); font-size: 19px; line-height: 1;
  display: grid; place-items: center;
}
.item.fav .name::after { content: " ★"; color: var(--warn); font-size: 12px; }
.item.qty-on.fav .name::after { color: inherit; opacity: .7; }

/* barra conferma fissa sopra la tabbar */
.cart-spacer { height: 4px; }
.cartbar {
  position: fixed; left: 0; right: 0; bottom: calc(60px + var(--safe-b)); z-index: 40;
  padding: 10px var(--pad); background: linear-gradient(to top, var(--bg) 72%, transparent);
}
.cartbar .inner { max-width: 640px; margin: 0 auto; }

/* ---------- lista giorni ---------- */
.days-list, .balance-list { display: flex; flex-direction: column; gap: 8px; }
.dayrow {
  display: flex; align-items: center; gap: 12px; background: var(--surface);
  border-radius: var(--radius); padding: 13px 14px; text-align: left; width: 100%;
}
.dayrow .dot { width: 9px; height: 9px; border-radius: 50%; flex: 0 0 auto; }
.dot.ok { background: var(--ok); } .dot.warn { background: var(--warn); } .dot.bad { background: var(--bad); }
.dayrow .d-main { flex: 1 1 auto; min-width: 0; }
/* Da span restano inline e si affiancano: servono blocchi perche' si
   impilino e perche' text-overflow abbia effetto. */
.dayrow .d-date, .dayrow .d-info { display: block; white-space: nowrap;
                                   overflow: hidden; text-overflow: ellipsis; }
.dayrow .d-date { font-weight: 600; font-size: 15px; }
.dayrow .d-info { color: var(--muted); font-size: 12.5px; margin-top: 2px; }
.dayrow .d-amt { font-weight: 700; font-variant-numeric: tabular-nums; font-size: 15px; }
.dayrow .d-amt.neg { color: var(--bad); } .dayrow .d-amt.pos { color: var(--ok); }

/* ---------- saldi ---------- */
.me-card { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.me-card .lbl { color: var(--muted); font-size: 13px; }
.me-card .val { font-size: 28px; font-weight: 700; font-variant-numeric: tabular-nums; letter-spacing: -.02em; }
.balrow { display: flex; align-items: center; gap: 12px; background: var(--surface);
          border-radius: var(--radius); padding: 12px 14px; }
.balrow .rank { width: 24px; color: var(--muted); font-size: 13px; font-weight: 600; font-variant-numeric: tabular-nums; }
.balrow .nm { flex: 1; font-weight: 600; font-size: 15px; min-width: 0;
              overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.balrow .bal { font-weight: 700; font-variant-numeric: tabular-nums; }
.balrow .bal.neg { color: var(--bad); } .balrow .bal.pos { color: var(--ok); }
.balrow.is-me { outline: 2px solid var(--accent); }
.balrow .tag { background: var(--bad); color: #fff; font-size: 10.5px; font-weight: 700;
               padding: 3px 7px; border-radius: 6px; text-transform: uppercase; letter-spacing: .04em; }

/* ---------- tabbar ---------- */
.tabbar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 50;
  display: grid; grid-template-columns: repeat(4, 1fr);
  background: var(--surface); border-top: 1px solid var(--line);
  padding-bottom: var(--safe-b);
}
.tab { padding: 8px 0 9px; font-size: 10.5px; font-weight: 600; color: var(--muted);
       display: flex; flex-direction: column; align-items: center; gap: 3px; }
.tab span { font-size: 21px; line-height: 1; }
.tab.active { color: var(--accent); }

/* ---------- sheet ---------- */
.sheet-wrap { position: fixed; inset: 0; z-index: 90; display: flex; align-items: flex-end; }
.sheet-backdrop { position: absolute; inset: 0; background: rgba(0,0,0,.5); animation: fade .2s ease; }
.sheet {
  position: relative; width: 100%; max-width: 640px; margin: 0 auto;
  background: var(--bg); border-radius: 18px 18px 0 0;
  max-height: 90vh; display: flex; flex-direction: column; animation: up .25s cubic-bezier(.2,.8,.3,1);
}
@keyframes fade { from { opacity: 0; } }
@keyframes up { from { transform: translateY(100%); } }
.sheet-grip { width: 36px; height: 4px; background: var(--line); border-radius: 2px; margin: 8px auto 0; }
.sheet-head { display: flex; align-items: center; justify-content: space-between; padding: 12px var(--pad) 6px; }
.sheet-x { color: var(--muted); font-size: 17px; padding: 4px 6px; }
.sheet-body { padding: 6px var(--pad) calc(var(--pad) + var(--safe-b)); overflow-y: auto; -webkit-overflow-scrolling: touch; }

/* ---------- form ---------- */
input[type=text], input[type=number], input[type=date] {
  width: 100%; padding: 14px; margin: 6px 0 12px; font-size: 17px;
  background: var(--surface); color: var(--text);
  border: 1px solid var(--line); border-radius: 12px; outline: none;
}
input:focus { border-color: var(--accent); }
label.fld { display: block; font-size: 13px; color: var(--muted); margin-top: 6px; }
.amount-in { font-size: 30px !important; font-weight: 700; text-align: center; font-variant-numeric: tabular-nums; }

.people { display: grid; grid-template-columns: repeat(2, 1fr); gap: 8px; margin: 4px 0 8px; }
.person { background: var(--surface); border-radius: 12px; padding: 13px 12px; font-size: 15px;
          font-weight: 500; text-align: left; display: flex; align-items: center; gap: 8px; }
.person .box { width: 19px; height: 19px; border-radius: 6px; border: 2px solid var(--muted); flex: 0 0 auto; }
.person.on { background: var(--accent); color: var(--on-accent); }
.person.on .box { border-color: currentColor; background: currentColor; position: relative; }
.person.on .box::after { content: "✓"; position: absolute; inset: 0; color: var(--accent);
                         font-size: 13px; font-weight: 900; display: grid; place-items: center; }

/* ---------- dettaglio giorno ---------- */
.kv { display: flex; justify-content: space-between; padding: 8px 0; border-bottom: 1px solid var(--line); font-size: 14.5px; }
.kv:last-child { border-bottom: 0; }
.kv .k { color: var(--muted); }
.kv .v { font-weight: 600; font-variant-numeric: tabular-nums; }
.kv .v.neg { color: var(--bad); } .kv .v.pos { color: var(--ok); }
.pill { display: inline-block; padding: 4px 10px; border-radius: 999px; font-size: 12px; font-weight: 700; }
.pill.ok { background: rgba(75,179,75,.16); color: var(--ok); }
.pill.bad { background: rgba(232,82,79,.16); color: var(--bad); }
.pill.warn { background: rgba(229,163,61,.16); color: var(--warn); }
.person-block { margin-top: 10px; }
.person-block .ph { display: flex; justify-content: space-between; font-weight: 600; font-size: 14.5px; padding: 6px 0; }
.person-block ul { list-style: none; margin: 0; padding: 0 0 0 2px; }
.person-block li { display: flex; justify-content: space-between; color: var(--muted);
                   font-size: 13px; padding: 3px 0; }

/* ---------- toast ---------- */
.toast {
  position: fixed; left: 50%; bottom: calc(84px + var(--safe-b)); transform: translateX(-50%);
  background: #000; color: #fff; padding: 11px 18px; border-radius: 11px; font-size: 14.5px;
  font-weight: 500; z-index: 120; max-width: 88vw; text-align: center; animation: up .2s ease;
  box-shadow: 0 6px 24px rgba(0,0,0,.35);
}
.toast.bad { background: var(--bad); }

/* ---------- onboarding ---------- */
.onboard { display: grid; place-items: center; min-height: 100vh; padding: var(--pad); }
.onboard-card { text-align: center; max-width: 340px; width: 100%; }
.onboard-icon { font-size: 54px; }
.onboard h1 { margin: 12px 0 6px; }
.onboard p { color: var(--muted); margin: 0 0 18px; font-size: 14.5px; }

.empty { text-align: center; color: var(--muted); font-size: 14px; padding: 26px 10px; }
