:root {
    --primary: #dc2626;
    --primary-hover: #b91c1c;
    --primary-light: #fef2f2;
    --success: #dc2626;
    --success-hover: #b91c1c;
    --success-light: #fef2f2;
    --danger: #dc2626;
    --danger-hover: #b91c1c;
    --danger-light: #fef2f2;
    --warning: #dc2626;
    --purple: #dc2626;
    --purple-hover: #b91c1c;
    --gray-50: #f9fafb;
    --gray-100: #f3f4f6;
    --gray-200: #e5e7eb;
    --gray-300: #d1d5db;
    --gray-400: #9ca3af;
    --gray-500: #6b7280;
    --gray-600: #4b5563;
    --gray-700: #374151;
    --gray-800: #1f2937;
    --gray-900: #111827;
    --shadow-sm: 0 1px 2px 0 rgb(0 0 0 / 0.05);
    --shadow: 0 1px 3px 0 rgb(0 0 0 / 0.1), 0 1px 2px -1px rgb(0 0 0 / 0.1);
    --shadow-md: 0 4px 6px -1px rgb(0 0 0 / 0.1), 0 2px 4px -2px rgb(0 0 0 / 0.1);
    --shadow-lg: 0 10px 15px -3px rgb(0 0 0 / 0.1), 0 4px 6px -4px rgb(0 0 0 / 0.1);
    --shadow-xl: 0 20px 25px -5px rgb(0 0 0 / 0.1), 0 8px 10px -6px rgb(0 0 0 / 0.1);
    --radius: 0.5rem;
    --radius-lg: 0.75rem;
}

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

body {
    font-family: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
    color: var(--gray-800);
    line-height: 1.5;
    background: #f5f5f5;
}

#map {
    width: 100%;
    height: 100vh;
}

.controls {
    position: absolute;
    top: 12px;
    right: 12px;
    z-index: 1000;
    background: white;
    padding: 0;
    border-radius: var(--radius-lg);
    box-shadow: var(--shadow-lg);
    width: 320px;
    max-height: calc(100vh - 24px);
    overflow: hidden;
    display: flex;
    flex-direction: column;
}

