:root {
  --bg: #0b0f14;
  --bg-elevated: #131a22;
  --bg-card: #171f29;
  --border: #232d3a;
  --text: #e8edf3;
  --text-dim: #8b98a8;
  --text-faint: #5a6879;

  --accent: #3b82f6;
  --accent-dim: #1e3a5f;
  --success: #10b981;
  --success-dim: #0d3b2d;
  --warning: #f59e0b;
  --warning-dim: #4a3510;
  --danger: #f43f5e;
  --danger-dim: #4a1420;

  --radius: 14px;
  --radius-sm: 10px;
  --radius-lg: 20px;
  --tap: 48px;

  --shadow: 0 4px 20px rgba(0,0,0,0.35);
  --shadow-lg: 0 10px 40px rgba(0,0,0,0.5);
}

* { box-sizing: border-box; }

html, body {
  margin: 0; padding: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  overscroll-behavior: none;
  -webkit-tap-highlight-color: transparent;
  user-select: none;
  touch-action: manipulation;
}

input, textarea {
  user-select: text;
}

body {
  overflow: hidden;
  position: fixed;
  inset: 0;
}

#app {
  position: absolute;
  inset: 0;
  overflow: hidden;
}

button {
  font-family: inherit;
  border: none;
  background: none;
  color: inherit;
  cursor: pointer;
}

.screen {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  background: var(--bg);
  animation: screen-in .25s ease;
}

@keyframes screen-in {
  from { opacity: 0; transform: translateY(8px); }
  to { opacity: 1; transform: translateY(0); }
}

