/* =========================================
   SISTEMA DE DISTRIBUIÇÃO — BACKOFFICE CSS
   ========================================= */

:root {
  --bg: #0f1117;
  --surface: #1a1d27;
  --surface2: #22263a;
  --border: #2e324a;
  --accent: #6c63ff;
  --accent2: #8b5cf6;
  --accent-glow: rgba(108, 99, 255, 0.25);
  --green: #10b981;
  --orange: #f59e0b;
  --red: #ef4444;
  --blue: #3b82f6;
  --purple: #8b5cf6;
  --teal: #06b6d4;
  --text: #e2e8f0;
  --text-muted: #64748b;
  --text-dim: #94a3b8;
  --sidebar-w: 240px;
  --topbar-h: 64px;
  --radius: 12px;
  --shadow: 0 4px 24px rgba(0,0,0,0.4);
}

* { box-sizing: border-box; margin: 0; padding: 0; }
html, body { height: 100%; font-family: 'Inter', sans-serif; background: var(--bg); color: var(--text); overflow: hidden; }
a { text-decoration: none; color: inherit; }
button { cursor: pointer; font-family: inherit; }
input, select, textarea { font-family: inherit; }
.hidden { display: none !important; }

/* ---- LOGIN ---- */
.login-screen {
  min-height: 100vh;
  display: flex; align-items: center; justify-content: center;
  background: radial-gradient(ellipse at 60% 40%, #1e1b4b 0%, #0f1117 70%);
}
.login-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 20px;
  padding: 48px 40px;
  width: 400px;
  box-shadow: var(--shadow), 0 0 80px var(--accent-glow);
}
.login-logo { text-align: center; margin-bottom: 36px; }
.logo-icon { font-size: 56px; margin-bottom: 12px; }
.login-logo h1 { font-size: 22px; font-weight: 700; color: var(--text); }
.login-logo p { color: var(--text-muted); margin-top: 4px; font-size: 14px; }

/* ---- APP LAYOUT ---- */
.app { display: flex; height: 100vh; overflow: hidden; }

