:root {
  --green-700: #1b5e20;
  --green-600: #2e7d32;
  --green-100: #e8f5e9;
  --border: #d0d7de;
  --text: #1f2937;
  --bg: #f6f8fa;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: 'Segoe UI', system-ui, sans-serif;
  color: var(--text);
  background: var(--bg);
  -webkit-tap-highlight-color: transparent;
}

html, body { height: 100%; }

.container { max-width: 1100px; margin: 0 auto; padding: 1rem; }

/* ==============================
   HEADER
   ============================== */
.site-header {
  background: var(--green-700);
  color: #fff;
  box-shadow: 0 2px 8px rgba(0,0,0,.15);
}

.header-inner {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: .5rem;
  padding: 0 .75rem;
  height: 48px;
}

.logo { color: #fff; text-decoration: none; font-weight: 700; font-size: 1rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

.site-header nav {
  display: flex;
  align-items: center;
  gap: .5rem;
  flex-wrap: nowrap;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
}

.site-header nav a {
  color: #e8f5e9;
  text-decoration: none;
  font-size: .82rem;
  white-space: nowrap;
  padding: .3rem .25rem;
  touch-action: manipulation;
}

.site-header nav a:hover { color: #fff; }

.user-badge {
  padding: .15rem .4rem;
  background: rgba(255,255,255,.15);
  border-radius: 4px;
  font-size: .75rem;
  display: inline-flex;
  align-items: center;
  gap: .3rem;
  white-space: nowrap;
}

/* Role badges */
.role-badge {
  display: inline-block;
  padding: .1rem .4rem;
  border-radius: 10px;
  font-size: .65rem;
  font-weight: 600;
  white-space: nowrap;
}
.badge-super-admin { background: #7b1fa2; color: #fff; }
.badge-admin { background: #c62828; color: #fff; }
.badge-manager { background: #e65100; color: #fff; }
.badge-editor { background: #1565c0; color: #fff; }
.badge-viewer { background: #558b2f; color: #fff; }
.badge-user { background: #6d4c41; color: #fff; }

/* ==============================
   FOOTER
   ============================== */
.site-footer {
  background: #fff;
  border-top: 1px solid var(--border);
  padding: .75rem 0;
  text-align: center;
  font-size: .8rem;
  color: #666;
}

/* ==============================
   BUTTONS
   ============================== */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: .5rem .85rem;
  border-radius: 6px;
  border: none;
  cursor: pointer;
  text-decoration: none;
  font-size: .85rem;
  touch-action: manipulation;
  -webkit-user-select: none;
  user-select: none;
}
.btn-primary { background: var(--green-600); color: #fff; }
.btn-primary:hover { background: var(--green-700); }
.btn-secondary { background: #fff; color: var(--green-700); border: 1px solid var(--green-600); }
.btn-small { padding: .3rem .6rem; font-size: .75rem; }
.btn-danger { background: #c62828; color: #fff; }
.btn-full { width: 100%; }

/* ==============================
   FORMS
   ============================== */
.form label { display: block; margin-bottom: .6rem; font-size: .82rem; }
.form input, .form select {
  display: block;
  width: 100%;
  margin-top: .2rem;
  padding: .4rem .5rem;
  border: 1px solid var(--border);
  border-radius: 4px;
  font-size: .85rem;
  background: #fff;
  touch-action: manipulation;
}
.form.compact label { margin-bottom: .4rem; }
.form-row { margin-bottom: .5rem; }

.form-row-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: .5rem;
  margin-bottom: .5rem;
}

.auth-box {
  max-width: 380px;
  margin: 2rem auto;
  background: #fff;
  padding: 1.5rem;
  border-radius: 8px;
  border: 1px solid var(--border);
}

.alert { padding: .6rem .75rem; border-radius: 4px; margin-bottom: .75rem; font-size: .85rem; }
.alert-error { background: #ffebee; color: #b71c1c; }
.alert-success { background: #e8f5e9; color: #1b5e20; }
.hint { font-size: .8rem; color: #666; }

.text-muted { color: #999; font-size: .78rem; font-style: italic; }

.hero { padding: 1.5rem 1rem; }
.hero h1 { color: var(--green-700); margin-bottom: .5rem; font-size: 1.3rem; }
.lead { font-size: .95rem; line-height: 1.5; max-width: 700px; }
.hero-actions { margin-top: 1.25rem; display: flex; gap: .6rem; flex-wrap: wrap; }

.cards {
  display: grid;
  grid-template-columns: 1fr;
  gap: .75rem;
  padding-bottom: 1.5rem;
}
.card {
  background: #fff;
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 1rem;
}
.card h3 { margin-top: 0; color: var(--green-600); font-size: .95rem; }

/* Homepage sections */
.section {
  padding: 1.5rem 0;
}
.section h2 {
  color: var(--green-700);
  font-size: 1.2rem;
  margin-bottom: 1rem;
  padding-bottom: .5rem;
  border-bottom: 2px solid var(--green-100);
}

/* Stats */
.hero-stats {
  display: flex;
  gap: 1.25rem;
  margin: 1.5rem 0;
  flex-wrap: wrap;
}
.stat-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  min-width: 80px;
}
.stat-value {
  font-size: 2rem;
  font-weight: 700;
  color: var(--green-700);
  line-height: 1;
}
.stat-label {
  font-size: .78rem;
  color: #666;
  margin-top: .25rem;
  text-align: center;
}

/* About grid */
.about-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
  gap: 1rem;
}
.about-card {
  background: #fff;
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 1.25rem;
}
.about-card h3 {
  margin: .5rem 0;
  color: var(--green-700);
  font-size: .95rem;
}
.about-card p {
  font-size: .85rem;
  line-height: 1.5;
  color: #555;
  margin: 0;
}
.about-icon {
  font-size: 2rem;
}

/* Features row */
.features-row {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
  gap: 1rem;
}
.feature-item {
  display: flex;
  gap: .75rem;
  align-items: flex-start;
  background: #fff;
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 1rem;
}
.feature-num {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 32px;
  height: 32px;
  border-radius: 50%;
  background: var(--green-600);
  color: #fff;
  font-weight: 700;
  font-size: .85rem;
  flex-shrink: 0;
}
.feature-item h4 {
  margin: 0 0 .25rem 0;
  font-size: .9rem;
  color: var(--green-700);
}
.feature-item p {
  margin: 0;
  font-size: .8rem;
  color: #666;
  line-height: 1.4;
}

/* Basemap cards */
.basemap-cards {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(240px, 1fr));
  gap: 1rem;
}
.basemap-card {
  background: #fff;
  border: 1px solid var(--border);
  border-radius: 8px;
  overflow: hidden;
}
.basemap-card h4 {
  margin: .75rem 1rem .25rem;
  font-size: .9rem;
  color: var(--green-700);
}
.basemap-card p {
  margin: 0 1rem .75rem;
  font-size: .8rem;
  color: #666;
}
.basemap-preview {
  height: 120px;
  background-size: cover;
  background-position: center;
  border-bottom: 1px solid var(--border);
}
.basemap-default {
  background: linear-gradient(135deg, #e8f5e9 0%, #c8e6c9 30%, #a5d6a7 60%, #81c784 100%);
  position: relative;
}
.basemap-default::after {
  content: '';
  position: absolute;
  inset: 0;
  background:
    linear-gradient(90deg, transparent 45%, #bbb 45%, #bbb 46%, transparent 46%),
    linear-gradient(0deg, transparent 45%, #bbb 45%, #bbb 46%, transparent 46%),
    linear-gradient(135deg, transparent 60%, #999 60%, #999 61%, transparent 61%);
  opacity: .5;
}
.basemap-satellite {
  background: linear-gradient(135deg, #2d5a27 0%, #1a472a 30%, #3d6b35 50%, #1e5631 80%, #2a6a30 100%);
  position: relative;
}
.basemap-satellite::after {
  content: '';
  position: absolute;
  inset: 0;
  background:
    radial-gradient(circle at 30% 40%, rgba(255,255,255,.08) 0%, transparent 50%),
    radial-gradient(circle at 70% 60%, rgba(0,0,0,.15) 0%, transparent 40%);
}

.data-table { width: 100%; border-collapse: collapse; background: #fff; font-size: .82rem; }
.data-table th, .data-table td { border: 1px solid var(--border); padding: .4rem .5rem; text-align: left; }
.data-table th { background: var(--green-100); }

/* ==============================
   MAP LAYOUT
   ============================== */
.map-layout {
  display: flex;
  height: calc(100vh - 48px);
  min-height: calc(100vh - 48px);
  margin: 0;
  position: relative;
  overflow: hidden;
}

.map-main {
  position: relative;
  flex: 1;
  height: 100%;
  min-width: 0;
}

.map-container {
  flex: 1;
  width: 100%;
  height: 100%;
  cursor: crosshair;
}

.map-loading {
  position: absolute;
  top: 0; left: 0; right: 0; bottom: 0;
  z-index: 600;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: .6rem;
  background: rgba(255,255,255,.6);
  backdrop-filter: blur(2px);
  font-size: .85rem;
  color: var(--green-700);
  font-weight: 600;
  pointer-events: none;
}
.map-loading.hidden { display: none; }
.spinner {
  width: 32px; height: 32px;
  border: 3px solid var(--green-100);
  border-top-color: var(--green-600);
  border-radius: 50%;
  animation: spin .7s linear infinite;
}
@keyframes spin {
  to { transform: rotate(360deg); }
}

.map-click-hint {
  position: absolute;
  bottom: .5rem;
  left: 50%;
  transform: translateX(-50%);
  z-index: 500;
  background: rgba(0,0,0,.55);
  color: #fff;
  font-size: .72rem;
  padding: .25rem .7rem;
  border-radius: 12px;
  pointer-events: none;
  white-space: nowrap;
}

/* ==============================
   SIDEBAR (desktop)
   ============================== */
.sidebar {
  width: 300px;
  min-width: 280px;
  background: #fff;
  border-right: 1px solid var(--border);
  overflow-y: auto;
  overflow-x: hidden;
  padding: .75rem;
  -webkit-overflow-scrolling: touch;
}

.sidebar h2, .sidebar h3 { margin-top: 0; font-size: .9rem; color: var(--green-700); }

.sidebar-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
}
.sidebar-close {
  display: none;
  background: none;
  border: none;
  font-size: 1.5rem;
  color: #666;
  cursor: pointer;
  padding: .2rem .4rem;
  line-height: 1;
}

/* ==============================
   SIDEBAR TOGGLE (mobile)
   ============================== */
.sidebar-toggle {
  display: none;
  position: fixed;
  top: 56px;
  left: 10px;
  z-index: 600;
  width: 42px;
  height: 42px;
  border-radius: 8px;
  background: var(--green-700);
  color: #fff;
  border: none;
  cursor: pointer;
  box-shadow: 0 2px 8px rgba(0,0,0,.25);
  touch-action: manipulation;
  align-items: center;
  justify-content: center;
}

.hamburger {
  display: block;
  width: 20px;
  height: 2px;
  background: #fff;
  position: relative;
}
.hamburger::before,
.hamburger::after {
  content: '';
  position: absolute;
  width: 20px;
  height: 2px;
  background: #fff;
  left: 0;
}
.hamburger::before { top: -6px; }
.hamburger::after { top: 6px; }

.sidebar-backdrop {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,.4);
  z-index: 650;
}
.sidebar-backdrop.visible { display: block; }

/* ==============================
   LEGEND (collapsible)
   ============================== */
.legend-panel {
  position: absolute;
  top: .75rem;
  right: .75rem;
  z-index: 500;
  width: 320px;
  max-width: 340px;
  background: rgba(255,255,255,.96);
  border: 1px solid var(--border);
  border-radius: 8px;
  box-shadow: 0 2px 8px rgba(0,0,0,.1);
  padding: .5rem .6rem;
}

.legend-toggle {
  display: flex;
  align-items: center;
  justify-content: space-between;
  cursor: pointer;
  -webkit-user-select: none;
  user-select: none;
}
.legend-toggle h3 {
  margin: 0;
  font-size: .82rem;
  color: var(--green-700);
}
.legend-chevron {
  display: inline-block;
  width: 8px;
  height: 8px;
  border-right: 2px solid #888;
  border-bottom: 2px solid #888;
  transform: rotate(45deg);
  margin-top: -3px;
  transition: transform .2s;
}
.legend-toggle.collapsed .legend-chevron {
  transform: rotate(-135deg);
}

.legend-body {
  margin-top: .3rem;
}

.legend-item {
  display: flex;
  align-items: center;
  gap: .4rem;
  font-size: .7rem;
  margin-bottom: .15rem;
}

.swatch { width: 14px; height: 11px; border-radius: 2px; flex-shrink: 0; }
.unit-label { font-size: .65rem; color: #666; margin: .2rem 0 0; }

.legend-bar-wrap { margin-top: .15rem; }
.legend-bar { display: flex; height: 20px; border: 1px solid #888; overflow: hidden; }
.legend-segment { flex: 1 1 0; }
.legend-ticks { position: relative; height: 12px; margin-top: 1px; }
.legend-tick { position: absolute; bottom: 0; transform: translateX(-50%); font-size: .58rem; line-height: 1; color: #333; }
.legend-underflow { margin-top: .25rem; display: inline-flex; align-items: center; gap: .25rem; font-size: .6rem; }

/* ==============================
   BOUNDARY PANEL
   ============================== */
.boundary-panel {
  margin-top: .75rem;
  border-top: 1px solid var(--border);
  padding-top: .6rem;
}
.boundary-panel h3 {
  margin: 0 0 .3rem 0;
  font-size: .88rem;
  color: var(--green-700);
}
.checkbox-label {
  display: flex;
  align-items: center;
  gap: .4rem;
  font-size: .8rem;
  cursor: pointer;
  min-height: 32px;
}
.checkbox-label input[type="checkbox"] {
  width: 16px;
  height: 16px;
  accent-color: var(--green-600);
  flex-shrink: 0;
}
.boundary-legend {
  margin-top: .4rem;
  padding: .35rem .5rem;
  background: #f8f9fa;
  border-radius: 4px;
  font-size: .72rem;
}
.boundary-legend-item {
  display: flex;
  align-items: center;
  gap: .4rem;
  margin-bottom: .2rem;
}
.boundary-line {
  display: inline-block;
  width: 22px;
  height: 0;
  border-top: 3px dashed;
  flex-shrink: 0;
}
.tn-line { border-color: #1b5e20; }
.dnb-line { border-color: #e65100; }

/* ==============================
   FLUX PANEL
   ============================== */
.flux-panel { margin-top: .75rem; border-top: 1px solid var(--border); padding-top: .6rem; }
.flux-item {
  padding: .35rem .45rem;
  border: 1px solid var(--border);
  border-radius: 4px;
  margin-bottom: .3rem;
  cursor: pointer;
  font-size: .78rem;
  touch-action: manipulation;
}
.flux-item:hover { background: var(--green-100); }
.flux-item:active { background: #c8e6c9; }

.badge {
  background: #e3f2fd;
  color: #1565c0;
  padding: .05rem .3rem;
  border-radius: 3px;
  font-size: .62rem;
}

/* ==============================
   VIETNAM UNITS PANEL
   ============================== */
.vn-units-panel {
  margin-top: .75rem;
  border-top: 1px solid var(--border);
  padding-top: .6rem;
}
.vn-units-panel h3 {
  margin: 0 0 .3rem 0;
  font-size: .88rem;
  color: var(--green-700);
}
.vn-units-search {
  width: 100%;
  padding: .4rem .5rem;
  border: 1px solid var(--border);
  border-radius: 4px;
  font-size: .85rem;
  min-height: 36px;
}
.vn-units-search:focus {
  outline: none;
  border-color: var(--green-600);
  box-shadow: 0 0 0 2px rgba(46,125,50,.15);
}
.vn-units-results {
  display: none;
  position: relative;
  z-index: 10;
  max-height: 220px;
  overflow-y: auto;
  background: #fff;
  border: 1px solid var(--border);
  border-radius: 4px;
  margin-top: 2px;
  box-shadow: 0 4px 12px rgba(0,0,0,.1);
  -webkit-overflow-scrolling: touch;
}
.vn-unit-result-item {
  display: flex;
  align-items: center;
  gap: .4rem;
  padding: .4rem .45rem;
  cursor: pointer;
  font-size: .78rem;
  border-bottom: 1px solid #f0f0f0;
  flex-wrap: wrap;
  min-height: 36px;
}
.vn-unit-result-item:hover { background: var(--green-100); }
.vn-unit-result-item:active { background: #c8e6c9; }
.vn-unit-type-badge {
  font-size: .6rem;
  padding: .05rem .3rem;
  border-radius: 3px;
  white-space: nowrap;
  flex-shrink: 0;
}
.vn-unit-type-badge.type-province { background: #e8f5e9; color: #1b5e20; }
.vn-unit-type-badge.type-ward { background: #e3f2fd; color: #1565c0; }
.vn-unit-name { flex: 1; min-width: 0; }
.vn-unit-province { font-size: .65rem; color: #888; width: 100%; padding-left: .15rem; }
.vn-unit-result-empty { padding: .4rem; color: #999; font-size: .78rem; text-align: center; }
#btn-toggle-provinces { margin-top: .35rem; }

.vn-province-label div {
  font-size: 11px;
  font-weight: 600;
  color: var(--green-700);
  white-space: nowrap;
  text-align: center;
  text-shadow: 0 0 4px #fff, 0 0 4px #fff;
  pointer-events: auto;
}
.vn-unit-marker-dot {
  width: 14px;
  height: 14px;
  background: var(--green-600);
  border: 2px solid #fff;
  border-radius: 50%;
  box-shadow: 0 1px 6px rgba(0,0,0,.25);
}

/* ==============================
   DESKTOP >= 769px
   ============================== */
@media (min-width: 769px) {
  .cards { grid-template-columns: repeat(auto-fit, minmax(240px, 1fr)); }
  .legend-chevron { display: none; }
  .legend-toggle { cursor: default; }
}

/* ==============================
   MOBILE <= 768px
   ============================== */
@media (max-width: 768px) {
  /* Header */
  .header-inner { padding: 0 .5rem; height: 44px; }
  .logo { font-size: .85rem; }
  .site-header nav a { font-size: .72rem; padding: .25rem .2rem; }
  .user-badge { font-size: .65rem; padding: .1rem .3rem; }
  .role-badge { font-size: .55rem; }

  /* Homepage mobile */
  .hero { padding: 1rem .75rem; }
  .hero h1 { font-size: 1.1rem; }
  .lead { font-size: .88rem; }
  .hero-stats { gap: .75rem; justify-content: center; }
  .stat-value { font-size: 1.5rem; }
  .section { padding: 1rem 0; }
  .section h2 { font-size: 1rem; }
  .about-grid, .features-row { grid-template-columns: 1fr; }

  /* Map fills remaining height */
  .map-layout {
    flex-direction: column;
    height: calc(100vh - 44px);
    min-height: calc(100vh - 44px);
  }

  /* Sidebar becomes a sliding drawer */
  .sidebar {
    position: fixed;
    top: 0;
    left: 0;
    width: 85%;
    max-width: 320px;
    height: 100vh;
    z-index: 700;
    transform: translateX(-100%);
    transition: transform .25s ease;
    border-right: none;
    box-shadow: none;
    padding-top: .5rem;
  }
  .sidebar.open {
    transform: translateX(0);
    box-shadow: 4px 0 20px rgba(0,0,0,.2);
  }
  .sidebar-close {
    display: block;
  }

  .sidebar-toggle { display: flex; }

  /* Map takes full width */
  .map-main { width: 100%; height: calc(100vh - 44px); }
  .map-container { height: 100%; }

  /* Legend becomes bottom-left collapsed */
  .legend-panel {
    top: auto;
    bottom: .5rem;
    right: .5rem;
    left: auto;
    width: auto;
    max-width: 210px;
    padding: .3rem .4rem;
    border-radius: 6px;
  }
  .legend-toggle h3 { font-size: .75rem; }
  .legend-item { font-size: .62rem; }
  .legend-tick { font-size: .52rem; }
  .unit-label { font-size: .58rem; }

  /* Sidebar form: 2-col grid for year+basemap */
  .form-row-grid { grid-template-columns: 1fr 1fr; gap: .4rem; }

  /* Touch targets minimum 44px */
  .btn { min-height: 38px; min-width: 44px; }
  .form select { min-height: 38px; }
  .flux-item { min-height: 36px; }

  .site-footer { padding: .5rem 0; font-size: .7rem; }
}

/* ==============================
   SMALL MOBILE <= 400px
   ============================== */
@media (max-width: 400px) {
  .header-inner { padding: 0 .4rem; }
  .logo { font-size: .75rem; }
  .site-header nav a { font-size: .65rem; padding: .2rem .15rem; }
  .site-header nav { gap: .25rem; }

  .sidebar { width: 90%; }
  .form-row-grid { grid-template-columns: 1fr; }

  .legend-panel { max-width: 180px; }
}
