/* === CSS Variables === */
:root {
    --bg-primary: #0f0f1a;
    --bg-secondary: #1a1a2e;
    --bg-tertiary: #16213e;
    --text-primary: #ffffff;
    --text-secondary: #a0a0b0;
    --accent-green: #27ae60;
    --accent-green-dark: #1e8449;
    --accent-blue: #2980b9;
    --accent-blue-dark: #1f618d;
    --accent-yellow: #f1c40f;
    --accent-yellow-dark: #d4ac0d;
    --accent-red: #c0392b;
    --accent-red-dark: #922b21;
    --border-color: #2d2d4a;
    --shadow-color: rgba(0, 0, 0, 0.3);
    --link-color: rgba(255, 255, 255, 0.6);
}

/* === Reset & Base === */
* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

body {
    font-family: 'Inter', -apple-system, BlinkMacSystemFont, sans-serif;
    background: linear-gradient(135deg, var(--bg-primary) 0%, var(--bg-tertiary) 100%);
    color: var(--text-primary);
    min-height: 100vh;
    overflow-x: hidden;
}

/* === Header === */
.header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 1rem 2rem;
    background: var(--bg-secondary);
    border-bottom: 1px solid var(--border-color);
    box-shadow: 0 4px 20px var(--shadow-color);
    flex-wrap: wrap;
    gap: 1rem;
}

.header-content {
    display: flex;
    flex-direction: column;
}

.logo {
    font-size: 1.8rem;
    font-weight: 700;
    background: linear-gradient(90deg, var(--accent-green), var(--accent-blue));
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    background-clip: text;
}

.subtitle {
    color: var(--text-secondary);
    font-size: 0.9rem;
    margin-top: 0.25rem;
}

.header-actions {
    display: flex;
    gap: 0.75rem;
    align-items: center;
    flex-wrap: wrap;
}

.cluster-filter {
    padding: 0.6rem 1rem;
    background: var(--bg-tertiary);
    border: 1px solid var(--border-color);
    border-radius: 8px;
    color: var(--text-primary);
    font-size: 0.9rem;
    min-width: 180px;
}

/* === Status Indicator === */
.status-indicator {
    width: 24px;
    height: 24px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 1rem;
}

.status-indicator.loading {
    animation: pulse 1s infinite;
}

@keyframes pulse {

    0%,
    100% {
        opacity: 1;
    }

    50% {
        opacity: 0.5;
    }
}

/* === Buttons === */
.btn {
    padding: 0.6rem 1.2rem;
    border: none;
    border-radius: 8px;
    font-size: 0.9rem;
    font-weight: 500;
    cursor: pointer;
    transition: all 0.2s ease;
    display: inline-flex;
    align-items: center;
    gap: 0.5rem;
    white-space: nowrap;
}

.btn-primary {
    background: linear-gradient(135deg, var(--accent-blue), var(--accent-blue-dark));
    color: white;
}

.btn-primary:hover {
    transform: translateY(-2px);
    box-shadow: 0 4px 15px rgba(41, 128, 185, 0.4);
}

.btn-secondary {
    background: var(--bg-tertiary);
    color: var(--text-primary);
    border: 1px solid var(--border-color);
}

.btn-secondary:hover {
    background: var(--border-color);
}

.btn-danger {
    background: linear-gradient(135deg, var(--accent-red), var(--accent-red-dark));
    color: white;
}

.btn-danger:hover {
    transform: translateY(-2px);
    box-shadow: 0 4px 15px rgba(192, 57, 43, 0.4);
}

.btn-sm {
    padding: 0.4rem 0.8rem;
    font-size: 0.8rem;
}

/* === Main Layout === */
.main-content {
    display: flex;
    height: calc(100vh - 80px);
}

/* === Sidebar === */
.sidebar {
    width: 320px;
    background: var(--bg-secondary);
    border-right: 1px solid var(--border-color);
    padding: 1.5rem;
    overflow-y: auto;
    flex-shrink: 0;
}

.sidebar-section {
    margin-bottom: 2rem;
}

