:root {
  --bg: #0f1115;
  --panel: #181b22;
  --border: #262a33;
  --text: #e6e8ec;
  --muted: #9aa0a8;
  --accent: #4ea1ff;
  --ok: #3ddc97;
  --warn: #ffb547;
  --err: #ff5c7c;
  --safe-bottom: env(safe-area-inset-bottom, 0);
  --safe-top: env(safe-area-inset-top, 0);
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

html, body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  font-size: 15px;
  overscroll-behavior: none;
}

body { padding-top: var(--safe-top); padding-bottom: var(--safe-bottom); }

#app { max-width: 480px; margin: 0 auto; padding: 12px; display: flex; flex-direction: column; gap: 12px; }

header, .sub-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 8px;
}
header h1 { margin: 0; font-size: 18px; font-weight: 600; }
.sub-header h2 { margin: 0; font-size: 16px; font-weight: 600; }

.icon-btn {
  background: transparent;
  border: none;
  color: var(--muted);
  font-size: 22px;
  cursor: pointer;
  line-height: 1;
  padding: 6px 10px;
  border-radius: 8px;
}
.icon-btn:hover, .icon-btn:active { background: var(--panel); color: var(--text); }

button, select, input, textarea {
  font: inherit;
  color: var(--text);
}

button {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 10px 14px;
  cursor: pointer;
  touch-action: manipulation;
}
button:active { background: #1f242d; }
button.primary {
  background: var(--accent);
  border-color: var(--accent);
  color: #06121f;
  font-weight: 600;
}
button.big { padding: 14px 22px; font-size: 16px; }
button:disabled { opacity: 0.6; }

input, textarea, select {
  width: 100%;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 10px 12px;
}
textarea { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 13px; }

label {
  display: flex;
  flex-direction: column;
  gap: 4px;
  margin-bottom: 10px;
  font-size: 13px;
  color: var(--muted);
}
label em { font-style: normal; color: var(--accent); font-size: 11px; }
label.row { flex-direction: row; align-items: center; }
label.row span { min-width: 140px; }

fieldset {
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 12px 14px;
  margin: 0;
  background: var(--panel);
}
legend { padding: 0 6px; font-size: 13px; color: var(--accent); font-weight: 600; }

.row { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.hint { color: var(--muted); font-size: 12px; line-height: 1.45; margin: 4px 0; }

#video-wrap {
  position: relative;
  width: 100%;
  aspect-ratio: 3 / 4;
  background: #000;
  border-radius: 12px;
  overflow: hidden;
  border: 1px solid var(--border);
}
#video {
  width: 100%;
  height: 100%;
  object-fit: cover;
}
#reticle {
  position: absolute;
  left: 12%;
  right: 12%;
  top: 38%;
  bottom: 38%;
  border: 2px dashed rgba(78, 161, 255, 0.75);
  border-radius: 8px;
  pointer-events: none;
  transition: border-color 0.15s, background 0.15s;
}
#reticle.flash-ok    { border-color: rgba(61, 220, 151, 1); background: rgba(61, 220, 151, 0.18); }
#reticle.flash-fail  { border-color: rgba(255, 92, 124, 1); background: rgba(255, 92, 124, 0.18); }
#reticle.armed       { border-color: rgba(255, 181, 71, 0.95); }

.status {
  font-size: 13px;
  padding: 8px 10px;
  border-radius: 8px;
  background: var(--panel);
  border: 1px solid var(--border);
  text-align: center;
}
.status.idle { color: var(--muted); }
.status.scan { color: var(--accent); }
.status.ok   { color: var(--ok); }
.status.warn { color: var(--warn); }
.status.err  { color: var(--err); }

.last {
  display: flex;
  gap: 8px;
  align-items: baseline;
  padding: 8px 12px;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 8px;
}
.last-code { font-weight: 700; font-size: 20px; letter-spacing: 2px; }
.last-meta { color: var(--muted); font-size: 12px; margin-left: auto; }

#controls {
  display: flex;
  gap: 10px;
  align-items: center;
}
.mode {
  display: flex;
  flex-direction: column;
  gap: 4px;
  flex: 1;
  margin: 0;
  color: var(--muted);
  font-size: 12px;
}
.mode select { padding: 10px 12px; }
#snap-btn { flex-shrink: 0; }

#confirm-bar {
  display: flex;
  gap: 6px;
  align-items: center;
  padding: 8px;
  background: var(--panel);
  border: 1px solid var(--accent);
  border-radius: 10px;
}
.confirm-label { color: var(--muted); font-size: 12px; white-space: nowrap; }
#confirm-input {
  flex: 1;
  font-weight: 700;
  letter-spacing: 2px;
  text-transform: uppercase;
  color: var(--accent);
}

#log-details summary { cursor: pointer; color: var(--muted); font-size: 13px; padding: 4px 0; }
#log { list-style: none; padding: 0; margin: 8px 0 0; max-height: 220px; overflow-y: auto; font-size: 13px; }
#log li {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 6px 8px;
  border-bottom: 1px solid var(--border);
}
#log li .code { font-weight: 600; letter-spacing: 1px; }
#log li .meta { color: var(--muted); font-size: 12px; }
#log li.ok .code   { color: var(--ok); }
#log li.fail .code { color: var(--err); }

.hidden { display: none !important; }
.bookmarklet-help { margin-top: 8px; }
.bookmarklet-help summary { cursor: pointer; color: var(--muted); font-size: 12px; }
#bookmarklet {
  display: inline-block;
  background: var(--bg);
  border: 1px solid var(--border);
  padding: 8px 12px;
  border-radius: 8px;
  color: var(--accent);
  text-decoration: none;
  font-weight: 600;
  margin-top: 6px;
}

/* QR modal */
.modal {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.75);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 50;
  padding: 12px;
}
.modal-card {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 14px;
  width: 100%;
  max-width: 420px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}
.modal-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.modal-header h2 { margin: 0; font-size: 16px; }
.qr-pane { display: flex; flex-direction: column; gap: 8px; align-items: center; }
.qr-pane.hidden { display: none; }
#qr-canvas-wrap {
  background: #fff;
  padding: 16px;
  border-radius: 10px;
  width: 100%;
  max-width: 340px;
  display: flex;
  justify-content: center;
}
#qr-canvas-wrap img, #qr-canvas-wrap canvas { width: 100%; height: auto; image-rendering: pixelated; display: block; }
#qr-video {
  width: 100%;
  max-width: 340px;
  aspect-ratio: 1 / 1;
  background: #000;
  border-radius: 10px;
  object-fit: cover;
}
