:root {
  --bg: #f4f6fb;
  --surface: #ffffff;
  --border: #dde2ef;
  --primary: #1e1e50;
  --primary-light: #2d2d80;
  --accent: #e84393;
  --text: #1a1a2e;
  --muted: #7a7a9a;
  --hotel: #c0392b;
  --petrol: #27ae60;
  --stop: #2980b9;
  --waypoint: #8e44ad;
  --start: #16a085;
  --end: #c0392b;
  --shadow: 0 2px 12px rgba(0,0,0,.1);
  --radius: 8px;
}

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

body { font-family: 'Segoe UI', system-ui, sans-serif; background: var(--bg); color: var(--text); height: 100vh; overflow: hidden; }

/* ── Auth ─────────────────────────────────────────────────────────────────── */
#auth-screen {
  display: flex; align-items: center; justify-content: center;
  height: 100vh; background: linear-gradient(135deg, var(--primary) 0%, var(--primary-light) 100%);
}
.auth-card {
  background: var(--surface); border-radius: 16px; padding: 40px;
  width: 360px; box-shadow: 0 20px 60px rgba(0,0,0,.3);
}
.auth-card h1 { color: var(--primary); font-size: 1.6rem; margin-bottom: 4px; }
.auth-card p { color: var(--muted); font-size: .9rem; margin-bottom: 28px; }
.auth-tabs { display: flex; gap: 0; margin-bottom: 24px; border-bottom: 2px solid var(--border); }
.auth-tab { flex: 1; padding: 8px; text-align: center; cursor: pointer; color: var(--muted); font-weight: 600; border-bottom: 2px solid transparent; margin-bottom: -2px; }
.auth-tab.active { color: var(--primary); border-bottom-color: var(--primary); }

/* ── Forms ─────────────────────────────────────────────────────────────────── */
.field { margin-bottom: 16px; }
.field label { display: block; font-size: .8rem; font-weight: 600; color: var(--muted); margin-bottom: 5px; text-transform: uppercase; letter-spacing: .05em; }
.field input, .field select, .field textarea {
  width: 100%; padding: 10px 12px; border: 1.5px solid var(--border);
  border-radius: var(--radius); font-size: .95rem; background: var(--surface);
  color: var(--text); transition: border-color .2s;
}
.field input:focus, .field select:focus, .field textarea:focus { outline: none; border-color: var(--primary); }
.field textarea { resize: vertical; min-height: 70px; }