/* ---- SIDEBAR ---- */
.sidebar {
  width: var(--sidebar-w);
  background: var(--surface);
  border-right: 1px solid var(--border);
  display: flex; flex-direction: column;
  flex-shrink: 0;
  transition: width 0.3s;
  overflow: hidden;
}
.sidebar.collapsed { width: 64px; }
.sidebar-header {
  display: flex; align-items: center; gap: 12px;
  padding: 20px 16px;
  border-bottom: 1px solid var(--border);
}
.sidebar-logo { font-size: 28px; flex-shrink: 0; }
.sidebar-title { font-size: 15px; font-weight: 700; color: var(--text); }
.sidebar-subtitle { font-size: 11px; color: var(--text-muted); }
.sidebar-nav { flex: 1; padding: 12px 8px; display: flex; flex-direction: column; gap: 2px; overflow-y: auto; }
.nav-item {
  display: flex; align-items: center; gap: 10px;
  padding: 10px 12px; border-radius: 10px;
  color: var(--text-muted); font-size: 14px; font-weight: 500;
  transition: all 0.2s; position: relative;
}
.nav-item:hover { background: var(--surface2); color: var(--text); }
.nav-item.active { background: var(--accent-glow); color: var(--accent); border: 1px solid var(--accent); }
.nav-icon { font-size: 18px; flex-shrink: 0; }
.badge {
  margin-left: auto; background: var(--orange);
  color: #000; font-size: 11px; font-weight: 700;
  padding: 2px 7px; border-radius: 20px;
}
.sidebar-footer {
  display: flex; align-items: center; gap: 10px;
  padding: 16px; border-top: 1px solid var(--border);
}
.user-info { flex: 1; min-width: 0; }
.user-avatar {
  width: 36px; height: 36px; border-radius: 50%;
  background: linear-gradient(135deg, var(--accent), var(--accent2));
  display: flex; align-items: center; justify-content: center;
  font-weight: 700; font-size: 15px; flex-shrink: 0;
}
.user-name { font-size: 13px; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.user-role { font-size: 11px; color: var(--text-muted); }

/* ---- MAIN CONTENT ---- */
.main-content { flex: 1; display: flex; flex-direction: column; overflow: hidden; }

/* ---- TOPBAR ---- */
.topbar {
  height: var(--topbar-h);
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  display: flex; align-items: center; justify-content: space-between;
  padding: 0 24px; flex-shrink: 0;
}
.topbar-left { display: flex; align-items: center; gap: 16px; }
.topbar-right { display: flex; align-items: center; gap: 16px; }
.page-title { font-size: 18px; font-weight: 700; }

/* ---- VIEWS ---- */
.view { flex: 1; overflow-y: auto; padding: 24px; }
.view.active { display: block; }
.view.hidden { display: none; }
main.main-content { display: flex; flex-direction: column; }

/* ---- BUTTONS ---- */
.btn {
  display: inline-flex; align-items: center; gap: 6px;
  padding: 9px 18px; border-radius: 8px;
  font-size: 13px; font-weight: 600; border: none;
  transition: all 0.2s;
}
.btn-primary { background: var(--accent); color: #fff; }
.btn-primary:hover { background: var(--accent2); box-shadow: 0 0 18px var(--accent-glow); }
.btn-secondary { background: var(--surface2); color: var(--text); border: 1px solid var(--border); }
.btn-secondary:hover { border-color: var(--accent); color: var(--accent); }
.btn-lg { padding: 12px 24px; font-size: 15px; width: 100%; justify-content: center; }
.btn-icon {
  background: transparent; border: none;
  color: var(--text-muted); font-size: 18px;
  padding: 6px 8px; border-radius: 8px; transition: all 0.2s;
}
.btn-icon:hover { color: var(--text); background: var(--surface2); }
.btn-text { background: none; border: none; color: var(--accent); font-size: 13px; cursor: pointer; }
.btn-text:hover { text-decoration: underline; }
.btn-sm { padding: 5px 10px; font-size: 12px; }
.btn-danger { background: var(--red); color: #fff; }

/* ---- FORMS ---- */
.form-group { margin-bottom: 18px; }
.form-group label { display: block; margin-bottom: 6px; font-size: 13px; font-weight: 500; color: var(--text-dim); }
.form-group small { display: block; margin-top: 4px; font-size: 11px; color: var(--text-muted); }
.form-group input, .form-group select, .form-group textarea {
  width: 100%; background: var(--surface2);
  border: 1px solid var(--border); border-radius: 8px;
  padding: 10px 14px; color: var(--text); font-size: 14px;
  outline: none; transition: border-color 0.2s;
}
.form-group input:focus, .form-group select:focus, .form-group textarea:focus {
  border-color: var(--accent);
}
.form-group textarea { resize: vertical; }
.form-row { display: flex; align-items: center; justify-content: space-between; }
.form-actions { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; margin-top: 8px; }

/* Toggle */
.toggle { position: relative; display: inline-block; width: 48px; height: 26px; }
.toggle input { opacity: 0; width: 0; height: 0; }
.toggle-slider {
  position: absolute; inset: 0; background: var(--surface2);
  border-radius: 26px; cursor: pointer; transition: 0.3s;
  border: 1px solid var(--border);
}
.toggle-slider:before {
  content: ''; position: absolute;
  width: 20px; height: 20px; left: 2px; bottom: 2px;
  background: var(--text-muted); border-radius: 50%; transition: 0.3s;
}
.toggle input:checked + .toggle-slider { background: var(--accent); }
.toggle input:checked + .toggle-slider:before { transform: translateX(22px); background: #fff; }

/* ---- CARDS ---- */
.card {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 20px;
  margin-bottom: 20px;
}
.card-header { display: flex; align-items: center; justify-content: space-between; margin-bottom: 16px; }
.card-header h3 { font-size: 15px; font-weight: 600; }

/* ---- KPIs ---- */
.kpi-grid { display: grid; grid-template-columns: repeat(4, 1fr); gap: 16px; margin-bottom: 24px; }
.kpi-card {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 20px;
  display: flex; flex-direction: column; align-items: center;
  gap: 8px; text-align: center; transition: transform 0.2s, box-shadow 0.2s;
  position: relative; overflow: hidden;
}
.kpi-card::before {
  content: ''; position: absolute;
  top: 0; left: 0; right: 0; height: 3px;
  border-radius: var(--radius) var(--radius) 0 0;
}
.kpi-total::before { background: var(--accent); }
.kpi-pendente::before { background: var(--orange); }
.kpi-rota::before { background: var(--purple); }
.kpi-entregue::before { background: var(--green); }
.kpi-card:hover { transform: translateY(-2px); box-shadow: 0 8px 32px rgba(0,0,0,0.3); }
.kpi-icon { font-size: 32px; }
.kpi-value { font-size: 36px; font-weight: 800; }
.kpi-label { font-size: 12px; color: var(--text-muted); font-weight: 500; }

/* ---- DASHBOARD GRID ---- */
.dashboard-grid { display: grid; grid-template-columns: 1fr 380px; gap: 20px; }

/* ---- TABLE ---- */
.table { width: 100%; border-collapse: collapse; }
.table th { padding: 10px 14px; text-align: left; font-size: 12px; font-weight: 600; color: var(--text-muted); text-transform: uppercase; border-bottom: 1px solid var(--border); }
.table td { padding: 12px 14px; font-size: 13px; border-bottom: 1px solid var(--border); vertical-align: middle; }
.table tbody tr { transition: background 0.15s; }
.table tbody tr:hover { background: var(--surface2); }
.table tbody tr:last-child td { border-bottom: none; }

/* ---- STATUS BADGES ---- */
.status-badge {
  display: inline-flex; align-items: center; gap: 5px;
  padding: 3px 10px; border-radius: 20px; font-size: 11px; font-weight: 600;
}
.status-pendente { background: rgba(245,158,11,0.15); color: var(--orange); }
.status-atribuida { background: rgba(59,130,246,0.15); color: var(--blue); }
.status-em_rota { background: rgba(139,92,246,0.15); color: var(--purple); }
.status-entregue { background: rgba(16,185,129,0.15); color: var(--green); }
.status-falhou { background: rgba(239,68,68,0.15); color: var(--red); }

/* ---- TOOLBAR ---- */
.toolbar { display: flex; align-items: center; justify-content: space-between; margin-bottom: 20px; gap: 12px; flex-wrap: wrap; }
.filters { display: flex; gap: 10px; flex-wrap: wrap; }
.select-filter {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: 8px; padding: 8px 12px; color: var(--text); font-size: 13px;
  outline: none;
}
.select-filter:focus { border-color: var(--accent); }
.toolbar-actions { display: flex; gap: 10px; }

/* ---- EMPTY STATE ---- */
.empty-state { text-align: center; padding: 48px; color: var(--text-muted); }
.empty-icon { font-size: 48px; margin-bottom: 12px; }

/* ---- MAP ---- */
.map-container { position: relative; height: calc(100vh - var(--topbar-h) - 48px); border-radius: var(--radius); overflow: hidden; border: 1px solid var(--border); }
#main-map { height: 100%; }
.map-legend {
  position: absolute; bottom: 16px; left: 16px; z-index: 999;
  background: var(--surface); border: 1px solid var(--border);
  border-radius: 10px; padding: 12px 16px; display: flex; flex-direction: column; gap: 6px;
}
.legend-item { display: flex; align-items: center; gap: 6px; font-size: 12px; color: var(--text-dim); }

/* ---- DOTS ---- */
.dot { width: 10px; height: 10px; border-radius: 50%; display: inline-block; flex-shrink: 0; }
.dot-green { background: var(--green); }
.dot-orange { background: var(--orange); }
.dot-red { background: var(--red); }
.dot-blue { background: var(--blue); }
.dot-purple { background: var(--purple); }
.dot-teal { background: var(--teal); }

/* ---- DRIVERS GRID ---- */
.drivers-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(280px, 1fr)); gap: 16px; }
.driver-card {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 20px;
  display: flex; align-items: center; gap: 14px;
  transition: border-color 0.2s, transform 0.2s;
}
.driver-card:hover { border-color: var(--accent); transform: translateY(-2px); }
.driver-avatar {
  width: 52px; height: 52px; border-radius: 50%; flex-shrink: 0;
  background: linear-gradient(135deg, var(--accent), var(--accent2));
  display: flex; align-items: center; justify-content: center;
  font-size: 22px; font-weight: 700;
}
.driver-info { flex: 1; }
.driver-name { font-size: 15px; font-weight: 600; }
.driver-vehicle { font-size: 12px; color: var(--text-muted); margin-top: 2px; }
.driver-stats { font-size: 11px; color: var(--text-dim); margin-top: 4px; }

/* ---- NOTIFICAÇÕES ---- */
.notif-wrapper { position: relative; }
.notif-btn { position: relative; }
.notif-count {
  position: absolute; top: 0; right: 0;
  background: var(--red); color: #fff;
  font-size: 9px; font-weight: 700;
  width: 16px; height: 16px; border-radius: 50%;
  display: flex; align-items: center; justify-content: center;
}
.notif-dropdown {
  position: absolute; right: 0; top: calc(100% + 8px);
  width: 320px; background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); box-shadow: var(--shadow); z-index: 1000;
}
.notif-header { display: flex; justify-content: space-between; align-items: center; padding: 12px 16px; border-bottom: 1px solid var(--border); font-size: 14px; font-weight: 600; }
.notif-list { max-height: 380px; overflow-y: auto; }
.notif-item { padding: 12px 16px; border-bottom: 1px solid var(--border); font-size: 13px; transition: background 0.15s; }
.notif-item:hover { background: var(--surface2); cursor: pointer; }
.notif-item.unread { border-left: 3px solid var(--accent); }
.notif-item-title { font-weight: 600; }
.notif-item-msg { color: var(--text-muted); margin-top: 2px; }
.notif-item-time { color: var(--text-muted); font-size: 11px; margin-top: 4px; }

/* ---- MODAL ---- */
.modal-overlay {
  position: fixed; inset: 0;
  background: rgba(0,0,0,0.6); backdrop-filter: blur(4px);
  display: flex; align-items: center; justify-content: center;
  z-index: 2000;
}
.modal {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: 16px; width: 600px; max-width: 95vw;
  max-height: 90vh; display: flex; flex-direction: column;
  box-shadow: var(--shadow);
}
.modal-header { display: flex; align-items: center; justify-content: space-between; padding: 20px 24px; border-bottom: 1px solid var(--border); }
.modal-header h3 { font-size: 16px; font-weight: 700; }
.modal-body { padding: 24px; overflow-y: auto; flex: 1; }

/* Modal detalhe */
.detail-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; margin-bottom: 16px; }
.detail-field label { font-size: 11px; color: var(--text-muted); font-weight: 600; text-transform: uppercase; display: block; margin-bottom: 4px; }
.detail-field span, .detail-field p { font-size: 13px; color: var(--text); }
.events-list { margin-top: 16px; }
.events-list h4 { font-size: 13px; font-weight: 600; margin-bottom: 10px; color: var(--text-dim); }
.event-item { display: flex; gap: 10px; margin-bottom: 8px; }
.event-dot { width: 8px; height: 8px; border-radius: 50%; background: var(--accent); flex-shrink: 0; margin-top: 5px; }
.event-text { font-size: 12px; }
.event-time { font-size: 11px; color: var(--text-muted); }
.modal-actions { display: flex; gap: 8px; flex-wrap: wrap; margin-top: 16px; padding-top: 16px; border-top: 1px solid var(--border); }
.detail-map { height: 200px; border-radius: 8px; margin-top: 12px; overflow: hidden; }

