:root {
  color-scheme: light;
  --bg: #f4f6f2;
  --surface: #ffffff;
  --surface-muted: #f8faf7;
  --ink: #17231d;
  --muted: #66746c;
  --line: #dfe6dd;
  --line-strong: #c9d4cb;
  --green: #28724f;
  --green-dark: #175139;
  --green-soft: #e7f4ed;
  --amber: #9a6418;
  --amber-soft: #fff3dc;
  --red: #af3333;
  --red-soft: #ffe8e8;
  --blue: #315f92;
  --blue-soft: #e8f0f9;
  --shadow: 0 18px 45px rgba(27, 44, 34, 0.08);
  --shadow-soft: 0 8px 22px rgba(27, 44, 34, 0.06);
}

* {
  box-sizing: border-box;
}

body {
  min-height: 100vh;
  margin: 0;
  display: grid;
  grid-template-columns: 280px minmax(0, 1fr);
  background: var(--bg);
  color: var(--ink);
  font-family: Inter, "Segoe UI", Arial, sans-serif;
}

body:not(.authenticated) {
  display: block;
}

body:not(.authenticated) .sidebar,
body:not(.authenticated) .shell {
  display: none;
}

body.authenticated .login-screen {
  display: none;
}

body:not(.is-admin) .admin-only {
  display: none !important;
}

button,
input,
select,
textarea {
  font: inherit;
}

button {
  cursor: pointer;
}

