:root {
  color-scheme: light;
  --bg: #f3f3f1;
  --surface: #ffffff;
  --surface-2: #f2f2f0;
  --text: #0b0b0b;
  --muted: #75756f;
  --line: rgba(10, 10, 10, .07);
  --line-strong: rgba(10, 10, 10, .14);
  --inv: #0b0b0b;
  --on-inv: #ffffff;
  --shadow: 0 1px 2px rgba(0, 0, 0, .04), 0 24px 48px -28px rgba(0, 0, 0, .22);
  --ease: cubic-bezier(.22, .7, .2, 1);
  --spring: cubic-bezier(.34, 1.5, .64, 1);
  --font: -apple-system, BlinkMacSystemFont, "SF Pro Text", Inter, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --mono: ui-monospace, "SF Mono", SFMono-Regular, Menlo, Consolas, monospace;
  --i-scan: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='black' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='M4 8V6a2 2 0 0 1 2-2h2M16 4h2a2 2 0 0 1 2 2v2M20 16v2a2 2 0 0 1-2 2h-2M8 20H6a2 2 0 0 1-2-2v-2M4 12h16'/%3E%3C/svg%3E");
  --i-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='black' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Crect x='3' y='3' width='18' height='18' rx='3'/%3E%3Ccircle cx='9' cy='9' r='1.6'/%3E%3Cpath d='m21 15-4.5-4.5L6 21'/%3E%3C/svg%3E");
  --i-stop: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24'%3E%3Crect x='6' y='6' width='12' height='12' rx='2.5' fill='black'/%3E%3C/svg%3E");
  --i-copy: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='black' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Crect x='9' y='9' width='11' height='11' rx='2.5'/%3E%3Cpath d='M5 15V6a2 2 0 0 1 2-2h8'/%3E%3C/svg%3E");
  --i-arrow: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='black' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='M7 17 17 7M8.5 7H17v8.5'/%3E%3C/svg%3E");
  --i-bolt: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='black' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='M13 3 5 14h6l-1 7 8-11h-6z'/%3E%3C/svg%3E");
  --i-chevron: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='black' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='m6 9 6 6 6-6'/%3E%3C/svg%3E");
}

html[data-theme="dark"] {
  color-scheme: dark;
  --bg: #0a0a0a;
  --surface: #141414;
  --surface-2: #1c1c1c;
  --text: #f4f4f2;
  --muted: #8d8d87;
  --line: rgba(255, 255, 255, .07);
  --line-strong: rgba(255, 255, 255, .14);
  --inv: #f4f4f2;
  --on-inv: #0a0a0a;
  --shadow: 0 1px 0 rgba(255, 255, 255, .04) inset, 0 24px 48px -28px rgba(0, 0, 0, .8);
}

*, *::before, *::after { box-sizing: border-box; }
[hidden] { display: none !important; }

html { -webkit-text-size-adjust: 100%; background: var(--bg); }

body {
  margin: 0;
  min-height: 100vh;
  min-height: 100dvh;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.45 var(--font);
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  transition: background-color .35s var(--ease), color .35s var(--ease);
}

button, select, textarea, input { font: inherit; color: inherit; }

.sr-only {
  position: absolute !important;
  width: 1px; height: 1px;
  padding: 0; margin: -1px;
  overflow: hidden; clip: rect(0 0 0 0);
  white-space: nowrap; border: 0;
}

.page {
  width: 100%;
  max-width: 460px;
  min-height: 100vh;
  min-height: 100dvh;
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  padding: max(8px, env(safe-area-inset-top)) max(16px, env(safe-area-inset-right)) max(12px, env(safe-area-inset-bottom)) max(16px, env(safe-area-inset-left));
}

/* Header */
.site-header {
  height: 56px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  animation: fadeIn .5s var(--ease) both;
}

.brand {
  display: inline-flex;
  align-items: center;
  gap: 10px;
  color: inherit;
  text-decoration: none;
  font-size: 16px;
  font-weight: 650;
  letter-spacing: -.015em;
}

