/* ===== CSS Variables & Reset ===== */
:root {
  --bg-primary: #0f172a;
  --bg-secondary: #1e293b;
  --bg-card: #1e293b;
  --bg-input: #334155;
  --text-primary: #f1f5f9;
  --text-secondary: #94a3b8;
  --text-muted: #64748b;
  --accent: #3b82f6;
  --accent-hover: #2563eb;
  --success: #22c55e;
  --warning: #f59e0b;
  --danger: #ef4444;
  --border: #334155;
  --radius: 12px;
  --radius-sm: 8px;
  --shadow: 0 4px 24px rgba(0, 0, 0, 0.3);
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-left: env(safe-area-inset-left, 0px);
  --safe-right: env(safe-area-inset-right, 0px);
}

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

html {
  font-size: 16px;
  -webkit-text-size-adjust: 100%;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, 'SF Pro Text', 'Segoe UI', Roboto, Oxygen, Ubuntu, sans-serif;
  background: var(--bg-primary);
  color: var(--text-primary);
  min-height: 100vh;
  min-height: 100dvh;
  overflow-x: hidden;
  -webkit-font-smoothing: antialiased;
  padding-top: var(--safe-top);
  padding-bottom: var(--safe-bottom);
  padding-left: var(--safe-left);
  padding-right: var(--safe-right);
}

/* ===== App Layout ===== */
#app {
  display: flex;
  flex-direction: column;
  min-height: 100vh;
  min-height: 100dvh;
  max-width: 480px;
  margin: 0 auto;
}

/* ===== Header ===== */
.app-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 16px;
  background: var(--bg-secondary);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 100;
}

.app-header h1 {
  font-size: 1.15rem;
  font-weight: 700;
  display: flex;
  align-items: center;
  gap: 8px;
}

.app-header h1 .icon {
  font-size: 1.3rem;
}

.header-actions {
  display: flex;
  gap: 4px;
}

/* ===== Navigation Tabs ===== */
.nav-tabs {
  display: flex;
  background: var(--bg-secondary);
  border-bottom: 1px solid var(--border);
  padding: 0 16px;
}

.nav-tab {
  flex: 1;
  padding: 10px 8px;
  background: none;
  border: none;
  border-bottom: 2px solid transparent;
  color: var(--text-muted);
  font-size: 0.85rem;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.2s;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
}

.nav-tab.active {
  color: var(--accent);
  border-bottom-color: var(--accent);
}

.nav-tab:hover {
  color: var(--text-secondary);
}

.nav-tab .badge {
  background: var(--accent);
  color: white;
  font-size: 0.7rem;
  padding: 1px 6px;
  border-radius: 10px;
  min-width: 18px;
  text-align: center;
}

/* ===== Main Content ===== */
.main-content {
  flex: 1;
  padding: 16px;
  overflow-y: auto;
}

/* ===== Scanner View ===== */
.scanner-container {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 16px;
}

.scanner-viewport {
  width: 100%;
  max-width: 400px;
  aspect-ratio: 4/3;
  border-radius: var(--radius);
  overflow: hidden;
  position: relative;
  background: #000;
}

#scanner-region {
  width: 100%;
  height: 100%;
}

/* Override html5-qrcode styles */
#scanner-region video {
  border-radius: var(--radius);
  object-fit: cover;
}

#scanner-region img[alt="Info"] {
  display: none !important;
}

#html5-qrcode-button-camera-permission,
#html5-qrcode-button-camera-start,
#html5-qrcode-button-camera-stop {
  display: none !important;
}

#html5-qrcode-anchor-scan-type-change {
  display: none !important;
}

#scanner-region > div:first-child {
  border: none !important;
}

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

.scan-frame {
  width: 70%;
  aspect-ratio: 3/2;
  border: 2px solid rgba(59, 130, 246, 0.6);
  border-radius: 8px;
  position: relative;
}

.scan-frame::before {
  content: '';
  position: absolute;
  left: -2px;
  right: -2px;
  top: 50%;
  height: 2px;
  background: var(--danger);
  opacity: 0.7;
  animation: scanLine 2s ease-in-out infinite;
}

@keyframes scanLine {
  0%, 100% { top: 20%; }
  50% { top: 80%; }
}

.scan-frame .corner {
  position: absolute;
  width: 20px;
  height: 20px;
  border-color: var(--accent);
  border-style: solid;
  border-width: 0;
}

