/* =============================================
   CSS Variables & Reset
   ============================================= */
:root {
  --primary: #1a73e8;
  --primary-dark: #1557b0;
  --success: #34a853;
  --success-dark: #2d8f47;
  --danger: #ea4335;
  --warning: #fbbc04;
  --secondary: #5f6368;
  --bg: #f8f9fa;
  --surface: #ffffff;
  --border: #dadce0;
  --text: #202124;
  --text-secondary: #5f6368;
  --shadow: 0 2px 8px rgba(0,0,0,0.12);
  --radius: 12px;
  --radius-sm: 8px;
  --header-h: 56px;
}

*, *::before, *::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
  -webkit-tap-highlight-color: transparent;
}

html, body {
  height: 100%;
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  font-size: 16px;
  color: var(--text);
  background: var(--bg);
  overflow: hidden;
  -webkit-font-smoothing: antialiased;
}

/* =============================================
   Screen System
   ============================================= */
.screen {
  position: fixed;
  inset: 0;
  display: none;
  flex-direction: column;
  background: var(--bg);
  padding-bottom: env(safe-area-inset-bottom);
}

.screen.active {
  display: flex;
}

/* =============================================
   Header
   ============================================= */
.app-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 16px;
  height: var(--header-h);
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
  padding-top: env(safe-area-inset-top);
}

.app-header h2 {
  font-size: 18px;
  font-weight: 600;
  color: var(--text);
}

/* =============================================
   Buttons
   ============================================= */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 14px 24px;
  border: none;
  border-radius: var(--radius);
  font-size: 17px;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.15s, transform 0.1s;
  width: 100%;
  min-height: 52px;
  text-align: center;
}

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

.btn-primary {
  background: var(--primary);
  color: #fff;
}
.btn-primary:hover { background: var(--primary-dark); }

.btn-success {
  background: var(--success);
  color: #fff;
}
.btn-success:hover { background: var(--success-dark); }

.btn-secondary {
  background: #e8eaed;
  color: var(--text);
}
.btn-secondary:hover { background: #dadce0; }

.btn-danger {
  background: var(--danger);
  color: #fff;
}

.btn-large {
  padding: 20px 24px;
  font-size: 19px;
  min-height: 68px;
  border-radius: 16px;
}

.btn-icon {
  background: none;
  border: none;
  color: var(--primary);
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
  padding: 8px 4px;
  white-space: nowrap;
}

.btn-delete {
  background: none;
  border: none;
  color: var(--danger);
  font-size: 20px;
  font-weight: bold;
  cursor: pointer;
  padding: 8px 12px;
  border-radius: 50%;
  flex-shrink: 0;
  min-width: 44px;
  min-height: 44px;
  display: flex;
  align-items: center;
  justify-content: center;
}
.btn-delete:active { background: #fce8e6; }

/* =============================================
   Login Screen
   ============================================= */
#screen-login {
  justify-content: center;
  align-items: center;
  background: linear-gradient(160deg, #1a73e8 0%, #0d47a1 100%);
}

.login-container {
  width: 100%;
  max-width: 360px;
  padding: 32px 24px;
}

.login-logo {
  text-align: center;
  margin-bottom: 40px;
  color: #fff;
}

.login-logo svg {
  width: 72px;
  height: 72px;
  margin-bottom: 12px;
}

.login-logo h1 {
  font-size: 26px;
  font-weight: 700;
  color: #fff;
  line-height: 1.2;
}

.login-form {
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.pin-input {
  padding: 18px 20px;
  font-size: 28px;
  text-align: center;
  border: 2px solid rgba(255,255,255,0.5);
  border-radius: var(--radius);
  background: rgba(255,255,255,0.15);
  color: #fff;
  outline: none;
  letter-spacing: 8px;
  width: 100%;
  caret-color: #fff;
}
.pin-input::placeholder {
  color: rgba(255,255,255,0.6);
  letter-spacing: 0;
  font-size: 16px;
}
.pin-input:focus {
  border-color: #fff;
  background: rgba(255,255,255,0.2);
}

.error-msg {
  color: #ffcdd2;
  font-size: 14px;
  text-align: center;
  padding: 8px;
  border-radius: var(--radius-sm);
  background: rgba(234,67,53,0.3);
}

.error-msg.hidden { display: none; }

@keyframes shake {
  0%, 100% { transform: translateX(0); }
  20% { transform: translateX(-8px); }
  40% { transform: translateX(8px); }
  60% { transform: translateX(-6px); }
  80% { transform: translateX(6px); }
}
.shake { animation: shake 0.4s ease; }

/* =============================================
   Home Screen
   ============================================= */
.home-container {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  flex: 1;
  gap: 20px;
  padding: 32px 24px;
}

.home-container h2 {
  font-size: 22px;
  color: var(--text-secondary);
  margin-bottom: 8px;
}

.user-name {
  font-weight: 600;
  font-size: 17px;
  color: var(--text);
}

/* =============================================
   Scanner Screen
   ============================================= */
#screen-scanner {
  background: #000;
}

.scanner-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 8px 16px;
  height: 50px;
  background: rgba(0,0,0,0.8);
  flex-shrink: 0;
  padding-top: max(8px, env(safe-area-inset-top));
}

.scanner-header .btn-icon {
  color: #fff;
}

.reception-info {
  font-size: 14px;
  color: rgba(255,255,255,0.8);
}

/* Camera zone: ~60% of remaining height */
.scanner-camera-zone {
  position: relative;
  flex: 6;
  min-height: 0;
  overflow: hidden;
  background: #000;
}

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

/* Override html5-qrcode default styles */
#qr-reader > * {
  border: none !important;
}
#qr-reader video {
  width: 100% !important;
  height: 100% !important;
  object-fit: cover !important;
}
#qr-reader img[alt="Info icon"] { display: none !important; }
#qr-reader__dashboard { display: none !important; }

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

