:root {
    --color-primary: #FF6B6B;
    --color-primary-dark: #e85555;
    --color-success: #10b981;
    --color-success-dark: #059669;
    --color-route: #4285F4;
    --color-bg: #f5f6f8;
    --color-surface: #ffffff;
    --color-border: #e2e5ea;
    --color-text: #2d3436;
    --color-text-muted: #7a7f87;
    --shadow-sm: 0 1px 3px rgba(0,0,0,0.08);
    --shadow-md: 0 4px 16px rgba(0,0,0,0.16);
    --radius: 10px;
    --header-height: 52px;
}

* { box-sizing: border-box; }

body {
    margin: 0;
    font-family: 'Segoe UI', Roboto, Helvetica, Arial, sans-serif;
    background: var(--color-bg);
    color: var(--color-text);
}

.container { display: flex; flex-direction: column; height: 100vh; }

.header {
    height: var(--header-height);
    min-height: var(--header-height);
    background: linear-gradient(135deg, var(--color-primary) 0%, var(--color-primary-dark) 100%);
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0 1rem;
    box-shadow: var(--shadow-sm);
    z-index: 10;
}
.header-content { display: flex; align-items: center; gap: 0.6rem; }
.header-icon { color: #fff; font-size: 1.15rem; }
.header h1 { color: #fff; font-size: 1.1rem; font-weight: 600; margin: 0; }
.header-action { background: rgba(255,255,255,0.2); color: #fff; box-shadow: none; }
.header-action:hover { background: rgba(255,255,255,0.32); }

.toast-container {
    position: fixed;
    top: calc(var(--header-height) + 12px);
    right: 16px;
    z-index: 3500;
    display: flex;
    flex-direction: column;
    gap: 8px;
    max-width: 320px;
}
.toast {
    background: var(--color-surface);
    border-left: 4px solid var(--color-primary);
    box-shadow: var(--shadow-md);
    border-radius: 8px;
    padding: 10px 14px;
    font-size: 0.85rem;
    animation: toast-in 0.2s ease-out;
}
.toast.success { border-left-color: var(--color-success); }
.toast.error { border-left-color: #e74c3c; }
.toast.info { border-left-color: #3498db; }
.toast.fade-out { animation: toast-out 0.25s ease-in forwards; }
@keyframes toast-in { from { opacity: 0; transform: translateX(20px); } to { opacity: 1; transform: translateX(0); } }
@keyframes toast-out { to { opacity: 0; transform: translateX(20px); } }

.main-content { flex: 1; position: relative; overflow: hidden; display: flex; }

.map-section { position: absolute; inset: 0; }
.map { width: 100%; height: 100%; }

.map-toolbar {
    position: absolute;
    top: 12px;
    right: 12px;
    display: flex;
    gap: 8px;
    z-index: 900;
}
.btn-toolbar {
    width: 42px; height: 42px; border-radius: 50%;
    background: var(--color-surface); color: var(--color-text);
    border: none; box-shadow: var(--shadow-md); cursor: pointer;
    display: flex; align-items: center; justify-content: center; font-size: 0.95rem;
}
.btn-toolbar:hover { color: var(--color-primary); }

.locate-fab {
    position: absolute;
    bottom: 64px;
    right: 14px;
    width: 46px; height: 46px; border-radius: 50%;
    background: var(--color-surface); color: var(--color-text);
    border: none; box-shadow: var(--shadow-md); cursor: pointer;
    display: flex; align-items: center; justify-content: center; font-size: 1.1rem;
    z-index: 800;
}
.locate-fab:hover { color: var(--color-primary); }

.sidebar {
    position: absolute;
    top: 12px;
    left: 12px;
    bottom: 12px;
    width: 300px;
    background: var(--color-surface);
    border-radius: var(--radius);
    box-shadow: var(--shadow-md);
    z-index: 850;
    overflow-y: auto;
    padding: 1rem;
    display: flex;
    flex-direction: column;
    gap: 1rem;
}

.sidebar-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding-bottom: 0.6rem;
    border-bottom: 1px solid var(--color-border);
}
.sidebar-title { font-weight: 600; font-size: 0.95rem; }

.btn-icon-round {
    width: 36px; height: 36px; border-radius: 50%;
    border: none; background: var(--color-surface); box-shadow: var(--shadow-sm);
    display: flex; align-items: center; justify-content: center;
    cursor: pointer; color: var(--color-text); flex: none;
}
.btn-icon-round:hover { color: var(--color-primary); }

.upload-section, .stats-section, .routes-section {
    background: var(--color-bg);
    border: 1px solid var(--color-border);
    border-radius: var(--radius);
    padding: 0.9rem;
}
.upload-section h2, .stats-section h3, .routes-section h3 {
    font-size: 0.9rem; margin: 0 0 0.7rem 0; display: flex; align-items: center; gap: 0.4rem;
}

.location-input-group { display: flex; gap: 0.4rem; }
.autocomplete-wrapper { position: relative; flex: 1; }
.location-input { width: 100%; padding: 0.5rem 1.7rem 0.5rem 0.6rem; border: 1px solid var(--color-border); border-radius: 6px; font-size: 0.85rem; }
.location-input:focus { outline: none; border-color: var(--color-primary); }
.hint-text { font-size: 0.75rem; color: var(--color-text-muted); margin: 0.5rem 0 0 0; }

.input-clear-btn {
    position: absolute;
    top: 50%; right: 8px; transform: translateY(-50%);
    background: none; border: none; color: var(--color-text-muted);
    font-size: 1.2rem; cursor: pointer; line-height: 1; padding: 2px;
}
.input-clear-btn:hover { color: var(--color-primary); }

.autocomplete-list {
    position: absolute;
    top: calc(100% + 4px);
    left: 0;
    right: 0;
    background: var(--color-surface);
    border: 1px solid var(--color-border);
    border-radius: 8px;
    box-shadow: var(--shadow-md);
    max-height: 200px;
    overflow-y: auto;
    z-index: 1000;
    display: none;
}
.autocomplete-item {
    padding: 8px 10px;
    font-size: 0.78rem;
    cursor: pointer;
    border-bottom: 1px solid var(--color-border);
}
.autocomplete-item:last-child { border-bottom: none; }
.autocomplete-item:hover { background: var(--color-bg); }

.upload-box { position: relative; margin-bottom: 0.6rem; }
.upload-box input[type="file"] { position: absolute; inset: 0; opacity: 0; cursor: pointer; }
.upload-label {
    display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 0.4rem;
    padding: 1.2rem 0.6rem; border: 2px dashed var(--color-border); border-radius: 8px;
    background: var(--color-surface); color: var(--color-text-muted); font-size: 0.8rem;
    text-align: center; cursor: pointer; transition: border-color 0.15s, color 0.15s;
}
.upload-label:hover { border-color: var(--color-primary); color: var(--color-primary); }
.upload-label i { font-size: 1.4rem; }

.btn {
    border: none; border-radius: 7px; padding: 0.55rem 0.9rem; font-size: 0.85rem;
    cursor: pointer; display: inline-flex; align-items: center; justify-content: center; gap: 0.4rem;
    transition: background 0.15s, opacity 0.15s, transform 0.1s; width: 100%;
}
.btn:active { transform: scale(0.98); }
.btn:disabled { opacity: 0.5; cursor: not-allowed; }
.btn-primary { background: var(--color-primary); color: #fff; }
.btn-primary:hover:not(:disabled) { background: var(--color-primary-dark); }
.btn-success { background: var(--color-success); color: #fff; text-decoration: none; }
.btn-success:hover { background: var(--color-success-dark); }
.btn-icon { width: 40px; flex: none; background: var(--color-surface); border: 1px solid var(--color-border); color: var(--color-text); }
.btn-icon:hover { border-color: var(--color-primary); color: var(--color-primary); }
.btn-sm-full { width: 100%; padding: 0.4rem; font-size: 0.8rem; }

.stat-item { display: flex; justify-content: space-between; align-items: center; padding: 0.35rem 0; font-size: 0.85rem; border-bottom: 1px solid var(--color-border); }
.stat-item:last-child { border-bottom: none; }
.stat-label { color: var(--color-text-muted); }
.stat-value { font-weight: 600; }

.routes-list { max-height: 260px; overflow-y: auto; display: flex; flex-direction: column; gap: 0.5rem; }
.empty-state { color: var(--color-text-muted); font-size: 0.8rem; text-align: center; padding: 1rem 0; }
.route-item { background: var(--color-surface); border-radius: 6px; padding: 0.5rem 0.6rem; font-size: 0.85rem; }
.route-info { color: var(--color-text-muted); }

.stop-marker-pin {
    width: 30px; height: 30px; border-radius: 50% 50% 50% 0; transform: rotate(-45deg);
    border: 2px solid #fff; box-shadow: 0 2px 6px rgba(0,0,0,0.4);
    display: flex; align-items: center; justify-content: center;
    background: linear-gradient(135deg, var(--color-primary) 0%, var(--color-primary-dark) 100%);
}
.stop-marker-pin.delivered { background: linear-gradient(135deg, var(--color-success) 0%, var(--color-success-dark) 100%); }
.stop-marker-number { transform: rotate(45deg); color: #fff; font-weight: bold; font-size: 11px; }
.stop-marker-badge {
    position: absolute; top: -2px; right: -6px; background: var(--color-primary); color: #fff;
    border-radius: 50%; width: 18px; height: 18px; display: flex; align-items: center; justify-content: center;
    font-size: 9px; font-weight: bold; border: 2px solid #fff;
}
.stop-marker-badge.delivered { background: var(--color-success); }

.user-location-marker { position: relative; }
.user-location-dot {
    width: 16px; height: 16px; border-radius: 50%; background: var(--color-route);
    border: 3px solid #fff; box-shadow: 0 0 4px rgba(0,0,0,0.4);
    position: absolute; top: 2px; left: 2px; z-index: 2;
}
.user-location-pulse {
    width: 20px; height: 20px; border-radius: 50%; background: rgba(66,133,244,0.35);
    position: absolute; top: 0; left: 0; animation: pulse 1.8s infinite;
}
@keyframes pulse {
    0% { transform: scale(1); opacity: 0.7; }
    70% { transform: scale(2.6); opacity: 0; }
    100% { opacity: 0; }
}

.popup-content { width: 250px; padding-right: 2px; }
.popup-content h4 { margin: 0 0 8px 0; color: var(--color-primary); font-size: 16px; }
.popup-nav { display: flex; align-items: center; justify-content: space-between; margin-bottom: 8px; }
.popup-nav-btn {
    width: 32px; height: 32px; border-radius: 50%;
    border: 1px solid var(--color-border);
    background: var(--color-surface);
    color: var(--color-text);
    cursor: pointer;
    display: flex; align-items: center; justify-content: center;
    font-size: 13px;
}
.popup-nav-btn:disabled { opacity: 0.3; cursor: not-allowed; }
.popup-nav-count { font-size: 0.9rem; color: var(--color-text-muted); font-weight: 600; }
.popup-delivery { margin-bottom: 8px; padding: 8px; background: var(--color-bg); border-radius: 6px; font-size: 0.95rem; }
.popup-info { margin-top: 3px; line-height: 1.35; font-size: 0.95rem; }
.popup-info-muted { color: var(--color-text-muted); font-size: 0.85rem; }
.popup-actions { margin-top: 6px; }
.popup-actions .btn-success { padding: 0.45rem 0.7rem; font-size: 0.85rem; }
.popup-footer-actions { display: flex; gap: 8px; margin-top: 8px; }
.popup-footer-actions .btn { padding: 0.5rem 0.6rem; font-size: 0.85rem; }

.modal-overlay {
    position: fixed; inset: 0; background: rgba(0,0,0,0.45);
    display: flex; align-items: center; justify-content: center; z-index: 4000;
}
.modal {
    background: #fff; border-radius: var(--radius); width: 90%; max-width: 420px; max-height: 70vh;
    display: flex; flex-direction: column; box-shadow: var(--shadow-md); overflow: hidden;
}
.modal-header { display: flex; justify-content: space-between; align-items: center; padding: 0.9rem 1rem; border-bottom: 1px solid var(--color-border); }
.modal-header h3 { margin: 0; font-size: 1rem; display: flex; align-items: center; gap: 0.4rem; }
.btn-close { background: none; border: none; cursor: pointer; color: var(--color-text-muted); font-size: 1rem; }
.history-list { padding: 0.8rem 1rem; overflow-y: auto; display: flex; flex-direction: column; gap: 0.6rem; }
.history-item { border: 1px solid var(--color-border); border-radius: 8px; padding: 0.6rem 0.7rem; font-size: 0.85rem; }
.history-item-header { display: flex; justify-content: space-between; align-items: center; margin-bottom: 4px; }
.history-item-stats { color: var(--color-text-muted); font-size: 0.8rem; margin-bottom: 6px; }

.container.focus-mode .header,
.container.focus-mode .sidebar {
    display: none !important;
}

body.dark-mode {
    --color-bg: #1b1e24;
    --color-surface: #262a33;
    --color-border: #383d47;
    --color-text: #e8e9ea;
    --color-text-muted: #9aa0ab;
}
body.dark-mode .upload-label { background: var(--color-surface); }
body.dark-mode .location-input { background: var(--color-surface); color: var(--color-text); }
body.dark-mode .leaflet-popup-content-wrapper,
body.dark-mode .leaflet-popup-tip {
    background: var(--color-surface);
    color: var(--color-text);
}
body.dark-mode .popup-delivery { background: #313641; }
body.dark-mode .popup-nav-btn { background: var(--color-surface); color: var(--color-text); }
body.dark-mode .modal { background: var(--color-surface); color: var(--color-text); }
body.dark-mode .toast { background: var(--color-surface); color: var(--color-text); }
body.dark-mode .btn-icon-round,
body.dark-mode .btn-toolbar,
body.dark-mode .locate-fab { background: var(--color-surface); color: var(--color-text); }
body.dark-mode .autocomplete-list { background: var(--color-surface); }
body.dark-mode .autocomplete-item:hover { background: var(--color-bg); }

/* MODO NOTURNO DO MAPA — AJUSTE AQUI
   Em vez de "escurecer" um mapa já escuro (o que não funciona bem, pixels pretos
   não clareiam), invertemos as cores do mapa CLARO normal. Resultado: fundo escuro
   de verdade, mas ainda legvel.
   - brightness: suba para 1.1/1.2 se quiser mais claro, desça para 0.8 se quiser mais escuro.
   - contrast: suba para 1 se quiser mais ntido.
   - hue-rotate: não mexa, corrige as cores após a inversão. */
body.dark-mode .leaflet-tile-pane {
    filter: invert(1) hue-rotate(180deg) brightness(0.95) contrast(0.9);
}

.routes-list::-webkit-scrollbar, .sidebar::-webkit-scrollbar, .history-list::-webkit-scrollbar { width: 6px; }
.routes-list::-webkit-scrollbar-thumb, .sidebar::-webkit-scrollbar-thumb, .history-list::-webkit-scrollbar-thumb { background: var(--color-border); border-radius: 3px; }

@media (max-width: 768px) {
    .sidebar { width: calc(100% - 24px); bottom: auto; max-height: 70vh; }
}