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

:root {
  --primary: #2563eb;
  --primary-dark: #1d4ed8;
  --danger:  #dc2626;
  --success: #16a34a;
  --warning: #d97706;
  --bg:      #f8fafc;
  --surface: #ffffff;
  --border:  #e2e8f0;
  --text:    #1e293b;
  --muted:   #64748b;
  --radius:  0.5rem;
  --shadow:  0 1px 3px rgba(0,0,0,.1), 0 1px 2px rgba(0,0,0,.06);
}

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  font-size: 0.9375rem;
  color: var(--text);
  background: var(--bg);
  min-height: 100vh;
}

a { color: var(--primary); text-decoration: none; }
a:hover { text-decoration: underline; }

/* ── Layout ───────────────────────────────────────────── */
.layout { display: flex; min-height: 100vh; }

.sidebar {
  width: 220px; min-height: 100vh;
  background: #1e293b; color: #e2e8f0;
  display: flex; flex-direction: column;
  flex-shrink: 0;
  transition: width .2s ease;
}
.sidebar-brand {
  padding: 1rem .85rem;
  font-size: 1rem; font-weight: 700;
  border-bottom: 1px solid #334155;
  color: #fff;
  display: flex; align-items: center; gap: .5rem;
  min-height: 3.25rem;
}
.brand-ico { font-size: 1.15rem; flex-shrink: 0; }
.brand-text { white-space: nowrap; overflow: hidden; }
.nav-ico { flex-shrink: 0; width: 1.35rem; text-align: center; }
.nav-label { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.nav-section {
  padding: .55rem .85rem .2rem;
  font-size: .68rem; color: #64748b;
  text-transform: uppercase; letter-spacing: .07em;
  margin-top: .35rem;
}
.sidebar nav { padding: .65rem 0; flex: 1; }
.sidebar nav a {
  display: flex; align-items: center; gap: .55rem;
  padding: .55rem .85rem;
  color: #cbd5e1; font-size: .84rem;
  border-left: 3px solid transparent;
  transition: background .15s, border-color .15s;
}
.sidebar nav a:hover,
.sidebar nav a.active {
  background: #334155; border-left-color: var(--primary); color: #fff;
  text-decoration: none;
}
.sidebar-footer {
  padding: 1rem 1.25rem;
  border-top: 1px solid #334155;
  font-size: .8rem; color: #94a3b8;
}

.main-content { flex: 1; display: flex; flex-direction: column; overflow: auto; }

.topbar {
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  padding: .75rem 1.25rem;
  display: flex; align-items: center; justify-content: space-between;
  box-shadow: var(--shadow);
  gap: .75rem;
}
.topbar-left { display: flex; align-items: center; gap: .5rem; min-width: 0; }
.topbar-title { font-size: 1.05rem; font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.topbar-user  { font-size: .875rem; color: var(--muted); flex-shrink: 0; }
.sidebar-pin-btn {
  display: none;
  background: #f1f5f9; border: 1px solid var(--border);
  border-radius: var(--radius); padding: .35rem .55rem;
  cursor: pointer; font-size: .9rem; line-height: 1;
  color: var(--text);
}
.sidebar-pin-btn:hover { background: #e2e8f0; }

.content { padding: 2rem; }

/* ── Cards ────────────────────────────────────────────── */
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  overflow: hidden;
}
.card-header {
  padding: 1rem 1.5rem;
  border-bottom: 1px solid var(--border);
  font-weight: 600; font-size: .9375rem;
  background: #f1f5f9;
  display: flex; align-items: center; justify-content: space-between;
}
.card-body { padding: 1.5rem; }

/* ── Stat cards ───────────────────────────────────────── */
.stats-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: 1.25rem;
  margin-bottom: 2rem;
}
.stat-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1.25rem 1.5rem;
  box-shadow: var(--shadow);
}
.stat-label { font-size: .78rem; color: var(--muted); text-transform: uppercase; letter-spacing: .05em; }
.stat-value { font-size: 1.75rem; font-weight: 700; margin-top: .25rem; }
.stat-sub   { font-size: .8rem; color: var(--muted); margin-top: .2rem; }

/* ── Tables ───────────────────────────────────────────── */
.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; font-size: .875rem; }
thead th {
  padding: .7rem 1rem;
  text-align: left;
  background: #f1f5f9;
  border-bottom: 2px solid var(--border);
  font-weight: 600; white-space: nowrap;
}
tbody td { padding: .65rem 1rem; border-bottom: 1px solid var(--border); }
tbody tr:last-child td { border-bottom: none; }
tbody tr:hover { background: #f8fafc; }

/* ── Forms ────────────────────────────────────────────── */
.form-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 1.25rem; }
@media (max-width: 640px) { .form-grid { grid-template-columns: 1fr; } }