.brand-mark {
  position: relative;
  width: 26px;
  height: 26px;
  border-radius: 8px;
  background: var(--inv);
  transition: background-color .35s var(--ease);
}

.brand-mark::after {
  content: "";
  position: absolute;
  inset: 0;
  margin: auto;
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--on-inv);
  animation: blink 2.8s ease-in-out infinite;
}

.theme-button {
  width: 38px;
  height: 38px;
  border-radius: 50%;
  border: 1px solid var(--line-strong);
  background: var(--surface);
  display: grid;
  place-items: center;
  font-size: 0;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
  transition: transform .2s var(--ease), background-color .35s var(--ease), border-color .35s var(--ease);
}

.theme-button::before {
  content: "";
  width: 15px;
  height: 15px;
  border-radius: 50%;
  border: 1.5px solid var(--text);
  background: linear-gradient(90deg, var(--text) 50%, transparent 50%);
  transition: transform .6s var(--spring);
}

html[data-theme="dark"] .theme-button::before { transform: rotate(180deg); }
.theme-button:hover { border-color: var(--text); }
.theme-button:active { transform: scale(.9); }

/* Layout */
.layout {
  flex: 1;
  display: flex;
  flex-direction: column;
  justify-content: center;
  gap: 12px;
  padding: 4px 0 16px;
}

.card {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 24px;
  box-shadow: var(--shadow);
  transition: background-color .35s var(--ease), border-color .35s var(--ease);
}

.scanner-card {
  padding: 10px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  animation: rise .7s var(--ease) .05s both;
}

/* Preview */
.preview {
  position: relative;
  width: 100%;
  aspect-ratio: 1 / 1;
  border-radius: 16px;
  overflow: hidden;
  background: #0c0c0c;
  isolation: isolate;
  color: #fff;
}

