/* ============================================================
   NT AG Property Dashboard — Mobile-first styles
   ============================================================ */

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

:root {
  --closed:     #DC2626;
  --critical:   #F97316;
  --active:     #16A34A;
  --bg:         #F3F4F6;
  --card-bg:    #FFFFFF;
  --header-bg:  #1E3A5F;
  --accent:     #2563EB;
  --text:       #111827;
  --muted:      #6B7280;
  --border:     #E5E7EB;
  --nav-h:      64px;
}

html, body {
  min-height: 100%;
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  -webkit-tap-highlight-color: transparent;
}

body {
  overflow-x: hidden;
}

/* ---- HEADER ---------------------------------------------------- */

#dash-header {
  position: fixed;
  top: 0; left: 0; right: 0;
  background: var(--header-bg);
  color: #fff;
  z-index: 100;
  padding: 14px 16px 0;
  box-shadow: 0 2px 8px rgba(0,0,0,0.25);
}

#header-top {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  margin-bottom: 12px;
}

#map-back-link {
  display: inline-block;
  color: #93C5FD;
  font-size: 12px;
  font-weight: 600;
  text-decoration: none;
  margin-bottom: 4px;
}
#map-back-link:hover { color: #fff; }

#dash-header h1 {
  font-size: 16px;
  font-weight: 800;
  line-height: 1.25;
  letter-spacing: -0.2px;
}

#header-sub {
  font-size: 11px;
  opacity: 0.65;
  margin-top: 3px;
}

#sync-status {
  display: flex;
  align-items: center;
  gap: 5px;
  font-size: 11px;
  opacity: 0.85;
  white-space: nowrap;
  flex-shrink: 0;
}

#sync-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: #9CA3AF;
  flex-shrink: 0;
}

/* Stats row */
#stats-row {
  display: flex;
  gap: 8px;
  margin-bottom: 10px;
}

.stat-chip {
  display: flex;
  flex-direction: column;
  align-items: center;
  background: rgba(255,255,255,0.12);
  border-radius: 8px;
  padding: 7px 16px;
  flex: 1;
}

.stat-chip.closed    { border-top: 3px solid var(--closed); }
.stat-chip.critical  { border-top: 3px solid var(--critical); }
.stat-chip.total     { border-top: 3px solid #60A5FA; }

.stat-num {
  font-size: 24px;
  font-weight: 800;
  line-height: 1;
}

.stat-label {
  font-size: 10px;
  opacity: 0.75;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  margin-top: 2px;
}

/* Filter tabs */
#filter-tabs {
  display: flex;
  margin: 0 -16px;
}

.filter-tab {
  flex: 1;
  border: none;
  background: rgba(255,255,255,0.08);
  color: rgba(255,255,255,0.6);
  padding: 9px 4px;
  font-size: 13px;
  font-weight: 500;
  cursor: pointer;
  transition: background 0.15s, color 0.15s;
  border-bottom: 3px solid transparent;
}

.filter-tab.active {
  color: #fff;
  font-weight: 700;
  border-bottom-color: #60A5FA;
  background: rgba(255,255,255,0.15);
}

/* Search */
#search-bar {
  margin: 0 -16px;
  padding: 8px 12px;
  background: rgba(0,0,0,0.15);
}

#search-input {
  width: 100%;
  border: none;
  border-radius: 8px;
  padding: 9px 14px;
  font-size: 14px;
  background: rgba(255,255,255,0.92);
  color: var(--text);
  outline: none;
}

/* ---- MAIN ------------------------------------------------------ */

#main-content {
  margin-top: 200px; /* overridden by JS fitHeader() */
  padding-bottom: 20px;
  margin-bottom: var(--nav-h);
}

.view { display: none; }
.view.active { display: block; }

/* ---- CARDS ----------------------------------------------------- */

#cards-container {
  padding: 12px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.church-card {
  background: var(--card-bg);
  border-radius: 12px;
  padding: 15px;
  box-shadow: 0 1px 4px rgba(0,0,0,0.08), 0 2px 8px rgba(0,0,0,0.04);
  border-left: 5px solid var(--border);
}

