/* ============================================================
   Koaf InvoicePro - Main Stylesheet
   ============================================================ */

:root {
  --brand: #2563eb;
  --brand-dark: #1d4ed8;
  --brand-light: #dbeafe;
  --bg: #f8fafc;
  --sidebar-bg: #0f172a;
  --sidebar-text: #94a3b8;
  --sidebar-active: #ffffff;
  --card-bg: #ffffff;
  --border: #e2e8f0;
  --text: #1e293b;
  --text-muted: #64748b;
  --success: #16a34a;
  --danger: #dc2626;
  --warning: #d97706;
  --info: #0891b2;
  --purple: #7c3aed;
  --radius: 8px;
  --shadow: 0 1px 3px rgba(0,0,0,.08), 0 1px 2px rgba(0,0,0,.06);
  --shadow-md: 0 4px 6px rgba(0,0,0,.07), 0 2px 4px rgba(0,0,0,.06);
}

*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

html { font-size: 16px; }

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

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

img { max-width: 100%; }

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

.sidebar {
  width: 240px;
  min-height: 100vh;
  background: var(--sidebar-bg);
  display: flex;
  flex-direction: column;
  flex-shrink: 0;
  transition: transform .25s;
}

.sidebar-nav {
  padding: 1rem 0;
  display: flex;
  flex-direction: column;
  gap: .25rem;
}

.sidebar-nav a {
  display: flex;
  align-items: center;
  gap: .75rem;
  padding: .65rem 1.25rem;
  color: var(--sidebar-text);
  font-size: .875rem;
  font-weight: 500;
  border-radius: 0;
  transition: background .15s, color .15s;
  text-decoration: none;
}

.sidebar-nav a svg {
  width: 18px;
  height: 18px;
  stroke: currentColor;
  fill: none;
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
  flex-shrink: 0;
}

.sidebar-nav a:hover,
.sidebar-nav a.active {
  background: rgba(255,255,255,.08);
  color: var(--sidebar-active);
  text-decoration: none;
}

.sidebar-nav a.active { border-left: 3px solid var(--brand); padding-left: calc(1.25rem - 3px); }

.sidebar-divider {
  height: 1px;
  background: rgba(255,255,255,.08);
  margin: .5rem 1rem;
}

.main-content {
  flex: 1;
  padding: 1.5rem;
  overflow-x: hidden;
}

/* ---- Navbar ---- */
.navbar {
  display: none;
  align-items: center;
  justify-content: space-between;
  background: var(--sidebar-bg);
  padding: .75rem 1rem;
  position: sticky;
  top: 0;
  z-index: 100;
}

.nav-brand { display: flex; align-items: center; gap: .5rem; }
.nav-logo { height: 32px; width: auto; border-radius: 4px; }
.nav-title { color: #fff; font-weight: 700; font-size: 1rem; }

.nav-toggle {
  background: none;
  border: none;
  color: #fff;
  font-size: 1.5rem;
  cursor: pointer;
}

/* ---- Cards ---- */
.card {
  background: var(--card-bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 1.5rem;
  margin-bottom: 1.25rem;
}

.card-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 1.25rem;
  padding-bottom: 1rem;
  border-bottom: 1px solid var(--border);
}

.card-title {
  font-size: 1.05rem;
  font-weight: 600;
  color: var(--text);
}

/* ---- Stats Grid ---- */
.stats-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(180px, 1fr));
  gap: 1rem;
  margin-bottom: 1.5rem;
}

