/* ===== Variables ===== */
:root {
  --bg: #1a1a2e;
  --bg-surface: #16213e;
  --bg-elevated: #0f3460;
  --accent: #e94560;
  --success: #4ecca3;
  --warning: #f5a623;
  --text: #eaeaea;
  --text-muted: #8892a4;
  --border: rgba(255,255,255,0.08);
  --overlay: rgba(0,0,0,0.65);
  --header-h: 56px;
  --radius: 12px;
  --radius-sm: 8px;
  --touch: 56px;
  --transition: 0.2s ease;
}

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

html {
  height: 100%;
  -webkit-text-size-adjust: 100%;
}

body {
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  font-size: 16px;
  line-height: 1.5;
  overflow-x: hidden;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
  user-select: none;
}

button {
  cursor: pointer;
  border: none;
  background: none;
  font: inherit;
  color: inherit;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}

/* ===== Header ===== */
.header {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  z-index: 100;
  height: var(--header-h);
  background: var(--bg-surface);
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 16px;
  gap: 12px;
}

.header-left {
  display: flex;
  align-items: center;
  gap: 10px;
}

.header-icon {
  width: 24px;
  height: 24px;
  color: var(--accent);
  flex-shrink: 0;
}

.header-title {
  font-size: 20px;
  font-weight: 700;
  letter-spacing: 0.5px;
  color: var(--text);
}

.header-right {
  display: flex;
  align-items: center;
  gap: 4px;
}

/* ===== Icon Buttons ===== */
.icon-btn {
  position: relative;
  width: var(--touch);
  height: var(--touch);
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 50%;
  color: var(--text-muted);
  transition: color var(--transition), background var(--transition);
}

.icon-btn:active {
  background: rgba(255,255,255,0.08);
  color: var(--text);
}

.icon-btn svg {
  width: 22px;
  height: 22px;
}

.icon-btn.active {
  color: var(--warning);
}

/* ===== Badge ===== */
.badge {
  position: absolute;
  top: 8px;
  right: 8px;
  min-width: 18px;
  height: 18px;
  background: var(--accent);
  color: #fff;
  font-size: 11px;
  font-weight: 700;
  border-radius: 9px;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0 4px;
  line-height: 1;
}

/* ===== Main ===== */
.main {
  padding-top: var(--header-h);
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
}

.format-toolbar {
  padding: 12px 12px 8px;
  border-bottom: 1px solid var(--border);
  background: linear-gradient(180deg, rgba(22,33,62,0.95), rgba(15,52,96,0.72));
}

.format-selector {
  display: flex;
  gap: 8px;
  overflow-x: auto;
  scrollbar-width: none;
}

.format-selector::-webkit-scrollbar {
  display: none;
}

.format-chip {
  flex: 0 0 auto;
  min-height: 38px;
  padding: 0 14px;
  border-radius: 999px;
  border: 1px solid rgba(255,255,255,0.12);
  background: rgba(255,255,255,0.04);
  color: var(--text-muted);
  font-size: 14px;
  font-weight: 600;
  transition: background var(--transition), color var(--transition), border-color var(--transition);
}

.format-chip.active,
.format-chip[aria-pressed="true"] {
  background: rgba(233, 69, 96, 0.18);
  border-color: rgba(233, 69, 96, 0.5);
  color: var(--text);
}

/* ===== Scanner Section ===== */
.scanner-section {
  flex: 1;
  display: flex;
  flex-direction: column;
  position: relative;
}

.scanner-wrapper {
  position: relative;
  width: 100%;
  background: #000;
  overflow: hidden;
  /* Aspect ratio: show enough video for comfortable scanning */
  aspect-ratio: 9/14;
  max-height: calc(100dvh - var(--header-h) - 120px);
}

/* html5-qrcode injects a <div> and <video> — normalize them */
#scanner-container {
  width: 100%;
  height: 100%;
}

#scanner-container > div {
  width: 100% !important;
  height: 100% !important;
  padding: 0 !important;
  border: none !important;
}