.preview::before {
  content: "";
  position: absolute;
  inset: 0;
  z-index: -1;
  background: radial-gradient(110% 80% at 50% 0%, #202020 0%, #0c0c0c 62%);
}

.preview video {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  object-fit: cover;
  animation: fadeIn .45s var(--ease);
}

.viewfinder {
  position: absolute;
  inset: 18%;
  z-index: 2;
  pointer-events: none;
  border-radius: 14px;
  opacity: .3;
  transition: opacity .45s var(--ease), box-shadow .45s var(--ease);
}

.vf-corner {
  position: absolute;
  width: 28px;
  height: 28px;
  border: 3px solid #fff;
}

.vf-corner--tl { top: 0; left: 0; border-right: 0; border-bottom: 0; border-top-left-radius: 14px; }
.vf-corner--tr { top: 0; right: 0; border-left: 0; border-bottom: 0; border-top-right-radius: 14px; }
.vf-corner--bl { bottom: 0; left: 0; border-right: 0; border-top: 0; border-bottom-left-radius: 14px; }
.vf-corner--br { bottom: 0; right: 0; border-left: 0; border-top: 0; border-bottom-right-radius: 14px; }

.scan-line {
  position: absolute;
  left: 8%;
  right: 8%;
  top: 4%;
  height: 2px;
  border-radius: 2px;
  background: linear-gradient(90deg, transparent, #fff 50%, transparent);
  box-shadow: 0 0 14px 2px rgba(255, 255, 255, .45);
  opacity: 0;
}

.preview.live .viewfinder,
body[data-state="scanning"] .viewfinder {
  opacity: 1;
  box-shadow: 0 0 0 100vmax rgba(0, 0, 0, .34);
  animation: breathe 2.6s ease-in-out infinite;
}

body[data-state="scanning"] .scan-line { animation: sweep 2.4s var(--ease) infinite; }

body[data-state="success"] .viewfinder {
  opacity: 1;
  animation: snap .55s var(--spring);
}

body[data-state="success"] .preview::after {
  content: "";
  position: absolute;
  inset: 0;
  z-index: 4;
  background: #fff;
  pointer-events: none;
  animation: flash .7s var(--ease) forwards;
}

.placeholder {
  position: absolute;
  inset: 0;
  z-index: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 14px;
  padding: 24px;
  text-align: center;
}

.ph-visual {
  width: 56px;
  height: 56px;
  display: grid;
  place-items: center;
}

.scan-symbol {
  width: 56px;
  height: 56px;
  color: rgba(255, 255, 255, .8);
  animation: float 4s ease-in-out infinite;
}

.loader, .done-mark { display: none; }

.loader {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  border: 2.5px solid rgba(255, 255, 255, .16);
  border-top-color: #fff;
  animation: spin .8s linear infinite;
}

.done-mark { width: 56px; height: 56px; color: #fff; }
.dm-circle { stroke-dasharray: 176; stroke-dashoffset: 176; animation: draw .6s var(--ease) forwards; }
.dm-check { stroke-dasharray: 36; stroke-dashoffset: 36; animation: draw .35s var(--ease) .45s forwards; }

body[data-state="starting"] .scan-symbol,
body[data-state="image"] .scan-symbol,
body[data-state="success"] .scan-symbol { display: none; }
body[data-state="starting"] .loader,
body[data-state="image"] .loader { display: block; }
body[data-state="success"] .done-mark { display: block; }

#placeholderText {
  margin: 0;
  max-width: 26ch;
  font-size: 13.5px;
  color: rgba(255, 255, 255, .62);
  animation: fadeIn .4s var(--ease);
}

body[data-state="idle"] #placeholderText,
body[data-state="success"] #placeholderText,
#placeholderText:empty { display: none; }

.camera-badge {
  position: absolute;
  top: 12px;
  left: 50%;
  z-index: 3;
  transform: translateX(-50%);
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 6px 12px;
  border-radius: 999px;
  background: rgba(0, 0, 0, .5);
  -webkit-backdrop-filter: blur(12px);
  backdrop-filter: blur(12px);
  color: #fff;
  font-size: 12.5px;
  font-weight: 550;
  white-space: nowrap;
  animation: badgeIn .4s var(--ease) both;
}

.camera-badge::before {
  content: "";
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: #fff;
  animation: blink 1.2s ease-in-out infinite;
}

/* Camera tools */
.camera-tools {
  display: flex;
  gap: 8px;
  animation: fadeIn .35s var(--ease);
}

#cameraChoice { flex: 1; min-width: 0; }
#cameraChoice[hidden] + #torchButton { flex: 1; }

.select-wrap { position: relative; display: block; }

.select-wrap::after {
  content: "";
  position: absolute;
  right: 12px;
  top: 50%;
  width: 16px;
  height: 16px;
  margin-top: -8px;
  background: var(--muted);
  -webkit-mask: var(--i-chevron) center / contain no-repeat;
  mask: var(--i-chevron) center / contain no-repeat;
  pointer-events: none;
}

select {
  width: 100%;
  height: 46px;
  padding: 0 38px 0 14px;
  border: 1px solid var(--line);
  border-radius: 14px;
  background: var(--surface-2);
  font-size: 14.5px;
  text-overflow: ellipsis;
  -webkit-appearance: none;
  appearance: none;
  cursor: pointer;
}

select:disabled { opacity: .55; }

/* Buttons */
.actions {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
}

.btn {
  --i: var(--i-scan);
  position: relative;
  min-width: 0;
  height: 50px;
  padding: 0 14px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  border: 1px solid transparent;
  border-radius: 14px;
  font-size: 15px;
  font-weight: 600;
  letter-spacing: -.01em;
  white-space: nowrap;
  text-decoration: none;
  overflow: hidden;
  cursor: pointer;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
  transition: transform .16s var(--ease), background-color .3s var(--ease), border-color .3s var(--ease), color .3s var(--ease), opacity .2s;
}

.btn::before {
  content: "";
  flex: 0 0 auto;
  width: 18px;
  height: 18px;
  background: currentColor;
  -webkit-mask: var(--i) center / contain no-repeat;
  mask: var(--i) center / contain no-repeat;
}

#startButton { --i: var(--i-scan); }
#stopButton { --i: var(--i-stop); }
#uploadButton { --i: var(--i-image); }
#torchButton { --i: var(--i-bolt); height: 46px; }
#copyButton { --i: var(--i-copy); }
#openLink { --i: var(--i-arrow); }

.btn.primary { background: var(--inv); color: var(--on-inv); }

.btn.primary::after {
  content: "";
  position: absolute;
  inset: 0;
  background: linear-gradient(105deg, transparent 35%, rgba(127, 127, 127, .22) 50%, transparent 65%);
  transform: translateX(-110%);
  transition: transform .8s var(--ease);
  pointer-events: none;
}

.btn--ghost, .btn--soft {
  background: var(--surface-2);
  color: var(--text);
  border-color: var(--line);
}

#torchButton[aria-pressed="true"] { background: var(--inv); color: var(--on-inv); }

@media (hover: hover) {
  .btn.primary:hover::after { transform: translateX(110%); }
  .btn--ghost:hover, .btn--soft:hover { border-color: var(--line-strong); }
}

.btn:active:not(:disabled) { transform: scale(.97); }
.btn:disabled { opacity: .5; cursor: default; }

body[data-state="image"] #uploadButton { opacity: 1; }
body[data-state="image"] #uploadButton::before {
  width: 16px;
  height: 16px;
  background: none;
  -webkit-mask: none;
  mask: none;
  border: 2px solid currentColor;
  border-right-color: transparent;
  border-radius: 50%;
  animation: spin .75s linear infinite;
}

.btn:focus-visible, select:focus-visible, .theme-button:focus-visible, textarea:focus-visible, .brand:focus-visible {
  outline: 2px solid var(--text);
  outline-offset: 2px;
}

/* Status: only errors are shown */
.status { margin: 0; }
.status:empty, .status:not([data-error="true"]) { display: none; }

.status[data-error="true"] {
  display: flex;
  align-items: flex-start;
  gap: 10px;
  padding: 11px 12px;
  border-radius: 14px;
  background: var(--surface-2);
  border: 1px solid var(--line-strong);
  font-size: 13.5px;
  line-height: 1.4;
  animation: shake .42s var(--ease);
}

.status[data-error="true"]::before {
  content: "!";
  flex: 0 0 18px;
  height: 18px;
  margin-top: 1px;
  border-radius: 50%;
  background: var(--inv);
  color: var(--on-inv);
  font-size: 12px;
  font-weight: 700;
  line-height: 18px;
  text-align: center;
}

/* Result */
.result {
  padding: 14px;
  display: flex;
  flex-direction: column;
  gap: 12px;
  animation: popIn .5s var(--ease) both;
}

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

.result-mark {
  flex: 0 0 auto;
  width: 28px;
  height: 28px;
  border-radius: 50%;
  display: grid;
  place-items: center;
  background: var(--inv);
  color: var(--on-inv);
  animation: pop .55s var(--spring) .1s both;
}

.result-mark svg { width: 16px; height: 16px; }

#resultTitle {
  flex: 1;
  min-width: 0;
  margin: 0;
  font-size: 16px;
  font-weight: 650;
  letter-spacing: -.015em;
}