.sidebar-section h2 {
    font-size: 1rem;
    font-weight: 600;
    color: var(--text-secondary);
    text-transform: uppercase;
    letter-spacing: 0.5px;
    margin-bottom: 1rem;
    padding-bottom: 0.5rem;
    border-bottom: 1px solid var(--border-color);
}

.form-group {
    margin-bottom: 1rem;
}

.form-group label {
    display: block;
    font-size: 0.85rem;
    color: var(--text-secondary);
    margin-bottom: 0.4rem;
}

.form-group input,
.form-group select {
    width: 100%;
    padding: 0.7rem;
    background: var(--bg-tertiary);
    border: 1px solid var(--border-color);
    border-radius: 6px;
    color: var(--text-primary);
    font-size: 0.9rem;
    transition: border-color 0.2s ease;
}

.form-group input:focus,
.form-group select:focus {
    outline: none;
    border-color: var(--accent-blue);
}

.form-group input::placeholder {
    color: var(--text-secondary);
    opacity: 0.6;
}

.form-group input[type="color"] {
    padding: 0.3rem;
    height: 40px;
    cursor: pointer;
}

.form-row {
    display: flex;
    gap: 1rem;
    align-items: flex-end;
}

.form-row .form-group {
    flex: 1;
    margin-bottom: 0;
}

.form-row .btn {
    flex-shrink: 0;
}

/* === Legend === */
.legend {
    background: var(--bg-tertiary);
    padding: 1rem;
    border-radius: 8px;
}

.legend-item {
    display: flex;
    align-items: center;
    gap: 0.75rem;
    margin-bottom: 0.6rem;
    font-size: 0.85rem;
}

.legend-item:last-child {
    margin-bottom: 0;
}

.legend-color {
    width: 20px;
    height: 20px;
    border-radius: 4px;
    flex-shrink: 0;
}

.legend-color.main {
    background: var(--accent-green);
}

.legend-color.t1 {
    background: var(--accent-blue);
}

.legend-color.t2 {
    background: var(--accent-yellow);
}

.legend-color.t3 {
    background: var(--accent-red);
}

/* === Stats === */
.stats {
    background: var(--bg-tertiary);
    padding: 1rem;
    border-radius: 8px;
}

#statsContainer {
    display: flex;
    justify-content: space-around;
    text-align: center;
}

.stat-item {
    display: flex;
    flex-direction: column;
}

.stat-value {
    font-size: 1.5rem;
    font-weight: 700;
    color: var(--accent-blue);
}

.stat-label {
    font-size: 0.75rem;
    color: var(--text-secondary);
    text-transform: uppercase;
}

/* === PBN Container === */
.pbn-container {
    flex: 1;
    padding: 2rem;
    overflow: auto;
}

.pbn-canvas {
    position: relative;
    min-height: 800px;
    min-width: 100%;
}

.links-layer {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    pointer-events: none;
    z-index: 1;
}

.links-layer line {
    stroke: var(--link-color);
    stroke-width: 2;
    transition: stroke 0.2s ease;
    pointer-events: stroke;
    cursor: pointer;
}

.links-layer line:hover {
    stroke: var(--accent-blue);
    stroke-width: 3;
}

.links-layer line.highlight {
    stroke: var(--accent-green);
    stroke-width: 3;
}

/* === Tier Rows === */
.tier-row {
    margin-bottom: 3rem;
    position: relative;
    z-index: 2;
}

.tier-label {
    font-size: 0.75rem;
    font-weight: 600;
    color: var(--text-secondary);
    text-transform: uppercase;
    letter-spacing: 1px;
    margin-bottom: 1rem;
    opacity: 0.7;
}

.sites-container {
    display: flex;
    flex-wrap: wrap;
    gap: 1rem;
    justify-content: center;
    min-height: 60px;
    padding: 0.5rem;
    border-radius: 8px;
    background: rgba(255, 255, 255, 0.02);
}

/* === Site Cards === */
.site-card {
    position: relative;
    padding: 0.8rem 1.2rem;
    padding-right: 2.5rem;
    border-radius: 8px;
    font-weight: 600;
    font-size: 0.9rem;
    cursor: pointer;
    transition: all 0.2s ease;
    text-align: center;
    min-width: 80px;
    box-shadow: 0 4px 15px var(--shadow-color);
    z-index: 10;
}

