/* ============================================
   Bolivian Banknote Scanner — style.css
   Premium dark theme with glassmorphism
   ============================================ */

@import url('https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700;800&display=swap');

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

:root {
  --bg-primary: #0a0e17;
  --bg-secondary: #111827;
  --bg-glass: rgba(17, 24, 39, 0.75);
  --text-primary: #f1f5f9;
  --text-secondary: #94a3b8;
  --accent: #6366f1;
  --accent-glow: rgba(99, 102, 241, 0.35);
  --valid-green: #22c55e;
  --valid-glow: rgba(34, 197, 94, 0.4);
  --invalid-red: #ef4444;
  --invalid-glow: rgba(239, 68, 68, 0.4);
  --border-subtle: rgba(148, 163, 184, 0.12);
  --radius: 16px;
  --transition: 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}

html,
body {
  width: 100%;
  height: 100%;
  font-family: 'Inter', system-ui, -apple-system, sans-serif;
  background: var(--bg-primary);
  color: var(--text-primary);
  overflow: hidden;
  -webkit-font-smoothing: antialiased;
}

/* ---- Layout ---- */
#app {
  display: flex;
  flex-direction: column;
  height: 100%;
  width: 100%;
  position: relative;
}

/* ---- Header ---- */
header {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  z-index: 20;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 10px 20px;
  background: linear-gradient(180deg, rgba(10, 14, 23, 0.92) 0%, rgba(10, 14, 23, 0) 100%);
}

header h1 {
  font-size: 0.85rem;
  font-weight: 700;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  color: var(--text-primary);
  display: flex;
  align-items: center;
  gap: 6px;
}

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

/* ---- Video viewport (upper portion) ---- */
#video-container {
  position: relative;
  height: 50vh;
  min-height: 220px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: #000;
  overflow: hidden;
  flex-shrink: 0;
}

#camera-feed {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

#hidden-canvas {
  display: none;
}

/* ---- ROI Overlay ---- */
#roi-overlay {
  position: absolute;
  inset: 0;
  z-index: 10;
  pointer-events: none;
}

.roi-frame {
  position: absolute;
  top: 45%;
  left: 45%;
  transform: translate(-50%, -50%);
  width: 50%;
  height: 60px;
  border: 2px solid rgba(99, 102, 241, 0.7);
  border-radius: 12px;
  box-shadow: 0 0 0 9999px rgba(0, 0, 0, 0.5),
    inset 0 0 20px rgba(99, 102, 241, 0.08);
  transition: border-color var(--transition), box-shadow var(--transition);
}

.roi-frame.scanning {
  animation: roi-pulse 1.6s ease-in-out infinite;
}

@keyframes roi-pulse {

  0%,
  100% {
    border-color: rgba(99, 102, 241, 0.7);
  }

  50% {
    border-color: rgba(99, 102, 241, 1);
    box-shadow: 0 0 0 9999px rgba(0, 0, 0, 0.5), 0 0 24px var(--accent-glow);
  }
}

.roi-label {
  position: absolute;
  top: 45%;
  left: 45%;
  transform: translate(-50%, 46px);
  font-size: 0.65rem;
  font-weight: 600;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--text-secondary);
  white-space: nowrap;
}

.roi-mask-b {
  position: absolute;
  top: 45%;
  left: 73%;
  transform: translateY(-50%);
  font-family: 'Inter', system-ui, sans-serif;
  font-size: 2.2rem;
  font-weight: 800;
  color: rgba(99, 102, 241, 0.85);
  text-shadow: 0 0 16px var(--accent-glow), 0 0 4px rgba(99, 102, 241, 0.3);
  letter-spacing: 0.05em;
  pointer-events: none;
  user-select: none;
}

/* ---- Scan line animation inside ROI ---- */
.roi-frame::after {
  content: '';
  position: absolute;
  top: 0;
  left: 10%;
  width: 80%;
  height: 2px;
  background: linear-gradient(90deg, transparent, var(--accent), transparent);
  animation: scan-line 2.2s ease-in-out infinite;
  opacity: 0.7;
}

@keyframes scan-line {
  0% {
    top: 0;
  }

  50% {
    top: calc(100% - 2px);
  }

  100% {
    top: 0;
  }
}

/* ---- Result alert (overlaid on camera bottom) ---- */
#result-alert {
  position: absolute;
  bottom: 10px;
  left: 50%;
  transform: translateX(-50%) translateY(10px);
  z-index: 15;
  width: calc(100% - 24px);
  max-width: 400px;
  padding: 10px 16px;
  border-radius: 12px;
  text-align: center;
  font-weight: 700;
  font-size: 0.9rem;
  letter-spacing: 0.03em;
  backdrop-filter: blur(16px);
  -webkit-backdrop-filter: blur(16px);
  border: 1px solid var(--border-subtle);
  background: var(--bg-glass);
  color: var(--text-secondary);
  transition: all var(--transition);
  opacity: 0;
}

#result-alert.visible {
  opacity: 1;
  transform: translateX(-50%) translateY(0);
}

