:root {
  --primary: #2563eb;
  --primary-hover: #1d4ed8;
  --success: #16a34a;
  --success-hover: #15803d;
  --danger: #dc2626;
  --danger-hover: #b91c1c;
  --warning: #d97706;
  --bg-page: #f8fafc;
  --bg-card: #ffffff;
  --text-main: #0f172a;
  --text-muted: #64748b;
  --border: #e2e8f0;
  --border-focus: #93c5fd;
  --radius-lg: 12px;
  --radius-md: 8px;
  --radius-sm: 6px;
  --shadow-sm: 0 1px 2px 0 rgb(0 0 0 / 0.05);
  --shadow-md: 0 4px 6px -1px rgb(0 0 0 / 0.1), 0 2px 4px -2px rgb(0 0 0 / 0.1);
  --shadow-lg: 0 10px 15px -3px rgb(0 0 0 / 0.1), 0 4px 6px -4px rgb(0 0 0 / 0.1);
  --font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
}

* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

body {
  font-family: var(--font-family);
  background-color: var(--bg-page);
  color: var(--text-main);
  line-height: 1.5;
  padding: 16px;
  -webkit-font-smoothing: antialiased;
}

.app-container {
  max-width: 860px;
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  gap: 20px;
}

/* Header */
.app-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  flex-wrap: wrap;
  gap: 12px;
  padding: 16px 20px;
  background: var(--bg-card);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-sm);
  border: 1px solid var(--border);
}

.logo-area {
  display: flex;
  align-items: center;
  gap: 12px;
}

.logo-area .icon {
  font-size: 2.2rem;
}

.app-header h1 {
  font-size: 1.35rem;
  font-weight: 700;
  color: var(--text-main);
}

.subtitle {
  font-size: 0.875rem;
  color: var(--text-muted);
}

.status-badge {
  font-size: 0.75rem;
  padding: 4px 10px;
  border-radius: 9999px;
  font-weight: 600;
}

.status-badge.connected {
  background-color: #dcfce7;
  color: #166534;
}

.status-badge.error {
  background-color: #fee2e2;
  color: #991b1b;
}

/* Cards */
.card {
  background: var(--bg-card);
  border-radius: var(--radius-lg);
  padding: 20px;
  border: 1px solid var(--border);
  box-shadow: var(--shadow-sm);
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.title-with-icon {
  display: flex;
  align-items: center;
  gap: 10px;
}

.step-num {
  background: var(--primary);
  color: #fff;
  width: 26px;
  height: 26px;
  border-radius: 50%;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-size: 0.85rem;
  font-weight: 700;
}

.card-header h2 {
  font-size: 1.15rem;
  font-weight: 600;
}

/* Buttons */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  font-family: inherit;
  font-size: 0.95rem;
  font-weight: 600;
  padding: 8px 16px;
  border-radius: var(--radius-md);
  border: 1px solid transparent;
  cursor: pointer;
  transition: all 0.15s ease-in-out;
}

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

.btn-secondary {
  background: #f1f5f9;
  color: #334155;
  border-color: #cbd5e1;
}
.btn-secondary:hover:not(:disabled) {
  background: #e2e8f0;
}

.btn-success {
  background: var(--success);
  color: #fff;
}
.btn-success:hover:not(:disabled) {
  background: var(--success-hover);
}

.btn-outline {
  background: transparent;
  border: 1px dashed #94a3b8;
  color: #475569;
}
.btn-outline:hover:not(:disabled) {
  background: #f8fafc;
  border-color: var(--primary);
  color: var(--primary);
}

.btn-sm {
  font-size: 0.85rem;
  padding: 6px 12px;
}

.btn-lg {
  font-size: 1.05rem;
  padding: 12px 20px;
}

.btn-block {
  width: 100%;
}

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

.btn-icon-only {
  background: #f1f5f9;
  border: 1px solid var(--border);
  padding: 6px 10px;
  border-radius: var(--radius-md);
  font-size: 1rem;
}

/* Info banner */
.info-banner {
  padding: 12px 16px;
  border-radius: var(--radius-md);
  font-size: 0.875rem;
}

.info-banner.info {
  background: #eff6ff;
  border: 1px solid #bfdbfe;
  color: #1e40af;
}

