/* ══════════════════════════════════════════════════════════════════
   Incorva Invoice System – Mobile-First Responsive Styles
   ══════════════════════════════════════════════════════════════════ */
:root {
  --primary: #2563eb;
  --primary-dark: #1d4ed8;
  --success: #16a34a;
  --warning: #d97706;
  --danger: #dc2626;
  --info: #0891b2;
  --bg: #f1f5f9;
  --sidebar-bg: #0f172a;
  --sidebar-w: 260px;
  --topbar-h: 60px;
}

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

html { -webkit-text-size-adjust: 100%; }

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  background: var(--bg);
  color: #1e293b;
  overflow-x: hidden;
  font-size: 14px;
  line-height: 1.5;
}

/* ── Sidebar ─────────────────────────────────────────────────────── */
#sidebar {
  position: fixed; left: 0; top: 0; bottom: 0;
  width: var(--sidebar-w);
  background: var(--sidebar-bg);
  z-index: 200;
  display: flex; flex-direction: column;
  transition: transform .3s cubic-bezier(.4,0,.2,1);
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
}
.sidebar-logo {
  padding: 18px 16px 14px;
  border-bottom: 1px solid rgba(255,255,255,.08);
  flex-shrink: 0;
}
.logo-wrap { display: flex; align-items: center; gap: 10px; }
.logo-icon {
  width: 38px; height: 38px; flex-shrink: 0;
  background: linear-gradient(135deg,#2563eb,#7c3aed);
  border-radius: 10px;
  display: flex; align-items: center; justify-content: center;
  font-size: 16px; color: white;
}
.logo-icon img { width: 24px; height: 24px; object-fit: contain; }
.logo-text h1 { font-size: 14px; font-weight: 700; color: white; }
.logo-text p { font-size: 10px; color: #64748b; margin-top: 1px; }
.sidebar-nav { flex: 1; padding: 10px 0; }
.nav-section {
  padding: 8px 14px 3px;
  font-size: 9.5px; font-weight: 700; color: #475569;
  letter-spacing: .08em; text-transform: uppercase;
}
.nav-item {
  display: flex; align-items: center; gap: 9px;
  padding: 9px 14px; margin: 1px 7px;
  border-radius: 8px; cursor: pointer;
  color: #94a3b8; font-size: 13px; font-weight: 500;
  text-decoration: none; transition: all .15s;
  -webkit-tap-highlight-color: transparent;
}
.nav-item:hover { background: rgba(255,255,255,.07); color: #e2e8f0; }
.nav-item.active { background: rgba(37,99,235,.2); color: #60a5fa; border-left: 3px solid #2563eb; padding-left: 11px; }
.nav-item i { width: 16px; text-align: center; font-size: 13px; flex-shrink: 0; }
.sidebar-footer {
  padding: 12px 14px;
  border-top: 1px solid rgba(255,255,255,.07);
  flex-shrink: 0;
}
/* Overlay for mobile */
#sidebar-overlay {
  display: none;
  position: fixed; inset: 0;
  background: rgba(0,0,0,.5);
  z-index: 199;
  -webkit-tap-highlight-color: transparent;
}
#sidebar-overlay.show { display: block; }

/* ── Main layout ─────────────────────────────────────────────────── */
#main {
  margin-left: var(--sidebar-w);
  min-height: 100vh;
  display: flex; flex-direction: column;
}
#topbar {
  height: var(--topbar-h);
  background: white;
  border-bottom: 1px solid #e2e8f0;
  display: flex; align-items: center; justify-content: space-between;
  padding: 0 20px;
  position: sticky; top: 0; z-index: 100;
  box-shadow: 0 1px 4px rgba(0,0,0,.05);
}
#content { flex: 1; padding: 20px; }

/* ── Cards ────────────────────────────────────────────────────────── */
.card {
  background: white; border-radius: 14px;
  border: 1px solid #e2e8f0;
  box-shadow: 0 1px 3px rgba(0,0,0,.04);
}
.card-header { padding: 18px 20px 0; }
.card-body { padding: 20px; }
.card-title { font-size: 15px; font-weight: 700; color: #1e293b; }

/* ── Stats grid ───────────────────────────────────────────────────── */
.stat-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 12px;
  margin-bottom: 20px;
}
.stat-card {
  background: white; border-radius: 14px; padding: 16px;
  border: 1px solid #e2e8f0;
  display: flex; align-items: center; gap: 12px;
}
.stat-icon {
  width: 44px; height: 44px; border-radius: 12px;
  display: flex; align-items: center; justify-content: center;
  font-size: 18px; flex-shrink: 0;
}
.stat-icon.blue  { background: #eff6ff; color: #2563eb; }
.stat-icon.green { background: #f0fdf4; color: #16a34a; }
.stat-icon.amber { background: #fffbeb; color: #d97706; }
.stat-icon.red   { background: #fef2f2; color: #dc2626; }
.stat-icon.purple{ background: #faf5ff; color: #7c3aed; }
.stat-label { font-size: 11px; color: #64748b; font-weight: 500; }
.stat-value { font-size: 18px; font-weight: 800; color: #1e293b; line-height: 1.2; word-break: break-all; }
.stat-sub   { font-size: 10px; color: #94a3b8; }

/* ── Badges ───────────────────────────────────────────────────────── */
.badge {
  display: inline-flex; align-items: center; gap: 3px;
  padding: 2px 9px; border-radius: 100px;
  font-size: 11px; font-weight: 600; white-space: nowrap;
}
.badge.paid       { background: #f0fdf4; color: #16a34a; border: 1px solid #bbf7d0; }
.badge.sent       { background: #eff6ff; color: #2563eb; border: 1px solid #bfdbfe; }
.badge.draft      { background: #f8fafc; color: #64748b; border: 1px solid #e2e8f0; }
.badge.overdue    { background: #fef2f2; color: #dc2626; border: 1px solid #fecaca; }
.badge.cancelled  { background: #f8fafc; color: #94a3b8; border: 1px solid #e2e8f0; }
.badge.pending    { background: #fffbeb; color: #d97706; border: 1px solid #fde68a; }

/* ── Buttons ──────────────────────────────────────────────────────── */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  padding: 8px 16px; border-radius: 9px;
  font-size: 13px; font-weight: 600;
  cursor: pointer; border: none; transition: all .15s;
  text-decoration: none; white-space: nowrap;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}
.btn-primary { background: linear-gradient(135deg,#2563eb,#1d4ed8); color: white; }
.btn-primary:hover  { transform: translateY(-1px); box-shadow: 0 4px 12px rgba(37,99,235,.35); }
.btn-primary:active { transform: translateY(0); }
.btn-success { background: linear-gradient(135deg,#16a34a,#15803d); color: white; }
.btn-success:hover  { box-shadow: 0 4px 12px rgba(22,163,74,.35); }
.btn-danger  { background: #fef2f2; color: #dc2626; border: 1px solid #fecaca; }
.btn-danger:hover   { background: #dc2626; color: white; }
.btn-secondary { background: #f1f5f9; color: #475569; border: 1px solid #e2e8f0; }
.btn-secondary:hover{ background: #e2e8f0; }
.btn-outline { background: transparent; border: 1.5px solid #2563eb; color: #2563eb; }
.btn-outline:hover  { background: #2563eb; color: white; }
.btn-sm  { padding: 6px 12px; font-size: 12px; }
.btn-xs  { padding: 4px 8px;  font-size: 11px; }
.btn-icon{ width: 34px; height: 34px; padding: 0; border-radius: 8px; }

/* ── Forms ────────────────────────────────────────────────────────── */
.form-group { margin-bottom: 14px; }
.form-label { display: block; font-size: 12px; font-weight: 600; color: #374151; margin-bottom: 5px; }
.form-control {
  width: 100%; padding: 9px 12px;
  border: 1.5px solid #e2e8f0; border-radius: 9px;
  font-size: 13px; color: #1e293b;
  transition: border-color .15s; background: white;
  -webkit-appearance: none; appearance: none;
}
.form-control:focus { outline: none; border-color: #2563eb; box-shadow: 0 0 0 3px rgba(37,99,235,.08); }
textarea.form-control { resize: vertical; min-height: 80px; }
.form-select {
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' fill='none' viewBox='0 0 20 20'%3E%3Cpath stroke='%236b7280' stroke-linecap='round' stroke-linejoin='round' stroke-width='1.5' d='M6 8l4 4 4-4'/%3E%3C/svg%3E");
  background-repeat: no-repeat; background-position: right 10px center; background-size: 16px;
  padding-right: 34px; cursor: pointer;
}
.form-row { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }

/* ── Table ────────────────────────────────────────────────────────── */
.table-wrap { overflow-x: auto; -webkit-overflow-scrolling: touch; }
table { width: 100%; border-collapse: collapse; min-width: 560px; }
thead tr { border-bottom: 2px solid #f1f5f9; }
th { padding: 10px 14px; text-align: left; font-size: 11px; font-weight: 700; color: #64748b; text-transform: uppercase; letter-spacing: .04em; white-space: nowrap; }
td { padding: 12px 14px; font-size: 13px; color: #334155; border-bottom: 1px solid #f8fafc; }
tbody tr { cursor: pointer; transition: background .1s; }
tbody tr:hover { background: #f8fafc; }
tbody tr:last-child td { border-bottom: none; }

/* ── Invoice line-item row ────────────────────────────────────────── */
.item-row {
  display: grid;
  grid-template-columns: 1fr 70px 110px 64px 88px 34px;
  gap: 6px;
  align-items: start;
  margin-bottom: 8px;
  padding: 10px;
  background: #f8fafc;
  border-radius: 10px;
  border: 1px solid #e2e8f0;
}
.item-row-header {
  display: grid;
  grid-template-columns: 1fr 70px 110px 64px 88px 34px;
  gap: 6px;
  padding: 0 10px;
  margin-bottom: 6px;
}
.item-row-header span {
  font-size: 10px; font-weight: 700; color: #94a3b8; text-transform: uppercase;
}

/* ── Invoice preview ──────────────────────────────────────────────── */
.invoice-preview {
  background: white; max-width: 820px; margin: 0 auto;
  border-radius: 14px; overflow: hidden;
  box-shadow: 0 4px 20px rgba(0,0,0,.07);
}
.inv-head { padding: 32px; background: linear-gradient(135deg,#0f172a,#1e3a5f); color: white; }
.inv-body { padding: 32px; }
.inv-foot { padding: 20px 32px; background: #f8fafc; border-top: 2px solid #f1f5f9; }

/* ── Currency badge ───────────────────────────────────────────────── */
.currency-badge {
  display: inline-flex; align-items: center; gap: 4px;
  background: linear-gradient(135deg,#7c3aed,#2563eb);
  color: white; padding: 3px 10px; border-radius: 20px;
  font-size: 11px; font-weight: 700;
}

/* ── Invoice form two-column layout ──────────────────────────────── */
/*  Desktop: left col (main forms) + right col (currency card)        */
/*  Mobile:  single column, currency card appears between details      */
/*           and line items for easy access                            */
.inv-form-layout {
  display: grid;
  grid-template-columns: 1fr 340px;
  gap: 18px;
  align-items: start;
}
.inv-form-left {
  display: flex;
  flex-direction: column;
  gap: 18px;
  min-width: 0;
}
.inv-form-right {
  position: sticky;
  top: calc(var(--topbar-h) + 12px);
}
.inv-currency-card {
  border: 2px solid #ddd6fe;
  box-shadow: 0 4px 24px rgba(124,58,237,.10);
}
.inv-currency-card .card-header {
  background: linear-gradient(135deg,#eff6ff,#faf5ff);
  border-bottom: 1.5px solid #ddd6fe;
}

@media (max-width: 899px) {
  .inv-form-layout {
    grid-template-columns: 1fr;
  }
  .inv-form-left {
    order: 1;
  }
  .inv-form-right {
    position: static;  /* un-sticky on mobile */
    order: 2;          /* shows after left column cards */
  }
}

/* ── Exchange rate widget ─────────────────────────────────────────── */
.rate-box {
  background: linear-gradient(135deg,#eff6ff,#faf5ff);
  border: 1.5px solid #bfdbfe; border-radius: 12px; padding: 14px;
  margin-top: 12px;
}
.rate-box .rate-value {
  font-size: 20px; font-weight: 800;
  background: linear-gradient(135deg,#2563eb,#7c3aed);
  -webkit-background-clip: text; -webkit-text-fill-color: transparent;
  background-clip: text;
}

/* ── Page header ──────────────────────────────────────────────────── */
.page-header {
  display: flex; align-items: flex-start; justify-content: space-between;
  margin-bottom: 20px; gap: 12px; flex-wrap: wrap;
}
.page-title { font-size: 20px; font-weight: 800; color: #1e293b; }
.page-sub   { font-size: 12px; color: #64748b; margin-top: 2px; }

/* ── Filter bar ───────────────────────────────────────────────────── */
.filter-bar {
  display: flex; align-items: center; gap: 8px;
  flex-wrap: wrap; margin-bottom: 16px;
}
.search-wrap { position: relative; flex: 1; min-width: 160px; }
.search-wrap i { position: absolute; left: 10px; top: 50%; transform: translateY(-50%); color: #94a3b8; font-size: 12px; }
.search-wrap input { padding-left: 32px; }

/* ── Client grid ──────────────────────────────────────────────────── */
.client-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(260px,1fr)); gap: 14px; }
.client-card {
  padding: 16px; border-radius: 12px; border: 1.5px solid #e2e8f0;
  background: white; cursor: pointer; transition: all .15s;
}
.client-card:hover { border-color: #2563eb; box-shadow: 0 4px 12px rgba(37,99,235,.1); transform: translateY(-2px); }

/* ── Modal ────────────────────────────────────────────────────────── */
.modal-overlay {
  position: fixed; inset: 0; z-index: 1000;
  background: rgba(0,0,0,.55); backdrop-filter: blur(4px);
  display: flex; align-items: flex-end; justify-content: center;
  padding: 0;
  opacity: 0; pointer-events: none; transition: opacity .2s;
}
.modal-overlay.show { opacity: 1; pointer-events: all; }
.modal {
  background: white;
  border-radius: 20px 20px 0 0;
  box-shadow: 0 -8px 40px rgba(0,0,0,.2);
  width: 100%; max-height: 92vh; overflow-y: auto;
  transform: translateY(100%); transition: transform .3s cubic-bezier(.4,0,.2,1);
  -webkit-overflow-scrolling: touch;
}
.modal-overlay.show .modal { transform: translateY(0); }
.modal-handle { width: 40px; height: 4px; background: #e2e8f0; border-radius: 4px; margin: 12px auto 0; }
.modal-header { padding: 16px 20px 0; display: flex; align-items: center; justify-content: space-between; }
.modal-title  { font-size: 16px; font-weight: 700; }
.modal-body   { padding: 16px 20px; }
.modal-footer { padding: 0 20px 24px; display: flex; justify-content: flex-end; gap: 8px; }

/* ── Toast ────────────────────────────────────────────────────────── */
#toast-container {
  position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%);
  z-index: 9999; display: flex; flex-direction: column; gap: 8px;
  width: calc(100% - 32px); max-width: 420px;
}
.toast {
  display: flex; align-items: center; gap: 10px;
  padding: 12px 16px; border-radius: 12px;
  background: #1e293b; color: white;
  box-shadow: 0 6px 24px rgba(0,0,0,.25);
  font-size: 13px; font-weight: 500;
  animation: toastIn .3s ease;
}
.toast.success { background: #16a34a; }
.toast.error   { background: #dc2626; }
.toast.info    { background: #0891b2; }
@keyframes toastIn {
  from { opacity:0; transform: translateY(20px); }
  to   { opacity:1; transform: translateY(0); }
}

/* ── Avatar ───────────────────────────────────────────────────────── */
.avatar {
  width: 32px; height: 32px; border-radius: 50%;
  background: linear-gradient(135deg,#2563eb,#7c3aed);
  display: flex; align-items: center; justify-content: center;
  font-size: 12px; font-weight: 700; color: white; flex-shrink: 0;
}

/* ── Misc ─────────────────────────────────────────────────────────── */
.divider { border: none; border-top: 1px solid #f1f5f9; margin: 16px 0; }
.text-money { font-family: 'SF Mono', 'Courier New', monospace; font-weight: 700; }
.empty-state { text-align: center; padding: 48px 20px; color: #94a3b8; }
.empty-state i { font-size: 40px; margin-bottom: 12px; display: block; }
.loading { display: flex; align-items: center; justify-content: center; padding: 48px; }
.spinner { width: 28px; height: 28px; border: 3px solid #e2e8f0; border-top-color: #2563eb; border-radius: 50%; animation: spin 1s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

/* ── Drive status indicator ───────────────────────────────────────── */
.drive-status {
  display: inline-flex; align-items: center; gap: 5px;
  font-size: 11px; padding: 3px 9px; border-radius: 20px;
  font-weight: 600;
}
.drive-status.synced   { background: #f0fdf4; color: #16a34a; }
.drive-status.unsynced { background: #fef2f2; color: #dc2626; }
.drive-status.loading  { background: #eff6ff; color: #2563eb; }

/* ══════════════════════════════════════════════════════════════════
   RESPONSIVE BREAKPOINTS
   ══════════════════════════════════════════════════════════════════ */

/* Tablet – 768px */
@media (min-width: 768px) {
  .stat-grid { grid-template-columns: repeat(3,1fr); }
  .modal-overlay { align-items: center; padding: 24px; }
  .modal { border-radius: 20px; max-width: 600px; transform: scale(.94); }
  .modal-overlay.show .modal { transform: scale(1); }
  .modal-handle { display: none; }
  #toast-container { left: auto; right: 20px; transform: none; bottom: 20px; }
}

/* Desktop – 1024px */
@media (min-width: 1024px) {
  .stat-grid { grid-template-columns: repeat(5,1fr); }
  #content { padding: 24px 28px; }
}

/* Mobile only (≤ 767px) */
@media (max-width: 767px) {
  :root { --sidebar-w: 260px; }

  #sidebar {
    transform: translateX(-100%);
    box-shadow: none;
  }
  #sidebar.open {
    transform: translateX(0);
    box-shadow: 4px 0 24px rgba(0,0,0,.3);
  }
  #main { margin-left: 0; }

  #topbar { padding: 0 14px; }
  #content { padding: 14px; }

  .page-header { flex-direction: column; align-items: stretch; }
  .page-header > .btn, .page-header > button { width: 100%; justify-content: center; }

  .filter-bar { flex-direction: column; align-items: stretch; }
  .filter-bar > * { width: 100%; }

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

  /* Stack invoice items vertically on mobile */
  .item-row {
    grid-template-columns: 1fr 1fr;
    grid-template-rows: auto auto auto;
  }
  .item-row > *:first-child { grid-column: 1 / -1; }
  .item-row .item-total { grid-column: 1; align-self: center; }
  .item-row > .btn-danger { grid-column: 2; justify-self: end; }
  .item-row-header { display: none; }

  /* Invoice preview */
  .inv-head, .inv-body { padding: 20px; }
  .inv-foot { padding: 16px 20px; }

  /* Charts stacked on mobile */
  .chart-grid { grid-template-columns: 1fr !important; }

  /* Client grid single col */
  .client-grid { grid-template-columns: 1fr; }

  /* Table – show fewer cols via card-style on mobile */
  .table-mobile-card thead { display: none; }
  .table-mobile-card tbody tr {
    display: flex; flex-direction: column;
    padding: 14px 16px; border-bottom: 1px solid #f1f5f9;
    cursor: pointer;
  }
  .table-mobile-card tbody td {
    display: flex; justify-content: space-between; align-items: center;
    padding: 4px 0; border: none; font-size: 13px;
  }
  .table-mobile-card tbody td::before {
    content: attr(data-label);
    font-size: 10px; font-weight: 700; color: #94a3b8;
    text-transform: uppercase; letter-spacing: .04em;
    flex-shrink: 0; margin-right: 8px;
  }
  .table-mobile-card tbody td:first-child { font-weight: 700; font-size: 14px; }
  .table-mobile-card tbody td:first-child::before { display: none; }

  /* Page title smaller */
  .page-title { font-size: 17px; }

  /* Stat value smaller */
  .stat-value { font-size: 15px; }
  .stat-icon { width: 38px; height: 38px; font-size: 15px; border-radius: 10px; }
}

/* Very small phones (≤ 380px) */
@media (max-width: 380px) {
  .stat-grid { grid-template-columns: 1fr; }
  .stat-card { flex-direction: row; }
}

/* ── Print ────────────────────────────────────────────────────────── */
@media print {
  #sidebar, #topbar, .no-print, #toast-container, #modal-container { display: none !important; }
  #main { margin-left: 0 !important; }
  #content { padding: 0 !important; }
  body { background: white !important; }
  .invoice-preview, .card { box-shadow: none !important; border: none !important; }
}