.site-card:hover {
    transform: translateY(-3px);
    box-shadow: 0 8px 25px var(--shadow-color);
}

.site-card.main {
    background: linear-gradient(135deg, var(--accent-green), var(--accent-green-dark));
    color: white;
    padding: 1rem 2.5rem 1rem 2rem;
    font-size: 1rem;
}

.site-card.t1 {
    background: linear-gradient(135deg, var(--accent-blue), var(--accent-blue-dark));
    color: white;
}

.site-card.t2 {
    background: linear-gradient(135deg, var(--accent-yellow), var(--accent-yellow-dark));
    color: #1a1a2e;
}

.site-card.t3 {
    background: linear-gradient(135deg, var(--accent-red), var(--accent-red-dark));
    color: white;
}

.site-card .site-name {
    display: block;
}

.site-card .link-count {
    position: absolute;
    top: -8px;
    right: -8px;
    background: var(--bg-secondary);
    color: var(--text-primary);
    font-size: 0.7rem;
    padding: 2px 6px;
    border-radius: 10px;
    border: 1px solid var(--border-color);
}

.site-card .add-link-btn {
    position: absolute;
    right: 4px;
    top: 50%;
    transform: translateY(-50%);
    background: rgba(255, 255, 255, 0.2);
    border: none;
    border-radius: 4px;
    width: 24px;
    height: 24px;
    font-size: 0.8rem;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    opacity: 0;
    transition: opacity 0.2s ease, background 0.2s ease;
}

.site-card:hover .add-link-btn {
    opacity: 1;
}

.site-card .add-link-btn:hover {
    background: rgba(255, 255, 255, 0.4);
}

.site-card .cluster-badge {
    position: absolute;
    bottom: -8px;
    left: 50%;
    transform: translateX(-50%);
    font-size: 0.6rem;
    padding: 2px 6px;
    border-radius: 8px;
    white-space: nowrap;
    max-width: 100px;
    overflow: hidden;
    text-overflow: ellipsis;
    color: white;
}

/* === Empty State === */
.empty-state {
    text-align: center;
    padding: 2rem;
    color: var(--text-secondary);
    font-style: italic;
    opacity: 0.5;
}

/* === Modal === */
.modal {
    display: none;
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.7);
    z-index: 1000;
    align-items: center;
    justify-content: center;
    backdrop-filter: blur(4px);
}

.modal.active {
    display: flex;
}

.modal-content {
    background: var(--bg-secondary);
    padding: 2rem;
    border-radius: 12px;
    width: 90%;
    max-width: 450px;
    border: 1px solid var(--border-color);
    box-shadow: 0 20px 60px var(--shadow-color);
    animation: modalSlideIn 0.3s ease;
    max-height: 80vh;
    overflow-y: auto;
}

.modal-content.modal-large {
    max-width: 600px;
}

@keyframes modalSlideIn {
    from {
        opacity: 0;
        transform: translateY(-20px);
    }

    to {
        opacity: 1;
        transform: translateY(0);
    }
}

.modal-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 1.5rem;
}

.modal-header h2 {
    font-size: 1.2rem;
    font-weight: 600;
}

.modal-close {
    background: none;
    border: none;
    color: var(--text-secondary);
    font-size: 1.5rem;
    cursor: pointer;
    padding: 0.25rem;
    line-height: 1;
    transition: color 0.2s ease;
}

.modal-close:hover {
    color: var(--text-primary);
}

.modal-actions {
    display: flex;
    justify-content: space-between;
    margin-top: 1.5rem;
    padding-top: 1rem;
    border-top: 1px solid var(--border-color);
}

/* === Quick Link Modal === */
.quick-link-from {
    background: var(--bg-tertiary);
    padding: 1rem;
    border-radius: 8px;
    margin-bottom: 1rem;
    display: flex;
    align-items: center;
    gap: 0.75rem;
}

