body {
    background: #f0f2f5;
}

/* ── Map / panel layout ─────────────────────────────────────── */

.map-wrap {
    flex: 1;
    min-height: 0;
    position: relative;
}

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

/* ── Panel sheet ────────────────────────────────────────────── */

.panel-sheet {
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    z-index: var(--z-panel);
    background: var(--c-bg-card);
    border-radius: var(--radius-2xl) var(--radius-2xl) 0 0;
    box-shadow: 0 -4px 28px rgba(0, 0, 0, 0.18);
    display: flex;
    flex-direction: column;
    max-height: 88dvh;
    will-change: transform;
    touch-action: none;
}

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

.panel-sheet:not(.dragging) {
    transition: transform 0.28s cubic-bezier(0.32, 0.72, 0, 1);
}

.sheet-handle {
    flex-shrink: 0;
    padding: 10px 16px 6px;
    cursor: grab;
    touch-action: none;
    user-select: none;
}

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

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

.sheet-handle-title {
    text-align: center;
    font-size: var(--font-base);
    font-weight: 600;
    color: var(--c-text);
}

.sheet-body {
    flex: 1;
    overflow-y: auto;
    overflow-x: hidden;
    -webkit-overflow-scrolling: touch;
    padding: 0 16px 24px;
    min-height: 0;
    touch-action: pan-y;
    position: relative;
}

@media (min-width: 768px) {
    .map-wrap {
        flex: 1;
    }

    .panel-sheet {
        position: static;
        transform: none !important;
        width: 440px;
        max-width: 44vw;
        max-height: none;
        height: 100%;
        border-radius: 0;
        box-shadow: none;
        border-left: 2px solid var(--c-border);
        flex-shrink: 0;
    }

    .sheet-handle {
        display: none;
    }

    .sheet-body {
        padding: 16px 20px 28px;
    }
}

/* ── Sync / saving ──────────────────────────────────────────── */

.sync-status {
    font-size: var(--font-sm);
    padding: 8px 12px;
    border-radius: var(--radius-md);
    margin-bottom: 12px;
}

