/* ============================================================
   ScanEat – Barcode Food Scanner for Health & Environment
   ============================================================ */

:root {
  --green: #27ae60;
  --green-bg: #e8f8ef;
  --green-border: #a3e4bc;

  --yellow: #f39c12;
  --yellow-bg: #fef9e7;
  --yellow-border: #f9e79f;

  --red: #e74c3c;
  --red-bg: #fdedec;
  --red-border: #f5b7b1;

  --gray-50: #fafafa;
  --gray-100: #f5f5f5;
  --gray-200: #e5e5e5;
  --gray-300: #d4d4d4;
  --gray-400: #a3a3a3;
  --gray-500: #737373;
  --gray-600: #525252;
  --gray-700: #404040;
  --gray-800: #262626;
  --gray-900: #171717;

  --blue: #3498db;
  --blue-dark: #2176ae;

  --radius: 12px;
  --radius-sm: 8px;
  --shadow: 0 2px 12px rgba(0,0,0,.08);
  --shadow-lg: 0 8px 32px rgba(0,0,0,.12);

  --font: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
}

/* Reset & Base */
*, *::before, *::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

body {
  font-family: var(--font);
  background: var(--gray-50);
  color: var(--gray-800);
  line-height: 1.6;
  padding: 0 0 80px;
  min-height: 100vh;
}

/* Header */
.app-header {
  background: linear-gradient(135deg, var(--blue), #2ecc71);
  color: #fff;
  padding: 20px 16px 18px;
  text-align: center;
  position: sticky;
  top: 0;
  z-index: 100;
  box-shadow: 0 2px 12px rgba(0,0,0,.15);
}
.app-header h1 {
  font-size: 1.5rem;
  font-weight: 700;
  letter-spacing: -.02em;
}
.app-header p {
  font-size: .82rem;
  opacity: .85;
  margin-top: 2px;
}

/* Container */
.container {
  max-width: 600px;
  margin: 0 auto;
  padding: 16px;
}

/* Cards */
.card {
  background: #fff;
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 20px;
  margin-bottom: 20px;
}

.card-title {
  font-size: 1rem;
  font-weight: 600;
  color: var(--gray-700);
  margin-bottom: 12px;
  display: flex;
  align-items: center;
  gap: 6px;
}

/* Scanner Section */
#reader {
  width: 100%;
  border-radius: var(--radius-sm);
  overflow: hidden;
  background: var(--gray-900);
  min-height: 220px;
}
#reader video {
  border-radius: var(--radius-sm);
  width: 100%;
  display: block;
}
#reader__scan_btn {
  display: none !important;
}
#reader button {
  background: var(--blue) !important;
  color: #fff !important;
  border: none !important;
  border-radius: var(--radius-sm) !important;
  padding: 8px 16px !important;
  font-size: .85rem !important;
  cursor: pointer;
}
#reader button:hover {
  background: var(--blue-dark) !important;
}

.scanner-controls {
  display: flex;
  gap: 8px;
  margin-top: 10px;
  flex-wrap: wrap;
}

.btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 10px 20px;
  border: none;
  border-radius: var(--radius-sm);
  font-size: .9rem;
  font-weight: 500;
  cursor: pointer;
  transition: background .2s, transform .1s;
}
.btn:active {
  transform: scale(.97);
}
.btn-primary {
  background: var(--blue);
  color: #fff;
}
.btn-primary:hover {
  background: var(--blue-dark);
}
.btn-secondary {
  background: var(--gray-200);
  color: var(--gray-700);
}
.btn-secondary:hover {
  background: var(--gray-300);
}
.btn-danger {
  background: var(--red);
  color: #fff;
}
.btn-danger:hover {
  background: #c0392b;
}
.btn:disabled {
  opacity: .5;
  cursor: not-allowed;
}