.church-card.status-closed      { border-left-color: var(--closed); }
.church-card.status-critical    { border-left-color: var(--critical); }
.church-card.status-active      { border-left-color: var(--active); }
.church-card.status-evaluating  { border-left-color: #7C3AED; }
.church-card.status-gc          { border-left-color: #0891B2; }

.card-top {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 8px;
  margin-bottom: 3px;
}

.card-name {
  font-size: 15px;
  font-weight: 700;
  line-height: 1.3;
  flex: 1;
}

.status-badge {
  font-size: 10px;
  font-weight: 700;
  padding: 3px 8px;
  border-radius: 20px;
  text-transform: uppercase;
  letter-spacing: 0.4px;
  white-space: nowrap;
  flex-shrink: 0;
}

.status-badge.status-closed      { background: #FEE2E2; color: var(--closed); }
.status-badge.status-critical    { background: #FFEDD5; color: #C2410C; }
.status-badge.status-active      { background: #DCFCE7; color: #15803D; }
.status-badge.status-evaluating  { background: #EDE9FE; color: #6D28D9; }
.status-badge.status-gc          { background: #CFFAFE; color: #0E7490; }

.card-location {
  font-size: 12px;
  color: var(--muted);
  margin-bottom: 1px;
}

.card-section {
  font-size: 11px;
  color: #9CA3AF;
  margin-bottom: 8px;
}

/* Score row */
.score-row {
  display: flex;
  align-items: center;
  gap: 7px;
  margin-bottom: 12px;
  flex-wrap: wrap;
}

.score-label {
  font-size: 11px;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.3px;
  font-weight: 600;
}

.score-val {
  font-size: 20px;
  font-weight: 800;
  line-height: 1;
}

.score-delta {
  font-size: 11px;
  font-weight: 700;
  padding: 2px 6px;
  border-radius: 10px;
}

.delta-up   { background: #DCFCE7; color: #15803D; }
.delta-down { background: #FEE2E2; color: #DC2626; }

.attend-val {
  font-size: 12px;
  color: var(--muted);
  margin-left: auto;
}

/* Demographics */
.demo-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 6px;
  margin-bottom: 12px;
}

.demo-cell {
  background: #F9FAFB;
  border-radius: 8px;
  padding: 7px 9px;
}

.demo-label {
  display: block;
  font-size: 9px;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.4px;
  margin-bottom: 3px;
}

.demo-val {
  font-size: 12px;
  font-weight: 600;
  color: var(--text);
  line-height: 1.3;
}

.card-divider {
  border: none;
  border-top: 1px solid var(--border);
  margin: 0 0 10px;
}

/* Tracking chips */
.tracking-row {
  display: flex;
  flex-wrap: wrap;
  gap: 5px;
  margin-bottom: 9px;
}

.tracking-chip {
  font-size: 11px;
  font-weight: 600;
  padding: 3px 9px;
  border-radius: 20px;
}

.chip-on  { background: #DCFCE7; color: #15803D; }
.chip-off { background: #F3F4F6; color: #9CA3AF; }

/* Makeover */
.makeover-row {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 10px;
}

.makeover-stage {
  border: none;
  border-radius: 6px;
  font-size: 11px;
  font-weight: 700;
  padding: 5px 11px;
  cursor: pointer;
  transition: opacity 0.15s, transform 0.1s;
  letter-spacing: 0.2px;
}

.makeover-stage:active { transform: scale(0.96); }

.mo-none      { background: #F3F4F6; color: #6B7280; }
.mo-requested { background: #FEF9C3; color: #854D0E; }
.mo-waiting   { background: #DBEAFE; color: #1E40AF; }
.mo-completed { background: #DCFCE7; color: #15803D; }

.tracking-notes-list {
  margin-bottom: 8px;
}

.tracking-note-item {
  font-size: 12px;
  color: var(--text);
  padding: 4px 0;
  border-bottom: 1px solid var(--border);
  line-height: 1.4;
}

.tracking-note-item:last-child { border-bottom: none; }

.tn-label {
  font-weight: 700;
  color: var(--muted);
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.3px;
  margin-right: 4px;
}

.card-updated {
  font-size: 10px;
  color: #9CA3AF;
  text-align: right;
  margin-bottom: 6px;
}

.tracking-note-wrap {
  padding: 6px 0 4px;
}

.tracking-note-input {
  font-size: 13px;
  min-height: 60px;
}

.card-notes {
  font-size: 12px;
  color: var(--muted);
  font-style: italic;
  background: #FFFBEB;
  border-left: 3px solid #FCD34D;
  border-radius: 0 6px 6px 0;
  padding: 6px 10px;
  margin-bottom: 10px;
}

.edit-btn {
  display: block;
  width: 100%;
  background: var(--accent);
  color: #fff;
  border: none;
  border-radius: 8px;
  padding: 9px;
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
  transition: opacity 0.15s;
  margin-top: 2px;
}

.edit-btn:active { opacity: 0.85; }

.empty-state {
  text-align: center;
  color: var(--muted);
  padding: 60px 20px;
  font-size: 15px;
}

/* ---- MAP ------------------------------------------------------- */

#map-view {
  position: relative;
  height: calc(100vh - 200px - var(--nav-h));
}

#dashboard-map {
  width: 100%;
  height: 100%;
}

/* Bottom sheet */
#map-sheet {
  position: absolute;
  bottom: 0; left: 0; right: 0;
  background: #fff;
  border-radius: 20px 20px 0 0;
  padding: 10px 16px 24px;
  box-shadow: 0 -6px 24px rgba(0,0,0,0.15);
  z-index: 500;
  max-height: 55%;
  overflow-y: auto;
}

#map-sheet.hidden { display: none; }

#map-sheet-handle {
  width: 36px;
  height: 4px;
  background: #D1D5DB;
  border-radius: 2px;
  margin: 2px auto 14px;
  cursor: pointer;
}

.sheet-name {
  font-size: 15px;
  font-weight: 700;
  margin-bottom: 3px;
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 4px;
}

.sheet-loc {
  font-size: 12px;
  color: var(--muted);
  margin-bottom: 10px;
}

.sheet-chips { display: flex; flex-wrap: wrap; gap: 5px; margin-bottom: 10px; }

.sheet-edit-btn {
  display: block;
  width: 100%;
  background: var(--accent);
  color: #fff;
  border: none;
  border-radius: 8px;
  padding: 10px;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  margin-top: 8px;
}

/* ---- BOTTOM NAV ------------------------------------------------ */

#bottom-nav {
  position: fixed;
  bottom: 0; left: 0; right: 0;
  height: var(--nav-h);
  background: #fff;
  border-top: 1px solid var(--border);
  display: flex;
  z-index: 200;
  box-shadow: 0 -2px 10px rgba(0,0,0,0.06);
  padding-bottom: env(safe-area-inset-bottom, 0);
}

.nav-btn {
  flex: 1;
  border: none;
  background: none;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 3px;
  cursor: pointer;
  color: var(--muted);
  font-size: 11px;
  font-weight: 500;
  transition: color 0.15s;
  padding-bottom: 4px;
}

.nav-btn.active { color: var(--accent); }

.nav-icon {
  font-size: 21px;
  line-height: 1;
}

/* ---- MODALS ---------------------------------------------------- */

#settings-overlay,
#edit-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.45);
  z-index: 400;
  display: flex;
  align-items: flex-end;
}

#settings-overlay.hidden,
#edit-overlay.hidden { display: none; }

#settings-panel,
#edit-panel {
  background: #fff;
  border-radius: 20px 20px 0 0;
  width: 100%;
  max-height: 85vh;
  overflow-y: auto;
  padding: 20px 20px 40px;
}

.modal-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 16px;
}

.modal-header h2 {
  font-size: 17px;
  font-weight: 700;
}

.modal-close {
  border: none;
  background: #F3F4F6;
  border-radius: 50%;
  width: 32px;
  height: 32px;
  font-size: 15px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}

/* Settings body */
#settings-desc {
  font-size: 13px;
  color: var(--muted);
  margin-bottom: 14px;
  line-height: 1.5;
}

.settings-label {
  display: block;
  font-size: 11px;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.4px;
  margin-bottom: 5px;
  font-weight: 600;
}

#sheets-url-input {
  width: 100%;
  border: 1.5px solid var(--border);
  border-radius: 8px;
  padding: 11px 13px;
  font-size: 13px;
  margin-bottom: 12px;
  outline: none;
  color: var(--text);
}

#sheets-url-input:focus { border-color: var(--accent); }

#save-settings {
  width: 100%;
  background: var(--accent);
  color: #fff;
  border: none;
  border-radius: 8px;
  padding: 13px;
  font-size: 15px;
  font-weight: 600;
  cursor: pointer;
  margin-bottom: 18px;
}

#settings-help details {
  font-size: 13px;
  color: var(--muted);
}

#settings-help summary {
  cursor: pointer;
  color: var(--accent);
  font-weight: 600;
  padding: 6px 0;
}

#settings-help ol {
  padding-left: 20px;
  margin-top: 8px;
  line-height: 2;
}

