:root {
    --bg-page: #f8fafc;
    --bg-surface: #ffffff;
    --text-primary: #0f172a;
    --text-secondary: #64748b;
    --border-color: #e2e8f0;
    --primary-color: #3b82f6;
    --primary-hover: #2563eb;
    --radius-lg: 12px;
    --radius-md: 8px;
    --shadow-sm: 0 1px 2px 0 rgb(0 0 0 / 0.05);
    --shadow-md: 0 4px 6px -1px rgb(0 0 0 / 0.1), 0 2px 4px -2px rgb(0 0 0 / 0.1);
    --shadow-hover: 0 10px 15px -3px rgb(0 0 0 / 0.1), 0 4px 6px -4px rgb(0 0 0 / 0.1);
    --transition-fast: 0.15s ease-in-out;
    --transition-normal: 0.3s ease;
}

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

body {
    font-family: 'Inter', sans-serif;
    background-color: var(--bg-page);
    color: var(--text-primary);
    line-height: 1.5;
    height: 100vh;
    height: 100dvh;
    display: flex;
    flex-direction: column;
    overflow: hidden;
}

/* --- App Header --- */
.app-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 1rem 2rem;
    background-color: var(--bg-surface);
    border-bottom: 1px solid var(--border-color);
    box-shadow: var(--shadow-sm);
    z-index: 10;
}

.header-left {
    display: flex;
    align-items: center;
    gap: 0.5rem;
}

.header-left i {
    font-size: 1.5rem;
    color: var(--primary-color);
}

.header-left h1 {
    font-size: 1.25rem;
    font-weight: 600;
}

.app-version {
    align-self: flex-end;
    margin: 0 0 0.15rem -0.25rem;
    color: var(--text-secondary);
    font-size: 0.62rem;
    font-weight: 600;
    line-height: 1;
}

.hamburger-button {
    width: 2.25rem;
    height: 2.25rem;
    display: grid;
    place-items: center;
    border: 0;
    border-radius: var(--radius-md);
    background: transparent;
    color: var(--text-primary);
    cursor: pointer;
}

.hamburger-button:hover,
.hamburger-button:focus-visible {
    background: var(--bg-page);
    outline: none;
}

.hamburger-button i {
    font-size: 1.5rem;
}

/* --- Search Bar --- */
.search-container {
    flex-grow: 1;
    max-width: 500px;
    position: relative;
    margin: 0 2rem;
}

.search-icon {
    position: absolute;
    left: 12px;
    top: 50%;
    transform: translateY(-50%);
    color: var(--text-secondary);
    font-size: 1.25rem;
}

#searchInput {
    width: 100%;
    padding: 0.6rem 1rem 0.6rem 2.5rem;
    border-radius: 999px;
    border: 1px solid var(--border-color);
    background-color: var(--bg-page);
    font-family: inherit;
    font-size: 0.95rem;
    transition: var(--transition-fast);
}

#searchInput:focus {
    outline: none;
    border-color: var(--primary-color);
    background-color: var(--bg-surface);
    box-shadow: 0 0 0 3px rgba(59, 130, 246, 0.1);
}

#searchInput:disabled {
    opacity: 0.6;
    cursor: not-allowed;
}

/* --- Buttons --- */
.btn-primary {
    background-color: var(--text-primary);
    color: white;
    padding: 0.6rem 1.2rem;
    border-radius: var(--radius-md);
    font-size: 0.9rem;
    font-weight: 500;
    cursor: pointer;
    display: inline-flex;
    align-items: center;
    gap: 0.5rem;
    transition: var(--transition-fast);
    border: none;
}

.btn-primary:hover {
    background-color: #334155;
    transform: translateY(-1px);
}
.btn-primary:active {
    transform: translateY(0);
}

.header-right {
    display: flex;
    align-items: center;
    gap: 0.5rem;
}

.btn-create {
    background-color: var(--primary-color);
}

.btn-create:hover {
    background-color: var(--primary-hover);
}

.button-count {
    min-width: 1.25rem;
    padding: 0.05rem 0.35rem;
    border-radius: 999px;
    background: var(--primary-color);
    color: white;
    font-size: 0.7rem;
    text-align: center;
}

