:root {
  --bg: #f4f6f9;
  --surface: #ffffff;
  --border: #e2e8f0;
  --text: #0f172a;
  --muted: #64748b;
  --primary: #0f766e;
  --primary-hover: #0d9488;
  --accent: #f59e0b;
  --danger: #dc2626;
  --success: #16a34a;
  --radius: 10px;
}

* { box-sizing: border-box; }
body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.45;
}

.hidden { display: none !important; }
.muted { color: var(--muted); font-size: 0.9rem; }
.stack-md { display: flex; flex-direction: column; gap: 14px; }
.spacer { flex: 1; }

/* Auth */
.auth-wrapper {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
}
.auth-card {
  width: 100%;
  max-width: 400px;
  background: var(--surface);
  padding: 28px;
  border-radius: var(--radius);
  border: 1px solid var(--border);
  box-shadow: 0 8px 24px rgba(15, 23, 42, 0.06);
}
.brand-badge {
  display: inline-block;
  background: #ccfbf1;
  color: var(--primary);
  font-weight: 700;
  padding: 4px 10px;
  border-radius: 999px;
  font-size: 0.85rem;
  margin-bottom: 10px;
}
.auth-card h1 { margin: 0 0 6px; font-size: 1.4rem; }
.test-hint {
  margin-top: 16px;
  padding: 10px 12px;
  background: #f8fafc;
  border: 1px dashed var(--border);
  border-radius: 8px;
  font-size: 0.82rem;
  color: var(--muted);
}

/* Topbar */
.topbar {
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  padding: 12px 24px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  position: sticky;
  top: 0;
  z-index: 20;
}
.topbar-left, .topbar-right { display: flex; align-items: center; gap: 16px; }
.logo { font-weight: 800; font-size: 1.1rem; color: var(--primary); }
.nav-tabs { display: flex; gap: 6px; }
.nav-tab {
  border: none;
  background: transparent;
  padding: 7px 14px;
  border-radius: 8px;
  font-weight: 600;
  color: var(--muted);
  cursor: pointer;
}
.nav-tab.active { background: #ccfbf1; color: var(--primary); }
.user-badge {
  font-size: 0.85rem;
  font-weight: 600;
  background: #f1f5f9;
  padding: 5px 10px;
  border-radius: 999px;
}

/* Layout */
.container { max-width: 1200px; margin: 0 auto; padding: 20px; }

.stats-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(170px, 1fr));
  gap: 12px;
  margin-bottom: 20px;
}
.stat-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px 16px;
}
.stat-label { font-size: 0.8rem; color: var(--muted); font-weight: 600; }
.stat-value { font-size: 1.35rem; font-weight: 800; margin-top: 4px; }
.stat-card.money .stat-value { color: var(--primary); }

.toolbar {
  display: flex;
  flex-wrap: wrap;
  gap: 12px;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 18px;
}
.search-box { flex: 1; min-width: 240px; }
.search-box input { width: 100%; }
.toolbar-actions { display: flex; gap: 10px; align-items: center; }

