@import url('https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&display=swap');

:root {
  --bg-primary: #F8FAFC;
  --primary: #1E3A8A;
  --primary-light: #2563EB;
  --secondary: #3B82F6;
  --text-primary: #1E293B;
  --text-secondary: #64748B;
  --surface: #FFFFFF;
  --border: #CBD5E1;
  --border-light: #E2E8F0;
  --success: #10B981;
  --error: #EF4444;
  --warning: #F59E0B;
  --shadow-sm: 0 1px 3px rgba(0,0,0,0.1), 0 1px 2px rgba(0,0,0,0.06);
  --shadow-md: 0 4px 6px -1px rgba(0,0,0,0.1), 0 2px 4px -1px rgba(0,0,0,0.06);
  --radius: 12px;
  --radius-sm: 8px;
  --transition: 200ms ease-in-out;
  --sidebar-width: 240px;
}

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

body {
  font-family: 'Inter', -apple-system, BlinkMacSystemFont, sans-serif;
  background: var(--bg-primary);
  color: var(--text-primary);
  font-size: 16px;
  line-height: 1.5;
  min-height: 100vh;
}

.app-layout { display: none; min-height: 100vh; }
.app-layout.active { display: flex; }

/* Sidebar (desktop) */
.sidebar {
  display: none;
  width: var(--sidebar-width);
  background: var(--surface);
  border-right: 1px solid var(--border-light);
  flex-direction: column;
  position: fixed;
  top: 0; left: 0; bottom: 0;
  z-index: 200;
}

.sidebar-header {
  padding: 24px 20px;
  border-bottom: 1px solid var(--border-light);
}

.sidebar-logo {
  display: flex;
  align-items: center;
  gap: 10px;
  text-decoration: none;
}

.sidebar-logo img { width: 32px; height: 32px; }
.sidebar-logo span { font-size: 20px; font-weight: 700; color: var(--primary); }

.sidebar-nav { flex: 1; padding: 12px 0; }
.sidebar-item {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 20px;
  color: var(--text-secondary);
  cursor: pointer;
  transition: all var(--transition);
  font-size: 14px;
  font-weight: 500;
  text-decoration: none;
}

.sidebar-item:hover { background: var(--bg-primary); color: var(--text-primary); }
.sidebar-item.active { background: #EEF2FF; color: var(--primary); font-weight: 600; }
.sidebar-item svg { width: 20px; height: 20px; flex-shrink: 0; }

.sidebar-footer {
  padding: 16px 20px;
  border-top: 1px solid var(--border-light);
}

.main-content {
  flex: 1;
  padding: 24px 16px 90px;
  max-width: 100%;
}

h1 { font-size: 26px; font-weight: 700; color: var(--text-primary); margin-bottom: 8px; }
h2 { font-size: 22px; font-weight: 600; margin-bottom: 12px; }
h3 { font-size: 18px; font-weight: 600; color: var(--text-primary); margin-bottom: 12px; }

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 12px 20px;
  font-size: 14px;
  font-weight: 600;
  border: none;
  border-radius: var(--radius-sm);
  cursor: pointer;
  transition: all var(--transition);
  text-decoration: none;
}

.btn-primary { background: var(--primary); color: white; }
.btn-primary:hover { background: var(--primary-light); }
.btn-secondary { background: var(--secondary); color: white; }
.btn-outline { background: transparent; border: 1px solid var(--border); color: var(--text-primary); }
.btn-outline:hover { border-color: var(--primary); color: var(--primary); }
.btn-danger { background: var(--error); color: white; }
.btn-danger:hover { background: #DC2626; }
.btn-ghost { background: transparent; color: var(--text-secondary); border: none; }
.btn-ghost:hover { background: var(--bg-primary); color: var(--text-primary); }
.btn-sm { padding: 8px 12px; font-size: 13px; }
.btn-full { width: 100%; }
.btn:disabled { opacity: 0.5; cursor: not-allowed; }

.card {
  background: var(--surface);
  border-radius: var(--radius);
  box-shadow: var(--shadow-sm);
  padding: 16px;
  margin-bottom: 16px;
}

.input-group { margin-bottom: 16px; }
.input-label { display: block; font-size: 14px; font-weight: 500; color: var(--text-primary); margin-bottom: 6px; }
.input-field {
  width: 100%;
  padding: 12px 14px;
  font-size: 16px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--surface);
  transition: border-color var(--transition);
  font-family: inherit;
}
.input-field:focus { outline: none; border-color: var(--secondary); }
.input-field.error { border-color: var(--error); }
.error-text { color: var(--error); font-size: 12px; margin-top: 4px; }

