@import url('https://fonts.googleapis.com/css2?family=Cairo:wght@400;500;600;700&display=swap');

:root {
    --primary: #0f6e56;
    --primary-dark: #085041;
    --primary-light: #e1f5ee;
    --danger: #a32d2d;
    --danger-light: #fcebeb;
    --warning: #854f0b;
    --warning-light: #faeeda;
    --info: #185fa5;
    --info-light: #e6f1fb;
    --success: #3b6d11;
    --success-light: #eaf3de;
    --gray-light: #f1efe8;
    --border: #d9d6cc;
    --text-primary: #2c2c2a;
    --text-secondary: #5f5e5a;
    --text-muted: #888780;
    --surface: #ffffff;
    --bg: #f7f6f2;
    --radius: 10px;
    --shadow: 0 1px 3px rgba(0,0,0,.06), 0 1px 2px rgba(0,0,0,.04);
}

* { box-sizing: border-box; }

html, body {
    margin: 0;
    padding: 0;
    background: var(--bg);
    color: var(--text-primary);
    font-family: 'Cairo', Tahoma, Arial, sans-serif;
    font-size: 15px;
    line-height: 1.7;
}

body { direction: rtl; text-align: right; }

a { color: var(--primary); text-decoration: none; }
a:hover { text-decoration: underline; }

h1, h2, h3, h4 { margin: 0 0 .5em; font-weight: 600; }
h1 { font-size: 22px; }
h2 { font-size: 18px; }
h3 { font-size: 16px; }

p { margin: 0 0 .75em; }

/* ---------------- Layout ---------------- */

.app-shell { display: flex; min-height: 100vh; }

.sidebar {
    width: 240px;
    background: var(--primary-dark);
    color: #fff;
    flex-shrink: 0;
    padding: 1rem 0;
    position: sticky;
    top: 0;
    height: 100vh;
    overflow-y: auto;
}
.sidebar .brand {
    display: flex;
    align-items: center;
    gap: .6rem;
    padding: 0 1.2rem 1rem;
    font-weight: 700;
    font-size: 16px;
    border-bottom: 1px solid rgba(255,255,255,.15);
    margin-bottom: .5rem;
}
.sidebar .brand-logo { width: 36px; height: 36px; border-radius: 8px; flex-shrink: 0; }
.sidebar .brand-text { min-width: 0; }
.sidebar .brand-text small { display: block; font-weight: 400; font-size: 12px; opacity: .75; }
.sidebar nav a {
    display: flex;
    align-items: center;
    gap: 10px;
    color: #fff;
    opacity: .85;
    padding: .65rem 1.2rem;
    font-size: 14px;
    text-decoration: none;
}
.sidebar nav a:hover, .sidebar nav a.active { opacity: 1; background: rgba(255,255,255,.1); }
/* زرار "تثبيت التطبيق" — نفس شكل روابط القائمة الجانبية بالظبط، لكنه
   <button> حقيقي (مش <a>) لأنه بيستدعي نافذة تثبيت المتصفح بجافاسكريبت
   مش بيروح لصفحة تانية. مخفي بالـ inline style افتراضيًا ولا يظهر إلا لو
   المتصفح فعليًا قابل للتثبيت (راجع app.js). */
.sidebar nav button.js-install-app {
    align-items: center;
    gap: 10px;
    width: 100%;
    color: #fff;
    opacity: .85;
    padding: .65rem 1.2rem;
    font-size: 14px;
    font-family: inherit;
    background: none;
    border: none;
    text-align: right;
    cursor: pointer;
}
.sidebar nav button.js-install-app:hover { opacity: 1; background: rgba(255,255,255,.1); }
.sidebar nav .section-label {
    padding: 1rem 1.2rem .3rem;
    font-size: 11px;
    text-transform: uppercase;
    letter-spacing: .05em;
    opacity: .55;
}

.main { flex: 1; min-width: 0; }

.topbar {
    background: var(--surface);
    border-bottom: 1px solid var(--border);
    padding: .8rem 1.5rem;
    display: flex;
    align-items: center;
    justify-content: space-between;
    position: sticky;
    top: 0;
    z-index: 10;
}
.topbar .user-box { display: flex; align-items: center; gap: 14px; font-size: 14px; }
.topbar .user-box .role-tag { color: var(--text-secondary); font-size: 12px; }
.topbar .bell { position: relative; font-size: 20px; color: var(--text-secondary); }
.topbar .bell .dot {
    position: absolute; top: -4px; left: -4px;
    background: var(--danger); color: #fff;
    border-radius: 50%; font-size: 10px;
    min-width: 16px; height: 16px; line-height: 16px; text-align: center;
    padding: 0 3px;
}

