/* ===== المتغيرات والثيمات ===== */
:root {
    --bg: #f1f5f9;
    --surface: #ffffff;
    --surface-2: #f8fafc;
    --border: #e2e8f0;
    --text: #0f172a;
    --text-muted: #64748b;
    --primary: #4f46e5;
    --primary-hover: #4338ca;
    --primary-soft: #eef2ff;
    --danger: #dc2626;
    --success: #16a34a;
    --radius: 12px;
    --shadow: 0 1px 3px rgba(15, 23, 42, .08);
}

[data-theme="dark"] {
    --bg: #0f172a;
    --surface: #1e293b;
    --surface-2: #172033;
    --border: #334155;
    --text: #f1f5f9;
    --text-muted: #94a3b8;
    --primary: #818cf8;
    --primary-hover: #a5b4fc;
    --primary-soft: #26264d;
    --shadow: 0 1px 3px rgba(0, 0, 0, .4);
}

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

body {
    font-family: "Segoe UI", Tahoma, Arial, sans-serif;
    background: var(--bg);
    color: var(--text);
    font-size: 15px;
    line-height: 1.7;
}

a { color: var(--primary); text-decoration: none; }
.link:hover { text-decoration: underline; }
.muted { color: var(--text-muted); font-size: .85rem; }
code { background: var(--surface-2); padding: 2px 6px; border-radius: 6px; font-size: .8rem; }

/* ===== الهيكل العام ===== */
.app { display: flex; min-height: 100vh; }

.sidebar {
    width: 250px;
    background: var(--surface);
    border-left: 1px solid var(--border);
    display: flex;
    flex-direction: column;
    position: sticky;
    top: 0;
    height: 100vh;
    flex-shrink: 0;
}

.brand {
    display: flex;
    align-items: center;
    gap: .6rem;
    padding: 1.2rem 1.2rem .8rem;
    font-size: 1.15rem;
    font-weight: 700;
}
.brand-icon { color: var(--primary); font-size: 1.3rem; }
.brand-center { justify-content: center; padding: 0 0 .4rem; }

.nav { flex: 1; padding: .6rem; overflow-y: auto; }
.nav-section {
    font-size: .75rem;
    color: var(--text-muted);
    padding: 1rem .8rem .3rem;
    text-transform: uppercase;
}
.nav-link {
    display: flex;
    align-items: center;
    gap: .7rem;
    padding: .55rem .8rem;
    border-radius: 8px;
    color: var(--text);
    margin-bottom: 2px;
}
.nav-link:hover { background: var(--primary-soft); color: var(--primary); }

.sidebar-footer { padding: .9rem; border-top: 1px solid var(--border); }
.user-chip { display: flex; align-items: center; gap: .6rem; margin-bottom: .6rem; }
.avatar {
    width: 36px; height: 36px;
    border-radius: 50%;
    background: var(--primary);
    color: #fff;
    display: flex; align-items: center; justify-content: center;
    font-weight: 700;
    flex-shrink: 0;
}
.user-name { font-weight: 600; font-size: .9rem; }
.user-role { font-size: .75rem; color: var(--text-muted); }

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

.topbar {
    display: flex;
    align-items: center;
    gap: .6rem;
    padding: .7rem 1.4rem;
    background: var(--surface);
    border-bottom: 1px solid var(--border);
    position: sticky;
    top: 0;
    z-index: 10;
}
.topbar-spacer { flex: 1; }
.icon-btn {
    background: none;
    border: 1px solid var(--border);
    border-radius: 8px;
    width: 36px; height: 36px;
    cursor: pointer;
    font-size: 1rem;
    color: var(--text);
}
.icon-btn:hover { background: var(--surface-2); }
#sidebarToggle { display: none; }

.content { padding: 1.6rem; max-width: 1200px; width: 100%; margin: 0 auto; }

/* ===== المكونات ===== */
.page-header {
    display: flex;
    align-items: flex-start;
    justify-content: space-between;
    gap: 1rem;
    margin-bottom: 1.4rem;
    flex-wrap: wrap;
}
.page-header h1 { font-size: 1.5rem; }
.meta-row { display: flex; align-items: center; gap: .6rem; margin-top: .3rem; flex-wrap: wrap; }

