/* ===== Reset & Base ===== */
*, *::before, *::after {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

:root {
    --primary: #2563eb;
    --primary-dark: #1d4ed8;
    --primary-light: #dbeafe;
    --success: #16a34a;
    --danger: #dc2626;
    --warning: #f59e0b;
    --bg: #f8fafc;
    --sidebar-bg: #ffffff;
    --header-bg: #4a4a4a;
    --text: #1e293b;
    --text-light: #64748b;
    --border: #e2e8f0;
    --input-bg: #ffffff;
    --card-bg: #f8fafc;
    --shadow: 0 1px 3px rgba(0,0,0,0.1);
    --shadow-lg: 0 4px 12px rgba(0,0,0,0.15);
    --radius: 8px;
    --logo-shadow: drop-shadow(0 1px 2px rgba(0,0,0,0.3));
}

[data-theme="dark"] {
    --primary: #3b82f6;
    --primary-dark: #2563eb;
    --primary-light: #1e3a5f;
    --success: #22c55e;
    --danger: #ef4444;
    --warning: #f59e0b;
    --bg: #0f172a;
    --sidebar-bg: #1e293b;
    --header-bg: #2d2d2d;
    --text: #e2e8f0;
    --text-light: #94a3b8;
    --border: #334155;
    --input-bg: #0f172a;
    --card-bg: #0f172a;
    --shadow: 0 1px 3px rgba(0,0,0,0.3);
    --shadow-lg: 0 4px 12px rgba(0,0,0,0.4);
    --logo-shadow: none;
}

body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
    background: var(--bg);
    color: var(--text);
    height: 100vh;
    overflow: hidden;
}

/* ===== Layout ===== */
.app-container {
    display: flex;
    height: 100vh;
}

/* ===== Sidebar ===== */
.sidebar {
    width: 380px;
    min-width: 380px;
    background: var(--sidebar-bg);
    border-right: 1px solid var(--border);
    display: flex;
    flex-direction: column;
    overflow-y: auto;
    box-shadow: var(--shadow);
    z-index: 10;
}

.sidebar-header {
    padding: 16px 24px;
    background: var(--header-bg);
    color: white;
}

.header-top {
    display: flex;
    align-items: center;
    justify-content: space-between;
}

.header-brand {
    display: flex;
    flex-direction: column;
    gap: 2px;
}

.header-logo {
    height: 40px;
    width: auto;
    object-fit: contain;
    filter: var(--logo-shadow);
}

.sidebar-header .subtitle {
    font-size: 0.75rem;
    opacity: 0.8;
    margin-top: 2px;
    letter-spacing: 0.5px;
}

.btn-theme {
    background: rgba(255,255,255,0.15);
    border: 1px solid rgba(255,255,255,0.2);
    color: white;
    width: 36px;
    height: 36px;
    border-radius: 50%;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.2s;
    flex-shrink: 0;
}

.btn-theme:hover {
    background: rgba(255,255,255,0.25);
}

/* Show/hide sun/moon icons based on theme */
.icon-moon { display: none; }
[data-theme="dark"] .icon-sun { display: none; }
[data-theme="dark"] .icon-moon { display: block; }

/* ===== Sections ===== */
.section {
    padding: 16px 24px;
    border-bottom: 1px solid var(--border);
}

.section h2 {
    font-size: 0.8rem;
    text-transform: uppercase;
    letter-spacing: 0.5px;
    color: var(--text-light);
    margin-bottom: 10px;
    font-weight: 600;
}

/* ===== Input Groups ===== */
.input-group {
    display: flex;
    gap: 8px;
}

.input-group input {
    flex: 1;
    padding: 10px 12px;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    font-size: 0.9rem;
    outline: none;
    transition: border-color 0.2s;
    background: var(--input-bg);
    color: var(--text);
}

.input-group input:focus {
    border-color: var(--primary);
    box-shadow: 0 0 0 3px var(--primary-light);
}

.input-group button {
    padding: 10px 12px;
    background: var(--primary);
    color: white;
    border: none;
    border-radius: var(--radius);
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: background 0.2s;
}

.input-group button:hover {
    background: var(--primary-dark);
}

.hint {
    font-size: 0.75rem;
    color: var(--text-light);
    margin-top: 8px;
    font-style: italic;
}

/* ===== Autocomplete ===== */
.autocomplete-wrapper {
    position: relative;
}