.sidebar {
  min-height: 100vh;
  padding: 22px 16px;
  background: linear-gradient(180deg, #123629 0%, #183125 100%);
  color: #f7fbf6;
  border-right: 1px solid rgba(255, 255, 255, 0.08);
}

.login-screen {
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: 24px;
  background: radial-gradient(circle at top left, #e7f4ed 0, transparent 34%), var(--bg);
}

.login-card {
  width: min(100%, 420px);
  display: grid;
  gap: 16px;
  padding: 28px;
  border: 1px solid var(--line);
  border-radius: 16px;
  background: var(--surface);
  box-shadow: var(--shadow);
}

.login-card .brand-mark {
  width: 54px;
  height: 54px;
}

.login-card h1 {
  font-size: 1.75rem;
}

.login-error {
  min-height: 20px;
  margin: 0;
  color: var(--red);
  font-size: 0.9rem;
  font-weight: 700;
}

.brand {
  display: flex;
  gap: 12px;
  align-items: center;
  padding: 0 8px 22px;
  margin-bottom: 4px;
  border-bottom: 1px solid rgba(255, 255, 255, 0.1);
}

.brand-mark {
  width: 46px;
  height: 46px;
  display: grid;
  place-items: center;
  border: 1px solid rgba(255, 255, 255, 0.24);
  border-radius: 10px;
  background: #ecf5e5;
  color: #173729;
  font-weight: 800;
  box-shadow: inset 0 -8px 18px rgba(40, 114, 79, 0.12);
}

.brand strong,
.brand small {
  display: block;
}

.brand small {
  margin-top: 3px;
  color: rgba(255, 255, 255, 0.66);
}

.nav {
  display: grid;
  gap: 4px;
  padding-top: 18px;
}

.nav-item {
  width: 100%;
  min-height: 46px;
  display: flex;
  gap: 10px;
  align-items: center;
  padding: 0 13px;
  border: 0;
  border-radius: 10px;
  background: transparent;
  color: rgba(255, 255, 255, 0.76);
  text-align: left;
  transition: background 0.16s ease, color 0.16s ease, transform 0.16s ease;
}

.nav-item:hover,
.nav-item.active {
  background: rgba(255, 255, 255, 0.13);
  color: #ffffff;
}

.nav-item.active {
  box-shadow: inset 3px 0 0 #9fd29d;
}

.nav-item:hover {
  transform: translateX(2px);
}

.nav-item svg,
button svg,
.search svg {
  width: 18px;
  height: 18px;
  flex: 0 0 auto;
}

.shell {
  min-width: 0;
  padding: 30px;
}

.topbar {
  display: flex;
  justify-content: space-between;
  gap: 18px;
  align-items: flex-start;
  margin-bottom: 22px;
  padding: 20px;
  border: 1px solid var(--line);
  border-radius: 14px;
  background: rgba(255, 255, 255, 0.74);
  box-shadow: var(--shadow-soft);
  backdrop-filter: blur(10px);
}

.eyebrow {
  margin: 0 0 6px;
  color: var(--green);
  font-size: 0.76rem;
  font-weight: 800;
  letter-spacing: 0;
  text-transform: uppercase;
}

h1,
h2 {
  margin: 0;
  letter-spacing: 0;
}

h1 {
  font-size: clamp(1.65rem, 2vw, 2.15rem);
  line-height: 1.1;
}

h2 {
  font-size: 1rem;
}

.topbar-actions,
.dialog-actions > div {
  display: flex;
  gap: 10px;
  align-items: center;
  flex-wrap: wrap;
  justify-content: flex-end;
}

.user-pill {
  min-height: 36px;
  display: inline-flex;
  align-items: center;
  border: 1px solid var(--line);
  border-radius: 999px;
  padding: 0 12px;
  background: var(--surface-muted);
  color: var(--muted);
  font-size: 0.84rem;
  font-weight: 800;
}

.primary-button,
.ghost-button,
.danger-button,
.icon-button {
  min-height: 42px;
  display: inline-flex;
  gap: 8px;
  align-items: center;
  justify-content: center;
  border-radius: 10px;
  border: 1px solid transparent;
  padding: 0 15px;
  font-weight: 700;
  transition: background 0.16s ease, border-color 0.16s ease, transform 0.16s ease, box-shadow 0.16s ease;
}

.primary-button {
  background: var(--green);
  color: #ffffff;
  box-shadow: 0 8px 16px rgba(40, 114, 79, 0.16);
}

.primary-button:hover {
  background: var(--green-dark);
  transform: translateY(-1px);
}

.ghost-button {
  background: var(--surface);
  border-color: var(--line);
  color: var(--ink);
}

.ghost-button:hover,
.icon-button:hover {
  border-color: var(--line-strong);
  background: var(--surface-muted);
}

.danger-button {
  background: #fff8f8;
  border-color: #f3caca;
  color: var(--red);
}

.icon-button {
  width: 40px;
  padding: 0;
  background: var(--surface-muted);
  border-color: var(--line);
  color: var(--ink);
  text-decoration: none;
}

.icon-button.whatsapp {
  background: #e7f8ef;
  border-color: #bde8cd;
  color: #177a42;
}

.icon-button.whatsapp:hover {
  background: #d9f3e5;
  border-color: #98dcb5;
}

.file-button {
  position: relative;
  overflow: hidden;
}

.file-button input {
  position: absolute;
  inset: 0;
  opacity: 0;
}

.view {
  display: none;
}

.view.active {
  display: block;
}

.metrics {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 16px;
  margin-bottom: 20px;
}

.metric,
.panel,
.table-wrap {
  border: 1px solid var(--line);
  border-radius: 14px;
  background: var(--surface);
  box-shadow: var(--shadow);
}

.metric {
  position: relative;
  overflow: hidden;
  padding: 18px;
}

.metric::before {
  content: "";
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  height: 4px;
  background: var(--green);
}

.metric span {
  display: block;
  color: var(--muted);
  font-size: 0.86rem;
  font-weight: 700;
}

.metric strong {
  display: block;
  margin-top: 12px;
  font-size: 2.15rem;
  line-height: 1;
}

.metric.warning strong {
  color: var(--amber);
}

.metric.warning::before {
  background: var(--amber);
}

.dashboard-grid {
  display: grid;
  grid-template-columns: minmax(0, 0.9fr) minmax(0, 1.1fr);
  gap: 20px;
}

.map-layout {
  display: grid;
  grid-template-columns: minmax(0, 1.5fr) minmax(320px, 0.55fr);
  gap: 20px;
}

.panel {
  min-height: 220px;
  padding: 18px;
}

.map-panel,
.map-list-panel {
  min-height: 620px;
}

.spaces-map {
  width: 100%;
  min-height: 540px;
  overflow: hidden;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: #edf2ec;
}

.map-legend {
  display: flex;
  gap: 14px;
  align-items: center;
  flex-wrap: wrap;
  color: var(--muted);
  font-size: 0.84rem;
  font-weight: 800;
}

.map-header-actions {
  display: flex;
  gap: 12px;
  align-items: center;
  justify-content: flex-end;
  flex-wrap: wrap;
}

.map-legend span {
  display: inline-flex;
  gap: 6px;
  align-items: center;
}

.map-legend svg {
  width: 16px;
  height: 16px;
}

.legend-dot {
  width: 14px;
  height: 14px;
  display: inline-block;
  border: 2px solid #8b6f00;
  border-radius: 999px;
  background: #ffd84d;
}

.legend-hand {
  width: 22px;
  height: 22px;
  display: inline-grid;
  place-items: center;
  border-radius: 999px;
  background: #177a42;
  color: #ffffff;
  font-size: 0.82rem;
}

.available-map-icon,
.adopted-map-icon {
  display: grid;
  place-items: center;
  background: transparent;
  border: 0;
}

.available-map-icon span {
  width: 24px;
  height: 24px;
  display: block;
  border: 3px solid #8b6f00;
  border-radius: 999px;
  background: #ffd84d;
  box-shadow: 0 6px 16px rgba(79, 64, 0, 0.26);
}

.adopted-map-icon span {
  width: 34px;
  height: 34px;
  display: grid;
  place-items: center;
  border: 2px solid #ffffff;
  border-radius: 999px;
  background: #177a42;
  color: #ffffff;
  box-shadow: 0 6px 16px rgba(23, 122, 66, 0.3);
  font-size: 1rem;
}

.map-popup-button {
  min-height: 30px;
  margin-top: 8px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--green);
  color: #ffffff;
  font-weight: 800;
}

.leaflet-popup-content {
  min-width: 190px;
}

.panel.wide {
  grid-column: 1 / -1;
}

.panel-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 16px;
  padding-bottom: 12px;
  border-bottom: 1px solid var(--line);
}

