/* ── Reset & base ─────────────────────────────────────────────────────────── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  font-size: 15px;
  background: #fffde7;
  color: #1a1a1a;
  min-height: 100vh;
}

.hidden { display: none !important; }

/* ── Login ────────────────────────────────────────────────────────────────── */
#login-screen, #newpassword-screen {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 100vh;
  background: #1a2a3a;
  padding: 16px;
}

.login-card {
  background: white;
  border-radius: 12px;
  padding: 40px;
  width: 100%;
  max-width: 380px;
  box-shadow: 0 8px 32px rgba(0,0,0,0.2);
  text-align: center;
}

.login-card .logo { font-size: 40px; margin-bottom: 8px; }
.login-card h1 { font-size: 22px; font-weight: 700; color: #1a2a3a; }
.login-card .subtitle { color: #666; margin-bottom: 24px; font-size: 14px; }
.login-card .field { text-align: left; margin-bottom: 16px; }
.login-card .btn-primary { width: 100%; margin-top: 8px; }

/* ── Header ───────────────────────────────────────────────────────────────── */
header {
  background: #1a2a3a;
  color: white;
  padding: 0 16px;
  height: 52px;
  display: flex;
  align-items: center;
  gap: 12px;
  position: sticky;
  top: 0;
  z-index: 100;
  box-shadow: 0 2px 8px rgba(0,0,0,0.2);
}

.header-left { display: flex; align-items: center; gap: 8px; flex: 1; min-width: 0; }
.logo-sm { font-size: 20px; flex-shrink: 0; }
.site-title { font-size: 15px; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

#main-nav { display: flex; gap: 2px; flex-shrink: 0; }
#main-nav button {
  background: transparent;
  border: none;
  color: rgba(255,255,255,0.7);
  padding: 6px 12px;
  border-radius: 6px;
  cursor: pointer;
  font-size: 14px;
  font-weight: 500;
  transition: all 0.15s;
  white-space: nowrap;
}
#main-nav button:hover { background: rgba(255,255,255,0.1); color: white; }
#main-nav button.active { background: rgba(255,255,255,0.15); color: white; }

.header-right { display: flex; align-items: center; gap: 8px; flex-shrink: 0; }
#user-label { font-size: 12px; color: rgba(255,255,255,0.6); display: none; }

/* ── Toolbar ──────────────────────────────────────────────────────────────── */
.toolbar {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 16px;
  background: white;
  border-bottom: 1px solid #e5e7eb;
}

.toolbar input[type="search"] { flex: 1; min-width: 0; }
.toolbar select { flex-shrink: 0; width: auto; }
.count-label { font-size: 12px; color: #888; white-space: nowrap; flex-shrink: 0; margin-left: auto; }

/* ── Status badges ────────────────────────────────────────────────────────── */
.status-badge {
  font-size: 11px;
  font-weight: 600;
  padding: 3px 8px;
  border-radius: 20px;
  white-space: nowrap;
  flex-shrink: 0;
}
.status-pending    { background: #fef3c7; color: #92400e; }
.status-clean      { background: #d1fae5; color: #065f46; }
.status-flagged    { background: #fee2e2; color: #991b1b; }
.status-no_reading { background: #f3f4f6; color: #374151; }
.status-stale { background: #dbeafe; color: #1e40af; }

/* ── Toast notification ───────────────────────────────────────────────────── */
#toast {
  position: fixed;
  bottom: 24px;
  left: 50%;
  transform: translateX(-50%);
  background: #166534;
  color: white;
  padding: 12px 24px;
  border-radius: 8px;
  font-size: 15px;
  font-weight: 500;
  box-shadow: 0 4px 12px rgba(0,0,0,0.2);
  z-index: 300;
  opacity: 1;
  transition: opacity 0.4s ease;
  white-space: nowrap;
}
#toast.hidden  { display: none; }
#toast.fading  { opacity: 0; }

/* ── Desktop: card grid ───────────────────────────────────────────────────── */
#household-list {
  padding: 12px 16px;
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
  gap: 8px;
}

.household-card {
  background: white;
  border: 1px solid #e5e7eb;
  border-radius: 10px;
  padding: 12px 14px;
  cursor: pointer;
  transition: all 0.15s;
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 8px;
}
.household-card:hover { border-color: #2563eb; box-shadow: 0 2px 8px rgba(37,99,235,0.1); }
.household-card:active { background: #f0f9ff; }

.card-main { min-width: 0; flex: 1; }
.card-name { font-weight: 600; font-size: 14px; margin-bottom: 2px; }
.card-address { font-size: 12px; color: #555; }
.card-meter { font-size: 12px; color: #888; margin-top: 2px; }
.card-flags { font-size: 11px; color: #d97706; margin-top: 3px; }

/* ── Forms ────────────────────────────────────────────────────────────────── */
.field { margin-bottom: 14px; }
.field label {
  display: block;
  font-size: 13px;
  font-weight: 600;
  color: #374151;
  margin-bottom: 5px;
}

input[type="text"],
input[type="email"],
input[type="password"],
input[type="number"],
input[type="date"],
input[type="search"],
select,
textarea {
  width: 100%;
  padding: 10px 12px;
  border: 1px solid #d1d5db;
  border-radius: 7px;
  font-size: 16px; /* 16px prevents iOS zoom on focus */
  font-family: inherit;
  color: #1a1a1a;
  background: white;
  outline: none;
  transition: border-color 0.15s;
  -webkit-appearance: none;
}
input:focus, select:focus, textarea:focus { border-color: #2563eb; box-shadow: 0 0 0 3px rgba(37,99,235,0.1); }

/* ── Issue flags collapsible ──────────────────────────────────────────────── */
#flags-details { border: 1px solid #d1d5db; border-radius: 7px; padding: 0; }

.flags-toggle {
  display: flex;
  align-items: center;
  justify-content: space-between;
  cursor: pointer;
  font-size: 13px;
  font-weight: 600;
  color: #374151;
  padding: 10px 12px;
  list-style: none;
  user-select: none;
}
.flags-toggle::-webkit-details-marker { display: none; }
.flags-toggle::after { content: "▸"; font-size: 11px; color: #6b7280; }
details[open] .flags-toggle { border-bottom: 1px solid #d1d5db; }
details[open] .flags-toggle::after { content: "▾"; }

.flags-count {
  font-size: 12px;
  font-weight: 600;
  color: #d97706;
  margin-left: 6px;
  flex: 1;
}

#flags-details .checklist { padding: 10px 12px 12px; }

.checklist { display: flex; flex-direction: column; gap: 10px; }
.checklist label {
  font-weight: normal;
  display: flex;
  align-items: center;
  gap: 10px;
  cursor: pointer;
  padding: 6px 0;
  font-size: 15px;
}
.checklist input[type="checkbox"] {
  width: 20px;
  height: 20px;
  flex-shrink: 0;
  accent-color: #2563eb;
}

/* ── Buttons ──────────────────────────────────────────────────────────────── */
.btn-primary {
  background: #2563eb;
  color: white;
  border: none;
  border-radius: 8px;
  padding: 12px 20px;
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.15s;
  touch-action: manipulation;
}
.btn-primary:hover { background: #1d4ed8; }
.btn-primary:active { background: #1e40af; }
.btn-primary:disabled { background: #93c5fd; cursor: not-allowed; }

.btn-secondary {
  background: white;
  color: #374151;
  border: 1px solid #d1d5db;
  border-radius: 8px;
  padding: 12px 20px;
  font-size: 16px;
  font-weight: 500;
  cursor: pointer;
  touch-action: manipulation;
}
.btn-secondary:hover { background: #f9fafb; }

.btn-danger {
  background: #dc2626;
  color: white;
  border: none;
  border-radius: 8px;
  padding: 12px 20px;
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
  touch-action: manipulation;
}
.btn-danger:hover { background: #b91c1c; }

.btn-sm {
  background: rgba(255,255,255,0.15);
  color: white;
  border: 1px solid rgba(255,255,255,0.3);
  border-radius: 6px;
  padding: 5px 10px;
  font-size: 13px;
  cursor: pointer;
  white-space: nowrap;
}
.btn-sm:hover { background: rgba(255,255,255,0.25); }

.btn-link {
  background: none;
  border: none;
  color: #2563eb;
  font-size: 14px;
  cursor: pointer;
  padding: 4px 0;
  text-decoration: underline;
  display: flex;
  align-items: center;
  gap: 4px;
}

/* ── Messages ─────────────────────────────────────────────────────────────── */
.error-msg {
  background: #fee2e2;
  color: #991b1b;
  border-radius: 7px;
  padding: 10px 14px;
  font-size: 14px;
  margin-bottom: 14px;
}
.warn-msg {
  background: #fef3c7;
  color: #92400e;
  border-radius: 6px;
  padding: 10px 12px;
  font-size: 14px;
  margin-top: 6px;
  font-weight: 500;
}
.result-box {
  background: #f0fdf4;
  color: #166534;
  border: 1px solid #bbf7d0;
  border-radius: 7px;
  padding: 12px 16px;
  font-size: 14px;
  margin-top: 12px;
}
.result-box.error { background: #fef2f2; color: #991b1b; border-color: #fecaca; }

/* ── Modal — desktop ──────────────────────────────────────────────────────── */
#modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.5);
  display: flex;
  align-items: flex-start;
  justify-content: center;
  z-index: 200;
  overflow-y: auto;
  padding: 40px 16px;
}

.modal {
  background: white;
  border-radius: 12px;
  padding: 24px;
  width: 100%;
  max-width: 520px;
  position: relative;
}

.modal h2 { font-size: 17px; font-weight: 700; margin-bottom: 10px; padding-right: 28px; }
.modal h3 { font-size: 15px; font-weight: 600; margin-bottom: 12px; color: #374151; }
.modal hr { border: none; border-top: 1px solid #e5e7eb; margin: 16px 0; }

.modal-close {
  position: absolute;
  top: 14px; right: 14px;
  background: #f3f4f6;
  border: none;
  width: 30px; height: 30px;
  border-radius: 50%;
  font-size: 18px;
  cursor: pointer;
  color: #666;
  line-height: 1;
  display: flex; align-items: center; justify-content: center;
}

.modal-info {
  background: #f8fafc;
  border: 1px solid #e5e7eb;
  border-radius: 8px;
  padding: 10px 14px;
  margin-bottom: 14px;
  font-size: 13px;
  line-height: 1.7;
}
.modal-info .info-meter { font-size: 15px; font-weight: 700; margin-bottom: 2px; }
.modal-info .info-reads { color: #555; }

/* Meter history (details/summary for replaced meters) */
.meter-history { margin-bottom: 2px; }
.meter-history-summary {
  font-size: 15px;
  font-weight: 700;
  cursor: pointer;
  list-style: none;
  display: flex;
  align-items: center;
  gap: 6px;
  user-select: none;
}
.meter-history-summary::-webkit-details-marker { display: none; }
.meter-history-summary::after { content: "▸"; font-size: 11px; color: #6b7280; }
details[open] .meter-history-summary::after { content: "▾"; }
.meter-event-row {
  margin-top: 6px;
  padding: 6px 8px;
  background: #f3f4f6;
  border-radius: 6px;
  font-size: 12.5px;
  color: #444;
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.modal-actions { display: flex; gap: 10px; justify-content: flex-end; margin-top: 16px; }

/* Meter replacement toggle */
.meter-replacement-toggle {
  margin-top: 4px;
}

#meter-replacement-section .replacement-form { margin-top: 12px; }

/* ── Admin panel ──────────────────────────────────────────────────────────── */
.admin-panel {
  max-width: 620px;
  margin: 24px auto;
  padding: 0 20px;
}

.admin-section {
  background: white;
  border: 1px solid #e5e7eb;
  border-radius: 10px;
  margin-bottom: 16px;
  overflow: hidden;
}

.admin-section-header {
  background: #1a2a3a;
  padding: 14px 20px;
}
.admin-section-header h2 {
  font-size: 16px;
  font-weight: 700;
  color: white;
  margin: 0 0 2px;
}
.admin-section-header p {
  font-size: 13px;
  color: rgba(255,255,255,0.7);
  margin: 0;
}

.admin-section-body {
  padding: 20px;
}

.admin-row { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; }
.admin-row select { max-width: 160px; }

/* ── PDF generation queue ────────────────────────────────────────────────── */
.pdf-section { margin-bottom: 20px; }

.pdf-count-note {
  font-size: 13px;
  color: #6b7280;
  margin: 6px 0 8px;
  min-height: 18px;
}

.pdf-queue-list {
  border: 1px solid #e5e7eb;
  border-radius: 8px;
  overflow: hidden;
  max-height: 280px;
  overflow-y: auto;
}

.pdf-queue-item {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 12px;
  border-bottom: 1px solid #f3f4f6;
  font-size: 13px;
}
.pdf-queue-item:last-child { border-bottom: none; }
.pdf-queue-name { font-weight: 600; flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.pdf-queue-addr { color: #6b7280; font-size: 12px; flex-shrink: 0; }

.pdf-tag-new {
  background: #dbeafe; color: #1e40af;
  font-size: 11px; font-weight: 600;
  padding: 2px 7px; border-radius: 10px;
  flex-shrink: 0;
}
.pdf-tag-corrected {
  background: #fef3c7; color: #92400e;
  font-size: 11px; font-weight: 600;
  padding: 2px 7px; border-radius: 10px;
  flex-shrink: 0;
}

/* ── Mobile styles ────────────────────────────────────────────────────────── */
@media (max-width: 767px) {

  /* Show user label on larger phones in landscape, hide on portrait */
  #user-label { display: none; }

  /* Toolbar: wrap into rows on mobile */
  .toolbar {
    flex-wrap: wrap;
    padding: 8px 12px;
    gap: 6px;
  }
  /* Row 1: street filter + locate button */
  #filter-street  { order: 1; flex: 1 1 calc(60% - 3px); }
  .btn-locate     { order: 1; flex-shrink: 0; }
  /* Row 2: status filter + sort toggle */
  #filter-status  { order: 2; flex: 1 1 calc(55% - 3px); }
  #sort-select    { order: 2; flex-shrink: 0; width: auto; }
  /* Row 3: search full width */
  .toolbar input[type="search"] { order: 3; flex: 1 1 100%; }
  /* Row 4: count */
  .count-label { order: 4; width: 100%; text-align: right; margin-left: 0; }

  /* Street divider full width on mobile list */
  .street-divider { padding: 10px 16px 4px; margin-bottom: 0; }

  /* Compact list instead of card grid */
  #household-list {
    display: block;
    padding: 0;
  }

  .household-card {
    border-radius: 0;
    border-left: none;
    border-right: none;
    border-top: none;
    border-bottom: 1px solid #e5e7eb;
    padding: 14px 16px;
    align-items: center;
    min-height: 64px;
  }

  .household-card:first-child { border-top: 1px solid #e5e7eb; }

  .card-name { font-size: 15px; }
  .card-address { font-size: 13px; }
  .card-meter { display: none; } /* shown in modal instead */

  /* Chevron indicator for tap-ability */
  .household-card::after {
    content: "›";
    font-size: 22px;
    color: #ccc;
    flex-shrink: 0;
    margin-left: 4px;
  }

  /* Modal as bottom sheet */
  #modal-overlay {
    align-items: flex-end;
    padding: 0;
    overflow: hidden;
  }

  .modal {
    border-radius: 16px 16px 0 0;
    max-width: 100%;
    max-height: 92vh;
    overflow-y: auto;
    padding: 20px 16px 32px;
    /* Safe area for notched phones */
    padding-bottom: max(32px, env(safe-area-inset-bottom));
  }

  /* Drag handle visual cue */
  .modal::before {
    content: "";
    display: block;
    width: 36px;
    height: 4px;
    background: #d1d5db;
    border-radius: 2px;
    margin: 0 auto 16px;
  }

  .modal h2 { font-size: 16px; }

  /* Sticky action buttons at bottom of sheet — always visible above keyboard */
  .modal-actions {
    position: sticky;
    bottom: 0;
    background: white;
    border-top: 1px solid #e5e7eb;
    padding: 12px 0 0;
    margin-top: 16px;
    flex-direction: column-reverse;
    gap: 8px;
    z-index: 1;
  }
  .modal-actions .btn-primary,
  .modal-actions .btn-secondary { width: 100%; text-align: center; padding: 14px; }

  /* Bigger reading input on mobile for easy entry */
  #reading-value {
    font-size: 28px;
    font-weight: 700;
    text-align: center;
    padding: 14px;
    letter-spacing: 1px;
  }

  /* Admin panel padding */
  .admin-panel { padding: 0 12px; margin: 16px auto; }
  .admin-section-body { padding: 16px; }
  .admin-row { flex-direction: column; align-items: stretch; }
  .admin-row select { max-width: 100%; }
}

/* ── Desktop tweaks ───────────────────────────────────────────────────────── */
@media (min-width: 768px) {
  #user-label { display: block; }
  .toolbar { flex-wrap: nowrap; }
  .toolbar input[type="search"] { max-width: 280px; }
  #filter-status, #filter-street { width: 160px; }
}

/* ── Large desktop ────────────────────────────────────────────────────────── */
@media (min-width: 1200px) {
  #household-list {
    grid-template-columns: repeat(auto-fill, minmax(320px, 1fr));
  }
}


/* ── Locate button ────────────────────────────────────────────────────────── */
.btn-locate {
  background: white;
  border: 1px solid #d1d5db;
  border-radius: 7px;
  padding: 8px 12px;
  font-size: 13px;
  font-weight: 500;
  color: #374151;
  cursor: pointer;
  white-space: nowrap;
  flex-shrink: 0;
  transition: all 0.15s;
}
.btn-locate:hover { background: #f0fdf4; border-color: #86efac; color: #166534; }
.btn-locate:disabled { opacity: 0.5; cursor: not-allowed; }

/* ── Locate status bar ────────────────────────────────────────────────────── */
.locate-status {
  padding: 8px 16px;
  font-size: 13px;
  background: #f0fdf4;
  border-bottom: 1px solid #bbf7d0;
  color: #166534;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}
.locate-status.error { background: #fef2f2; border-color: #fecaca; color: #991b1b; }
.locate-status-close {
  background: none;
  border: none;
  font-size: 18px;
  line-height: 1;
  cursor: pointer;
  color: inherit;
  opacity: 0.6;
  flex-shrink: 0;
  padding: 0 4px;
}
.locate-status-close:hover { opacity: 1; }

/* ── Street divider ───────────────────────────────────────────────────────── */
.street-divider {
  grid-column: 1 / -1;
  padding: 10px 4px 4px;
  font-size: 12px;
  font-weight: 700;
  color: #6b7280;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  border-bottom: 1px solid #e5e7eb;
  margin-bottom: 2px;
}

/* ── Empty state ──────────────────────────────────────────────────────────── */
.empty-state {
  text-align: center;
  padding: 48px 16px;
  color: #9ca3af;
}
.empty-state p { font-size: 15px; }
