:root {
    --primary-color: #6366f1;
    --secondary-color: #a855f7;
    --bg-dark: #0f172a;
    --card-bg: rgba(30, 41, 59, 0.7);
    --text-primary: #f8fafc;
    --text-secondary: #94a3b8;
    --accent: #38bdf8;
    --font-heading: 'Inter', sans-serif;
    --glass-border: 1px solid rgba(255, 255, 255, 0.1);
}

* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

body {
    font-family: var(--font-heading);
    background-color: var(--bg-dark);
    background-image: 
        radial-gradient(at 0% 0%, hsla(253,16%,7%,1) 0, transparent 50%), 
        radial-gradient(at 50% 100%, hsla(225,39%,30%,1) 0, transparent 50%);
    color: var(--text-primary);
    min-height: 100vh;
    padding: 2rem;
    overflow-x: hidden;
}

.app-wrapper {
    max-width: 1200px;
    margin: 0 auto;
    display: flex;
    flex-direction: column;
    gap: 2rem;
}

/* Header & Search */
.header {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 1rem;
    margin-bottom: 2rem;
    position: relative;
    z-index: 100;
}

@media (min-width: 768px) {
    .header {
        flex-direction: row;
        justify-content: center;
    }
}

.search-container {
    position: relative;
    width: 100%;
    max-width: 500px;
    display: flex;
    align-items: center;
}

.header-actions {
    display: flex;
    gap: 0.5rem;
}

#ip-input {
    width: 100%;
    background: rgba(15, 23, 42, 0.8);
    border: var(--glass-border);
    padding: 1rem 1.5rem;
    border-radius: 50px;
    color: white;
    font-size: 1rem;
    outline: none;
    transition: all 0.3s;
    backdrop-filter: blur(10px);
    box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1);
}

#ip-input:focus {
    border-color: var(--primary-color);
    box-shadow: 0 0 0 3px rgba(99, 102, 241, 0.2);
}

#search-btn {
    position: absolute;
    right: 8px;
    background: var(--primary-color);
    border: none;
    width: 36px;
    height: 36px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    color: white;
    cursor: pointer;
    transition: transform 0.2s;
}

#search-btn:hover {
    transform: scale(1.1);
    background: var(--secondary-color);
}

.icon-btn {
    background: rgba(30, 41, 59, 0.5);
    border: var(--glass-border);
    color: var(--text-secondary);
    cursor: pointer;
    padding: 0.8rem;
    transition: all 0.2s;
    border-radius: 12px;
    display: flex;
    align-items: center;
    justify-content: center;
}

.icon-btn:hover {
    color: var(--text-primary);
    background: rgba(255, 255, 255, 0.1);
    transform: translateY(-2px);
}

/* History Panel */
.history-panel {
    background: var(--card-bg);
    border: var(--glass-border);
    backdrop-filter: blur(16px);
    border-radius: 16px;
    padding: 1rem;
    position: absolute;
    top: 80px;
    right: 0;
    left: 0;
    margin: auto;
    width: 90%;
    max-width: 400px;
    z-index: 50;
    box-shadow: 0 20px 25px -5px rgba(0, 0, 0, 0.3);
    opacity: 0;
    transform: translateY(-10px);
    pointer-events: none;
    transition: all 0.3s ease;
}

@media (min-width: 768px) {
    .history-panel {
        right: 20%; 
        left: auto;
    }
}

.history-panel.show {
    opacity: 1;
    transform: translateY(0);
    pointer-events: all;
}

.history-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 1rem;
    padding-bottom: 0.5rem;
    border-bottom: 1px solid rgba(255,255,255,0.1);
}

.history-header h3 {
    margin: 0;
    font-size: 0.9rem;
    color: var(--text-primary);
}

#clear-history {
    background: none;
    border: none;
    color: #ef4444;
    font-size: 0.8rem;
    cursor: pointer;
}

.history-item {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 0.75rem;
    border-radius: 8px;
    cursor: pointer;
    transition: background 0.2s;
}

.history-item:hover {
    background: rgba(255,255,255,0.05);
}

.history-ip {
    color: var(--accent);
    font-weight: 500;
}

.history-loc {
    color: var(--text-secondary);
    font-size: 0.8rem;
}

/* Dashboard Layout */

.dashboard {
    display: grid;
    grid-template-columns: 1fr;
    gap: 1.5rem;
}

/* Responsive Grid for larger screens */
@media (min-width: 768px) {
    .dashboard {
        grid-template-columns: 1fr 1fr;
        grid-template-areas: 
            "hero hero"
            "map map"
            "details details";
    }
    
    .hero-card { grid-area: hero; }
    .map-card { grid-area: map; height: 400px; }
    
    .grid-container {
        grid-area: details;
        display: grid;
        grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
        gap: 1.5rem;
    }
}

/* Cards */
.card {
    background: var(--card-bg);
    backdrop-filter: blur(12px);
    border: var(--glass-border);
    border-radius: 20px;
    padding: 1.5rem;
    box-shadow: 0 10px 15px -3px rgba(0, 0, 0, 0.1);
    transition: transform 0.3s ease;
}