.status-list,
.compact-list,
.bar-list,
.insight-grid {
  display: grid;
  gap: 10px;
}

.status-row,
.compact-row,
.insight-card {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 14px;
  padding: 12px 13px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--surface-muted);
}

.compact-row {
  align-items: flex-start;
}

.insight-grid {
  grid-template-columns: repeat(4, minmax(0, 1fr));
}

.insight-card {
  display: grid;
  align-content: space-between;
  min-height: 112px;
}

.insight-card span {
  color: var(--muted);
  font-size: 0.84rem;
  font-weight: 700;
}

.insight-card strong {
  font-size: 1.55rem;
  line-height: 1.1;
}

.bar-row {
  display: grid;
  grid-template-columns: minmax(120px, 0.8fr) minmax(160px, 1.4fr) 42px;
  gap: 12px;
  align-items: center;
}

.bar-row span {
  color: var(--muted);
  font-size: 0.86rem;
}

.bar-track {
  height: 10px;
  overflow: hidden;
  border-radius: 999px;
  background: #edf2ec;
}

.bar-fill {
  height: 100%;
  min-width: 6px;
  border-radius: inherit;
  background: var(--green);
}

.compact-row strong,
.compact-row span {
  display: block;
}

.compact-row span,
.status-row span {
  color: var(--muted);
  font-size: 0.86rem;
}

.toolbar {
  display: flex;
  justify-content: space-between;
  gap: 14px;
  margin-bottom: 14px;
}

.search {
  width: min(100%, 440px);
  min-height: 46px;
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 0 14px;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: var(--surface);
  box-shadow: var(--shadow-soft);
}

.search input {
  width: 100%;
  border: 0;
  outline: 0;
  background: transparent;
  color: var(--ink);
}

.table-wrap {
  overflow: auto;
}

table {
  width: 100%;
  min-width: 820px;
  border-collapse: collapse;
}

th,
td {
  padding: 15px 16px;
  border-bottom: 1px solid var(--line);
  text-align: left;
  vertical-align: top;
}

th {
  color: #526158;
  font-size: 0.78rem;
  font-weight: 800;
  text-transform: uppercase;
  background: #fbfcfa;
  position: sticky;
  top: 0;
  z-index: 1;
}

tr:last-child td {
  border-bottom: 0;
}

tbody tr {
  transition: background 0.14s ease;
}

tbody tr:hover {
  background: #fbfcfa;
}

.row-title {
  font-weight: 800;
}

.row-subtitle {
  margin-top: 3px;
  color: var(--muted);
  font-size: 0.86rem;
}

.badge {
  display: inline-flex;
  min-height: 26px;
  align-items: center;
  border-radius: 999px;
  padding: 0 10px;
  background: var(--green-soft);
  color: var(--green-dark);
  font-size: 0.78rem;
  font-weight: 800;
  white-space: nowrap;
}

