:root,
[data-theme="dark"] {
  color-scheme: dark;
  --bg: #0b1120;
  --bg-elevated: #111827;
  --surface: #1e293b;
  --surface-subtle: #0f172a;
  --border: #334155;
  --border-strong: #475569;
  --text: #f8fafc;
  --text-secondary: #cbd5e1;
  --text-muted: #94a3b8;
  --primary: #3b82f6;
  --primary-hover: #2563eb;
  --accent: #0ea5e9;
  --accent-hover: #0284c7;
  --danger: #ef4444;
  --danger-hover: #dc2626;
  --success-bg: rgba(16, 185, 129, 0.16);
  --success-text: #34d399;
  --error-bg: rgba(239, 68, 68, 0.16);
  --error-border: rgba(239, 68, 68, 0.4);
  --error-text: #fca5a5;
  --info-bg: rgba(59, 130, 246, 0.16);
  --info-border: rgba(59, 130, 246, 0.4);
  --info-text: #93c5fd;
  --topbar-bg: rgba(11, 17, 32, 0.9);
  --focus-ring: #60a5fa;
  --shadow: 0 8px 24px rgba(0, 0, 0, 0.32);
}

[data-theme="light"] {
  color-scheme: light;
  --bg: #f1f5f9;
  --bg-elevated: #ffffff;
  --surface: #ffffff;
  --surface-subtle: #f8fafc;
  --border: #e2e8f0;
  --border-strong: #cbd5e1;
  --text: #0f172a;
  --text-secondary: #334155;
  --text-muted: #64748b;
  --primary: #2563eb;
  --primary-hover: #1d4ed8;
  --accent: #0284c7;
  --accent-hover: #0369a1;
  --danger: #dc2626;
  --danger-hover: #b91c1c;
  --success-bg: #dcfce7;
  --success-text: #15803d;
  --error-bg: #fee2e2;
  --error-border: #fca5a5;
  --error-text: #b91c1c;
  --info-bg: #dbeafe;
  --info-border: #93c5fd;
  --info-text: #1d4ed8;
  --topbar-bg: rgba(255, 255, 255, 0.92);
  --focus-ring: #2563eb;
  --shadow: 0 6px 20px rgba(15, 23, 42, 0.06);
}

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

body {
  font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  background-color: var(--bg);
  color: var(--text);
  min-height: 100vh;
  line-height: 1.45;
  overflow-x: hidden;
}

:focus-visible {
  outline: 2px solid var(--focus-ring);
  outline-offset: 2px;
}

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

.skip-link {
  position: absolute;
  left: -9999px;
  top: 8px;
  background-color: var(--primary);
  color: #ffffff;
  padding: 8px 14px;
  border-radius: 8px;
  font-weight: 600;
  z-index: 100;
}

.skip-link:focus {
  left: 12px;
}

.app {
  width: 100%;
  max-width: 680px;
  margin: 0 auto;
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

.topbar {
  position: sticky;
  top: 0;
  z-index: 20;
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
  padding: calc(12px + env(safe-area-inset-top)) 16px 12px;
  background-color: var(--topbar-bg);
  backdrop-filter: blur(10px);
  border-bottom: 1px solid var(--border);
}

.brand {
  display: flex;
  align-items: center;
  gap: 10px;
  min-width: 0;
}

.brand-badge {
  background-color: var(--primary);
  color: #ffffff;
  font-size: 0.72rem;
  font-weight: 800;
  letter-spacing: 0.06em;
  padding: 4px 8px;
  border-radius: 6px;
  flex-shrink: 0;
}

.brand h1 {
  font-size: 1.05rem;
  font-weight: 700;
  text-wrap: balance;
}

.topbar-actions {
  display: flex;
  align-items: center;
  gap: 8px;
}

.btn-icon {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 38px;
  height: 38px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background-color: var(--surface);
  color: var(--text);
  cursor: pointer;
  touch-action: manipulation;
  transition: background-color 0.15s ease, border-color 0.15s ease;
}

.btn-icon:hover {
  border-color: var(--primary);
}

[data-theme="dark"] .icon-moon {
  display: none;
}

[data-theme="light"] .icon-sun {
  display: none;
}

.topbar-link {
  color: var(--primary);
  text-decoration: none;
  font-size: 0.84rem;
  font-weight: 600;
  padding: 8px 12px;
  border: 1px solid var(--border);
  background-color: var(--surface);
  border-radius: 10px;
  touch-action: manipulation;
  transition: border-color 0.15s ease;
}

.topbar-link:hover {
  border-color: var(--primary);
}

.content {
  display: flex;
  flex-direction: column;
  gap: 16px;
  padding: 16px 16px calc(24px + env(safe-area-inset-bottom));
  flex: 1;
}

.card {
  background-color: var(--surface);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 18px;
  box-shadow: var(--shadow);
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.section-head,
.subsection-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 10px;
}

.section-head h2,
.subsection h3 {
  font-size: 1rem;
  font-weight: 700;
  color: var(--text);
  text-wrap: balance;
}

.status-badge {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 5px 10px;
  border-radius: 9999px;
  font-size: 0.78rem;
  font-weight: 600;
  background-color: var(--surface-subtle);
  border: 1px solid var(--border);
  color: var(--text-secondary);
}

.status-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background-color: var(--text-muted);
}