#result-alert.valid {
  background: rgba(34, 197, 94, 0.15);
  border-color: var(--valid-green);
  color: var(--valid-green);
  box-shadow: 0 0 24px var(--valid-glow);
}

#result-alert.invalid {
  background: rgba(239, 68, 68, 0.15);
  border-color: var(--invalid-red);
  color: var(--invalid-red);
  box-shadow: 0 0 24px var(--invalid-glow);
  animation: alert-shake 0.45s ease-in-out;
}

#result-alert.neutral {
  background: var(--bg-glass);
  border-color: var(--border-subtle);
  color: var(--text-secondary);
  box-shadow: none;
}

@keyframes alert-shake {

  0%,
  100% {
    transform: translateX(-50%) translateY(0);
  }

  20% {
    transform: translateX(calc(-50% - 6px)) translateY(0);
  }

  40% {
    transform: translateX(calc(-50% + 6px)) translateY(0);
  }

  60% {
    transform: translateX(calc(-50% - 4px)) translateY(0);
  }

  80% {
    transform: translateX(calc(-50% + 4px)) translateY(0);
  }
}

#result-number {
  display: inline;
  font-size: 1.1rem;
  font-weight: 800;
  margin-right: 8px;
  font-variant-numeric: tabular-nums;
}

#result-label {
  display: inline;
  font-size: 0.75rem;
  font-weight: 600;
  letter-spacing: 0.06em;
  text-transform: uppercase;
}

/* ---- History Panel (bottom portion) ---- */
#history-panel {
  flex: 1;
  display: flex;
  flex-direction: column;
  background: var(--bg-primary);
  border-top: 1px solid var(--border-subtle);
  min-height: 0;
  overflow: hidden;
}

#history-header {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 16px;
  background: var(--bg-secondary);
  border-bottom: 1px solid var(--border-subtle);
  flex-shrink: 0;
  flex-wrap: wrap;
}

#history-header h2 {
  font-size: 0.82rem;
  font-weight: 700;
  white-space: nowrap;
}

#history-stats {
  font-size: 0.68rem;
  font-weight: 500;
  color: var(--text-secondary);
  flex: 1;
}

#history-stats .stat-sep {
  margin: 0 4px;
  opacity: 0.4;
}

#history-stats .stat-ok {
  color: var(--valid-green);
}

#history-stats .stat-bad {
  color: var(--invalid-red);
}

#history-clear {
  padding: 4px 12px;
  border: 1px solid var(--border-subtle);
  border-radius: 8px;
  background: transparent;
  color: var(--text-secondary);
  font-family: inherit;
  font-size: 0.65rem;
  font-weight: 600;
  cursor: pointer;
  transition: all var(--transition);
}

#history-clear:hover {
  background: rgba(239, 68, 68, 0.1);
  border-color: var(--invalid-red);
  color: var(--invalid-red);
}

#status-bar {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 6px;
  font-size: 0.68rem;
  font-weight: 500;
  color: var(--text-secondary);
  letter-spacing: 0.04em;
  flex-shrink: 0;
}

#status-bar .dot {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--text-secondary);
  transition: background var(--transition);
}

#status-bar .dot.ready {
  background: var(--valid-green);
  box-shadow: 0 0 6px var(--valid-glow);
}

#status-bar .dot.loading {
  background: var(--accent);
  animation: dot-blink 1s ease-in-out infinite;
}

@keyframes dot-blink {

  0%,
  100% {
    opacity: 1;
  }

  50% {
    opacity: 0.3;
  }
}

/* ---- History list ---- */
#history-list {
  flex: 1;
  overflow-y: auto;
  padding: 4px 10px 10px;
  -webkit-overflow-scrolling: touch;
}

#history-empty {
  text-align: center;
  color: var(--text-secondary);
  font-size: 0.75rem;
  padding: 24px 16px;
  opacity: 0.6;
}

.history-item {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 12px;
  margin-bottom: 6px;
  border-radius: 10px;
  border: 1px solid var(--border-subtle);
  background: var(--bg-secondary);
  animation: slide-in 0.3s ease;
}

@keyframes slide-in {
  from {
    opacity: 0;
    transform: translateY(-8px);
  }

  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.history-item .h-badge {
  width: 32px;
  height: 32px;
  border-radius: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1rem;
  flex-shrink: 0;
}

.history-item.h-valid .h-badge {
  background: rgba(34, 197, 94, 0.15);
}

.history-item.h-invalid .h-badge {
  background: rgba(239, 68, 68, 0.15);
}

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

.history-item .h-serial {
  font-size: 0.9rem;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.history-item.h-valid .h-serial {
  color: var(--valid-green);
}

.history-item.h-invalid .h-serial {
  color: var(--invalid-red);
}

.history-item .h-status {
  font-size: 0.65rem;
  font-weight: 600;
  letter-spacing: 0.05em;
  text-transform: uppercase;
  color: var(--text-secondary);
}

.history-item .h-time {
  font-size: 0.62rem;
  font-weight: 500;
  color: var(--text-secondary);
  opacity: 0.6;
  white-space: nowrap;
}

/* ---- Start / permission screen ---- */
#start-screen {
  position: absolute;
  inset: 0;
  z-index: 50;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 24px;
  background: var(--bg-primary);
  transition: opacity 0.5s ease, visibility 0.5s ease;
}

#start-screen.hidden {
  opacity: 0;
  visibility: hidden;
  pointer-events: none;
}

