:root {
  --bg: #07090d;
  --surface: rgba(18, 22, 30, 0.82);
  --surface-solid: #11151d;
  --line: rgba(255, 255, 255, 0.1);
  --text: #eef2f7;
  --muted: #8b95a5;
  --grab: #22d3ee;
  --drop: #f472b6;
  --accent: var(--grab);
  --ok: #34d399;
  --warn: #fbbf24;
  --bad: #f87171;
  --safe-t: env(safe-area-inset-top, 0px);
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-l: env(safe-area-inset-left, 0px);
  --safe-r: env(safe-area-inset-right, 0px);
  --font: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}
body[data-mode="drop"] { --accent: var(--drop); }

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; height: 100%; background: var(--bg); color: var(--text); font-family: var(--font); overflow: hidden; overscroll-behavior: none; touch-action: manipulation; -webkit-user-select: none; user-select: none; }
button { font: inherit; color: inherit; border: 0; background: none; cursor: pointer; }
[hidden] { display: none !important; }
.muted { color: var(--muted); }
.small { font-size: 13px; }
.error { color: var(--bad); }

/* Viewfinder */
#viewfinder { position: fixed; inset: 0; background: #000; overflow: hidden; }
#video { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: cover; background: #05070a; }
#aim { position: absolute; inset: 0; width: 100%; height: 100%; pointer-events: none; }
#crosshair { position: absolute; left: 50%; top: 50%; width: 80px; height: 80px; margin: -40px 0 0 -40px; color: var(--accent); pointer-events: none; filter: drop-shadow(0 0 6px color-mix(in srgb, var(--accent) 70%, transparent)) drop-shadow(0 0 1px #000); transition: color .2s, transform .15s; }
#crosshair.locked { transform: scale(0.82); }
#viewfinder.flash::after { content: ""; position: absolute; inset: 0; background: #fff; opacity: 0; animation: flash .25s ease-out; }
@keyframes flash { 0% { opacity: .55; } 100% { opacity: 0; } }