.stat-card {
  background: var(--card-bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1.25rem;
  box-shadow: var(--shadow);
}

.stat-label {
  font-size: .75rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: .05em;
  color: var(--text-muted);
  margin-bottom: .35rem;
}

.stat-value {
  font-size: 1.6rem;
  font-weight: 700;
  color: var(--text);
}

.stat-card.accent { border-top: 3px solid var(--brand); }
.stat-card.success { border-top: 3px solid var(--success); }
.stat-card.danger  { border-top: 3px solid var(--danger); }
.stat-card.warning { border-top: 3px solid var(--warning); }
.stat-card.purple  { border-top: 3px solid var(--purple); }

/* ---- Buttons ---- */
.btn {
  display: inline-flex;
  align-items: center;
  gap: .45rem;
  padding: .5rem 1rem;
  font-size: .875rem;
  font-weight: 500;
  border-radius: 6px;
  border: 1px solid transparent;
  cursor: pointer;
  transition: all .15s;
  text-decoration: none;
  white-space: nowrap;
}

.btn:hover { text-decoration: none; opacity: .9; }

.btn-primary   { background: var(--brand); color: #fff; border-color: var(--brand); }
.btn-secondary { background: #f1f5f9; color: var(--text); border-color: var(--border); }
.btn-success   { background: var(--success); color: #fff; }
.btn-danger    { background: var(--danger); color: #fff; }
.btn-warning   { background: var(--warning); color: #fff; }
.btn-outline   { background: transparent; color: var(--brand); border-color: var(--brand); }
.btn-whatsapp  { background: #25d366; color: #fff; }
.btn-sm        { padding: .3rem .65rem; font-size: .8rem; }
.btn-icon svg  { width: 15px; height: 15px; stroke: currentColor; fill: none; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }

/* ---- Forms ---- */
.form-group { margin-bottom: 1rem; }
.form-label { display: block; font-size: .875rem; font-weight: 500; margin-bottom: .35rem; }
.form-control {
  width: 100%;
  padding: .5rem .75rem;
  font-size: .875rem;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: #fff;
  color: var(--text);
  transition: border-color .15s, box-shadow .15s;
  outline: none;
}
.form-control:focus {
  border-color: var(--brand);
  box-shadow: 0 0 0 3px rgba(37,99,235,.15);
}

textarea.form-control { min-height: 90px; resize: vertical; }
select.form-control { appearance: auto; }

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

.form-actions {
  display: flex;
  gap: .75rem;
  align-items: center;
  margin-top: 1.25rem;
}

/* ---- Tables ---- */
.table-wrapper { overflow-x: auto; }

table {
  width: 100%;
  border-collapse: collapse;
  font-size: .875rem;
}

thead th {
  text-align: left;
  padding: .65rem .85rem;
  background: #f8fafc;
  font-size: .75rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: .05em;
  color: var(--text-muted);
  border-bottom: 1px solid var(--border);
}

tbody td {
  padding: .75rem .85rem;
  border-bottom: 1px solid var(--border);
  vertical-align: middle;
}

tbody tr:last-child td { border-bottom: none; }
tbody tr:hover { background: #f8fafc; }

/* ---- Badges ---- */
.badge {
  display: inline-block;
  padding: .2rem .55rem;
  font-size: .7rem;
  font-weight: 700;
  border-radius: 20px;
  text-transform: uppercase;
  letter-spacing: .04em;
}

.badge-secondary { background: #f1f5f9; color: var(--text-muted); }
.badge-info      { background: #e0f2fe; color: #0369a1; }
.badge-warning   { background: #fef3c7; color: #92400e; }
.badge-success   { background: #dcfce7; color: #166534; }
.badge-danger    { background: #fee2e2; color: #991b1b; }
.badge-purple    { background: #ede9fe; color: #5b21b6; }

/* ---- Alerts ---- */
.alert {
  padding: .85rem 1rem;
  border-radius: var(--radius);
  font-size: .875rem;
  margin-bottom: 1rem;
  border: 1px solid transparent;
}

.alert-success { background: #dcfce7; color: #166534; border-color: #86efac; }
.alert-danger  { background: #fee2e2; color: #991b1b; border-color: #fca5a5; }
.alert-info    { background: #e0f2fe; color: #0369a1; border-color: #7dd3fc; }
.alert-warning { background: #fef3c7; color: #92400e; border-color: #fde68a; }

/* ---- Page Header ---- */
.page-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 1.5rem;
  flex-wrap: wrap;
  gap: .75rem;
}

.page-title {
  font-size: 1.35rem;
  font-weight: 700;
  color: var(--text);
}

.page-subtitle {
  font-size: .875rem;
  color: var(--text-muted);
  margin-top: .15rem;
}

/* ---- Search/Filter Bar ---- */
.filter-bar {
  display: flex;
  gap: .75rem;
  flex-wrap: wrap;
  margin-bottom: 1.25rem;
}

.filter-bar .form-control { flex: 1; min-width: 140px; }

/* ---- Invoice Items Table ---- */
.items-table { width: 100%; border-collapse: collapse; margin-bottom: .75rem; }
.items-table th { padding: .5rem .6rem; font-size: .78rem; font-weight: 700; text-transform: uppercase; color: var(--text-muted); background: #f8fafc; border-bottom: 1px solid var(--border); }
.items-table td { padding: .45rem .4rem; border-bottom: 1px solid #f1f5f9; vertical-align: middle; }
.items-table .item-desc { min-width: 200px; }
.items-table input { width: 100%; }

/* ---- Invoice View ---- */
.invoice-view {
  background: #fff;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow-md);
  max-width: 860px;
  margin: 0 auto;
  padding: 2.5rem;
}

.invoice-header {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  margin-bottom: 2rem;
  flex-wrap: wrap;
  gap: 1rem;
}

.invoice-meta-table { width: 100%; font-size: .875rem; }
.invoice-meta-table td { padding: .3rem .5rem; }
.invoice-meta-table td:first-child { font-weight: 600; color: var(--text-muted); width: 120px; }

.invoice-parties {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 2rem;
  margin-bottom: 2rem;
  padding-bottom: 1.5rem;
  border-bottom: 1px solid var(--border);
}

.party-label { font-size: .7rem; font-weight: 700; text-transform: uppercase; letter-spacing: .08em; color: var(--text-muted); margin-bottom: .4rem; }
.party-name  { font-size: 1rem; font-weight: 700; margin-bottom: .2rem; }
.party-detail { font-size: .85rem; color: var(--text-muted); line-height: 1.6; }

.invoice-totals {
  margin-left: auto;
  max-width: 280px;
  font-size: .875rem;
}

.invoice-totals table { width: 100%; }
.invoice-totals td { padding: .35rem .5rem; }
.invoice-totals td:last-child { text-align: right; font-weight: 500; }
.invoice-totals .total-row td { font-size: 1.05rem; font-weight: 700; border-top: 2px solid var(--border); padding-top: .65rem; color: var(--brand); }

.invoice-footer { margin-top: 2rem; padding-top: 1rem; border-top: 1px solid var(--border); font-size: .85rem; color: var(--text-muted); }

.invoice-actions {
  display: flex;
  gap: .65rem;
  flex-wrap: wrap;
  margin-bottom: 1.5rem;
}

/* ---- Toggle switch ---- */
.toggle-group { display: flex; align-items: center; gap: .75rem; margin-bottom: 1rem; }
.toggle { position: relative; display: inline-block; width: 40px; height: 22px; }
.toggle input { opacity: 0; width: 0; height: 0; }
.toggle-slider {
  position: absolute;
  cursor: pointer;
  inset: 0;
  background: #cbd5e1;
  border-radius: 22px;
  transition: .25s;
}
.toggle-slider::before {
  content: '';
  position: absolute;
  width: 16px; height: 16px;
  left: 3px; top: 3px;
  background: #fff;
  border-radius: 50%;
  transition: .25s;
}
.toggle input:checked + .toggle-slider { background: var(--brand); }
.toggle input:checked + .toggle-slider::before { transform: translateX(18px); }

/* ---- Monthly chart ---- */
.chart-bar-wrap { display: flex; align-items: flex-end; gap: .5rem; height: 120px; margin-top: .75rem; }
.chart-bar-col { flex: 1; display: flex; flex-direction: column; align-items: center; gap: .25rem; }
.chart-bar { width: 100%; background: var(--brand); border-radius: 4px 4px 0 0; min-height: 4px; opacity: .85; transition: opacity .15s; }
.chart-bar:hover { opacity: 1; }
.chart-label { font-size: .65rem; color: var(--text-muted); text-align: center; }

/* ---- Auth pages ---- */
.auth-wrap {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, #0f172a 0%, #1e3a8a 100%);
  padding: 1.5rem;
}

.auth-card {
  background: #fff;
  border-radius: 12px;
  box-shadow: 0 20px 50px rgba(0,0,0,.25);
  padding: 2.5rem;
  width: 100%;
  max-width: 420px;
}

.auth-logo {
  text-align: center;
  margin-bottom: 1.75rem;
}

.auth-logo-title {
  font-size: 1.4rem;
  font-weight: 800;
  color: var(--brand);
  display: block;
}

.auth-subtitle { font-size: .875rem; color: var(--text-muted); text-align: center; margin-bottom: 1.5rem; }

.auth-footer { text-align: center; margin-top: 1.25rem; font-size: .875rem; color: var(--text-muted); }

/* ---- Responsive ---- */
@media (max-width: 768px) {
  .navbar { display: flex; }
  .layout { flex-direction: column; }
  .sidebar { width: 240px; min-height: auto; position: fixed; top: 0; left: 0; height: 100%; z-index: 200; transform: translateX(-100%); }
  .sidebar.open { transform: translateX(0); }
  .main-content { padding: 1rem; margin-top: 56px; }
  .form-row { grid-template-columns: 1fr; }
  .invoice-parties { grid-template-columns: 1fr; }
  .invoice-view { padding: 1.25rem; }
  .stats-grid { grid-template-columns: 1fr 1fr; }
  .page-header { flex-direction: column; align-items: flex-start; }
  .invoice-header { flex-direction: column; }
}

@media (max-width: 480px) {
  .stats-grid { grid-template-columns: 1fr; }
}

/* ---- Print ---- */
@media print {
  .navbar, .sidebar, .invoice-actions, .no-print { display: none !important; }
  .main-content { padding: 0; margin: 0; }
  .invoice-view { box-shadow: none; border: none; max-width: 100%; }
  body { background: #fff; }
}

/* ---- Color picker preview ---- */
.color-preview { display: inline-block; width: 28px; height: 28px; border-radius: 4px; border: 1px solid var(--border); vertical-align: middle; margin-left: .5rem; }

/* ---- Empty state ---- */
.empty-state { text-align: center; padding: 3rem 1rem; color: var(--text-muted); }
.empty-state-icon { font-size: 3rem; margin-bottom: .75rem; }
.empty-state h3 { font-size: 1rem; font-weight: 600; margin-bottom: .35rem; color: var(--text); }