.suggestions-list {
    position: absolute;
    top: 100%;
    left: 0;
    right: 0;
    background: var(--sidebar-bg);
    border: 1px solid var(--border);
    border-top: none;
    border-radius: 0 0 var(--radius) var(--radius);
    list-style: none;
    max-height: 220px;
    overflow-y: auto;
    z-index: 100;
    box-shadow: var(--shadow-lg);
}

.suggestions-list li {
    padding: 10px 12px;
    font-size: 0.85rem;
    cursor: pointer;
    border-bottom: 1px solid var(--border);
    display: flex;
    align-items: flex-start;
    gap: 8px;
    transition: background 0.15s;
}

.suggestions-list li:last-child {
    border-bottom: none;
}

.suggestions-list li:hover,
.suggestions-list li.active {
    background: var(--primary-light);
}

.suggestions-list li .suggestion-icon {
    color: var(--text-light);
    flex-shrink: 0;
    margin-top: 1px;
}

.suggestions-list li .suggestion-text {
    flex: 1;
    line-height: 1.3;
}

.suggestions-list li .suggestion-main {
    display: block;
    font-weight: 500;
    color: var(--text);
}

.suggestions-list li .suggestion-detail {
    display: block;
    font-size: 0.75rem;
    color: var(--text-light);
    margin-top: 1px;
}

.suggestions-list .suggestion-loading {
    padding: 12px;
    text-align: center;
    color: var(--text-light);
    font-size: 0.8rem;
    font-style: italic;
}

/* ===== Favorite suggestions ===== */
.suggestion-fav .suggestion-icon {
    color: var(--warning);
}

.suggestion-fav .suggestion-main {
    font-weight: 600;
}

/* ===== Favorites Section ===== */
.favorites-section {
    padding-bottom: 8px;
}

.fav-item {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 6px 8px;
    border-radius: var(--radius);
    transition: background 0.15s;
}

.fav-item:hover {
    background: var(--card-bg);
}

.fav-icon {
    color: var(--warning);
    flex-shrink: 0;
    display: flex;
    align-items: center;
}

