:root {
    --ios-blue: #007AFF;
    --ios-green: #34C759;
    --wa-green: #25D366;
    --ios-red: #FF3B30;
    --ios-orange: #FF9500;
    --ios-yellow: #FFCC00;
    --label: #1C1C1E;
    --label-secondary: #6D6D72;
    --label-tertiary: #AEAEB2;
    --bg: #F2F2F7;
    --grouped-bg: #FFFFFF;
    --separator: rgba(60, 60, 67, 0.13);
    --fill-gray6: #F2F2F7;
    --fill-gray5: #E5E5EA;
    --topbar-bg: rgba(242, 242, 247, 0.85);
    --tabbar-bg: rgba(249, 249, 249, 0.88);
    --card-shadow: 0 1px 2px rgba(0,0,0,0.04);
    --avatar-bg: #E5E5EA;
    --avatar-color: #6D6D72;
    --card-border: rgba(0, 0, 0, 0.06);
    --chip-bg: #FFFFFF;
}

@media (prefers-color-scheme: dark) {
    :root {
        --ios-blue: #0A84FF;
        --ios-green: #30D158;
        --ios-red: #FF453A;
        --ios-orange: #FF9F0A;
        --ios-yellow: #FFD60A;
        --label: #FFFFFF;
        --label-secondary: #98989D;
        --label-tertiary: #636366;
        --bg: #000000;
        --grouped-bg: #1C1C1E;
        --separator: rgba(84, 84, 88, 0.6);
        --fill-gray6: #2C2C2E;
        --fill-gray5: #3A3A3C;
        --topbar-bg: rgba(0, 0, 0, 0.8);
        --tabbar-bg: rgba(20, 20, 22, 0.85);
        --card-shadow: 0 1px 2px rgba(0,0,0,0.3);
        --avatar-bg: #3A3A3C;
        --avatar-color: #98989D;
        --card-border: rgba(255, 255, 255, 0.08);
        --chip-bg: #17191F;
    }
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html { color-scheme: light dark; }
body {
    margin: 0;
    font-family: -apple-system, BlinkMacSystemFont, 'SF Pro Text', 'Segoe UI', Roboto, sans-serif;
    background: var(--bg);
    color: var(--label);
    -webkit-font-smoothing: antialiased;
}

/* ---------- Login ---------- */
.login-body {
    display: flex;
    align-items: center;
    justify-content: center;
    min-height: 100vh;
    padding: 20px;
    background: var(--bg);
}
.login-card {
    background: var(--grouped-bg);
    padding: 36px 26px;
    border-radius: 20px;
    box-shadow: var(--card-shadow);
    width: 100%;
    max-width: 340px;
}
.login-card__icon {
    width: 64px;
    height: 64px;
    border-radius: 16px;
    background: linear-gradient(160deg, var(--wa-green), #1ebe5b);
    display: flex;
    align-items: center;
    justify-content: center;
    margin: 0 auto 18px;
    font-size: 30px;
    box-shadow: 0 6px 16px rgba(37,211,102,0.35);
}
.login-card h1 { font-size: 1.25rem; margin: 0 0 4px; text-align: center; font-weight: 700; letter-spacing: -0.01em; }
.login-card .subtitle { color: var(--label-secondary); font-size: 0.86rem; margin: 0 0 24px; text-align: center; }
.login-card label { display: block; font-size: 0.8rem; margin: 14px 0 6px; font-weight: 600; color: var(--label-secondary); }
.login-card input {
    width: 100%;
    padding: 13px 14px;
    border: none;
    background: var(--fill-gray6);
    border-radius: 12px;
    font-size: 16px;
    color: var(--label);
}
.login-card input:focus { outline: 2px solid var(--ios-blue); outline-offset: -1px; }

/* ---------- Top bar ---------- */
.topbar {
    position: sticky;
    top: 0;
    z-index: 20;
    padding: calc(env(safe-area-inset-top, 0px) + 14px) 20px 12px;
    background: var(--topbar-bg);
    backdrop-filter: saturate(180%) blur(20px);
    -webkit-backdrop-filter: saturate(180%) blur(20px);
    border-bottom: 1px solid var(--separator);
}
.topbar__title {
    font-size: 1.65rem;
    font-weight: 700;
    letter-spacing: -0.02em;
    color: var(--label);
}

/* ---------- Bottom tab bar ---------- */
.tabbar {
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    z-index: 20;
    display: flex;
    background: var(--tabbar-bg);
    backdrop-filter: saturate(180%) blur(20px);
    -webkit-backdrop-filter: saturate(180%) blur(20px);
    border-top: 1px solid var(--separator);
    padding: 6px 6px calc(env(safe-area-inset-bottom, 0px) + 6px);
}
.tabbar__item {
    flex: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 2px;
    text-decoration: none;
    color: var(--label-tertiary);
    padding: 4px 0;
    font-size: 0.66rem;
    font-weight: 600;
}
.tabbar__item svg { width: 24px; height: 24px; stroke: currentColor; fill: none; stroke-width: 1.8; }
.tabbar__item.active { color: var(--ios-blue); }

/* ---------- Layout ---------- */
.container {
    max-width: 600px;
    margin: 0 auto;
    padding: 14px 16px calc(env(safe-area-inset-bottom, 0px) + 88px);
}
.container--top {
    padding-top: calc(env(safe-area-inset-top, 0px) + 16px);
}

.card {
    background: var(--grouped-bg);
    border-radius: 14px;
    padding: 0;
    margin-bottom: 18px;
    overflow: hidden;
    box-shadow: var(--card-shadow);
}
.card h2 { margin: 0; padding: 14px 16px 4px; font-size: 1rem; font-weight: 700; }
.card p.hint { color: var(--label-secondary); font-size: 0.8rem; margin: 0; padding: 0 16px 10px; }

.contacts-card { padding: 2px 16px; }

.add-card { padding: 4px 16px; }
.add-card summary {
    cursor: pointer;
    font-weight: 600;
    font-size: 0.95rem;
    color: var(--ios-blue);
    list-style: none;
    padding: 12px 0;
    display: flex;
    align-items: center;
    gap: 8px;
}
.add-card summary::-webkit-details-marker { display: none; }
.add-card summary::before {
    content: "+";
    width: 22px;
    height: 22px;
    border-radius: 50%;
    background: var(--ios-blue);
    color: #fff;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 15px;
    font-weight: 700;
    line-height: 1;
}
.add-card[open] summary { padding-bottom: 10px; }
.add-card p.hint { padding: 0 0 8px; }
.add-card > textarea, .add-card > button { margin-bottom: 12px; }

/* ---------- Form controls ---------- */
textarea, input[type=text], input[type=url], input[type=number], select {
    width: 100%;
    padding: 13px 14px;
    border: none;
    background: var(--fill-gray6);
    border-radius: 12px;
    font-size: 16px;
    font-family: inherit;
    color: var(--label);
}
textarea:focus, input:focus, select:focus { outline: 2px solid var(--ios-blue); outline-offset: -1px; }
label { display: block; font-weight: 600; font-size: 0.78rem; margin: 16px 0 6px; color: var(--label-secondary); text-transform: uppercase; letter-spacing: 0.02em; }
code {
    background: var(--fill-gray5);
    padding: 1px 5px;
    border-radius: 4px;
    font-size: 0.85em;
}

/* ---------- Buttons ---------- */
.btn {
    display: inline-block;
    padding: 11px 18px;
    border-radius: 980px;
    border: none;
    font-size: 0.9rem;
    font-weight: 600;
    cursor: pointer;
    text-decoration: none;
    text-align: center;
    min-height: 40px;
    line-height: 18px;
    transition: opacity 0.15s, transform 0.1s;
}
.btn:active { opacity: 0.7; transform: scale(0.98); }
.btn-primary { background: var(--wa-green); color: #fff; }
.btn-secondary { background: var(--ios-blue); color: #fff; }
.btn-danger { background: rgba(255,59,48,0.12); color: var(--ios-red); }
.btn-outline { background: var(--fill-gray6); color: var(--label); }
.btn-block { width: 100%; margin-top: 18px; }
.btn-sm { padding: 9px 14px; font-size: 0.83rem; min-height: 36px; border-radius: 980px; }

.alert { padding: 12px 16px; border-radius: 12px; font-size: 0.85rem; margin: 0 16px 12px; }
.alert-success { background: rgba(52,199,89,0.12); color: #248a3d; }
.alert-error { background: rgba(255,59,48,0.12); color: #c62c22; }

/* ---------- Badges ---------- */
.badge {
    display: inline-block;
    padding: 3px 10px;
    border-radius: 980px;
    font-size: 0.68rem;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 0.02em;
}
.badge-pending { background: rgba(255,149,0,0.14); color: #b3690a; }
.badge-sent { background: rgba(0,122,255,0.12); color: #0062cc; }
.badge-reminded { background: rgba(255,204,0,0.18); color: #9c7900; }
.badge-completed { background: rgba(52,199,89,0.14); color: #248a3d; }

.muted { color: var(--label-tertiary); font-size: 0.78rem; }

/* ---------- Filter tabs (segmented) ---------- */
.filter-tabs {
    display: flex;
    gap: 6px;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    padding: 2px 2px 4px;
    margin-bottom: 12px;
    scrollbar-width: none;
    mask-image: linear-gradient(to right, black calc(100% - 28px), transparent 100%);
    -webkit-mask-image: linear-gradient(to right, black calc(100% - 28px), transparent 100%);
}
.filter-tabs::-webkit-scrollbar { display: none; }
.filter-tab {
    display: inline-block;
    flex: 0 0 auto;
    white-space: nowrap;
    padding: 7px 14px;
    border-radius: 980px;
    background: var(--grouped-bg);
    color: var(--label-secondary);
    text-decoration: none;
    font-size: 0.78rem;
    font-weight: 600;
    box-shadow: var(--card-shadow);
}
.filter-tab.active { background: var(--ios-blue); color: #fff; box-shadow: none; }

/* ---------- Contact list — iOS grouped rows ---------- */
.contact-row {
    padding: 12px 0;
    border-bottom: 1px solid var(--separator);
    min-height: 44px;
}
.contact-row:last-child { border-bottom: none; }
.contact-row__view { display: flex; gap: 12px; align-items: flex-start; }
.contact-row__avatar {
    flex: 0 0 auto;
    width: 40px;
    height: 40px;
    border-radius: 50%;
    background: var(--avatar-bg);
    color: var(--avatar-color);
    display: flex;
    align-items: center;
    justify-content: center;
    font-weight: 700;
    font-size: 0.95rem;
    margin-top: 1px;
}
.contact-row__body { flex: 1 1 auto; min-width: 0; }
.contact-row__top {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 8px;
}
.contact-row__name {
    font-weight: 600;
    font-size: 0.98rem;
    letter-spacing: -0.01em;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}
.contact-row__phone { color: var(--label-secondary); font-size: 0.82rem; margin-top: 2px; }

.actions { display: flex; gap: 8px; flex-wrap: wrap; margin-top: 10px; }
.actions .btn, .actions button { flex: 1 1 auto; }
.actions--end { justify-content: flex-end; }
.actions--end .btn { flex: 0 0 auto; }

.contact-row__edit .edit-name, .contact-row__edit .edit-phone {
    margin-bottom: 8px;
}
.contact-row__edit .row-links { margin-top: 4px; }
.contact-row__edit .row-links .btn { flex: 1 1 auto; }

.row-links { display: flex; gap: 18px; margin-top: 9px; flex-wrap: wrap; }
.contact-card__actions .row-links {
    border-top: 1px solid var(--separator);
    padding-top: 12px;
    margin-top: 12px;
}
.row-link {
    font-size: 0.78rem;
    color: var(--label-secondary);
    text-decoration: none;
    font-weight: 600;
    display: inline-flex;
    align-items: center;
    gap: 4px;
}
.row-link--danger { color: var(--ios-red); }
.row-link--done { color: var(--ios-green); }

@media (min-width: 640px) {
    .actions .btn, .actions button { flex: 0 0 auto; }
}

/* ---------- Add Contacts card (v2) ---------- */
.add-card-v2 {
    background: var(--grouped-bg);
    border: 1px solid var(--card-border);
    border-radius: 16px;
    padding: 0 16px;
    margin-bottom: 16px;
}
.add-card-v2 summary {
    cursor: pointer;
    list-style: none;
    padding: 14px 0;
    display: flex;
    align-items: center;
    gap: 14px;
}
.add-card-v2 summary::-webkit-details-marker { display: none; }
.add-card-v2__icon {
    flex: 0 0 auto;
    width: 40px;
    height: 40px;
    border-radius: 50%;
    background: rgba(0,122,255,0.14);
    color: var(--ios-blue);
    display: flex;
    align-items: center;
    justify-content: center;
}
.add-card-v2__text { flex: 1 1 auto; min-width: 0; }
.add-card-v2__title { display: block; font-weight: 700; font-size: 0.95rem; }
.add-card-v2__subtitle { display: block; font-size: 0.78rem; color: var(--label-secondary); margin-top: 1px; }
.add-card-v2__chevron { color: var(--label-tertiary); transition: transform 0.2s; flex: 0 0 auto; }
.add-card-v2[open] .add-card-v2__chevron { transform: rotate(90deg); }
.add-card-v2__body { padding: 0 0 16px; }
.add-card-v2__body .hint { color: var(--label-secondary); font-size: 0.8rem; margin: 0 0 10px; }

/* ---------- Stat chips ---------- */
.stat-chips {
    display: flex;
    gap: 8px;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    padding: 2px 2px 4px;
    margin-bottom: 16px;
    scrollbar-width: none;
    mask-image: linear-gradient(to right, black calc(100% - 28px), transparent 100%);
    -webkit-mask-image: linear-gradient(to right, black calc(100% - 28px), transparent 100%);
}
.stat-chips::-webkit-scrollbar { display: none; }
.stat-chip {
    flex: 0 0 auto;
    display: inline-flex;
    align-items: center;
    gap: 7px;
    white-space: nowrap;
    padding: 9px 14px;
    border-radius: 12px;
    background: var(--chip-bg);
    border: 1px solid var(--card-border);
    color: var(--label-secondary);
    text-decoration: none;
    font-size: 0.82rem;
    font-weight: 600;
}
.stat-chip__count {
    background: var(--fill-gray6);
    color: var(--label);
    border-radius: 999px;
    padding: 1px 7px;
    font-size: 0.74rem;
    font-weight: 700;
}
.stat-chip.active {
    background: var(--ios-blue);
    border-color: var(--ios-blue);
    color: #fff;
}
.stat-chip.active .stat-chip__count { background: rgba(255,255,255,0.25); color: #fff; }

/* ---------- Contact cards (v2) ---------- */
.contact-card {
    background: var(--grouped-bg);
    border: 1px solid var(--card-border);
    border-radius: 16px;
    padding: 14px 16px;
    margin-bottom: 12px;
}
.contact-card__header {
    display: flex;
    align-items: center;
    gap: 12px;
}
.avatar {
    flex: 0 0 auto;
    width: 44px;
    height: 44px;
    border-radius: 50%;
    color: #fff;
    display: flex;
    align-items: center;
    justify-content: center;
    font-weight: 700;
    font-size: 1rem;
}
.contact-card__id { flex: 1 1 auto; min-width: 0; }
.contact-card__name {
    font-weight: 700;
    font-size: 0.98rem;
    letter-spacing: -0.01em;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}
.contact-card__phone { color: var(--label-secondary); font-size: 0.82rem; margin-top: 1px; }

.status-pill {
    flex: 0 0 auto;
    display: inline-flex;
    align-items: center;
    gap: 5px;
    padding: 5px 10px;
    border-radius: 999px;
    font-size: 0.68rem;
    font-weight: 700;
    letter-spacing: 0.02em;
}
.status-pill--pending { background: rgba(255,149,0,0.14); color: var(--ios-orange); }
.status-pill--sent { background: rgba(10,132,255,0.16); color: var(--ios-blue); }
.status-pill--reminded { background: rgba(255,214,10,0.18); color: #b8860b; }
.status-pill--completed { background: rgba(48,209,88,0.16); color: var(--ios-green); }
.status-pill--cold { background: rgba(100,210,255,0.16); color: #0a84ff; }

.meta-row {
    display: flex;
    align-items: center;
    gap: 8px;
    margin-top: 10px;
    color: var(--label-secondary);
    font-size: 0.76rem;
    flex-wrap: wrap;
}
.meta-row span { display: inline-flex; align-items: center; gap: 5px; }
.meta-sep { color: var(--separator); }

.contact-card__actions { margin-top: 12px; }
.btn-resend {
    background: transparent;
    border: 1px solid var(--ios-blue);
    color: var(--ios-blue);
}
.btn svg, .row-link svg { flex: 0 0 auto; }
.btn { display: inline-flex; align-items: center; justify-content: center; gap: 6px; }

/* ---------- Pagination ---------- */
.pagination-footer {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 12px;
    margin-top: 8px;
    padding: 12px 0 4px;
}
.pagination { display: flex; align-items: center; gap: 6px; }
.pagination__btn, .pagination__page {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 34px;
    height: 34px;
    border-radius: 10px;
    background: var(--chip-bg);
    border: 1px solid var(--card-border);
    color: var(--label);
    text-decoration: none;
    font-size: 0.85rem;
    font-weight: 600;
}
.pagination__page.active { background: var(--ios-blue); border-color: var(--ios-blue); color: #fff; }
.pagination__btn.disabled { opacity: 0.35; pointer-events: none; }
