:root {
  --bg: #f5f5f7;
  --bg2: #ffffff;
  --bg3: #ebebef;
  --text: #111118;
  --text2: #6b6b80;
  --text3: #9999aa;
  --border: rgba(0,0,0,0.08);
  --border2: rgba(0,0,0,0.14);
  --accent: #2563eb;
  --accent-light: #eff3ff;
  --accent-text: #1d4ed8;
  --green: #16a34a;
  --green-light: #f0fdf4;
  --amber: #d97706;
  --amber-light: #fffbeb;
  --red: #dc2626;
  --red-light: #fef2f2;
  --radius: 14px;
  --radius-sm: 8px;
  --nav-h: 72px;
  --top-h: 56px;
  --font: 'DM Sans', system-ui, sans-serif;
  --mono: 'DM Mono', monospace;
  --shadow: 0 1px 3px rgba(0,0,0,0.08), 0 1px 2px rgba(0,0,0,0.04);
  --shadow-md: 0 4px 16px rgba(0,0,0,0.10);
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f0f14;
    --bg2: #1a1a24;
    --bg3: #252532;
    --text: #f0f0f8;
    --text2: #9090a8;
    --text3: #60607a;
    --border: rgba(255,255,255,0.07);
    --border2: rgba(255,255,255,0.12);
    --accent-light: #1e2d50;
    --accent-text: #93b4fa;
    --green-light: #0d2218;
    --amber-light: #271c06;
    --red-light: #2a0e0e;
    --shadow: 0 1px 3px rgba(0,0,0,0.4);
    --shadow-md: 0 4px 16px rgba(0,0,0,0.5);
  }
}

*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
html { font-size: 16px; -webkit-tap-highlight-color: transparent; }
body {
  font-family: var(--font);
  background: var(--bg);
  color: var(--text);
  min-height: 100dvh;
  overflow-x: hidden;
  -webkit-font-smoothing: antialiased;
}
input, select, textarea, button { font-family: var(--font); }

/* ── SCREENS ── */
.screen { display: none; min-height: 100dvh; }
.screen.active { display: flex; flex-direction: column; }

/* ── AUTH ── */
.auth-wrap {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 2rem 1.5rem;
  max-width: 420px;
  margin: 0 auto;
  width: 100%;
}
.auth-logo { margin-bottom: 1rem; }
.auth-title {
  font-size: 2rem;
  font-weight: 600;
  text-align: center;
  line-height: 1.2;
  margin-bottom: 0.5rem;
}
.auth-sub {
  font-size: 0.9rem;
  color: var(--text2);
  text-align: center;
  margin-bottom: 2rem;
  line-height: 1.6;
}
.auth-tabs {
  display: flex;
  background: var(--bg3);
  border-radius: 10px;
  padding: 3px;
  gap: 2px;
  margin-bottom: 1.5rem;
  width: 100%;
}
.auth-tab {
  flex: 1;
  padding: 8px;
  border: none;
  background: transparent;
  border-radius: 8px;
  font-size: 0.875rem;
  font-weight: 500;
  color: var(--text2);
  cursor: pointer;
  transition: all 0.2s;
}
.auth-tab.active {
  background: var(--bg2);
  color: var(--text);
  box-shadow: var(--shadow);
}
.auth-form { width: 100%; }
.auth-error {
  font-size: 0.8rem;
  color: var(--red);
  margin-bottom: 0.75rem;
  min-height: 1rem;
}
.auth-footer {
  margin-top: 2rem;
  font-size: 0.78rem;
  color: var(--text3);
}

/* ── TOPBAR ── */
.topbar {
  height: var(--top-h);
  background: var(--bg2);
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 1rem;
  position: sticky;
  top: 0;
  z-index: 50;
  flex-shrink: 0;
}
.topbar-left { display: flex; align-items: center; gap: 10px; }
.topbar-logo { display: flex; }
.topbar-title { font-size: 1rem; font-weight: 600; }
.topbar-right { display: flex; gap: 4px; }
.icon-btn {
  width: 38px; height: 38px;
  border: none;
  background: transparent;
  border-radius: var(--radius-sm);
  display: flex; align-items: center; justify-content: center;
  color: var(--text2);
  cursor: pointer;
  transition: background 0.15s;
}
.icon-btn:hover { background: var(--bg3); color: var(--text); }