.info-banner.success {
  background: #f0fdf4;
  border: 1px solid #bbf7d0;
  color: #166534;
}

.info-banner.error {
  background: #fef2f2;
  border: 1px solid #fecaca;
  color: #991b1b;
}

/* Metrics & Address */
.metrics-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(160px, 1fr));
  gap: 12px;
  margin-top: 8px;
}

.metric-item {
  background: #f8fafc;
  padding: 10px 14px;
  border-radius: var(--radius-md);
  border: 1px solid var(--border);
  display: flex;
  flex-direction: column;
}

.metric-item .label {
  font-size: 0.75rem;
  color: var(--text-muted);
  text-transform: uppercase;
  font-weight: 600;
}

.metric-item .value {
  font-size: 1.05rem;
  font-weight: 700;
  color: var(--text-main);
  margin-top: 2px;
}

.address-box {
  background: #f1f5f9;
  border-left: 4px solid var(--primary);
  padding: 10px 14px;
  border-radius: 0 var(--radius-md) var(--radius-md) 0;
  font-size: 0.9rem;
  margin-top: 12px;
}

.address-label {
  font-size: 0.75rem;
  font-weight: 700;
  text-transform: uppercase;
  color: var(--text-muted);
  display: block;
}

.address-value {
  margin-top: 2px;
  font-weight: 500;
}

/* Map */
.map-wrapper {
  margin-top: 12px;
  border-radius: var(--radius-md);
  overflow: hidden;
  border: 1px solid var(--border);
  height: 220px;
}

#map {
  width: 100%;
  height: 100%;
}

/* Camera Stage */
.camera-stage {
  position: relative;
  background: #0f172a;
  border-radius: var(--radius-md);
  overflow: hidden;
  aspect-ratio: 4 / 3;
  max-height: 420px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.viewfinder-container {
  width: 100%;
  height: 100%;
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
}

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

.viewfinder-overlay {
  position: absolute;
  inset: 0;
  pointer-events: none;
  display: flex;
  align-items: center;
  justify-content: center;
}

.crosshair {
  width: 120px;
  height: 120px;
  border: 2px dashed rgba(255, 255, 255, 0.4);
  border-radius: 12px;
}

.camera-prompt {
  position: absolute;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 12px;
  color: #fff;
  text-align: center;
  padding: 20px;
}

.preview-container {
  width: 100%;
  height: 100%;
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  background: #000;
}

#photo-preview {
  width: 100%;
  height: 100%;
  object-fit: contain;
}

.preview-badge {
  position: absolute;
  top: 12px;
  right: 12px;
  background: rgba(22, 163, 74, 0.9);
  color: #fff;
  font-size: 0.8rem;
  font-weight: 600;
  padding: 4px 10px;
  border-radius: 9999px;
  backdrop-filter: blur(4px);
}

.camera-toolbar {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 12px;
}

.btn-group {
  display: flex;
  gap: 10px;
  width: 100%;
  justify-content: center;
}

.btn-capture {
  padding: 10px 24px;
  font-size: 1rem;
}

.shutter-circle {
  width: 14px;
  height: 14px;
  border-radius: 50%;
  background: #fff;
  display: inline-block;
  margin-right: 4px;
}

.file-fallback {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
  width: 100%;
}

.divider-text {
  font-size: 0.8rem;
  color: var(--text-muted);
}

.file-upload-label {
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  gap: 8px;
}

.file-upload-label input[type="file"] {
  display: none;
}

/* Save Form */
.form-group {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.form-group label {
  font-size: 0.875rem;
  font-weight: 600;
  color: var(--text-main);
}

textarea {
  width: 100%;
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  padding: 10px 12px;
  font-family: inherit;
  font-size: 0.95rem;
  resize: vertical;
  outline: none;
  transition: border-color 0.15s;
}

textarea:focus {
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.15);
}

.save-status-summary {
  background: #f8fafc;
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  padding: 10px 14px;
  margin: 10px 0;
}

.checklist {
  display: flex;
  flex-wrap: wrap;
  gap: 14px;
  font-size: 0.85rem;
}

.check-item {
  font-weight: 600;
}

.check-item.missing {
  color: var(--danger);
}

.check-item.ok {
  color: var(--success);
}