.tag {
  flex: 0 0 auto;
  padding: 4px 10px;
  border-radius: 999px;
  border: 1px solid var(--line);
  background: var(--surface-2);
  color: var(--muted);
  font-size: 12px;
  font-weight: 600;
}

#resultText {
  width: 100%;
  min-height: 76px;
  padding: 12px 14px;
  border: 1px solid var(--line);
  border-radius: 14px;
  background: var(--surface-2);
  font: 14px/1.5 var(--mono);
  word-break: break-all;
  resize: vertical;
  outline: none;
}

.result-note { display: none; }

.actions--result:has(#openLink[hidden]) { grid-template-columns: 1fr; }

.copy-status {
  margin: -2px 0 0;
  font-size: 12.5px;
  color: var(--muted);
  text-align: center;
  animation: fadeIn .3s var(--ease);
}

.copy-status:empty { display: none; }

.noscript-note { margin: 0; text-align: center; color: var(--muted); }

/* Footer */
.site-footer {
  display: flex;
  justify-content: center;
  align-items: center;
  gap: 8px;
  padding: 6px 0 2px;
  font-size: 12px;
  color: var(--muted);
  animation: fadeIn .6s var(--ease) .3s both;
}

.version {
  padding: 1px 7px;
  border-radius: 999px;
  border: 1px solid var(--line);
}

/* Drag and drop */
.drop-zone {
  position: fixed;
  inset: 0;
  z-index: 50;
  display: grid;
  place-items: center;
  padding: 24px;
  background: rgba(127, 127, 127, .18);
  -webkit-backdrop-filter: blur(10px);
  backdrop-filter: blur(10px);
  opacity: 0;
  visibility: hidden;
  pointer-events: none;
  transition: opacity .25s var(--ease), visibility .25s;
}

.drop-inner {
  padding: 36px 48px;
  border: 2px dashed var(--line-strong);
  border-radius: 24px;
  background: var(--surface);
  text-align: center;
  transform: scale(.96);
  transition: transform .35s var(--spring);
}

.drop-inner p { margin: 0; font-weight: 600; }

.drop-icon {
  display: block;
  width: 32px;
  height: 32px;
  margin: 0 auto 10px;
  background: currentColor;
  -webkit-mask: var(--i-image) center / contain no-repeat;
  mask: var(--i-image) center / contain no-repeat;
}

body.is-dragging .drop-zone { opacity: 1; visibility: visible; }
body.is-dragging .drop-inner { transform: scale(1); }

@media (max-width: 359px) {
  .actions { grid-template-columns: 1fr; }
  .page { padding-left: 12px; padding-right: 12px; }
}

@media (min-width: 900px) {
  .page { max-width: 480px; }
  .scanner-card { padding: 12px; gap: 12px; }
}

/* Keyframes */
@keyframes fadeIn { from { opacity: 0; } to { opacity: 1; } }
@keyframes rise { from { opacity: 0; transform: translateY(14px) scale(.985); } to { opacity: 1; transform: none; } }
@keyframes popIn { from { opacity: 0; transform: translateY(10px) scale(.98); } to { opacity: 1; transform: none; } }
@keyframes pop { from { transform: scale(.3); opacity: 0; } to { transform: scale(1); opacity: 1; } }
@keyframes badgeIn { from { opacity: 0; transform: translate(-50%, -6px); } to { opacity: 1; transform: translate(-50%, 0); } }
@keyframes breathe { 0%, 100% { transform: scale(1); } 50% { transform: scale(1.025); } }
@keyframes snap { 0% { transform: scale(1.12); } 100% { transform: scale(1); } }
@keyframes sweep {
  0% { top: 4%; opacity: 0; }
  15% { opacity: 1; }
  85% { opacity: 1; }
  100% { top: 96%; opacity: 0; }
}
@keyframes float { 0%, 100% { transform: translateY(0); } 50% { transform: translateY(-4px); } }
@keyframes blink { 0%, 100% { opacity: 1; } 50% { opacity: .35; } }
@keyframes spin { to { transform: rotate(360deg); } }
@keyframes flash { from { opacity: .32; } to { opacity: 0; } }
@keyframes draw { to { stroke-dashoffset: 0; } }
@keyframes shake {
  0%, 100% { transform: translateX(0); }
  25% { transform: translateX(-4px); }
  50% { transform: translateX(4px); }
  75% { transform: translateX(-2px); }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: .01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: .01ms !important;
  }
  .scan-line { display: none; }
}