.fav-name {
    flex: 1;
    font-size: 0.8rem;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.fav-remove {
    background: none;
    border: none;
    color: var(--text-light);
    cursor: pointer;
    padding: 4px;
    border-radius: 4px;
    display: flex;
    align-items: center;
    opacity: 0;
    transition: all 0.2s;
}

.fav-item:hover .fav-remove {
    opacity: 1;
}

.fav-remove:hover {
    color: var(--danger);
    background: rgba(239, 68, 68, 0.1);
}

/* ===== Stop Favorite Button ===== */
.stop-fav {
    background: none;
    border: none;
    color: var(--text-light);
    cursor: pointer;
    padding: 4px;
    border-radius: 4px;
    display: flex;
    align-items: center;
    transition: all 0.2s;
}

.stop-fav:hover {
    color: var(--warning);
}

.stop-fav.saved {
    color: var(--warning);
    cursor: default;
}

/* ===== Location Info ===== */
.location-info {
    margin-top: 8px;
    padding: 8px 12px;
    background: var(--primary-light);
    border-radius: var(--radius);
    font-size: 0.85rem;
    color: var(--text);
}

/* ===== Stops List ===== */
.stops-list {
    list-style: none;
    margin-top: 12px;
    display: flex;
    flex-direction: column;
    gap: 6px;
}

.stop-item {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 10px 12px;
    background: var(--card-bg);
    border-radius: var(--radius);
    border: 1px solid var(--border);
    transition: all 0.2s;
}

.stop-item:hover {
    border-color: var(--primary);
}

.stop-number {
    width: 26px;
    height: 26px;
    background: var(--primary);
    color: white;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 0.75rem;
    font-weight: 700;
    flex-shrink: 0;
}

.stop-item.optimized .stop-number {
    background: var(--success);
}

.stop-name {
    flex: 1;
    font-size: 0.85rem;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.stop-distance {
    font-size: 0.75rem;
    color: var(--text-light);
    flex-shrink: 0;
}

/* ===== Urgency Select ===== */
.urgency-select {
    padding: 4px 6px;
    border: 1px solid var(--border);
    border-radius: 4px;
    font-size: 0.7rem;
    font-weight: 600;
    cursor: pointer;
    outline: none;
    background: var(--input-bg);
    color: var(--text);
    flex-shrink: 0;
    min-width: 90px;
    transition: all 0.2s;
}

.urgency-select:focus {
    border-color: var(--primary);
}

.urgency-select.urgency-low {
    color: var(--text-light);
    border-color: var(--border);
}

.urgency-select.urgency-normal {
    color: var(--text);
    border-color: var(--border);
}

.urgency-select.urgency-high {
    color: #f59e0b;
    border-color: #f59e0b;
    background: rgba(245, 158, 11, 0.1);
}

.urgency-select.urgency-urgent {
    color: #ef4444;
    border-color: #ef4444;
    background: rgba(239, 68, 68, 0.1);
}

.urgency-badge {
    display: inline-block;
    padding: 1px 6px;
    border-radius: 3px;
    font-size: 0.65rem;
    font-weight: 700;
    text-transform: uppercase;
    vertical-align: middle;
    margin-left: 4px;
}

.urgency-badge.urgency-low {
    background: var(--card-bg);
    color: var(--text-light);
}

.urgency-badge.urgency-high {
    background: rgba(245, 158, 11, 0.15);
    color: #f59e0b;
}

.urgency-badge.urgency-urgent {
    background: rgba(239, 68, 68, 0.15);
    color: #ef4444;
}

.stop-remove {
    background: none;
    border: none;
    color: var(--text-light);
    cursor: pointer;
    padding: 4px;
    border-radius: 4px;
    display: flex;
    align-items: center;
    transition: all 0.2s;
}

.stop-remove:hover {
    color: var(--danger);
    background: rgba(239, 68, 68, 0.1);
}

/* ===== Buttons ===== */
.actions {
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.btn-primary {
    padding: 12px 20px;
    background: var(--primary);
    color: white;
    border: none;
    border-radius: var(--radius);
    font-size: 0.95rem;
    font-weight: 600;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    transition: all 0.2s;
}

.btn-primary:hover:not(:disabled) {
    background: var(--primary-dark);
    transform: translateY(-1px);
    box-shadow: var(--shadow-lg);
}

.btn-primary:disabled {
    opacity: 0.5;
    cursor: not-allowed;
}

.btn-primary.optimizing {
    background: var(--warning);
}

.btn-secondary {
    padding: 10px 20px;
    background: transparent;
    color: var(--text-light);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    font-size: 0.85rem;
    cursor: pointer;
    transition: all 0.2s;
}

.btn-secondary:hover {
    background: var(--card-bg);
    color: var(--text);
}

.btn-gmaps {
    width: 100%;
    padding: 12px 20px;
    background: #34a853;
    color: white;
    border: none;
    border-radius: var(--radius);
    font-size: 0.9rem;
    font-weight: 600;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    margin-top: 12px;
    transition: all 0.2s;
}

.btn-gmaps:hover {
    background: #2d9249;
    transform: translateY(-1px);
}

/* ===== Summary ===== */
.summary {
    flex: 1;
}

.summary-stats {
    display: grid;
    grid-template-columns: 1fr 1fr 1fr;
    gap: 8px;
    margin-bottom: 16px;
}

.stat {
    text-align: center;
    padding: 10px 8px;
    background: var(--card-bg);
    border-radius: var(--radius);
    border: 1px solid var(--border);
}

.stat-label {
    display: block;
    font-size: 0.7rem;
    text-transform: uppercase;
    color: var(--text-light);
    margin-bottom: 4px;
}

.stat-value {
    display: block;
    font-size: 1.1rem;
    font-weight: 700;
    color: var(--primary);
}

.route-steps {
    display: flex;
    flex-direction: column;
    gap: 2px;
}

.route-step {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 8px 0;
}

.route-step-icon {
    width: 28px;
    height: 28px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 0.75rem;
    font-weight: 700;
    flex-shrink: 0;
}

.route-step-icon.start {
    background: var(--success);
    color: white;
}

.route-step-icon.stop {
    background: var(--primary);
    color: white;
}

.route-step-info {
    flex: 1;
}

.route-step-name {
    font-size: 0.85rem;
    font-weight: 500;
}

.route-step-detail {
    font-size: 0.75rem;
    color: var(--text-light);
}

.route-step-connector {
    width: 28px;
    display: flex;
    justify-content: center;
    padding: 2px 0;
}

.route-step-connector::after {
    content: '';
    width: 2px;
    height: 16px;
    background: var(--border);
    display: block;
}

/* ===== Map ===== */
.map-container {
    flex: 1;
    position: relative;
}

#map {
    width: 100%;
    height: 100%;
}

/* Dark map tiles */
[data-theme="dark"] .leaflet-tile-pane {
    filter: invert(1) hue-rotate(180deg) brightness(0.95) contrast(0.9);
}

[data-theme="dark"] .leaflet-control-zoom a {
    background: var(--sidebar-bg);
    color: var(--text);
    border-color: var(--border);
}

[data-theme="dark"] .leaflet-control-attribution {
    background: rgba(15, 23, 42, 0.8) !important;
    color: var(--text-light) !important;
}

[data-theme="dark"] .leaflet-control-attribution a {
    color: var(--primary) !important;
}

.map-tooltip {
    position: absolute;
    top: 16px;
    left: 50%;
    transform: translateX(-50%);
    background: var(--text);
    color: var(--bg);
    padding: 8px 16px;
    border-radius: 20px;
    font-size: 0.85rem;
    z-index: 1000;
    pointer-events: none;
    opacity: 0.9;
}

/* ===== Custom Markers ===== */
.custom-marker {
    display: flex;
    align-items: center;
    justify-content: center;
}

.marker-pin {
    width: 32px;
    height: 32px;
    border-radius: 50% 50% 50% 0;
    transform: rotate(-45deg);
    display: flex;
    align-items: center;
    justify-content: center;
    box-shadow: 0 2px 6px rgba(0,0,0,0.3);
}

.marker-pin.start {
    background: var(--success);
}

.marker-pin.stop {
    background: var(--primary);
}

.marker-pin span {
    transform: rotate(45deg);
    color: white;
    font-size: 0.7rem;
    font-weight: 700;
}

/* ===== Utility ===== */
.hidden {
    display: none !important;
}

/* ===== Scrollbar ===== */
.sidebar::-webkit-scrollbar {
    width: 6px;
}

.sidebar::-webkit-scrollbar-thumb {
    background: var(--border);
    border-radius: 3px;
}

/* ===== Loading spinner ===== */
.spinner {
    width: 18px;
    height: 18px;
    border: 2px solid rgba(255,255,255,0.3);
    border-top-color: white;
    border-radius: 50%;
    animation: spin 0.6s linear infinite;
}

@keyframes spin {
    to { transform: rotate(360deg); }
}

/* ===== Responsive ===== */
@media (max-width: 768px) {
    .app-container {
        flex-direction: column;
    }

    .sidebar {
        width: 100%;
        min-width: unset;
        max-height: 45vh;
        border-right: none;
        border-bottom: 1px solid var(--border);
    }

    .sidebar-header {
        padding: 10px 16px;
    }

    .header-logo {
        height: 32px;
    }

    .sidebar-header .subtitle {
        font-size: 0.65rem;
    }

    .section {
        padding: 12px 16px;
    }

    .section h2 {
        font-size: 0.75rem;
        margin-bottom: 8px;
    }

    .input-group input {
        padding: 12px 12px;
        font-size: 16px; /* Evita zoom en iOS */
    }

    .input-group button {
        padding: 12px 14px;
    }

    .map-container {
        min-height: 55vh;
        flex: 1;
    }

    .map-tooltip {
        font-size: 0.75rem;
        padding: 6px 12px;
    }

    .suggestions-list {
        max-height: 180px;
    }

    .suggestions-list li {
        padding: 12px;
    }

    .summary-stats {
        grid-template-columns: 1fr 1fr 1fr;
        gap: 6px;
    }

    .stat {
        padding: 8px 4px;
    }

    .stat-label {
        font-size: 0.6rem;
    }

    .stat-value {
        font-size: 0.95rem;
    }

    .btn-primary {
        padding: 14px 20px;
    }

    .btn-gmaps {
        padding: 14px 20px;
    }

    .stop-item {
        padding: 8px 10px;
    }

    .urgency-select {
        min-width: 75px;
        font-size: 0.65rem;
    }
}

@media (max-width: 380px) {
    .sidebar {
        max-height: 40vh;
    }

    .sidebar-header .subtitle {
        display: none;
    }

    .summary-stats {
        grid-template-columns: 1fr 1fr;
    }
}
