/* ═══════════════════════════════════════════════════════════════
   Scontrini App — Mobile-First Design System
   ═══════════════════════════════════════════════════════════════ */

:root {
  /* Palette */
  --primary: #3b82f6;
  --primary-dark: #2563eb;
  --primary-light: #dbeafe;
  --accent: #f59e0b;
  --accent-dark: #d97706;
  --success: #10b981;
  --danger: #ef4444;

  /* Neutri (light mode) */
  --bg: #f1f5f9;
  --surface: #ffffff;
  --surface-2: #f8fafc;
  --surface-hover: #e2e8f0;
  --border: #e2e8f0;
  --text: #0f172a;
  --text-secondary: #475569;
  --text-muted: #94a3b8;

  /* Spacing */
  --space-xs: 0.25rem;
  --space-sm: 0.5rem;
  --space-md: 1rem;
  --space-lg: 1.5rem;
  --space-xl: 2rem;

  /* Radius */
  --radius-sm: 0.5rem;
  --radius-md: 0.75rem;
  --radius-lg: 1rem;
  --radius-xl: 1.5rem;
  --radius-full: 9999px;

  /* Shadows */
  --shadow-sm: 0 1px 2px rgb(0 0 0 / 0.05);
  --shadow: 0 2px 8px rgb(0 0 0 / 0.08);
  --shadow-md: 0 4px 12px rgb(0 0 0 / 0.1);
  --shadow-lg: 0 10px 25px rgb(0 0 0 / 0.15);

  /* Safe areas (notch, home indicator) */
  --sat: env(safe-area-inset-top, 0px);
  --sab: env(safe-area-inset-bottom, 0px);
  --sal: env(safe-area-inset-left, 0px);
  --sar: env(safe-area-inset-right, 0px);

  /* Bottom nav height */
  --nav-height: 4.5rem;

  --transition: all 0.2s cubic-bezier(0.4, 0, 0.2, 1);
}

/* Dark mode */
[data-theme="dark"] {
  --bg: #0f172a;
  --surface: #1e293b;
  --surface-2: #334155;
  --surface-hover: #475569;
  --border: #334155;
  --text: #f1f5f9;
  --text-secondary: #cbd5e1;
  --text-muted: #64748b;
  --primary-light: #1e3a8a;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0f172a;
    --surface: #1e293b;
    --surface-2: #334155;
    --surface-hover: #475569;
    --border: #334155;
    --text: #f1f5f9;
    --text-secondary: #cbd5e1;
    --text-muted: #64748b;
    --primary-light: #1e3a8a;
  }
}

* { box-sizing: border-box; margin: 0; padding: 0; }

html {
  font-size: 17px; /* Base più grande per mobile */
  -webkit-text-size-adjust: 100%;
  -webkit-tap-highlight-color: transparent;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Inter', 'Segoe UI', Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.5;
  min-height: 100vh;
  min-height: 100dvh;
  padding-bottom: calc(var(--nav-height) + var(--sab));
  overflow-x: hidden;
  -webkit-font-smoothing: antialiased;
}

/* ═══════════════════════════════════════════════════════════════
   Header
   ═══════════════════════════════════════════════════════════════ */

.app-header {
  position: sticky;
  top: 0;
  z-index: 50;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  padding: calc(var(--space-md) + var(--sat)) var(--space-md) var(--space-md);
  display: flex;
  align-items: center;
  justify-content: space-between;
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
}

.page-title {
  font-size: 1.25rem;
  font-weight: 700;
  color: var(--text);
}

.icon-btn {
  width: 44px;
  height: 44px;
  border: none;
  background: var(--surface-2);
  border-radius: var(--radius-full);
  font-size: 1.2rem;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: var(--transition);
}

.icon-btn:active { transform: scale(0.92); }

/* ═══════════════════════════════════════════════════════════════
   Main
   ═══════════════════════════════════════════════════════════════ */

.app-main {
  padding: var(--space-md);
  padding-left: calc(var(--space-md) + var(--sal));
  padding-right: calc(var(--space-md) + var(--sar));
  max-width: 720px;
  margin: 0 auto;
}

