:root {
  --bg: #0b1220;
  --card: #0f1a30;
  --text: #e7eefc;
  --muted: #9fb2d8;
  --line: rgba(255,255,255,0.12);
  --good: #2c6e49;
  --warn: #edae49;
  --bad: #d1495b;
  --btn: #30638e;
}

* { box-sizing: border-box; }
body {
  margin: 0;
  font-family: system-ui, -apple-system, Segoe UI, Roboto, Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
}

a { color: inherit; text-decoration: none; }

.app-container {
  display: flex;
  min-height: 100vh;
}

.sidebar {
  width: 260px;
  background: var(--card);
  border-right: 1px solid var(--line);
  display: flex;
  flex-direction: column;
  transition: transform 0.3s ease;
  z-index: 100;
}

.sidebar-header {
  padding: 20px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  border-bottom: 1px solid var(--line);
}

.logo {
  display: flex;
  align-items: center;
  gap: 10px;
  font-weight: 700;
  font-size: 1.1rem;
}

.logo-icon {
  color: var(--btn);
}

.sidebar-close {
  display: none;
  background: none;
  border: none;
  color: var(--muted);
  cursor: pointer;
  padding: 4px;
}

.sidebar-nav {
  padding: 16px 10px;
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.sidebar-footer {
  margin-top: auto;
  padding: 14px;
  border-top: 1px solid var(--line);
  font-size: 13px;
  color: var(--muted);
  text-align: center;
}

.nav-item {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 14px;
  border-radius: 10px;
  color: var(--muted);
  transition: all 0.2s;
}

.nav-item:hover {
  background: rgba(255,255,255,0.05);
  color: var(--text);
}

.nav-item.active {
  background: var(--btn);
  color: white;
}

.nav-item i {
  width: 20px;
  height: 20px;
}

.main-wrapper {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-width: 0;
}

.top-header {
  height: 64px;
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 0 20px;
  border-bottom: 1px solid var(--line);
  background: var(--bg);
}

.sidebar-open {
  display: none;
  background: none;
  border: none;
  color: var(--text);
  cursor: pointer;
  padding: 4px;
}

.header-info {
  display: flex;
  flex-direction: column;
}

main {
  padding: 20px;
  max-width: 1200px;
  width: 100%;
  margin: 0 auto;
}

@media (max-width: 768px) {
  .sidebar {
    position: fixed;
    top: 0;
    left: 0;
    bottom: 0;
    transform: translateX(-100%);
  }

  .sidebar.open {
    transform: translateX(0);
    box-shadow: 10px 0 30px rgba(0,0,0,0.5);
  }

  .sidebar-close, .sidebar-open {
    display: flex;
    align-items: center;
    justify-content: center;
  }

  .main-wrapper {
    width: 100%;
  }
}

.card {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 14px;
  padding: 14px;
}

.grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(260px, 1fr));
  gap: 12px;
}

/* Report grid: 3 columns on desktop, 2 on tablet, 1 on mobile */
.report-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 12px;
}

/* Dashboard stat cards */
.card.stat {
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center; /* centered horizontally */
  padding: 18px;
  text-align: center; /* center text inside */
}
.card.stat h3 { color: var(--muted); font-weight: 600; text-align: center; }
.card.stat .huge { font-size: 28px; font-weight: 700; color: var(--text); margin-top: 8px; text-align: center; }

@media (max-width: 1000px) {
  .card.stat { align-items: center; }
}
@media (max-width: 1000px) {
  .report-grid { grid-template-columns: repeat(2, 1fr); }
}

@media (max-width: 600px) {
  .report-grid { grid-template-columns: 1fr; }
}

/* Per-card totals row */
.card table tfoot .total-row td {
  padding-top: 8px;
  font-weight: 600;
  border-top: 1px solid var(--line);
}
.card table tfoot .total-row td:first-child {
  color: var(--muted);
  text-align: left;
  font-style: italic;
  font-weight: 400;
}
.card table tfoot .total-row td:not(:first-child) {
  text-align: center;
}



