* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
    -webkit-tap-highlight-color: transparent;
}

:root {
    --color-bg: #1a1a2e;
    --color-bg-card: #16213e;
    --color-bg-elevated: #1e2a47;
    --color-primary: #4cc9f0;
    --color-success: #4ade80;
    --color-warning: #fbbf24;
    --color-danger: #ef4444;
    --color-muted: #6b7280;
    --color-text: #eaeaea;
    --color-text-secondary: #888;
    --nav-height: 64px;
    --header-height: 56px;
    --safe-bottom: env(safe-area-inset-bottom, 0px);
    --safe-top: env(safe-area-inset-top, 0px);
}

html {
    overscroll-behavior: none;
}

body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, sans-serif;
    background-color: var(--color-bg);
    color: var(--color-text);
    min-height: 100vh;
    min-height: 100dvh;
}

/* App Container */
.app {
    display: flex;
    flex-direction: column;
    height: 100vh;
    height: 100dvh;
    padding-top: var(--safe-top);
}

/* Header */
.app-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    height: var(--header-height);
    padding: 0 16px;
    background: var(--color-bg);
    border-bottom: 1px solid rgba(255, 255, 255, 0.1);
    flex-shrink: 0;
}

.header-title {
    display: flex;
    align-items: baseline;
    gap: 12px;
}

.app-header h1 {
    font-size: 1.25rem;
    font-weight: 600;
    color: var(--color-primary);
}

.app-header .subtitle {
    display: none;
    font-size: 0.85rem;
    color: var(--color-text-secondary);
}

/* Section titles - hidden on mobile */
.section-title {
    display: none;
    color: var(--color-primary);
    font-size: 1.1rem;
    font-weight: 600;
    margin-bottom: 12px;
}

.connection-indicator {
    display: flex;
    align-items: center;
    gap: 6px;
    font-size: 0.75rem;
    color: var(--color-text-secondary);
}

.connection-dot {
    width: 8px;
    height: 8px;
    border-radius: 50%;
    background: var(--color-danger);
}

.connection-indicator.connected .connection-dot {
    background: var(--color-success);
    animation: pulse 2s infinite;
}

@keyframes pulse {
    0%, 100% { opacity: 1; }
    50% { opacity: 0.5; }
}

/* Tab Content */
.tab-content {
    flex: 1;
    overflow: hidden;
    position: relative;
}

.tab-panel {
    display: none;
    height: 100%;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
}

.tab-panel.active {
    display: flex;
    flex-direction: column;
}

/* Status Tab */
.status-cards {
    display: flex;
    flex-direction: column;
    gap: 16px;
    padding: 20px 16px;
    flex: 1;
    justify-content: center;
}

.status-card {
    background: var(--color-bg-card);
    border-radius: 20px;
    padding: 24px;
    text-align: center;
    border: 2px solid transparent;
    transition: all 0.3s ease;
}

.status-card.status-at_home {
    border-color: var(--color-success);
    box-shadow: 0 0 30px rgba(74, 222, 128, 0.15);
}

.status-card.status-away {
    border-color: var(--color-warning);
    box-shadow: 0 0 30px rgba(251, 191, 36, 0.15);
}

.status-card.status-unknown {
    border-color: var(--color-muted);
}

.charging-card.charging-enabled {
    border-color: var(--color-success);
    box-shadow: 0 0 30px rgba(74, 222, 128, 0.15);
}

.charging-card.charging-disabled {
    border-color: var(--color-danger);
    box-shadow: 0 0 30px rgba(239, 68, 68, 0.15);
}

.charging-card.charging-unknown {
    border-color: var(--color-muted);
}

.status-icon {
    width: 48px;
    height: 48px;
    margin: 0 auto 12px;
    color: var(--color-muted);
}

.status-icon svg {
    width: 100%;
    height: 100%;
}

.status-at_home .status-icon {
    color: var(--color-success);
}

.status-at_home .icon-car {
    display: none;
}

.status-away .status-icon {
    color: var(--color-warning);
}

.status-away .icon-home {
    display: none;
}

.status-unknown .icon-home {
    display: none;
}

.charging-enabled .status-icon {
    color: var(--color-success);
}

.charging-disabled .status-icon {
    color: var(--color-danger);
}

.status-label {
    font-size: 0.7rem;
    color: var(--color-text-secondary);
    text-transform: uppercase;
    letter-spacing: 1.5px;
    margin-bottom: 8px;
}

.status-value {
    font-size: 2rem;
    font-weight: 700;
    margin-bottom: 8px;
    letter-spacing: 1px;
}

