/* ========== الألوان (فاتح / داكن تلقائيًا) ========== */
:root {
    --bg: #f4f8fc;
    --surface: #ffffff;
    --surface-2: #f5f9fd;
    --border: #dce7f1;
    --text: #17324d;
    --muted: #6b7f95;
    --primary: #2477d4;
    --primary-ink: #ffffff;
    --primary-soft: rgba(36, 119, 212, 0.1);
    --danger: #c84848;
    --danger-soft: #fff7f7;
    --danger-border: #efcccc;
    --green: #1f9d55;
    --shadow: 0 20px 50px rgba(30, 75, 115, 0.1);
    --overlay: rgba(10, 25, 45, 0.45);
    color-scheme: light;
}

@media (prefers-color-scheme: dark) {
    :root {
        --bg: #0d1724;
        --surface: #142132;
        --surface-2: #1a293d;
        --border: #26384f;
        --text: #e6eef7;
        --muted: #93a6bb;
        --primary: #4a9af0;
        --primary-ink: #ffffff;
        --primary-soft: rgba(74, 154, 240, 0.16);
        --danger: #ff8a8a;
        --danger-soft: #2a1a20;
        --danger-border: #5a2e36;
        --green: #4cc38a;
        --shadow: 0 20px 50px rgba(0, 0, 0, 0.35);
        --overlay: rgba(0, 0, 0, 0.6);
        color-scheme: dark;
    }
}

* { box-sizing: border-box; }

html { -webkit-text-size-adjust: 100%; }

body {
    margin: 0;
    min-height: 100vh;
    background: var(--bg);
    color: var(--text);
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Tahoma, "Noto Sans Arabic", Arial, sans-serif;
    line-height: 1.5;
    padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
}

button, input { font: inherit; color: inherit; }
button { cursor: pointer; }
:focus-visible { outline: 3px solid var(--primary); outline-offset: 2px; }

/* ========== الرأس ========== */
.app-header {
    width: min(1000px, calc(100% - 32px));
    margin: 28px auto 18px;
    display: flex;
    align-items: center;
    gap: 14px;
}

.logo {
    width: 52px;
    height: 52px;
    flex: none;
    display: grid;
    place-items: center;
    border-radius: 16px;
    background: var(--primary);
    color: #fff;
    font-size: 24px;
    font-weight: 700;
}

.header-text { flex: 1; min-width: 0; }
.app-header h1 { margin: 0; font-size: 24px; line-height: 1.3; }
.app-header p { margin: 2px 0 0; color: var(--muted); font-size: 13px; }

.count-badge {
    min-width: 46px;
    padding: 6px 12px;
    border-radius: 999px;
    background: var(--primary-soft);
    color: var(--primary);
    font-weight: 700;
    text-align: center;
    font-variant-numeric: tabular-nums;
}

/* ========== البطاقة الرئيسية ========== */
.app-card {
    width: min(1000px, calc(100% - 32px));
    margin: 0 auto 40px;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 24px;
    box-shadow: var(--shadow);
}

.sticky-search {
    position: sticky;
    top: 0;
    z-index: 20;
    padding: 16px 18px 12px;
    background: var(--surface);
    border-radius: 24px 24px 0 0;
}

.search-box { position: relative; }

.search-box svg {
    position: absolute;
    inset-inline-start: 16px;
    top: 50%;
    width: 20px;
    height: 20px;
    transform: translateY(-50%);
    color: var(--muted);
    pointer-events: none;
}

#searchInput {
    display: block;
    width: 100%;
    height: 52px;
    padding-inline: 46px 16px;
    border: 1px solid var(--border);
    border-radius: 14px;
    background: var(--surface-2);
    font-size: 16px; /* 16px يمنع الآيفون من تكبير الصفحة عند الكتابة */
    outline: none;
    transition: border-color .15s, box-shadow .15s;
}