#scanner-container video {
  width: 100% !important;
  height: 100% !important;
  object-fit: cover !important;
}

/* Hide html5-qrcode's own overlay/canvas so we use ours */
#scanner-container canvas {
  display: none !important;
}

/* ===== Scanner Overlay ===== */
.scanner-overlay {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  pointer-events: none;
}

.overlay-top,
.overlay-bottom {
  background: var(--overlay);
  flex: 1;
}

.overlay-bottom {
  display: flex;
  align-items: flex-start;
  justify-content: center;
  padding: 12px 16px 8px;
}

.scanner-guidance {
  width: min(92%, 360px);
  display: grid;
  gap: 10px;
}

.overlay-middle {
  display: flex;
  align-items: stretch;
  height: 110px;
}

.overlay-side {
  background: var(--overlay);
  width: 24px;
  flex-shrink: 0;
}

/* ===== Scan Region ===== */
.scan-region {
  flex: 1;
  position: relative;
  /* transparent cut-out — shows camera through */
}

/* Corner brackets */
.corner {
  position: absolute;
  width: 22px;
  height: 22px;
}

.corner-tl { top: 0; left: 0; border-top: 3px solid var(--accent); border-left: 3px solid var(--accent); border-radius: 4px 0 0 0; }
.corner-tr { top: 0; right: 0; border-top: 3px solid var(--accent); border-right: 3px solid var(--accent); border-radius: 0 4px 0 0; }
.corner-bl { bottom: 0; left: 0; border-bottom: 3px solid var(--accent); border-left: 3px solid var(--accent); border-radius: 0 0 0 4px; }
.corner-br { bottom: 0; right: 0; border-bottom: 3px solid var(--accent); border-right: 3px solid var(--accent); border-radius: 0 0 4px 0; }

/* Animated scanning line */
.scan-line {
  position: absolute;
  left: 4px;
  right: 4px;
  height: 2px;
  background: linear-gradient(90deg, transparent, var(--accent), transparent);
  animation: scan-move 2s ease-in-out infinite;
  top: 50%;
}

@keyframes scan-move {
  0%   { transform: translateY(-40px); opacity: 0; }
  10%  { opacity: 1; }
  90%  { opacity: 1; }
  100% { transform: translateY(40px); opacity: 0; }
}

.scan-line.paused {
  animation-play-state: paused;
}

/* ===== Scanner Hint ===== */
.scanner-hint {
  font-size: 13px;
  color: rgba(255,255,255,0.75);
  text-align: center;
  line-height: 1.4;
}

.scanner-hint[data-tone="warning"] {
  color: #ffd27d;
}

.scanner-hint[data-tone="error"] {
  color: #ff9aa9;
}

/* ===== Scanner Status Messages ===== */
.scanner-status {
  padding: 16px;
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 72px;
}

.status-idle {
  display: flex;
  align-items: center;
  gap: 12px;
  color: var(--text-muted);
  font-size: 15px;
}

.status-error {
  flex-direction: column;
  align-items: center;
  gap: 10px;
  color: var(--accent);
  text-align: center;
}

.status-error svg {
  width: 32px;
  height: 32px;
}

/* ===== Spinner ===== */
.spinner {
  width: 22px;
  height: 22px;
  border: 2px solid var(--border);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
  flex-shrink: 0;
}

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

/* ===== Tap-to-focus indicator ===== */
.focus-indicator {
  position: absolute;
  width: 56px;
  height: 56px;
  border: 2px solid #fff;
  border-radius: 50%;
  pointer-events: none;
  opacity: 0;
  transform: translate(-50%, -50%) scale(1.2);
  transition: opacity 0.15s, transform 0.15s;
  z-index: 10;
}

.focus-indicator.active {
  opacity: 1;
  transform: translate(-50%, -50%) scale(1);
  animation: focus-fade 1.2s ease forwards;
}