/* Edit form */
#edit-church-name {
  font-size: 14px;
  font-weight: 600;
  color: var(--muted);
  margin-bottom: 14px;
  display: block;
}

.form-section {
  font-size: 11px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  color: var(--muted);
  padding: 8px 0 6px;
  margin-bottom: 6px;
  border-top: 1px solid var(--border);
}

.form-section:first-child { border-top: none; padding-top: 0; }

.form-group { margin-bottom: 10px; }

.form-label {
  display: block;
  font-size: 11px;
  color: var(--muted);
  margin-bottom: 4px;
  text-transform: uppercase;
  letter-spacing: 0.3px;
  font-weight: 600;
}

.form-input {
  width: 100%;
  border: 1.5px solid var(--border);
  border-radius: 8px;
  padding: 10px 12px;
  font-size: 14px;
  color: var(--text);
  background: #fff;
  outline: none;
  font-family: inherit;
}

.form-input:focus { border-color: var(--accent); }

textarea.form-input {
  resize: vertical;
  min-height: 72px;
}

.form-toggle-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 9px 0;
  border-bottom: 1px solid var(--border);
}

/* Toggle switch */
.toggle-switch {
  position: relative;
  display: inline-block;
  width: 46px;
  height: 26px;
  flex-shrink: 0;
}