.scan-frame .corner.tl { top: -2px; left: -2px; border-top-width: 3px; border-left-width: 3px; border-radius: 4px 0 0 0; }
.scan-frame .corner.tr { top: -2px; right: -2px; border-top-width: 3px; border-right-width: 3px; border-radius: 0 4px 0 0; }
.scan-frame .corner.bl { bottom: -2px; left: -2px; border-bottom-width: 3px; border-left-width: 3px; border-radius: 0 0 0 4px; }
.scan-frame .corner.br { bottom: -2px; right: -2px; border-bottom-width: 3px; border-right-width: 3px; border-radius: 0 0 4px 0; }

.scanner-controls {
  display: flex;
  gap: 12px;
  width: 100%;
  max-width: 400px;
}

.scanner-hint {
  text-align: center;
  color: var(--text-muted);
  font-size: 0.85rem;
  margin-top: -4px;
}

/* ===== Buttons ===== */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 12px 20px;
  border: none;
  border-radius: var(--radius-sm);
  font-size: 0.95rem;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.2s;
  -webkit-tap-highlight-color: transparent;
  min-height: 44px;
}

.btn-primary {
  background: var(--accent);
  color: white;
  flex: 1;
}

.btn-primary:hover { background: var(--accent-hover); }
.btn-primary:active { transform: scale(0.97); }

.btn-secondary {
  background: var(--bg-input);
  color: var(--text-primary);
}

.btn-secondary:hover { background: var(--border); }

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

.btn-icon {
  width: 44px;
  height: 44px;
  padding: 0;
  border-radius: var(--radius-sm);
  background: var(--bg-input);
  border: none;
  color: var(--text-primary);
  font-size: 1.2rem;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 0.2s;
  -webkit-tap-highlight-color: transparent;
}

.btn-icon:hover { background: var(--border); }

.btn-sm {
  padding: 6px 12px;
  font-size: 0.8rem;
  min-height: 32px;
}

/* ===== Manual Entry ===== */
.manual-entry {
  width: 100%;
  max-width: 400px;
  display: flex;
  gap: 8px;
}

.manual-entry input {
  flex: 1;
  padding: 12px 14px;
  background: var(--bg-input);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  color: var(--text-primary);
  font-size: 1rem;
  outline: none;
  transition: border-color 0.2s;
}

.manual-entry input:focus {
  border-color: var(--accent);
}

.manual-entry input::placeholder {
  color: var(--text-muted);
}

.divider {
  display: flex;
  align-items: center;
  gap: 12px;
  width: 100%;
  max-width: 400px;
  color: var(--text-muted);
  font-size: 0.8rem;
}

.divider::before, .divider::after {
  content: '';
  flex: 1;
  height: 1px;
  background: var(--border);
}

/* ===== Product Result Card ===== */
.result-card {
  background: var(--bg-card);
  border-radius: var(--radius);
  overflow: hidden;
  box-shadow: var(--shadow);
  animation: slideUp 0.3s ease;
}

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

.result-image-container {
  width: 100%;
  height: 220px;
  background: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
}

.result-image-container img {
  max-width: 100%;
  max-height: 100%;
  object-fit: contain;
}

.result-image-placeholder {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  color: var(--text-muted);
  font-size: 3rem;
}

.result-image-placeholder span {
  font-size: 0.85rem;
}

.result-body {
  padding: 16px;
}

.result-body h2 {
  font-size: 1.2rem;
  font-weight: 700;
  margin-bottom: 4px;
  line-height: 1.3;
}

.result-brand {
  color: var(--accent);
  font-size: 0.9rem;
  font-weight: 600;
  margin-bottom: 12px;
}

.result-barcode {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  background: var(--bg-input);
  padding: 4px 10px;
  border-radius: 20px;
  font-size: 0.78rem;
  color: var(--text-secondary);
  font-family: 'SF Mono', 'Fira Code', monospace;
  margin-bottom: 12px;
}

.result-details {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.detail-row {
  display: flex;
  gap: 8px;
}

.detail-label {
  color: var(--text-muted);
  font-size: 0.82rem;
  min-width: 80px;
  font-weight: 600;
}

.detail-value {
  color: var(--text-secondary);
  font-size: 0.85rem;
  line-height: 1.4;
}

/* Nutrition table */
.nutrition-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 8px;
  margin-top: 12px;
  padding-top: 12px;
  border-top: 1px solid var(--border);
}

