@import url('https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600;700&display=swap');

/* ===== CSS Variables ===== */
:root {
  --bg-dark: #0d1117;
  --bg-card: #161b22;
  --bg-card-hover: #1c2330;
  --bg-input: #0d1117;
  --border: #30363d;
  --border-active: #388bfd;
  --text-primary: #e6edf3;
  --text-secondary: #8b949e;
  --text-muted: #484f58;
  --accent: #3fb950;
  --accent-hover: #2ea043;
  --accent-bg: rgba(63, 185, 80, 0.12);
  --danger: #f85149;
  --danger-bg: rgba(248, 81, 73, 0.12);
  --warning: #d29922;
  --blue: #388bfd;
  --blue-bg: rgba(56, 139, 253, 0.12);
  --tag-bg: #21262d;
  --tag-selected-bg: rgba(63, 185, 80, 0.2);
  --tag-selected-border: #3fb950;
  --shadow: 0 8px 32px rgba(0, 0, 0, 0.4);
  --radius: 12px;
  --radius-sm: 8px;
  --nav-h: 64px;
}

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

html {
  scroll-behavior: smooth;
}

body {
  font-family: 'Inter', -apple-system, BlinkMacSystemFont, sans-serif;
  background: var(--bg-dark);
  color: var(--text-primary);
  min-height: 100vh;
  line-height: 1.6;
  -webkit-font-smoothing: antialiased;
}

/* ===== Navigation ===== */
.nav {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  height: var(--nav-h);
  background: rgba(13, 17, 23, 0.85);
  backdrop-filter: blur(16px);
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  padding: 0 24px;
  gap: 8px;
  z-index: 100;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  scrollbar-width: none;
}

.nav::-webkit-scrollbar {
  display: none;
}

.nav>* {
  flex-shrink: 0;
}

.nav-brand {
  font-size: 18px;
  font-weight: 700;
  color: var(--accent);
  margin-right: 16px;
  display: flex;
  align-items: center;
  gap: 8px;
  text-decoration: none;
}

.nav-link {
  padding: 6px 16px;
  border-radius: 20px;
  color: var(--text-secondary);
  text-decoration: none;
  font-size: 14px;
  font-weight: 500;
  transition: all 0.2s;
}

.nav-link:hover {
  color: var(--text-primary);
  background: var(--tag-bg);
}

.nav-link.active {
  background: var(--accent-bg);
  color: var(--accent);
}

/* ===== Page Layout ===== */
.page {
  padding-top: calc(var(--nav-h) + 32px);
  padding-bottom: 60px;
  min-height: 100vh;
}

.container {
  max-width: 900px;
  margin: 0 auto;
  padding: 0 24px;
}

/* ===== Section / Card ===== */
.section-title {
  font-size: 13px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--text-muted);
  margin-bottom: 12px;
}

.card {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 24px;
  margin-bottom: 20px;
  transition: border-color 0.2s;
}

.card:hover {
  border-color: #444c56;
}

/* ===== Tags / Labels ===== */
.tags {
  display: flex;
  flex-wrap: wrap;
  gap: 12px;
}

.tag {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 14px 24px;
  border-radius: 36px;
  border: 2px solid var(--border);
  background: var(--tag-bg);
  color: var(--text-secondary);
  font-size: 20px;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.18s;
  user-select: none;
  min-height: 56px;
}

.tag:hover {
  border-color: var(--accent);
  color: var(--text-primary);
  background: rgba(63, 185, 80, 0.08);
}

.tag.selected {
  border-color: var(--tag-selected-border);
  background: var(--tag-selected-bg);
  color: var(--accent);
}

.tag .del-btn {
  background: none;
  border: none;
  color: var(--text-muted);
  cursor: pointer;
  font-size: 16px;
  line-height: 1;
  padding: 0 2px;
  transition: color 0.15s;
  display: flex;
  align-items: center;
}

.tag .del-btn:hover {
  color: var(--danger);
}

/* ===== Form Controls ===== */
.form-row {
  display: flex;
  gap: 16px;
  flex-wrap: wrap;
}

.form-group {
  flex: 1;
  min-width: 160px;
}

.form-group label {
  display: block;
  font-size: 13px;
  font-weight: 500;
  color: var(--text-secondary);
  margin-bottom: 8px;
}

.form-control {
  width: 100%;
  padding: 10px 14px;
  background: var(--bg-input);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  color: var(--text-primary);
  font-family: inherit;
  font-size: 15px;
  transition: border-color 0.2s, box-shadow 0.2s;
  outline: none;
  appearance: none;
}

.form-control:focus {
  border-color: var(--border-active);
  box-shadow: 0 0 0 3px rgba(56, 139, 253, 0.15);
}

.form-control::placeholder {
  color: var(--text-muted);
}

/* ===== Buttons ===== */
.btn {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 10px 22px;
  border-radius: var(--radius-sm);
  font-family: inherit;
  font-size: 15px;
  font-weight: 600;
  cursor: pointer;
  border: none;
  transition: all 0.18s;
  outline: none;
  white-space: nowrap;
}