.inlineToastContainer { position: fixed; right: 16px; bottom: 16px; z-index: 70; }
.inlineToast { margin-top: 8px; padding: 10px 12px; background: rgba(0,0,0,0.6); color: white; border-radius: 8px; box-shadow: 0 6px 16px rgba(0,0,0,0.4); }

/* Sortable table header indicators */
table.sortable thead th.sortable { position: relative; }
table.sortable thead th.sortable::after { content: '\2195'; opacity: 0.5; margin-left: 6px; font-size: 11px; }
table.sortable thead th.sortable.sort-asc::after { content: '\2191'; }
table.sortable thead th.sortable.sort-desc::after { content: '\2193'; }


label { display: block; font-size: 13px; color: var(--muted); margin-bottom: 6px; }
input, select, button, textarea {
  font: inherit;
}

input, select, textarea {
  width: 100%;
  padding: 10px 10px;
  border-radius: 10px;
  border: 1px solid var(--line);
  background: rgba(255,255,255,0.03);
  color: var(--text);
  outline: none;
}

/* Improve dark-theme readability for native controls (Windows/Chrome/Edge) */
select {
  background-color: rgba(255,255,255,0.03);
  color: var(--text);
}

select option {
  background-color: var(--card);
  color: var(--text);
}

/* Hint browsers to render native pickers in dark mode */
input[type="date"],
input[type="month"],
input[type="week"],
select {
  color-scheme: dark;
}

/* Make calendar indicators visible on dark background (WebKit/Blink) */
input[type="date"]::-webkit-calendar-picker-indicator,
input[type="month"]::-webkit-calendar-picker-indicator,
input[type="week"]::-webkit-calendar-picker-indicator {
  filter: invert(1);
  opacity: 0.85;
}

/* Input + icon button row (used by Reports pickers) */
.picker-row {
  display: flex;
  gap: 8px;
  align-items: center;
}

.picker-row input,
.picker-row select {
  flex: 1;
}

/* Remove number arrows */
input[type=number]::-webkit-inner-spin-button, 
input[type=number]::-webkit-outer-spin-button { 
  -webkit-appearance: none; 
  margin: 0; 
}
input[type=number] {
  -moz-appearance: textfield;
  appearance: textfield;
}

/* Default button base - use `.btn` for semantic clarity. Also apply to buttons that do not use any of the special classes (secondary/danger/good/warn/icon-btn) */
button, button.btn, button:not(.secondary):not(.danger):not(.good):not(.warn):not(.icon-btn) {
  padding: 10px 16px;
  border-radius: 10px;
  border: 1px solid var(--line);
  background: var(--btn);
  color: white;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  transition: all 0.2s ease;
  font-weight: 500;
}

button:hover, button.btn:hover {
  transform: translateY(-1px);
  box-shadow: 0 4px 12px rgba(0,0,0,0.15);
}

button:active {
  transform: translateY(0);
}

button i {
  width: 18px;
  height: 18px;
}

/* Sidebar control appearance: visible only on mobile and not styled like normal action buttons */
button.sidebar-close, button.sidebar-open, .sidebar-close, .sidebar-open {
  background: none;
  border: none;
  padding: 6px;
  cursor: pointer;
  color: var(--warn);
  display: none !important; /* hidden by default (desktop) */
  box-shadow: none;
  transform: none;
}
/* Open/menu button uses warn color for visibility */
button.sidebar-open { color: var(--muted); }

/* Prevent hover/active effects so they don't look like normal buttons */
button.sidebar-close:hover, button.sidebar-open:hover,
.sidebar-close:hover, .sidebar-open:hover {
  transform: none;
  box-shadow: none;
  background: none;
}

/* Ensure the SVG icons inside the buttons use warn color (force with !important to override any inline SVG defaults) */
button.sidebar-close svg, button.sidebar-open svg,
.sidebar-close svg, .sidebar-open svg,
button.sidebar-close i, button.sidebar-open i,
.sidebar-close i, .sidebar-open i {
  color: var(--muted) !important;
  stroke: var(--muted) !important;
  fill: var(--muted) !important;
}