.nutrition-item {
  background: var(--bg-input);
  padding: 10px;
  border-radius: var(--radius-sm);
  text-align: center;
}

.nutrition-item .value {
  font-size: 1.1rem;
  font-weight: 700;
  color: var(--accent);
}

.nutrition-item .label {
  font-size: 0.72rem;
  color: var(--text-muted);
  margin-top: 2px;
}

.result-actions {
  display: flex;
  gap: 8px;
  padding: 12px 16px;
  border-top: 1px solid var(--border);
}

/* ===== Loading State ===== */
.loading-container {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 16px;
  padding: 40px 0;
}

.spinner {
  width: 48px;
  height: 48px;
  border: 4px solid var(--border);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}

@keyframes spin {
  to { transform: rotate(360deg); }
}

.loading-text {
  color: var(--text-secondary);
  font-size: 0.9rem;
}

/* ===== Error / Not Found ===== */
.error-card, .not-found-card {
  background: var(--bg-card);
  border-radius: var(--radius);
  padding: 32px 20px;
  text-align: center;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 12px;
}

.error-card .error-icon, .not-found-card .nf-icon {
  font-size: 3rem;
}

.error-card h3, .not-found-card h3 {
  font-size: 1.1rem;
}

.error-card p, .not-found-card p {
  color: var(--text-secondary);
  font-size: 0.9rem;
  line-height: 1.5;
}

/* ===== History View ===== */
.history-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.history-item {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px;
  background: var(--bg-card);
  border-radius: var(--radius-sm);
  cursor: pointer;
  transition: background 0.2s;
  -webkit-tap-highlight-color: transparent;
}

.history-item:hover { background: var(--bg-input); }
.history-item:active { transform: scale(0.99); }

.history-thumb {
  width: 48px;
  height: 48px;
  border-radius: 8px;
  background: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
  flex-shrink: 0;
}

.history-thumb img {
  width: 100%;
  height: 100%;
  object-fit: contain;
}

.history-thumb .placeholder {
  font-size: 1.5rem;
}

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

.history-info .name {
  font-size: 0.9rem;
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.history-info .meta {
  font-size: 0.75rem;
  color: var(--text-muted);
  display: flex;
  gap: 8px;
  margin-top: 2px;
}

.history-delete {
  background: none;
  border: none;
  color: var(--text-muted);
  font-size: 1rem;
  cursor: pointer;
  padding: 8px;
  transition: color 0.2s;
}

.history-delete:hover { color: var(--danger); }

.history-empty {
  text-align: center;
  padding: 60px 20px;
  color: var(--text-muted);
}

.history-empty .icon {
  font-size: 3rem;
  margin-bottom: 12px;
}

.history-empty p {
  font-size: 0.9rem;
  line-height: 1.5;
}

.history-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 12px;
}

.history-header h2 {
  font-size: 1rem;
  font-weight: 600;
}

/* ===== Scan Success Flash ===== */
.scan-success-flash {
  position: fixed;
  inset: 0;
  background: rgba(34, 197, 94, 0.15);
  z-index: 200;
  pointer-events: none;
  animation: flashFade 0.5s ease forwards;
}

@keyframes flashFade {
  0% { opacity: 1; }
  100% { opacity: 0; }
}

/* ===== Toast ===== */
.toast {
  position: fixed;
  bottom: calc(20px + var(--safe-bottom));
  left: 50%;
  transform: translateX(-50%);
  background: var(--bg-input);
  color: var(--text-primary);
  padding: 10px 20px;
  border-radius: 20px;
  font-size: 0.85rem;
  font-weight: 500;
  box-shadow: var(--shadow);
  z-index: 300;
  animation: toastIn 0.3s ease, toastOut 0.3s ease 2.2s forwards;
}

@keyframes toastIn {
  from { opacity: 0; transform: translateX(-50%) translateY(20px); }
  to { opacity: 1; transform: translateX(-50%) translateY(0); }
}

@keyframes toastOut {
  from { opacity: 1; transform: translateX(-50%) translateY(0); }
  to { opacity: 0; transform: translateX(-50%) translateY(20px); }
}

/* ===== Responsive ===== */
@media (min-width: 481px) {
  #app {
    border-left: 1px solid var(--border);
    border-right: 1px solid var(--border);
  }
}

/* ===== Accessibility ===== */
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.01ms !important;
    transition-duration: 0.01ms !important;
  }
}

/* ===== Selection ===== */
::selection {
  background: var(--accent);
  color: white;
}