.card:hover {
    transform: translateY(-2px);
    border-color: rgba(255, 255, 255, 0.2);
}

h2, h3 {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    font-weight: 600;
    margin-bottom: 1rem;
    color: var(--text-secondary);
    font-size: 0.9rem;
    text-transform: uppercase;
    letter-spacing: 0.05em;
}

h2 {
    font-size: 1.1rem;
    color: var(--accent);
}

/* Hero IP Display */
.ip-display-lg {
    font-size: 2.5rem;
    font-weight: 700;
    margin: 1rem 0;
    display: flex;
    align-items: center;
    gap: 1rem;
    cursor: pointer;
    transition: opacity 0.2s;
}

.ip-display-lg:hover {
    opacity: 0.8;
}

.copy-icon {
    font-size: 1.2rem;
    opacity: 0.5;
}

/* Badges */
.badge-container {
    display: flex;
    gap: 0.5rem;
    flex-wrap: wrap;
}

.badge {
    background: rgba(99, 102, 241, 0.2);
    color: #c7d2fe;
    padding: 0.25rem 0.75rem;
    border-radius: 20px;
    font-size: 0.8rem;
    font-weight: 500;
}

/* Map */
#map {
    width: 100%;
    height: 300px;
    border-radius: 12px;
    z-index: 1;
}

@media (min-width: 768px) {
    #map { height: 100%; }
}

/* Detail Lists */
.info-row {
    display: flex;
    justify-content: space-between;
    margin-bottom: 0.75rem;
    border-bottom: 1px solid rgba(255,255,255,0.05);
    padding-bottom: 0.25rem;
}

.info-row:last-child {
    border-bottom: none;
    margin-bottom: 0;
}

.detail-card .label {
    color: var(--text-secondary);
    font-size: 0.85rem;
}

.detail-card .value {
    color: var(--text-primary);
    font-weight: 500;
    font-size: 0.95rem;
    text-align: right;
}

/* Toast */
.toast {
    position: fixed;
    bottom: 2rem;
    left: 50%;
    transform: translateX(-50%) translateY(100px);
    background: #10b981;
    color: white;
    padding: 0.75rem 1.5rem;
    border-radius: 50px;
    font-weight: 500;
    box-shadow: 0 10px 15px -3px rgba(16, 185, 129, 0.4);
    opacity: 0;
    transition: all 0.4s cubic-bezier(0.175, 0.885, 0.32, 1.275);
    z-index: 1000;
    pointer-events: none;
}

.toast.show {
    transform: translateX(-50%) translateY(0);
    opacity: 1;
}

/* Leaflet dark mode overrides */
.leaflet-container {
    background: #0f172a !important;
}

/* Modal Styles */
/* Modal Styles */
.modal-backdrop {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.6);
    backdrop-filter: blur(5px);
    z-index: 2000;
    display: none; /* Hidden by default */
    align-items: center;
    justify-content: center;
    opacity: 0;
    transition: opacity 0.3s ease;
}

.modal-backdrop.open {
    display: flex;
    opacity: 1;
}

.modal {
    background: #1e293b;
    border: 1px solid rgba(255, 255, 255, 0.1);
    border-radius: 20px;
    width: 90%;
    max-width: 600px;
    height: 85vh; /* Fixed height for consistent slide layout */
    display: flex;
    flex-direction: column;
    position: relative;
    box-shadow: 0 25px 50px -12px rgba(0, 0, 0, 0.5);
    transform: translateY(20px);
    transition: transform 0.3s ease;
    overflow: hidden;
}

.modal-backdrop.open .modal {
    transform: translateY(0);
}

.modal-header {
    padding: 1.25rem 1.5rem;
    border-bottom: 1px solid rgba(255, 255, 255, 0.1);
    display: flex;
    justify-content: space-between;
    align-items: center;
    background: #1e293b;
    flex-shrink: 0;
}

.modal-header h2 {
    margin: 0;
    font-size: 1.25rem;
    color: white;
}

.close-modal {
    background: none;
    border: none;
    color: var(--text-secondary);
    font-size: 2rem;
    line-height: 1;
    cursor: pointer;
    padding: 0.5rem;
    transition: color 0.2s;
}

.close-modal:hover {
    color: white;
}

/* Modal Content & Pagination */
.modal-body {
    flex: 1;
    overflow-y: auto;
    padding: 1.5rem;
    scroll-behavior: smooth;
}

.lesson-slide {
    display: none;
    animation: fadeIn 0.4s ease;
}

.lesson-slide.active {
    display: block;
}

.lesson-image {
    width: 100%;
    height: 120px;
    background: linear-gradient(135deg, rgba(99, 102, 241, 0.1) 0%, rgba(168, 85, 247, 0.1) 100%);
    border-radius: 12px;
    margin-bottom: 1.5rem;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 2rem;
    color: var(--accent);
}

.lesson-slide h3 {
    font-size: 1.5rem;
    color: white;
    margin-bottom: 1rem;
    display: block;
    line-height: 1.3;
}