.map-progress-icon {
    margin-left: auto;
    color: var(--primary-color);
    animation: spin 1s linear infinite;
}

.map-progress-icon[hidden] {
    display: none;
}

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

.btn-secondary {
    background-color: transparent;
    color: var(--text-secondary);
    border: 1px solid var(--border-color);
    padding: 0.4rem 0.8rem;
    border-radius: var(--radius-md);
    cursor: pointer;
    font-family: inherit;
    transition: var(--transition-fast);
}

.btn-secondary:hover:not(:disabled) {
    background-color: var(--bg-page);
    color: var(--text-primary);
}

.btn-small {
    font-size: 0.8rem;
    padding: 0.2rem 0.6rem;
}

.mobile-labels-toggle,
.mobile-label-search,
.mobile-labels-footer {
    display: none;
}

.sidebar .mobile-labels-close {
    display: none;
}

/* --- Dropdown --- */
.dropdown {
    position: relative;
}

.dropdown-menu {
    position: absolute;
    z-index: 30;
    top: calc(100% + 0.5rem);
    right: 0;
    min-width: 230px;
    padding: 0.4rem;
    background: var(--bg-surface);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-md);
    box-shadow: var(--shadow-md);
}

.dropdown-menu[hidden] {
    display: none;
}

.dropdown-item {
    width: 100%;
    display: flex;
    align-items: center;
    gap: 0.65rem;
    padding: 0.65rem 0.75rem;
    color: var(--text-primary);
    background: transparent;
    border: 0;
    border-radius: 6px;
    font: inherit;
    font-size: 0.9rem;
    text-align: left;
    cursor: pointer;
}

.dropdown-item:hover,
.dropdown-item:focus-visible {
    background: var(--bg-page);
    outline: none;
}

.dropdown-item:disabled {
    opacity: 0.45;
    cursor: not-allowed;
}

.settings-menu {
    right: auto;
    left: 0;
}

.dropdown-item-danger {
    color: #b91c1c;
}

.dropdown-item-danger:hover:not(:disabled),
.dropdown-item-danger:focus-visible:not(:disabled) {
    background: #fef2f2;
}

/* --- Create contact dialog --- */
.contact-dialog {
    width: min(520px, calc(100% - 2rem));
    max-height: calc(100% - 2rem);
    margin: auto;
    padding: 0;
    border: 1px solid var(--border-color);
    border-radius: var(--radius-lg);
    box-shadow: var(--shadow-hover);
    color: var(--text-primary);
    overflow-y: auto;
}

.contact-dialog::backdrop {
    background: rgb(15 23 42 / 0.45);
    backdrop-filter: blur(2px);
}

.contact-dialog form {
    padding: 1.5rem;
}

.dialog-header {
    display: flex;
    justify-content: space-between;
    gap: 1rem;
    margin-bottom: 1.5rem;
}

.dialog-header h2 {
    font-size: 1.25rem;
}

.dialog-header p {
    margin-top: 0.25rem;
    color: var(--text-secondary);
    font-size: 0.85rem;
}

.icon-button {
    width: 2rem;
    height: 2rem;
    display: grid;
    place-items: center;
    flex-shrink: 0;
    border: 0;
    border-radius: 999px;
    background: transparent;
    color: var(--text-secondary);
    cursor: pointer;
    font-size: 1.25rem;
}

.icon-button:hover {
    background: var(--bg-page);
    color: var(--text-primary);
}

.form-grid {
    display: grid;
    gap: 1rem;
}

.photo-field {
    display: flex;
    align-items: center;
    gap: 1rem;
}

.photo-preview {
    width: 72px;
    height: 72px;
    display: grid;
    place-items: center;
    flex-shrink: 0;
    overflow: hidden;
    border-radius: 999px;
    background: #e2e8f0;
    color: var(--text-secondary);
    font-size: 2rem;
}

.photo-preview img {
    width: 100%;
    height: 100%;
    object-fit: contain;
}

.photo-upload-button {
    display: inline-flex;
    align-items: center;
    gap: 0.4rem;
}

.photo-field small {
    display: block;
    max-width: 300px;
    margin-top: 0.4rem;
    color: var(--text-secondary);
    font-size: 0.75rem;
}

