*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
:root {
  /* Soft Lilás — nova paleta */
  --primary: #9333EA;
  --primary-hover: #7C3AED;
  --primary-light: #C084FC;
  --primary-bg: #F3E8FF;

  /* Legacy lila vars mapeadas para nova paleta */
  --lila-50: #F3E8FF;
  --lila-100: #E9D5FF;
  --lila-200: #C084FC;
  --lila-400: #9333EA;
  --lila-600: #7C3AED;
  --lila-800: #6B21A8;

  --bg: #FAF7FF;
  --surface: #ffffff;
  --border: #EDE9FE;
  --text: #1a1a2e;
  --muted: #6b6880;
  --hint: #a09cb8;

  --green: #3B6D11; --green-bg: #EAF3DE;
  --red: #A32D2D; --red-bg: #FCEBEB;
  --amber: #854F0B; --amber-bg: #FAEEDA;
}

body { font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif; background: var(--bg); color: var(--text); }
.app { display: flex; height: 100vh; }

/* Sidebar */
.sidebar { width: 210px; min-width: 210px; background: var(--surface); border-right: 1px solid var(--border); box-shadow: 2px 0 12px rgba(147,51,234,0.06); display: flex; flex-direction: column; }
.logo { padding: 0; display: flex; align-items: center; justify-content: center; background: var(--surface); }
.logo img { width: 100%; height: auto; object-fit: contain; display: block; }
nav { padding: 0.5rem 0; flex: 1; }
.nav-item { display: flex; align-items: center; gap: 10px; padding: 10px 1.25rem; cursor: pointer; font-size: 13px; color: var(--muted); transition: all 0.15s; user-select: none; border-radius: 0; }
.nav-item:hover { background: #F9F4FF; color: var(--text); }
.nav-item.active { color: var(--primary-hover); background: rgba(147,51,234,0.06); font-weight: 600; }
.nav-item.active .nav-badge { box-shadow: 0 2px 6px rgba(0,0,0,0.15); }
.nav-icon { width: 18px; height: 18px; min-width: 18px; }

.nav-badge { width: 30px; height: 30px; min-width: 30px; border-radius: 9px; display: flex; align-items: center; justify-content: center; transition: box-shadow 0.15s; }
.nav-badge-vendas { background: rgba(147,51,234,0.14); color: #9333EA; }
.nav-badge-estoque { background: rgba(184,134,43,0.14); color: #B8862B; }
.nav-badge-clientes { background: rgba(194,91,135,0.14); color: #C25B87; }
.nav-badge-fornecedores { background: rgba(58,130,150,0.14); color: #3A8296; }
.nav-badge-financeiro { background: rgba(61,90,153,0.14); color: #3D5A99; }
.nav-badge-despesas { background: rgba(189,98,64,0.14); color: #BD6240; }

/* Main */
.main { flex: 1; display: flex; flex-direction: column; overflow: hidden; }
.topbar { background: var(--surface); border-bottom: 1px solid var(--border); padding: 1rem 1.5rem; display: flex; align-items: center; justify-content: space-between; position: relative; }
.page-title { font-size: 15px; font-weight: 600; position: absolute; left: 50%; top: 50%; transform: translate(-50%, -50%); }
.content { flex: 1; overflow-y: auto; padding: 1.5rem; }

/* Cards & Grids */
.metric-grid { display: grid; grid-template-columns: repeat(4, minmax(0,1fr)); gap: 12px; margin-bottom: 1.25rem; }
.metric-card { background: var(--surface); border: 1px solid var(--border); border-radius: 14px; padding: 1rem; box-shadow: 0 2px 8px rgba(147,51,234,0.07); }
.metric-label { font-size: 11px; color: var(--muted); margin-bottom: 6px; text-transform: uppercase; letter-spacing: 0.05em; }
.metric-value { font-size: 22px; font-weight: 600; }
.metric-sub { font-size: 11px; color: var(--hint); margin-top: 4px; }
.card { background: var(--surface); border: 1px solid var(--border); border-radius: 14px; box-shadow: 0 2px 8px rgba(147,51,234,0.07); padding: 1.25rem; margin-bottom: 1rem; }
.card-header { display: flex; align-items: center; justify-content: space-between; margin-bottom: 1rem; }
.card-title { font-size: 13px; font-weight: 600; }
.two-col { display: grid; grid-template-columns: repeat(2, minmax(0,1fr)); gap: 1rem; }

/* Table */
table { width: 100%; border-collapse: collapse; font-size: 13px; }
th { text-align: left; padding: 8px 10px; color: var(--muted); font-weight: 600; font-size: 11px; border-bottom: 1px solid var(--border); text-transform: uppercase; letter-spacing: 0.04em; }
td, th { vertical-align: middle; }
th[onclick] { cursor: pointer; user-select: none; }
td { padding: 9px 10px; border-bottom: 1px solid var(--border); }
tr:last-child td { border-bottom: none; }
tr:hover td { background: var(--primary-bg); }

/* Badges */
.badge { font-size: 11px; padding: 3px 8px; border-radius: 20px; font-weight: 600; display: inline-block; }
.badge-lila { background: var(--lila-50); color: var(--lila-800); }
.badge-red { background: var(--red-bg); color: var(--red); }
.badge-green { background: var(--green-bg); color: var(--green); }
.badge-amber { background: var(--amber-bg); color: var(--amber); }

/* Colors */
.c-green { color: var(--green); font-weight: 600; }
.c-red { color: var(--red); font-weight: 600; }
.c-lila { color: var(--lila-600); font-weight: 600; }
.c-amber { color: var(--amber); }
.c-muted { color: var(--muted); }

/* Forms */
.form-row { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; margin-bottom: 12px; align-items: start; }
.form-group { display: flex; flex-direction: column; gap: 4px; }
.form-group label {
  font-size: 11px; color: var(--muted); font-weight: 600;
  letter-spacing: 0.03em; line-height: 1.4; min-height: 16px;
  display: flex; align-items: center;
}
.form-group input, .form-group select {
  font-size: 13px; padding: 8px 10px; border: 1px solid var(--border);
  border-radius: 8px; background: var(--surface); color: var(--text);
  outline: none; transition: border 0.15s; height: 36px; width: 100%;
}
.form-group textarea {
  font-size: 13px; padding: 8px 10px; border: 1px solid var(--border);
  border-radius: 8px; background: var(--surface); color: var(--text);
  outline: none; transition: border 0.15s; width: 100%; resize: vertical;
}
.form-group input:focus, .form-group select:focus, .form-group textarea:focus { border-color: var(--primary-light); }
.form-label-hidden { visibility: hidden; }
input::placeholder, textarea::placeholder { font-size: 12px; color: var(--hint); }
.cart-input { font-size: 13px; padding: 8px 10px; border: 1px solid var(--border); border-radius: 8px; background: var(--surface); color: var(--text); outline: none; transition: border 0.15s; width: 100%; height: 36px; }
.cart-input:focus { border-color: var(--primary-light); }
.cart-cell { font-size: 13px; padding: 8px 10px; border: 1px solid var(--border); border-radius: 8px; background: var(--bg); color: var(--text); text-align: right; height: 36px; display: flex; align-items: center; justify-content: flex-end; }
.btn { padding: 8px 16px; font-size: 13px; border-radius: 8px; cursor: pointer; font-weight: 600; border: none; transition: all 0.15s; }
.btn-primary { background: var(--primary); color: white; }
.btn-primary:hover { background: var(--primary-hover); }
.btn-danger { background: var(--red-bg); color: var(--red); }
.btn-danger:hover { background: #f7c1c1; }
.btn-sm { padding: 4px 10px; font-size: 11px; }

/* Custom toggle switch (despesas) */
.toggle-wrap {
  display: flex;
  align-items: center;
  gap: 10px;
  font-size: 13px;
  color: var(--muted);
}
.toggle {
  position: relative;
  width: 40px;
  height: 22px;
  flex-shrink: 0;
}
.toggle input {
  opacity: 0;
  width: 0;
  height: 0;
}
.toggle-slider {
  position: absolute;
  inset: 0;
  background: var(--border);
  border-radius: 22px;
  transition: 0.2s;
  cursor: pointer;
}
.toggle-slider:before {
  content: '';
  position: absolute;
  width: 16px;
  height: 16px;
  left: 3px;
  top: 3px;
  background: white;
  border-radius: 50%;
  transition: 0.2s;
}
.toggle input:checked + .toggle-slider {
  background: var(--lila-600);
}
.toggle input:checked + .toggle-slider:before {
  transform: translateX(18px);
}

/* Chart */
.chart-wrap { position: relative; width: 100%; height: 220px; }

/* List items */
.list-item { display: flex; align-items: center; justify-content: space-between; padding: 10px 0; border-bottom: 1px solid var(--border); }
.list-item:last-child { border-bottom: none; }
.list-name { font-size: 13px; font-weight: 500; }
.list-sub { font-size: 11px; color: var(--hint); margin-top: 2px; }

/* Summary rows */
.sum-row { display: flex; justify-content: space-between; padding: 8px 0; border-bottom: 1px solid var(--border); font-size: 13px; }
.sum-row:last-child { border-bottom: none; }
.sum-row.total { font-weight: 600; border-top: 1px solid var(--border); padding-top: 12px; margin-top: 4px; }

/* Loader */
.loader { text-align: center; padding: 3rem; color: var(--hint); font-size: 13px; }
.alert-overlay { display:none; position:fixed; inset:0; background:rgba(0,0,0,0.45); z-index:10001; align-items:center; justify-content:center; }
.alert-overlay.show { display:flex; }
.alert-box { background:var(--surface); border-radius:14px; padding:2rem 2rem 1.5rem; width:340px; max-width:90vw; text-align:center; box-shadow:0 8px 32px rgba(0,0,0,0.18); animation: alertPop 0.18s ease; }
.alert-box .alert-icon { font-size:32px; margin-bottom:10px; }
.alert-box .alert-msg { font-size:14px; color:var(--text); line-height:1.5; margin-bottom:1.5rem; }
.alert-box .btn-ok { min-width:100px; }
@keyframes alertPop { from { transform:scale(0.88); opacity:0; } to { transform:scale(1); opacity:1; } }

/* Hover feedback global */
button, .btn {
  cursor: pointer;
  transition: all 0.15s ease;
}
.btn-sm:hover {
  filter: brightness(0.92);
  transform: translateY(-1px);
}
button:not(.btn):hover {
  opacity: 0.75;
}

/* Flatpickr theme integration */
.flatpickr-calendar {
  border: 1px solid var(--border);
  border-radius: 12px;
  box-shadow: 0 8px 24px rgba(147,51,234,0.12);
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
}
.flatpickr-day.selected,
.flatpickr-day.selected:hover {
  background: var(--primary);
  border-color: var(--primary);
}
.flatpickr-day:hover {
  background: var(--lila-50);
}

/* Confirm modal */
#confirm-overlay .alert-box {
  min-width: 320px;
}

/* Hamburger button */
.hamburger {
  display: none;
  flex-direction: column;
  justify-content: center;
  gap: 5px;
  padding: 6px;
  background: none;
  border: none;
  cursor: pointer;
  border-radius: 6px;
}
.hamburger span {
  display: block;
  width: 20px;
  height: 2px;
  background: var(--text);
  border-radius: 2px;
  transition: all 0.2s;
}

/* Sidebar overlay (mobile) */
.sidebar-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.4);
  z-index: 299;
}
.sidebar-overlay.show { display: block; }

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

  /* Topbar: padding menor + oculta nome do usuário (mantém botão Sair) */
  .topbar { padding: 0.75rem 1rem; }
  #topbar-user { display: none; }

  /* Sidebar: off-canvas drawer */
  .sidebar {
    position: fixed;
    top: 0; left: 0; bottom: 0;
    z-index: 300;
    transform: translateX(-100%);
    transition: transform 0.25s ease;
    width: 220px;
  }
  .sidebar.open { transform: translateX(0); }

  /* Layout reset */
  .app { flex-direction: column; }
  .main { height: 100vh; overflow: hidden; }
  .content { padding: 1rem; overflow-y: auto; }

  /* Single-column forms */
  .form-row { grid-template-columns: 1fr !important; }

  /* Botão alinhado à direita apenas no mobile */
  .btn-align-right-mobile { display: flex; justify-content: flex-end; }
  .two-col  { grid-template-columns: 1fr !important; }

  /* Labels sempre acima dos campos */
  .form-group { display: flex; flex-direction: column; gap: 4px; }

  /* Espaçamento vertical entre campos */
  .form-group + .form-group { margin-top: 12px; }

  /* 2-column metric grid max */
  .metric-grid { grid-template-columns: repeat(2, minmax(0,1fr)) !important; }

  /* Vendas and Financeiro layout grids */
  .vendas-top-grid { grid-template-columns: 1fr !important; }
  .fin-analysis-grid { grid-template-columns: 1fr !important; }

  /* Tables: horizontal scroll */
  .card { overflow-x: auto !important; -webkit-overflow-scrolling: touch; }
  table { min-width: 560px; }

  /* Cart items scroll (Vendas) */
  #carrinho-wrap { overflow-x: auto; -webkit-overflow-scrolling: touch; }
  #carrinho-wrap > div {
    /* Grupo 1 visível (sem scroll): Cód(62) Produto(1fr) Qtd(50)
       Grupo 2 via scroll: VUnit(60) Desc%(70) DescR$(75) Subtotal(80) Estoque(65) Remover(30) */
    min-width: 800px;
    grid-template-columns: 62px 1fr 50px 60px 70px 75px 80px 65px 30px !important;
  }
  /* Inputs occupy full cell width; product search fills its relative wrapper with safe max-width */
  #carrinho-wrap .cart-input { width: 100%; }
  #carrinho-wrap div[style*="position:relative"] {
    width: 100%;
    max-width: calc(100% - 62px - 50px - 20px);
  }
  /* Product search dropdown: fixed positioning (JS sets top/left/width), internal scroll */
  #carrinho-wrap div[id^="carr-drop-"] {
    position: fixed !important;
    z-index: 9999 !important;
    max-height: 300px !important;
    overflow-y: auto !important;
    -webkit-overflow-scrolling: touch !important;
    scrollbar-width: none;
  }
  #carrinho-wrap div[id^="carr-drop-"]::-webkit-scrollbar { display: none; }
  /* Client code field: match product code column width */
  #v-cliente-cod { width: 62px !important; }

  /* Client search row (Vendas) — empilhar verticalmente */
  #v-cliente-row { flex-direction: column !important; gap: 8px !important; }
  #v-cliente-row .form-group { width: 100% !important; flex: unset !important; }

  /* Touch-friendly inputs — 16px evita zoom no iOS */
  .form-group input,
  .form-group select,
  .form-group textarea { font-size: 16px !important; height: 44px !important; min-height: 44px; }
  .cart-input { font-size: 16px !important; height: 44px !important; min-height: 44px; }
  input, select, textarea { font-size: 16px !important; }

  /* Touch-friendly buttons */
  .btn { min-height: 44px; min-width: 44px; font-size: 14px; }
  .btn-sm { min-height: 44px; font-size: 11px; }

  /* Despesas filter row */
  #d-filtro-forn, #d-per-page { min-height: 44px; width: 100%; }

  /* Bottom-sheet modals (alert/confirm) */
  .alert-overlay { align-items: flex-end !important; }
  .alert-box {
    border-radius: 18px 18px 0 0 !important;
    width: 100% !important;
    max-width: 100% !important;
    padding-bottom: 2rem;
  }

  /* Vendas inline modals */
  #modal-cliente > div,
  #modal-detalhe-pedido > div {
    max-width: 95vw !important;
    max-height: 90vh;
    overflow-y: auto;
    margin: auto;
  }

  /* Estoque edit modal */
  #edit-modal { align-items: flex-end !important; }
  #edit-modal > div {
    border-radius: 18px 18px 0 0 !important;
    width: 100% !important;
    max-width: 100% !important;
    max-height: 85vh;
    overflow-y: auto;
  }

  /* ── Vendas — totals bar (wrap into 2-col grid + full-width Valor Líquido) ── */
  #v-totals-bar {
    flex-wrap: wrap !important;
    gap: 0 !important;
    padding: 0 !important;
  }
  /* Hide the thin dividers between fields */
  #v-totals-bar > div[style*="width:1px"] { display: none !important; }
  /* First 4 metric fields: 2 per row */
  #v-totals-bar > div:not([style*="width:1px"]):not(:last-child) {
    flex: 1 1 40% !important;
    min-width: 0 !important;
    padding: 8px 12px !important;
    text-align: left !important;
    border-bottom: 1px solid var(--border);
  }
  /* Valor Líquido: full-width bottom row, visually highlighted */
  #v-totals-bar > div:last-child {
    flex: 1 1 100% !important;
    min-width: 100% !important;
    text-align: center !important;
    background: var(--lila-50) !important;
    border-radius: 0 0 8px 8px;
    padding: 10px !important;
  }
  #v-total-liquido {
    font-size: 28px !important;
    font-weight: 700 !important;
    color: var(--lila-600) !important;
  }

  /* ── Clientes — pedidos modal ── */
  #modal-pedidos-cliente > div {
    width: 95vw !important;
    max-width: 95vw !important;
    max-height: 80vh !important;
    overflow-y: auto !important;
    -webkit-overflow-scrolling: touch !important;
    padding: 0 !important;
  }
  /* Sticky close button / title header */
  #modal-pedidos-cliente > div > div:first-child {
    position: sticky;
    top: 0;
    background: var(--surface);
    z-index: 1;
    padding: 1rem 1.5rem;
    border-bottom: 1px solid var(--border);
  }
  /* Restore padding for the content area */
  #modal-ped-conteudo {
    padding: 1rem;
  }
  /* Per-order card: allow horizontal scroll for the table */
  #modal-ped-conteudo > div {
    overflow-x: auto !important;
    -webkit-overflow-scrolling: touch !important;
  }
  /* Let the 7-column table grow beyond its container so horizontal scroll kicks in */
  #modal-ped-conteudo table {
    table-layout: auto !important;
    min-width: 620px;
  }
}

@media (max-width: 480px) {
  .metric-grid { grid-template-columns: 1fr !important; }
  table { font-size: 12px; }
  table th { font-size: 10px; }
  .content { padding: 0.75rem; }
}

@media (max-width: 360px) {
  .content { padding: 0.5rem; }
}
