/* ==========================================================================
   HouseChoice — app styles
   Layout: CSS grid (sidebar | map) on desktop, map full-bleed with a
   bottom-sheet sidebar under 800px. Design tokens as custom properties.
   ========================================================================== */

:root {
  --color-bg: #f4f5f3;
  --color-surface: #ffffff;
  --color-surface-alt: #f0f1ee;
  --color-border: #dfe2de;
  --color-text: #1b2321;
  --color-text-muted: #5a6560;
  --color-accent: #a8461f;
  --color-accent-bright: #c1502b;
  --color-accent-contrast: #ffffff;
  --sidebar-width: 380px;
  --radius-sm: 6px;
  --radius-md: 10px;
  --radius-lg: 16px;
  --shadow-sm: 0 1px 2px rgba(20, 24, 22, 0.08);
  --shadow-md: 0 8px 24px rgba(20, 24, 22, 0.12);
  --font-sans: "Inter", -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto,
    Helvetica, Arial, sans-serif;
}

* { box-sizing: border-box; }
html { height: 100%; margin: 0; }
button { font: inherit; color: inherit; }
a { color: var(--color-accent); }
:focus-visible { outline: 2px solid var(--color-accent); outline-offset: 2px; }

body {
  font-family: var(--font-sans);
  color: var(--color-text);
  background: var(--color-bg);
  display: grid;
  grid-template-columns: var(--sidebar-width) 1fr;
  grid-template-rows: minmax(0, 1fr);
  overflow: hidden;
  margin: 0;
  height: 100vh;
  height: 100dvh; /* dvh accounts for mobile browser chrome; vh is the fallback */
}

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

/* ---------- Map ---------- */

#map {
  grid-column: 2;
  grid-row: 1;
  height: 100%;
  width: 100%;
  background: var(--color-surface-alt);
}

/* ---------- Sidebar shell ---------- */

