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

html,
body {
    height: 100%;
    overflow: hidden;
}

body {
    font-family:
        -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
    background: #f5f5f5;
}

[hidden] {
    display: none !important;
}

/* ── Header ── */
.header {
    background: #2c3e50;
    color: white;
    padding: 10px 16px;
    display: flex;
    align-items: center;
    gap: 12px;
    flex-shrink: 0;
    z-index: 1500;
    position: relative;
}

.header-title {
    flex: 1;
    min-width: 0;
    text-align: center;
}

.header h1 {
    font-size: 18px;
    font-weight: 600;
}

.header .subtitle {
    font-size: 12px;
    opacity: 0.8;
    margin-top: 2px;
}

.header .subtitle:empty {
    display: none;
}

/* ── Auth section ── */
.header-auth {
    flex-shrink: 0;
    position: relative;
}

.btn-login {
    display: flex;
    align-items: center;
    gap: 6px;
    padding: 8px 12px;
    background: white;
    color: #2c3e50;
    border: none;
    border-radius: 8px;
    font-size: 13px;
    font-weight: 600;
    cursor: pointer;
    font-family: inherit;
}

.btn-login:hover {
    background: #f0f0f0;
}

.btn-login svg {
    width: 16px;
    height: 16px;
}

/* Avatar button */
.avatar-btn {
    position: relative;
    width: 38px;
    height: 38px;
    padding: 0;
    border: none;
    background: none;
    cursor: pointer;
    border-radius: 50%;
}

.avatar-btn img {
    width: 38px;
    height: 38px;
    border-radius: 50%;
    border: 2px solid rgba(255, 255, 255, 0.4);
    transition: border-color 0.15s;
}

.avatar-btn:hover img {
    border-color: rgba(255, 255, 255, 0.8);
}

/* Admin indicator - mała zielona kropka */
.avatar-btn .admin-dot {
    position: absolute;
    bottom: 0;
    right: 0;
    width: 12px;
    height: 12px;
    background: #27ae60;
    border: 2px solid #2c3e50;
    border-radius: 50%;
}

/* Dropdown menu */
.user-dropdown {
    position: absolute;
    top: calc(100% + 8px);
    right: 0;
    background: white;
    border-radius: 12px;
    box-shadow: 0 4px 24px rgba(0, 0, 0, 0.2);
    min-width: 200px;
    z-index: 2000;
    opacity: 0;
    visibility: hidden;
    transform: translateY(-8px);
    transition:
        opacity 0.15s,
        transform 0.15s,
        visibility 0.15s;
}

.user-dropdown.open {
    opacity: 1;
    visibility: visible;
    transform: translateY(0);
}

.user-dropdown::before {
    content: "";
    position: absolute;
    top: -6px;
    right: 14px;
    width: 12px;
    height: 12px;
    background: white;
    transform: rotate(45deg);
    border-radius: 2px;
}

.dropdown-header {
    padding: 12px 14px;
    border-bottom: 1px solid #eee;
}

.dropdown-name {
    font-size: 14px;
    font-weight: 600;
    color: #2c3e50;
}

.dropdown-email {
    font-size: 11px;
    color: #888;
    margin-top: 2px;
    word-break: break-all;
}

.dropdown-badge {
    display: inline-block;
    margin-top: 6px;
    padding: 2px 8px;
    background: #27ae60;
    color: white;
    font-size: 10px;
    font-weight: 600;
    border-radius: 4px;
}

.dropdown-item {
    display: block;
    width: 100%;
    padding: 12px 14px;
    border: none;
    background: none;
    text-align: left;
    font-size: 14px;
    color: #2c3e50;
    cursor: pointer;
    font-family: inherit;
    transition: background 0.1s;
}

.dropdown-item:hover {
    background: #f5f5f5;
}

.dropdown-item:last-child {
    border-radius: 0 0 12px 12px;
}

.dropdown-item.logout {
    color: #e74c3c;
}

/* ── Main layout ── */
.app {
    display: flex;
    flex-direction: column;
    height: 100dvh;
}

.main-layout {
    flex: 1;
    display: flex;
    flex-direction: column;
    min-height: 0;
    overflow: hidden;
    position: relative;
}

#map {
    flex: 1;
    min-height: 0;
}

/* ── Bottom Sheet (mobile) ── */
.bottom-sheet {
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    z-index: 500;
    background: white;
    border-radius: 20px 20px 0 0;
    box-shadow: 0 -4px 24px rgba(0, 0, 0, 0.15);
    max-height: 85dvh;
    display: flex;
    flex-direction: column;
    transform: translateY(calc(100% - 60px));
    transition: transform 0.3s cubic-bezier(0.32, 0.72, 0, 1);
    touch-action: none;
}

.bottom-sheet.expanded {
    transform: translateY(0);
}

.bottom-sheet.dragging {
    transition: none;
}

.sheet-handle {
    padding: 12px 16px 8px;
    cursor: grab;
    flex-shrink: 0;
    touch-action: none;
}

.sheet-handle:active {
    cursor: grabbing;
}