.controls-header {
    padding: 16px 20px;
    background: linear-gradient(135deg, #1f2937 0%, #111827 100%);
    color: white;
}

.controls-header h3 {
    font-size: 16px;
    font-weight: 600;
    margin-bottom: 4px;
}

.controls-header p {
    font-size: 12px;
    opacity: 0.85;
}

.controls-body {
    padding: 16px;
    overflow-y: auto;
    flex: 1;
}

.controls-section {
    margin-bottom: 16px;
}

.controls-section:last-child {
    margin-bottom: 0;
}

.section-title {
    font-size: 11px;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    color: var(--gray-500);
    margin-bottom: 8px;
}

.controls button {
    width: 100%;
    padding: 10px 14px;
    margin: 0;
    border: none;
    border-radius: var(--radius);
    cursor: pointer;
    font-size: 13px;
    font-weight: 500;
    font-family: inherit;
    transition: all 0.15s ease;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
}

.btn-group {
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.btn-toggle {
    background: var(--gray-100);
    color: var(--gray-700);
    border: 1px solid var(--gray-200) !important;
}

.btn-toggle:hover {
    background: var(--gray-200);
}

.btn-toggle.active {
    background: var(--success-light);
    color: var(--success);
    border-color: var(--success) !important;
}

.btn-clear {
    background: var(--danger-light);
    color: var(--danger);
}

.btn-clear:hover {
    background: var(--danger);
    color: white;
}

.btn-pdf {
    background: linear-gradient(135deg, #1f2937 0%, #111827 100%);
    color: white;
    font-weight: 600;
    padding: 12px 14px;
}

.btn-pdf:hover {
    background: linear-gradient(135deg, #374151 0%, #1f2937 100%);
    transform: translateY(-1px);
    box-shadow: var(--shadow-md);
}

.btn-pdf:disabled {
    background: var(--gray-300);
    cursor: not-allowed;
    transform: none;
    box-shadow: none;
}

.btn-icon {
    width: 18px;
    height: 18px;
    flex-shrink: 0;
}

.selected-list {
    max-height: 150px;
    overflow-y: auto;
    margin-top: 8px;
}

.selected-list:empty::before {
    content: 'Brak zaznaczonych działek';
    display: block;
    text-align: center;
    color: var(--gray-400);
    font-size: 12px;
    padding: 12px;
    background: var(--gray-50);
    border-radius: var(--radius);
    border: 1px dashed var(--gray-200);
}

.selected-item {
    padding: 8px 10px;
    margin-bottom: 4px;
    background: var(--gray-50);
    border-radius: var(--radius);
    font-size: 12px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    border: 1px solid var(--gray-100);
}

.selected-item:last-child {
    margin-bottom: 0;
}

.selected-item .parcel-info {
    display: flex;
    flex-direction: column;
    gap: 4px;
    flex: 1;
    min-width: 0;
}

.selected-item span {
    font-family: 'SF Mono', 'Monaco', 'Inconsolata', monospace;
    font-size: 11px;
    color: var(--gray-700);
    word-break: break-all;
}

.selected-item .forest-badge {
    font-size: 10px;
    color: #166534;
    background: #dcfce7;
    padding: 2px 6px;
    border-radius: 4px;
    display: inline-flex;
    align-items: flex-start;
    gap: 4px;
    max-width: 100%;
    flex-wrap: wrap;
    line-height: 1.4;
}

.selected-item button {
    width: auto;
    padding: 4px 8px;
    font-size: 10px;
    background: transparent;
    color: var(--danger);
    border: 1px solid var(--danger) !important;
    flex-shrink: 0;
}

.selected-item button:hover {
    background: var(--danger);
    color: white;
}

.forest-info-section {
    margin-top: 12px;
    padding: 12px;
    background: linear-gradient(135deg, #dcfce7 0%, #bbf7d0 100%);
    border-radius: var(--radius);
    border: 1px solid #86efac;
    display: none;
}

.forest-info-section.visible {
    display: block;
}

.forest-info-header {
    display: flex;
    align-items: center;
    gap: 8px;
    margin-bottom: 8px;
}

.forest-info-header span {
    font-size: 18px;
}

.forest-info-header h4 {
    font-size: 12px;
    font-weight: 600;
    color: #166534;
    margin: 0;
}

.forest-info-text {
    font-size: 11px;
    color: #15803d;
    line-height: 1.4;
}

.author-section {
    padding-top: 16px;
    border-top: 1px solid var(--gray-200);
}

.form-group {
    margin-bottom: 12px;
}

.form-group:last-of-type {
    margin-bottom: 16px;
}

.form-group label {
    display: block;
    font-size: 12px;
    font-weight: 500;
    color: var(--gray-600);
    margin-bottom: 4px;
}

.form-group input {
    width: 100%;
    padding: 10px 12px;
    border: 1px solid var(--gray-200);
    border-radius: var(--radius);
    font-size: 13px;
    font-family: inherit;
    transition: all 0.15s ease;
    background: white;
}

.form-group input:focus {
    outline: none;
    border-color: var(--primary);
    box-shadow: 0 0 0 3px var(--primary-light);
}

.form-group input::placeholder {
    color: var(--gray-400);
}

.status-bar {
    position: absolute;
    bottom: 12px;
    left: 12px;
    z-index: 900;
    background: rgba(255, 255, 255, 0.95);
    backdrop-filter: blur(8px);
    padding: 8px 14px;
    border-radius: var(--radius);
    font-size: 11px;
    box-shadow: var(--shadow-md);
    display: flex;
    align-items: center;
    gap: 12px;
    font-family: 'SF Mono', 'Monaco', 'Inconsolata', monospace;
}

.status-bar .coords {
    color: var(--gray-600);
}

.status-bar .selected-count {
    color: var(--primary);
    font-weight: 600;
}

.status-divider {
    width: 1px;
    height: 16px;
    background: var(--gray-300);
}

.attribution-control {
    position: absolute;
    bottom: 12px;
    right: 12px;
    z-index: 999;
    background: rgba(255, 255, 255, 0.85);
    backdrop-filter: blur(4px);
    padding: 6px 10px;
    border-radius: var(--radius);
    font-size: 10px;
    color: var(--gray-500);
    max-width: 280px;
}

.init-loading {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: linear-gradient(135deg, #1f2937 0%, #111827 100%);
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    z-index: 3000;
    color: white;
}

.init-loading.hidden {
    display: none;
}

.init-loading-spinner {
    width: 48px;
    height: 48px;
    border: 4px solid rgba(255, 255, 255, 0.3);
    border-top-color: white;
    border-radius: 50%;
    animation: spin 1s linear infinite;
    margin-bottom: 16px;
}

.init-loading-text {
    font-size: 16px;
    font-weight: 500;
}

@keyframes spin {
    to {
        transform: rotate(360deg);
    }
}

.loading {
    position: fixed;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    background: white;
    padding: 24px 32px;
    border-radius: var(--radius-lg);
    box-shadow: var(--shadow-xl);
    z-index: 2000;
    display: none;
    text-align: center;
}

.loading.show {
    display: block;
}

.loading-spinner {
    width: 32px;
    height: 32px;
    border: 3px solid var(--gray-200);
    border-top-color: #dc2626;
    border-radius: 50%;
    animation: spin 1s linear infinite;
    margin: 0 auto 12px;
}

.loading-text {
    font-size: 14px;
    color: var(--gray-600);
}

.pdf-generating {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(0, 0, 0, 0.6);
    backdrop-filter: blur(4px);
    display: flex;
    justify-content: center;
    align-items: center;
    z-index: 5000;
}

.pdf-generating.hidden {
    display: none;
}

.pdf-generating-content {
    background: white;
    padding: 32px 48px;
    border-radius: var(--radius-lg);
    text-align: center;
    box-shadow: var(--shadow-xl);
}

.pdf-generating-content .loading-spinner {
    width: 40px;
    height: 40px;
    margin-bottom: 16px;
}

.pdf-generating-content h3 {
    font-size: 18px;
    font-weight: 600;
    color: var(--gray-800);
    margin-bottom: 8px;
}

.pdf-generating-content p {
    font-size: 14px;
    color: var(--gray-500);
}

.modal-overlay {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(0, 0, 0, 0.5);
    backdrop-filter: blur(4px);
    display: flex;
    justify-content: center;
    align-items: center;
    z-index: 4000;
    padding: 20px;
}

.modal-overlay.hidden {
    display: none;
}

.modal {
    background: white;
    border-radius: var(--radius-lg);
    box-shadow: var(--shadow-xl);
    max-width: 480px;
    width: 100%;
    max-height: 80vh;
    /* Fallback */
    max-height: 80dvh;
    /* Dynamic viewport height for mobile browsers */
    display: flex;
    /* Enable flex layout */
    flex-direction: column;
    /* Stack children vertically */
    overflow: hidden;
    animation: modalIn 0.2s ease-out;
    margin: auto;
    /* Ensure centering */
}

@keyframes modalIn {
    from {
        opacity: 0;
        transform: scale(0.95) translateY(10px);
    }

    to {
        opacity: 1;
        transform: scale(1) translateY(0);
    }
}

.modal-header {
    padding: 24px 24px 0;
    text-align: center;
}

.modal-icon {
    width: 64px;
    height: 64px;
    background: var(--primary-light);
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    margin: 0 auto 16px;
    font-size: 32px;
}

.modal-header h2 {
    font-size: 20px;
    font-weight: 700;
    color: var(--gray-900);
    margin-bottom: 8px;
}

.modal-header p {
    font-size: 14px;
    color: var(--gray-500);
}

.modal-body {
    padding: 24px;
    overflow-y: auto;
    /* Enable scrolling */
    flex: 1;
    /* Take remaining space */
}

.instruction-list {
    list-style: none;
}

.instruction-item {
    display: flex;
    align-items: flex-start;
    gap: 12px;
    padding: 12px 0;
    border-bottom: 1px solid var(--gray-100);
}

.instruction-item:last-child {
    border-bottom: none;
}

.instruction-number {
    width: 28px;
    height: 28px;
    background: var(--primary);
    color: white;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 13px;
    font-weight: 600;
    flex-shrink: 0;
}

.instruction-text {
    font-size: 14px;
    color: var(--gray-700);
    padding-top: 4px;
}

.modal-footer {
    padding: 16px 24px 24px;
}

.modal-footer button {
    width: 100%;
    padding: 12px;
    background: var(--primary);
    color: white;
    border: none;
    border-radius: var(--radius);
    font-size: 14px;
    font-weight: 600;
    cursor: pointer;
    font-family: inherit;
    transition: all 0.15s ease;
}

.modal-footer button:hover {
    background: var(--primary-hover);
}

.modal-checkbox {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    margin-top: 12px;
    font-size: 13px;
    color: var(--gray-500);
}

.modal-checkbox input {
    width: 16px;
    height: 16px;
    cursor: pointer;
}

.help-btn {
    position: absolute;
    top: 12px;
    left: 12px;
    z-index: 1000;
    width: 40px;
    height: 40px;
    background: white;
    border: none;
    border-radius: 50%;
    box-shadow: var(--shadow-md);
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 18px;
    color: var(--gray-600);
    transition: all 0.15s ease;
}

.help-btn:hover {
    background: var(--gray-50);
    color: var(--primary);
}

@media (max-width: 768px) {
    .controls {
        position: fixed;
        top: auto;
        bottom: 0;
        left: 0;
        right: 0;
        width: 100%;
        max-height: 75vh;
        border-radius: var(--radius-lg) var(--radius-lg) 0 0;
        transform: translateY(calc(100% - 90px));
        transition: transform 0.3s ease;
    }

    .controls.expanded {
        transform: translateY(0);
    }

    .controls.bounce {
        animation: panelBounce 2s ease-in-out;
    }

    @keyframes panelBounce {

        0%,
        100% {
            transform: translateY(calc(100% - 90px));
        }

        15% {
            transform: translateY(calc(100% - 130px));
        }

        30% {
            transform: translateY(calc(100% - 90px));
        }

        45% {
            transform: translateY(calc(100% - 120px));
        }

        60% {
            transform: translateY(calc(100% - 90px));
        }

        75% {
            transform: translateY(calc(100% - 110px));
        }

        90% {
            transform: translateY(calc(100% - 90px));
        }
    }

    .controls-header {
        cursor: pointer;
        position: relative;
        padding-top: 20px;
    }

    .controls-header::before {
        content: '';
        position: absolute;
        top: 8px;
        left: 50%;
        transform: translateX(-50%);
        width: 40px;
        height: 4px;
        background: rgba(255, 255, 255, 0.4);
        border-radius: 2px;
    }

    .controls-header .help-btn-mobile {
        position: absolute;
        top: 16px;
        right: 16px;
        width: 28px !important;
        min-width: 28px !important;
        max-width: 28px !important;
        height: 28px !important;
        padding: 0 !important;
        margin: 0 !important;
        background: rgba(255, 255, 255, 0.2) !important;
        border: none !important;
        border-radius: 50% !important;
        cursor: pointer;
        display: flex !important;
        align-items: center;
        justify-content: center;
        color: white !important;
        transition: background 0.15s ease;
    }

    .controls-header .help-btn-mobile:hover {
        background: rgba(255, 255, 255, 0.3) !important;
    }

    .controls-header .help-btn-header {
        display: none !important;
    }

    .status-bar {
        bottom: 100px;
        left: 12px;
        right: 12px;
        justify-content: center;
        z-index: 900;
    }

    .attribution-control {
        display: none;
    }

    .help-btn {
        display: none;
    }

    .modal {
        max-width: 100%;
        margin: 0 12px;
    }
}

@media (min-width: 769px) {
    .controls-header {
        position: relative;
    }

    .controls-header .help-btn-header {
        position: absolute;
        top: 12px;
        right: 12px;
        width: 28px !important;
        min-width: 28px !important;
        max-width: 28px !important;
        height: 28px !important;
        padding: 0 !important;
        margin: 0 !important;
        background: rgba(255, 255, 255, 0.2) !important;
        border: none !important;
        border-radius: 50% !important;
        cursor: pointer;
        display: flex !important;
        align-items: center;
        justify-content: center;
        color: white !important;
        transition: background 0.15s ease;
    }

    .controls-header .help-btn-mobile {
        display: none !important;
    }

    .controls-header .help-btn-header:hover {
        background: rgba(255, 255, 255, 0.3);
    }

    .help-btn {
        display: none;
    }

    .help-btn-mobile {
        display: none !important;
    }
}

@media (max-width: 480px) {
    .status-bar {
        flex-wrap: wrap;
        gap: 4px;
        font-size: 10px;
    }

    .status-divider {
        display: none;
    }
}