.badge.muted {
  background: #eef0ee;
  color: var(--muted);
}

.badge.warn {
  background: var(--amber-soft);
  color: var(--amber);
}

.badge.danger {
  background: var(--red-soft);
  color: var(--red);
}

.table-actions {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
}

.report-actions {
  display: flex;
  justify-content: space-between;
  gap: 16px;
  align-items: flex-end;
  margin-bottom: 16px;
}

.report-filters {
  display: flex;
  gap: 12px;
  align-items: flex-end;
  flex-wrap: wrap;
}

.report-filters label {
  display: grid;
  gap: 7px;
  color: var(--muted);
  font-size: 0.82rem;
  font-weight: 800;
}

.report-filters input,
.report-filters select {
  min-height: 42px;
  min-width: 160px;
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 8px 10px;
  background: var(--surface);
  color: var(--ink);
}

.report-sheet {
  display: grid;
  gap: 18px;
}

.report-heading {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 18px;
  padding: 18px;
  border: 1px solid var(--line);
  border-radius: 14px;
  background: var(--surface);
  box-shadow: var(--shadow);
}

.metrics.compact .metric {
  box-shadow: var(--shadow-soft);
}

dialog {
  width: min(920px, calc(100vw - 28px));
  max-height: calc(100vh - 28px);
  border: 0;
  border-radius: 16px;
  padding: 0;
  box-shadow: 0 30px 80px rgba(18, 36, 26, 0.3);
}

dialog::backdrop {
  background: rgba(12, 24, 17, 0.45);
}

#record-form {
  padding: 24px;
}

.dialog-header {
  display: flex;
  justify-content: space-between;
  gap: 18px;
  align-items: flex-start;
  margin-bottom: 20px;
  padding-bottom: 16px;
  border-bottom: 1px solid var(--line);
}

.form-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 15px;
}

.field {
  display: grid;
  gap: 7px;
}

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

.field label {
  color: var(--muted);
  font-size: 0.82rem;
  font-weight: 800;
}

.field input,
.field select,
.field textarea {
  width: 100%;
  min-height: 44px;
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 9px 12px;
  outline: 0;
  background: #ffffff;
  color: var(--ink);
}

.field textarea {
  min-height: 92px;
  resize: vertical;
}

.field input:focus,
.field select:focus,
.field textarea:focus {
  border-color: var(--green);
  box-shadow: 0 0 0 3px rgba(47, 111, 78, 0.12);
}

.dialog-actions {
  display: flex;
  justify-content: space-between;
  gap: 14px;
  align-items: center;
  margin-top: 22px;
  padding-top: 18px;
  border-top: 1px solid var(--line);
}

.empty {
  padding: 28px;
  color: var(--muted);
  text-align: center;
}

@media (max-width: 980px) {
  body {
    grid-template-columns: 1fr;
  }

  .sidebar {
    min-height: auto;
    position: sticky;
    top: 0;
    z-index: 2;
    padding: 14px;
    border-right: 0;
  }

  .brand {
    padding-bottom: 12px;
  }

  .nav {
    grid-template-columns: repeat(3, minmax(0, 1fr));
    padding-top: 12px;
  }

  .nav-item {
    justify-content: center;
    box-shadow: none;
  }

  .shell {
    padding: 18px;
  }

  .topbar {
    display: grid;
    padding: 18px;
  }

  .topbar-actions {
    justify-content: flex-start;
  }

  .metrics,
  .dashboard-grid,
  .map-layout,
  .insight-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .map-layout {
    grid-template-columns: 1fr;
  }

  .report-actions {
    display: grid;
  }

  .report-actions .topbar-actions {
    justify-content: flex-start;
  }
}