.sheet-handle-bar {
    width: 36px;
    height: 4px;
    background: #ddd;
    border-radius: 2px;
    margin: 0 auto 8px;
}

.sheet-handle-title {
    font-size: 15px;
    font-weight: 600;
    color: #2c3e50;
    text-align: center;
}

.sheet-content {
    flex: 1;
    overflow-y: auto;
    overflow-x: hidden;
    -webkit-overflow-scrolling: touch;
    padding: 0 16px 24px;
    overscroll-behavior: contain;
}

/* ── Desktop layout ── */
@media (min-width: 768px) {
    .main-layout {
        flex-direction: row;
    }

    #map {
        flex: 1;
    }

    .bottom-sheet {
        position: static;
        transform: none !important;
        width: 400px;
        max-width: 45vw;
        max-height: none;
        height: 100%;
        border-radius: 0;
        box-shadow: none;
        border-left: 1px solid #e0e0e0;
        flex-shrink: 0;
    }

    .sheet-handle {
        display: none;
    }

    .sheet-content {
        padding: 16px 20px 24px;
    }
}

/* ── Panel content ── */
.browse-toolbar {
    display: flex;
    flex-direction: column;
    gap: 10px;
    margin-bottom: 12px;
}

.browse-search {
    width: 100%;
    padding: 12px 14px;
    font-size: 15px;
    border: 1px solid #ddd;
    border-radius: 10px;
    background: white;
    color: #2c3e50;
    box-shadow: 0 1px 4px rgba(0, 0, 0, 0.06);
}

.browse-search:focus {
    outline: none;
    border-color: #356ac3;
    box-shadow: 0 0 0 3px rgba(53, 106, 195, 0.15);
}

.browse-sort {
    display: flex;
    align-items: center;
    gap: 8px;
}

.browse-sort-label {
    font-size: 13px;
    color: #777;
}

.browse-sort select {
    flex: 1;
    padding: 8px 10px;
    font-size: 14px;
    border: 1px solid #ddd;
    border-radius: 8px;
    background: white;
}

.browse-count {
    font-size: 13px;
    color: #777;
    margin-bottom: 12px;
    text-align: center;
}

/* ── Terrain list ── */
.terrain-list {
    list-style: none;
    display: flex;
    flex-direction: column;
    gap: 10px;
}

.terrain-item {
    display: flex;
    align-items: stretch;
    background: white;
    border-radius: 12px;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.08);
    border: 1px solid #e8e8e8;
    overflow: hidden;
}

.terrain-item.selected {
    border-color: #356ac3;
    box-shadow: 0 2px 12px rgba(53, 106, 195, 0.2);
}

.terrain-select {
    flex: 1;
    display: block;
    text-align: left;
    border: none;
    background: transparent;
    padding: 12px 14px;
    cursor: pointer;
    color: #2c3e50;
    font-size: 14px;
    font-weight: 600;
    font-family: inherit;
}

.terrain-select:active {
    background: #f8f8f8;
}

.terrain-item .tid {
    display: block;
    font-size: 11px;
    font-weight: 500;
    color: #999;
    margin-top: 3px;
}

.terrain-item .terrain-desc {
    font-size: 11px;
    color: #888;
    margin-top: 2px;
    font-style: italic;
    font-weight: 400;
}

.terrain-item .terrain-color-dot {
    display: inline-block;
    width: 8px;
    height: 8px;
    border-radius: 50%;
    margin-left: 6px;
    vertical-align: middle;
}

.terrain-actions {
    display: flex;
    align-items: center;
    border-left: 1px solid #e8e8e8;
    flex-shrink: 0;
}

.terrain-open {
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 0 12px;
    color: #356ac3;
    text-decoration: none;
}

.terrain-open:hover {
    background: #f0f4fb;
}

.admin-actions {
    display: none;
    gap: 4px;
    padding: 0 6px;
    align-items: center;
}

body.is-admin .admin-actions {
    display: flex;
}

.action-btn {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 28px;
    height: 28px;
    border: none;
    border-radius: 6px;
    cursor: pointer;
    color: white;
    font-size: 12px;
    font-weight: 700;
}

.action-btn.qr {
    background: #3498db;
}

.action-btn.edit {
    background: #7f8c8d;
}

.action-btn.delete {
    background: #e74c3c;
}

/* Create new */
.create-new {
    display: none;
}

body.is-admin .create-new {
    display: block;
}

.create-new-inner {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 14px 16px;
    border: 2px dashed #27ae60;
    background: #f0faf4;
    color: #1e8449;
    border-radius: 12px;
    cursor: pointer;
}

.create-new-inner:hover {
    background: #e8f8f0;
}