.form-group { display: flex; flex-direction: column; gap: .35rem; }
.form-group label { font-weight: 500; font-size: .875rem; }
.form-control {
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: .5rem .75rem;
  font-size: .875rem;
  transition: border-color .15s, box-shadow .15s;
  width: 100%;
}
.form-control:focus {
  outline: none;
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(37,99,235,.15);
}
.form-control.is-invalid { border-color: var(--danger); }
.invalid-feedback { color: var(--danger); font-size: .8rem; }

/* ── Buttons ──────────────────────────────────────────── */
.btn {
  display: inline-flex; align-items: center; gap: .4rem;
  padding: .475rem 1rem;
  font-size: .875rem; font-weight: 500;
  border: 1px solid transparent;
  border-radius: var(--radius);
  cursor: pointer;
  transition: background .15s, opacity .15s;
  text-decoration: none;
}
.btn:hover { opacity: .9; text-decoration: none; }
.btn-primary  { background: var(--primary);  color: #fff; }
.btn-danger   { background: var(--danger);   color: #fff; }
.btn-success  { background: var(--success);  color: #fff; }
.btn-warning  { background: var(--warning);  color: #fff; }
.btn-outline  { background: transparent; border-color: var(--border); color: var(--text); }
.btn-sm { padding: .3rem .75rem; font-size: .8rem; }

/* ── Badges ───────────────────────────────────────────── */
.badge {
  display: inline-block; padding: .2rem .55rem;
  font-size: .75rem; font-weight: 600;
  border-radius: 9999px;
}
.badge-success { background: #dcfce7; color: #15803d; }
.badge-danger  { background: #fee2e2; color: #b91c1c; }
.badge-warning { background: #fef9c3; color: #92400e; }
.badge-info    { background: #dbeafe; color: #1d4ed8; }

/* ── Alerts ───────────────────────────────────────────── */
.alert {
  padding: .85rem 1.25rem;
  border-radius: var(--radius);
  margin-bottom: 1.25rem;
  font-size: .875rem;
}
.alert-success { background: #f0fdf4; border: 1px solid #bbf7d0; color: #15803d; }
.alert-danger  { background: #fef2f2; border: 1px solid #fecaca; color: #b91c1c; }

/* ── Pagination ───────────────────────────────────────── */
.pagination { display: flex; gap: .4rem; align-items: center; margin-top: 1.25rem; }
.page-btn {
  padding: .35rem .75rem; border: 1px solid var(--border);
  border-radius: var(--radius); font-size: .8rem; cursor: pointer;
  background: var(--surface); color: var(--text);
  text-decoration: none;
}
.page-btn.active { background: var(--primary); color: #fff; border-color: var(--primary); }
.page-btn:disabled { opacity: .4; cursor: default; }

/* ── Audit log changes ─────────────────────────────────── */
pre.audit-changes {
  margin: 0;
  padding: .5rem .65rem;
  background: #f0fdf4;
  border: 1px solid #bbf7d0;
  border-radius: var(--radius);
  font-family: inherit;
  font-size: .78rem;
  line-height: 1.45;
  white-space: pre-wrap;
  word-break: break-word;
  color: #14532d;
}

/* ── Utilities ────────────────────────────────────────── */
.text-right { text-align: right; }
.text-muted { color: var(--muted); }
.mt-1 { margin-top: .5rem; }
.mt-2 { margin-top: 1rem; }
.mt-3 { margin-top: 1.5rem; }
.mb-2 { margin-bottom: 1rem; }
.gap-2 { gap: .5rem; }
.flex { display: flex; }
.items-center { align-items: center; }
.justify-between { justify-content: space-between; }
.locked-banner {
  background: #fef9c3; border: 1px solid #fde68a;
  color: #92400e; padding: .6rem 1rem;
  border-radius: var(--radius); font-size: .875rem;
  margin-bottom: 1rem;
}

/* ── Hamburger ────────────────────────────────────────── */
.hamburger {
  display: none;
  background: none; border: none;
  cursor: pointer; padding: .3rem .5rem;
  color: var(--text);
}
.hamburger .bar {
  display: block; width: 22px; height: 2px;
  background: currentColor; margin: 5px 0;
  transition: all .2s;
}

/* ── Sidebar overlay (mobile) ─────────────────────────── */
.sidebar-overlay {
  display: none; position: fixed; inset: 0;
  background: rgba(0,0,0,.45); z-index: 998;
}
.sidebar-overlay.active { display: block; }

/* ── Responsive ───────────────────────────────────────── */
@media (max-width: 768px) {
  .hamburger { display: block; }

  .sidebar {
    position: fixed; left: -260px; top: 0;
    z-index: 999; height: 100vh; width: 240px;
    transition: left .25s ease;
  }
  .sidebar.open { left: 0; }
  .layout { display: block; }
  .main-content { width: 100%; }
  .topbar { padding: .6rem 1rem; }
  .content { padding: 1rem; }

  .stats-grid {
    grid-template-columns: repeat(2, 1fr);
    gap: .75rem; margin-bottom: 1.25rem;
  }
  .stat-value { font-size: 1.4rem; }

  .flex { flex-wrap: wrap; }
  .btn { font-size: .8rem; padding: .4rem .75rem; }
  .btn-sm { padding: .25rem .55rem; font-size: .75rem; }
  .card-header { padding: .75rem 1rem; }
  .card-body   { padding: 1rem; }

  table { font-size: .8rem; }
  thead th, tbody td { padding: .45rem .6rem; }
}

@media (max-width: 480px) {
  .stats-grid { grid-template-columns: 1fr; }
  .form-grid  { grid-template-columns: 1fr; }
  .stat-value { font-size: 1.25rem; }
}

/* ── Sidebar mini (desktop) ───────────────────────────── */
@media (min-width: 769px) {
  .sidebar-pin-btn { display: inline-flex; align-items: center; justify-content: center; }
  .layout.sidebar-mini .sidebar { width: 4.25rem; }
  .layout.sidebar-mini .brand-text,
  .layout.sidebar-mini .nav-label,
  .layout.sidebar-mini .nav-section,
  .layout.sidebar-mini .sidebar-footer .footer-user,
  .layout.sidebar-mini .footer-credit { display: none; }
  .layout.sidebar-mini .sidebar-brand { justify-content: center; padding: 1rem .5rem; }
  .layout.sidebar-mini .sidebar nav a { justify-content: center; padding: .65rem .5rem; }
  .layout.sidebar-mini .sidebar-footer { padding: .75rem .35rem; text-align: center; }
  .layout.sidebar-mini .logout-link { display: inline-block; font-size: .75rem; }
}

/* ── Page helpers ─────────────────────────────────────── */
.page-actions { display: flex; gap: .5rem; flex-wrap: wrap; margin-bottom: .75rem; }
.page-hint { font-size: .85rem; color: var(--muted); margin: -.25rem 0 .75rem; }
.card-subhint { font-size: .8rem; color: var(--muted); padding: .5rem 1rem 0; margin: 0; }
.filter-form { display: flex; flex-wrap: wrap; gap: .75rem; align-items: flex-end; }
.filter-form .form-group { margin: 0; min-width: 140px; flex: 1 1 140px; }
.stats-grid-compact { margin-bottom: 1rem; }
.stat-card-danger { border-left: 4px solid #f87171; }
.stat-card-danger .stat-value { color: #dc2626; }
.stat-card-success { border-left: 4px solid #4ade80; }
.stat-card-success .stat-value { color: #15803d; }
.mb-2 { margin-bottom: 1rem; }
.cell-sub { font-size: .75rem; color: var(--muted); margin-top: .2rem; }
.table-empty { text-align: center; padding: 2rem; color: #94a3b8; }
tr.row-overdue td { background: #fef2f2; }
.data-table { min-width: 720px; }

/* ── Mobile tables (card layout) ──────────────────────── */
@media (max-width: 768px) {
  .topbar-user { display: none; }
  .table-invoices thead { display: none; }
  .table-invoices tbody tr {
    display: block; margin: 0 0 .75rem; padding: .75rem;
    border: 1px solid var(--border); border-radius: var(--radius);
    background: var(--surface);
  }
  .table-invoices tbody tr.row-overdue { background: #fef2f2; }
  .table-invoices tbody td {
    display: flex; justify-content: space-between; align-items: flex-start;
    gap: .75rem; padding: .35rem 0; border: none;
  }
  .table-invoices tbody td::before {
    content: attr(data-label);
    font-weight: 600; font-size: .72rem; color: var(--muted);
    flex: 0 0 38%; text-transform: uppercase; letter-spacing: .03em;
  }
  .table-invoices .td-actions { flex-direction: column; align-items: stretch; }
  .table-invoices .td-actions::before { margin-bottom: .25rem; }
  .table-invoices .td-actions .btn { width: 100%; justify-content: center; margin: .15rem 0; }
  .table-invoices .col-periodicity,
  .table-invoices .col-completed { display: none; }
  .form-control, select.form-control, input.form-control { font-size: 16px; }
}