h1, h2, h3 { line-height: 1.25; font-weight: 700; }
h2 { font-size: 1.5rem; margin-bottom: var(--space-md); }
h3 { font-size: 1.15rem; margin-bottom: var(--space-sm); color: var(--text-secondary); }

/* ═══════════════════════════════════════════════════════════════
   Bottom navigation
   ═══════════════════════════════════════════════════════════════ */

.bottom-nav {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  height: calc(var(--nav-height) + var(--sab));
  padding-bottom: var(--sab);
  background: var(--surface);
  border-top: 1px solid var(--border);
  display: flex;
  justify-content: space-around;
  align-items: center;
  z-index: 100;
  box-shadow: 0 -2px 10px rgb(0 0 0 / 0.05);
}

.nav-item {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  padding: var(--space-xs);
  color: var(--text-muted);
  text-decoration: none;
  font-size: 0.7rem;
  font-weight: 500;
  transition: var(--transition);
  min-height: 48px;
  position: relative;
}

.nav-item .nav-icon {
  font-size: 1.5rem;
  transition: var(--transition);
}

.nav-item.active {
  color: var(--primary);
}

.nav-item.active::before {
  content: '';
  position: absolute;
  top: 0;
  left: 25%;
  right: 25%;
  height: 3px;
  background: var(--primary);
  border-radius: 0 0 3px 3px;
}

.nav-item.scan-btn {
  color: var(--accent);
}

.nav-item.scan-btn .nav-icon {
  background: var(--accent);
  color: white;
  width: 48px;
  height: 48px;
  border-radius: var(--radius-full);
  display: flex;
  align-items: center;
  justify-content: center;
  margin-top: -20px;
  box-shadow: var(--shadow-md);
  font-size: 1.4rem;
}

.nav-item:active { transform: scale(0.92); }

/* ═══════════════════════════════════════════════════════════════
   FAB (Floating Action Button)
   ═══════════════════════════════════════════════════════════════ */

.fab {
  display: none; /* Nascosto: usiamo il bottone nella bottom nav */
}

/* ═══════════════════════════════════════════════════════════════
   Stats
   ═══════════════════════════════════════════════════════════════ */

.stats {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: var(--space-sm);
  margin-bottom: var(--space-lg);
}

.stats div {
  background: var(--surface);
  border-radius: var(--radius-lg);
  padding: var(--space-md);
  text-align: center;
  box-shadow: var(--shadow-sm);
  border: 1px solid var(--border);
}

.stats b {
  display: block;
  font-size: 1.5rem;
  font-weight: 800;
  color: var(--primary);
  margin-bottom: 2px;
}

.stats span {
  font-size: 0.75rem;
  color: var(--text-secondary);
  text-transform: uppercase;
  letter-spacing: 0.05em;
  font-weight: 600;
}

/* ═══════════════════════════════════════════════════════════════
   Search
   ═══════════════════════════════════════════════════════════════ */

.search {
  display: flex;
  gap: var(--space-sm);
  margin-bottom: var(--space-lg);
}

.search input {
  flex: 1;
  padding: 0.875rem 1rem;
  border: 2px solid var(--border);
  border-radius: var(--radius-full);
  font-size: 1rem;
  background: var(--surface);
  color: var(--text);
  min-height: 48px;
}

.search input:focus {
  outline: none;
  border-color: var(--primary);
}

.search button {
  padding: 0 1.25rem;
  border-radius: var(--radius-full);
  min-height: 48px;
  min-width: 48px;
}

/* ═══════════════════════════════════════════════════════════════
   Buttons
   ═══════════════════════════════════════════════════════════════ */

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: var(--space-xs);
  background: var(--surface);
  color: var(--text);
  border: 2px solid var(--border);
  padding: 0.75rem 1.25rem;
  border-radius: var(--radius-md);
  font-size: 1rem;
  font-weight: 600;
  cursor: pointer;
  transition: var(--transition);
  text-decoration: none;
  min-height: 48px;
  -webkit-user-select: none;
  user-select: none;
}

.btn:active { transform: scale(0.96); }