.content { padding: 1.5rem; max-width: 1100px; margin: 0 auto; }

/* ---------------- Cards / grid ---------------- */

.card {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 1.2rem 1.4rem;
    margin-bottom: 1.2rem;
    box-shadow: var(--shadow);
}

.grid { display: grid; gap: 1rem; }
.grid-2 { grid-template-columns: repeat(2, minmax(0,1fr)); }
.grid-3 { grid-template-columns: repeat(3, minmax(0,1fr)); }
.grid-4 { grid-template-columns: repeat(4, minmax(0,1fr)); }
@media (max-width: 780px) {
    .grid-2, .grid-3, .grid-4 { grid-template-columns: 1fr 1fr; }
}
@media (max-width: 520px) {
    .grid-2, .grid-3, .grid-4 { grid-template-columns: 1fr; }
}

.stat-card { text-align: center; }
.stat-card .num { font-size: 26px; font-weight: 700; color: var(--primary-dark); }
.stat-card .label { color: var(--text-secondary); font-size: 13px; }

/* ---------------- Forms ---------------- */

label { display: block; font-size: 13px; font-weight: 600; margin-bottom: 4px; color: var(--text-secondary); }
.field { margin-bottom: 1rem; }
input[type=text], input[type=number], input[type=password],
input[type=date], input[type=time], input[type=file], select, textarea {
    width: 100%;
    padding: .55rem .75rem;
    border: 1px solid var(--border);
    border-radius: 8px;
    font-family: inherit;
    font-size: 14px;
    background: var(--surface);
    color: var(--text-primary);
}
input:focus, select:focus, textarea:focus {
    outline: none;
    border-color: var(--primary);
    box-shadow: 0 0 0 3px var(--primary-light);
}
textarea { resize: vertical; min-height: 80px; }
.hint { font-size: 12px; color: var(--text-muted); margin-top: 4px; }
.form-actions { display: flex; gap: .6rem; margin-top: 1.2rem; }

/* مجموعة نماذج قرار جنب بعض (موافقة/رفض...) — بتترص تحت بعض تلقائيًا على الموبايل */
.action-group { display: flex; gap: 1rem; flex-wrap: wrap; }
.action-form { flex: 1; min-width: 260px; }

/* ---------------- Buttons ---------------- */