.toggle-switch input { opacity: 0; width: 0; height: 0; }

.toggle-slider {
  position: absolute;
  inset: 0;
  background: #D1D5DB;
  border-radius: 26px;
  cursor: pointer;
  transition: background 0.2s;
}

.toggle-slider::before {
  content: '';
  position: absolute;
  width: 20px;
  height: 20px;
  left: 3px;
  top: 3px;
  background: #fff;
  border-radius: 50%;
  transition: transform 0.2s;
  box-shadow: 0 1px 3px rgba(0,0,0,0.2);
}

.toggle-switch input:checked + .toggle-slider { background: var(--accent); }
.toggle-switch input:checked + .toggle-slider::before { transform: translateX(20px); }

/* Edit actions */
#edit-actions {
  display: flex;
  gap: 10px;
  margin-top: 18px;
}

#edit-cancel {
  flex: 1;
  background: #F3F4F6;
  color: var(--text);
  border: none;
  border-radius: 8px;
  padding: 12px;
  font-size: 15px;
  font-weight: 600;
  cursor: pointer;
}

#edit-save {
  flex: 2;
  background: var(--accent);
  color: #fff;
  border: none;
  border-radius: 8px;
  padding: 12px;
  font-size: 15px;
  font-weight: 600;
  cursor: pointer;
}

/* ---- ANALYTICS VIEW -------------------------------------------- */

#analytics-container {
  padding: 12px;
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.an-kpi-row {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
}

.an-kpi-chip {
  background: var(--card-bg);
  border-radius: 10px;
  padding: 10px 8px;
  text-align: center;
  box-shadow: 0 1px 4px rgba(0,0,0,0.07);
  border-top: 3px solid #E5E7EB;
}