/* Top bar */
#topbar { position: fixed; top: 0; left: 0; right: 0; padding: calc(var(--safe-t) + 10px) calc(var(--safe-r) + 12px) 0 calc(var(--safe-l) + 12px); display: flex; justify-content: space-between; align-items: flex-start; gap: 8px; pointer-events: none; z-index: 5; }
.pill, .aimstate { display: inline-flex; align-items: center; gap: 8px; height: 32px; padding: 0 12px; border-radius: 16px; background: var(--surface); border: 1px solid var(--line); backdrop-filter: blur(14px); -webkit-backdrop-filter: blur(14px); font-size: 13px; font-weight: 500; max-width: 60vw; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.dot { width: 8px; height: 8px; border-radius: 50%; background: var(--muted); flex: none; }
.pill[data-state="connected"] .dot { background: var(--ok); box-shadow: 0 0 8px var(--ok); }
.pill[data-state="connecting"] .dot, .pill[data-state="reconnecting"] .dot { background: var(--warn); animation: pulse 1s infinite; }
.pill[data-state="expired"] .dot { background: var(--bad); }
@keyframes pulse { 50% { opacity: .3; } }
.aimstate { color: var(--accent); border-color: color-mix(in srgb, var(--accent) 40%, transparent); }
.aimstate.locked { background: color-mix(in srgb, var(--accent) 22%, var(--surface-solid)); color: #fff; }

.banner { position: fixed; z-index: 6; left: 50%; transform: translateX(-50%); top: calc(var(--safe-t) + 54px); max-width: calc(100vw - 32px); padding: 10px 16px; border-radius: 14px; background: var(--surface-solid); border: 1px solid var(--line); font-size: 14px; text-align: center; box-shadow: 0 8px 30px rgba(0,0,0,.5); }
.banner.bad { border-color: color-mix(in srgb, var(--bad) 60%, transparent); }
.banner.warn { border-color: color-mix(in srgb, var(--warn) 60%, transparent); }
.banner button { margin-left: 10px; padding: 4px 12px; border-radius: 10px; background: rgba(255,255,255,.12); }

/* Controls */
#controls { position: fixed; left: 0; right: 0; bottom: 0; padding: 12px calc(var(--safe-r) + 16px) calc(var(--safe-b) + 18px) calc(var(--safe-l) + 16px); display: flex; flex-direction: column; align-items: center; gap: 14px; background: linear-gradient(to top, rgba(0,0,0,.75), rgba(0,0,0,0)); z-index: 5; pointer-events: none; }
#controls > * { pointer-events: auto; }
.row { display: flex; justify-content: center; }
.seg { display: inline-flex; padding: 4px; border-radius: 999px; background: var(--surface); border: 1px solid var(--line); backdrop-filter: blur(14px); -webkit-backdrop-filter: blur(14px); }
.seg button { min-width: 96px; height: 44px; padding: 0 20px; border-radius: 999px; font-size: 16px; font-weight: 600; color: var(--muted); transition: background .2s, color .2s; }
.seg button[aria-selected="true"] { color: #05070a; background: var(--accent); box-shadow: 0 0 18px color-mix(in srgb, var(--accent) 50%, transparent); }
#mode-drop[aria-selected="true"] { background: var(--drop); box-shadow: 0 0 18px color-mix(in srgb, var(--drop) 50%, transparent); }
#mode-grab[aria-selected="true"] { background: var(--grab); }

#shutter { position: relative; width: 84px; height: 84px; border-radius: 50%; flex: none; }
#shutter .ring { position: absolute; inset: 0; border-radius: 50%; border: 4px solid var(--accent); box-shadow: 0 0 22px color-mix(in srgb, var(--accent) 45%, transparent); transition: border-color .2s; }
#shutter .core { position: absolute; inset: 10px; border-radius: 50%; background: var(--accent); transition: transform .12s, background .2s, opacity .2s; }
#shutter:active .core { transform: scale(.86); }
#shutter:disabled { opacity: .45; }

.filebar { display: flex; gap: 8px; align-items: center; justify-content: center; flex-wrap: wrap; max-width: 100%; }
.chip-btn { display: inline-flex; align-items: center; gap: 8px; height: 44px; padding: 0 18px; border-radius: 999px; background: var(--surface); border: 1px solid color-mix(in srgb, var(--drop) 55%, transparent); color: var(--drop); font-weight: 600; font-size: 15px; backdrop-filter: blur(14px); -webkit-backdrop-filter: blur(14px); }
.filechip { display: inline-flex; align-items: center; gap: 8px; height: 44px; padding: 0 6px 0 16px; border-radius: 999px; background: var(--surface); border: 1px solid var(--line); font-size: 14px; max-width: 70vw; }
.filechip #filename { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; max-width: 36vw; }
.filechip button { width: 34px; height: 34px; border-radius: 50%; font-size: 22px; line-height: 1; color: var(--muted); }

/* Screens */
.screen { position: fixed; inset: 0; z-index: 20; display: flex; align-items: center; justify-content: center; padding: calc(var(--safe-t) + 24px) calc(var(--safe-r) + 24px) calc(var(--safe-b) + 24px) calc(var(--safe-l) + 24px); background: radial-gradient(120% 80% at 50% 0%, #10202a 0%, var(--bg) 60%); }
.card { width: min(420px, 100%); text-align: center; display: flex; flex-direction: column; align-items: center; gap: 14px; }
.card h1 { margin: 4px 0 0; font-size: 34px; letter-spacing: -.02em; }
.lead { margin: 0; color: #b8c1cf; line-height: 1.5; font-size: 16px; }
.logo { border-radius: 18px; box-shadow: 0 8px 40px rgba(34, 211, 238, .25); }
.primary { width: 100%; height: 58px; border-radius: 18px; background: linear-gradient(135deg, #22d3ee, #6366f1); color: #05070a; font-weight: 700; font-size: 18px; margin-top: 10px; box-shadow: 0 10px 30px rgba(34, 211, 238, .25); }
.primary:active { transform: scale(.98); }
.primary:disabled { opacity: .5; }
.ghost { height: 52px; width: 100%; border-radius: 16px; background: rgba(255,255,255,.07); font-weight: 600; font-size: 16px; }

/* Progress */
.progress { position: fixed; z-index: 15; left: 50%; transform: translateX(-50%); bottom: calc(var(--safe-b) + 150px); width: min(360px, calc(100vw - 32px)); padding: 12px 16px 14px; border-radius: 16px; background: var(--surface-solid); border: 1px solid var(--line); box-shadow: 0 8px 30px rgba(0,0,0,.5); }
.progress-label { font-size: 14px; margin-bottom: 8px; }
.bar { height: 6px; border-radius: 3px; background: rgba(255,255,255,.1); overflow: hidden; }
#progress-fill { height: 100%; width: 0%; background: var(--accent); border-radius: 3px; transition: width .12s linear; }
#progress-fill.indeterminate { width: 40%; animation: slide 1s infinite ease-in-out; }
@keyframes slide { 0% { margin-left: -40%; } 100% { margin-left: 100%; } }

/* Sheet */
#sheet-backdrop { position: fixed; inset: 0; z-index: 30; background: rgba(0,0,0,.5); animation: fade .2s; }
#sheet { position: fixed; z-index: 31; left: 0; right: 0; bottom: 0; max-height: 82vh; overflow-y: auto; padding: 10px calc(var(--safe-r) + 18px) calc(var(--safe-b) + 18px) calc(var(--safe-l) + 18px); border-radius: 26px 26px 0 0; background: var(--surface-solid); border-top: 1px solid var(--line); box-shadow: 0 -20px 60px rgba(0,0,0,.6); animation: up .28s cubic-bezier(.2,.9,.3,1); -webkit-user-select: text; user-select: text; }
@keyframes up { from { transform: translateY(100%); } to { transform: none; } }
@keyframes fade { from { opacity: 0; } }
.grip { width: 40px; height: 4px; border-radius: 2px; background: rgba(255,255,255,.2); margin: 0 auto 14px; }
#sheet-body { display: flex; flex-direction: column; gap: 10px; align-items: stretch; }
#sheet-body h2 { margin: 0; font-size: 20px; word-break: break-word; }
#sheet-body .meta { color: var(--muted); font-size: 13px; }
#sheet-body img.preview { width: 100%; max-height: 42vh; object-fit: contain; border-radius: 14px; background: #05070a; border: 1px solid var(--line); }
#sheet-body pre { margin: 0; padding: 12px; border-radius: 12px; background: #05070a; border: 1px solid var(--line); white-space: pre-wrap; word-break: break-word; max-height: 30vh; overflow: auto; font: 14px ui-monospace, Menlo, Consolas, monospace; }
#sheet-body .url { color: var(--grab); word-break: break-all; font-size: 14px; }
.actions { display: flex; gap: 10px; margin: 14px 0 10px; }
.actions .btn { flex: 1; height: 54px; border-radius: 16px; background: var(--accent); color: #05070a; font-weight: 700; font-size: 17px; display: inline-flex; align-items: center; justify-content: center; text-decoration: none; }
.actions .btn.secondary { background: rgba(255,255,255,.09); color: var(--text); }
.badge-ok { color: var(--ok); }
.badge-bad { color: var(--bad); }

/* Toasts */
#toasts { position: fixed; z-index: 50; left: 0; right: 0; top: calc(var(--safe-t) + 54px); display: flex; flex-direction: column; align-items: center; gap: 8px; pointer-events: none; padding: 0 16px; }
.toast { max-width: 440px; padding: 12px 18px; border-radius: 14px; background: #1b2230; border: 1px solid var(--line); box-shadow: 0 10px 34px rgba(0,0,0,.55); font-size: 15px; text-align: center; animation: toast-in .22s ease-out; }
.toast.bad { border-color: color-mix(in srgb, var(--bad) 55%, transparent); }
.toast.ok { border-color: color-mix(in srgb, var(--ok) 55%, transparent); }
.toast.out { opacity: 0; transform: translateY(-6px); transition: all .25s; }
@keyframes toast-in { from { opacity: 0; transform: translateY(-10px); } }

@media (orientation: landscape) and (max-height: 520px) {
  #controls { flex-direction: row; justify-content: space-between; align-items: center; padding-bottom: calc(var(--safe-b) + 10px); }
  #shutter { width: 68px; height: 68px; }
  .filebar { order: -1; }
}
@media (prefers-reduced-motion: reduce) { * { animation-duration: .01ms !important; transition-duration: .01ms !important; } }