.status-at_home .status-value {
    color: var(--color-success);
}

.status-away .status-value {
    color: var(--color-warning);
}

.status-unknown .status-value {
    color: var(--color-muted);
}

.charging-enabled .status-value {
    color: var(--color-success);
}

.charging-disabled .status-value {
    color: var(--color-danger);
}

.distance-value {
    font-size: 1rem;
    color: var(--color-text-secondary);
    margin-bottom: 8px;
}

.updated-at {
    font-size: 0.75rem;
    color: var(--color-muted);
}

/* Map Tab */
#tab-map {
    padding: 0;
}

#tab-map .section-title {
    padding: 16px 16px 0;
}

#map {
    width: 100%;
    flex: 1;
    min-height: 300px;
    touch-action: pan-x pan-y;
}

.leaflet-control-zoom {
    border: none !important;
    box-shadow: 0 2px 10px rgba(0, 0, 0, 0.3) !important;
}

.leaflet-control-zoom a {
    width: 40px !important;
    height: 40px !important;
    line-height: 40px !important;
    font-size: 18px !important;
}

/* History Tab */
#tab-history .section-title {
    padding: 0 16px;
}

.history-container {
    display: flex;
    flex-direction: column;
    flex: 1;
}

.history-list {
    flex: 1;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
}

.history-item {
    display: flex;
    align-items: center;
    padding: 14px 16px;
    border-bottom: 1px solid rgba(255, 255, 255, 0.06);
    gap: 12px;
}

.history-item:active {
    background: var(--color-bg-elevated);
}

.history-time {
    width: 60px;
    flex-shrink: 0;
}

.history-time .time {
    font-size: 0.9rem;
    font-weight: 500;
    color: var(--color-text);
}

.history-time .date {
    font-size: 0.7rem;
    color: var(--color-muted);
}

.history-transition {
    flex: 1;
    display: flex;
    align-items: center;
    gap: 8px;
}

.history-arrow {
    width: 16px;
    height: 16px;
    color: var(--color-muted);
}

.history-distance {
    font-size: 0.8rem;
    color: var(--color-text-secondary);
    text-align: right;
    min-width: 50px;
}

.state-badge {
    display: inline-block;
    padding: 5px 10px;
    border-radius: 16px;
    font-size: 0.7rem;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.5px;
}

.state-badge.at_home {
    background: rgba(74, 222, 128, 0.15);
    color: var(--color-success);
}

.state-badge.away {
    background: rgba(251, 191, 36, 0.15);
    color: var(--color-warning);
}

.state-badge.unknown {
    background: rgba(107, 114, 128, 0.15);
    color: var(--color-muted);
}

.loading-state {
    display: flex;
    align-items: center;
    justify-content: center;
    height: 100%;
    color: var(--color-muted);
    font-size: 0.9rem;
}

/* Pagination */
.pagination {
    display: flex;
    justify-content: center;
    align-items: center;
    gap: 24px;
    padding: 12px 16px;
    border-top: 1px solid rgba(255, 255, 255, 0.1);
    background: var(--color-bg-card);
}

.pagination button {
    width: 44px;
    height: 44px;
    border-radius: 50%;
    background: var(--color-primary);
    border: none;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    transition: all 0.2s ease;
}

.pagination button svg {
    width: 20px;
    height: 20px;
    color: var(--color-bg);
}

.pagination button:active:not(:disabled) {
    transform: scale(0.9);
}

.pagination button:disabled {
    background: var(--color-muted);
    opacity: 0.3;
    cursor: not-allowed;
}

#page-info {
    font-size: 0.85rem;
    color: var(--color-text-secondary);
    min-width: 60px;
    text-align: center;
}

/* Bottom Navigation */
.bottom-nav {
    display: flex;
    justify-content: space-around;
    align-items: stretch;
    height: var(--nav-height);
    padding-bottom: var(--safe-bottom);
    background: var(--color-bg-card);
    border-top: 1px solid rgba(255, 255, 255, 0.1);
    flex-shrink: 0;
}

.nav-item {
    flex: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 4px;
    background: none;
    border: none;
    color: var(--color-muted);
    cursor: pointer;
    transition: color 0.2s ease;
    padding: 8px 0;
}

.nav-item svg {
    width: 24px;
    height: 24px;
}

.nav-item span {
    font-size: 0.65rem;
    font-weight: 500;
    text-transform: uppercase;
    letter-spacing: 0.5px;
}

.nav-item.active {
    color: var(--color-primary);
}