.btn-primary {
  background: var(--accent);
  color: #fff;
}

.btn-primary:hover {
  background: var(--accent-hover);
  transform: translateY(-1px);
}

.btn-primary:active {
  transform: translateY(0);
}

.btn-primary:disabled {
  opacity: 0.4;
  cursor: not-allowed;
  transform: none;
}

.btn-secondary {
  background: var(--tag-bg);
  color: var(--text-primary);
  border: 1px solid var(--border);
}

.btn-secondary:hover {
  background: var(--bg-card-hover);
  border-color: #444c56;
}

.btn-danger {
  background: var(--danger-bg);
  color: var(--danger);
  border: 1px solid var(--danger);
}

.btn-danger:hover {
  background: rgba(248, 81, 73, 0.22);
}

.btn-blue {
  background: var(--blue);
  color: #fff;
}

.btn-blue:hover {
  background: #2f7be8;
  transform: translateY(-1px);
}

/* ===== Camera Modal ===== */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.8);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.25s;
  padding: 20px;
}

.modal-overlay.open {
  opacity: 1;
  pointer-events: all;
}

.modal {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 28px;
  width: 100%;
  max-width: 520px;
  transform: scale(0.95) translateY(20px);
  transition: transform 0.25s;
  box-shadow: var(--shadow);
}

.modal-overlay.open .modal {
  transform: scale(1) translateY(0);
}

.modal-title {
  font-size: 18px;
  font-weight: 700;
  margin-bottom: 20px;
  display: flex;
  align-items: center;
  gap: 10px;
}

.video-wrap {
  position: relative;
  width: 100%;
  aspect-ratio: 4/3;
  border-radius: var(--radius-sm);
  overflow: hidden;
  background: #000;
  margin-bottom: 16px;
}

#cameraVideo {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.video-overlay {
  position: absolute;
  inset: 0;
  border: 2px solid var(--accent);
  border-radius: var(--radius-sm);
  pointer-events: none;
}

.video-overlay::before,
.video-overlay::after {
  content: '';
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  border: 1px solid rgba(63, 185, 80, 0.3);
  border-radius: 50%;
}

.video-overlay::before {
  width: 80px;
  height: 80px;
}

.video-overlay::after {
  width: 40px;
  height: 40px;
}

.modal-actions {
  display: flex;
  gap: 12px;
  justify-content: flex-end;
}

canvas#snapshotCanvas {
  display: none;
}

/* ===== Toast ===== */
.toast-wrap {
  position: fixed;
  bottom: 28px;
  right: 28px;
  z-index: 2000;
  display: flex;
  flex-direction: column;
  gap: 10px;
  pointer-events: none;
}

.toast {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 14px 20px;
  border-radius: var(--radius-sm);
  background: var(--bg-card);
  border: 1px solid var(--border);
  box-shadow: var(--shadow);
  color: var(--text-primary);
  font-size: 14px;
  font-weight: 500;
  min-width: 240px;
  animation: slideIn 0.3s ease forwards;
}

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

.toast.error {
  border-color: var(--danger);
}

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

.toast.error .toast-icon {
  color: var(--danger);
}

@keyframes slideIn {
  from {
    opacity: 0;
    transform: translateX(30px);
  }

  to {
    opacity: 1;
    transform: translateX(0);
  }
}

@keyframes fadeOut {
  from {
    opacity: 1;
  }

  to {
    opacity: 0;
  }
}

/* ===== Record Cards (Query Page) ===== */
.records-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
  gap: 16px;
}

.record-card {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
  transition: all 0.2s;
  cursor: default;
}

.record-card:hover {
  border-color: #444c56;
  transform: translateY(-2px);
  box-shadow: var(--shadow);
}

.record-img {
  width: 100%;
  aspect-ratio: 4/3;
  object-fit: cover;
  display: block;
  background: var(--tag-bg);
  cursor: zoom-in;
  transition: opacity 0.2s;
}

.record-img:hover {
  opacity: 0.85;
}

.record-img-placeholder {
  width: 100%;
  aspect-ratio: 4/3;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--tag-bg);
  color: var(--text-muted);
  font-size: 36px;
}

.record-body {
  padding: 16px;
}

.record-label {
  font-size: 18px;
  font-weight: 700;
  margin-bottom: 10px;
  display: flex;
  align-items: center;
  gap: 8px;
}

.record-label-badge {
  font-size: 11px;
  padding: 2px 8px;
  border-radius: 12px;
  background: var(--accent-bg);
  color: var(--accent);
  font-weight: 600;
}

.record-meta {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
  font-size: 13px;
}

.record-meta-item {
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.record-meta-item .meta-key {
  color: var(--text-muted);
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.05em;
}

.record-meta-item .meta-val {
  color: var(--text-primary);
  font-weight: 600;
}

/* ===== Photo Lightbox ===== */
.lightbox {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.93);
  z-index: 1500;
  display: flex;
  align-items: center;
  justify-content: center;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.25s;
  padding: 24px;
}

.lightbox.open {
  opacity: 1;
  pointer-events: all;
}