/* ---- BULK BAR ---- */
.bulk-bar {
  display: flex; align-items: center; gap: 12px;
  padding: 10px 16px; margin-bottom: 12px;
  background: rgba(108,99,255,0.1); border: 1px solid rgba(108,99,255,0.3);
  border-radius: var(--radius); font-size: 13px; font-weight: 600;
  animation: toastIn 0.2s ease;
}

/* ---- CARGO PREPARATION ---- */
.cargo-info-bar {
  display: flex; justify-content: space-between; gap: 16px;
  padding: 10px 16px; margin-bottom: 16px;
  background: rgba(108,99,255,0.08); border: 1px solid rgba(108,99,255,0.2);
  border-radius: 8px; font-size: 12px; color: var(--text-dim);
}
.cargo-badge {
  background: var(--accent); color: #fff; padding: 3px 10px;
  border-radius: 20px; font-size: 11px; font-weight: 700;
}
.cargo-num {
  width: 32px; height: 32px; border-radius: 50%;
  display: flex; align-items: center; justify-content: center;
  font-weight: 800; font-size: 13px; color: #fff; flex-shrink: 0;
}
.cargo-first { background: var(--red); }
.cargo-last { background: var(--green); }
.cargo-mid { background: var(--accent); }
.cargo-row-first td { background: rgba(239,68,68,0.06); }
.cargo-row-last td { background: rgba(16,185,129,0.06); }
.cargo-tag {
  display: inline-block; padding: 2px 8px; border-radius: 10px;
  font-size: 10px; font-weight: 700;
}
.cargo-tag-first { background: rgba(239,68,68,0.15); color: var(--red); }
.cargo-tag-last { background: rgba(16,185,129,0.15); color: var(--green); }