/* ── Buttons ─────────────────────────────────────────────────────────────── */
.btn {
  display: inline-flex; align-items: center; gap: 6px; padding: 9px 18px;
  border: none; border-radius: var(--radius); font-size: .9rem; font-weight: 600;
  cursor: pointer; transition: all .18s; text-decoration: none;
}
.btn-primary { background: var(--primary); color: #fff; }
.btn-primary:hover { background: var(--primary-light); }
.btn-accent { background: var(--accent); color: #fff; }
.btn-accent:hover { filter: brightness(1.1); }
.btn-ghost { background: transparent; color: var(--primary); border: 1.5px solid var(--border); }
.btn-ghost:hover { background: var(--bg); }
.btn-danger { background: #e74c3c; color: #fff; }
.btn-danger:hover { background: #c0392b; }
.btn-sm { padding: 5px 10px; font-size: .8rem; }
.btn-full { width: 100%; justify-content: center; }
.btn-icon { padding: 7px; border-radius: 6px; }

/* ── Layout ─────────────────────────────────────────────────────────────── */
#app { display: none; flex-direction: column; height: 100vh; }

.topbar {
  display: flex; align-items: center; gap: 12px;
  padding: 0 20px; height: 56px; background: var(--primary); color: #fff;
  box-shadow: var(--shadow); z-index: 100; flex-shrink: 0;
}
.topbar-brand { font-size: 1.1rem; font-weight: 700; letter-spacing: -.01em; }
.topbar-brand span { color: #a0c4ff; }
.topbar-sep { flex: 1; }
.topbar-user { font-size: .85rem; color: rgba(255,255,255,.7); }

/* ── Dashboard ─────────────────────────────────────────────────────────── */
#dashboard {
  display: none; flex: 1; overflow-y: auto; padding: 30px;
}
#dashboard.active { display: block; }
.dash-header { display: flex; align-items: center; margin-bottom: 24px; }
.dash-header h2 { font-size: 1.4rem; color: var(--primary); flex: 1; }
.route-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(280px, 1fr)); gap: 16px; }
.route-card {
  background: var(--surface); border: 1.5px solid var(--border); border-radius: var(--radius);
  padding: 20px; cursor: pointer; transition: all .2s; position: relative;
}
.route-card:hover { border-color: var(--primary); box-shadow: var(--shadow); transform: translateY(-1px); }
.route-card h3 { font-size: 1rem; color: var(--primary); margin-bottom: 5px; }
.route-card p { font-size: .85rem; color: var(--muted); margin-bottom: 12px; }
.route-meta { display: flex; gap: 8px; flex-wrap: wrap; }
.badge { padding: 3px 8px; border-radius: 20px; font-size: .75rem; font-weight: 600; background: var(--bg); color: var(--muted); border: 1px solid var(--border); }
.badge-published { background: #e8f5e9; color: #2e7d32; border-color: #a5d6a7; }
.route-card-actions { position: absolute; top: 12px; right: 12px; display: flex; gap: 4px; opacity: 0; transition: opacity .2s; }
.route-card:hover .route-card-actions { opacity: 1; }

/* ── Editor layout ─────────────────────────────────────────────────────── */
#editor { display: none; flex: 1; overflow: hidden; min-height: 0; flex-direction: column; }
#editor.active { display: flex; }
.editor-layout { display: flex; flex: 1; min-height: 0; }

.sidebar {
  width: 340px; flex-shrink: 0; background: var(--surface);
  border-right: 1.5px solid var(--border); display: flex; flex-direction: column; overflow: hidden;
}
.sidebar-header { padding: 14px 16px; border-bottom: 1.5px solid var(--border); }
.sidebar-header h2 { font-size: 1rem; color: var(--primary); margin-bottom: 8px; }
.sidebar-tools { display: flex; flex-wrap: wrap; gap: 6px; }

.sidebar-stops { flex: 1; overflow-y: auto; padding: 8px 0; }

.stop-item {
  padding: 10px 14px; border-bottom: 1px solid var(--border); cursor: pointer;
  display: flex; align-items: flex-start; gap: 10px; transition: background .15s;
}
.stop-item:hover { background: var(--bg); }
.stop-item.selected { background: #eef2ff; }
.stop-icon {
  width: 28px; height: 28px; border-radius: 50%; display: flex; align-items: center; justify-content: center;
  font-size: .75rem; font-weight: 700; color: #fff; flex-shrink: 0; margin-top: 1px;
}
.stop-icon.hotel { background: var(--hotel); }
.stop-icon.petrol { background: var(--petrol); }
.stop-icon.stop { background: var(--stop); }
.stop-icon.waypoint { background: var(--waypoint); }
.stop-icon.start { background: var(--start); }
.stop-icon.end { background: var(--end); }
.stop-info { flex: 1; min-width: 0; }
.stop-name { font-size: .9rem; font-weight: 600; color: var(--text); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.stop-meta { font-size: .78rem; color: var(--muted); }
.stop-actions { display: flex; gap: 3px; }
.stop-del { background: none; border: none; cursor: pointer; color: var(--muted); padding: 2px 5px; border-radius: 4px; font-size: 1rem; }
.stop-del:hover { color: #e74c3c; background: #ffeaea; }

.day-divider {
  padding: 6px 14px; background: var(--bg); font-size: .75rem; font-weight: 700;
  color: var(--primary); text-transform: uppercase; letter-spacing: .08em; border-bottom: 1px solid var(--border);
}

.sidebar-footer { padding: 12px 14px; border-top: 1.5px solid var(--border); display: flex; flex-direction: column; gap: 8px; }
.route-stats { font-size: .8rem; color: var(--muted); }

.map-wrap { flex: 1; position: relative; }
#map { width: 100%; height: 100%; }

/* Toolbar inside map */
.map-toolbar {
  position: absolute; top: 10px; left: 10px; z-index: 500;
  display: flex; flex-direction: column; gap: 6px;
}
.tool-btn {
  display: flex; align-items: center; gap: 7px; padding: 8px 12px;
  background: var(--surface); border: 1.5px solid var(--border); border-radius: var(--radius);
  font-size: .82rem; font-weight: 600; cursor: pointer; color: var(--primary);
  box-shadow: var(--shadow); white-space: nowrap; transition: all .15s;
}
.tool-btn:hover { background: var(--primary); color: #fff; border-color: var(--primary); }
.tool-btn.active { background: var(--primary); color: #fff; border-color: var(--primary); }
.tool-dot { width: 10px; height: 10px; border-radius: 50%; }

/* ── Edit panel (stop details) ─────────────────────────────────────────── */
.edit-panel {
  position: absolute; bottom: 0; right: 0; left: 0; background: var(--surface);
  border-top: 2px solid var(--primary); padding: 14px 16px;
  box-shadow: 0 -4px 16px rgba(0,0,0,.1); z-index: 10;
}
.edit-panel-header { display: flex; align-items: center; margin-bottom: 10px; }
.edit-panel-header h3 { flex: 1; font-size: .95rem; color: var(--primary); }
.edit-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.edit-grid .span2 { grid-column: 1/-1; }

/* ── Map search overlay ──────────────────────────────────────────────────── */
#search-box-wrap {
  position: absolute;
  top: 10px;
  left: 50%;
  transform: translateX(-50%);
  z-index: 500;
  width: 320px;
  max-width: calc(100% - 120px);
}
#place-search {
  width: 100%;
  padding: 10px 14px;
  border: none;
  border-radius: 8px;
  box-shadow: 0 2px 14px rgba(0,0,0,.22);
  font-size: .95rem;
  outline: none;
  color: var(--text);
}
#place-search:focus { box-shadow: 0 2px 14px rgba(0,0,0,.3), 0 0 0 2px var(--primary); }
#search-results {
  display: none;
  background: var(--surface);
  border-radius: 8px;
  box-shadow: 0 6px 20px rgba(0,0,0,.18);
  margin-top: 5px;
  overflow: hidden;
  max-height: 280px;
  overflow-y: auto;
}
.sr-item {
  padding: 10px 14px;
  cursor: pointer;
  border-bottom: 1px solid var(--border);
  transition: background .12s;
}
.sr-item:last-child { border-bottom: none; }
.sr-item:hover { background: #eef2ff; }
.sr-name { font-size: .92rem; font-weight: 600; color: var(--text); }
.sr-sub  { font-size: .78rem; color: var(--muted); margin-top: 2px; }
.sr-loading { color: var(--muted); font-size: .88rem; cursor: default; }
.sr-loading:hover { background: none; }

/* ── Stop type colors in toolbar ─────────────────────────────────────────── */
.dot-waypoint { background: var(--waypoint); }
.dot-stop { background: var(--stop); }
.dot-hotel { background: var(--hotel); }
.dot-petrol { background: var(--petrol); }
.dot-start { background: var(--start); }
.dot-end { background: var(--end); }

/* ── Viewer ─────────────────────────────────────────────────────────────── */
#viewer { display: none; flex: 1; overflow: hidden; min-height: 0; }
#viewer.active { display: flex; }
.viewer-layout { display: flex; flex: 1; min-height: 0; }
.viewer-sidebar { width: 320px; flex-shrink: 0; overflow-y: auto; background: var(--surface); border-right: 1.5px solid var(--border); }
.viewer-header { padding: 20px 16px 14px; border-bottom: 1.5px solid var(--border); }
.viewer-header h2 { color: var(--primary); font-size: 1.1rem; margin-bottom: 6px; }
.viewer-header p { color: var(--muted); font-size: .85rem; }
.viewer-actions { padding: 12px 16px; border-bottom: 1.5px solid var(--border); display: flex; gap: 8px; flex-wrap: wrap; }
.day-section { padding: 10px 16px 4px; }
.day-section h3 { font-size: .8rem; text-transform: uppercase; letter-spacing: .08em; color: var(--primary); font-weight: 700; margin-bottom: 8px; }
.viewer-stop { display: flex; align-items: flex-start; gap: 10px; padding: 8px 0; border-bottom: 1px solid var(--border); }
.viewer-stop:last-child { border-bottom: none; }
.viewer-map { flex: 1; position: relative; }
#viewer-map { width: 100%; height: 100%; }

/* ── Modal ─────────────────────────────────────────────────────────────── */
.modal-bg { position: fixed; inset: 0; background: rgba(0,0,0,.4); z-index: 1000; display: flex; align-items: center; justify-content: center; }
.modal { background: var(--surface); border-radius: 12px; padding: 28px; width: 420px; max-width: 95vw; box-shadow: 0 20px 60px rgba(0,0,0,.3); }
.modal h2 { color: var(--primary); margin-bottom: 20px; font-size: 1.2rem; }
.modal-footer { display: flex; justify-content: flex-end; gap: 10px; margin-top: 20px; }

/* ── Misc ─────────────────────────────────────────────────────────────── */
.error-msg { background: #ffeaea; color: #c0392b; border: 1px solid #f5b7b1; border-radius: var(--radius); padding: 10px 14px; font-size: .9rem; margin-bottom: 12px; display: none; }
.leaflet-container { background: #e8ecf0; }
.segment-distance { font-size: .75rem; color: var(--muted); padding: 2px 14px; display: flex; align-items: center; gap: 6px; }
.segment-distance::before { content: ''; display: block; width: 1px; height: 16px; background: var(--border); }

/* ── Notifications ─────────────────────────────────────────────────────── */
.notif {
  position: fixed; bottom: 20px; right: 20px; z-index: 9999;
  padding: 12px 18px; background: var(--primary); color: #fff;
  border-radius: var(--radius); box-shadow: var(--shadow);
  font-size: .9rem; animation: slideIn .2s ease;
}
.notif.error { background: #e74c3c; }
@keyframes slideIn { from { transform: translateY(20px); opacity: 0; } to { transform: translateY(0); opacity: 1; } }

/* ── Loading ─────────────────────────────────────────────────────────────── */
.spinner { display: inline-block; width: 16px; height: 16px; border: 2px solid rgba(255,255,255,.3); border-top-color: #fff; border-radius: 50%; animation: spin .6s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