.status-badge.active {
  background-color: var(--success-bg);
  border-color: transparent;
  color: var(--success-text);
}

.status-badge.active .status-dot {
  background-color: var(--success-text);
}

.status-badge.error {
  background-color: var(--error-bg);
  border-color: transparent;
  color: var(--error-text);
}

.status-badge.error .status-dot {
  background-color: var(--error-text);
}

.action-row {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
}

.btn {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 46px;
  padding: 10px 16px;
  border: 1px solid transparent;
  border-radius: 10px;
  font-size: 0.94rem;
  font-weight: 600;
  cursor: pointer;
  text-decoration: none;
  touch-action: manipulation;
  transition: transform 0.1s ease, background-color 0.15s ease, border-color 0.15s ease, opacity 0.15s ease;
}

.btn:active:not(:disabled) {
  transform: scale(0.98);
}

.btn:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.btn-sm {
  min-height: 38px;
  padding: 7px 13px;
  font-size: 0.84rem;
}

.btn-primary {
  background-color: var(--primary);
  color: #ffffff;
  flex: 1 1 130px;
}

.btn-primary:hover:not(:disabled) {
  background-color: var(--primary-hover);
}

.btn-accent {
  background-color: var(--accent);
  color: #ffffff;
  flex: 1 1 130px;
}

.btn-accent:hover:not(:disabled) {
  background-color: var(--accent-hover);
}

.btn-secondary {
  background-color: var(--surface-subtle);
  border-color: var(--border);
  color: var(--text);
}

.btn-secondary:hover:not(:disabled) {
  border-color: var(--primary);
}

.btn-danger {
  background-color: var(--danger);
  color: #ffffff;
}

.btn-danger:hover:not(:disabled) {
  background-color: var(--danger-hover);
}

.camera-box {
  position: relative;
  width: 100%;
  height: 280px;
  background-color: #020617;
  border-radius: 12px;
  overflow: hidden;
  border: 1px solid var(--border);
  display: flex;
  justify-content: center;
  align-items: center;
}