/* Show on small screens */
@media (max-width: 768px) {
  button.sidebar-close, button.sidebar-open, .sidebar-close, .sidebar-open {
    display: flex !important;
    align-items: center;
    justify-content: center;
  }
}

button.secondary {
  background: rgba(255,255,255,0.06);
}

button.secondary:hover {
  transform: translateY(-1px);
  box-shadow: 0 4px 12px rgba(0,0,0,0.15);
}

button.danger {
  background: var(--bad);
}

button.danger:hover {
  transform: translateY(-1px);
  box-shadow: 0 4px 12px rgba(0,0,0,0.15);
}

/* Positive button color */
button.good {
  background: var(--good);
  color: white;
}
button.good:hover {
  transform: translateY(-1px);
  box-shadow: 0 4px 12px rgba(0,0,0,0.15);
}

/* Warning button color */
button.warn {
  background: var(--warn);
  color: white;
}
button.warn:hover {
  transform: translateY(-1px);
  box-shadow: 0 4px 12px rgba(0,0,0,0.15);
}

/* Compact icon-only buttons used in tables/actions */
button.icon-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 36px;
  height: 36px;
  padding: 6px;
  border-radius: 8px;
  background: rgba(255,255,255,0.03);
}

/* Ward details button: prevent wrapping and use readable padding */
button.ward-details, button.ward-details:link, button.ward-details:visited {
  display: inline-block;
  white-space: nowrap;
  padding: 0;
  border: none !important;
  background: transparent !important;
  box-shadow: none !important;
  -webkit-appearance: none;
  appearance: none;
  color: var(--text);
  text-decoration: underline;
  cursor: pointer;
}
button.ward-details:hover {
  text-decoration: underline;
  filter: none;
  color: var(--text);
}
button.ward-details:focus {
  outline: 2px solid rgba(255,255,255,0.06);
  outline-offset: 2px;
}
button.icon-btn i { width: 18px; height: 18px; }
button.icon-btn:hover { transform: translateY(-1px); box-shadow: 0 4px 12px rgba(0,0,0,0.15); }
button.icon-btn.danger { background: var(--bad); }
button.icon-btn.danger:hover { transform: translateY(-1px); box-shadow: 0 4px 12px rgba(0,0,0,0.15); }

/* Positive action icon button */
button.icon-btn.good { background: var(--good); color: white; }
button.icon-btn.good:hover { transform: translateY(-1px); box-shadow: 0 4px 12px rgba(0,0,0,0.15); }

/* Warning action icon button */
button.icon-btn.warn { background: var(--warn); color: white; }
button.icon-btn.warn:hover { transform: translateY(-1px); box-shadow: 0 4px 12px rgba(0,0,0,0.15); }

/* Ensure actions gap for icon buttons and keep them on one row */
/* Single authoritative .actions rule keeps buttons right-aligned */
.actions { gap: 8px; display:flex; align-items:center; justify-content:flex-end; flex-wrap:nowrap; }

table {
  width: 100%;
  border-collapse: collapse;
}
th, td {
  padding: 10px;
  border-bottom: 1px solid var(--line);
  text-align: left;
  vertical-align: top;
}
th { color: var(--muted); font-weight: 600; font-size: 13px; }

/* Column width helpers to keep actions compact */
.table-responsive table th.col-small { width: 56px; min-width: 44px; }
.table-responsive table th.col-medium { width: 56px; min-width: 44px; }
.table-responsive table th.col-item { width: 220px; min-width: 120px; max-width: 40%; }

/* Wider name column for tables where name is primary column */
.table-name-wide th:first-child,
.table-name-wide td:first-child {
  min-width: 320px; /* increase the minimum width for the Name column */
}
@media (max-width: 640px) {
  .table-name-wide th:first-child,
  .table-name-wide td:first-child { min-width: 200px; }
}

/* Status and badge compacting */
.status { display:inline-flex; gap:6px; align-items:center; white-space:nowrap; }
.status .status-count { color: var(--muted); font-size: 12px; }