.scan-frame {
  width: 220px;
  height: 220px;
  position: relative;
}

.scan-frame::before,
.scan-frame::after,
.scan-frame > span::before,
.scan-frame > span::after {
  content: '';
  position: absolute;
  width: 28px;
  height: 28px;
  border-color: #fff;
  border-style: solid;
}

.scan-frame::before { top: 0; left: 0; border-width: 3px 0 0 3px; }
.scan-frame::after  { top: 0; right: 0; border-width: 3px 3px 0 0; }
.scan-frame > span::before { bottom: 0; left: 0; border-width: 0 0 3px 3px; }
.scan-frame > span::after  { bottom: 0; right: 0; border-width: 0 3px 3px 0; }

.scan-hint {
  position: absolute;
  bottom: 72px;
  left: 0; right: 0;
  text-align: center;
  color: rgba(255,255,255,0.85);
  font-size: 14px;
  pointer-events: none;
  text-shadow: 0 1px 3px rgba(0,0,0,0.5);
}

.camera-controls {
  position: absolute;
  bottom: 12px;
  right: 16px;
  display: flex;
  gap: 12px;
}

.btn-icon-circle {
  background: rgba(0,0,0,0.5);
  border: 1px solid rgba(255,255,255,0.3);
  color: #fff;
  font-size: 20px;
  width: 48px;
  height: 48px;
  border-radius: 50%;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
}
.btn-icon-circle.active {
  background: rgba(255,220,0,0.4);
  border-color: #ffd700;
}

/* Results zone: ~40% */
.scanner-results-zone {
  flex: 4;
  min-height: 0;
  display: flex;
  flex-direction: column;
  background: var(--bg);
  border-top: 1px solid var(--border);
}

/* Last scanned card */
.last-scan-card {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 16px;
  background: #e8f0fe;
  border-bottom: 1px solid #c5d8fb;
  flex-shrink: 0;
}

.last-scan-card.hidden { display: none; }

.last-scan-info {
  flex: 1;
  min-width: 0;
}

.last-scan-name {
  font-weight: 600;
  font-size: 15px;
  color: var(--primary-dark);
  display: block;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.last-scan-meta {
  font-size: 12px;
  color: var(--text-secondary);
  margin-top: 2px;
}

/* Summary table */
.summary-container {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
}

.summary-table {
  padding: 4px 0;
}

.summary-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 16px;
  border-bottom: 1px solid var(--border);
}

.summary-row:last-child { border-bottom: none; }

.summary-product-name {
  flex: 1;
  font-size: 16px;
  color: var(--text);
  padding-right: 12px;
  line-height: 1.3;
}

.summary-count {
  font-size: 17px;
  font-weight: 700;
  color: var(--primary);
  white-space: nowrap;
}

.summary-empty {
  padding: 24px 16px;
  text-align: center;
  color: var(--text-secondary);
  font-size: 15px;
}

.summary-total-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 12px 16px;
  border-top: 2px solid var(--border);
  background: var(--surface);
  font-size: 17px;
  font-weight: 700;
  color: var(--text);
  flex-shrink: 0;
}

/* Scanner action buttons */
.scanner-actions {
  display: flex;
  gap: 10px;
  padding: 10px 16px;
  padding-bottom: max(10px, env(safe-area-inset-bottom));
  background: var(--surface);
  border-top: 1px solid var(--border);
  flex-shrink: 0;
}

.scanner-actions .btn {
  flex: 1;
  padding: 12px 8px;
  font-size: 14px;
  min-height: 48px;
}

/* =============================================
   History Screen
   ============================================= */