/* ── MAIN ── */
.main-content {
  flex: 1;
  overflow-y: auto;
  padding: 1rem;
  padding-bottom: calc(var(--nav-h) + 1rem);
  -webkit-overflow-scrolling: touch;
}
.tab { display: none; }
.tab.active { display: block; }

/* ── BOTTOM NAV ── */
.bottom-nav {
  height: var(--nav-h);
  background: var(--bg2);
  border-top: 1px solid var(--border);
  display: flex;
  align-items: center;
  position: fixed;
  bottom: 0;
  left: 0; right: 0;
  z-index: 50;
  padding-bottom: env(safe-area-inset-bottom);
}
.nav-item {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 3px;
  padding: 8px 0;
  border: none;
  background: transparent;
  color: var(--text3);
  cursor: pointer;
  transition: color 0.15s;
  font-size: 0.68rem;
  font-weight: 500;
}
.nav-item.active { color: var(--accent); }
.nav-item svg { transition: transform 0.15s; }
.nav-item.active svg { transform: translateY(-1px); }

/* ── METRICS ── */
.metrics-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
  margin-bottom: 1.5rem;
}
.metric-card {
  background: var(--bg2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1rem;
  box-shadow: var(--shadow);
}
.metric-label { font-size: 0.75rem; color: var(--text2); margin-bottom: 4px; }
.metric-value { font-size: 1.5rem; font-weight: 600; line-height: 1; }
.metric-card.accent { background: var(--accent); border-color: var(--accent); }
.metric-card.accent .metric-label,
.metric-card.accent .metric-value { color: #fff; }

/* ── SECTION TITLE ── */
.section-title {
  font-size: 0.78rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--text3);
  margin-bottom: 0.625rem;
}

/* ── CARDS ── */
.card {
  background: var(--bg2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1rem;
  margin-bottom: 10px;
  box-shadow: var(--shadow);
}
.card-row {
  display: flex;
  align-items: center;
  gap: 10px;
}
.card-body { flex: 1; min-width: 0; }
.card-title { font-size: 0.9rem; font-weight: 500; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.card-sub { font-size: 0.78rem; color: var(--text2); margin-top: 2px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.card-right { display: flex; flex-direction: column; align-items: flex-end; gap: 4px; flex-shrink: 0; }
.card-amount { font-size: 0.9rem; font-weight: 600; font-variant-numeric: tabular-nums; }
.card-actions { display: flex; gap: 6px; margin-top: 10px; padding-top: 10px; border-top: 1px solid var(--border); }

/* ── AVATAR ── */
.avatar {
  width: 40px; height: 40px;
  border-radius: 50%;
  background: var(--accent-light);
  color: var(--accent-text);
  display: flex; align-items: center; justify-content: center;
  font-size: 0.8rem;
  font-weight: 600;
  flex-shrink: 0;
}

/* ── BADGES ── */
.badge {
  display: inline-flex;
  align-items: center;
  padding: 3px 8px;
  border-radius: 20px;
  font-size: 0.7rem;
  font-weight: 500;
  white-space: nowrap;
}
.badge-green { background: var(--green-light); color: var(--green); }
.badge-amber { background: var(--amber-light); color: var(--amber); }
.badge-blue  { background: var(--accent-light); color: var(--accent-text); }
.badge-red   { background: var(--red-light); color: var(--red); }
.badge-gray  { background: var(--bg3); color: var(--text2); }

/* ── SEARCH BAR ── */
.search-bar {
  display: flex;
  align-items: center;
  gap: 8px;
  background: var(--bg2);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 0 12px;
  margin-bottom: 1rem;
  height: 42px;
  color: var(--text2);
}
.search-bar input {
  flex: 1;
  border: none;
  background: transparent;
  color: var(--text);
  font-size: 0.9rem;
  outline: none;
}
.search-bar input::placeholder { color: var(--text3); }

/* ── FORM FIELDS ── */
.field { margin-bottom: 1rem; }
.field label { display: block; font-size: 0.78rem; font-weight: 500; color: var(--text2); margin-bottom: 5px; }
input[type="text"], input[type="email"], input[type="password"],
input[type="tel"], input[type="number"], input[type="date"],
select, textarea {
  width: 100%;
  padding: 10px 12px;
  background: var(--bg3);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  color: var(--text);
  font-size: 0.9rem;
  outline: none;
  transition: border-color 0.15s;
  appearance: none;
  -webkit-appearance: none;
}
input:focus, select:focus, textarea:focus {
  border-color: var(--accent);
  background: var(--bg2);
}
textarea { resize: vertical; min-height: 70px; line-height: 1.5; }
select { background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 24 24' fill='none' stroke='%239999aa' stroke-width='2'%3E%3Cpolyline points='6 9 12 15 18 9'/%3E%3C/svg%3E"); background-repeat: no-repeat; background-position: right 12px center; padding-right: 36px; }
.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.field-row { display: flex; gap: 8px; align-items: flex-end; }

/* ── BUTTONS ── */
.btn-main {
  width: 100%;
  padding: 13px;
  background: var(--accent);
  color: #fff;
  border: none;
  border-radius: var(--radius-sm);
  font-size: 0.95rem;
  font-weight: 600;
  cursor: pointer;
  transition: opacity 0.15s, transform 0.1s;
  margin-top: 0.25rem;
}
.btn-main:hover { opacity: 0.9; }
.btn-main:active { transform: scale(0.98); }
.btn {
  padding: 8px 14px;
  border: 1px solid var(--border2);
  border-radius: var(--radius-sm);
  background: transparent;
  color: var(--text);
  font-size: 0.82rem;
  font-weight: 500;
  cursor: pointer;
  transition: background 0.15s;
  white-space: nowrap;
}
.btn:hover { background: var(--bg3); }
.btn-accent { background: var(--accent); color: #fff; border-color: var(--accent); }
.btn-accent:hover { opacity: 0.9; background: var(--accent); }
.btn-danger { color: var(--red); border-color: var(--red); }
.btn-danger:hover { background: var(--red-light); }
.btn-sm { padding: 5px 10px; font-size: 0.78rem; }
.btn-icon { width: 34px; height: 34px; padding: 0; display: flex; align-items: center; justify-content: center; flex-shrink: 0; }

/* ── DRAWER ── */
.drawer-overlay {
  display: none;
  position: fixed; inset: 0;
  background: rgba(0,0,0,0.5);
  z-index: 100;
  backdrop-filter: blur(2px);
}
.drawer-overlay.open { display: block; }
.drawer {
  position: fixed;
  bottom: 0; left: 0; right: 0;
  background: var(--bg2);
  border-radius: 20px 20px 0 0;
  z-index: 101;
  transform: translateY(100%);
  transition: transform 0.3s cubic-bezier(0.32, 0.72, 0, 1);
  max-height: 92dvh;
  display: flex;
  flex-direction: column;
}
.drawer.open { transform: translateY(0); }
.drawer-handle {
  width: 36px; height: 4px;
  background: var(--border2);
  border-radius: 2px;
  margin: 10px auto 4px;
  flex-shrink: 0;
}
#drawer-body {
  overflow-y: auto;
  padding: 0.5rem 1.25rem 2rem;
  flex: 1;
  -webkit-overflow-scrolling: touch;
}
.drawer-title {
  font-size: 1.1rem;
  font-weight: 600;
  margin-bottom: 1.25rem;
  padding-top: 0.25rem;
}
.drawer-actions {
  display: flex;
  gap: 8px;
  margin-top: 1.25rem;
  padding-top: 1rem;
  border-top: 1px solid var(--border);
}
.drawer-actions .btn-main { margin-top: 0; }

/* ── VOCI PREVENTIVO ── */
.voce-list { margin-bottom: 0.75rem; }
.voce-item {
  background: var(--bg3);
  border-radius: var(--radius-sm);
  padding: 10px 12px;
  margin-bottom: 8px;
  position: relative;
}
.voce-item.materiale { border-left: 3px solid var(--accent); }
.voce-item.manodopera { border-left: 3px solid var(--green); }
.voce-header-row { display: flex; gap: 8px; margin-bottom: 8px; align-items: center; }
.voce-tipo { font-size: 0.7rem; font-weight: 600; text-transform: uppercase; letter-spacing: 0.05em; }
.voce-tipo.mat { color: var(--accent-text); }
.voce-tipo.man { color: var(--green); }
.voce-nums { display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 6px; }
.voce-nums input { padding: 7px 8px; font-size: 0.82rem; }
.voce-del {
  position: absolute; top: 8px; right: 8px;
  width: 24px; height: 24px;
  border: none; background: transparent;
  color: var(--text3); cursor: pointer;
  display: flex; align-items: center; justify-content: center;
  border-radius: 4px;
  font-size: 0.9rem;
}
.voce-del:hover { background: var(--bg2); color: var(--red); }
.add-voce-btns { display: flex; gap: 8px; margin-bottom: 1rem; }

/* ── TOTALI BOX ── */
.totali-box {
  background: var(--bg3);
  border-radius: var(--radius-sm);
  padding: 12px;
  margin-bottom: 1rem;
}
.totali-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 0.85rem;
  padding: 3px 0;
  color: var(--text2);
}
.totali-row.total {
  font-size: 1rem;
  font-weight: 600;
  color: var(--text);
  padding-top: 8px;
  margin-top: 4px;
  border-top: 1px solid var(--border2);
}

/* ── EMPTY STATE ── */
.empty {
  text-align: center;
  padding: 3rem 1rem;
  color: var(--text3);
  font-size: 0.9rem;
}
.empty svg { opacity: 0.3; margin-bottom: 1rem; }

/* ── TOAST ── */
.toast {
  position: fixed;
  bottom: calc(var(--nav-h) + 12px);
  left: 50%; transform: translateX(-50%) translateY(20px);
  background: var(--text);
  color: var(--bg2);
  padding: 10px 18px;
  border-radius: 20px;
  font-size: 0.85rem;
  font-weight: 500;
  opacity: 0;
  transition: all 0.25s;
  z-index: 200;
  white-space: nowrap;
  pointer-events: none;
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }

/* ── LOADING ── */
.loading {
  display: flex; align-items: center; justify-content: center;
  padding: 2rem;
  color: var(--text3);
  font-size: 0.85rem;
  gap: 8px;
}
@keyframes spin { to { transform: rotate(360deg); } }
.spinner {
  width: 18px; height: 18px;
  border: 2px solid var(--border2);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.7s linear infinite;
}

/* ── PDF PRINT ── */
@media print {
  .bottom-nav, .topbar, .drawer, .drawer-overlay, .toast { display: none !important; }
  .main-content { padding-bottom: 0; }
}

/* ── DESKTOP ── */
@media (min-width: 768px) {
  body { display: flex; justify-content: center; }
  #screen-app {
    max-width: 480px;
    width: 100%;
    position: relative;
    box-shadow: 0 0 0 1px var(--border), var(--shadow-md);
    min-height: 100dvh;
  }
  .drawer { max-width: 480px; left: 50%; transform: translateX(-50%) translateY(100%); right: auto; width: 100%; }
  .drawer.open { transform: translateX(-50%) translateY(0); }
  #screen-auth { max-width: 420px; margin: 0 auto; }
}

/* ── CONFIGURAZIONE SUPABASE ── */
.config-box {
  background: var(--amber-light);
  border: 1px solid var(--amber);
  border-radius: var(--radius-sm);
  padding: 1rem;
  margin-bottom: 1.25rem;
}
.config-box p { font-size: 0.82rem; color: var(--amber); line-height: 1.5; }
.config-box strong { color: var(--amber); }
.config-box code {
  font-family: var(--mono);
  font-size: 0.78rem;
  background: rgba(0,0,0,0.1);
  padding: 1px 5px;
  border-radius: 4px;
}