#searchInput:focus {
    border-color: var(--primary);
    background: var(--surface);
    box-shadow: 0 0 0 3px var(--primary-soft);
}

.search-status:empty { display: none; }

.search-status {
    margin: 6px 4px 0;
    font-size: 13px;
    color: var(--muted);
}

/* ========== الأزرار ========== */
.toolbar {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(128px, 1fr));
    gap: 8px;
    padding: 4px 18px 16px;
    border-bottom: 1px solid var(--border);
}

.btn {
    min-height: 44px;
    padding: 0 14px;
    border: 1px solid var(--border);
    border-radius: 12px;
    background: var(--surface);
    font-size: 14px;
    font-weight: 700;
    white-space: nowrap;
    transition: background .15s, transform .1s;
}

.btn:hover { background: var(--surface-2); }
.btn:active { transform: scale(.97); }

.btn.primary {
    background: var(--primary);
    border-color: var(--primary);
    color: var(--primary-ink);
}
.btn.primary:hover { filter: brightness(1.06); }

.btn.danger {
    color: var(--danger);
    border-color: var(--danger-border);
    background: var(--danger-soft);
}

/* ========== القائمة ========== */
.contacts-list { padding: 4px 0 10px; }

.letter-head {
    padding: 14px 22px 4px;
    color: var(--primary);
    font-size: 13px;
    font-weight: 700;
}

.contact-item {
    display: flex;
    align-items: flex-start;
    gap: 12px;
    padding: 12px 18px;
    border-top: 1px solid var(--border);
}

.letter-head + .contact-item,
.contacts-list > .contact-item:first-child { border-top: none; }

.avatar {
    --h: 210;
    flex: none;
    width: 42px;
    height: 42px;
    border-radius: 50%;
    display: grid;
    place-items: center;
    background: hsl(var(--h) 60% 92%);
    color: hsl(var(--h) 55% 32%);
    font-weight: 700;
    font-size: 17px;
}

@media (prefers-color-scheme: dark) {
    .avatar {
        background: hsl(var(--h) 35% 24%);
        color: hsl(var(--h) 70% 82%);
    }
}

.contact-info { flex: 1; min-width: 0; }

.contact-info h3 {
    margin: 2px 0 2px;
    font-size: 16px;
    overflow-wrap: anywhere;
}

.phone-row {
    display: flex;
    align-items: center;
    gap: 2px;
}

.phone {
    flex: 1;
    min-width: 0;
    overflow: hidden;
    white-space: nowrap;
    text-overflow: ellipsis;
    text-align: right;
    color: var(--muted);
    font-size: 15px;
    font-variant-numeric: tabular-nums;
}

.no-phone { margin: 0; color: var(--muted); font-size: 14px; }

.icon-btn {
    flex: none;
    width: 36px;
    height: 36px;
    display: inline-grid;
    place-items: center;
    border: none;
    border-radius: 10px;
    background: transparent;
    color: var(--muted);
    text-decoration: none;
    font-size: 20px;
    line-height: 1;
}

.icon-btn:hover { background: var(--surface-2); color: var(--primary); }
.icon-btn.wa:hover { color: var(--green); }
.icon-btn svg { width: 19px; height: 19px; }
.edit-btn { margin-top: 2px; }

/* ========== الحالات الفارغة ========== */
.empty-state {
    min-height: 380px;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 6px;
    padding: 24px;
    text-align: center;
}

.empty-state h2 { margin: 0; font-size: 20px; }
.empty-state p { margin: 0; color: var(--muted); }
.empty-state .hint { font-size: 13px; margin-top: 10px; }

.empty-actions {
    display: flex;
    flex-wrap: wrap;
    justify-content: center;
    gap: 8px;
    margin-top: 14px;
}

/* ========== نافذة الإضافة والتعديل ========== */
dialog {
    width: min(440px, calc(100% - 32px));
    padding: 0;
    border: 1px solid var(--border);
    border-radius: 20px;
    background: var(--surface);
    color: var(--text);
    box-shadow: var(--shadow);
}