#screen-history {
  background: var(--bg);
}

.history-list {
  flex: 1;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
}

.history-item {
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}

.history-item-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px;
  cursor: pointer;
  user-select: none;
  -webkit-user-select: none;
}

.history-item-header:active { background: var(--bg); }

.history-date {
  font-size: 16px;
  font-weight: 600;
  color: var(--text);
}

.history-meta {
  font-size: 14px;
  color: var(--text-secondary);
  margin-top: 2px;
}

.history-badge {
  display: flex;
  align-items: center;
  gap: 8px;
}

.badge {
  padding: 4px 10px;
  border-radius: 20px;
  font-size: 13px;
  font-weight: 600;
}
.badge-completed { background: #e6f4ea; color: var(--success-dark); }
.badge-active    { background: #e8f0fe; color: var(--primary-dark); }

.history-chevron {
  color: var(--text-secondary);
  font-size: 18px;
  transition: transform 0.2s;
}

.history-item.open .history-chevron { transform: rotate(180deg); }

.history-item-body {
  display: none;
  padding: 0 16px 16px;
  background: var(--bg);
}

.history-item.open .history-item-body { display: block; }

.history-summary-row {
  display: flex;
  justify-content: space-between;
  padding: 8px 0;
  border-bottom: 1px solid var(--border);
  font-size: 15px;
}
.history-summary-row:last-child { border-bottom: none; }

.history-empty {
  text-align: center;
  padding: 40px 24px;
  color: var(--text-secondary);
  font-size: 16px;
}

/* =============================================
   Modal
   ============================================= */
.modal {
  position: fixed;
  inset: 0;
  z-index: 100;
  display: flex;
  align-items: flex-end;
}

.modal.hidden { display: none; }

.modal-overlay {
  position: absolute;
  inset: 0;
  background: rgba(0,0,0,0.5);
}

.modal-content {
  position: relative;
  background: var(--surface);
  border-radius: 20px 20px 0 0;
  width: 100%;
  max-height: 80vh;
  display: flex;
  flex-direction: column;
  padding-bottom: max(16px, env(safe-area-inset-bottom));
}

.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px 20px 12px;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}

.modal-header h3 {
  font-size: 18px;
  font-weight: 700;
}

.search-input {
  margin: 12px 16px;
  padding: 12px 16px;
  font-size: 16px;
  border: 1.5px solid var(--border);
  border-radius: var(--radius-sm);
  outline: none;
  flex-shrink: 0;
}
.search-input:focus { border-color: var(--primary); }

.products-list {
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  flex: 1;
}

.product-item {
  padding: 14px 20px;
  border-bottom: 1px solid var(--border);
  cursor: pointer;
  display: flex;
  justify-content: space-between;
  align-items: center;
  min-height: 56px;
}
.product-item:active { background: var(--bg); }

.product-item-name {
  font-size: 16px;
  font-weight: 500;
  flex: 1;
  padding-right: 8px;
}

.product-item-gtin {
  font-size: 12px;
  color: var(--text-secondary);
  white-space: nowrap;
}

/* Complete modal */
.complete-summary {
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  max-height: 35vh;
  padding: 0 16px;
  flex: 1;
}

.complete-summary-row {
  display: flex;
  justify-content: space-between;
  padding: 10px 0;
  border-bottom: 1px solid var(--border);
  font-size: 16px;
}
.complete-summary-row:last-child { border-bottom: none; }

.complete-total {
  display: flex;
  justify-content: space-between;
  padding: 12px 16px;
  font-size: 17px;
  font-weight: 700;
  border-top: 2px solid var(--border);
  flex-shrink: 0;
}

.confirm-text {
  padding: 12px 16px;
  font-size: 15px;
  color: var(--text-secondary);
  flex-shrink: 0;
}

.modal-actions {
  display: flex;
  gap: 12px;
  padding: 8px 16px;
  flex-shrink: 0;
}
.modal-actions .btn { flex: 1; }

/* =============================================
   Toast
   ============================================= */
.toast {
  position: fixed;
  bottom: 80px;
  left: 50%;
  transform: translateX(-50%);
  background: rgba(0,0,0,0.8);
  color: #fff;
  padding: 12px 20px;
  border-radius: 24px;
  font-size: 15px;
  max-width: 320px;
  text-align: center;
  z-index: 200;
  pointer-events: none;
  white-space: pre-wrap;
}

.toast.hidden { display: none; }

.toast.error {
  background: rgba(234,67,53,0.9);
}

.toast.success {
  background: rgba(52,168,83,0.9);
}

/* =============================================
   Loading / Utility
   ============================================= */
.loading {
  text-align: center;
  padding: 32px 16px;
  color: var(--text-secondary);
  font-size: 16px;
}

.hidden { display: none !important; }