.nav-item:active {
    color: var(--color-primary);
}

/* ======================= */
/* Desktop / Large Screens */
/* ======================= */

@media (min-width: 769px) {
    :root {
        --nav-height: 0px;
        --header-height: 70px;
    }

    .app-header {
        justify-content: center;
        position: relative;
    }

    .header-title {
        flex-direction: column;
        align-items: center;
        gap: 4px;
    }

    .app-header h1 {
        font-size: 1.75rem;
    }

    .app-header .subtitle {
        display: block;
    }

    .connection-indicator {
        position: absolute;
        right: 24px;
    }

    /* Show section titles on desktop */
    .section-title {
        display: block;
    }

    /* Hide bottom nav on desktop */
    .bottom-nav {
        display: none;
    }

    /* Show all sections vertically */
    .tab-content {
        display: flex;
        flex-direction: column;
        gap: 24px;
        padding: 24px;
        max-width: 1200px;
        margin: 0 auto;
        overflow-y: auto;
    }

    .tab-panel {
        display: block !important;
        height: auto !important;
        overflow: visible !important;
        flex-shrink: 0;
    }

    #tab-state {
        order: 1;
    }

    #tab-map {
        order: 2;
    }

    #tab-history {
        order: 3;
    }

    /* Status cards side by side */
    .status-cards {
        flex-direction: row;
        justify-content: center;
        padding: 0;
        flex: none;
    }

    .status-card {
        max-width: 350px;
        flex: 1;
    }

    /* Map section */
    #tab-map {
        height: auto !important;
    }

    #map {
        height: 400px;
        border-radius: 20px;
        box-shadow: 0 4px 20px rgba(0, 0, 0, 0.3);
    }

    /* History section */
    #tab-history .section-title {
        padding: 0;
        margin-bottom: 12px;
    }

    .history-container {
        background: var(--color-bg-card);
        border-radius: 20px;
        overflow: hidden;
        height: auto;
        flex: none;
    }

    .history-list {
        max-height: 340px;
        overflow-y: auto;
    }

    /* Map section title spacing */
    #tab-map .section-title {
        padding: 0;
        margin-bottom: 12px;
    }
}

/* Tablet */
@media (min-width: 769px) and (max-width: 1024px) {
    .tab-content {
        padding: 20px;
        gap: 20px;
    }

    .status-card {
        padding: 20px;
    }

    #map {
        height: 350px;
    }
}

/* Landscape phone - keep tab navigation but adjust layout */
@media (max-width: 768px) and (orientation: landscape) {
    :root {
        --header-height: 44px;
        --nav-height: 56px;
    }

    .app-header h1 {
        font-size: 1rem;
    }

    .status-cards {
        flex-direction: row;
        padding: 12px;
        gap: 12px;
    }

    .status-card {
        padding: 16px;
        flex: 1;
    }

    .status-icon {
        width: 32px;
        height: 32px;
        margin-bottom: 8px;
    }

    .status-value {
        font-size: 1.4rem;
    }

    .status-label {
        font-size: 0.6rem;
    }

    .distance-value {
        font-size: 0.85rem;
        margin-bottom: 4px;
    }

    .updated-at {
        font-size: 0.65rem;
    }

    .nav-item svg {
        width: 20px;
        height: 20px;
    }

    .nav-item span {
        font-size: 0.6rem;
    }
}

/* Small phones */
@media (max-width: 380px) {
    .status-cards {
        padding: 16px 12px;
        gap: 12px;
    }

    .status-card {
        padding: 20px 16px;
    }

    .status-icon {
        width: 40px;
        height: 40px;
    }

    .status-value {
        font-size: 1.6rem;
    }

    .history-item {
        padding: 12px;
    }

    .state-badge {
        padding: 4px 8px;
        font-size: 0.65rem;
    }
}

/* Dark mode adjustments for OLED screens */
@media (prefers-color-scheme: dark) {
    :root {
        --color-bg: #0a0a14;
        --color-bg-card: #12121e;
        --color-bg-elevated: #1a1a28;
    }
}

/* Reduce motion for accessibility */
@media (prefers-reduced-motion: reduce) {
    *,
    *::before,
    *::after {
        animation-duration: 0.01ms !important;
        animation-iteration-count: 1 !important;
        transition-duration: 0.01ms !important;
    }
}

/* High contrast mode */
@media (prefers-contrast: high) {
    .status-card {
        border-width: 3px;
    }

    .state-badge {
        border: 1px solid currentColor;
    }

    .bottom-nav,
    .pagination {
        border-top-width: 2px;
    }
}