/* Manual Input */
.manual-input {
  display: flex;
  gap: 8px;
  margin-top: 10px;
}
.manual-input input {
  flex: 1;
  padding: 10px 14px;
  border: 2px solid var(--gray-300);
  border-radius: var(--radius-sm);
  font-size: 1rem;
  outline: none;
  transition: border-color .2s;
}
.manual-input input:focus {
  border-color: var(--blue);
}
.manual-input button {
  padding: 10px 18px;
}

/* Scanner hints */
.scanner-hint {
  text-align: center;
  font-size: .82rem;
  color: var(--gray-500);
  margin-top: 8px;
}

/* ==================== RESULTS ==================== */
#resultSection {
  display: none;
}
#resultSection.visible {
  display: block;
  animation: slideUp .35s ease;
}

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

.product-header {
  display: flex;
  gap: 16px;
  align-items: flex-start;
  margin-bottom: 16px;
}
.product-image {
  width: 90px;
  height: 90px;
  object-fit: contain;
  border-radius: var(--radius-sm);
  background: var(--gray-100);
  flex-shrink: 0;
}
.product-image-placeholder {
  width: 90px;
  height: 90px;
  border-radius: var(--radius-sm);
  background: var(--gray-200);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 2rem;
  color: var(--gray-400);
  flex-shrink: 0;
}
.product-info {
  flex: 1;
  min-width: 0;
}
.product-name {
  font-size: 1.15rem;
  font-weight: 600;
  line-height: 1.3;
  margin-bottom: 4px;
}
.product-barcode {
  font-size: .78rem;
  color: var(--gray-400);
  word-break: break-all;
}

/* Rating Badges */
.ratings-row {
  display: flex;
  gap: 12px;
  margin: 14px 0;
}
.rating-badge {
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 10px 18px;
  border-radius: var(--radius-sm);
  font-weight: 600;
  min-width: 90px;
  text-align: center;
}
.rating-badge .label {
  font-size: .7rem;
  text-transform: uppercase;
  letter-spacing: .04em;
  opacity: .8;
  margin-bottom: 2px;
}
.rating-badge .value {
  font-size: 1.1rem;
}
.rating-green {
  background: var(--green-bg);
  color: var(--green);
  border: 1px solid var(--green-border);
}
.rating-yellow {
  background: var(--yellow-bg);
  color: var(--yellow);
  border: 1px solid var(--yellow-border);
}
.rating-red {
  background: var(--red-bg);
  color: var(--red);
  border: 1px solid var(--red-border);
}