.camera-video {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.scan-frame {
  position: absolute;
  width: 190px;
  height: 190px;
  border: 2px dashed #60a5fa;
  border-radius: 14px;
  box-shadow: 0 0 0 9999px rgba(2, 6, 23, 0.45);
  pointer-events: none;
}

.camera-controls {
  position: absolute;
  bottom: 10px;
  left: 10px;
  right: 10px;
  display: flex;
  justify-content: space-between;
  gap: 8px;
}

.hint {
  font-size: 0.82rem;
  color: var(--text-muted);
}

.alert {
  padding: 12px 14px;
  border-radius: 12px;
  font-size: 0.9rem;
  background-color: var(--error-bg);
  border: 1px solid var(--error-border);
  color: var(--error-text);
}

.alert.info {
  background-color: var(--info-bg);
  border-color: var(--info-border);
  color: var(--info-text);
}

.hidden {
  display: none !important;
}

.device-header {
  display: flex;
  flex-direction: column;
  gap: 8px;
  border-bottom: 1px solid var(--border);
  padding-bottom: 14px;
}

.device-title-wrap {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 6px;
}

.device-title-wrap h2 {
  font-size: 1.25rem;
  font-weight: 700;
  text-wrap: balance;
}

.code-pill {
  font-family: "Cascadia Code", Consolas, monospace;
  font-size: 0.84rem;
  font-weight: 700;
  color: var(--info-text);
  background-color: var(--info-bg);
  border: 1px solid var(--info-border);
  padding: 4px 10px;
  border-radius: 9999px;
}

.device-location {
  font-size: 0.92rem;
  color: var(--text-secondary);
  font-weight: 600;
}

.device-description {
  font-size: 0.9rem;
  color: var(--text-muted);
}

.device-meta {
  font-size: 0.78rem;
  color: var(--text-muted);
  font-variant-numeric: tabular-nums;
}

.subsection {
  display: flex;
  flex-direction: column;
  gap: 10px;
  padding-top: 6px;
}

.gps-box {
  padding: 12px;
  background-color: var(--surface-subtle);
  border: 1px solid var(--border);
  border-radius: 12px;
}

.coords-pill {
  font-family: "Cascadia Code", Consolas, monospace;
  font-size: 0.82rem;
  font-weight: 600;
  font-variant-numeric: tabular-nums;
  color: var(--text-secondary);
  background-color: var(--surface);
  border: 1px solid var(--border);
  padding: 3px 9px;
  border-radius: 8px;
}

.count-pill {
  background-color: var(--surface-subtle);
  border: 1px solid var(--border);
  color: var(--text-secondary);
  font-size: 0.78rem;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  padding: 2px 9px;
  border-radius: 9999px;
}

.documents-list {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.doc-item {
  display: flex;
  flex-direction: column;
  gap: 10px;
  padding: 12px;
  background-color: var(--surface-subtle);
  border: 1px solid var(--border);
  border-radius: 12px;
}

.doc-item.schematic-card {
  border-color: var(--info-border);
  background-color: var(--info-bg);
}

.doc-top {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 10px;
}

.doc-info {
  display: flex;
  flex-direction: column;
  gap: 3px;
  min-width: 0;
  flex: 1;
}

.doc-title {
  font-size: 0.95rem;
  font-weight: 700;
  color: var(--text);
  word-break: break-word;
}

.doc-meta {
  font-size: 0.8rem;
  color: var(--text-muted);
  font-variant-numeric: tabular-nums;
  word-break: break-word;
}

.doc-cat {
  font-size: 0.72rem;
  text-transform: uppercase;
  font-weight: 700;
  padding: 3px 8px;
  border-radius: 6px;
  background-color: var(--surface);
  border: 1px solid var(--border);
  color: var(--text-secondary);
  flex-shrink: 0;
}

.doc-actions {
  display: flex;
  gap: 8px;
}

.doc-actions .btn {
  flex: 1;
  min-height: 40px;
  font-size: 0.86rem;
}

.gallery-list {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
}

.gallery-thumb-btn {
  display: flex;
  flex-direction: column;
  flex: 1 1 140px;
  max-width: calc(50% - 5px);
  background-color: var(--surface-subtle);
  border: 1px solid var(--border);
  border-radius: 12px;
  overflow: hidden;
  padding: 0;
  cursor: pointer;
  text-align: left;
  color: var(--text);
  touch-action: manipulation;
  transition: transform 0.15s ease, border-color 0.15s ease;
}

.gallery-thumb-btn:hover {
  border-color: var(--primary);
}

.gallery-thumb-btn:active {
  transform: scale(0.98);
}

.gallery-thumb-img {
  width: 100%;
  height: 130px;
  object-fit: cover;
  display: block;
  background-color: var(--bg);
}

.gallery-thumb-caption {
  padding: 8px 10px;
  font-size: 0.8rem;
  font-weight: 600;
  color: var(--text-secondary);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.attributes-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.attr-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
  padding: 10px 12px;
  background-color: var(--surface-subtle);
  border: 1px solid var(--border);
  border-radius: 10px;
}

.attr-label {
  font-size: 0.86rem;
  color: var(--text-muted);
}

.attr-value {
  font-size: 0.92rem;
  font-weight: 600;
  color: var(--text);
  font-variant-numeric: tabular-nums;
  text-align: right;
  word-break: break-word;
}

.attr-link {
  color: var(--primary);
  text-decoration: underline;
}

.bool-pill {
  padding: 3px 10px;
  border-radius: 9999px;
  font-size: 0.78rem;
  font-weight: 700;
}

.bool-yes {
  background-color: var(--success-bg);
  color: var(--success-text);
}

.bool-no {
  background-color: var(--error-bg);
  color: var(--error-text);
}

.empty-text {
  font-size: 0.86rem;
  color: var(--text-muted);
}

.lightbox {
  position: fixed;
  inset: 0;
  z-index: 100;
  background-color: rgba(2, 6, 23, 0.94);
  display: flex;
  flex-direction: column;
  overscroll-behavior: contain;
  padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
}

.lightbox-bar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
  padding: 12px 16px;
  color: #f8fafc;
}

.lightbox-caption {
  font-size: 0.92rem;
  font-weight: 600;
  min-width: 0;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.lightbox-bar-actions {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-shrink: 0;
}

.lightbox-stage {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
  padding: 12px;
  min-height: 0;
}

.lightbox-img {
  max-width: 100%;
  max-height: 100%;
  width: auto;
  height: auto;
  object-fit: contain;
  border-radius: 8px;
}

.lightbox-nav {
  position: absolute;
  top: 50%;
  transform: translateY(-50%);
  width: 44px;
  height: 44px;
  border-radius: 50%;
  border: 1px solid rgba(255, 255, 255, 0.25);
  background-color: rgba(15, 23, 42, 0.75);
  color: #ffffff;
  font-size: 1.6rem;
  line-height: 1;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
}

#lightboxPrevBtn {
  left: 12px;
}

#lightboxNextBtn {
  right: 12px;
}

@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    transition-duration: 0.01ms !important;
    animation-duration: 0.01ms !important;
  }
}
