:root {
  --bg: #e8eef2;
  --ink: #1a2b33;
  --muted: #5a6f7a;
  --accent: #0d6e6e;
  --accent-soft: #c5e4e4;
  --danger: #b33a3a;
  --sheet: #f7fafb;
  --border: #c9d6dc;
  --shadow: 0 -4px 24px rgba(26, 43, 51, 0.12);
  --font: "Segoe UI", "PT Sans", "Noto Sans", sans-serif;
  --display: "Georgia", "Times New Roman", serif;
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  font-family: var(--font);
  color: var(--ink);
  background: var(--bg);
  overflow: hidden;
}

.hidden { display: none !important; }
.error { color: var(--danger); font-size: 0.9rem; }

.auth-gate {
  min-height: 100%;
  display: grid;
  place-items: center;
  padding: 1.5rem;
  background:
    radial-gradient(ellipse at 20% 10%, #c5e4e4 0%, transparent 45%),
    radial-gradient(ellipse at 80% 90%, #d4dde3 0%, transparent 40%),
    linear-gradient(160deg, #e8eef2, #d7e3e8);
}
.auth-card {
  max-width: 26rem;
  text-align: center;
}
.auth-card .auth-logo {
  display: block;
  width: 4.5rem;
  height: 4.5rem;
  margin: 0 auto 0.75rem;
  object-fit: contain;
}
.auth-card h1 {
  font-family: var(--display);
  font-weight: 600;
  font-size: clamp(1.6rem, 4vw, 2.2rem);
  margin: 0 0 0.75rem;
}
.auth-card p { color: var(--muted); margin: 0 0 1.25rem; }
.auth-tabs {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0.35rem;
  margin-bottom: 1rem;
}
.auth-tab {
  border: 1px solid var(--border);
  background: transparent;
  color: var(--muted);
  border-radius: 6px;
  padding: 0.45rem 0.5rem;
  font: inherit;
  cursor: pointer;
}
.auth-tab.active {
  background: #fff;
  color: var(--ink);
  border-color: var(--accent);
}
.password-form {
  display: grid;
  gap: 0.75rem;
  text-align: left;
  margin-bottom: 1rem;
}
.password-form label {
  display: grid;
  gap: 0.25rem;
  font-size: 0.85rem;
  color: var(--muted);
}
.password-form input {
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 0.55rem 0.65rem;
  font: inherit;
  color: var(--ink);
  background: #fff;
}
.password-form .btn { width: 100%; margin-top: 0.25rem; }
.auth-or {
  margin: 0 0 0.85rem !important;
  font-size: 0.85rem;
  text-transform: lowercase;
}
#google-btn { display: flex; justify-content: center; }

/* District polygons: stroke follows real GeoJSON contours */
.leaflet-overlay-pane path.district-poly {
  stroke-linejoin: round;
  stroke-linecap: round;
  transition: fill-opacity 0.15s ease, stroke-width 0.15s ease, stroke 0.15s ease;
}
.leaflet-overlay-pane path.district-selected {
  filter: drop-shadow(0 0 6px rgba(13, 110, 110, 0.75));
}

.app {
  height: 100%;
  display: grid;
  grid-template-rows: auto 1fr;
  position: relative;
}

.map-wrap { position: relative; min-height: 0; }
#map { height: 100%; width: 100%; touch-action: none; }

.place-note-fab {
  position: fixed;
  right: max(1rem, env(safe-area-inset-right, 0px));
  z-index: 950;
  width: 3.5rem;
  height: 3.5rem;
  border: 2px solid #fff;
  border-radius: 50%;
  background: var(--accent);
  color: #fff;
  box-shadow: 0 4px 20px rgba(13, 110, 110, 0.55);
  display: grid;
  place-items: center;
  cursor: grab;
  touch-action: none;
  -webkit-user-select: none;
  user-select: none;
}
.place-note-fab::after {
  content: "+";
  position: absolute;
  right: -2px;
  bottom: -2px;
  width: 1.1rem;
  height: 1.1rem;
  border-radius: 50%;
  background: #fff;
  color: var(--accent);
  font-size: 0.85rem;
  font-weight: 700;
  line-height: 1.1rem;
  text-align: center;
  box-shadow: 0 1px 4px rgba(0, 0, 0, 0.2);
}
.place-note-fab.hidden { display: none !important; }
.place-note-fab:active { cursor: grabbing; }
.place-note-ghost {
  position: fixed;
  z-index: 2000;
  width: 2.25rem;
  height: 2.25rem;
  margin: -1.125rem 0 0 -1.125rem;
  border-radius: 50%;
  background: var(--accent);
  color: #fff;
  box-shadow: var(--shadow);
  pointer-events: none;
  display: grid;
  place-items: center;
  opacity: 0.92;
}
.place-note-ghost.hidden { display: none !important; }
.place-note-ghost::after {
  content: "";
  width: 0.55rem;
  height: 0.55rem;
  background: #fff;
  border-radius: 50%;
}
body.place-note-dragging #map { cursor: grabbing; }
body.place-note-dragging .leaflet-container { pointer-events: none; }

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
  padding: 0.55rem 0.85rem;
  background: rgba(247, 250, 251, 0.94);
  border-bottom: 1px solid var(--border);
  z-index: 1000;
  backdrop-filter: blur(8px);
}
.brand {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  min-width: 0;
  font-family: var(--display);
  font-size: 1rem;
  font-weight: 600;
}
.brand-logo {
  flex-shrink: 0;
  width: 1.75rem;
  height: 1.75rem;
  object-fit: contain;
}
.brand span {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.topbar-actions {
  display: flex;
  align-items: center;
  gap: 0.4rem;
  flex-shrink: 0;
}
.zoom-mode {
  font-size: 0.75rem;
  color: var(--accent);
  background: var(--accent-soft);
  padding: 0.2rem 0.5rem;
  border-radius: 4px;
}

.city-picker {
  position: absolute;
  inset: 0;
  z-index: 1200;
  display: grid;
  place-items: center;
  padding: 1.25rem;
  background:
    radial-gradient(ellipse at 20% 10%, #c5e4e4 0%, transparent 45%),
    radial-gradient(ellipse at 80% 90%, #d4dde3 0%, transparent 40%),
    rgba(232, 238, 242, 0.96);
}
.city-picker.hidden { display: none !important; }
.city-picker-card {
  width: min(100%, 22rem);
  text-align: center;
}
.city-picker-card h2 {
  font-family: var(--display);
  font-size: clamp(1.4rem, 4vw, 1.9rem);
  margin: 0 0 0.5rem;
}
.city-picker-card p {
  color: var(--muted);
  margin: 0 0 1rem;
  font-size: 0.95rem;
}
.city-picker-list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: grid;
  gap: 0.5rem;
}
.city-picker-list button {
  width: 100%;
  text-align: left;
  border: 1px solid var(--border);
  background: #fff;
  border-radius: 8px;
  padding: 0.75rem 0.9rem;
  font: inherit;
  color: var(--ink);
  cursor: pointer;
}
.city-picker-list button:hover,
.city-picker-list button:focus-visible {
  border-color: var(--accent);
  outline: none;
}
.city-picker-list button.active {
  border-color: var(--accent);
  background: var(--accent-soft);
  font-weight: 600;
}
.city-picker-list .meta {
  display: block;
  font-size: 0.8rem;
  color: var(--muted);
  margin-top: 0.15rem;
  font-weight: 400;
}

.city-drawer-tab {
  position: absolute;
  left: 0;
  top: 40%;
  z-index: 960;
  writing-mode: vertical-rl;
  transform: rotate(180deg);
  border: 1px solid var(--border);
  border-left: none;
  border-radius: 0 8px 8px 0;
  background: rgba(247, 250, 251, 0.96);
  color: var(--ink);
  font: inherit;
  font-size: 0.75rem;
  letter-spacing: 0.04em;
  padding: 0.65rem 0.35rem;
  cursor: pointer;
  box-shadow: 2px 0 10px rgba(26, 43, 51, 0.08);
}
.city-drawer {
  position: absolute;
  left: 0;
  top: 0;
  bottom: 0;
  z-index: 970;
  width: min(16rem, 78vw);
  background: rgba(247, 250, 251, 0.98);
  border-right: 1px solid var(--border);
  padding: 1rem 0.85rem;
  transform: translateX(-105%);
  transition: transform 0.2s ease;
  overflow: auto;
  box-shadow: 4px 0 20px rgba(26, 43, 51, 0.12);
}
.city-drawer.open { transform: translateX(0); }
.city-drawer h3 {
  font-family: var(--display);
  font-size: 1rem;
  margin: 0 0 0.75rem;
}
.city-drawer-scrim {
  position: absolute;
  inset: 0;
  z-index: 965;
  background: rgba(26, 43, 51, 0.2);
}
.city-drawer-scrim.hidden { display: none !important; }

.btn {
  border: 1px solid var(--border);
  background: #fff;
  color: var(--ink);
  border-radius: 6px;
  padding: 0.45rem 0.75rem;
  font: inherit;
  font-size: 0.9rem;
  cursor: pointer;
}
.btn.primary {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}
.btn.ghost {
  background: transparent;
  border-color: transparent;
  color: var(--muted);
}
.btn.danger { color: var(--danger); }

.sheet {
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  max-height: 45dvh;
  background: var(--sheet);
  border-radius: 16px 16px 0 0;
  box-shadow: var(--shadow);
  padding: 0;
  z-index: 900;
  overflow: hidden;
  display: flex;
  flex-direction: column;
  transform: translateY(0);
  transition: max-height 0.2s ease;
}
.sheet-chrome {
  flex-shrink: 0;
  padding: 0 1rem 0;
}
.sheet-body {
  flex: 1;
  overflow-y: auto;
  min-height: 0;
  padding: 0 1rem 1rem;
}
.sheet-handle {
  display: block;
  width: 100%;
  min-height: 2.75rem;
  border: none;
  background: transparent;
  padding: 0;
  cursor: pointer;
  touch-action: none;
}
.sheet-handle-bar {
  display: block;
  width: 2.5rem;
  height: 4px;
  background: var(--border);
  border-radius: 2px;
  margin: 0 auto;
}
.sheet-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
  margin-bottom: 0.35rem;
}
.sheet-header h2 {
  font-size: 1.05rem;
  margin: 0;
  font-family: var(--display);
}
.sheet-meta {
  font-size: 0.85rem;
  color: var(--muted);
  margin-bottom: 0.75rem;
  min-height: 1.2em;
}

.notes-list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 0.65rem;
}
.note-card {
  border-top: 1px solid var(--border);
  padding-top: 0.65rem;
}
.note-card .score {
  font-weight: 600;
  color: var(--accent);
  margin-right: 0.5rem;
}
.note-card p {
  margin: 0.25rem 0 0.4rem;
  white-space: pre-wrap;
  font-size: 0.95rem;
}
.note-card .meta {
  font-size: 0.75rem;
  color: var(--muted);
}
.note-actions { display: flex; gap: 0.25rem; margin-top: 0.25rem; }
.empty-notes { color: var(--muted); font-size: 0.9rem; }

.note-dialog {
  border: none;
  border-radius: 12px 12px 0 0;
  padding: 0;
  width: min(100vw, 28rem);
  max-width: 100%;
  margin: auto 0 0 auto;
  background: var(--sheet);
  box-shadow: var(--shadow);
}
.note-dialog::backdrop { background: rgba(26, 43, 51, 0.35); }
.note-dialog form {
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
  padding: 1rem 1.1rem 1.25rem;
}
.note-dialog h3 {
  margin: 0;
  font-family: var(--display);
}
.note-dialog label {
  display: flex;
  flex-direction: column;
  gap: 0.3rem;
  font-size: 0.85rem;
  color: var(--muted);
}
.note-dialog textarea,
.note-dialog input[type="number"] {
  font: inherit;
  color: var(--ink);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 0.5rem 0.6rem;
  background: #fff;
}
.note-text-wrap {
  display: flex;
  align-items: flex-start;
  gap: 0.35rem;
}
.note-text-wrap textarea {
  flex: 1;
  min-width: 0;
}
.note-voice-btn {
  flex-shrink: 0;
  padding: 0.45rem;
  line-height: 0;
}
.note-voice-btn.listening {
  color: var(--accent);
  border-color: var(--accent);
  animation: voice-pulse 1.2s ease-in-out infinite;
}
@keyframes voice-pulse {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.55; }
}
.note-voice-status {
  font-size: 0.8rem;
  color: var(--accent);
  min-height: 1.1em;
}
.note-voice-status.is-error { color: #b54a4a; }
.note-dialog input[type="range"] { width: 100%; accent-color: var(--accent); }
.criteria-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0.5rem;
}
.dialog-actions {
  display: flex;
  justify-content: flex-end;
  gap: 0.5rem;
  margin-top: 0.25rem;
}

.housing-panel {
  display: none;
  position: absolute;
  top: 0.5rem;
  right: 0.5rem;
  z-index: 950;
  width: min(92vw, 22rem);
  max-height: calc(100% - 1rem);
  overflow: auto;
  background: rgba(247, 250, 251, 0.97);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 0.65rem 0.75rem 0.85rem;
  box-shadow: var(--shadow);
}
.housing-panel.open { display: block; }
.housing-tabs {
  display: flex;
  flex-wrap: wrap;
  gap: 0.25rem;
  margin-bottom: 0.65rem;
}
.housing-tabs .auth-tab { font-size: 0.75rem; padding: 0.3rem 0.45rem; }
.housing-list { list-style: none; padding: 0; margin: 0.5rem 0 0; display: grid; gap: 0.4rem; }
.housing-list li {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.35rem;
  font-size: 0.85rem;
  border-top: 1px solid var(--border);
  padding-top: 0.35rem;
}
.housing-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.72rem;
  margin-top: 0.5rem;
}
.housing-table th, .housing-table td {
  border-bottom: 1px solid var(--border);
  padding: 0.25rem 0.2rem;
  text-align: left;
  white-space: nowrap;
}
.housing-filters {
  display: grid;
  gap: 0.35rem;
  margin-bottom: 0.5rem;
  font-size: 0.8rem;
}
.housing-filters input[type="number"] { width: 4rem; }
.housing-district-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 0.25rem;
  margin-bottom: 0.65rem;
}
.housing-panel label {
  display: flex;
  justify-content: space-between;
  gap: 0.5rem;
  font-size: 0.85rem;
  color: var(--muted);
  margin-bottom: 0.35rem;
}
.housing-panel label input[type="number"] { width: 4rem; }
.muted { color: var(--muted); font-size: 0.85rem; }
#housing-place-anchor.active { outline: 2px solid var(--accent); }

@media (max-width: 899px) {
  .sheet.sheet-peek { max-height: 5.5rem; }
  .sheet.sheet-half { max-height: 45dvh; }
  .sheet.sheet-full { max-height: 78dvh; }
}

@media (min-width: 900px) {
  .app {
    grid-template-columns: 1fr 360px;
    grid-template-rows: auto 1fr;
  }
  .topbar { grid-column: 1 / -1; }
  .map-wrap { grid-column: 1; }
  .place-note-fab {
    width: 3.25rem;
    height: 3.25rem;
    bottom: max(1.5rem, env(safe-area-inset-bottom, 0px));
    right: calc(360px + 1rem);
  }
  .sheet {
    position: relative;
    max-height: none;
    height: 100%;
    border-radius: 0;
    box-shadow: none;
    border-left: 1px solid var(--border);
    grid-column: 2;
    grid-row: 2;
  }
  .sheet-handle { display: none; }
  .note-dialog {
    border-radius: 12px;
    margin: auto;
  }
}