.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);
}
.card-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: .9rem;
}
.card-header h2 { font-size: 1.05rem; }
.card-form { max-width: 760px; }

.stats-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(160px, 1fr));
    gap: 1rem;
    margin-bottom: 1.4rem;
}
.stat-card {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 1rem 1.2rem;
    box-shadow: var(--shadow);
}
.stat-value { font-size: 1.9rem; font-weight: 800; color: var(--primary); }
.stat-label { color: var(--text-muted); font-size: .85rem; }

.grid-dashboard {
    display: grid;
    grid-template-columns: 2fr 1fr;
    gap: 1.2rem;
    align-items: start;
}
.side-cards { display: flex; flex-direction: column; }

/* ===== الجداول ===== */
.table { width: 100%; border-collapse: collapse; }
.table th {
    text-align: right;
    font-size: .78rem;
    color: var(--text-muted);
    padding: .5rem .6rem;
    border-bottom: 1px solid var(--border);
}
.table td { padding: .6rem; border-bottom: 1px solid var(--border); vertical-align: middle; }
.table tr:last-child td { border-bottom: none; }
.row-actions { display: flex; gap: .4rem; align-items: center; flex-wrap: wrap; }
.row-actions form { display: inline; }

/* ===== الأزرار والحقول ===== */
.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: .4rem;
    padding: .5rem 1rem;
    border-radius: 8px;
    border: 1px solid var(--border);
    background: var(--surface);
    color: var(--text);
    font-size: .88rem;
    font-family: inherit;
    cursor: pointer;
    white-space: nowrap;
}
.btn:hover { background: var(--surface-2); }
.btn-primary { background: var(--primary); border-color: var(--primary); color: #fff; }
.btn-primary:hover { background: var(--primary-hover); border-color: var(--primary-hover); }
.btn-danger { color: var(--danger); border-color: var(--danger); background: transparent; }
.btn-danger:hover { background: var(--danger); color: #fff; }
.btn-ghost { border-color: transparent; background: transparent; color: var(--text-muted); }
.btn-sm { padding: .25rem .6rem; font-size: .8rem; }
.btn-block { width: 100%; }
.btn-group { display: flex; gap: .5rem; align-items: center; }

.field { margin-bottom: 1rem; }
.field label { display: block; font-size: .85rem; font-weight: 600; margin-bottom: .3rem; }
.field input, .field select, .field textarea, .filters input, .filters select, textarea#aiPrompt {
    width: 100%;
    padding: .55rem .8rem;
    border: 1px solid var(--border);
    border-radius: 8px;
    background: var(--surface-2);
    color: var(--text);
    font-family: inherit;
    font-size: .9rem;
}
.field input:focus, .field select:focus, .field textarea:focus, textarea#aiPrompt:focus {
    outline: 2px solid var(--primary);
    outline-offset: 0;
    border-color: var(--primary);
}
.field-checkbox label { display: flex; align-items: center; gap: .5rem; font-weight: normal; }
.field-checkbox input { width: auto; }
.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 0 1rem; }
.form-actions { display: flex; align-items: center; gap: .6rem; margin-top: .4rem; }
.form-section-title { margin: 1.1rem 0 .7rem; font-size: .95rem; color: var(--primary); }

.filters { display: flex; gap: .6rem; margin-bottom: 1.2rem; flex-wrap: wrap; }
.filters input[type="search"] { flex: 2; min-width: 200px; }
.filters select { flex: 1; min-width: 140px; width: auto; }
.filters .btn { flex-shrink: 0; }