/* Compact inputs in table cells */
.catalog-table td input[data-field="max"], .catalog-table td input[data-field="quota"] { width: 100% !important; box-sizing: border-box; }

/* Keep action buttons on one line */
.actions { display:flex; gap:6px; justify-content:flex-end; align-items:center; flex-wrap:nowrap; }

/* Ensure action buttons in Catalog fit on a single row and don't wrap */
.catalog-table .actions {
  gap:6px;
  flex-wrap:nowrap;
  min-width: 88px; /* ensures two icon buttons fit comfortably */
  justify-content: flex-end;
}
.catalog-table .actions button { white-space: nowrap; }
@media (max-width: 480px) {
  .catalog-table .actions { gap:6px; flex-wrap:nowrap; min-width: 64px; }
  .catalog-table button.icon-btn { width:34px; height:34px; padding:4px; }
}

/* Usage indicators */
.catalog-table tr.usage-warn td { background: rgba(255, 165, 0, 0.04); }
.catalog-table tr.usage-warn td:first-child { border-left: 4px solid rgba(255, 165, 0, 0.25); }
.catalog-table tr.usage-critical td { background: rgba(220, 35, 45, 0.04); }
.catalog-table tr.usage-critical td:first-child { border-left: 4px solid rgba(220, 35, 45, 0.25); }

/* Action column: slightly wider to give more room for buttons */
.table-responsive table th.actions-col,
.table-responsive table td.actions-cell {
  width: 120px;
  min-width: 88px;
  max-width: 160px;
  padding-left: 6px;
  padding-right: 6px;
}

/* Inline validation styles */
.input-invalid {
  border-color: var(--bad) !important;
  box-shadow: 0 0 0 4px rgba(209,73,91,0.08);
}

.inline-error {
  color: var(--bad);
  font-size: 13px;
  margin-top: 6px;
}

/* For inputs inside table cells, make the error text smaller */
.catalog-table .inline-error { font-size: 12px; margin-top: 4px; }

/* Accessibility: visually indicate errors to screen-readers */
.input-invalid[aria-invalid="true"] {
  outline: 2px solid rgba(209,73,91,0.12);
}

}
@media (max-width: 560px) {
  .table-responsive table th.actions-col,
  .table-responsive table td.actions-cell { width: auto; min-width: 56px; }
}

/* Slightly smaller badges so status uses less space */
.badge { font-size: 11px; padding: 2px 6px; }

@media (max-width: 760px) {
  .table-responsive table th.col-small { min-width: 35px; width: 35px; }
  .table-responsive table th.col-medium { min-width: 35px; width: 35px; }
  td input[data-field="max"], td input[data-field="quota"] { width: 35px; }
  .catalog-table td input[data-field="max"], .catalog-table td input[data-field="quota"] { width: 100% !important; box-sizing: border-box; }
  .status .status-count { display:none; }
}
@media (max-width: 480px) {
  .actions { gap:8px; }
  .actions { flex-wrap: wrap; }
}

.badge {
  display: inline-flex;
  align-items: center;
  padding: 3px 8px;
  border-radius: 999px;
  border: 1px solid var(--line);
  font-size: 12px;
}
.badge.good { color: var(--good); }
.badge.warn { color: var(--warn); }
.badge.bad { color: var(--bad); }

/* Responsive table helpers */
.table-responsive { overflow-x: auto; }
.actions {
  display: flex;
  gap: 8px;
  justify-content: flex-end;
  align-items: center;
  flex-wrap: wrap;
}
.actions button { white-space: nowrap; }

@media (max-width: 640px) {
  .actions { justify-content: flex-start; }
  td.actions-cell { padding-top: 8px; }
  td input[data-field="max"], td input[data-field="quota"] { width: 72px; }
}

.toastHost {
  position: fixed;
  top: 12px;
  right: 12px;
  display: grid;
  gap: 10px;
  z-index: 50;
}
.toast {
  max-width: 380px;
  background: rgba(15,26,48,0.96);
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 12px;
}