.form-field {
    display: grid;
    gap: 0.35rem;
    font-size: 0.85rem;
    font-weight: 500;
}

.form-field strong {
    color: #dc2626;
}

.form-field input,
.form-field textarea {
    width: 100%;
    padding: 0.65rem 0.75rem;
    border: 1px solid var(--border-color);
    border-radius: var(--radius-md);
    font: inherit;
    resize: vertical;
}

.form-field input:focus,
.form-field textarea:focus {
    outline: none;
    border-color: var(--primary-color);
    box-shadow: 0 0 0 3px rgb(59 130 246 / 0.1);
}

/* --- Contact map --- */
.map-dialog {
    width: min(1100px, calc(100% - 2rem));
    height: min(800px, calc(100% - 2rem));
    margin: auto;
    padding: 0;
    border: 1px solid var(--border-color);
    border-radius: var(--radius-lg);
    box-shadow: var(--shadow-hover);
}

.map-dialog::backdrop {
    background: rgb(15 23 42 / 0.45);
    backdrop-filter: blur(2px);
}

.map-dialog-layout {
    height: 100%;
    display: flex;
    flex-direction: column;
    padding: 1.25rem;
}

.map-dialog-header {
    margin-bottom: 0.75rem;
}

.map-status {
    min-height: 1.5rem;
    margin-bottom: 0.5rem;
    color: var(--text-secondary);
    font-size: 0.8rem;
}

.contacts-map {
    min-height: 320px;
    flex: 1;
    border: 1px solid var(--border-color);
    border-radius: var(--radius-md);
    overflow: hidden;
}

.map-contact-popup {
    display: grid;
    gap: 0.25rem;
}

.map-contact-popup strong {
    font-size: 0.9rem;
}

.map-contact-popup span {
    color: #475569;
}

.contact-photo-marker-wrapper {
    background: transparent;
    border: 0;
}

.contact-photo-marker {
    position: relative;
    z-index: 2;
    width: 48px;
    height: 48px;
    display: grid;
    place-items: center;
    border: 3px solid white;
    border-radius: 10px;
    background: #dbeafe;
    color: #1d4ed8;
    box-shadow: 0 3px 9px rgb(15 23 42 / 0.35);
    font-size: 0.8rem;
    font-weight: 700;
    transition: transform var(--transition-fast);
}

.contact-photo-marker-wrapper:hover .contact-photo-marker {
    transform: translateY(-2px) scale(1.08);
}

.contact-photo-marker img {
    display: block;
    position: absolute;
    inset: 3px;
    width: calc(100% - 6px) !important;
    height: calc(100% - 6px) !important;
    max-width: none !important;
    max-height: none !important;
    object-fit: contain;
    border-radius: 6px;
    image-rendering: auto;
}

.contact-photo-marker.marker-synced {
    border-color: #22c55e;
}

.contact-marker-tip {
    position: absolute;
    z-index: 1;
    left: 19px;
    bottom: 4px;
    width: 14px;
    height: 14px;
    transform: rotate(45deg);
    border-right: 3px solid white;
    border-bottom: 3px solid white;
    background: white;
    box-shadow: 3px 3px 5px rgb(15 23 42 / 0.18);
}

.contact-marker-tip.marker-synced {
    border-color: #22c55e;
    background: #22c55e;
}

.contact-cluster {
    width: 48px !important;
    height: 48px !important;
    display: grid !important;
    place-items: center;
    border: 3px solid white;
    border-radius: 10px;
    background: #22c55e !important;
    color: white;
    box-shadow: 0 3px 10px rgb(15 23 42 / 0.35);
    font-size: 1.15rem !important;
    font-weight: 700;
    line-height: 1 !important;
    text-align: center;
    transition: transform var(--transition-fast);
}

.contact-cluster span {
    width: 100%;
    height: 100%;
    display: grid !important;
    place-items: center;
    margin: 0 !important;
    padding: 0 !important;
    font: inherit !important;
    line-height: 1 !important;
    text-align: center;
}

.contact-cluster:hover {
    transform: scale(1.08);
}

.contact-cluster-medium {
    background: #16a34a !important;
}