@media (max-width: 640px) {
  .nav {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .nav-item {
    font-size: 0.88rem;
    min-height: 42px;
    padding: 0 10px;
  }

  .metrics,
  .dashboard-grid,
  .map-layout,
  .form-grid,
  .insight-grid {
    grid-template-columns: 1fr;
  }

  .map-panel,
  .map-list-panel {
    min-height: auto;
  }

  .spaces-map {
    min-height: 420px;
  }

  .bar-row {
    grid-template-columns: 1fr;
    gap: 7px;
  }

  .report-heading {
    display: grid;
  }

  .map-header-actions {
    justify-content: flex-start;
  }

  .report-filters,
  .report-filters label,
  .report-filters input,
  .report-filters select {
    width: 100%;
  }

  .panel.wide,
  .field.full {
    grid-column: auto;
  }

  .dialog-actions {
    align-items: stretch;
    flex-direction: column-reverse;
  }

  .dialog-actions > div,
  .dialog-actions button {
    width: 100%;
  }
}

/* ============ ALERT BANNER ============ */

.alert-banner {
  display: grid;
  gap: 8px;
  margin-bottom: 18px;
}

.alert-item {
  display: flex;
  gap: 10px;
  align-items: center;
  padding: 11px 15px;
  border-radius: 10px;
  font-size: 0.88rem;
  font-weight: 700;
}

.alert-item svg {
  width: 18px;
  height: 18px;
  flex: 0 0 auto;
}

.alert-warn {
  background: var(--amber-soft);
  color: var(--amber);
  border: 1px solid #f5d88a;
}

.alert-danger {
  background: var(--red-soft);
  color: var(--red);
  border: 1px solid #f3caca;
}

/* ============ SYSTEM NOTICES ============ */

.system-notice {
  position: fixed;
  top: 16px;
  left: 50%;
  transform: translateX(-50%);
  z-index: 9999;
  display: flex;
  gap: 12px;
  align-items: center;
  min-width: 320px;
  max-width: min(680px, calc(100vw - 32px));
  padding: 13px 16px;
  border-radius: 12px;
  font-size: 0.88rem;
  font-weight: 700;
  box-shadow: 0 8px 28px rgba(0, 0, 0, 0.18);
}

.system-notice svg {
  width: 18px;
  height: 18px;
  flex: 0 0 auto;
}

.system-notice.warn {
  background: var(--amber-soft);
  color: var(--amber);
  border: 1px solid #f5d88a;
}

.system-notice.info {
  background: var(--blue-soft);
  color: var(--blue);
  border: 1px solid #bcd3ef;
}

.system-notice span {
  flex: 1;
}

.notice-actions {
  display: flex;
  gap: 8px;
  align-items: center;
}

.notice-btn {
  min-height: 32px;
  padding: 0 12px;
  border: 1px solid currentColor;
  border-radius: 8px;
  background: transparent;
  color: inherit;
  font: inherit;
  font-weight: 700;
  cursor: pointer;
}

.notice-close {
  width: 30px;
  height: 30px;
  display: grid;
  place-items: center;
  border: 0;
  border-radius: 6px;
  background: transparent;
  color: inherit;
  cursor: pointer;
  opacity: 0.7;
}

.notice-close:hover {
  opacity: 1;
  background: rgba(0, 0, 0, 0.08);
}

/* ============ PAGINATION ============ */

.pagination {
  display: flex;
  gap: 10px;
  align-items: center;
  flex-wrap: wrap;
  justify-content: flex-end;
}

.pagination-info {
  color: var(--muted);
  font-size: 0.84rem;
  font-weight: 700;
}

.pagination-controls {
  display: flex;
  gap: 6px;
}

.pagination-btn {
  min-height: 36px;
  padding: 0 12px;
  font-size: 0.84rem;
}

/* ============ PASSWORD CHANGE MODAL ============ */

.modal-overlay {
  position: fixed;
  inset: 0;
  z-index: 1000;
  display: grid;
  place-items: center;
  padding: 20px;
  background: rgba(12, 24, 17, 0.5);
  backdrop-filter: blur(4px);
}

.modal-card {
  width: min(480px, 100%);
  padding: 28px;
  border-radius: 16px;
  background: var(--surface);
  box-shadow: 0 30px 80px rgba(18, 36, 26, 0.3);
}

.modal-description {
  margin: 0 0 18px;
  color: var(--muted);
  font-size: 0.9rem;
  line-height: 1.5;
}

.pwchange-error {
  grid-column: 1 / -1;
}

/* ============ EXPIRING CONTRACT ROW ============ */

tr.expiring td {
  background: #fff9f0;
}

tr.expiring:hover td {
  background: #fff3e0;
}

@media print {
  body {
    display: block;
    background: #ffffff;
  }

  .sidebar,
  .topbar,
  .report-actions,
  .view:not(#reports) {
    display: none !important;
  }

  .shell {
    padding: 0;
  }

  #reports,
  #report-output {
    display: block !important;
  }

  .panel,
  .metric,
  .report-heading {
    break-inside: avoid;
    box-shadow: none;
  }
}
