/* ---- Base / Reset ---- */
*,
*::before,
*::after {
  box-sizing: border-box;
}

:root {
  --bg: #fdfcf8;
  --panel: #ffffff;
  --elev: #ffffff;
  --text: #1f2937;
  --muted: #667085;
  --primary: #0ea5e9;
  --primary-700: #0284c7;
  --danger: #ef4444;
  --ok: #22c55e;
  --border: #e5e7eb;
  --shadow: 0 6px 20px rgba(0,0,0,.08);
  --radius: 12px;
}

html, body {
  height: 100%;
}

body {
  margin: 0;
  font-family: system-ui, -apple-system, Segoe UI, Roboto, Helvetica, Arial, "Apple Color Emoji", "Segoe UI Emoji";
  color: var(--text);
  background: radial-gradient(1200px 800px at 20% -10%, #fff7ed 0%, #ecfeff 55%) no-repeat fixed,
              var(--bg);
  line-height: 1.5;
}

/* ---- Header / Nav ---- */
.app-header {
  position: sticky;
  top: 0;
  z-index: 10;
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto minmax(0, 1fr); /* flags | title | nav */
  grid-template-areas: "flags title nav";
  align-items: center;
  gap: 16px;
  padding: 22px 24px;
  border-bottom: 1px solid var(--border);
  background:
    linear-gradient(180deg, #fffaf5, #f7fbff) padding-box,
    radial-gradient(1200px 800px at 10% -20%, rgba(14,165,233,.15), rgba(14,165,233,0) 40%) border-box;
  backdrop-filter: blur(6px);
}

.brand {
  display: flex;
  align-items: center;
  justify-self: center; /* center in grid */
  grid-area: title;
  gap: 10px;
  text-align: center;
}

.brand .logo {
  font-size: 22px;
}

.brand h1 {
  margin: 0;
  font-size: 40px;
  font-weight: 700;
  letter-spacing: .2px;
  font-family: 'Baloo 2', system-ui, -apple-system, 'Segoe UI', Roboto, Helvetica, Arial, 'Apple Color Emoji', 'Segoe UI Emoji';
}

.flags {
  display: flex;
  flex-wrap: wrap; /* allow flags to wrap to next line instead of overlapping title */
  align-items: center;
  grid-area: flags;
  gap: 6px;
  position: relative;
  z-index: 11; /* keep above background */
}

.flags .flag-emoji {
  font-size: 28px;
  line-height: 1;
}

.flags .flag-img {
  width: 28px;
  height: 21px;
  display: inline-block;
  border-radius: 3px;
  box-shadow: 0 1px 2px rgba(0,0,0,.12);
}

/* Selected header flag styling */
.flags .flag-item {
  display: inline-flex;
  align-items: center;
  border-radius: 6px;
  padding: 2px;
  cursor: pointer;
}
.flags .flag-item.selected {
  outline: 2px solid var(--primary);
  background: #eaf6ff;
}

/* Auth area (anchored under content, small) */
.auth-wrap {
  display: flex;
  justify-content: flex-end;
  margin-top: 10px;
}
.auth {
  position: static;
  display: flex;
  align-items: center;
  gap: 6px;
  background: #ffffff;
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 6px 8px;
  box-shadow: var(--shadow);
}
.auth-row {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-size: 12px;
}
.auth-row input[type="email"],
.auth-row input#otp-code {
  height: 22px;
  padding: 2px 6px;
  font-size: 11px;
  width: 120px;
}
.auth-row button {
  padding: 4px 8px;
  font-size: 11px;
}
.badge {
  display: inline-block;
  font-size: 10px;
  padding: 2px 6px;
  border: 1px solid var(--border);
  border-radius: 999px;
  background: #fff;
}

.nav {
  display: flex;
  grid-area: nav;
  gap: 8px;
  justify-self: end; /* right-align in grid */
}

.nav-link {
  color: var(--text);
  text-decoration: none;
  padding: 10px 14px;
  border: 1px solid var(--border);
  border-radius: 999px;
  background: #ffffff;
  box-shadow: 0 1px 0 rgba(0,0,0,.02);
  transition: all .15s ease;
}

.nav-link:hover {
  border-color: #3a4561;
  transform: translateY(-1px);
}

.nav-link.active {
  background: linear-gradient(180deg, #223049, #1b2740);
  border-color: #3a4d72;
  color: #eaf4ff;
  box-shadow: inset 0 1px 0 rgba(255,255,255,.06), 0 4px 16px rgba(77,171,247,.15);
}

/* ---- Main / Views ---- */
.app-main {
  padding: 16px;
}

.view {
  display: none;
}

.view.active {
  display: block;
}

/* ---- Filters (Map + Table toolbars) ---- */
.filters,
.toolbar {
  display: flex;
  align-items: flex-end;
  justify-content: space-between;
  gap: 12px;
  padding: 12px;
  margin-bottom: 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: linear-gradient(180deg, #ffffff, #f8fafc);
  box-shadow: var(--shadow);
}

.hidden {
  display: none !important;
}

.filters-toggle {
  margin: 6px 0 10px;
  padding: 8px 12px;
  border: 1px solid var(--border);
  border-radius: 999px;
  background: linear-gradient(180deg, #fff, #f7fbff);
  color: var(--text);
  box-shadow: 0 1px 0 rgba(0,0,0,.02);
}

.filter-group {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.filter-group label {
  font-size: 12px;
  color: var(--muted);
}

.year-range {
  display: flex;
  align-items: center;
  gap: 8px;
}

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

/* ---- Map View ---- */
#view-map {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

#map {
  width: 100%;
  min-height: 400px;
  height: calc(100vh - 180px); /* header + filters approx */
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
  box-shadow: var(--shadow);
}

/* ---- Table ---- */
.table-wrap {
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
  background: var(--panel);
  box-shadow: var(--shadow);
}

table {
  width: 100%;
  border-collapse: collapse;
  font-size: 14px;
}

thead th {
  position: sticky;
  top: 0;
  background: #f8fafc;
  color: #334155;
  text-align: left;
  padding: 10px;
  border-bottom: 1px solid var(--border);
  user-select: none;
  white-space: nowrap;
}

tbody td {
  padding: 10px;
  border-top: 1px solid var(--border);
  vertical-align: top;
}
tbody td .row-jump {
  margin-right: 6px;
  padding: 2px 6px;
  font-size: 12px;
  line-height: 1;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: #fff;
}
tbody td .row-jump:hover {
  border-color: #3a4561;
  background: #f3f6ff;
}

tbody tr:nth-child(odd) td {
  background: #f9fafb;
}

tbody tr:hover td {
  background: #eef2ff;
}

th[data-sort] {
  cursor: pointer;
}

th[data-sort]:hover {
  color: var(--primary);
}

/* ---- Buttons / Inputs ---- */
button,
input,
select,
textarea {
  color: var(--text);
  background: #ffffff;
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 8px 10px;
  outline: none;
}

input::placeholder,
textarea::placeholder {
  color: #7f8aa1;
}

button {
  cursor: pointer;
  background: linear-gradient(180deg, #e0f2fe, #bae6fd);
  border-color: #7dd3fc;
  box-shadow: inset 0 1px 0 rgba(255,255,255,.6);
  transition: transform .08s ease, box-shadow .15s ease;
}

button:hover {
  transform: translateY(-1px);
  box-shadow: 0 8px 20px rgba(77,171,247,.12);
}

button.ghost {
  background: transparent;
  border-color: var(--border);
}

.icon-btn {
  background: transparent;
  border: none;
  padding: 6px;
}

/* Smaller buttons for map popup actions */
.popup-actions button {
  padding: 4px 8px;
  font-size: 12px;
  border-radius: 8px;
}

/* ---- Toolbar ---- */
.toolbar {
  align-items: center;
}

.toolbar .left,
.toolbar .right {
  display: flex;
  gap: 8px;
  align-items: center;
}

/* Export dropdown */
.export-wrap {
  position: relative;
  display: inline-flex;
  align-items: center;
}

.menu {
  position: absolute;
  top: calc(100% + 6px);
  left: 0;
  min-width: 140px;
  display: flex;
  flex-direction: column;
  gap: 6px;
  padding: 8px;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: #ffffff;
  box-shadow: var(--shadow);
  z-index: 20;
}

.menu button {
  width: 100%;
  text-align: left;
  padding: 8px 10px;
  border-radius: 10px;
}

.inline-filters {
  display: flex;
  gap: 10px;
  align-items: center;
  flex-wrap: wrap;
}

.inline-filters label {
  font-size: 12px;
  color: var(--muted);
  display: inline-flex;
  align-items: center;
  gap: 8px;
}

/* ---- Modal ---- */
.modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,.5);
  backdrop-filter: blur(2px);
  z-index: 10000;
}

.modal {
  position: fixed;
  inset: 0;
  display: grid;
  place-items: center;
  padding: 20px;
  z-index: 10001;
}

.modal.hidden,
.modal-backdrop.hidden {
  display: none;
}

.modal-content {
  width: min(780px, 94vw);
  max-height: 92vh;
  background: linear-gradient(180deg, #ffffff, #f8fafc);
  border: 1px solid var(--border);
  border-radius: 14px;
  box-shadow: var(--shadow);
  overflow: hidden;
  display: flex;
  flex-direction: column;
}

.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px 18px;
  border-bottom: 1px solid var(--border);
  background: #f8fafc;
}

.modal-header h2 {
  margin: 0;
  font-size: 16px;
  font-weight: 600;
}

#trip-form {
  display: flex;
  flex-direction: column;
  min-height: 0; /* allow flex children to shrink within max-height modal */
}

.form-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 12px;
  padding: 14px;
  flex: 1 1 auto;   /* grow to fill modal-content */
  min-height: 0;    /* enable overflow container */
  overflow: auto;   /* scroll when content is tall (many stops) */
}

.form-grid label {
  display: grid;
  gap: 6px;
  font-size: 13px;
  color: var(--muted);
}

.form-grid .full {
  grid-column: 1 / -1;
}

.geo-row {
  display: grid;
  grid-template-columns: 1fr 1fr auto;
  gap: 12px;
  align-items: end;
}
/* Dither/disable trip-level coords when using stops */
.geo-row.disabled {
  opacity: .6;
  pointer-events: none;
}
.geo-row.disabled input,
.geo-row.disabled button {
  background: #f3f4f6;
}

/* Stops (Itinerary) inputs */
.stops-list .stop-row input[type="text"],
.stops-list .stop-row input[type="number"] {
  width: 100%;
}
.stops-list .stop-row .stop-remove {
  color: var(--danger);
}

textarea {
  resize: vertical;
}

.modal-actions {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
  padding: 12px 14px 16px;
  border-top: 1px solid var(--border);
}

/* ---- Utility ---- */
.visually-hidden {
  position: absolute !important;
  height: 1px; width: 1px;
  overflow: hidden;
  clip: rect(1px,1px,1px,1px);
  white-space: nowrap;
}

/* ---- Leaflet override ---- */
.leaflet-container {
  background: #f2f6ff;
}

/* Custom trip pin */
.trip-pin {
  width: 18px;
  height: 18px;
  border: 2px solid rgba(0,0,0,.12);
  border-radius: 50%;
  box-shadow: 0 2px 6px rgba(0,0,0,.15);
}

/* Tooltip for trip */
.leaflet-tooltip.trip-tip {
  background: #ffffff;
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 8px;
  box-shadow: 0 6px 16px rgba(0,0,0,.08);
}
.leaflet-tooltip.trip-tip .muted { color: var(--muted); font-size: 12px; }

/* Map legend */
.map-legend {
  background: #ffffff;
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 10px;
  box-shadow: var(--shadow);
  padding: 8px 10px;
  font-size: 12px;
  line-height: 1.3;
  max-width: 260px;
  z-index: 1001;            /* ensure above tiles */
  pointer-events: auto;     /* allow hover/select */
}

.leaflet-bottom .map-legend {
  margin: 0 0 10px 10px;    /* spacing from edges */
}
.map-legend .leg-row {
  display: flex;
  align-items: center;
  gap: 8px;
  margin: 4px 0;
  white-space: nowrap;
  cursor: pointer;
  border-radius: 8px;
  padding: 4px 6px;
}
.map-legend .leg-row:hover {
  background: #f2f6ff;
}
.map-legend .leg-row.inactive {
  opacity: .45;
}
.map-legend .leg-row.active .leg-label {
  font-weight: 600;
}
.map-legend .leg-swatch {
  width: 12px;
  height: 12px;
  border-radius: 50%;
  border: 1px solid rgba(0,0,0,.15);
  flex: 0 0 12px;
}
.map-legend .leg-label {
  flex: 1 1 auto;
  overflow: hidden;
  text-overflow: ellipsis;
}
.map-legend .leg-count {
  color: var(--muted);
  margin-left: 6px;
}

.map-legend .leg-title {
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-weight: 600;
  margin-bottom: 4px;
}
.map-legend .leg-clear {
  background: #fff;
  border: 1px solid var(--border);
  border-radius: 999px;
  font-size: 11px;
  padding: 2px 8px;
  cursor: pointer;
}

.marker-cluster-small {
  background-color: rgba(77,171,247,0.2);
}
.marker-cluster-small div {
  background-color: rgba(77,171,247,0.8);
  color: #001428;
  border: 2px solid rgba(255,255,255,.6);
}

.marker-cluster-medium {
  background-color: rgba(64,192,87,0.2);
}
.marker-cluster-medium div {
  background-color: rgba(64,192,87,0.85);
  color: #00160a;
  border: 2px solid rgba(255,255,255,.6);
}

.marker-cluster-large {
  background-color: rgba(250,82,82,0.2);
}
.marker-cluster-large div {
  background-color: rgba(250,82,82,0.85);
  color: #2b0000;
  border: 2px solid rgba(255,255,255,.6);
}

/* View-only mode: hide write controls on the client UI */
body.view-only #btn-add-trip,
body.view-only #btn-manage-attendees,
body.view-only #btn-manage-access,
body.view-only .row-edit,
body.view-only .row-del,
body.view-only #btn-open-attendees,
body.view-only #btn-save-trip {
  display: none !important;
}