/* History / Gallery */
.history-card .badge {
  background: #e2e8f0;
  color: #334155;
  font-size: 0.8rem;
  padding: 2px 8px;
  border-radius: 9999px;
  font-weight: 700;
}

.history-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(260px, 1fr));
  gap: 16px;
}

.empty-state {
  grid-column: 1 / -1;
  text-align: center;
  padding: 30px;
  color: var(--text-muted);
  font-size: 0.95rem;
}

.history-item {
  background: #f8fafc;
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  overflow: hidden;
  display: flex;
  flex-direction: column;
  transition: transform 0.15s, box-shadow 0.15s;
}

.history-item:hover {
  transform: translateY(-2px);
  box-shadow: var(--shadow-md);
}

.history-thumb-wrap {
  width: 100%;
  aspect-ratio: 4 / 3;
  overflow: hidden;
  background: #000;
  cursor: pointer;
  position: relative;
}

.history-thumb {
  width: 100%;
  height: 100%;
  object-fit: cover;
  transition: transform 0.2s;
}

.history-thumb:hover {
  transform: scale(1.03);
}

.history-details {
  padding: 12px;
  display: flex;
  flex-direction: column;
  gap: 6px;
  flex: 1;
}

.history-time {
  font-size: 0.75rem;
  color: var(--text-muted);
  font-weight: 600;
}

.history-address {
  font-size: 0.9rem;
  font-weight: 600;
  color: var(--text-main);
  line-height: 1.3;
}

.history-coords {
  font-size: 0.75rem;
  color: var(--text-muted);
}

.history-note {
  font-size: 0.85rem;
  background: #fff;
  border: 1px solid var(--border);
  padding: 6px 8px;
  border-radius: var(--radius-sm);
  margin-top: 4px;
}

.history-actions {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-top: auto;
  padding-top: 8px;
  border-top: 1px solid var(--border);
}

.history-actions a {
  font-size: 0.8rem;
  color: var(--primary);
  text-decoration: none;
  font-weight: 600;
}

.history-actions a:hover {
  text-decoration: underline;
}

.btn-delete {
  background: transparent;
  border: none;
  color: var(--danger);
  cursor: pointer;
  font-size: 0.8rem;
  font-weight: 600;
  padding: 4px 6px;
}

.btn-delete:hover {
  text-decoration: underline;
}

/* Modal */
.modal {
  position: fixed;
  inset: 0;
  z-index: 1000;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
}

.modal-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(0, 0, 0, 0.75);
  backdrop-filter: blur(4px);
}

.modal-dialog {
  position: relative;
  background: #fff;
  border-radius: var(--radius-lg);
  max-width: 90vw;
  max-height: 90vh;
  overflow: hidden;
  box-shadow: var(--shadow-lg);
  display: flex;
  flex-direction: column;
}

.modal-close-btn {
  position: absolute;
  top: 10px;
  right: 10px;
  background: rgba(0, 0, 0, 0.6);
  color: #fff;
  border: none;
  width: 32px;
  height: 32px;
  border-radius: 50%;
  font-size: 1.1rem;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 10;
}

#modal-image {
  max-width: 100%;
  max-height: 75vh;
  object-fit: contain;
  background: #000;
}

.modal-caption {
  padding: 12px 16px;
  font-size: 0.85rem;
  color: var(--text-main);
  background: #fff;
}

/* Toast */
.toast-container {
  position: fixed;
  bottom: 20px;
  right: 20px;
  z-index: 2000;
  display: flex;
  flex-direction: column;
  gap: 10px;
  max-width: 360px;
}

.toast {
  padding: 12px 16px;
  border-radius: var(--radius-md);
  background: #1e293b;
  color: #fff;
  font-size: 0.875rem;
  box-shadow: var(--shadow-lg);
  display: flex;
  align-items: center;
  gap: 10px;
  animation: slideIn 0.2s ease-out;
}

.toast.toast-success {
  background: #166534;
}

.toast.toast-error {
  background: #991b1b;
}

@keyframes slideIn {
  from {
    transform: translateY(20px);
    opacity: 0;
  }
  to {
    transform: translateY(0);
    opacity: 1;
  }
}

.hidden {
  display: none !important;
}

/* Footer */
.app-footer {
  text-align: center;
  font-size: 0.8rem;
  color: var(--text-muted);
  padding: 12px;
}