@keyframes focus-fade {
  0%   { opacity: 1; transform: translate(-50%, -50%) scale(1); }
  60%  { opacity: 1; transform: translate(-50%, -50%) scale(0.85); }
  100% { opacity: 0; transform: translate(-50%, -50%) scale(0.85); }
}

/* ===== Buttons ===== */
.btn-primary {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  height: var(--touch);
  padding: 0 24px;
  background: var(--accent);
  color: #fff;
  font-size: 16px;
  font-weight: 600;
  border-radius: var(--radius);
  transition: opacity var(--transition), transform var(--transition);
  width: 100%;
}

.btn-primary:active {
  opacity: 0.85;
  transform: scale(0.98);
}

.btn-primary svg {
  width: 18px;
  height: 18px;
}

.btn-primary.btn-small {
  height: 40px;
  font-size: 14px;
  padding: 0 16px;
  width: auto;
}

.btn-secondary {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  height: 44px;
  padding: 0 20px;
  background: var(--bg-elevated);
  color: var(--text);
  font-size: 15px;
  font-weight: 500;
  border-radius: var(--radius);
  transition: opacity var(--transition);
}

.btn-secondary:active { opacity: 0.75; }

.btn-text {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 8px 12px;
  height: 44px;
  color: var(--text-muted);
  font-size: 14px;
  border-radius: var(--radius-sm);
  transition: color var(--transition), background var(--transition);
}

.btn-text:active {
  background: rgba(255,255,255,0.06);
  color: var(--text);
}

.btn-text svg {
  width: 18px;
  height: 18px;
}

.btn-text.btn-small {
  padding: 6px 10px;
  height: 36px;
}

.btn-danger { color: var(--accent); }
.btn-danger:active { color: var(--accent); }

/* ===== Result Section ===== */
.result-section {
  flex: 1;
  display: flex;
  align-items: flex-start;
  justify-content: center;
  padding: 16px;
}

.result-card {
  width: 100%;
  max-width: 480px;
  background: var(--bg-surface);
  border-radius: var(--radius);
  padding: 20px;
  display: flex;
  flex-direction: column;
  gap: 16px;
  border: 1px solid var(--border);
  animation: slide-up 0.25s ease;
}

@keyframes slide-up {
  from { opacity: 0; transform: translateY(16px); }
  to   { opacity: 1; transform: translateY(0); }
}

.result-header {
  display: flex;
  align-items: center;
  gap: 10px;
}

.result-icon {
  width: 28px;
  height: 28px;
  color: var(--success);
  flex-shrink: 0;
}

.result-label {
  font-size: 14px;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.5px;
}

.result-code-wrapper {
  display: flex;
  align-items: center;
  gap: 12px;
  background: var(--bg-elevated);
  border-radius: var(--radius-sm);
  padding: 12px 16px;
}

.result-code {
  flex: 1;
  font-family: 'Courier New', Courier, monospace;
  font-size: 26px;
  font-weight: 700;
  letter-spacing: 2px;
  color: var(--success);
  word-break: break-all;
}

.btn-copy {
  flex-shrink: 0;
  width: 44px;
  height: 44px;
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--text-muted);
  border-radius: var(--radius-sm);
  transition: color var(--transition), background var(--transition);
}

.btn-copy:active {
  background: rgba(255,255,255,0.08);
  color: var(--success);
}

.btn-copy svg {
  width: 20px;
  height: 20px;
}

.result-meta {
  font-size: 13px;
  color: var(--text-muted);
}

/* ===== History Drawer ===== */
.drawer-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.5);
  z-index: 200;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.3s;
}

.drawer-overlay.active {
  opacity: 1;
  pointer-events: auto;
}

.history-drawer {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  z-index: 201;
  background: var(--bg-surface);
  border-radius: 20px 20px 0 0;
  max-height: 80dvh;
  display: flex;
  flex-direction: column;
  transform: translateY(100%);
  transition: transform 0.3s cubic-bezier(0.32, 0.72, 0, 1);
  box-shadow: 0 -4px 32px rgba(0,0,0,0.4);
}