select.input-field {
  appearance: none;
  -webkit-appearance: none;
  -moz-appearance: none;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='24' height='24' viewBox='0 0 24 24' fill='none' stroke='%2364748B' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpolyline points='6 9 12 15 18 9'%3E%3C/polyline%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 12px center;
  background-size: 18px;
  padding-right: 40px;
  cursor: pointer;
}

.page-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 24px;
  gap: 12px;
  position: sticky;
  top: 0;
  z-index: 50;
  background: var(--bg-primary);
  padding: 12px 0;
}

.header-left { display: flex; flex-direction: column; }

.back-link {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  color: var(--primary);
  text-decoration: none;
  font-weight: 600;
  font-size: 15px;
  cursor: pointer;
  margin-bottom: 4px;
}

.back-link svg { width: 20px; height: 20px; margin-left: -4px; }

/* Modal */
.modal {
  position: fixed;
  top: 0; left: 0; right: 0; bottom: 0;
  background: rgba(0,0,0,0.6);
  backdrop-filter: blur(4px);
  -webkit-backdrop-filter: blur(4px);
  display: none;
  align-items: center;
  justify-content: center;
  z-index: 2000;
  padding: 20px;
}

.modal.active { display: flex; }

.modal-content {
  background: white;
  border-radius: 24px;
  padding: 24px;
  width: 100%;
  max-width: 520px;
  max-height: 90vh;
  overflow-y: auto;
  box-shadow: 0 20px 25px -5px rgba(0, 0, 0, 0.1);
  animation: modalSlideUp 0.3s ease-out;
}

.modal-wide { max-width: 640px; }

@keyframes modalSlideUp {
  from { transform: translateY(20px); opacity: 0; }
  to { transform: translateY(0); opacity: 1; }
}

.modal-header { display: flex; justify-content: space-between; align-items: center; margin-bottom: 20px; }
.modal-title { font-size: 20px; font-weight: 700; }
.modal-close { background: var(--bg-primary); border: none; cursor: pointer; color: var(--text-secondary); width: 32px; height: 32px; border-radius: 50%; display: flex; align-items: center; justify-content: center; font-size: 18px; }
.modal-close:hover { background: var(--border-light); }
.modal-footer { display: flex; justify-content: flex-end; gap: 12px; margin-top: 24px; }

.ai-textarea { min-height: 180px !important; resize: none; font-size: 15px; }

/* List Items */
.list-item {
  background: var(--surface);
  border-radius: var(--radius);
  padding: 16px;
  margin-bottom: 12px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  box-shadow: var(--shadow-sm);
  border: 1px solid var(--border-light);
}

.list-item-content { flex: 1; min-width: 0; }
.list-item-title { font-weight: 600; font-size: 16px; color: var(--text-primary); margin-bottom: 2px; }
.list-item-subtitle { font-size: 14px; color: var(--text-secondary); }
.list-item-actions { display: flex; gap: 8px; flex-shrink: 0; }

.btn-icon {
  padding: 8px;
  min-width: 36px;
  height: 36px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
}

/* Document Cards */
.document-card {
  display: block;
  padding: 16px;
  background: white;
  border-radius: var(--radius);
  box-shadow: var(--shadow-sm);
  margin-bottom: 12px;
  text-decoration: none;
  color: inherit;
  border: 1px solid var(--border-light);
  cursor: pointer;
  transition: all var(--transition);
}

.document-card:hover { box-shadow: var(--shadow-md); border-color: var(--secondary); }

.document-card-header { display: flex; justify-content: space-between; align-items: center; margin-bottom: 8px; }
.document-number { font-size: 12px; font-weight: 600; color: var(--text-secondary); }
.document-client { font-weight: 600; font-size: 16px; margin-bottom: 4px; }
.document-total { font-weight: 700; color: var(--primary); font-size: 18px; }
.document-date { font-size: 13px; color: var(--text-secondary); }