#start-screen .logo {
  font-size: 3rem;
}

#start-screen h2 {
  font-size: 1.3rem;
  font-weight: 700;
  text-align: center;
  max-width: 280px;
  line-height: 1.4;
}

#start-screen p {
  font-size: 0.82rem;
  color: var(--text-secondary);
  max-width: 260px;
  text-align: center;
  line-height: 1.5;
}

#start-btn {
  margin-top: 8px;
  padding: 14px 40px;
  border: none;
  border-radius: 12px;
  background: linear-gradient(135deg, var(--accent), #8b5cf6);
  color: #fff;
  font-family: inherit;
  font-size: 0.95rem;
  font-weight: 700;
  letter-spacing: 0.04em;
  cursor: pointer;
  transition: transform var(--transition), box-shadow var(--transition);
  box-shadow: 0 4px 20px var(--accent-glow);
}

#start-btn:hover {
  transform: translateY(-2px);
  box-shadow: 0 8px 30px var(--accent-glow);
}

#start-btn:active {
  transform: translateY(0);
}

#start-btn:disabled {
  opacity: 0.5;
  cursor: not-allowed;
  transform: none;
  box-shadow: none;
}

#start-btn:disabled:hover {
  transform: none;
  box-shadow: none;
}

/* ---- OCR Loading Progress ---- */
#ocr-loading {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  width: 100%;
  max-width: 260px;
}

#ocr-progress-bar {
  width: 100%;
  height: 4px;
  border-radius: 4px;
  background: rgba(148, 163, 184, 0.15);
  overflow: hidden;
}

#ocr-progress-fill {
  height: 100%;
  width: 0%;
  border-radius: 4px;
  background: linear-gradient(90deg, var(--accent), #8b5cf6);
  box-shadow: 0 0 10px var(--accent-glow);
  transition: width 0.3s ease;
}

#ocr-progress-text {
  font-size: 0.7rem;
  color: var(--text-secondary);
  text-align: center;
}

#ocr-loading.done {
  display: none;
}

/* ---- Responsive ---- */
@media (min-width: 600px) {
  .roi-frame {
    width: 35%;
    max-width: 350px;
    height: 70px;
  }

  .roi-mask-b {
    left: 66%;
    font-size: 2.5rem;
  }

  #video-container {
    height: 55vh;
  }
}

@media (min-width: 1024px) {
  .roi-frame {
    width: 25%;
    max-width: 400px;
    height: 80px;
  }

  .roi-mask-b {
    left: 64%;
    font-size: 2.8rem;
  }

  #video-container {
    height: 55vh;
  }
}

/* ---- Debug Log Panel ---- */
#debug-panel {
  position: fixed;
  bottom: 10px;
  right: 10px;
  z-index: 100;
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  gap: 6px;
  pointer-events: auto;
}

#debug-toggle {
  padding: 6px 10px;
  border: 1px solid var(--border-subtle);
  border-radius: 8px;
  background: var(--bg-glass);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  color: var(--text-secondary);
  font-family: inherit;
  font-size: 0.65rem;
  font-weight: 600;
  cursor: pointer;
  transition: all var(--transition);
  display: flex;
  align-items: center;
  gap: 4px;
}

#debug-toggle:hover {
  background: var(--bg-secondary);
  color: var(--text-primary);
}

#debug-count {
  background: var(--accent);
  color: #fff;
  font-size: 0.6rem;
  font-weight: 700;
  padding: 0 5px;
  border-radius: 6px;
  min-width: 16px;
  text-align: center;
}

#debug-log {
  width: 300px;
  max-height: 200px;
  overflow-y: auto;
  padding: 10px;
  border: 1px solid var(--border-subtle);
  border-radius: 12px;
  background: rgba(10, 14, 23, 0.95);
  backdrop-filter: blur(16px);
  -webkit-backdrop-filter: blur(16px);
  font-family: 'Courier New', monospace;
  font-size: 0.62rem;
  line-height: 1.5;
  color: var(--text-secondary);
  transition: opacity 0.25s ease, max-height 0.3s ease;
}

#debug-log.collapsed {
  max-height: 0;
  padding: 0 10px;
  border-color: transparent;
  opacity: 0;
  pointer-events: none;
}

#debug-log .log-entry {
  padding: 1px 0;
  border-bottom: 1px solid rgba(148, 163, 184, 0.06);
  word-break: break-all;
}

#debug-log .log-entry .log-time {
  color: var(--accent);
  margin-right: 4px;
}

#debug-log .log-entry.log-ok {
  color: var(--valid-green);
}

#debug-log .log-entry.log-err {
  color: var(--invalid-red);
}

@media (max-width: 380px) {
  #debug-log {
    width: calc(100vw - 20px);
  }
}