.btn {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    padding: .55rem 1.1rem;
    border-radius: 8px;
    border: 1px solid var(--border);
    background: var(--surface);
    color: var(--text-primary);
    font-family: inherit;
    font-size: 14px;
    font-weight: 600;
    cursor: pointer;
    text-decoration: none;
}
.btn:hover { background: var(--gray-light); text-decoration: none; }
.btn-primary { background: var(--primary); border-color: var(--primary); color: #fff; }
.btn-primary:hover { background: var(--primary-dark); }
.btn-danger { background: var(--danger); border-color: var(--danger); color: #fff; }
.btn-danger:hover { opacity: .9; }
.btn-sm { padding: .35rem .7rem; font-size: 13px; }
.btn-block { width: 100%; justify-content: center; }
.btn:disabled { opacity: .5; cursor: not-allowed; }

/* ---------------- Tables ---------------- */

table { width: 100%; border-collapse: collapse; font-size: 14px; }
table.data th, table.data td {
    padding: .6rem .7rem;
    border-bottom: 1px solid var(--border);
    text-align: right;
}
table.data th { background: var(--gray-light); font-weight: 600; font-size: 13px; color: var(--text-secondary); }
table.data tr:hover td { background: #fafaf7; }

/* جداول عرض/تقارير عريضة بحقول جوه الخلايا (لو فيه أي) — بنقلل البادينج
   وعرض الحقول جوه الخلية بدل ما تاخد 100%. جداول إدخال البيانات لكل صنف
   (.entry-table: أصناف الطلب، مراجعة المخازن، استلام الشحنة، تسجيل
   الموردين) مستثناة من القاعدة دي عمدًا — الحقول فيها لازم تاخد عرضها
   الطبيعي (خصوصًا على الموبايل، راجع الـ media query الخاص بيها تحت). */
table.data:not(.entry-table) td input[type=text],
table.data:not(.entry-table) td input[type=number],
table.data:not(.entry-table) td input[type=date] {
    width: auto;
    min-width: 90px;
    max-width: 140px;
    padding: .35rem .5rem;
    font-size: 13px;
}
table.data td { vertical-align: middle; }

/* حقول محسوبة تلقائيًا وبس للعرض (زي "سعر الوحدة بعد الخصم") — مش قابلة
   للتعديل المباشر، فبنميزها بخلفية رمادية عشان تبان مختلفة عن باقي الحقول */
input[readonly] {
    background: var(--gray-light);
    color: var(--text-secondary);
    cursor: default;
}

/* ---------------- Badges ---------------- */

.badge {
    display: inline-block;
    padding: .2rem .65rem;
    border-radius: 999px;
    font-size: 12px;
    font-weight: 600;
    white-space: nowrap;
}
.badge-gray   { background: var(--gray-light); color: var(--text-secondary); }
.badge-amber  { background: var(--warning-light); color: var(--warning); }
.badge-red    { background: var(--danger-light); color: var(--danger); }
.badge-teal   { background: var(--primary-light); color: var(--primary-dark); }
.badge-blue   { background: var(--info-light); color: var(--info); }
.badge-green  { background: var(--success-light); color: var(--success); }

.priority-urgent { color: var(--danger); font-weight: 700; }
.priority-normal { color: var(--warning); font-weight: 600; }
.priority-low    { color: var(--success); font-weight: 600; }

/* ---------------- Alerts / flash ---------------- */

.alert { padding: .8rem 1rem; border-radius: 8px; margin-bottom: 1rem; font-size: 14px; }
.alert-success { background: var(--success-light); color: var(--success); }
.alert-error   { background: var(--danger-light); color: var(--danger); }
.alert-info    { background: var(--info-light); color: var(--info); }

/* ---------------- Timeline ---------------- */

.timeline { list-style: none; margin: 0; padding: 0; }
.timeline li {
    position: relative;
    padding-inline-start: 24px;
    padding-bottom: 1.2rem;
    border-inline-start: 2px solid var(--border);
    margin-inline-start: 6px;
}
.timeline li:last-child { border-color: transparent; }
.timeline li::before {
    content: '';
    position: absolute;
    right: -7px;
    top: 2px;
    width: 12px; height: 12px;
    border-radius: 50%;
    background: var(--primary);
    border: 2px solid var(--surface);
}
/* v10: ألوان نقاط التتبع — تم (أخضر)، بانتظار (كهرماني)، مرفوض (أحمر) */
.timeline li.tl-done::before    { background: var(--success); }
.timeline li.tl-waiting::before { background: #fff; border-color: var(--warning); box-shadow: 0 0 0 2px var(--warning-light); }
.timeline li.tl-waiting { opacity: .8; }
.timeline li.tl-waiting.tl-current { opacity: 1; }
.timeline li.tl-waiting.tl-current::before { background: var(--warning); border-color: #fff; box-shadow: 0 0 0 3px var(--warning-light); }
.timeline li.tl-waiting.tl-current .t-title { color: var(--warning); }
.timeline li.tl-rejected::before { background: var(--danger); }
.timeline li.tl-rejected .t-title { color: var(--danger); }
.timeline .t-title { font-weight: 600; font-size: 14px; }
.timeline .t-meta { font-size: 12px; color: var(--text-muted); }
.timeline .t-note { font-size: 13px; color: var(--text-secondary); margin-top: 2px; }

/* ---------------- Login page ---------------- */

.login-wrap {
    min-height: 100vh;
    display: flex;
    align-items: center;
    justify-content: center;
    background: linear-gradient(180deg, var(--primary-dark), var(--primary));
    padding: 1rem;
}
.login-card {
    background: var(--surface);
    border-radius: 16px;
    padding: 2.2rem 2rem;
    width: 100%;
    max-width: 380px;
    box-shadow: 0 10px 30px rgba(0,0,0,.15);
    text-align: center;
}
.login-logo { width: 72px; height: 72px; border-radius: 16px; margin-bottom: .8rem; }
.login-card h1 { font-size: 18px; margin-bottom: 1.5rem; }
.login-card .sub { color: var(--text-secondary); font-size: 13px; margin-bottom: 1.5rem; }

/* ---------------- Print (official purchase order doc) ---------------- */

@media print {
    .no-print { display: none !important; }
    body { background: #fff; }
}

.mobile-toggle { display: none; }

/* =========================================================================
   الموبايل (Mobile)
   ========================================================================= */

/* يمنع أي ظهور لسكرول أفقي على مستوى الصفحة كلها (لو عنصر واحد طلع بره
   الشاشة بالغلط)، مع الإبقاء على سكرول أفقي مقصود للجداول العريضة بس */
html, body { max-width: 100%; overflow-x: hidden; }

@media (max-width: 900px) {
    .sidebar { position: fixed; right: 0; top: 0; z-index: 50; width: 78vw; max-width: 280px; transform: translateX(100%); transition: transform .2s; box-shadow: -4px 0 20px rgba(0,0,0,.25); }
    .sidebar.open { transform: translateX(0); }
    .mobile-toggle { display: block; font-size: 22px; background: none; border: none; cursor: pointer; color: var(--text-primary); padding: 4px 8px; }
    .content { padding: 1rem; }
}

@media (max-width: 700px) {
    /* الجداول عادةً أعرض من شاشة الموبايل — بدل ما تتكسر أو تتزنق، بتاخد
       سكرول أفقي خاص بيها بس (الجدول عمومًا مش الصفحة كلها) */
    table.data {
        display: block;
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
        white-space: nowrap;
        border-bottom: 1px solid var(--border);
    }
    table.data th, table.data td { white-space: nowrap; }

    /* جداول إدخال البيانات لكل صنف (أصناف الطلب، مراجعة المخازن، استلام
       الشحنة، تسجيل الموردين): سكرول أفقي مش مناسب هنا — المستخدم بيدخل
       بيانات (مش بيقرا تقرير)، ولو أعمدة زي "الكمية" أو حقول الأسعار
       اتقطعت برّه الشاشة من غير أي إشارة واضحة إنه لازم يعمل سحب لليسار،
       هيفتكر إنها مش موجودة أصلًا. فبدل السكرول الأفقي، كل صف (صنف) بيتحول
       لكارت مستقل وكل حقل بيوخد سطر لوحده بعرض الشاشة كامل مع اسمه فوقه. */
    table.entry-table {
        display: block;
        overflow-x: visible;
        white-space: normal;
        border-bottom: none;
    }
    table.entry-table thead { display: none; }
    table.entry-table tbody { display: block; }
    table.entry-table tbody tr {
        display: block;
        white-space: normal;
        border: 1px solid var(--border);
        border-radius: var(--radius);
        padding: .8rem .9rem;
        margin-bottom: .8rem;
    }
    table.entry-table tbody tr td {
        display: block;
        width: 100%;
        border-bottom: none;
        padding: .4rem 0;
        white-space: normal;
    }
    table.entry-table tbody tr td[data-label]:not([data-label=""])::before {
        content: attr(data-label);
        display: block;
        font-size: 12px;
        font-weight: 600;
        color: var(--text-secondary);
        margin-bottom: 4px;
    }
    table.entry-table tbody tr td input:not([type=checkbox]):not([type=radio]),
    table.entry-table tbody tr td select {
        width: 100%;
        max-width: none;
    }
    table.entry-table tbody tr td input[type=checkbox],
    table.entry-table tbody tr td input[type=radio] {
        width: 22px;
        height: 22px;
    }
    table.entry-table tbody tr td:last-child { padding-top: .5rem; }
    table.entry-table tbody tr td:last-child .btn { width: 100%; justify-content: center; }

    /* أزرار الإجراءات (موافقة/رفض/حفظ) بتاخد عرض الشاشة كاملة على الموبايل
       عشان تبقى سهلة اللمس، وتترص فوق بعض بدل جنب بعض */
    .form-actions { flex-direction: column; }
    .form-actions .btn { width: 100%; justify-content: center; }

    /* نفس الفكرة لمجموعات أزرار القرار (موافقة/رفض) جوه صفحة تفاصيل الطلب */
    .action-form { min-width: 100%; }

    .card { padding: 1rem 1.1rem; }
    h1 { font-size: 19px; }
    h2 { font-size: 17px; }
}

@media (max-width: 480px) {
    .content { padding: .75rem; }
    .card { padding: .9rem 1rem; margin-bottom: 1rem; }

    .topbar { padding: .6rem .8rem; gap: .5rem; }
    .topbar h1 { font-size: 13px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
    .topbar .user-box { gap: 8px; font-size: 13px; }
    .topbar .user-box .role-tag { display: none; } /* اسم الدور/القسم بيتاخد مساحة زيادة على شاشة صغيرة جدًا */

    .stat-card .num { font-size: 22px; }

    /* أزرار كبيرة كفاية للمس بالإصبع براحة (48px تقريبًا ارتفاع أدنى) */
    .btn { padding: .7rem 1rem; min-height: 44px; }
    .btn-sm { padding: .45rem .8rem; min-height: 36px; }

    input[type=text], input[type=number], input[type=password],
    input[type=date], input[type=time], select, textarea {
        font-size: 16px; /* يمنع سفاري بتاع الآيفون من عمل زوم تلقائي لما تدوس على حقل الإدخال */
        padding: .65rem .8rem;
    }
}
