:root {
  --bg: #0f172a;
  --surface: #1e293b;
  --surface-2: #334155;
  --text: #f1f5f9;
  --text-dim: #94a3b8;
  --primary: #38bdf8;
  --primary-contrast: #04263a;
  --danger: #f87171;
  --radius: 14px;
  --gap: 16px;
  --bar-h: 84px;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  line-height: 1.5;
}

body {
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
  padding-bottom: calc(var(--bar-h) + env(safe-area-inset-bottom));
}

.app-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--gap);
  padding: calc(env(safe-area-inset-top) + 14px) 18px 14px;
  background: var(--surface);
  position: sticky;
  top: 0;
  z-index: 10;
}

.app-header h1 { font-size: 1.25rem; margin: 0; letter-spacing: 0.5px; }

.app-main {
  flex: 1;
  width: 100%;
  max-width: 640px;
  margin: 0 auto;
  padding: var(--gap);
  display: flex;
  flex-direction: column;
  gap: var(--gap);
}

.card {
  background: var(--surface);
  border-radius: var(--radius);
  padding: var(--gap);
}

.video-wrap {
  position: relative;
  width: 100%;
  aspect-ratio: 4 / 3;
  max-height: 68vh;
  background: #000;
  border-radius: var(--radius);
  overflow: hidden;
}

#preview {
  width: 100%;
  height: 100%;
  object-fit: contain;
  display: block;
}

.reticle {
  position: absolute;
  inset: 20% 12%;
  border: 3px solid rgba(56, 189, 248, 0.9);
  border-radius: 12px;
  box-shadow: 0 0 0 9999px rgba(0, 0, 0, 0.35);
  pointer-events: none;
  transition: inset 0.15s ease;
}

.video-wrap.tight .reticle {
  inset: 35% 16%;
  border-color: rgba(74, 222, 128, 0.95);
  box-shadow: 0 0 0 9999px rgba(0, 0, 0, 0.5);
}

.live-code {
  position: absolute;
  left: 0;
  right: 0;
  top: 0;
  z-index: 3;
  padding: 10px 12px;
  background: rgba(4, 38, 58, 0.85);
  color: #e0f2fe;
  font-family: ui-monospace, Menlo, Consolas, monospace;
  font-size: 1rem;
  font-weight: 700;
  text-align: center;
  word-break: break-all;
  pointer-events: none;
}

.tap-hint {
  position: absolute;
  left: 50%;
  bottom: 12px;
  transform: translateX(-50%);
  z-index: 3;
  margin: 0;
  padding: 6px 14px;
  border-radius: 999px;
  background: rgba(4, 38, 58, 0.75);
  color: #e0f2fe;
  font-size: 0.85rem;
  white-space: nowrap;
  pointer-events: none;
}

.controls {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  margin-top: var(--gap);
}

.btn {
  flex: 1 1 auto;
  min-width: 110px;
  padding: 12px 16px;
  border: none;
  border-radius: 10px;
  background: var(--surface-2);
  color: var(--text);
  font-size: 1rem;
  font-weight: 600;
  cursor: pointer;
  transition: transform 0.05s ease, opacity 0.15s ease;
}

.btn:active { transform: scale(0.98); }
.btn:disabled { opacity: 0.5; cursor: not-allowed; }
.btn-primary { background: var(--primary); color: var(--primary-contrast); }
.btn-ghost { background: transparent; border: 1px solid var(--surface-2); flex: 0 0 auto; min-width: 0; }

.status {
  margin: 12px 0 0;
  color: var(--text-dim);
  font-size: 0.9rem;
  min-height: 1.4em;
}

.diag {
  margin-top: 4px;
  font-family: ui-monospace, Menlo, Consolas, monospace;
  font-size: 0.72rem;
  color: var(--text-dim);
  white-space: pre-wrap;
  word-break: break-all;
  max-height: 40vh;
  overflow: auto;
  -webkit-user-select: text;
  user-select: text;
}

.result-value {
  font-size: 1.15rem;
  font-weight: 700;
  word-break: break-all;
  margin: 6px 0;
}

.result-meta { color: var(--text-dim); font-size: 0.85rem; margin: 0 0 4px; }

.history-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}

.history-head-btns { display: flex; gap: 8px; }

h2 { font-size: 1rem; margin: 0 0 8px; }

.history-list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.history-list li {
  background: var(--surface-2);
  border-radius: 10px;
  padding: 10px 12px;
  font-size: 0.9rem;
  word-break: break-all;
}

.history-list li.empty { background: transparent; color: var(--text-dim); padding: 4px 0; }
.history-list .h-value { font-weight: 600; }
.history-list .h-meta { display: block; color: var(--text-dim); font-size: 0.78rem; margin-top: 2px; }

.app-footer {
  display: flex;
  align-items: center;
  gap: 10px;
  justify-content: center;
  padding: 14px;
  color: var(--text-dim);
}

.badge {
  background: var(--danger);
  color: #2a0a0a;
  font-weight: 700;
  font-size: 0.75rem;
  padding: 2px 8px;
  border-radius: 999px;
}

/* Barra azioni fissa in basso */
.action-bar {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 20;
  display: flex;
  gap: 10px;
  padding: 12px 14px calc(12px + env(safe-area-inset-bottom));
  background: var(--surface);
  box-shadow: 0 -6px 20px rgba(0, 0, 0, 0.35);
}

.bar-main {
  flex: 1;
  padding: 16px;
  font-size: 1.1rem;
}

.bar-side { flex: 0 0 auto; min-width: 96px; }

.qty-bar { width: 100%; }

.qty-code {
  margin: 0 0 8px;
  text-align: center;
  font-family: ui-monospace, Menlo, Consolas, monospace;
  font-size: 1.05rem;
  font-weight: 700;
  color: #e0f2fe;
  word-break: break-all;
}

.qty-line { display: flex; align-items: stretch; gap: 8px; }

.qty-row { display: flex; gap: 6px; flex: 0 0 auto; }

.qty-step {
  flex: 0 0 auto;
  min-width: 52px;
  font-size: 1.5rem;
  line-height: 1;
  padding: 8px 0;
}

.qty-input {
  width: 68px;
  text-align: center;
  font-size: 1.4rem;
  font-weight: 700;
  border: none;
  border-radius: 10px;
  background: var(--bg);
  color: var(--text);
  padding: 4px;
}

.qty-cancel { flex: 0 0 auto; min-width: 0; padding: 12px; }
.qty-ok { flex: 1; padding: 14px; font-size: 1.15rem; }

@media (prefers-color-scheme: light) {
  :root {
    --bg: #f1f5f9;
    --surface: #ffffff;
    --surface-2: #e2e8f0;
    --text: #0f172a;
    --text-dim: #475569;
    --primary: #0284c7;
    --primary-contrast: #ffffff;
  }
}