.history-drawer.open {
  transform: translateY(0);
}

.drawer-handle {
  width: 40px;
  height: 4px;
  background: var(--border);
  border-radius: 2px;
  margin: 12px auto 0;
  flex-shrink: 0;
}

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

.drawer-title {
  font-size: 18px;
  font-weight: 600;
}

.drawer-actions {
  display: flex;
  align-items: center;
  gap: 4px;
}

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

.history-list {
  list-style: none;
  padding: 8px 0 24px;
}

.history-empty {
  padding: 32px 16px;
  text-align: center;
  color: var(--text-muted);
  font-size: 15px;
}

.history-item {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 16px;
  border-bottom: 1px solid var(--border);
  transition: background var(--transition);
  animation: slide-up 0.2s ease;
}

.history-item:active {
  background: rgba(255,255,255,0.04);
}

.history-item:last-child {
  border-bottom: none;
}

.history-item-icon {
  width: 36px;
  height: 36px;
  background: var(--bg-elevated);
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  color: var(--accent);
}

.history-item-icon svg {
  width: 18px;
  height: 18px;
}

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

.history-item-code {
  font-family: 'Courier New', Courier, monospace;
  font-size: 17px;
  font-weight: 600;
  letter-spacing: 1px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.history-item-time {
  font-size: 12px;
  color: var(--text-muted);
  margin-top: 2px;
}

.history-item-copy {
  flex-shrink: 0;
  width: 40px;
  height: 40px;
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--text-muted);
  border-radius: var(--radius-sm);
  transition: color var(--transition), background var(--transition);
}

.history-item-copy:active {
  background: rgba(255,255,255,0.08);
  color: var(--success);
}

.history-item-copy svg {
  width: 18px;
  height: 18px;
}

/* ===== Toast Notifications ===== */
.toast-container {
  position: fixed;
  top: calc(var(--header-h) + 8px);
  left: 50%;
  transform: translateX(-50%);
  z-index: 300;
  display: flex;
  flex-direction: column;
  gap: 8px;
  width: calc(100% - 32px);
  max-width: 360px;
  pointer-events: none;
}

.toast {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 12px 16px;
  background: var(--bg-elevated);
  border-radius: var(--radius);
  box-shadow: 0 4px 16px rgba(0,0,0,0.4);
  font-size: 14px;
  font-weight: 500;
  border-left: 3px solid var(--text-muted);
  animation: toast-in 0.25s ease forwards;
  pointer-events: auto;
}

.toast.success { border-left-color: var(--success); }
.toast.error   { border-left-color: var(--accent); }
.toast.info    { border-left-color: var(--bg-elevated); }

.toast svg {
  width: 18px;
  height: 18px;
  flex-shrink: 0;
}

.toast.success svg { color: var(--success); }
.toast.error   svg { color: var(--accent); }

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

@keyframes toast-out {
  from { opacity: 1; transform: translateY(0); }
  to   { opacity: 0; transform: translateY(-8px); }
}

.toast.removing {
  animation: toast-out 0.2s ease forwards;
}

/* ===== Install Banner ===== */
.install-banner {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  z-index: 150;
  background: var(--bg-elevated);
  border-top: 1px solid var(--border);
  padding: 12px 16px;
  display: flex;
  align-items: center;
  gap: 12px;
  animation: slide-up 0.3s ease;
}

.install-text {
  flex: 1;
  min-width: 0;
}

.install-text strong {
  display: block;
  font-size: 15px;
}

.install-text span {
  font-size: 12px;
  color: var(--text-muted);
}

.install-actions {
  display: flex;
  gap: 8px;
  flex-shrink: 0;
}

/* ===== Permission Error ===== */
.permission-info {
  font-size: 13px;
  color: var(--text-muted);
  text-align: center;
  line-height: 1.6;
  padding: 0 16px;
}

/* ===== Scrollbar ===== */
::-webkit-scrollbar { width: 4px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--border); border-radius: 2px; }
