/* ============================================================
   Modals + Forms — أنماط موحَّدة لكل النوافذ المنبثقة (Add/Edit dialogs)
   كان السبب في الشاشات المكسورة سابقاً: غياب styling لكلاسات modal-backdrop،
   modal-card، form-row، form-actions
   ============================================================ */

/* ===== Modal overlay + card ===== */
.modal-backdrop {
    position: fixed;
    inset: 0;
    background: rgba(15, 23, 42, 0.6);
    backdrop-filter: blur(4px);
    -webkit-backdrop-filter: blur(4px);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 1050;
    padding: 20px;
    overflow-y: auto;
    animation: modal-fade-in 0.18s ease-out;
}

.modal-card {
    position: relative;  /* للسماح بـ X button بـ absolute positioning */
    background: var(--c-surface, #ffffff);
    border-radius: 16px;
    box-shadow: 0 20px 60px rgba(0, 0, 0, 0.25), 0 2px 8px rgba(0, 0, 0, 0.08);
    border: 1px solid var(--c-border, #e5e7eb);
    width: 100%;
    max-width: 560px;
    max-height: calc(100vh - 40px);
    overflow-y: auto;
    padding: 24px 28px;
    animation: modal-slide-up 0.22s cubic-bezier(0.34, 1.56, 0.64, 1);
}

.modal-card h3 {
    margin: 0 0 18px;
    font-size: 18px;
    font-weight: 800;
    color: var(--c-text, #1f2937);
    padding-bottom: 14px;
    border-bottom: 1px solid var(--c-border, #e5e7eb);
    padding-inline-end: 40px;  /* مساحة للزر X */
}

/* ===== X close button (top corner) ===== */
.modal-close {
    position: absolute;
    top: 14px;
    inset-inline-end: 14px;
    width: 36px;
    height: 36px;
    border: none;
    background: transparent;
    color: var(--c-text-muted, #6b7280);
    border-radius: 50%;
    cursor: pointer;
    display: grid;
    place-items: center;
    font-size: 22px;
    line-height: 1;
    transition: all 0.15s;
    z-index: 2;
    padding: 0;
}

.modal-close:hover {
    background: var(--c-bg-soft, #f3f4f6);
    color: var(--c-red, #dc2626);
    transform: rotate(90deg);
}

.modal-close .material-symbols-rounded {
    font-size: 22px;
}

@keyframes modal-fade-in {
    from { opacity: 0; }
    to { opacity: 1; }
}

@keyframes modal-slide-up {
    from { opacity: 0; transform: translateY(20px) scale(0.95); }
    to { opacity: 1; transform: translateY(0) scale(1); }
}

/* ===== Form grid layout (2 columns by default in modals) ===== */
.modal-card .form-grid,
.form-grid {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 16px;
    margin-bottom: 18px;
}

/* داخل الـ modal، اجعلها عمودين دائماً */
.modal-card .form-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
}

/* صفّ بعرض كامل (يمتد لكل الأعمدة) */
.form-row.full,
.modal-card .form-row.full {
    grid-column: 1 / -1;
}

/* على الشاشات الصغيرة: عمود واحد */
@media (max-width: 720px) {
    .modal-card .form-grid,
    .form-grid {
        grid-template-columns: 1fr;
    }
}

/* ===== Single form-row (label + input stacked) ===== */
.form-row {
    display: flex;
    flex-direction: column;
    gap: 6px;
    min-width: 0;
}

.form-row label {
    color: var(--c-text, #1f2937);
    font-weight: 700;
    font-size: 12.5px;
    text-align: start;
    margin: 0;
    line-height: 1.4;
}

.form-row label .req {
    color: var(--c-red, #dc2626);
    margin-inline-start: 3px;
    font-weight: 800;
}

/* ===== Inputs — أوضح وأنيق =====
   نستخدم :not() لاستثناء الأنواع الخاصة (checkbox/radio/file/color)
   هذا يضمن استهداف <input> بدون type أيضاً (يُعامَل كـ text افتراضياً) */
.modal-card .form-row input:not([type="checkbox"]):not([type="radio"]):not([type="file"]):not([type="color"]):not([type="range"]),
.modal-card .form-row select,
.modal-card .form-row textarea,
.form-row input:not([type="checkbox"]):not([type="radio"]):not([type="file"]):not([type="color"]):not([type="range"]),
.form-row select,
.form-row textarea {
    width: 100%;
    background: #f9fafb;
    border: 1.5px solid #d1d5db;
    border-radius: 10px;
    height: 42px;
    padding: 0 14px;
    font-size: 14px;
    font-family: inherit;
    color: var(--c-text, #1f2937);
    transition: all 0.15s;
    box-sizing: border-box;
    outline: none;
    display: block;
}

.form-row input::placeholder,
.form-row textarea::placeholder {
    color: #9ca3af;
}

.form-row textarea {
    height: auto;
    min-height: 84px;
    padding: 10px 14px;
    resize: vertical;
    line-height: 1.55;
}

/* Color picker — حجم خاص ومربّع */
.form-row input[type="color"] {
    width: 60px;
    height: 42px;
    padding: 4px;
    border: 1.5px solid #d1d5db;
    border-radius: 10px;
    background: #ffffff;
    cursor: pointer;
    box-sizing: border-box;
}

.form-row input[type="color"]:hover {
    border-color: #9ca3af;
}

/* Hover effect */
.form-row input:hover:not(:disabled):not(:focus):not([type="checkbox"]):not([type="radio"]):not([type="color"]),
.form-row select:hover:not(:disabled):not(:focus),
.form-row textarea:hover:not(:disabled):not(:focus) {
    border-color: #9ca3af;
    background: #ffffff;
}

/* Focus state — أقوى وأوضح */
.form-row input:focus:not([type="checkbox"]):not([type="radio"]):not([type="color"]),
.form-row select:focus,
.form-row textarea:focus {
    border-color: var(--c-primary, #6366f1);
    background: #ffffff;
    box-shadow: 0 0 0 4px rgba(99, 102, 241, 0.15);
}

.form-row input:disabled,
.form-row select:disabled,
.form-row textarea:disabled {
    background: #f3f4f6;
    color: #9ca3af;
    cursor: not-allowed;
}

.form-row small {
    color: var(--c-text-muted, #6b7280);
    font-size: 11.5px;
    margin-top: 2px;
}

/* Checkbox / radio inline */
.form-row input[type="checkbox"],
.form-row input[type="radio"] {
    width: 18px;
    height: 18px;
    margin-inline-end: 8px;
    vertical-align: middle;
    accent-color: var(--c-primary, #6366f1);
    cursor: pointer;
}

/* ===== Form action buttons (footer of modal) ===== */
.form-actions {
    display: flex;
    justify-content: flex-end;
    gap: 10px;
    margin-top: 22px;
    padding-top: 18px;
    border-top: 1px solid var(--c-border, #e5e7eb);
    flex-wrap: wrap;
}

/* ===== Button styling داخل المودالات (override .btn) ===== */
.modal-card .btn,
.form-actions .btn {
    min-width: 100px;
    height: 42px;
    padding: 0 20px;
    border-radius: 10px;
    font-weight: 700;
    font-size: 14px;
    border: 1.5px solid #d1d5db;
    background: #ffffff;
    color: var(--c-text, #1f2937);
    cursor: pointer;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 6px;
    transition: all 0.15s;
    font-family: inherit;
}

.modal-card .btn:hover:not(:disabled),
.form-actions .btn:hover:not(:disabled) {
    background: #f9fafb;
    border-color: #9ca3af;
    transform: translateY(-1px);
    box-shadow: 0 2px 6px rgba(0, 0, 0, 0.08);
}

.modal-card .btn:active,
.form-actions .btn:active {
    transform: translateY(0);
}

.modal-card .btn:disabled,
.form-actions .btn:disabled {
    opacity: 0.5;
    cursor: not-allowed;
    transform: none;
}

/* Primary button — لون مميز للحفظ */
.modal-card .btn-primary,
.modal-card .btn.primary,
.form-actions .btn-primary,
.form-actions .btn.primary {
    background: linear-gradient(135deg, var(--c-primary, #6366f1), #8b5cf6);
    color: white;
    border-color: transparent;
    box-shadow: 0 2px 8px rgba(99, 102, 241, 0.25);
}

.modal-card .btn-primary:hover:not(:disabled),
.modal-card .btn.primary:hover:not(:disabled),
.form-actions .btn-primary:hover:not(:disabled),
.form-actions .btn.primary:hover:not(:disabled) {
    background: linear-gradient(135deg, #5558e3, #7c3aed);
    box-shadow: 0 4px 14px rgba(99, 102, 241, 0.4);
    color: white;
}

/* ===== RTL polish ===== */
[dir="rtl"] .form-row label {
    text-align: start;
}

.form-actions {
    justify-content: flex-end;
}

/* ===== Dark mode support ===== */
[data-theme="dark"] .modal-backdrop {
    background: rgba(0, 0, 0, 0.75);
}

[data-theme="dark"] .modal-card {
    background: #1f2937;
    border-color: #374151;
    box-shadow: 0 20px 60px rgba(0, 0, 0, 0.6);
}

[data-theme="dark"] .modal-card h3 {
    color: #f3f4f6;
    border-bottom-color: #374151;
}

[data-theme="dark"] .form-row input,
[data-theme="dark"] .form-row select,
[data-theme="dark"] .form-row textarea {
    background: #111827;
    color: #f3f4f6;
    border-color: #374151;
}

[data-theme="dark"] .form-row input:hover:not(:disabled):not(:focus),
[data-theme="dark"] .form-row select:hover:not(:disabled):not(:focus) {
    background: #1f2937;
    border-color: #4b5563;
}

[data-theme="dark"] .form-row input:focus,
[data-theme="dark"] .form-row select:focus,
[data-theme="dark"] .form-row textarea:focus {
    background: #1f2937;
}

[data-theme="dark"] .modal-card .btn,
[data-theme="dark"] .form-actions .btn {
    background: #374151;
    color: #f3f4f6;
    border-color: #4b5563;
}

[data-theme="dark"] .modal-card .btn:hover:not(:disabled),
[data-theme="dark"] .form-actions .btn:hover:not(:disabled) {
    background: #4b5563;
    border-color: #6b7280;
}

[data-theme="dark"] .modal-close:hover {
    background: #374151;
}

/* ===== Generic .field class (used by some pages alongside form-row) ===== */
.modal-card .field {
    display: flex;
    flex-direction: column;
    gap: 6px;
    min-width: 0;
}

.modal-card .field.full {
    grid-column: 1 / -1;
}

/* ===== Body lock when modal open (apply via JS) ===== */
body.modal-open {
    overflow: hidden;
}

/* ============================================================
   Loading states — Progress bar + Skeleton rows
   ============================================================ */

/* Top progress bar (indeterminate) */
.loading-bar {
    position: relative;
    height: 3px;
    background: rgba(99, 102, 241, 0.12);
    border-radius: 3px;
    overflow: hidden;
    margin-bottom: 10px;
}

.loading-bar::before {
    content: "";
    position: absolute;
    top: 0;
    inset-inline-start: 0;
    height: 100%;
    width: 35%;
    background: linear-gradient(90deg,
        transparent,
        var(--c-primary, #6366f1) 30%,
        #8b5cf6 70%,
        transparent);
    border-radius: 3px;
    animation: loading-slide 1.2s ease-in-out infinite;
}

@keyframes loading-slide {
    0%   { inset-inline-start: -35%; }
    100% { inset-inline-start: 100%; }
}

/* Page-wide top progress bar (fixed at top) */
.loading-bar-top {
    position: fixed;
    top: 0;
    inset-inline-start: 0;
    width: 100%;
    height: 3px;
    background: transparent;
    z-index: 9999;
    overflow: hidden;
}

.loading-bar-top::before {
    content: "";
    position: absolute;
    top: 0;
    inset-inline-start: 0;
    height: 100%;
    width: 35%;
    background: linear-gradient(90deg,
        transparent,
        var(--c-primary, #6366f1) 30%,
        #8b5cf6 70%,
        transparent);
    animation: loading-slide 1.2s ease-in-out infinite;
    box-shadow: 0 0 8px rgba(99, 102, 241, 0.6);
}

/* Skeleton row — shimmer effect for loading state */
.skeleton-row td {
    padding: 14px 12px !important;
}

.skeleton-box {
    display: inline-block;
    height: 12px;
    background: linear-gradient(90deg, #e5e7eb 0%, #f3f4f6 50%, #e5e7eb 100%);
    background-size: 200% 100%;
    border-radius: 4px;
    animation: skeleton-shimmer 1.4s ease-in-out infinite;
    vertical-align: middle;
}

.skeleton-circle {
    display: inline-block;
    width: 32px;
    height: 32px;
    border-radius: 50%;
    background: linear-gradient(90deg, #e5e7eb 0%, #f3f4f6 50%, #e5e7eb 100%);
    background-size: 200% 100%;
    animation: skeleton-shimmer 1.4s ease-in-out infinite;
    vertical-align: middle;
}

@keyframes skeleton-shimmer {
    0%   { background-position: 200% 0; }
    100% { background-position: -200% 0; }
}

[data-theme="dark"] .skeleton-box,
[data-theme="dark"] .skeleton-circle {
    background: linear-gradient(90deg, #374151 0%, #4b5563 50%, #374151 100%);
    background-size: 200% 100%;
}

/* Table opacity during loading (subtle visual cue) */
.table-loading {
    position: relative;
    pointer-events: none;
    user-select: none;
}

.table-loading::after {
    content: "";
    position: absolute;
    inset: 0;
    background: rgba(255, 255, 255, 0.3);
    z-index: 1;
    pointer-events: none;
}

[data-theme="dark"] .table-loading::after {
    background: rgba(0, 0, 0, 0.2);
}

/* Spinner for inline use */
.spinner-inline {
    display: inline-block;
    width: 14px;
    height: 14px;
    border: 2px solid rgba(99, 102, 241, 0.2);
    border-top-color: var(--c-primary, #6366f1);
    border-radius: 50%;
    animation: spin 0.7s linear infinite;
    vertical-align: middle;
}

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

/* ============================================================
   General nice inputs — لجميع الـ inputs في الجداول والصفحات العادية
   (يستثني الـ inputs داخل modal/form-row لأنها مُنسَّقة بشكل مختلف)
   ============================================================ */
.prod-table td input[type="text"],
.prod-table td input[type="number"],
.prod-table td input[type="email"],
.prod-table td input[type="tel"],
.prod-table td input[type="date"],
.prod-table td input[type="search"],
.prod-table td input:not([type]):not([type="checkbox"]):not([type="radio"]):not([type="file"]):not([type="color"]),
.prod-table td select,
.prod-table td textarea {
    height: 36px;
    padding: 0 10px;
    border: 1.5px solid #d1d5db;
    border-radius: 8px;
    background: #f9fafb;
    color: #1f2937;
    font-size: 14px;
    font-weight: 600;
    font-family: inherit;
    transition: all 0.15s;
    box-sizing: border-box;
    outline: none;
}

/* إخفاء الـ spinner المزعج في input[type=number] داخل الجداول
   (يأكل مساحة من الحقل الضيّق) */
.prod-table td input[type="number"]::-webkit-outer-spin-button,
.prod-table td input[type="number"]::-webkit-inner-spin-button {
    -webkit-appearance: none;
    margin: 0;
}
.prod-table td input[type="number"] {
    -moz-appearance: textfield;
    appearance: textfield;
}

.prod-table td input:hover:not(:disabled):not(:focus):not([type="checkbox"]):not([type="radio"]),
.prod-table td select:hover:not(:disabled):not(:focus),
.prod-table td textarea:hover:not(:disabled):not(:focus) {
    border-color: #9ca3af;
    background: #ffffff;
}

.prod-table td input:focus:not([type="checkbox"]):not([type="radio"]),
.prod-table td select:focus,
.prod-table td textarea:focus {
    border-color: var(--c-primary, #6366f1);
    background: #ffffff;
    box-shadow: 0 0 0 3px rgba(99, 102, 241, 0.15);
}

.prod-table td textarea {
    height: auto;
    min-height: 60px;
    padding: 8px 12px;
    resize: vertical;
}

/* خلايا الفلاتر العلوية (.card div > input/select) */
.card > input[type="text"],
.card > input[type="number"],
.card > input[type="email"],
.card > input[type="tel"],
.card > input[type="date"],
.card > input[type="search"],
.card > input:not([type]):not([type="checkbox"]):not([type="radio"]),
.card > select,
.card > div > input[type="text"]:not(.iec-input),
.card > div > input[type="number"]:not(.iec-input),
.card > div > input[type="date"]:not(.iec-input),
.card > div > input[type="search"]:not(.iec-input),
.card > div > input:not([type]):not(.iec-input):not([type="checkbox"]):not([type="radio"]),
.card > div > select {
    height: 38px;
    padding: 0 12px;
    border: 1.5px solid #d1d5db;
    border-radius: 8px;
    background: #f9fafb;
    color: var(--c-text, #1f2937);
    font-size: 13px;
    font-family: inherit;
    transition: all 0.15s;
    box-sizing: border-box;
    outline: none;
}

.card > input:hover:not(:disabled):not(:focus),
.card > select:hover:not(:disabled):not(:focus),
.card > div > input:hover:not(:disabled):not(:focus):not(.iec-input),
.card > div > select:hover:not(:disabled):not(:focus) {
    border-color: #9ca3af;
    background: #ffffff;
}

.card > input:focus,
.card > select:focus,
.card > div > input:focus:not(.iec-input),
.card > div > select:focus {
    border-color: var(--c-primary, #6366f1);
    background: #ffffff;
    box-shadow: 0 0 0 3px rgba(99, 102, 241, 0.15);
}

/* Dark mode for table + card inputs */
[data-theme="dark"] .prod-table td input,
[data-theme="dark"] .prod-table td select,
[data-theme="dark"] .prod-table td textarea,
[data-theme="dark"] .card > input,
[data-theme="dark"] .card > select,
[data-theme="dark"] .card > div > input:not(.iec-input),
[data-theme="dark"] .card > div > select {
    background: #111827;
    color: #f3f4f6;
    border-color: #374151;
}