.quick-link-from .label {
    color: var(--text-secondary);
    font-size: 0.9rem;
}

.quick-link-from .site-name {
    font-weight: 600;
    color: var(--accent-blue);
}

/* === Link Details Modal === */
.link-detail-row {
    display: flex;
    padding: 0.75rem 0;
    border-bottom: 1px solid var(--border-color);
}

.link-detail-row:last-child {
    border-bottom: none;
}

.link-detail-label {
    font-weight: 600;
    color: var(--text-secondary);
    width: 140px;
    flex-shrink: 0;
}

.link-detail-value {
    color: var(--text-primary);
    word-break: break-all;
}

.link-url {
    color: var(--accent-blue);
    text-decoration: none;
}

.link-url:hover {
    text-decoration: underline;
}

.link-url.clickable {
    color: var(--accent-green);
    cursor: pointer;
}

.link-url.clickable:hover {
    text-decoration: underline;
}

/* === Cluster Management === */
.cluster-form {
    margin-bottom: 1.5rem;
    padding-bottom: 1.5rem;
    border-bottom: 1px solid var(--border-color);
}

.cluster-list {
    display: flex;
    flex-direction: column;
    gap: 0.75rem;
}

.cluster-item {
    display: flex;
    align-items: center;
    gap: 1rem;
    padding: 0.75rem 1rem;
    background: var(--bg-tertiary);
    border-radius: 8px;
    border: 1px solid var(--border-color);
}

.cluster-item .cluster-color {
    width: 24px;
    height: 24px;
    border-radius: 6px;
    flex-shrink: 0;
}

.cluster-item .cluster-name {
    flex: 1;
    font-weight: 500;
}

.cluster-item .cluster-count {
    color: var(--text-secondary);
    font-size: 0.85rem;
}

.cluster-item .btn-delete {
    background: none;
    border: none;
    color: var(--accent-red);
    cursor: pointer;
    font-size: 1.2rem;
    padding: 0.25rem;
    transition: transform 0.2s ease;
}

.cluster-item .btn-delete:hover {
    transform: scale(1.2);
}

/* === Responsive === */

/* Mobile Toggle Button */
.sidebar-toggle {
    display: none;
    position: fixed;
    bottom: 20px;
    right: 20px;
    width: 56px;
    height: 56px;
    border-radius: 50%;
    background: linear-gradient(135deg, var(--accent-blue), var(--accent-blue-dark));
    color: white;
    border: none;
    font-size: 1.5rem;
    cursor: pointer;
    z-index: 999;
    box-shadow: 0 4px 20px rgba(41, 128, 185, 0.5);
    transition: transform 0.3s ease;
}

.sidebar-toggle:hover {
    transform: scale(1.1);
}

.sidebar-toggle:active {
    transform: scale(0.95);
}

/* Mobile Navigation Bar */
.mobile-nav {
    display: none;
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    background: var(--bg-secondary);
    border-top: 1px solid var(--border-color);
    padding: 0.5rem;
    z-index: 998;
    justify-content: space-around;
    gap: 0.5rem;
}

.mobile-nav .nav-btn {
    flex: 1;
    padding: 0.6rem 0.5rem;
    background: var(--bg-tertiary);
    border: 1px solid var(--border-color);
    border-radius: 8px;
    color: var(--text-primary);
    font-size: 0.75rem;
    cursor: pointer;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 0.25rem;
}

.mobile-nav .nav-btn span {
    font-size: 1.2rem;
}

.mobile-nav .nav-btn:active {
    background: var(--accent-blue);
}

/* Tablet & Small Desktop */
@media (max-width: 1100px) {
    .sidebar {
        width: 280px;
    }

    .header-actions {
        gap: 0.5rem;
    }

    .btn {
        padding: 0.5rem 0.8rem;
        font-size: 0.85rem;
    }
}