.sync-status.synced  { background: #d4edda; color: #155724; }
.sync-status.local   { background: #fff3cd; color: #856404; }
.sync-status.loading { background: #e2e3e5; color: #383d41; }
.sync-status.error   { background: #f8d7da; color: #721c24; }
.sync-status.saving  { background: #cce5ff; color: #004085; }

.sheet-saving {
    display: none;
    position: absolute;
    inset: 0;
    z-index: 60;
    background: rgba(255, 255, 255, 0.82);
    align-items: center;
    justify-content: center;
    flex-direction: column;
    gap: 14px;
    pointer-events: all;
}

.panel-sheet.is-saving .sheet-saving {
    display: flex;
}

.panel-sheet.is-saving .sheet-body {
    overflow: hidden;
}

.sheet-saving .loading-spinner {
    width: 36px;
    height: 36px;
}

.sheet-saving p {
    font-size: var(--font-base);
    font-weight: 600;
    color: var(--c-text);
}

/* ── Navigation / views ─────────────────────────────────────── */

.nav-tabs {
    display: flex;
    gap: 6px;
    margin-bottom: 12px;
}

.nav-tab {
    flex: 1;
    padding: 10px 6px;
    border: 2px solid var(--c-border);
    background: #f8f9fa;
    border-radius: var(--radius-lg);
    font-size: 13px;
    font-weight: 600;
    cursor: pointer;
    color: #555;
    font-family: inherit;
    transition: background 0.15s, color 0.15s, border-color 0.15s;
}

.nav-tab.active {
    background: var(--c-primary);
    color: white;
    border-color: var(--c-primary);
}

.panel-view {
    display: none;
}

.panel-view.active {
    display: block;
}

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

.panel-header h2 {
    flex: 1;
    font-size: 17px;
    font-weight: 600;
    color: var(--c-text);
}

.btn-back {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 36px;
    height: 36px;
    border: none;
    background: #ecf0f1;
    border-radius: var(--radius-md);
    cursor: pointer;
    color: var(--c-text);
    flex-shrink: 0;
}

/* ── Filters / controls ─────────────────────────────────────── */

.filter-bar {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
    margin-bottom: 12px;
    align-items: center;
}

.filter-bar select {
    flex: 1;
    min-width: 120px;
    padding: 8px 10px;
    border: 2px solid var(--c-border);
    border-radius: var(--radius-md);
    font-size: 13px;
    font-family: inherit;
}

.color-mode-toggle {
    display: flex;
    gap: 4px;
    width: 100%;
    margin-bottom: 14px;
}

.color-mode-btn {
    flex: 1;
    padding: 8px;
    border: 2px solid var(--c-border);
    background: var(--c-bg-card);
    border-radius: var(--radius-md);
    font-size: 11px;
    font-weight: 600;
    cursor: pointer;
    font-family: inherit;
    transition: background 0.15s, border-color 0.15s, color 0.15s;
}

.color-mode-btn.active {
    border-color: var(--c-accent);
    background: var(--c-accent-light);
    color: var(--c-accent-dark);
}

/* ── Buttons ────────────────────────────────────────────────── */

.btn-primary {
    display: block;
    width: 100%;
    padding: 13px;
    background: var(--c-primary);
    color: white;
    border: none;
    border-radius: var(--radius-lg);
    font-size: 15px;
    font-weight: 600;
    cursor: pointer;
    font-family: inherit;
    text-align: center;
}

.btn-primary:hover {
    background: var(--c-primary-light);
}

.btn-success {
    background: var(--c-success);
}

.btn-success:hover {
    background: var(--c-success-dark);
}

.btn-warning {
    background: var(--c-warning);
    color: white;
}

.btn-secondary {
    display: block;
    width: 100%;
    padding: 11px;
    background: #ecf0f1;
    color: var(--c-text);
    border: 2px solid #bdc3c7;
    border-radius: var(--radius-lg);
    font-size: var(--font-base);
    font-weight: 600;
    cursor: pointer;
    font-family: inherit;
    margin-top: 8px;
    text-align: center;
}

.btn-secondary:hover {
    background: var(--c-border);
}

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

.btn-row .btn-primary,
.btn-row .btn-secondary {
    flex: 1;
    margin-top: 0;
}

.btn-sm {
    padding: 6px 10px;
    font-size: var(--font-sm);
    border-radius: var(--radius-sm);
    border: none;
    cursor: pointer;
    font-family: inherit;
    font-weight: 600;
}

.btn-rework {
    background: #8e44ad;
    color: white;
}

/* ── Forms ──────────────────────────────────────────────────── */

label {
    display: block;
    margin: 10px 0 4px;
    font-weight: 600;
    font-size: 13px;
    color: #34495e;
}

input[type="text"],
input[type="date"],
select.full {
    width: 100%;
    padding: 10px 12px;
    border: 2px solid var(--c-border);
    border-radius: var(--radius-md);
    font-size: var(--font-base);
    font-family: inherit;
}

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

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

/* ── Lists ──────────────────────────────────────────────────── */

.teren-count {
    font-size: 13px;
    color: var(--c-text-secondary);
    margin-bottom: 10px;
}

.teren-list,
.person-list,
.group-list {
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.teren-item,
.person-item,
.group-item {
    background: #f8f9fa;
    border: 1px solid var(--c-border);
    border-radius: var(--radius-lg);
    padding: 12px;
    font-size: var(--font-base);
    position: relative;
}

.teren-item {
    background: #f8f9fa;
    border: 1px solid var(--c-border);
    border-left: 4px solid var(--terrain-color, var(--c-border));
    border-radius: var(--radius-lg);
    padding: 0;
    font-size: var(--font-base);
    position: relative;
    cursor: pointer;
    transition: border-color 0.15s, box-shadow 0.15s;
}

.teren-item.selected {
    border-left-color: var(--terrain-color, var(--c-accent));
    border-color: var(--terrain-color, var(--c-accent));
    box-shadow:
        0 0 0 1px var(--terrain-color, var(--c-accent)),
        0 2px 12px rgba(0, 0, 0, 0.06);
    background: #f8f9fa;
}

@supports (background: color-mix(in srgb, red 50%, white)) {
    .teren-item.selected {
        background: color-mix(in srgb, var(--terrain-color, var(--c-accent)) 4%, white);
    }
}

.teren-item.subteren {
    margin-left: 16px;
}

.teren-item.archived {
    opacity: 0.45;
}

.teren-item.map-hidden {
    opacity: 0.42;
    filter: grayscale(0.4);
    background: #f0f0f0;
}

.teren-item.map-hidden strong {
    color: #7f8c8d;
}

/* ── Teren item layout ── */

.teren-item-header {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 10px 12px;
}

.teren-item-id {
    display: flex;
    align-items: center;
    justify-content: center;
    min-width: 36px;
    height: 32px;
    padding: 0 8px;
    background: var(--c-bg-subtle);
    border: 1px solid var(--c-border);
    border-radius: var(--radius-md);
    font-size: 14px;
    font-weight: 700;
    color: var(--c-text);
    flex-shrink: 0;
    transition: background 0.15s, border-color 0.15s;
}

.teren-item.selected .teren-item-id {
    background: var(--c-accent-light);
    border-color: var(--terrain-color, var(--c-accent));
}

@supports (background: color-mix(in srgb, red 50%, white)) {
    .teren-item.selected .teren-item-id {
        background: color-mix(in srgb, var(--terrain-color, var(--c-accent)) 12%, white);
    }
}

.teren-item-body {
    flex: 1;
    min-width: 0;
}

.teren-item-body strong {
    font-size: var(--font-base);
    color: var(--c-text);
}

.teren-item-actions {
    display: flex;
    gap: 4px;
    flex-shrink: 0;
}

.action-btn {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 28px;
    height: 28px;
    border: none;
    border-radius: var(--radius-sm);
    cursor: pointer;
    font-size: 13px;
    font-weight: 700;
    line-height: 1;
    font-family: inherit;
}

.action-btn.hide {
    background: #ecf0f1;
    color: #566573;
}

.action-btn.hide.is-hidden {
    background: #d5dbdb;
    color: var(--c-text);
}

.badge {
    display: inline-block;
    font-size: 11px;
    padding: 2px 8px;
    border-radius: var(--radius-lg);
    background: #dfe6e9;
    color: var(--c-text);
    margin: 2px 4px 2px 0;
}

.badge-group {
    background: #d6eaf8;
    color: #1a5276;
}

.badge-brat {
    background: #fdebd0;
    color: #7d6608;
}

.teren-meta {
    font-size: var(--font-sm);
    color: var(--c-text-secondary);
    margin-top: 4px;
}

/* ── Empty state ────────────────────────────────────────────── */

.empty-state {
    font-size: 13px;
    color: #6c7a89;
    font-style: italic;
    padding: 10px 12px;
    background: var(--c-bg-card);
    border-radius: var(--radius-md);
    border: 1px dashed #b8c9d9;
    text-align: center;
}

/* ── Person / group expanders ───────────────────────────────── */

.person-item-header,
.group-item-header {
    display: flex;
    justify-content: space-between;
    align-items: flex-start;
    gap: 8px;
    cursor: pointer;
    user-select: none;
}

.person-item-chevron,
.group-item-chevron {
    font-size: 11px;
    color: var(--c-text-muted);
    line-height: 1.6;
    transition: transform 0.2s ease;
    flex-shrink: 0;
}

.person-item.expanded,
.group-item.expanded {
    border-color: var(--c-accent);
    background: var(--c-accent-light);
}

.person-item.expanded .person-item-chevron,
.group-item.expanded .group-item-chevron {
    transform: rotate(180deg);
}

.person-item-body,
.group-item-body {
    margin-top: 12px;
    padding: 14px 10px 10px;
    border-top: 1px solid #c5d9eb;
    background: rgba(255, 255, 255, 0.72);
    border-radius: var(--radius-md);
    cursor: default;
}

/* ── Group / brother detail ─────────────────────────────────── */

.group-section {
    margin-bottom: 14px;
}

.group-section:last-of-type {
    margin-bottom: 0;
}

.group-section-label {
    display: block;
    font-size: 11px;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    color: #4a6278;
    margin: 0 0 8px;
}

.group-chip-list {
    display: flex;
    flex-wrap: wrap;
    gap: 6px;
}

/* ── Chips ──────────────────────────────────────────────────── */

.chip {
    display: inline-flex;
    align-items: center;
    padding: 6px 12px;
    font-size: 13px;
    font-weight: 600;
    line-height: 1.2;
    background: var(--c-bg-card);
    border: 1px solid var(--c-border);
    border-radius: var(--radius-full);
    box-shadow: 0 1px 2px rgba(0, 0, 0, 0.06);
}

.chip--member {
    color: #1a5276;
    border-color: #85c1e9;
}

.chip--teren {
    color: #145a32;
    border-color: #7dcea0;
    border-radius: var(--radius-md);
    font-weight: 700;
    cursor: pointer;
    transition: background 0.15s, border-color 0.15s;
}

.chip--teren:hover {
    background: #eafaf1;
    border-color: var(--c-success);
}

.chip--completion {
    padding: 5px 10px;
    font-size: var(--font-sm);
    color: #6c3483;
    background: #f5eef8;
    border-color: #d7bde2;
    border-radius: var(--radius-full);
    margin: 4px 4px 0 0;
}

/* ── Brother detail ─────────────────────────────────────────── */

.brat-delete-btn {
    display: block;
    width: 100%;
    margin-top: 8px;
    padding: 10px 16px;
    border: 1px solid #f5b7b1;
    border-radius: var(--radius-lg);
    font-size: 13px;
    font-weight: 600;
    font-family: inherit;
    cursor: pointer;
    background: var(--c-bg-card);
    color: #c0392b;
    transition: background 0.15s, border-color 0.15s;
}

.brat-delete-btn:hover {
    background: var(--c-danger-light);
    border-color: #e6b0aa;
}

.brat-grupa-select {
    width: 100%;
    padding: 10px 12px;
    border: 2px solid #d4e6f1;
    border-radius: var(--radius-md);
    font-size: var(--font-base);
    font-family: inherit;
    background: var(--c-bg-card);
    color: var(--c-text);
}

.brat-teren-list {
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.brat-teren-card {
    padding: 10px 12px;
    background: var(--c-bg-card);
    border: 1px solid #d5e8d4;
    border-radius: var(--radius-md);
    box-shadow: 0 1px 2px rgba(20, 90, 50, 0.06);
}

.brat-teren-link {
    color: var(--c-accent-dark);
    text-decoration: none;
    font-weight: 600;
}

.brat-teren-link:hover {
    text-decoration: underline;
}

/* ── Assign buttons (reuse btn-primary btn-success) ─────────── */

.grupa-assign-btn,
.brat-primary-btn {
    margin-top: 14px;
}

/* ── Territory detail / history ─────────────────────────────── */

.cycle-history {
    margin: 12px 0;
}

.cycle-block {
    margin-bottom: 10px;
    padding: 12px;
    background: var(--c-bg-card);
    border: 1px solid #dce4ec;
    border-radius: var(--radius-md);
}

.cycle-block.active {
    border-color: var(--c-accent);
    box-shadow: 0 0 0 1px rgba(52, 152, 219, 0.25);
}

.cycle-block-title {
    font-size: 11px;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    color: #4a6278;
    margin: 0 0 10px;
}

.cycle-field {
    margin-bottom: 8px;
}

.cycle-field:last-child {
    margin-bottom: 0;
}

.cycle-field label {
    margin: 0 0 4px;
    font-size: var(--font-sm);
    font-weight: 600;
    color: #566573;
}

.cycle-field input[type="date"] {
    width: 100%;
    padding: 8px 10px;
    border: 2px solid #d4e6f1;
    border-radius: var(--radius-md);
    font-size: var(--font-base);
    font-family: inherit;
}

.period-przydzial-block {
    margin-bottom: 10px;
    padding: 10px 12px;
    background: #fef9e7;
    border: 1px solid #f4d03f;
    border-radius: var(--radius-md);
}

.period-przydzial-block strong {
    color: #7d6608;
}

.teren-detail-actions {
    display: flex;
    flex-direction: column;
    gap: 8px;
    margin-top: 14px;
}

.teren-detail-actions .btn-sm {
    width: 100%;
    padding: 10px 14px;
    font-size: 13px;
    border-radius: var(--radius-md);
}

/* ── Modal ──────────────────────────────────────────────────── */

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

.modal.open {
    display: flex;
}

.modal-box {
    background: var(--c-bg-card);
    border-radius: var(--radius-2xl);
    padding: 20px;
    max-width: 400px;
    width: 100%;
    max-height: 90dvh;
    overflow-y: auto;
}

.modal-box h3 {
    font-size: var(--font-lg);
    margin-bottom: 14px;
    color: var(--c-text);
}

/* ── Legend ─────────────────────────────────────────────────── */

.legend {
    display: flex;
    flex-wrap: wrap;
    gap: 10px 12px;
    font-size: 11px;
    margin-top: 4px;
    margin-bottom: 16px;
    padding-top: 12px;
    border-top: 1px solid #e8ecef;
}

.legend-item {
    display: flex;
    align-items: center;
    gap: 4px;
}

.legend-dot {
    width: 10px;
    height: 10px;
    border-radius: 50%;
    flex-shrink: 0;
}