.badge { display: inline-flex; padding: 4px 10px; font-size: 10px; font-weight: 700; border-radius: 20px; text-transform: uppercase; }
.badge-budget { background: #E0F2FE; color: #0369A1; }
.badge-receipt { background: #DCFCE7; color: #15803D; }
.badge-certificate { background: #EEF2FF; color: #4338CA; }

/* Dashboard */
.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.dashboard-action-btn { height: 110px; flex-direction: column; gap: 10px; border-radius: 16px !important; box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1); }
.section-title { display: flex; justify-content: space-between; align-items: center; margin: 24px 0 16px; }

.empty-state { text-align: center; padding: 40px 20px; color: var(--text-secondary); }
.empty-state svg { width: 48px; height: 48px; margin-bottom: 16px; opacity: 0.5; }

/* Nav Bar (mobile) */
.nav-bar {
  position: fixed;
  bottom: 0; left: 0; right: 0;
  background: white;
  box-shadow: 0 -2px 10px rgba(0,0,0,0.05);
  z-index: 100;
  padding-bottom: 4px;
}

.nav-items {
  display: flex;
  justify-content: space-around;
  padding: 6px 0 2px;
}

.nav-credit {
  text-align: center;
  font-size: 9px;
  color: var(--text-secondary);
  padding: 4px 0 6px;
  line-height: 1;
}

.nav-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  font-size: 10px;
  color: var(--text-secondary);
  text-decoration: none;
  gap: 2px;
  cursor: pointer;
  border: none;
  background: none;
}

.nav-item.active { color: var(--primary); }
.nav-item svg { width: 20px; height: 20px; }

/* Utility classes */
.hidden { display: none !important; }
.mb-4 { margin-bottom: 16px; }
.mb-2 { margin-bottom: 8px; }
.mt-8 { margin-top: 32px; }
.mt-4 { margin-top: 16px; }
.mt-2 { margin-top: 8px; }
.flex { display: flex; }
.justify-between { justify-content: space-between; }
.justify-center { justify-content: center; }
.items-center { align-items: center; }
.items-start { align-items: flex-start; }
.gap-2 { gap: 8px; }
.gap-4 { gap: 16px; }
.text-center { text-align: center; }
.text-right { text-align: right; }
.flex-1 { flex: 1; }
.w-full { width: 100%; }

.summary-row { display: flex; justify-content: space-between; padding: 10px 0; border-bottom: 1px solid var(--border-light); }
.summary-row.total { font-weight: 700; font-size: 18px; color: var(--primary); border-bottom: 2px solid var(--primary); }

#main-view { animation: fadeIn 0.3s ease-out; }
@keyframes fadeIn { from { opacity: 0; transform: translateY(5px); } to { opacity: 1; } }

.spinner { width: 24px; height: 24px; border: 3px solid var(--border); border-top-color: var(--primary); border-radius: 50%; animation: spin 0.8s linear infinite; margin: 0 auto; }
.spinner-sm { width: 16px; height: 16px; border: 2px solid var(--border); border-top-color: var(--primary); border-radius: 50%; display: inline-block; animation: spin 1s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

/* Loading overlay */
#loading-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(255,255,255,0.8);
  z-index: 3000;
  align-items: center;
  justify-content: center;
}

#loading-overlay.active { display: flex; }
#loading-overlay .spinner { width: 40px; height: 40px; border-width: 4px; }

/* Toast notifications */
#toast-container {
  position: fixed;
  top: 20px;
  right: 20px;
  z-index: 5000;
  display: flex;
  flex-direction: column;
  gap: 8px;
  max-width: 380px;
}

.toast {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 14px 18px;
  border-radius: var(--radius-sm);
  background: var(--surface);
  box-shadow: 0 4px 12px rgba(0,0,0,0.15);
  animation: toastIn 0.3s ease-out;
  font-size: 14px;
  font-weight: 500;
  border-left: 4px solid;
}

.toast-success { border-color: var(--success); }
.toast-error { border-color: var(--error); }
.toast-info { border-color: var(--secondary); }

@keyframes toastIn {
  from { transform: translateX(100%); opacity: 0; }
  to { transform: translateX(0); opacity: 1; }
}

.toast-icon { font-size: 18px; flex-shrink: 0; }
.toast-close { margin-left: auto; background: none; border: none; cursor: pointer; color: var(--text-secondary); padding: 4px; }

/* Auth Screen */
#auth-screen { display: flex; flex-direction: column; align-items: center; justify-content: center; min-height: 100vh; padding: 20px; }

.auth-card {
  background: var(--surface);
  border-radius: 24px;
  padding: 40px 32px;
  width: 100%;
  max-width: 420px;
  box-shadow: var(--shadow-md);
}