.create-icon {
    width: 32px;
    height: 32px;
    border-radius: 50%;
    background: linear-gradient(135deg, #27ae60, #1e8449);
    color: white;
    font-size: 20px;
    line-height: 32px;
    text-align: center;
}

.create-label {
    font-size: 14px;
    font-weight: 600;
}

/* ── Editor Panel ── */
.editor-panel {
    background: #f8f9fa;
    border-radius: 12px;
    padding: 16px;
    margin-bottom: 16px;
    border: 1px solid #e0e0e0;
    position: relative;
}

.editor-header {
    display: flex;
    align-items: center;
    gap: 10px;
    margin-bottom: 14px;
}

.editor-header h3 {
    flex: 1;
    font-size: 15px;
    font-weight: 600;
    color: #2c3e50;
}

.btn-back {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 32px;
    height: 32px;
    border: none;
    background: #e0e0e0;
    border-radius: 8px;
    cursor: pointer;
    color: #2c3e50;
}

.btn-back:hover {
    background: #d0d0d0;
}

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

.form-group label {
    display: block;
    margin-bottom: 4px;
    font-weight: 600;
    font-size: 12px;
    color: #555;
}

.form-group input[type="text"] {
    width: 100%;
    padding: 10px 12px;
    border: 1px solid #ddd;
    border-radius: 8px;
    font-size: 14px;
    font-family: inherit;
    background: white;
}

.form-group input:focus {
    border-color: #3498db;
    outline: none;
}

.color-row {
    display: flex;
    gap: 8px;
    align-items: center;
}

.color-row input[type="color"] {
    width: 44px;
    height: 36px;
    border: 1px solid #ddd;
    border-radius: 6px;
    cursor: pointer;
    padding: 2px;
}

.color-row input[type="text"] {
    flex: 1;
}

/* ── Buttons ── */
.btn-primary {
    display: block;
    width: 100%;
    padding: 12px;
    background: #2c3e50;
    color: white;
    border: none;
    border-radius: 10px;
    font-size: 14px;
    font-weight: 600;
    cursor: pointer;
    font-family: inherit;
}

.btn-primary:hover {
    background: #34495e;
}

.btn-primary:disabled {
    background: #95a5a6;
    cursor: not-allowed;
}

.btn-success {
    background: #27ae60;
}

.btn-success:hover {
    background: #1e8449;
}

.btn-secondary {
    display: block;
    width: 100%;
    padding: 10px;
    background: white;
    color: #2c3e50;
    border: 1px solid #ccc;
    border-radius: 10px;
    font-size: 14px;
    font-weight: 600;
    cursor: pointer;
    font-family: inherit;
}

.btn-secondary:hover {
    background: #f5f5f5;
}

.btn-draw {
    background: #3498db;
    color: white;
    border-color: #2980b9;
}

.btn-draw:hover {
    background: #2980b9;
}

.btn-row {
    display: flex;
    gap: 8px;
    margin-top: 12px;
}

.btn-row>* {
    flex: 1;
}

/* ── Draw banner ── */
.draw-banner {
    position: absolute;
    top: 12px;
    left: 50%;
    transform: translateX(-50%);
    z-index: 800;
    background: rgba(44, 62, 80, 0.92);
    color: white;
    padding: 10px 18px;
    border-radius: 10px;
    font-size: 13px;
    font-weight: 500;
    max-width: calc(100% - 32px);
    text-align: center;
}

/* ── QR modal ── */
.qr-modal {
    position: fixed;
    inset: 0;
    z-index: 2000;
    background: rgba(0, 0, 0, 0.5);
    display: none;
    align-items: center;
    justify-content: center;
    padding: 20px;
}

.qr-modal.open {
    display: flex;
}

.qr-modal-box {
    background: white;
    border-radius: 16px;
    padding: 24px;
    max-width: 300px;
    width: 100%;
    text-align: center;
}

.qr-modal-box h3 {
    font-size: 15px;
    margin-bottom: 16px;
    color: #2c3e50;
}

#qr-output img {
    border: 6px solid white;
    box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
}

.qr-modal-close {
    margin-top: 16px;
}

/* ── Loading (pełnoekranowy overlay) ── */
.loading-overlay {
    position: fixed;
    inset: 0;
    z-index: 3000;
    background: rgba(240, 242, 245, 0.94);
    display: none;
    align-items: center;
    justify-content: center;
}

.loading-overlay.visible {
    display: flex;
}

.loading-box {
    text-align: center;
    padding: 32px 24px;
}

.loading-spinner {
    width: 40px;
    height: 40px;
    border: 3px solid #e0e0e0;
    border-top-color: #3498db;
    border-radius: 50%;
    animation: spin 0.75s linear infinite;
    margin: 0 auto 16px;
}

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

.loading-box p {
    font-size: 14px;
    color: #2c3e50;
    font-weight: 500;
}

/* ── Inline saving overlay (w panelu edytora) ── */
.saving-overlay {
    position: absolute;
    inset: 0;
    z-index: 10;
    background: rgba(255, 255, 255, 0.85);
    display: flex;
    align-items: center;
    justify-content: center;
    flex-direction: column;
    gap: 12px;
    border-radius: 12px;
}

.saving-overlay .loading-spinner {
    width: 32px;
    height: 32px;
    margin: 0;
}

.saving-overlay p {
    font-size: 13px;
    font-weight: 600;
    color: #2c3e50;
}

/* ── Leaflet ── */
.leaflet-control-layers {
    border-radius: 8px !important;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.2) !important;
}