.btn.primary {
  background: var(--primary);
  color: white;
  border-color: var(--primary);
}

.btn.primary:active { background: var(--primary-dark); }

.btn.accent {
  background: var(--accent);
  color: white;
  border-color: var(--accent);
}

.btn.danger {
  background: var(--danger);
  color: white;
  border-color: var(--danger);
}

.btn.small {
  padding: 0.5rem 0.875rem;
  font-size: 0.9rem;
  min-height: 40px;
}

.btn.big {
  width: 100%;
  padding: 1rem;
  font-size: 1.1rem;
  margin-top: var(--space-md);
  min-height: 56px;
}

/* ═══════════════════════════════════════════════════════════════
   Cards & product list
   ═══════════════════════════════════════════════════════════════ */

.card {
  background: var(--surface);
  padding: var(--space-lg);
  border-radius: var(--radius-lg);
  margin-bottom: var(--space-md);
  box-shadow: var(--shadow-sm);
  border: 1px solid var(--border);
}

.cards {
  list-style: none;
  display: flex;
  flex-direction: column;
  gap: var(--space-sm);
}

.cards li {
  background: var(--surface);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-sm);
  border: 1px solid var(--border);
  overflow: hidden;
  transition: var(--transition);
}

.cards li:active {
  transform: scale(0.98);
  background: var(--surface-hover);
}

.cards a {
  text-decoration: none;
  color: inherit;
  display: block;
  padding: var(--space-md);
}

.cards strong {
  display: block;
  font-size: 1.05rem;
  color: var(--text);
  margin-bottom: 4px;
}

.cards small {
  display: block;
  color: var(--text-secondary);
  font-size: 0.85rem;
  margin-bottom: var(--space-xs);
  word-break: break-all;
}

.cards .meta {
  display: flex;
  align-items: center;
  gap: var(--space-xs);
  color: var(--primary);
  font-size: 0.85rem;
  font-weight: 600;
  margin-top: var(--space-xs);
  flex-wrap: wrap;
}

.cards .meta .best-price {
  background: var(--primary-light);
  padding: 2px 8px;
  border-radius: var(--radius-full);
  color: var(--primary-dark);
}

/* ═══════════════════════════════════════════════════════════════
   Forms
   ═══════════════════════════════════════════════════════════════ */

label {
  display: block;
  margin-bottom: var(--space-md);
  font-weight: 600;
  color: var(--text);
  font-size: 0.95rem;
}

label input,
label textarea,
label select {
  width: 100%;
  padding: 0.875rem 1rem;
  border: 2px solid var(--border);
  border-radius: var(--radius-md);
  font-size: 1rem;
  background: var(--surface);
  color: var(--text);
  margin-top: var(--space-xs);
  min-height: 48px;
  transition: var(--transition);
  -webkit-appearance: none;
  appearance: none;
}

label input:focus,
label textarea:focus {
  outline: none;
  border-color: var(--primary);
  box-shadow: 0 0 0 3px var(--primary-light);
}

.row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: var(--space-sm);
}

.row label { margin-bottom: 0; }

/* Quick select chips per negozi frequenti */
.quick-select {
  display: flex;
  gap: var(--space-xs);
  flex-wrap: wrap;
  margin-bottom: var(--space-md);
}

.chip {
  padding: 0.5rem 0.875rem;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: var(--radius-full);
  font-size: 0.85rem;
  cursor: pointer;
  transition: var(--transition);
  min-height: 36px;
  display: inline-flex;
  align-items: center;
  color: var(--text);
}

.chip:active {
  background: var(--primary);
  color: white;
  border-color: var(--primary);
}

/* ═══════════════════════════════════════════════════════════════
   Tables
   ═══════════════════════════════════════════════════════════════ */

.table-wrap {
  overflow-x: auto;
  margin-bottom: var(--space-md);
  border-radius: var(--radius-lg);
  background: var(--surface);
  box-shadow: var(--shadow-sm);
  border: 1px solid var(--border);
  -webkit-overflow-scrolling: touch;
}

table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.9rem;
  min-width: 400px;
}

thead {
  background: var(--surface-2);
}