/* Nutri-Score visual */
.nutri-score {
  display: inline-flex;
  gap: 4px;
  margin: 8px 0;
}
.nutri-letter {
  width: 28px;
  height: 28px;
  border-radius: 4px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: .75rem;
  font-weight: 700;
  color: #fff;
  background: var(--gray-300);
}
.nutri-letter.active {
  transform: scale(1.15);
  box-shadow: 0 2px 8px rgba(0,0,0,.2);
}
.nutri-a { background: #1a9c4a; }
.nutri-b { background: #58b947; }
.nutri-c { background: #fecb02; color: #333; }
.nutri-d { background: #f58220; }
.nutri-e { background: #e03a20; }

/* Ingredients */
.ingredients-section {
  margin-top: 12px;
}
.ingredients-title {
  font-weight: 600;
  font-size: .9rem;
  color: var(--gray-600);
  margin-bottom: 8px;
}
.ingredients-text {
  font-size: .85rem;
  color: var(--gray-600);
  line-height: 1.5;
  background: var(--gray-100);
  padding: 10px 14px;
  border-radius: var(--radius-sm);
  margin-bottom: 10px;
}

.ingredient-item {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px 0;
  border-bottom: 1px solid var(--gray-100);
  font-size: .85rem;
}
.ingredient-item:last-child {
  border-bottom: none;
}
.ingredient-dot {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  flex-shrink: 0;
}
.ingredient-name {
  font-weight: 500;
  flex: 1;
}
.ingredient-reason {
  font-size: .75rem;
  color: var(--gray-500);
  max-width: 220px;
  text-align: right;
}

/* Nutrition Facts mini */
.nutrition-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 8px;
  margin: 10px 0;
}
.nutri-item {
  background: var(--gray-100);
  padding: 8px 12px;
  border-radius: var(--radius-sm);
  font-size: .82rem;
}
.nutri-item .nutri-label {
  color: var(--gray-500);
  font-size: .72rem;
}
.nutri-item .nutri-value {
  font-weight: 600;
  font-size: .95rem;
}

/* Ingredients "good for" explanations */
.reasons-box {
  background: var(--gray-50);
  border-radius: var(--radius-sm);
  padding: 14px;
  margin: 10px 0;
  font-size: .85rem;
  line-height: 1.5;
}
.reasons-box strong {
  color: var(--gray-700);
}
.reason-green { color: var(--green); }
.reason-yellow { color: var(--yellow); }
.reason-red { color: var(--red); }

/* NOVA group info */
.nova-info {
  margin: 8px 0;
  padding: 10px 14px;
  background: var(--gray-100);
  border-radius: var(--radius-sm);
  font-size: .85rem;
}
.nova-info strong {
  display: block;
  margin-bottom: 2px;
}

/* ==================== HISTORY ==================== */
.history-list {
  max-height: 320px;
  overflow-y: auto;
}
.history-item {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 0;
  border-bottom: 1px solid var(--gray-100);
  cursor: pointer;
  transition: background .15s;
}
.history-item:hover {
  background: var(--gray-50);
  margin: 0 -4px;
  padding: 10px 4px;
  border-radius: var(--radius-sm);
}
.history-item:last-child {
  border-bottom: none;
}
.history-thumb {
  width: 40px;
  height: 40px;
  object-fit: contain;
  background: var(--gray-100);
  border-radius: 6px;
  flex-shrink: 0;
}
.history-info {
  flex: 1;
  min-width: 0;
}
.history-name {
  font-size: .88rem;
  font-weight: 500;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.history-barcode {
  font-size: .72rem;
  color: var(--gray-400);
}
.history-ratings {
  display: flex;
  gap: 4px;
  flex-shrink: 0;
}
.history-badge {
  width: 28px;
  height: 24px;
  border-radius: 4px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: .65rem;
  font-weight: 700;
  color: #fff;
}
.empty-history {
  text-align: center;
  color: var(--gray-400);
  padding: 20px 0;
  font-size: .85rem;
}

/* ==================== SEARCH RESULTS ==================== */
.search-results {
  max-height: 380px;
  overflow-y: auto;
  margin-top: 8px;
}

.search-loading {
  text-align: center;
  padding: 20px 0;
}

.search-meta {
  font-size: .78rem;
  color: var(--gray-400);
  margin-bottom: 6px;
  padding: 4px 0;
}

.search-result-item {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 0;
  border-bottom: 1px solid var(--gray-100);
  cursor: pointer;
  transition: background .15s;
}
.search-result-item:hover {
  background: var(--gray-50);
  margin: 0 -4px;
  padding: 10px 4px;
  border-radius: var(--radius-sm);
}
.search-result-item:last-child {
  border-bottom: none;
}

.search-thumb {
  width: 44px;
  height: 44px;
  object-fit: contain;
  background: var(--gray-100);
  border-radius: 6px;
  flex-shrink: 0;
}
.search-thumb-placeholder {
  width: 44px;
  height: 44px;
  background: var(--gray-100);
  border-radius: 6px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1.2rem;
  flex-shrink: 0;
}

.search-result-info {
  flex: 1;
  min-width: 0;
}

.search-result-name {
  font-size: .88rem;
  font-weight: 500;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.search-result-barcode {
  font-size: .72rem;
  color: var(--gray-400);
  display: flex;
  align-items: center;
  gap: 6px;
}

.search-nutri {
  display: inline-block;
  width: 18px;
  height: 18px;
  border-radius: 3px;
  font-size: .6rem;
  font-weight: 700;
  text-align: center;
  line-height: 18px;
  color: #fff;
  background: var(--gray-300);
}
.search-nutri.best { background: #1a9c4a; }
.search-nutri.good { background: #58b947; }
.search-nutri.ok   { background: #fecb02; color: #333; }
.search-nutri.bad  { background: #f58220; }
.search-nutri.worst{ background: #e03a20; }

/* Autocomplete dropdown */
.autocomplete-dropdown {
  position:absolute;top:0;left:0;right:0;z-index:50;
  background:#fff;border:1px solid var(--gray-300);
  border-radius:0 0 var(--radius-sm) var(--radius-sm);
  box-shadow:0 8px 24px rgba(0,0,0,.12);
  max-height:280px;overflow-y:auto;display:none
}
.autocomplete-dropdown.active{display:block}
.ac-item{
  display:flex;align-items:center;gap:10px;padding:8px 12px;
  cursor:pointer;transition:background .1s;border-bottom:1px solid var(--gray-100)
}
.ac-item:last-child{border-bottom:none}
.ac-item:hover,.ac-item.highlight{background:var(--gray-50)}
.ac-img{width:28px;height:28px;object-fit:contain;border-radius:4px;background:var(--gray-100);flex-shrink:0}
.ac-img-pl{width:28px;height:28px;border-radius:4px;background:var(--gray-100);display:flex;align-items:center;justify-content:center;font-size:.9rem;flex-shrink:0}
.ac-info{flex:1;min-width:0}
.ac-name{font-size:.82rem;font-weight:500;color:var(--gray-700);white-space:nowrap;overflow:hidden;text-overflow:ellipsis}
.ac-bc{font-size:.68rem;color:var(--gray-400)}
.ac-badge{font-size:.6rem;font-weight:600;padding:1px 6px;border-radius:8px;flex-shrink:0}
.ac-loading{text-align:center;padding:12px;font-size:.78rem;color:var(--gray-400)}

/* ==================== SCANNER OVERLAY ==================== */
.scan-overlay {
  position:absolute;inset:0;z-index:5;
  pointer-events:none;
}
.scan-frame {
  position:absolute;top:50%;left:10%;right:10%;
  height:120px;transform:translateY(-60px);
  border:2px solid rgba(255,50,50,.5);
  border-radius:6px;
  animation: scanPulse 2s ease-in-out infinite;
}
@keyframes scanPulse {
  0%,100%{border-color:rgba(255,50,50,.4)}
  50%{border-color:rgba(255,50,50,.8)}
}
.scan-line {
  position:absolute;left:-2px;right:-2px;height:2px;
  background:linear-gradient(90deg,transparent,#ff3333,transparent);
  box-shadow:0 0 8px #ff3333;
  animation: scanMove 1.8s ease-in-out infinite;
}
@keyframes scanMove {
  0%{top:2px}
  50%{top:calc(100% - 2px)}
  100%{top:2px}
}
.scan-line.ok{background:linear-gradient(90deg,transparent,#27ae60,transparent);box-shadow:0 0 8px #27ae60}
.scan-line.err{background:linear-gradient(90deg,transparent,#e74c3c,transparent);box-shadow:0 0 8px #e74c3c}

/* Corner brackets */
.sc-tl,.sc-tr,.sc-bl,.sc-br{
  position:absolute;width:20px;height:20px;border-color:#ff4444;border-style:solid
}
.sc-tl{top:-2px;left:-2px;border-width:3px 0 0 3px;border-radius:4px 0 0 0}
.sc-tr{top:-2px;right:-2px;border-width:3px 3px 0 0;border-radius:0 4px 0 0}
.sc-bl{bottom:-2px;left:-2px;border-width:0 0 3px 3px;border-radius:0 0 0 4px}
.sc-br{bottom:-2px;right:-2px;border-width:0 3px 3px 0;border-radius:0 0 4px 0}

.scan-status{
  position:absolute;bottom:8px;left:0;right:0;text-align:center;
  font-size:.8rem;color:rgba(255,255,255,.8);
  text-shadow:0 1px 4px rgba(0,0,0,.6);
  pointer-events:none
}
.scan-status.good{color:#27ae60}
.scan-status.bad{color:#e74c3c}

/* Scan flash animations */
@keyframes flashGreen {
  0%{box-shadow:inset 0 0 0 0 rgba(39,174,96,0)}
  30%{box-shadow:inset 0 0 60px 20px rgba(39,174,96,.5)}
  100%{box-shadow:inset 0 0 0 0 rgba(39,174,96,0)}
}
@keyframes flashRed {
  0%{box-shadow:inset 0 0 0 0 rgba(231,76,60,0)}
  30%{box-shadow:inset 0 0 60px 20px rgba(231,76,60,.5)}
  100%{box-shadow:inset 0 0 0 0 rgba(231,76,60,0)}
}
.scan-flash-green{animation:flashGreen .5s ease}
.scan-flash-red{animation:flashRed .5s ease}


/* Inline search inside product result */
.product-search-inline {
  margin: 14px 0 8px;
}
.ps-input-wrap {
  display: flex;
  gap: 6px;
}
.ps-input {
  flex: 1;
  padding: 8px 12px;
  border: 2px solid var(--gray-300);
  border-radius: var(--radius-sm);
  font-size: .88rem;
  outline: none;
  transition: border-color .2s;
  font-family: var(--font);
}
.ps-input:focus {
  border-color: var(--blue);
}
.ps-btn {
  padding: 8px 14px;
  border: none;
  border-radius: var(--radius-sm);
  background: var(--blue);
  color: #fff;
  font-size: .82rem;
  font-weight: 500;
  cursor: pointer;
  white-space: nowrap;
  transition: background .2s;
}
.ps-btn:hover {
  background: var(--blue-dark);
}
.ps-btn:active {
  transform: scale(.97);
}

/* Loading */
.loading-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(255,255,255,.85);
  z-index: 200;
  justify-content: center;
  align-items: center;
  flex-direction: column;
  gap: 12px;
}
.loading-overlay.active {
  display: flex;
}
.spinner {
  width: 40px;
  height: 40px;
  border: 4px solid var(--gray-300);
  border-top-color: var(--blue);
  border-radius: 50%;
  animation: spin .7s linear infinite;
}
@keyframes spin {
  to { transform: rotate(360deg); }
}
.loading-text {
  font-size: .9rem;
  color: var(--gray-600);
}

/* Error */
.error-msg {
  background: var(--red-bg);
  color: var(--red);
  padding: 10px 14px;
  border-radius: var(--radius-sm);
  font-size: .85rem;
  margin: 8px 0;
  display: none;
}
.error-msg.visible {
  display: block;
}

/* Utilities */
.hidden { display: none !important; }
.text-center { text-align: center; }
.mt-8 { margin-top: 8px; }
.mt-12 { margin-top: 12px; }
.mt-16 { margin-top: 16px; }

/* Responsive */
@media (max-width: 480px) {
  .product-header {
    flex-direction: column;
    align-items: center;
    text-align: center;
  }
  .ratings-row {
    justify-content: center;
  }
  .nutrition-grid {
    grid-template-columns: 1fr;
  }
  .manual-input {
    flex-direction: column;
  }
  .manual-input button {
    width: 100%;
  }
}

/* Scrollbar */
::-webkit-scrollbar {
  width: 5px;
}
::-webkit-scrollbar-track {
  background: transparent;
}
::-webkit-scrollbar-thumb {
  background: var(--gray-300);
  border-radius: 10px;
}