.sidebar {
  grid-column: 1;
  grid-row: 1;
  height: 100%;
  background: var(--color-bg);
  border-right: 1px solid var(--color-border);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.sidebar-toggle {
  display: none;
}

.sidebar__body {
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
  padding: 20px 18px 16px;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
}

.sidebar__header {
  margin-bottom: 14px;
}

.sidebar__title {
  margin: 0;
  font-size: 1.375rem;
  font-weight: 700;
  letter-spacing: -0.01em;
}

.sidebar__subtitle {
  margin: 2px 0 0;
  font-size: 0.875rem;
  color: var(--color-text-muted);
}

.sidebar__meta {
  margin: 6px 0 0;
  font-size: 0.8125rem;
  font-weight: 500;
  color: var(--color-accent);
}

.panel-root {
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
  gap: 14px;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
}

.sidebar__footer {
  margin-top: 16px;
  padding-top: 12px;
  border-top: 1px solid var(--color-border);
  font-size: 0.75rem;
  line-height: 1.5;
  color: var(--color-text-muted);
}

/* ---------- Filters (state A) ---------- */

.filters {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.filters__group {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}

.chip {
  border: 1px solid var(--color-border);
  background: var(--color-surface);
  border-radius: 999px;
  padding: 5px 12px;
  font-size: 0.8125rem;
  font-weight: 500;
  cursor: pointer;
  color: var(--color-text-muted);
  transition: background 0.15s ease, color 0.15s ease, border-color 0.15s ease;
}

.filters__group:first-child .chip.chip--active {
  background: var(--color-accent);
  border-color: var(--color-accent);
  color: var(--color-accent-contrast);
}

.filters__group:last-child .chip {
  border-color: var(--chip-color, var(--color-border));
  color: var(--chip-color, var(--color-text-muted));
}

.filters__group:last-child .chip.chip--active {
  background: var(--chip-color, var(--color-accent));
  color: #fff;
}

.filters__search input {
  width: 100%;
  padding: 8px 12px;
  border-radius: var(--radius-md);
  border: 1px solid var(--color-border);
  background: var(--color-surface);
  font-size: 0.875rem;
  color: var(--color-text);
}

.filters__search input::placeholder {
  color: var(--color-text-muted);
}

/* ---------- School list (state A) ---------- */

.school-list {
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.school-row {
  display: flex;
  align-items: center;
  gap: 10px;
  width: 100%;
  text-align: left;
  padding: 9px 10px;
  border: 1px solid transparent;
  border-radius: var(--radius-md);
  background: transparent;
  cursor: pointer;
  transition: background 0.12s ease;
}

.school-row:hover,
.school-row:focus-visible {
  background: var(--color-surface);
  border-color: var(--color-border);
}

.school-row__dot {
  flex: none;
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background: var(--dot-color, var(--color-accent));
}

.school-row__main {
  flex: 1;
  min-width: 0;
  display: flex;
  align-items: center;
  gap: 8px;
}

.school-row__name {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-size: 0.875rem;
  font-weight: 500;
}

.school-row__badge {
  flex: none;
  font-size: 0.6875rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.03em;
  color: var(--color-text-muted);
  background: var(--color-surface-alt);
  border-radius: 999px;
  padding: 1px 7px;
}

.school-row__count {
  flex: none;
  font-size: 0.75rem;
  color: var(--color-text-muted);
  white-space: nowrap;
}

.empty-state {
  color: var(--color-text-muted);
  font-size: 0.875rem;
  padding: 12px 4px;
}

/* ---------- School detail (state B) ---------- */

.back-button {
  align-self: flex-start;
  border: none;
  background: none;
  color: var(--color-accent);
  font-weight: 600;
  font-size: 0.8125rem;
  cursor: pointer;
  padding: 2px 0;
}

.school-detail {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.school-detail__header {
  display: flex;
  align-items: flex-start;
  gap: 10px;
}

.school-detail__dot {
  flex: none;
  width: 12px;
  height: 12px;
  margin-top: 6px;
  border-radius: 50%;
  background: var(--dot-color, var(--color-accent));
}

.school-detail__name {
  margin: 0;
  font-size: 1.0625rem;
  font-weight: 700;
  line-height: 1.3;
}

.school-detail__meta,
.school-detail__line {
  margin: 0;
  font-size: 0.8125rem;
  color: var(--color-text-muted);
}

.school-detail__line a {
  font-weight: 500;
}

.school-detail__count {
  margin: 6px 0 0;
  font-size: 0.8125rem;
  font-weight: 600;
}

.sort-control {
  display: flex;
  gap: 6px;
  border-bottom: 1px solid var(--color-border);
  padding-bottom: 10px;
}

.sort-control__option {
  border: none;
  background: var(--color-surface-alt);
  border-radius: 999px;
  padding: 5px 11px;
  font-size: 0.75rem;
  font-weight: 500;
  color: var(--color-text-muted);
  cursor: pointer;
}

.sort-control__option--active { background: var(--color-text); color: var(--color-surface); }

/* ---------- House cards ---------- */

.house-list {
  display: flex;
  flex-direction: column;
  gap: 12px;
  padding-bottom: 8px;
}

.house-card {
  display: flex;
  gap: 12px;
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-lg);
  padding: 10px;
  cursor: pointer;
  transition: box-shadow 0.15s ease, border-color 0.15s ease;
}

.house-card:hover,
.house-card:focus-visible {
  box-shadow: var(--shadow-md);
  border-color: var(--color-accent);
}

.house-card__image {
  flex: none;
  width: 92px;
  height: 92px;
  border-radius: var(--radius-md);
  object-fit: cover;
  background: var(--color-surface-alt);
}

.house-card__image--placeholder { background: linear-gradient(135deg, var(--color-surface-alt), var(--color-border)); }

.house-card__body {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  gap: 3px;
}

.house-card__top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}

.house-card__price { font-size: 0.9375rem; font-weight: 700; }

.house-card__type-badge {
  flex: none;
  font-size: 0.6875rem;
  font-weight: 600;
  color: var(--color-text-muted);
  background: var(--color-surface-alt);
  border-radius: 999px;
  padding: 1px 8px;
}

.house-card__specs,
.house-card__address {
  margin: 0;
  font-size: 0.8125rem;
  color: var(--color-text-muted);
}

.house-card__address {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.house-card__commute {
  margin: 2px 0 0;
  font-size: 0.8125rem;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 2px 5px;
  min-width: 0;
}

.house-card__commute--unknown {
  color: var(--color-text-muted);
  font-style: italic;
}

.house-card__estimate {
  font-size: 0.6875rem;
  color: var(--color-text-muted);
  cursor: help;
  border-bottom: 1px dotted var(--color-text-muted);
}

.house-card__reassign {
  margin: 2px 0 0;
  font-size: 0.75rem;
  color: var(--color-accent);
  font-weight: 500;
}

.house-card__chain {
  margin: 2px 0 0;
  font-size: 0.75rem;
  color: var(--color-text-muted);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.house-card__links {
  margin: 4px 0 0;
  display: flex;
  gap: 12px;
  font-size: 0.8125rem;
  font-weight: 600;
}

.house-card__links a {
  text-decoration: none;
}

.house-card__links a:hover {
  text-decoration: underline;
}

/* ---------- Leaflet marker + popup overrides ---------- */

.marker-school {
  display: block;
  border-radius: 50%;
  background: var(--marker-color, var(--color-accent));
  border: 2px solid #fff;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.35);
  cursor: pointer;
}

.marker-house {
  display: block;
  width: 14px;
  height: 14px;
  border-radius: 4px 4px 4px 0;
  transform: rotate(45deg);
  background: #9aa19c;
  border: 1.5px solid #fff;
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.3);
  cursor: pointer;
  transition: background 0.15s ease, transform 0.15s ease;
}

.marker-house--accent {
  background: var(--color-accent-bright);
  width: 17px;
  height: 17px;
}

.marker-house--faded {
  opacity: 0.35;
}

.marker-house--hover {
  transform: rotate(45deg) scale(1.25);
}

.leaflet-popup-content-wrapper {
  border-radius: var(--radius-md);
}

.popup {
  font-family: var(--font-sans);
  min-width: 180px;
}

.school-tooltip {
  font-family: var(--font-sans);
  font-size: 0.8125rem;
  font-weight: 600;
  color: var(--color-text);
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-sm);
  box-shadow: var(--shadow-sm);
  padding: 4px 8px;
}

.school-tooltip::before {
  border-top-color: var(--color-border);
}

.popup__title {
  margin: 0 0 2px;
  font-weight: 700;
  font-size: 0.9375rem;
}

.popup__meta,
.popup__line {
  margin: 0 0 4px;
  font-size: 0.8125rem;
  color: var(--color-text-muted);
}

.popup__cta {
  margin-top: 4px;
  border: none;
  background: var(--color-accent);
  color: #fff;
  border-radius: var(--radius-sm);
  padding: 5px 10px;
  font-size: 0.8125rem;
  font-weight: 600;
  cursor: pointer;
}

.popup__links a {
  font-size: 0.8125rem;
  font-weight: 600;
}

/* ---------- Responsive: bottom-sheet sidebar under 800px ----------
   Three states, driven by [data-sheet-state] on .sidebar (see app.js /
   lib/sheet.js): peek (handle only, map is the hero), half (~48% of the
   viewport, the default once a school is selected) and full (~90%).
   The sheet is anchored to the bottom edge and simply grows/shrinks its
   own height — no translateY needed, and .sidebar { overflow: hidden }
   (set above, unconditionally) clips the body down to nothing at peek. */

@media (max-width: 800px) {
  body {
    display: block;
  }

  #map {
    position: fixed;
    inset: 0;
  }

  .sidebar,
  .sidebar * {
    min-width: 0;
  }

  .sidebar {
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    width: 100%;
    box-sizing: border-box;
    overflow-x: hidden; /* guard on top of the min-width/wrap fixes below */
    border-right: none;
    border-top-left-radius: var(--radius-lg);
    border-top-right-radius: var(--radius-lg);
    box-shadow: var(--shadow-md);
    padding-bottom: env(safe-area-inset-bottom, 0px);
    transition: height 0.25s ease;
    z-index: 500;
    height: calc(58px + env(safe-area-inset-bottom, 0px)); /* peek: default */
  }

  .sidebar[data-sheet-state="half"] {
    height: calc(52vh + env(safe-area-inset-bottom, 0px));
    height: calc(52dvh + env(safe-area-inset-bottom, 0px));
  }

  .sidebar[data-sheet-state="full"] {
    height: calc(90vh + env(safe-area-inset-bottom, 0px));
    height: calc(90dvh + env(safe-area-inset-bottom, 0px));
  }

  .sidebar-toggle {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    width: 100%;
    height: 58px;
    flex: none;
    border: none;
    background: var(--color-surface);
    border-top-left-radius: var(--radius-lg);
    border-top-right-radius: var(--radius-lg);
    cursor: pointer;
    touch-action: none; /* the handle owns vertical drag, not page scroll */
  }

  .sidebar-toggle__bar {
    width: 36px;
    height: 4px;
    border-radius: 999px;
    background: var(--color-border);
  }

  .sidebar-toggle__label {
    font-weight: 700;
  }

  .sidebar__body {
    padding-top: 4px;
    overflow-y: hidden; /* .panel-root is the single scroller on mobile */
  }

  /* The handle already shows the "HouseChoice" label, so the header only
     needs to keep the house-count/snapshot-date line on mobile — title and
     subtitle are visually hidden (kept for screen readers) to leave more
     of the half-state budget for cards. */
  .sidebar__header {
    margin-bottom: 4px;
  }

  .sidebar__title,
  .sidebar__subtitle {
    position: absolute;
    width: 1px;
    height: 1px;
    padding: 0;
    margin: -1px;
    overflow: hidden;
    clip: rect(0, 0, 0, 0);
    white-space: nowrap;
    border: 0;
  }

  .sidebar__meta {
    margin: 0;
  }

  .panel-root {
    overflow-y: auto;
  }

  /* Attribution footer is non-essential chrome: keep it out of the tight
     peek/half budgets and only show it once the sheet is fully open. */
  .sidebar__footer {
    display: none;
  }

  .sidebar[data-sheet-state="full"] .sidebar__footer {
    display: block;
  }

  /* Keep "← Todos los colegios" reachable while the card list scrolls. */
  .back-button {
    position: sticky;
    top: 0;
    z-index: 2;
    align-self: stretch;
    background: var(--color-bg);
    padding: 8px 0;
    min-height: 44px;
    display: flex;
    align-items: center;
    touch-action: manipulation;
  }

  /* Long strings wrap instead of clipping under an ellipsis. */
  .school-row__name,
  .house-card__address,
  .house-card__chain,
  .sidebar__footer {
    overflow: visible;
    text-overflow: clip;
    white-space: normal;
    overflow-wrap: anywhere;
  }

  /* ---------- Touch targets (>=44px) ---------- */

  .chip {
    min-height: 44px;
    display: inline-flex;
    align-items: center;
    touch-action: manipulation;
  }

  .school-row {
    min-height: 44px;
    touch-action: manipulation;
  }

  .sort-control__option {
    min-height: 44px;
    display: inline-flex;
    align-items: center;
    touch-action: manipulation;
  }

  .house-card {
    touch-action: manipulation;
  }

  .house-card__links {
    flex-direction: column;
    gap: 8px;
  }

  .house-card__links a {
    display: flex;
    align-items: center;
    justify-content: center;
    min-height: 44px;
    padding: 8px 12px;
    border-radius: var(--radius-md);
    background: var(--color-surface-alt);
    border: 1px solid var(--color-border);
    text-align: center;
    touch-action: manipulation;
  }

  /* ---------- Card layout: 96x72 photo left, details right ---------- */

  .house-card {
    align-items: flex-start;
  }

  .house-card__image,
  .house-card__image--placeholder {
    width: 96px;
    height: 72px;
  }
}