/* Print-only cargo elements */
.cargo-print-header, .cargo-print-footer { display: none; }

@media print {
  * { color: #000 !important; background: #fff !important; }
  .sidebar, .topbar, .toolbar, .cargo-info-bar, .empty-state { display: none !important; }
  .view { display: none !important; }
  #view-cargo { display: block !important; padding: 0 !important; }
  .main-content { overflow: visible !important; }
  .app { display: block !important; }
  .card { border: none !important; padding: 0 !important; box-shadow: none !important; }
  .cargo-print-header, .cargo-print-footer { display: block !important; }
  .table th { background: #eee !important; color: #333 !important; font-size: 10px !important; }
  .table td { font-size: 11px !important; padding: 6px 8px !important; }
  .cargo-num { border: 2px solid #333; color: #333 !important; }
  .cargo-row-first td { background: #f9e0e0 !important; }
  .cargo-row-last td { background: #dff5e7 !important; }
  .cargo-tag { border: 1px solid; }
}

/* ---- CONFIG ---- */
.config-grid { display: grid; grid-template-columns: 1fr 400px; gap: 20px; }
.sync-status-info .status-row { display: flex; justify-content: space-between; padding: 8px 0; border-bottom: 1px solid var(--border); font-size: 13px; }
.sync-log { margin-top: 16px; }
.sync-log-title { font-size: 12px; font-weight: 600; color: var(--text-muted); margin-bottom: 8px; text-transform: uppercase; }
.log-entry { font-size: 12px; padding: 4px 0; color: var(--text-dim); }
.log-entry.success { color: var(--green); }
.log-entry.error { color: var(--red); }

/* ---- RECENT DELIVERIES ---- */
.recent-item { display: flex; align-items: center; justify-content: space-between; padding: 10px 0; border-bottom: 1px solid var(--border); }
.recent-item:last-child { border-bottom: none; }
.recent-info .recent-doc { font-size: 13px; font-weight: 600; }
.recent-info .recent-client { font-size: 12px; color: var(--text-muted); margin-top: 2px; }

/* ---- STATUS BARS ---- */
.status-bar-item { margin-bottom: 12px; }
.status-bar-label { display: flex; justify-content: space-between; font-size: 12px; margin-bottom: 4px; color: var(--text-dim); }
.progress-bar { height: 6px; background: var(--surface2); border-radius: 3px; overflow: hidden; }
.progress-fill { height: 100%; border-radius: 3px; transition: width 0.8s ease; }

/* ---- TOAST ---- */
#toast-container { position: fixed; bottom: 24px; right: 24px; z-index: 9999; display: flex; flex-direction: column; gap: 8px; }
.toast {
  background: var(--surface2); border: 1px solid var(--border);
  border-radius: 10px; padding: 12px 18px; font-size: 13px;
  box-shadow: var(--shadow); min-width: 240px;
  display: flex; align-items: center; gap: 10px;
  animation: toastIn 0.3s ease;
}
.toast.success { border-left: 3px solid var(--green); }
.toast.error { border-left: 3px solid var(--red); }
.toast.info { border-left: 3px solid var(--accent); }
@keyframes toastIn { from { transform: translateX(100%); opacity: 0; } to { transform: translateX(0); opacity: 1; } }

/* ---- ERROR MSG ---- */
.error-msg, .msg { padding: 10px 14px; border-radius: 8px; font-size: 13px; }
.error-msg { background: rgba(239,68,68,0.1); color: var(--red); border: 1px solid rgba(239,68,68,0.3); }
.msg.success { background: rgba(16,185,129,0.1); color: var(--green); border: 1px solid rgba(16,185,129,0.3); }
.msg.error { background: rgba(239,68,68,0.1); color: var(--red); border: 1px solid rgba(239,68,68,0.3); }

/* ---- TEST RESULT ---- */
.test-result {
  margin-top: 16px; padding: 16px; border-radius: 10px;
  font-size: 13px; animation: toastIn 0.3s ease;
}
.test-result.success { background: rgba(16,185,129,0.1); border: 1px solid rgba(16,185,129,0.3); color: var(--green); }
.test-result.error { background: rgba(239,68,68,0.1); border: 1px solid rgba(239,68,68,0.3); color: var(--red); }
.test-result h4 { font-size: 14px; margin-bottom: 8px; }
.test-result .test-detail { font-size: 12px; color: var(--text-dim); margin-top: 4px; }
.test-result .test-sample { margin-top: 10px; padding-top: 10px; border-top: 1px solid var(--border); }
.test-result .test-sample-item { display: flex; justify-content: space-between; padding: 4px 0; font-size: 12px; }
.test-result .test-sample-item:nth-child(even) { background: rgba(255,255,255,0.03); }

/* ---- CONNECTION STATUS ---- */
.connection-status { display: flex; align-items: center; gap: 6px; font-size: 12px; color: var(--text-muted); }

/* ---- LEAFLET OVERRIDES ---- */
.leaflet-popup-content-wrapper { background: var(--surface2) !important; border: 1px solid var(--border) !important; color: var(--text) !important; border-radius: 10px !important; }
.leaflet-popup-tip { background: var(--surface2) !important; }

/* ---- SCROLLBAR ---- */
::-webkit-scrollbar { width: 6px; height: 6px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--border); border-radius: 3px; }
::-webkit-scrollbar-thumb:hover { background: var(--text-muted); }

/* ---- RESPONSIVE ---- */
@media (max-width: 1200px) {
  .kpi-grid { grid-template-columns: repeat(2, 1fr); }
  .dashboard-grid { grid-template-columns: 1fr; }
  .config-grid { grid-template-columns: 1fr; }
}
@media (max-width: 768px) {
  .sidebar { position: fixed; z-index: 100; height: 100vh; transform: translateX(-100%); transition: transform 0.3s; }
  .sidebar.open { transform: translateX(0); }
  .kpi-grid { grid-template-columns: repeat(2, 1fr); }
  .detail-grid { grid-template-columns: 1fr; }
}