/* Auth responsive */
@media (max-width: 720px) {
  .auth-wrap { justify-content: center; }
  .auth-row input[type="email"],
  .auth-row input#otp-code { width: 100px; }
}

/* ---- Responsive ---- */
@media (max-width: 720px) {
  .app-main {
    padding: 12px;
  }
  .filters, .toolbar {
    flex-direction: column;
    align-items: stretch;
  }
  .geo-row {
    grid-template-columns: 1fr 1fr;
  }
  .geo-row button {
    grid-column: 1 / -1;
    justify-self: start;
  }
  #map {
    height: calc(100vh - 220px);
  }

  /* Header: stack rows and tighten spacing on mobile */
  .app-header {
    grid-template-columns: 1fr;
    grid-template-areas:
      "title"
      "flags"
      "nav";
    gap: 10px;
    padding: 14px 12px;
  }
  .brand h1 {
    font-size: 28px;
  }
  .flags {
    justify-content: center;
  }
  .flags .flag-emoji {
    font-size: 24px;
  }
  .flags .flag-img {
    width: 24px;
    height: 18px;
  }
  .nav {
    justify-self: center;
  }

  /* Table: enable horizontal scrolling on small screens */
  .table-wrap {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
  }
  table {
    min-width: 720px; /* force horizontal scroll instead of squishing columns */
  }
}