/* Mobile - breakpoint increased to 992px */
@media (max-width: 992px) {

    /* Header */
    .header {
        padding: 0.75rem 1rem !important;
        flex-direction: row !important;
        justify-content: space-between !important;
        position: sticky !important;
        top: 0 !important;
        z-index: 100 !important;
    }

    .header-content {
        flex: 0 0 auto !important;
    }

    .logo {
        font-size: 1.3rem !important;
    }

    .subtitle {
        font-size: 0.75rem !important;
        display: none !important;
    }

    .header-actions {
        display: none !important;
    }

    /* Show mobile elements */
    .sidebar-toggle {
        display: flex !important;
        align-items: center !important;
        justify-content: center !important;
    }

    .mobile-nav {
        display: flex !important;
    }

    /* Main content - PBN visible first */
    .main-content {
        flex-direction: column-reverse !important;
        height: auto !important;
        min-height: calc(100vh - 50px) !important;
        padding-bottom: 80px !important;
    }

    /* Sidebar - HIDDEN by default on mobile */
    .sidebar {
        display: none !important;
        position: fixed !important;
        top: 0 !important;
        left: 0 !important;
        width: 100% !important;
        height: 100% !important;
        z-index: 1000 !important;
        padding-top: 20px !important;
        overflow-y: auto !important;
        background: var(--bg-secondary) !important;
    }

    /* Show sidebar when open */
    .sidebar.open {
        display: block !important;
    }

    /* Close button for sidebar */
    .sidebar::before {
        content: '✕ Fermer';
        position: absolute;
        top: 15px;
        right: 15px;
        padding: 0.5rem 1rem;
        background: var(--accent-red);
        color: white;
        border-radius: 6px;
        font-size: 0.9rem;
        font-weight: 500;
        cursor: pointer;
    }

    /* PBN Container - full width and height */
    .pbn-container {
        flex: 1;
        padding: 1rem;
        padding-bottom: 100px;
        overflow: auto;
        -webkit-overflow-scrolling: touch;
    }

    .pbn-canvas {
        min-height: 600px;
        min-width: 600px;
        /* Allow horizontal scroll for the diagram */
    }

    /* Tier rows more compact */
    .tier-row {
        margin-bottom: 2rem;
    }

    .tier-label {
        font-size: 0.7rem;
        margin-bottom: 0.5rem;
    }

    .sites-container {
        gap: 0.75rem;
        padding: 0.25rem;
    }

    /* Site cards - smaller on mobile */
    .site-card {
        padding: 0.6rem 0.9rem;
        padding-right: 2rem;
        font-size: 0.8rem;
        min-width: 60px;
    }

    .site-card.main {
        padding: 0.8rem 2rem 0.8rem 1.5rem;
        font-size: 0.9rem;
    }

    .site-card .add-link-btn {
        opacity: 1;
        /* Always visible on mobile */
        width: 20px;
        height: 20px;
        font-size: 0.7rem;
    }

    .site-card .cluster-badge {
        font-size: 0.5rem;
        padding: 1px 4px;
        max-width: 70px;
    }

    /* Modal - full screen on mobile */
    .modal-content {
        width: 95%;
        max-width: none;
        max-height: 90vh;
        margin: 1rem;
    }

    /* Stats always visible */
    .stats {
        position: fixed;
        bottom: 70px;
        left: 10px;
        right: 10px;
        padding: 0.5rem;
        border-radius: 8px;
        z-index: 50;
        display: none;
    }

    .stat-value {
        font-size: 1.2rem;
    }

    .stat-label {
        font-size: 0.65rem;
    }
}

/* Very small screens */
@media (max-width: 400px) {
    .logo {
        font-size: 1.1rem;
    }

    .site-card {
        padding: 0.5rem 0.7rem;
        padding-right: 1.8rem;
        font-size: 0.75rem;
        min-width: 50px;
    }

    .mobile-nav .nav-btn {
        font-size: 0.65rem;
        padding: 0.5rem 0.25rem;
    }
}

/* === Animations === */
@keyframes fadeIn {
    from {
        opacity: 0;
    }

    to {
        opacity: 1;
    }
}

.site-card {
    animation: fadeIn 0.3s ease;
}

/* Slide in animation for sidebar */
@keyframes slideIn {
    from {
        transform: translateX(-100%);
    }

    to {
        transform: translateX(0);
    }
}