th {
  padding: 0.75rem;
  text-align: left;
  font-weight: 700;
  font-size: 0.75rem;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--text-secondary);
  border-bottom: 2px solid var(--border);
}

td {
  padding: 0.75rem;
  border-bottom: 1px solid var(--border);
}

tbody tr:last-child td { border-bottom: none; }

tr.best {
  background: linear-gradient(90deg, #fef3c7 0%, #fde68a 100%);
  font-weight: 600;
}

[data-theme="dark"] tr.best {
  background: linear-gradient(90deg, #78350f 0%, #92400e 100%);
  color: #fef3c7;
}

/* ═══════════════════════════════════════════════════════════════
   Best price box
   ═══════════════════════════════════════════════════════════════ */

.best-box {
  background: linear-gradient(135deg, #fef3c7 0%, #fde68a 100%);
  border-left: 4px solid var(--accent);
  padding: var(--space-md);
  border-radius: var(--radius-lg);
  margin-bottom: var(--space-md);
  box-shadow: var(--shadow-sm);
  font-size: 0.95rem;
}

[data-theme="dark"] .best-box {
  background: linear-gradient(135deg, #78350f 0%, #92400e 100%);
  color: #fef3c7;
}

.best-box b { color: var(--accent-dark); }
[data-theme="dark"] .best-box b { color: #fcd34d; }

/* ═══════════════════════════════════════════════════════════════
   Scanner
   ═══════════════════════════════════════════════════════════════ */

.scanner-container {
  position: relative;
  width: 100%;
  max-width: 500px;
  margin: 0 auto var(--space-md);
  border-radius: var(--radius-xl);
  overflow: hidden;
  background: #000;
  aspect-ratio: 4/3;
  box-shadow: var(--shadow-lg);
}

#reader {
  width: 100%;
  height: 100%;
}

#reader video {
  object-fit: cover !important;
  width: 100% !important;
  height: 100% !important;
}

.scanner-overlay {
  position: absolute;
  inset: 0;
  pointer-events: none;
  display: flex;
  align-items: center;
  justify-content: center;
}

.scan-frame {
  width: 75%;
  height: 40%;
  border: 3px solid var(--accent);
  border-radius: var(--radius-lg);
  box-shadow: 0 0 0 9999px rgb(0 0 0 / 0.4);
  position: relative;
}

.scan-frame::before,
.scan-frame::after {
  content: '';
  position: absolute;
  width: 30px;
  height: 30px;
  border: 4px solid var(--accent);
}

.scan-frame::before {
  top: -4px;
  left: -4px;
  border-right: none;
  border-bottom: none;
  border-radius: 10px 0 0 0;
}

.scan-frame::after {
  bottom: -4px;
  right: -4px;
  border-left: none;
  border-top: none;
  border-radius: 0 0 10px 0;
}

.scanner-controls {
  display: flex;
  justify-content: center;
  gap: var(--space-sm);
  margin-bottom: var(--space-md);
}

.scanner-controls .btn {
  flex: 1;
  max-width: 150px;
}

.status {
  text-align: center;
  color: var(--text-secondary);
  padding: var(--space-md);
  font-size: 0.95rem;
  background: var(--surface);
  border-radius: var(--radius-lg);
  margin-bottom: var(--space-md);
  border: 2px dashed var(--border);
}

.status.success {
  background: #d1fae5;
  color: #065f46;
  border-color: var(--success);
}

.status.error {
  background: #fee2e2;
  color: #991b1b;
  border-color: var(--danger);
}

[data-theme="dark"] .status.success {
  background: #064e3b;
  color: #a7f3d0;
}

[data-theme="dark"] .status.error {
  background: #7f1d1d;
  color: #fecaca;
}

/* ═══════════════════════════════════════════════════════════════
   Barcode display
   ═══════════════════════════════════════════════════════════════ */

.barcode-display {
  background: var(--surface-2);
  padding: var(--space-sm) var(--space-md);
  border-radius: var(--radius-md);
  display: inline-flex;
  align-items: center;
  gap: var(--space-xs);
  margin-bottom: var(--space-md);
  font-family: 'Courier New', monospace;
  font-weight: 600;
  color: var(--primary-dark);
}

[data-theme="dark"] .barcode-display { color: #93c5fd; }

/* ═══════════════════════════════════════════════════════════════
   Empty state
   ═══════════════════════════════════════════════════════════════ */

.empty {
  text-align: center;
  color: var(--text-muted);
  padding: var(--space-xl) var(--space-md);
  font-size: 1rem;
}

.empty-icon {
  font-size: 3rem;
  display: block;
  margin-bottom: var(--space-sm);
  opacity: 0.5;
}

.empty a {
  color: var(--primary);
  font-weight: 600;
  text-decoration: none;
}

/* ═══════════════════════════════════════════════════════════════
   Toast notifications
   ═══════════════════════════════════════════════════════════════ */

.toast-container {
  position: fixed;
  top: calc(var(--space-md) + var(--sat));
  left: 50%;
  transform: translateX(-50%);
  z-index: 1000;
  display: flex;
  flex-direction: column;
  gap: var(--space-xs);
  pointer-events: none;
  width: calc(100% - 2rem);
  max-width: 400px;
}

.toast {
  background: var(--text);
  color: var(--bg);
  padding: 0.875rem 1rem;
  border-radius: var(--radius-md);
  box-shadow: var(--shadow-lg);
  font-size: 0.9rem;
  font-weight: 500;
  animation: slideDown 0.3s ease-out;
  pointer-events: auto;
}

.toast.success { background: var(--success); color: white; }
.toast.error { background: var(--danger); color: white; }

@keyframes slideDown {
  from { opacity: 0; transform: translateY(-20px); }
  to { opacity: 1; transform: translateY(0); }
}

/* ═══════════════════════════════════════════════════════════════
   Misc
   ═══════════════════════════════════════════════════════════════ */

code {
  background: var(--surface-2);
  padding: 2px 6px;
  border-radius: var(--radius-sm);
  font-family: 'Courier New', monospace;
  font-size: 0.85rem;
  color: var(--primary-dark);
  font-weight: 600;
}

[data-theme="dark"] code { color: #93c5fd; }

.muted { color: var(--text-secondary); font-size: 0.9rem; }
.hidden { display: none !important; }

/* Install banner */
.install-banner {
  background: var(--primary-light);
  color: var(--primary-dark);
  padding: var(--space-md);
  border-radius: var(--radius-lg);
  margin-bottom: var(--space-md);
  display: flex;
  align-items: center;
  gap: var(--space-md);
}

.install-banner button {
  margin-left: auto;
  background: var(--primary);
  color: white;
  border: none;
  padding: 0.5rem 1rem;
  border-radius: var(--radius-md);
  font-weight: 600;
  cursor: pointer;
}

/* ═══════════════════════════════════════════════════════════════
   Desktop (>768px)
   ═══════════════════════════════════════════════════════════════ */

@media (min-width: 768px) {
  html { font-size: 16px; }

  .app-main {
    padding: var(--space-xl);
  }

  .stats {
    grid-template-columns: repeat(4, 1fr);
  }

  .cards {
    display: grid;
    grid-template-columns: repeat(2, 1fr);
  }

  .bottom-nav {
    max-width: 500px;
    left: 50%;
    transform: translateX(-50%);
    border-radius: var(--radius-xl) var(--radius-xl) 0 0;
    border: 1px solid var(--border);
    border-bottom: none;
  }
}

/* ═══════════════════════════════════════════════════════════════
   Print
   ═══════════════════════════════════════════════════════════════ */

@media print {
  .app-header, .bottom-nav, .fab, .btn, .scanner-container { display: none; }
  body { padding: 0; background: white; color: black; }
}

/* Danger zone */
.danger-zone {
  border: 2px solid var(--danger);
  background: linear-gradient(135deg, #fef2f2 0%, #fee2e2 100%);
}

[data-theme="dark"] .danger-zone {
  background: linear-gradient(135deg, #450a0a 0%, #7f1d1d 100%);
}

.danger-zone h3 {
  color: var(--danger);
}