.lesson-slide p {
    color: var(--text-secondary);
    line-height: 1.7;
    margin-bottom: 1rem;
    font-size: 1rem;
}

.lesson-slide ul {
    margin: 1rem 0;
    padding-left: 1.5rem;
    color: var(--text-secondary);
}

.lesson-slide li {
    margin-bottom: 0.5rem;
    line-height: 1.6;
}

.key-term {
    color: var(--accent);
    font-weight: 600;
}

/* Modal Footer */
.modal-footer {
    padding: 1rem 1.5rem;
    border-top: 1px solid rgba(255, 255, 255, 0.1);
    display: flex;
    justify-content: space-between;
    align-items: center;
    background: #0f172a;
    flex-shrink: 0;
}

.pagination-dots {
    display: flex;
    justify-content: center;
    align-items: center;
    flex: 1; /* Take up remaining space */
}

.lesson-counter {
    font-size: 0.9rem;
    color: var(--text-secondary);
    font-weight: 500;
    background: rgba(255, 255, 255, 0.05);
    padding: 0.25rem 0.75rem;
    border-radius: 20px;
    white-space: nowrap;
}

.nav-btn {
    padding: 0.6rem 1.2rem;
    border-radius: 8px;
    border: 1px solid rgba(255, 255, 255, 0.1);
    background: rgba(255, 255, 255, 0.05);
    color: white;
    cursor: pointer;
    font-weight: 500;
    transition: all 0.2s;
    min-width: 80px; /* Ensure buttons don't shrink too much */
    display: flex;
    justify-content: center;
    align-items: center;
}

.nav-btn:hover:not(:disabled) {
    background: var(--primary-color);
    border-color: var(--primary-color);
}

.nav-btn:disabled {
    opacity: 0.5;
    cursor: not-allowed;
}

@keyframes fadeIn {
    from { opacity: 0; transform: translateY(10px); }
    to { opacity: 1; transform: translateY(0); }
}

/* Mobile Optimizations */
@media (max-width: 768px) {
    body {
        padding: 0.75rem;
    }
    
    .card {
        padding: 1rem;
    }

    /* Fix header actions on mobile */
    .header {
        margin-bottom: 1.5rem;
    }

    .ip-display-lg {
        font-size: 1.3rem; 
        flex-direction: column;
        align-items: flex-start;
        gap: 0.25rem;
        word-break: break-all;
    }

    .copy-icon {
        display: none; 
    }

    /* Details Card Improvements */
    .info-row {
        flex-direction: column; 
        align-items: flex-start;
        gap: 0.1rem;
    }
    
    .detail-card .value {
        text-align: left;
        word-break: break-word; 
        font-size: 0.95rem;
    }

    .detail-card .label {
        font-size: 0.75rem;
        opacity: 0.7;
    }

    /* Modal Mobile Fixes */
    /* Use fixed positioning to fill 'real' viewport */
    .modal {
        position: fixed; 
        top: 0;
        left: 0;
        right: 0;
        bottom: 0;
        width: 100vw !important; /* Force full width */
        height: 100vh; /* Fallback */
        height: var(--app-height, 100dvh) !important; /* Use JS calculated height or dynamic viewport */
        max-width: none;
        max-height: none;
        border-radius: 0;
        transform: none;
        margin: 0;
        display: flex;
        flex-direction: column;
    }
    
    .modal-header {
        padding: 0.75rem 1rem;
        flex-shrink: 0; 
        background: #1e293b; /* Ensure solid background */
    }

    .modal-header h2 {
        font-size: 1rem;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis; 
        max-width: 80%;
    }
    
    .close-modal {
        padding: 0.5rem;
        font-size: 1.5rem;
        display: flex;
        align-items: center;
        justify-content: center;
    }

    .modal-body {
        padding: 1rem;
        overflow-x: hidden; 
        overflow-y: auto; /* Ensure vertical scrolling */
        flex: 1; /* Take all available space */
        width: 100%;
        -webkit-overflow-scrolling: touch; /* Smooth scroll on iOS */
    }

    .lesson-image {
        height: 80px; /* Compress image height on mobile */
        margin-bottom: 1rem;
    }

    .lesson-image svg {
        width: 32px;
        height: 32px;
    }

    .lesson-slide {
        padding-bottom: 1rem;
    }

    .lesson-slide h3 {
        font-size: 1.1rem;
        line-height: 1.3;
        margin-bottom: 0.75rem;
        word-wrap: break-word; 
        overflow-wrap: break-word; 
    }
    
    .modal-footer {
        padding: 0.75rem 1rem;
        /* Handle safe area for iPhone X+ home bar */
        padding-bottom: calc(0.75rem + env(safe-area-inset-bottom, 0px));
        background: #0f172a;
        flex-shrink: 0;
        z-index: 10;
    }
    
    .nav-btn {
        padding: 0.5rem 0.8rem;
        font-size: 0.85rem;
        min-width: unset; 
        flex: 0 0 auto;
    }

    .lesson-counter {
        font-size: 0.8rem;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
        flex: 1;
        text-align: center;
    }
}