.an-kpi-chip.blue   { border-top-color: #2563EB; }
.an-kpi-chip.green  { border-top-color: #16A34A; }
.an-kpi-chip.orange { border-top-color: #F97316; }
.an-kpi-chip.red    { border-top-color: #DC2626; }
.an-kpi-chip.yellow { border-top-color: #D97706; }

.an-kpi-num {
  font-size: 22px;
  font-weight: 800;
  line-height: 1;
  color: var(--text);
}

.an-kpi-label {
  font-size: 10px;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.3px;
  margin-top: 3px;
}

.an-charts-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
}

.an-chart-card {
  background: var(--card-bg);
  border-radius: 12px;
  padding: 13px 12px;
  box-shadow: 0 1px 4px rgba(0,0,0,0.07);
}

.an-chart-card.an-full {
  grid-column: 1 / -1;
}

.an-chart-title {
  font-size: 12px;
  font-weight: 700;
  color: var(--text);
  text-transform: uppercase;
  letter-spacing: 0.3px;
  margin-bottom: 10px;
}

.an-chart-wrap {
  height: 160px;
  position: relative;
  touch-action: pan-y;
}

.an-chart-wrap canvas {
  touch-action: pan-y !important;
}

.an-chart-wrap.donut-wrap { height: 160px; }

.an-section-card {
  background: var(--card-bg);
  border-radius: 12px;
  padding: 14px;
  box-shadow: 0 1px 4px rgba(0,0,0,0.07);
}

.an-section-title {
  font-size: 13px;
  font-weight: 700;
  margin-bottom: 4px;
}

.an-section-desc {
  font-size: 11px;
  color: var(--muted);
  margin-bottom: 10px;
}

.an-priority-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 9px 0;
  border-bottom: 1px solid var(--border);
  gap: 8px;
}

.an-priority-item:last-child { border-bottom: none; }

.an-priority-name {
  font-size: 13px;
  font-weight: 600;
}

.an-priority-city {
  font-size: 11px;
  color: var(--muted);
  margin-top: 2px;
}

.an-priority-right {
  display: flex;
  align-items: center;
  gap: 6px;
  flex-shrink: 0;
}

.an-priority-score {
  font-size: 18px;
  font-weight: 800;
  line-height: 1;
}

.an-empty {
  text-align: center;
  color: var(--muted);
  padding: 20px;
  font-size: 13px;
}

/* ---- SORT BAR (cards view) ------------------------------------- */

#sort-bar {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 14px 4px;
  font-size: 12px;
  color: var(--muted);
}

#sort-bar label {
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.4px;
  font-weight: 600;
}

#sort-select {
  flex: 1;
  border: 1.5px solid var(--border);
  border-radius: 8px;
  padding: 7px 10px;
  font-size: 13px;
  background: #fff;
  color: var(--text);
  outline: none;
  font-family: inherit;
}

#sort-select:focus { border-color: var(--accent); }

/* ---- BY CITY VIEW ---------------------------------------------- */

#bycity-container {
  padding: 12px;
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.bycity-group {
  background: var(--card-bg);
  border-radius: 12px;
  padding: 10px 12px 4px;
  box-shadow: 0 1px 4px rgba(0,0,0,0.07);
}

.bycity-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-size: 14px;
  font-weight: 800;
  color: var(--text);
  padding: 4px 2px 8px;
  border-bottom: 2px solid var(--border);
  margin-bottom: 6px;
  letter-spacing: -0.2px;
}

.bycity-count {
  font-size: 11px;
  color: var(--muted);
  font-weight: 700;
  background: #F3F4F6;
  padding: 2px 8px;
  border-radius: 12px;
}

.bycity-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 10px 4px;
  border-bottom: 1px solid var(--border);
  cursor: pointer;
  gap: 8px;
  transition: background 0.1s;
}

.bycity-item:last-child  { border-bottom: none; }
.bycity-item:active      { background: #F9FAFB; }

.bycity-info { flex: 1; min-width: 0; }

.bycity-name {
  font-size: 13px;
  font-weight: 600;
  color: var(--text);
  line-height: 1.3;
}

.bycity-sub {
  font-size: 11px;
  color: var(--muted);
  margin-top: 2px;
}

.bycity-right {
  display: flex;
  align-items: center;
  gap: 7px;
  flex-shrink: 0;
}

.bycity-score {
  font-size: 17px;
  font-weight: 800;
  line-height: 1;
}

/* ---- EDIT MODAL: HISTORY CHART --------------------------------- */

#edit-history-wrap {
  margin-top: 22px;
  padding-top: 18px;
  border-top: 2px solid var(--border);
}

#edit-history-wrap .form-section {
  border-top: none;
  padding-top: 0;
}

.edit-history-status {
  font-size: 12px;
  color: var(--muted);
  margin-bottom: 10px;
}

.edit-history-chart-wrap {
  position: relative;
  height: 200px;
  touch-action: pan-y;
}

.edit-history-chart-wrap canvas {
  touch-action: pan-y !important;
}

/* ---- ANALYTICS PRIORITY ITEM CLICKABLE ------------------------- */

.an-priority-item {
  cursor: pointer;
  transition: background 0.1s;
}

.an-priority-item:active {
  background: #F9FAFB;
}

/* ---- SAFE AREA (iPhone home indicator) ------------------------- */
@supports (padding-bottom: env(safe-area-inset-bottom)) {
  #bottom-nav {
    padding-bottom: env(safe-area-inset-bottom);
    height: calc(var(--nav-h) + env(safe-area-inset-bottom));
  }
  #main-content {
    margin-bottom: calc(var(--nav-h) + env(safe-area-inset-bottom));
  }
}