/* ---------- Header ---------- */
.app-header {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: max(14px, env(safe-area-inset-top)) 16px 14px;
  background: var(--bg-elevated);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
  min-height: var(--tap);
}
.app-header h2 { margin: 0; font-size: 17px; font-weight: 600; flex: 1; }
.app-header.with-back h2 { text-align: left; }
.app-logo { font-size: 20px; font-weight: 700; display: flex; align-items: center; gap: 8px; }
.app-logo span { background: linear-gradient(90deg, var(--accent), #60a5fa); -webkit-background-clip: text; background-clip: text; color: transparent; }
.header-counter { font-size: 13px; color: var(--text-dim); font-variant-numeric: tabular-nums; }

.icon-btn {
  width: var(--tap); height: var(--tap);
  display: flex; align-items: center; justify-content: center;
  font-size: 20px;
  border-radius: var(--radius-sm);
  color: var(--text);
  margin: -8px;
}
.icon-btn:active { background: rgba(255,255,255,0.06); }

/* ---------- Home ---------- */
.home-main {
  flex: 1;
  display: flex;
  flex-direction: column;
  justify-content: center;
  gap: 16px;
  padding: 24px;
}
.home-footer {
  text-align: center;
  padding: 16px;
  color: var(--text-faint);
  font-size: 12px;
  padding-bottom: max(16px, env(safe-area-inset-bottom));
}

.btn {
  min-height: var(--tap);
  border-radius: var(--radius);
  font-size: 16px;
  font-weight: 600;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  transition: transform .1s ease, filter .15s ease;
  padding: 14px 18px;
}
.btn:active { transform: scale(0.97); filter: brightness(0.9); }
.btn:disabled { opacity: 0.4; pointer-events: none; }

.btn-primary { background: var(--accent); color: white; }
.btn-secondary { background: var(--bg-card); color: var(--text); border: 1px solid var(--border); }
.btn-ghost { background: transparent; color: var(--text-dim); }
.btn-danger { background: var(--danger); color: white; }
.btn-success { background: var(--success); color: white; }

.btn-large {
  flex-direction: column;
  padding: 22px 18px;
  border-radius: var(--radius-lg);
  gap: 4px;
}
.btn-icon { font-size: 30px; margin-bottom: 4px; }
.btn-sub { font-size: 12.5px; font-weight: 400; opacity: 0.75; }
.btn-large.btn-primary .btn-sub { opacity: 0.85; }

/* ---------- Paste addresses ---------- */
.paste-body { flex: 1; display: flex; flex-direction: column; padding: 16px; gap: 10px; overflow: hidden; }
.paste-hint { margin: 0; font-size: 13px; color: var(--text-dim); }
.paste-textarea {
  flex: 1; width: 100%; resize: none;
  background: var(--bg-card); border: 1px solid var(--border); border-radius: var(--radius);
  color: var(--text); font-size: 15px; line-height: 1.5; padding: 14px;
  font-family: inherit; user-select: text;
}
.paste-textarea:focus { outline: none; border-color: var(--accent); }

/* ---------- Scan / loader ---------- */
.screen-scan { align-items: center; justify-content: center; }
.scan-loader {
  display: flex; flex-direction: column; align-items: center; gap: 16px;
  padding: 32px; text-align: center; max-width: 320px;
}
.scan-spinner {
  width: 56px; height: 56px;
  border: 4px solid var(--border);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.9s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }
.scan-loader h2 { margin: 0; font-size: 18px; }
.scan-progress-text { color: var(--text-dim); font-size: 14px; margin: 0; min-height: 20px; }
.progress-bar {
  width: 100%; height: 8px; background: var(--bg-card);
  border-radius: 99px; overflow: hidden; border: 1px solid var(--border);
}
.progress-bar-fill {
  height: 100%; width: 0%; background: linear-gradient(90deg, var(--accent), #60a5fa);
  transition: width .3s ease; border-radius: 99px;
}

/* ---------- Validate ---------- */
.validate-list {
  flex: 1; overflow-y: auto; padding: 12px; display: flex; flex-direction: column; gap: 10px;
  -webkit-overflow-scrolling: touch;
}
.addr-card {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-left: 4px solid var(--text-faint);
  border-radius: var(--radius);
  padding: 14px;
  display: flex; flex-direction: column; gap: 6px;
}
.addr-card.status-ok { border-left-color: var(--success); }
.addr-card.status-warn { border-left-color: var(--warning); }
.addr-card.status-error { border-left-color: var(--danger); }
.addr-card-top { display: flex; align-items: flex-start; gap: 10px; }
.addr-index {
  flex-shrink: 0; width: 26px; height: 26px; border-radius: 50%;
  background: var(--bg-elevated); display: flex; align-items: center; justify-content: center;
  font-size: 12px; font-weight: 700; color: var(--text-dim);
}
.addr-text { flex: 1; font-size: 15px; line-height: 1.35; word-break: break-word; }
.addr-status-badge {
  font-size: 11.5px; font-weight: 600; padding: 3px 9px; border-radius: 99px; flex-shrink: 0;
  white-space: nowrap;
}
.status-ok .addr-status-badge { background: var(--success-dim); color: var(--success); }
.status-warn .addr-status-badge { background: var(--warning-dim); color: var(--warning); }
.status-error .addr-status-badge { background: var(--danger-dim); color: var(--danger); }

.addr-edit-row { display: none; gap: 8px; margin-top: 4px; }
.addr-card.editing .addr-edit-row { display: flex; }
.addr-edit-row input {
  flex: 1; background: var(--bg-elevated); border: 1px solid var(--border);
  border-radius: var(--radius-sm); padding: 10px 12px; color: var(--text); font-size: 14.5px;
  min-height: 44px;
}
.addr-edit-row button {
  flex-shrink: 0; background: var(--accent); color: white; border-radius: var(--radius-sm);
  padding: 0 16px; font-size: 13.5px; font-weight: 600; min-height: 44px;
}
.addr-actions { display: flex; gap: 8px; margin-top: 2px; }
.addr-actions button {
  font-size: 12.5px; padding: 7px 12px; border-radius: 99px; background: var(--bg-elevated);
  color: var(--text-dim); border: 1px solid var(--border); min-height: 34px;
}
.addr-actions button.primary { color: var(--accent); border-color: var(--accent-dim); }

.sticky-footer {
  flex-shrink: 0; padding: 14px 16px max(14px, env(safe-area-inset-bottom));
  background: var(--bg-elevated); border-top: 1px solid var(--border);
}

/* ---------- Build / map ---------- */
.map-full { flex: 1; width: 100%; }
.build-hint {
  position: absolute; top: calc(var(--tap) + env(safe-area-inset-top) + 16px);
  left: 50%; transform: translateX(-50%);
  background: rgba(19,26,34,0.92); color: var(--text-dim);
  font-size: 12.5px; padding: 8px 14px; border-radius: 99px;
  border: 1px solid var(--border); z-index: 500; pointer-events: none;
  white-space: nowrap;
}

.marker-dot {
  width: 34px; height: 34px; border-radius: 50%;
  background: var(--bg-card); border: 2px solid var(--text-faint);
  display: flex; align-items: center; justify-content: center;
  font-size: 14px; font-weight: 700; color: var(--text-dim);
  box-shadow: var(--shadow);
}
.marker-dot.numbered { background: var(--accent); border-color: #60a5fa; color: white; }
.marker-dot.done { background: var(--success); border-color: #34d399; color: white; opacity: 0.55; }
.marker-dot.skip { background: var(--bg-elevated); border: 2px dashed var(--text-faint); color: var(--text-faint); }
.marker-dot.warn { border-color: var(--warning); }
.marker-me {
  width: 20px; height: 20px; border-radius: 50%;
  background: var(--accent); border: 3px solid white; box-shadow: 0 0 0 6px rgba(59,130,246,0.3);
}

/* ---------- Active tour ---------- */
.view-switch {
  flex: 1; display: flex; background: var(--bg-card); border-radius: 99px; padding: 3px;
  border: 1px solid var(--border);
}
.switch-btn {
  flex: 1; padding: 8px; font-size: 13px; font-weight: 600; color: var(--text-dim);
  border-radius: 99px; min-height: 38px;
}
.switch-btn.active { background: var(--accent); color: white; }

.active-list {
  flex: 1; overflow-y: auto; padding: 12px; display: flex; flex-direction: column; gap: 10px;
  -webkit-overflow-scrolling: touch;
}
.active-list.hidden, .map-full.hidden { display: none; }
.empty-hint { padding: 40px 16px; text-align: center; color: var(--text-dim); }

.history-list {
  flex: 1; overflow-y: auto; padding: 12px; display: flex; flex-direction: column; gap: 10px;
  -webkit-overflow-scrolling: touch;
}
.history-card {
  background: var(--bg-card); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 14px 16px; display: flex; flex-direction: column; gap: 4px;
}
.history-card-date { font-size: 15px; font-weight: 600; text-transform: capitalize; }
.history-card-time { font-size: 12.5px; color: var(--text-faint); }
.history-card-stats { font-size: 13px; color: var(--text-dim); margin-top: 2px; }

.stop-card {
  background: var(--bg-card); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 14px; display: flex; align-items: center; gap: 12px;
}
.stop-card.done { opacity: 0.5; }
.stop-card.skip { opacity: 0.45; border-style: dashed; }
.stop-num {
  width: 32px; height: 32px; border-radius: 50%; background: var(--accent-dim); color: var(--accent);
  display: flex; align-items: center; justify-content: center; font-weight: 700; font-size: 13px; flex-shrink: 0;
}
.stop-card.done .stop-num { background: var(--success-dim); color: var(--success); }
.stop-body { flex: 1; min-width: 0; }
.stop-addr { font-size: 14.5px; line-height: 1.3; }
.stop-status { font-size: 12px; color: var(--text-dim); margin-top: 2px; }
.stop-check {
  width: 40px; height: 40px; border-radius: 50%; background: var(--bg-elevated);
  border: 2px solid var(--border); display: flex; align-items: center; justify-content: center;
  font-size: 18px; color: var(--text-dim); flex-shrink: 0;
}
.stop-card.done .stop-check { background: var(--success); border-color: var(--success); color: white; }

/* ---------- Bottom sheet ---------- */
.sheet-overlay {
  position: fixed; inset: 0; background: rgba(0,0,0,0.55); z-index: 1000;
  display: flex; align-items: flex-end; justify-content: center;
  animation: fade-in .18s ease;
}
.sheet-overlay.hidden { display: none; }
@keyframes fade-in { from { opacity: 0; } to { opacity: 1; } }

.bottom-sheet {
  background: var(--bg-elevated); width: 100%; max-width: 480px;
  border-radius: 20px 20px 0 0; padding: 10px 18px max(20px, env(safe-area-inset-bottom));
  animation: sheet-up .22s ease;
  border-top: 1px solid var(--border);
}
@keyframes sheet-up { from { transform: translateY(100%); } to { transform: translateY(0); } }
.sheet-handle {
  width: 40px; height: 4px; background: var(--border); border-radius: 99px; margin: 8px auto 14px;
}
.sheet-address { font-size: 17px; font-weight: 600; margin-bottom: 4px; }
.sheet-status { font-size: 13px; color: var(--text-dim); margin-bottom: 16px; }
.sheet-actions { display: flex; flex-direction: column; gap: 10px; }

.context-menu {
  background: var(--bg-elevated); width: 100%; max-width: 480px; margin: 0 16px 16px;
  border-radius: var(--radius-lg); overflow: hidden; border: 1px solid var(--border);
  animation: sheet-up .18s ease;
}
.context-menu-title {
  padding: 14px; text-align: center; font-size: 12.5px; color: var(--text-dim);
  border-bottom: 1px solid var(--border);
}
.context-menu-item {
  width: 100%; padding: 16px; font-size: 16px; text-align: center;
  border-bottom: 1px solid var(--border); min-height: var(--tap);
}
.context-menu-item:last-child { border-bottom: none; color: var(--danger); font-weight: 600; }
.context-menu-item:active { background: rgba(255,255,255,0.05); }

/* ---------- Toasts ---------- */
#toast-container {
  position: fixed; top: max(16px, env(safe-area-inset-top)); left: 0; right: 0;
  display: flex; flex-direction: column; align-items: center; gap: 8px; z-index: 2000; pointer-events: none;
}
.toast {
  background: var(--bg-card); border: 1px solid var(--border); color: var(--text);
  padding: 10px 16px; border-radius: 99px; font-size: 13.5px; box-shadow: var(--shadow-lg);
  animation: toast-in .2s ease;
}
.toast.success { border-color: var(--success); color: var(--success); }
.toast.error { border-color: var(--danger); color: var(--danger); }
@keyframes toast-in { from { opacity: 0; transform: translateY(-10px); } to { opacity: 1; transform: translateY(0); } }

/* Leaflet popup theming */
.leaflet-popup-content-wrapper, .leaflet-popup-tip { background: var(--bg-card); color: var(--text); }
.leaflet-container { background: var(--bg-elevated); }