/* Cards */
.customers-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(340px, 1fr));
  gap: 14px;
}
.customer-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  transition: box-shadow 0.15s;
}
.customer-card:hover { box-shadow: 0 6px 18px rgba(15, 23, 42, 0.06); }
.card-top { display: flex; justify-content: space-between; align-items: flex-start; gap: 8px; }
.card-title { font-size: 1.05rem; font-weight: 700; margin: 0; }
.card-sub { font-size: 0.84rem; color: var(--muted); }
.badges { display: flex; flex-wrap: wrap; gap: 6px; }
.pill {
  font-size: 0.74rem;
  font-weight: 700;
  padding: 3px 8px;
  border-radius: 999px;
  background: #f1f5f9;
  color: #334155;
}
.pill.hybrid { background: #fef3c7; color: #92400e; }
.pill.ongrid { background: #e0f2fe; color: #075985; }
.pill.done { background: #dcfce7; color: #166534; }
.pill.loan { background: #ede9fe; color: #5b21b6; }
.pill.pending-money { background: #fee2e2; color: #991b1b; }
.cost-summary { display: flex; flex-wrap: wrap; align-items: center; gap: 6px; }

.stage-strip {
  display: flex;
  flex-wrap: wrap;
  gap: 5px;
  padding-top: 6px;
  border-top: 1px solid var(--border);
}
.stage-dot {
  font-size: 0.72rem;
  padding: 2px 7px;
  border-radius: 6px;
  background: #f1f5f9;
  border: 1px solid #cbd5e1;
  color: #475569;
}
.stage-dot.ok { background: #dcfce7; border-color: #86efac; color: #166534; font-weight: 600; }
.stage-dot.pending { background: #fee2e2; border-color: #fca5a5; color: #991b1b; font-weight: 600; }
.stage-dot.na { background: #f1f5f9; border-color: #cbd5e1; color: #64748b; }

.history-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
  max-height: 240px;
  overflow-y: auto;
}
.history-item {
  background: #f8fafc;
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 9px 11px;
  font-size: 0.84rem;
}
.history-meta {
  display: flex;
  justify-content: space-between;
  gap: 8px;
  flex-wrap: wrap;
  color: var(--primary);
  margin-bottom: 3px;
}
.history-changes { color: var(--text); }

.card-footer {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding-top: 6px;
  font-size: 0.85rem;
}
.cost-tag { font-weight: 800; color: var(--primary); }

/* Forms & Inputs */
.field { display: flex; flex-direction: column; gap: 5px; }
.field label { font-size: 0.82rem; font-weight: 600; color: #334155; }
input[type="text"],
input[type="password"],
input[type="number"],
input[type="search"],
select,
textarea {
  padding: 9px 11px;
  border: 1px solid var(--border);
  border-radius: 8px;
  font-size: 0.92rem;
  font-family: inherit;
  background: #fff;
}
input:focus, select:focus, textarea:focus {
  outline: 2px solid #99f6e4;
  border-color: var(--primary);
}

.btn {
  border: none;
  border-radius: 8px;
  padding: 9px 16px;
  font-weight: 600;
  font-size: 0.9rem;
  cursor: pointer;
}
.btn-sm { padding: 6px 11px; font-size: 0.82rem; }
.btn-full { width: 100%; }
.btn-primary { background: var(--primary); color: #fff; }
.btn-primary:hover { background: var(--primary-hover); }
.btn-ghost { background: #f1f5f9; color: var(--text); }
.btn-ghost:hover { background: #e2e8f0; }
.btn-danger { background: #fee2e2; color: var(--danger); }
.btn-icon {
  background: transparent;
  border: none;
  font-size: 1.5rem;
  cursor: pointer;
  color: var(--muted);
}

.error-box {
  background: #fee2e2;
  color: #991b1b;
  padding: 9px 12px;
  border-radius: 8px;
  font-size: 0.86rem;
}

/* Modal */
.modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(15, 23, 42, 0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
  z-index: 50;
}
.modal-dialog {
  background: var(--surface);
  width: 100%;
  max-width: 820px;
  max-height: 92vh;
  border-radius: var(--radius);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}
.modal-header {
  padding: 14px 20px;
  border-bottom: 1px solid var(--border);
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.modal-header h2 { margin: 0; font-size: 1.15rem; }
.modal-body {
  padding: 18px 20px;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 16px;
}
.modal-footer {
  display: flex;
  gap: 10px;
  align-items: center;
  padding-top: 10px;
  border-top: 1px solid var(--border);
}

.form-section {
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 14px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}
.form-section legend {
  font-weight: 700;
  font-size: 0.92rem;
  color: var(--primary);
  padding: 0 6px;
}
.grid-2 {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
  gap: 12px;
}
.grid-3 {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
  gap: 12px;
}
.sub-box {
  background: #f8fafc;
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 12px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}
.loan-part {
  padding-bottom: 10px;
  border-bottom: 1px dashed var(--border);
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.loan-part:last-child { border-bottom: none; padding-bottom: 0; }
.checkbox-row {
  display: flex;
  align-items: center;
  gap: 8px;
  font-weight: 600;
  font-size: 0.9rem;
  cursor: pointer;
}

/* Sub-Admins */
.split-layout {
  display: grid;
  grid-template-columns: 340px 1fr;
  gap: 18px;
}
@media (max-width: 800px) {
  .split-layout { grid-template-columns: 1fr; }
}
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 18px;
}
.card h2 { margin-top: 0; font-size: 1.1rem; }
.users-list { display: flex; flex-direction: column; gap: 10px; }
.user-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: 8px;
}

input[type="checkbox"] {
  width: 18px;
  height: 18px;
  accent-color: var(--primary);
  flex-shrink: 0;
}

/* Mobile-First & Touch Friendly */
@media (max-width: 640px) {
  .topbar {
    padding: 10px 14px;
    flex-wrap: wrap;
    gap: 8px;
  }
  .topbar-left, .topbar-right {
    width: 100%;
    justify-content: space-between;
    gap: 8px;
  }
  .container { padding: 12px; }

  .stats-grid {
    grid-template-columns: repeat(2, 1fr);
    gap: 8px;
  }
  .stat-card { padding: 11px 12px; }
  .stat-value { font-size: 1.15rem; }

  .toolbar { gap: 8px; }
  .search-box { min-width: 100%; }
  .toolbar-actions {
    width: 100%;
    justify-content: space-between;
  }
  .toolbar-actions select,
  .toolbar-actions .btn {
    flex: 1;
    min-height: 44px;
  }

  .customers-grid {
    grid-template-columns: 1fr;
    gap: 10px;
  }
  .customer-card { padding: 14px; }

  input[type="text"],
  input[type="password"],
  input[type="number"],
  input[type="search"],
  select,
  textarea {
    font-size: 16px;
    min-height: 44px;
    padding: 10px 12px;
  }
  .btn { min-height: 44px; }
  .btn-sm { min-height: 36px; }
  input[type="checkbox"] { width: 22px; height: 22px; }
  .checkbox-row { padding: 4px 0; font-size: 0.95rem; }

  /* Full-screen modal on mobile */
  .modal-backdrop { padding: 0; }
  .modal-dialog {
    max-width: 100%;
    max-height: 100dvh;
    height: 100dvh;
    border-radius: 0;
  }
  .modal-header {
    padding: 12px 16px;
    position: sticky;
    top: 0;
    background: var(--surface);
    z-index: 2;
  }
  .modal-body { padding: 14px 16px; }
  .modal-footer {
    position: sticky;
    bottom: 0;
    background: var(--surface);
    padding: 12px 0 4px;
    z-index: 2;
  }
  .grid-2, .grid-3 { grid-template-columns: 1fr; }
}