.lightbox img {
  max-width: 100%;
  max-height: 90vh;
  object-fit: contain;
  border-radius: var(--radius-sm);
  transform: scale(0.95);
  transition: transform 0.25s;
}

.lightbox.open img {
  transform: scale(1);
}

.lightbox-close {
  position: absolute;
  top: 20px;
  right: 24px;
  background: none;
  border: none;
  color: #fff;
  font-size: 32px;
  cursor: pointer;
  opacity: 0.7;
  transition: opacity 0.15s;
}

.lightbox-close:hover {
  opacity: 1;
}

/* ===== Empty State ===== */
.empty-state {
  text-align: center;
  padding: 60px 20px;
  color: var(--text-muted);
}

.empty-state .empty-icon {
  font-size: 56px;
  margin-bottom: 16px;
}

.empty-state .empty-title {
  font-size: 18px;
  font-weight: 600;
  color: var(--text-secondary);
  margin-bottom: 8px;
}

.empty-state .empty-desc {
  font-size: 14px;
}

/* ===== Page Header ===== */
.page-header {
  margin-bottom: 32px;
}

.page-title {
  font-size: 28px;
  font-weight: 800;
  letter-spacing: -0.02em;
  margin-bottom: 6px;
}

.page-subtitle {
  color: var(--text-secondary);
  font-size: 15px;
}

/* ===== Stats Bar ===== */
.stats-bar {
  display: flex;
  gap: 16px;
  flex-wrap: wrap;
  margin-bottom: 28px;
}

.stat-chip {
  background: var(--tag-bg);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 8px 16px;
  font-size: 13px;
  color: var(--text-secondary);
  display: flex;
  align-items: center;
  gap: 6px;
}

.stat-chip strong {
  color: var(--text-primary);
  font-weight: 700;
}

/* ===== Settings ===== */
.add-label-row {
  display: flex;
  gap: 10px;
  margin-bottom: 20px;
}

.add-label-row .form-control {
  flex: 1;
}

/* ===== Responsive ===== */
@media (max-width: 600px) {
  .container {
    padding: 0 16px;
  }

  .form-row {
    flex-direction: column;
  }

  /* Fix Add User Form flex alignment on mobile */
  #addUserForm {
    align-items: stretch !important;
  }

  #addUserForm .form-group {
    flex: 1 1 auto !important;
  }

  .records-grid {
    grid-template-columns: 1fr;
  }

  .modal {
    padding: 20px;
  }

  .nav {
    padding: 0 16px;
    gap: 4px;
  }

  .nav-brand {
    font-size: 16px;
    margin-right: 8px;
  }

  .nav-link {
    padding: 6px 12px;
    font-size: 13px;
  }

  .user-item {
    flex-direction: column;
    align-items: flex-start;
    gap: 12px;
  }

  .user-actions {
    width: 100%;
    justify-content: flex-end;
  }
}

@media (max-width: 768px) {
  .nav {
    padding: 0 16px;
  }
}

/* ===== Login & User Management ===== */
.login-body {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 100vh;
  padding: 20px;
}

.login-container {
  width: 100%;
  max-width: 400px;
}

.login-card {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 32px;
  box-shadow: var(--shadow);
}

.login-header {
  text-align: center;
  margin-bottom: 24px;
}

.login-icon {
  font-size: 48px;
  margin-bottom: 12px;
}

.login-header h1 {
  font-size: 24px;
  margin-bottom: 8px;
}

.login-header p {
  color: var(--text-secondary);
  font-size: 14px;
}

.login-form .form-group {
  margin-bottom: 16px;
}

.login-error {
  color: var(--danger);
  background: var(--danger-bg);
  padding: 10px;
  border-radius: var(--radius-sm);
  font-size: 13px;
  margin-bottom: 16px;
  text-align: center;
  border: 1px solid rgba(248, 81, 73, 0.3);
}

.login-error.hidden {
  display: none;
}

.btn-block {
  width: 100%;
  justify-content: center;
}

@keyframes shake {

  0%,
  100% {
    transform: translateX(0);
  }

  10%,
  30%,
  50%,
  70%,
  90% {
    transform: translateX(-5px);
  }

  20%,
  40%,
  60%,
  80% {
    transform: translateX(5px);
  }
}

.shake {
  animation: shake 0.4s cubic-bezier(.36, .07, .19, .97) both;
}

.users-list {
  display: flex;
  flex-direction: column;
  gap: 12px;
  margin-top: 20px;
}

.user-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 16px;
  background: var(--bg-dark);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
}

.user-info {
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.user-name {
  font-weight: 600;
  font-size: 16px;
  display: flex;
  align-items: center;
  gap: 8px;
}

.role-badge {
  font-size: 11px;
  padding: 2px 8px;
  border-radius: 12px;
  font-weight: 600;
}

.role-admin {
  background: rgba(210, 153, 34, 0.15);
  color: var(--warning);
}

.role-user {
  background: rgba(88, 166, 255, 0.15);
  color: #58a6ff;
}

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

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

.nav-right {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-left: auto;
}