.modalOverlay {
  position: fixed;
  inset: 0;
  display: none;
  place-items: center;
  padding: 16px;
  background: rgba(15,26,48,0.70);
  z-index: 60;
}
.modalOverlay.open { display: grid; }

.modal {
  width: min(560px, 100%);
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 14px;
  padding: 14px;
  max-height: 85vh;
  display: flex;
  flex-direction: column;
}

.modalHeader {
  display: flex;
  gap: 10px;
  align-items: center;
  margin-bottom: 10px;
}

.modalBody {
  flex: 1 1 auto;
  overflow: auto;
  max-height: calc(85vh - 140px);
  white-space: normal;
  color: var(--text);
  line-height: 1.45;
}

.modalActions {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
  margin-top: 14px;
}

.searchable {
  position: relative;
}
.searchable .dropdown {
  position: absolute;
  top: calc(100% + 6px);
  left: 0;
  right: 0;
  background: #0e1730;
  border: 1px solid var(--line);
  border-radius: 12px;
  max-height: 260px;
  overflow: auto;
  display: none;
  z-index: 10; /* base dropdown stacking */
}
/* Ensure ward select dropdown sits above other controls */
#wardSelect .dropdown { z-index: 60; }
.searchable.open .dropdown { display: block; }
.searchable .option {
  padding: 10px;
  cursor: pointer;
  border-bottom: 1px solid rgba(255,255,255,0.06);
}
.searchable .option:hover { background: rgba(255,255,255,0.04); }
.searchable .option small { color: var(--muted); }

/* Reports custom pickers (reuse searchable dropdown look) */
.dp-nav {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 8px;
  border-bottom: 1px solid rgba(255,255,255,0.06);
}

.dp-title {
  font-weight: 600;
  color: var(--text);
  font-size: 13px;
}

.dp-nav-btn {
  background: rgba(255,255,255,0.06);
  border: 1px solid var(--line);
  color: var(--text);
  border-radius: 10px;
  padding: 6px 10px;
  cursor: pointer;
}

.dp-dow {
  display: grid;
  grid-template-columns: repeat(7, minmax(0, 1fr));
  gap: 6px;
  padding: 8px 10px 0 10px;
  color: var(--muted);
  font-size: 12px;
}

.dp-grid {
  display: grid;
  grid-template-columns: repeat(7, minmax(0, 1fr));
  gap: 6px;
  padding: 8px 10px 10px 10px;
}

.dp-cell {
  background: rgba(255,255,255,0.03);
  border: 1px solid rgba(255,255,255,0.08);
  color: var(--text);
  border-radius: 10px;
  padding: 8px 0;
  cursor: pointer;
}

.dp-cell.muted {
  color: var(--muted);
}

.dp-selected {
  background: rgba(255,255,255,0.10);
  border-color: rgba(255,255,255,0.18);
}

.dp-month-grid {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 8px;
  padding: 10px;
}

.dp-month, .dp-year {
  background: rgba(255,255,255,0.03);
  border: 1px solid rgba(255,255,255,0.08);
  color: var(--text);
  border-radius: 10px;
  padding: 10px;
  cursor: pointer;
  text-align: center;
}

.dp-year-grid {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 8px;
  padding: 10px;
}

/* Two-column layout for small forms */
.row.two-col {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 10px;
  align-items: start;
}
/* Left-align the Add button */
.row.two-col .control-button { justify-self: start; align-self: end; }
.row.two-col .col-compact input { width: 100%; }
@media (max-width: 640px) {
  .row.two-col { grid-template-columns: 1fr; }
  .row.two-col .control-button { justify-self: start; }
}

/* Three-column layout for small forms */
.row.three-col {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 10px;
  align-items: start;
}
.row.three-col .control-button { justify-self: start; align-self: end; }
@media (max-width: 640px) {
  .row.three-col { grid-template-columns: 1fr; }
  .row.three-col .control-button { justify-self: start; }
}

.muted { color: var(--muted); }