.contact-cluster-large {
    background: #15803d !important;
}

.unmapped-contacts {
    margin-top: 0.75rem;
    color: var(--text-secondary);
    font-size: 0.8rem;
}

.unmapped-contacts ul {
    max-height: 100px;
    margin: 0.4rem 0 0 1.25rem;
    overflow-y: auto;
}

.map-privacy {
    margin-top: 0.65rem;
    color: var(--text-secondary);
    font-size: 0.7rem;
}

.form-field small {
    color: var(--text-secondary);
    font-weight: 400;
}

.form-error {
    margin-top: 1rem;
    color: #b91c1c;
    font-size: 0.85rem;
}

.dialog-actions {
    display: flex;
    justify-content: flex-end;
    gap: 0.5rem;
    margin-top: 1.5rem;
}

/* --- Main Layout --- */
.app-layout {
    display: flex;
    flex-grow: 1;
    overflow: hidden; /* Prevent scrolling on the layout level */
}

/* --- Sidebar (Labels) --- */
.sidebar {
    width: 280px;
    background-color: var(--bg-surface);
    border-right: 1px solid var(--border-color);
    display: flex;
    flex-direction: column;
}

.sidebar-header {
    padding: 1.5rem 1.5rem 0.5rem;
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.sidebar-header h2 {
    font-size: 0.85rem;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    color: var(--text-secondary);
    font-weight: 600;
    display: flex;
    align-items: center;
    gap: 0.45rem;
}

.sidebar-header-actions {
    display: flex;
    align-items: center;
    gap: 0.35rem;
}

.add-label-group-btn {
    width: 2rem;
    height: 2rem;
    color: var(--primary-color);
}

.sidebar-result-count {
    min-width: 1.5rem;
    padding: 0.08rem 0.4rem;
    border-radius: 999px;
    background: #e2e8f0;
    color: #475569;
    font-size: 0.7rem;
    line-height: 1.35;
    text-align: center;
}

.sidebar-result-count.is-filtered {
    background: #dbeafe;
    color: #1d4ed8;
}

.labels-list {
    padding: 0.5rem 1.5rem 1.5rem;
    overflow-y: auto;
    flex-grow: 1;
    display: flex;
    flex-direction: column;
    gap: 0.65rem;
}

.storage-info {
    padding: 1rem 1.5rem;
    border-top: 1px solid var(--border-color);
    color: var(--text-secondary);
    font-size: 0.75rem;
}

/* Custom Checkbox for labels */
.label-item {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    cursor: pointer;
    padding: 0.4rem 0.5rem;
    border-radius: var(--radius-md);
    transition: var(--transition-fast);
}

.label-group {
    overflow: hidden;
    border: 1px solid var(--border-color);
    border-radius: var(--radius-lg);
    background: var(--bg-surface);
    box-shadow: 0 1px 2px rgb(15 23 42 / 0.04);
    transition: var(--transition-fast);
}

.label-group.drag-over {
    border-color: var(--primary-color);
    background: rgb(59 130 246 / 0.08);
    box-shadow: 0 0 0 3px rgb(59 130 246 / 0.12);
}

.label-group-header {
    width: 100%;
    min-height: 2.5rem;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 0.5rem;
    padding: 0.55rem 0.65rem;
    border: 0;
    border-radius: 0;
    background: #f8fafc;
    color: var(--text-secondary);
    font: inherit;
    cursor: pointer;
}

.label-group-header:hover {
    background: #f1f5f9;
    color: var(--text-primary);
}

.label-group-header > span:first-child {
    min-width: 0;
    display: flex;
    align-items: center;
    gap: 0.35rem;
}

.label-group-header strong {
    overflow: hidden;
    font-size: 0.78rem;
    font-weight: 600;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.label-group-header .ph-folder,
.label-group-header .ph-tray {
    color: var(--primary-color);
    font-size: 1rem;
}

.label-group-header .ph-caret-down,
.label-group-header .ph-caret-right {
    font-size: 0.72rem;
}

.label-group-count {
    min-width: 1.35rem;
    padding: 0.05rem 0.35rem;
    border-radius: 999px;
    background: #dbeafe;
    color: #1d4ed8;
    font-size: 0.66rem;
    text-align: center;
}

.label-group-body {
    display: grid;
    gap: 0.15rem;
    padding: 0.35rem;
    border-top: 1px solid var(--border-color);
    background: var(--bg-surface);
}

.label-group-body[hidden] {
    display: none;
}

.label-group-empty {
    margin: 0.25rem 0.5rem 0.45rem;
    padding: 0.55rem;
    border: 1px dashed var(--border-color);
    border-radius: var(--radius-md);
    color: var(--text-secondary);
    font-size: 0.72rem;
    text-align: center;
}

.label-drag-handle {
    flex-shrink: 0;
    color: #94a3b8;
    cursor: grab;
}

.label-group .label-item {
    padding-right: 0.6rem;
    padding-left: 0.35rem;
}

.label-group .label-item:hover {
    background: #f1f5f9;
}

.label-item.is-dragging {
    opacity: 0.4;
}

.label-item:hover {
    background-color: var(--bg-page);
}

.label-item input[type="checkbox"] {
    appearance: none;
    -webkit-appearance: none;
    width: 1.1rem;
    height: 1.1rem;
    border: 2px solid var(--border-color);
    border-radius: 4px;
    cursor: pointer;
    position: relative;
    transition: var(--transition-fast);
    flex-shrink: 0;
}

.label-item input[type="checkbox"]:checked {
    background-color: var(--primary-color);
    border-color: var(--primary-color);
}

.label-item input[type="checkbox"]:checked::after {
    content: '';
    position: absolute;
    left: 4px;
    top: 0px;
    width: 4px;
    height: 9px;
    border: solid white;
    border-width: 0 2px 2px 0;
    transform: rotate(45deg);
}

.label-text {
    font-size: 0.9rem;
    color: var(--text-primary);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

/* --- Main Content Area --- */
.content-area {
    flex-grow: 1;
    background-color: var(--bg-page);
    padding: 2rem;
    overflow-y: auto;
    position: relative;
}

.status-message {
    text-align: center;
    color: var(--text-secondary);
    font-style: italic;
    margin-top: 2rem;
}

/* --- Contacts Grid --- */
.contacts-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
    gap: 1.5rem;
    align-items: start;
}

.contact-card {
    background-color: var(--bg-surface);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-lg);
    padding: 1.25rem;
    box-shadow: var(--shadow-sm);
    transition: var(--transition-normal);
    position: relative;
    overflow: hidden;
    cursor: pointer;
}

.contact-card:hover {
    transform: translateY(-4px);
    box-shadow: var(--shadow-hover);
    border-color: #cbd5e1;
}

.card-name {
    font-size: 1.1rem;
    font-weight: 600;
    color: var(--text-primary);
}

.results-summary {
    position: sticky;
    top: -1rem;
    z-index: 5;
    width: fit-content;
    max-width: 100%;
    display: flex;
    align-items: center;
    gap: 0.6rem;
    margin: 0 0 1.25rem;
    padding: 0.45rem 0.7rem;
    border: 1px solid var(--border-color);
    border-radius: 999px;
    background: rgb(255 255 255 / 0.92);
    box-shadow: var(--shadow-sm);
    backdrop-filter: blur(8px);
    color: var(--text-secondary);
    font-size: 0.78rem;
}

.results-summary[hidden] {
    display: none;
}

.results-count {
    display: flex;
    align-items: baseline;
    gap: 0.25rem;
    white-space: nowrap;
}

.results-count strong {
    color: var(--text-primary);
    font-size: 0.95rem;
}

.results-filtered {
    border-color: #bfdbfe;
    background: rgb(239 246 255 / 0.94);
}

.results-filtered .results-count strong {
    color: #1d4ed8;
}

.results-context {
    min-width: 0;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.contact-card:focus-visible {
    outline: 3px solid rgb(59 130 246 / 0.35);
    outline-offset: 2px;
}

.card-heading {
    display: flex;
    align-items: center;
    gap: 0.8rem;
    padding-right: 4rem;
    margin-bottom: 0.75rem;
}

.card-avatar {
    width: 46px;
    height: 46px;
    display: grid;
    place-items: center;
    flex-shrink: 0;
    overflow: hidden;
    border-radius: 999px;
}

.card-avatar img {
    width: 100%;
    height: 100%;
    object-fit: contain;
}

.card-avatar-fallback {
    background: #dbeafe;
    color: #1d4ed8;
    font-size: 0.8rem;
    font-weight: 700;
    letter-spacing: 0.03em;
}

/* --- Contact details --- */
.contact-details-content {
    padding: 1.5rem;
}

.details-profile {
    display: flex;
    align-items: center;
    gap: 1rem;
    margin-bottom: 1.5rem;
}

.details-profile h3 {
    margin-bottom: 0.35rem;
    font-size: 1.35rem;
}

.details-avatar {
    width: 88px;
    height: 88px;
    display: grid;
    place-items: center;
    flex-shrink: 0;
    overflow: hidden;
    border-radius: var(--radius-lg);
    background: #e2e8f0;
}

.details-avatar img {
    width: 100%;
    height: 100%;
    object-fit: contain;
}

.details-avatar-fallback {
    color: #1d4ed8;
    background: #dbeafe;
    font-size: 1.35rem;
    font-weight: 700;
}

.detail-sync-status {
    display: inline-block;
    padding: 0.15rem 0.45rem;
    border-radius: 999px;
    font-size: 0.7rem;
    font-weight: 600;
}

.detail-sync-complete {
    color: #166534;
    background: #dcfce7;
}

.detail-sync-pending {
    color: #92400e;
    background: #fef3c7;
}

.detail-sync-error {
    color: #b91c1c;
    background: #fee2e2;
}

.detail-sync-local {
    color: #475569;
    background: #e2e8f0;
}

.details-fields {
    display: grid;
    gap: 0.25rem;
}

.detail-row {
    display: grid;
    grid-template-columns: 1.5rem 1fr;
    gap: 0.65rem;
    padding: 0.75rem 0;
    border-bottom: 1px solid var(--border-color);
}

.detail-row > i {
    margin-top: 0.15rem;
    color: var(--text-secondary);
    font-size: 1.15rem;
}

.detail-row span,
.details-section-label {
    color: var(--text-secondary);
    font-size: 0.7rem;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.04em;
}

.detail-row p {
    margin-top: 0.1rem;
    white-space: pre-line;
}

.details-labels {
    margin-top: 1.25rem;
}

.details-empty {
    color: var(--text-secondary);
    font-size: 0.85rem;
}

.card-sync-indicator {
    position: absolute;
    top: 0.9rem;
    right: 0.9rem;
    width: 1.75rem;
    height: 1.75rem;
    display: grid;
    place-items: center;
    border-radius: 999px;
    font-size: 1.15rem;
}

.sync-complete {
    color: #166534;
    background: #dcfce7;
}

.sync-pending {
    color: #92400e;
    background: #fef3c7;
}

.sync-error {
    color: #b91c1c;
    background: #fee2e2;
}

/* Highlighted match styling */
.highlight {
    background-color: rgba(253, 224, 71, 0.4); /* subtle yellow */
    border-radius: 2px;
    padding: 0 2px;
    font-weight: 600;
}

.card-info {
    font-size: 0.85rem;
    color: var(--text-secondary);
    margin-bottom: 0.25rem;
    display: flex;
    align-items: center;
    gap: 0.5rem;
}

.card-info i {
    font-size: 1.1rem;
    opacity: 0.7;
}

.card-labels {
    margin-top: 1rem;
    display: flex;
    flex-wrap: wrap;
    gap: 0.4rem;
}

.card-label-tag {
    background-color: #e2e8f0;
    color: #475569;
    font-size: 0.7rem;
    padding: 0.2rem 0.5rem;
    border-radius: 999px;
    font-weight: 500;
}

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

.contact-card {
    animation: fadeIn 0.2s ease-out backwards;
}

/* Add staggered animation delays via JS if possible, or generic */
.contact-card:nth-child(1) { animation-delay: 0.05s; }
.contact-card:nth-child(2) { animation-delay: 0.1s; }
.contact-card:nth-child(3) { animation-delay: 0.15s; }
.contact-card:nth-child(4) { animation-delay: 0.2s; }

/* Responsive adjustments */
@media (max-width: 768px) {
    body {
        min-width: 0;
    }

    .app-header {
        display: grid;
        grid-template-columns: minmax(0, 1fr);
        gap: 0.75rem;
        flex-shrink: 0;
        padding:
            max(0.75rem, env(safe-area-inset-top))
            max(0.75rem, env(safe-area-inset-right))
            0.75rem
            max(0.75rem, env(safe-area-inset-left));
    }

    .header-left {
        min-width: 0;
    }

    .hamburger-button {
        width: 2.75rem;
        height: 2.75rem;
    }

    .search-container {
        grid-row: 2;
        width: 100%;
        max-width: none;
        margin: 0;
    }

    #searchInput {
        min-height: 44px;
        font-size: 1rem;
    }

    .header-right {
        grid-row: 3;
        width: 100%;
        min-width: 0;
    }

    .header-right > .dropdown,
    .header-right > .btn-primary {
        flex: 1 1 0;
        min-width: 0;
    }

    .header-right > .dropdown > .btn-primary,
    .header-right > .btn-primary {
        width: 100%;
        min-height: 44px;
        justify-content: center;
        padding: 0.65rem 0.75rem;
        white-space: nowrap;
    }

    .header-right .dropdown-menu {
        right: auto;
        left: 0;
        max-width: calc(100vw - 1.5rem);
    }

    .dropdown-item {
        min-height: 44px;
    }

    .app-layout {
        min-height: 0;
        flex-direction: column;
    }

    .sidebar {
        width: 100%;
        height: auto;
        max-height: none;
        flex-shrink: 0;
        border-right: none;
        border-bottom: 1px solid var(--border-color);
    }

    .sidebar > .sidebar-header,
    .sidebar > .mobile-label-search,
    .sidebar > .labels-list,
    .sidebar > .mobile-labels-footer {
        display: none;
    }

    .mobile-labels-toggle {
        width: 100%;
        min-height: 54px;
        display: flex;
        align-items: center;
        justify-content: space-between;
        gap: 0.75rem;
        padding: 0.7rem 1rem;
        border: 0;
        background: var(--bg-surface);
        color: var(--text-primary);
        font: inherit;
        cursor: pointer;
    }

    .mobile-labels-toggle > span {
        min-width: 0;
        display: flex;
        align-items: center;
        gap: 0.45rem;
    }

    .mobile-labels-toggle > span:last-child {
        color: var(--text-secondary);
        font-size: 0.78rem;
        white-space: nowrap;
    }

    .mobile-selected-count {
        padding: 0.1rem 0.4rem;
        border-radius: 999px;
        background: #dbeafe;
        color: #1d4ed8;
        font-size: 0.68rem;
        font-weight: 600;
    }

    .sidebar.mobile-expanded {
        position: fixed;
        z-index: 100;
        inset: 0;
        width: 100%;
        height: 100dvh;
        display: flex;
        background: var(--bg-surface);
        border: 0;
    }

    .sidebar.mobile-expanded .mobile-labels-toggle {
        display: none;
    }

    .sidebar.mobile-expanded > .sidebar-header {
        display: flex;
        flex-shrink: 0;
        padding:
            max(0.9rem, env(safe-area-inset-top))
            1rem
            0.75rem;
        border-bottom: 1px solid var(--border-color);
    }

    .sidebar-header-actions {
        display: flex;
        align-items: center;
        gap: 0.5rem;
    }

    .sidebar.mobile-expanded .mobile-labels-close {
        display: grid;
        width: 2.75rem;
        height: 2.75rem;
    }

    .sidebar.mobile-expanded > .mobile-label-search {
        position: relative;
        display: block;
        flex-shrink: 0;
        padding: 0.75rem 1rem;
    }

    .mobile-label-search > i {
        position: absolute;
        left: 1.75rem;
        top: 50%;
        transform: translateY(-50%);
        color: var(--text-secondary);
    }

    .mobile-label-search input {
        width: 100%;
        min-height: 44px;
        padding: 0.65rem 0.75rem 0.65rem 2.5rem;
        border: 1px solid var(--border-color);
        border-radius: 999px;
        background: var(--bg-page);
        font: inherit;
        font-size: 1rem;
    }

    .mobile-label-search input:focus {
        outline: none;
        border-color: var(--primary-color);
        box-shadow: 0 0 0 3px rgb(59 130 246 / 0.1);
    }

    .sidebar.mobile-expanded > .labels-list {
        min-height: 0;
        display: flex;
        flex: 1;
        flex-direction: column;
        gap: 0.35rem;
        padding: 0.25rem 1rem 1rem;
        overflow-x: hidden;
        overflow-y: auto;
        overscroll-behavior-y: contain;
        -webkit-overflow-scrolling: touch;
        touch-action: pan-y;
    }

    .sidebar.mobile-expanded .label-item {
        width: 100%;
        min-height: 48px;
        flex: 0 0 auto;
        padding: 0.65rem 0.75rem;
        border: 1px solid var(--border-color);
        background: var(--bg-page);
        touch-action: pan-y;
    }

    .sidebar.mobile-expanded .label-group-header {
        min-height: 44px;
        padding: 0.55rem 0.65rem;
    }

    .sidebar.mobile-expanded .label-group {
        flex: 0 0 auto;
    }

    .sidebar.mobile-expanded .label-text {
        max-width: none;
        white-space: normal;
    }

    .sidebar.mobile-expanded > .mobile-labels-footer {
        display: flex;
        align-items: center;
        justify-content: space-between;
        gap: 1rem;
        flex-shrink: 0;
        padding: 0.75rem 1rem max(0.75rem, env(safe-area-inset-bottom));
        border-top: 1px solid var(--border-color);
        background: var(--bg-surface);
        color: var(--text-secondary);
        font-size: 0.8rem;
        box-shadow: 0 -4px 12px rgb(15 23 42 / 0.06);
    }

    .mobile-labels-footer .btn-primary {
        min-width: 100px;
        min-height: 44px;
        justify-content: center;
    }

    .storage-info {
        display: none;
    }

    .content-area {
        min-height: 0;
        padding: 1rem;
        padding-bottom: max(1rem, env(safe-area-inset-bottom));
    }

    .results-summary {
        top: 0;
        max-width: 100%;
        margin-bottom: 1rem;
    }

    .contacts-grid {
        grid-template-columns: 1fr;
        gap: 0.85rem;
    }

    .contact-card {
        padding: 1rem;
    }

    .contact-card:hover {
        transform: none;
    }

    .contact-dialog {
        width: calc(100% - 1rem);
        max-height: calc(100dvh - 1rem);
        border-radius: var(--radius-lg);
    }

    .contact-dialog form,
    .contact-details-content {
        padding: 1rem;
    }

    .dialog-header {
        margin-bottom: 1rem;
    }

    .form-field input,
    .form-field textarea {
        min-height: 44px;
        font-size: 1rem;
    }

    .dialog-actions {
        position: sticky;
        bottom: -1rem;
        margin: 1rem -1rem -1rem;
        padding: 0.75rem 1rem max(0.75rem, env(safe-area-inset-bottom));
        border-top: 1px solid var(--border-color);
        background: var(--bg-surface);
    }

    .dialog-actions .btn-primary,
    .dialog-actions .btn-secondary {
        min-height: 44px;
    }

    .map-dialog {
        width: calc(100% - 0.5rem);
        height: calc(100dvh - 0.5rem);
        max-height: none;
        margin: auto;
        border-radius: var(--radius-md);
    }

    .map-dialog-layout {
        padding: 0.75rem;
        padding-bottom: max(0.75rem, env(safe-area-inset-bottom));
    }

    .contacts-map {
        min-height: 260px;
    }

    .details-profile {
        align-items: flex-start;
    }

    .details-avatar {
        width: 72px;
        height: 72px;
    }
}

@media (max-width: 380px) {
    .header-left h1 {
        font-size: 1.1rem;
    }

    .header-right > .dropdown > .btn-primary,
    .header-right > .btn-primary {
        gap: 0.35rem;
        padding-inline: 0.5rem;
        font-size: 0.78rem;
    }

    .photo-field {
        align-items: flex-start;
    }

    .results-context {
        display: none;
    }
}