.auth-logo { text-align: center; margin-bottom: 16px; }
.auth-logo img { width: 64px; height: 64px; }
.auth-title { text-align: center; font-size: 28px; margin-bottom: 4px; }
.auth-subtitle { text-align: center; color: var(--text-secondary); margin-bottom: 24px; font-size: 14px; }

.auth-tabs { display: flex; width: 100%; margin-bottom: 20px; gap: 0; }
.auth-tab { flex: 1; text-align: center; padding: 10px; cursor: pointer; border-bottom: 2px solid transparent; font-weight: 600; color: var(--text-secondary); }
.auth-tab.active { border-bottom-color: var(--primary); color: var(--primary); }
.auth-form { display: none; width: 100%; }
.auth-form.active { display: block; }

.header-bar { display: flex; justify-content: space-between; align-items: center; margin-bottom: 20px; }

/* Toggle switch */
.toggle { position: relative; display: inline-flex; align-items: center; cursor: pointer; gap: 10px; }
.toggle-input { position: absolute; opacity: 0; width: 0; height: 0; }
.toggle-slider {
  width: 44px; height: 24px; background: var(--border); border-radius: 24px;
  position: relative; transition: 0.3s; flex-shrink: 0;
}
.toggle-slider:before {
  content: ""; position: absolute; height: 18px; width: 18px;
  left: 3px; bottom: 3px; background: white; border-radius: 50%; transition: 0.3s;
}
.toggle-input:checked + .toggle-slider { background: var(--primary); }
.toggle-input:checked + .toggle-slider:before { transform: translateX(20px); }

/* Document detail view */
.doc-detail-header { padding-bottom: 16px; border-bottom: 2px solid var(--border-light); margin-bottom: 20px; }
.doc-detail-number { font-size: 14px; color: var(--text-secondary); margin-bottom: 4px; }
.doc-detail-title { font-size: 24px; font-weight: 700; }
.doc-detail-date { font-size: 13px; color: var(--text-secondary); }

.doc-info-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; margin-bottom: 20px; }
.doc-info-item { }
.doc-info-label { font-size: 11px; font-weight: 600; text-transform: uppercase; color: var(--text-secondary); margin-bottom: 2px; }
.doc-info-value { font-size: 15px; font-weight: 600; color: var(--text-primary); }

.doc-items-table { width: 100%; border-collapse: collapse; margin-bottom: 20px; min-width: 360px; }
.doc-items-table th {
  background: var(--primary); color: white; padding: 10px 10px;
  font-size: 11px; font-weight: 600; text-transform: uppercase; text-align: left;
}
.doc-items-table td { padding: 10px 10px; border-bottom: 1px solid var(--border-light); font-size: 13px; }
.doc-items-table tr:nth-child(even) td { background: #F8FAFC; }

@media (max-width: 480px) {
  .doc-items-table { min-width: 300px; }
  .doc-items-table th, .doc-items-table td { padding: 8px 6px; font-size: 12px; }
}

.doc-totals { margin-left: auto; width: 280px; }
.doc-total-row { display: flex; justify-content: space-between; padding: 8px 0; font-size: 14px; }
.doc-total-row.final { font-weight: 700; font-size: 18px; color: var(--primary); border-top: 2px solid var(--primary); padding-top: 12px; margin-top: 4px; }

/* Item editor rows */
.item-row {
  background: var(--bg-primary);
  border-radius: var(--radius-sm);
  padding: 12px;
  margin-bottom: 8px;
  border: 1px solid var(--border-light);
}

.item-row-grid {
  display: grid;
  grid-template-columns: 1fr 80px 100px 36px;
  gap: 8px;
  align-items: start;
}

.item-row-desc { margin-top: 8px; }

/* Responsive: Desktop */
@media (min-width: 768px) {
  .sidebar { display: flex; }
  .main-content { margin-left: var(--sidebar-width); padding: 32px; padding-bottom: 32px; max-width: calc(100% - var(--sidebar-width)); }
  .nav-bar { display: none; }
  .auth-card { padding: 48px; }
}

/* Responsive: wide desktop */
@media (min-width: 1200px) {
  .main-content { max-width: 960px; }
}

/* Image upload preview */
.img-preview { width: 80px; height: 80px; border-radius: 16px; overflow: hidden; background: var(--bg-primary); display: flex; align-items: center; justify-content: center; border: 1px solid var(--border-light); flex-shrink: 0; }
.img-preview img { width: 100%; height: 100%; object-fit: contain; }