/* ===== الشارات والتنبيهات ===== */
.badge {
    display: inline-block;
    padding: .15rem .6rem;
    border-radius: 999px;
    font-size: .74rem;
    font-weight: 600;
    background: var(--surface-2);
    border: 1px solid var(--border);
}
.badge-draft { color: var(--text-muted); }
.badge-in_progress { color: #2563eb; border-color: #2563eb44; background: #2563eb11; }
.badge-review { color: #d97706; border-color: #d9770644; background: #d9770611; }
.badge-approved { color: var(--success); border-color: #16a34a44; background: #16a34a11; }
.badge-completed { color: var(--primary); border-color: transparent; background: var(--primary-soft); }
.badge-archived { color: var(--text-muted); }

.alert {
    padding: .7rem 1rem;
    border-radius: 10px;
    margin-bottom: 1rem;
    font-size: .9rem;
    border: 1px solid;
}
.alert-success { background: #16a34a14; border-color: #16a34a55; color: var(--success); }
.alert-error { background: #dc262614; border-color: #dc262655; color: var(--danger); }

.empty-state { color: var(--text-muted); text-align: center; padding: 1.5rem 0; }
.status-list { list-style: none; }
.status-list li {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: .4rem 0;
    border-bottom: 1px solid var(--border);
    font-size: .88rem;
}
.status-list li:last-child { border-bottom: none; }

.tags { display: flex; gap: .4rem; flex-wrap: wrap; margin-top: .8rem; }
.tag {
    background: var(--primary-soft);
    color: var(--primary);
    padding: .1rem .6rem;
    border-radius: 999px;
    font-size: .78rem;
}
.prose { white-space: pre-line; }

/* ===== المساعد الذكي ===== */
.ai-assistant { border: 1px solid var(--primary); }
.ai-modes { display: flex; gap: .4rem; flex-wrap: wrap; margin-bottom: .8rem; }
.chip {
    padding: .25rem .8rem;
    border-radius: 999px;
    border: 1px solid var(--border);
    background: var(--surface-2);
    color: var(--text);
    font-size: .8rem;
    font-family: inherit;
    cursor: pointer;
}
.chip.active { background: var(--primary); color: #fff; border-color: var(--primary); }
textarea#aiPrompt { margin-bottom: .7rem; resize: vertical; }
.ai-result {
    margin-top: 1rem;
    background: var(--surface-2);
    border: 1px solid var(--border);
    border-radius: 10px;
    padding: .9rem;
}
.ai-result pre {
    white-space: pre-wrap;
    font-family: inherit;
    font-size: .92rem;
    max-height: 420px;
    overflow-y: auto;
}
.ai-result-toolbar { display: flex; justify-content: flex-end; margin-bottom: .4rem; }

/* ===== التعليقات ===== */
.comment-form { display: flex; gap: .6rem; align-items: flex-start; margin-bottom: 1rem; }
.comment-form textarea { flex: 1; }
.comment { padding: .7rem 0; border-top: 1px solid var(--border); }
.comment-head { display: flex; justify-content: space-between; margin-bottom: .2rem; }

/* ===== صفحات الدخول والتثبيت ===== */
.auth-page {
    display: flex;
    align-items: center;
    justify-content: center;
    min-height: 100vh;
    padding: 1.5rem;
    background: linear-gradient(135deg, var(--bg), var(--primary-soft));
}
.auth-card {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 16px;
    padding: 2rem;
    width: 100%;
    max-width: 420px;
    box-shadow: 0 10px 40px rgba(15, 23, 42, .12);
}
.auth-card-wide { max-width: 640px; }
.auth-card .brand { font-size: 1.4rem; }
.auth-subtitle {
    text-align: center;
    color: var(--text-muted);
    font-size: .88rem;
    margin-bottom: 1.4rem;
}

/* ===== التجاوب ===== */
@media (max-width: 900px) {
    .grid-dashboard { grid-template-columns: 1fr; }
    .grid-2 { grid-template-columns: 1fr; }

    #sidebarToggle { display: inline-flex; }
    .sidebar {
        position: fixed;
        right: 0;
        z-index: 50;
        transform: translateX(100%);
        transition: transform .25s ease;
        box-shadow: -8px 0 30px rgba(0,0,0,.25);
    }
    .sidebar.open { transform: translateX(0); }
    .content { padding: 1rem; }
}