dialog::backdrop { background: var(--overlay); }

#contactForm {
    display: flex;
    flex-direction: column;
    gap: 14px;
    padding: 22px;
}

#contactForm h2 { margin: 0; font-size: 19px; }

.field { display: flex; flex-direction: column; gap: 6px; }
.field > span { font-size: 13px; font-weight: 700; color: var(--muted); }

.field input {
    width: 100%;
    height: 46px;
    padding: 0 14px;
    border: 1px solid var(--border);
    border-radius: 12px;
    background: var(--surface-2);
    font-size: 16px;
    outline: none;
}

.field input:focus { border-color: var(--primary); box-shadow: 0 0 0 3px var(--primary-soft); }
.field input[dir="ltr"] { text-align: left; }

.phones-fields { display: flex; flex-direction: column; gap: 8px; }
.phone-input-row { display: flex; gap: 6px; align-items: center; }

.link-button {
    align-self: flex-start;
    padding: 4px 0;
    border: none;
    background: none;
    color: var(--primary);
    font-weight: 700;
}

.dialog-actions { display: flex; gap: 8px; margin-top: 4px; }
.dialog-actions .danger { margin-inline-start: auto; }

/* ========== الإشعار ========== */
.toast {
    position: fixed;
    left: 50%;
    bottom: calc(20px + env(safe-area-inset-bottom));
    z-index: 1000;
    display: flex;
    align-items: center;
    gap: 14px;
    max-width: calc(100% - 32px);
    padding: 12px 18px;
    border-radius: 14px;
    background: #17324d;
    color: #fff;
    font-size: 14px;
    box-shadow: 0 10px 30px rgba(0, 0, 0, 0.25);
    transform: translateX(-50%);
    animation: toast-in .2s ease-out;
}

.toast[hidden] { display: none; }

.toast-undo {
    border: none;
    background: none;
    color: #8fc3ff;
    font-weight: 700;
    padding: 0;
}

@keyframes toast-in {
    from { opacity: 0; transform: translate(-50%, 10px); }
    to { opacity: 1; transform: translate(-50%, 0); }
}

/* ========== السحب والإفلات ========== */
.drop-overlay {
    position: fixed;
    inset: 12px;
    z-index: 900;
    display: grid;
    place-items: center;
    border: 3px dashed var(--primary);
    border-radius: 24px;
    background: var(--primary-soft);
    color: var(--primary);
    font-size: 22px;
    font-weight: 700;
    backdrop-filter: blur(4px);
    pointer-events: none;
}

.drop-overlay[hidden] { display: none; }

/* ========== الجوال ========== */
@media (max-width: 560px) {
    .app-header { margin: 18px auto 14px; }
    .app-header h1 { font-size: 21px; }
    .logo { width: 46px; height: 46px; font-size: 21px; border-radius: 14px; }
    .app-card { border-radius: 20px; }
    .sticky-search { padding: 12px 12px 10px; border-radius: 20px 20px 0 0; }
    .toolbar { padding: 4px 12px 12px; grid-template-columns: repeat(2, 1fr); }
    .toolbar #addButton { grid-column: 1 / -1; }
    .btn { font-size: 13px; padding: 0 8px; }
    .contact-item { padding: 10px 12px; gap: 10px; }
    .letter-head { padding: 12px 16px 2px; }
    .avatar { width: 36px; height: 36px; font-size: 15px; }
    .contact-info h3 { font-size: 15px; }
    .phone { font-size: 14px; }
    .icon-btn { width: 32px; height: 32px; }
    .icon-btn svg { width: 17px; height: 17px; }
    .app-card, .app-header { width: calc(100% - 20px); }
}

@media (prefers-reduced-motion: reduce) {
    * { animation: none !important; transition: none !important; }
}
