* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

:root {
    --primary: #2563eb;
    --primary-dark: #1d4ed8;
    --success: #16a34a;
    --danger: #dc2626;
    --warning: #d97706;
    --bg: #f1f5f9;
    --card: #ffffff;
    --card-hover: #f8fafc;
    --text: #1e293b;
    --text-muted: #64748b;
    --border: #e2e8f0;
    --input-bg: #ffffff;
    --shadow: 0 1px 3px rgb(0 0 0 / 0.08), 0 4px 12px rgb(0 0 0 / 0.05);
}

body {
    font-family: 'Segoe UI', Tahoma, Arial, sans-serif;
    background: var(--bg);
    background-image: radial-gradient(ellipse at top, #dbeafe 0%, var(--bg) 55%);
    color: var(--text);
    direction: rtl;
    line-height: 1.6;
    min-height: 100vh;
    -webkit-text-size-adjust: 100%;
    overflow-x: hidden;
}

body.modal-open {
    overflow: hidden;
}

/* ===== Login Screen ===== */
.login-screen {
    min-height: 100vh;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 24px 16px;
    background: var(--bg);
    background-image: radial-gradient(ellipse at top, #dbeafe 0%, var(--bg) 55%);
}

.login-card {
    width: 100%;
    max-width: 400px;
    background: var(--card);
    border: 1px solid var(--border);
    border-radius: 16px;
    padding: 32px 28px;
    box-shadow: var(--shadow);
    text-align: center;
}

.login-card h2 {
    color: var(--primary);
    margin-bottom: 8px;
    font-size: 1.5rem;
}

.login-subtitle {
    color: var(--text-muted);
    margin-bottom: 24px;
    font-size: 0.95rem;
}

.login-card .form-group {
    text-align: right;
}

.login-error {
    margin-top: 14px;
    padding: 10px 12px;
    background: #fee2e2;
    color: #991b1b;
    border-radius: 8px;
    font-size: 0.9rem;
}

body.role-viewer .admin-only {
    display: none !important;
}

#logoutBtn {
    flex-shrink: 0;
}

.container {
    max-width: 1200px;
    margin: 0 auto;
    padding: 24px 16px;
    width: 100%;
}

header {
    margin-bottom: 32px;
}

.header-top {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 16px;
    flex-wrap: wrap;
}

.header-text {
    text-align: right;
    flex: 1;
}

header h1 {
    font-size: 2rem;
    color: var(--primary);
    margin-bottom: 8px;
}

header p {
    color: var(--text-muted);
}

.layout {
    display: grid;
    grid-template-columns: 320px 1fr;
    gap: 24px;
    align-items: start;
}

@media (max-width: 900px) {
    .layout {
        grid-template-columns: 1fr;
    }

    .header-top {
        flex-direction: column;
        align-items: stretch;
    }

    .header-text {
        text-align: center;
    }

    #openNationalitiesBtn {
        width: 100%;
    }
}

.sidebar-nationalities-btn {
    margin-top: 10px;
}

.card {
    background: var(--card);
    border-radius: 12px;
    box-shadow: var(--shadow);
    padding: 24px;
    border: 1px solid var(--border);
    transition: border-color 0.2s;
}

.card h2 {
    font-size: 1.25rem;
    margin-bottom: 16px;
    padding-bottom: 12px;
    border-bottom: 2px solid var(--border);
}

.form-group {
    margin-bottom: 16px;
}

.form-group label {
    display: block;
    margin-bottom: 6px;
    font-weight: 600;
    font-size: 0.9rem;
}

.optional-label {
    font-weight: 400;
    color: var(--text-muted);
    font-size: 0.82rem;
}

.form-group input,
.form-group select {
    width: 100%;
    padding: 10px 12px;
    border: 1px solid var(--border);
    border-radius: 8px;
    font-size: 1rem;
    font-family: inherit;
    background: var(--input-bg);
    color: var(--text);
    transition: border-color 0.2s;
    color-scheme: light;
}

.form-group input:focus,
.form-group select:focus {
    outline: none;
    border-color: var(--primary);
    box-shadow: 0 0 0 3px rgb(59 130 246 / 0.25);
}

.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 6px;
    padding: 10px 18px;
    border: none;
    border-radius: 8px;
    font-size: 0.95rem;
    font-weight: 600;
    font-family: inherit;
    cursor: pointer;
    transition: background 0.2s, transform 0.1s;
}

.btn:active {
    transform: scale(0.98);
}

.btn-primary {
    background: var(--primary);
    color: white;
}

.btn-primary:hover {
    background: var(--primary-dark);
}

.btn-success {
    background: var(--success);
    color: white;
}

.btn-success:hover {
    background: #15803d;
}

.btn-danger {
    background: var(--danger);
    color: white;
}

.btn-danger:hover {
    background: #b91c1c;
}

.btn-warning {
    background: var(--warning);
    color: white;
}

.btn-warning:hover {
    background: #b45309;
}

.btn-sm {
    padding: 6px 12px;
    font-size: 0.85rem;
}

.btn-block {
    width: 100%;
}

.establishment-list {
    list-style: none;
    margin-top: 16px;
}

.establishment-item {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 12px 14px;
    border: 1px solid var(--border);
    border-radius: 8px;
    margin-bottom: 8px;
    cursor: pointer;
    transition: all 0.2s;
}

.establishment-item:hover {
    border-color: var(--primary);
    background: #eff6ff;
}

.establishment-item.active {
    border-color: var(--primary);
    background: #eff6ff;
    font-weight: 600;
}

.est-info {
    display: flex;
    flex-direction: column;
    gap: 6px;
    flex: 1;
    min-width: 0;
}

.est-name {
    word-break: break-word;
}

.est-count-badge {
    display: inline-block;
    width: fit-content;
    padding: 2px 10px;
    border-radius: 12px;
    font-size: 0.78rem;
    font-weight: 600;
    background: #dbeafe;
    color: #1d4ed8;
    border: 1px solid #bfdbfe;
}

.establishment-item.active .est-count-badge {
    background: #bfdbfe;
    color: #1e40af;
}

.establishment-item .actions {
    display: flex;
    gap: 6px;
}

.header-actions {
    display: flex;
    gap: 10px;
    flex-wrap: wrap;
}

#openNationalitiesBtn {
    white-space: nowrap;
}

.btn-nationalities {
    gap: 8px;
    padding-inline: 20px;
}

.btn-icon {
    font-size: 1.1rem;
    line-height: 1;
}

/* ===== نافذة الجنسيات ===== */
.modal-nationalities {
    max-width: 440px;
    padding: 0;
    overflow: hidden;
}

.nat-modal-header {
    display: flex;
    align-items: flex-start;
    gap: 14px;
    padding: 24px 24px 16px;
    background: linear-gradient(180deg, #eff6ff 0%, var(--card) 100%);
    border-bottom: 1px solid var(--border);
}

.nat-modal-icon {
    font-size: 2rem;
    line-height: 1;
    flex-shrink: 0;
}

.nat-modal-header h3 {
    margin-bottom: 4px;
    font-size: 1.25rem;
}

.nat-modal-subtitle {
    color: var(--text-muted);
    font-size: 0.85rem;
    line-height: 1.5;
}

.nat-add-box {
    padding: 20px 24px;
    background: #f8fafc;
    border-bottom: 1px solid var(--border);
}

.nat-add-form .form-group {
    margin-bottom: 0;
}

.nat-input-group label {
    margin-bottom: 8px;
}

.nat-input-row {
    display: flex;
    gap: 10px;
    align-items: stretch;
}

.nat-input-row input {
    flex: 1;
    padding: 12px 14px;
    border: 1px solid var(--border);
    border-radius: 10px;
    font-size: 1rem;
    font-family: inherit;
    background: var(--input-bg);
    color: var(--text);
    transition: border-color 0.2s, box-shadow 0.2s;
}

.nat-input-row input:focus {
    outline: none;
    border-color: var(--primary);
    box-shadow: 0 0 0 3px rgb(59 130 246 / 0.25);
}

.nat-input-row .btn {
    flex-shrink: 0;
    padding-inline: 20px;
    border-radius: 10px;
}

.nat-list-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 16px 24px 10px;
    font-weight: 600;
    font-size: 0.9rem;
    color: var(--text-muted);
}

.nat-count-badge {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    min-width: 28px;
    height: 28px;
    padding: 0 8px;
    background: var(--primary);
    color: #fff;
    border-radius: 14px;
    font-size: 0.85rem;
    font-weight: 700;
}

.nationality-list {
    list-style: none;
    margin: 0;
    padding: 0 24px;
    max-height: 280px;
    overflow-y: auto;
}

.nationality-list .nat-item {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    padding: 12px 14px;
    margin-bottom: 8px;
    background: var(--input-bg);
    border: 1px solid var(--border);
    border-radius: 10px;
    transition: border-color 0.2s, background 0.2s;
}

.nationality-list .nat-item:hover {
    border-color: var(--primary);
    background: #f8fafc;
}

.nat-item-name {
    display: flex;
    align-items: center;
    gap: 10px;
    flex: 1;
    font-weight: 500;
}

.nat-item-dot {
    width: 8px;
    height: 8px;
    border-radius: 50%;
    background: var(--success);
    flex-shrink: 0;
}

.nationality-list .empty-list-msg {
    padding: 32px 16px;
    margin-bottom: 8px;
    background: var(--input-bg);
    border: 1px dashed var(--border);
    border-radius: 10px;
}

.modal-nationalities .modal-actions {
    padding: 16px 24px 24px;
    margin-top: 8px;
}

.sidebar-divider {
    height: 1px;
    background: var(--border);
    margin: 24px 0;
}

.manage-list {
    list-style: none;
    margin-top: 20px;
    max-height: 320px;
    overflow-y: auto;
}

.manage-item {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 10px 12px;
    border: 1px solid var(--border);
    border-radius: 8px;
    margin-bottom: 6px;
    font-size: 0.9rem;
}

.manage-item span {
    flex: 1;
}

.nationality-list {
    list-style: none;
    margin-top: 20px;
    max-height: 320px;
    overflow-y: auto;
}

.nationality-item {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 10px 12px;
    border: 1px solid var(--border);
    border-radius: 8px;
    margin-bottom: 6px;
    font-size: 0.9rem;
}

.nationality-item span {
    flex: 1;
}

.empty-list-msg {
    text-align: center;
    padding: 16px;
    color: var(--text-muted);
    font-size: 0.9rem;
}

.empty-state {
    text-align: center;
    padding: 48px 24px;
    color: var(--text-muted);
}

.empty-state svg {
    width: 64px;
    height: 64px;
    margin-bottom: 16px;
    opacity: 0.4;
}

.workers-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 20px;
    flex-wrap: wrap;
    gap: 12px;
}

.workers-header h2 span {
    color: var(--primary);
}

.workers-table-wrapper {
    overflow-x: hidden;
    width: 100%;
}

table {
    width: 100%;
    border-collapse: collapse;
    font-size: 0.95rem;
    table-layout: fixed;
}

thead {
    background: #f8fafc;
}

th, td {
    padding: 12px 14px;
    text-align: right;
    border-bottom: 1px solid var(--border);
}

th {
    font-weight: 600;
    color: var(--text-muted);
    white-space: nowrap;
}

tbody tr:hover {
    background: #f8fafc;
}

th.sort-active {
    color: var(--primary);
}

.col-rank {
    width: 48px;
    text-align: center !important;
}

.col-days {
    min-width: 110px;
}

.rank-badge {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 28px;
    height: 28px;
    border-radius: 50%;
    font-size: 0.8rem;
    font-weight: 700;
    background: #e2e8f0;
    color: #64748b;
}

.rank-badge.rank-1 {
    background: linear-gradient(135deg, #f59e0b, #d97706);
    color: #fff;
    box-shadow: 0 2px 8px rgb(245 158 11 / 0.4);
}

.rank-badge.rank-2 {
    background: #64748b;
    color: #fff;
}

.rank-badge.rank-3 {
    background: #92400e;
    color: #fde68a;
}

.days-badge {
    display: inline-block;
    background: #dbeafe;
    color: #1d4ed8;
    padding: 4px 12px;
    border-radius: 20px;
    font-weight: 700;
    font-size: 0.9rem;
}

.days-badge.days-bg-yellow {
    background: #fef08a;
    color: #854d0e;
}

.days-badge.days-bg-red {
    background: #fecaca;
    color: #991b1b;
}

.remaining-badge {
    display: inline-block;
    padding: 4px 12px;
    border-radius: 20px;
    font-weight: 700;
    font-size: 0.85rem;
    white-space: nowrap;
}

.remaining-badge.remaining-ok {
    background: #dcfce7;
    color: #166534;
}

.remaining-badge.remaining-warning {
    background: #fef9c3;
    color: #854d0e;
}

.remaining-badge.remaining-urgent {
    background: #ffedd5;
    color: #9a3412;
}

.remaining-badge.remaining-expired {
    background: #fee2e2;
    color: #991b1b;
}

.remaining-badge.remaining-unknown {
    background: #f1f5f9;
    color: #64748b;
}

.contract-badge {
    display: inline-block;
    padding: 4px 12px;
    border-radius: 20px;
    font-weight: 600;
    font-size: 0.85rem;
    white-space: nowrap;
}

.contract-badge.authenticated {
    background: #dcfce7;
    color: #166534;
}

.contract-badge.not-authenticated {
    background: #fee2e2;
    color: #991b1b;
}

.actions-cell {
    display: flex;
    gap: 6px;
    white-space: nowrap;
}

.modal-overlay {
    display: none;
    position: fixed;
    inset: 0;
    background: rgb(15 23 42 / 0.45);
    z-index: 1000;
    align-items: center;
    justify-content: center;
    padding: 16px;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
}

.modal-overlay.active {
    display: flex;
}

#nationalityModal { z-index: 1100; }
#workerModal { z-index: 1100; }
#establishmentModal { z-index: 1100; }
#deleteModal { z-index: 1200; }

.modal {
    background: var(--card);
    border-radius: 12px;
    padding: 28px;
    width: 100%;
    max-width: 480px;
    max-height: 90vh;
    overflow-y: auto;
    box-shadow: 0 20px 25px -5px rgb(0 0 0 / 0.12);
    border: 1px solid var(--border);
}

.modal h3 {
    margin-bottom: 20px;
    font-size: 1.2rem;
}

.modal-actions {
    display: flex;
    gap: 10px;
    margin-top: 20px;
}

.modal-actions .btn {
    flex: 1;
}

.toast {
    position: fixed;
    bottom: 24px;
    left: 50%;
    transform: translateX(-50%) translateY(100px);
    background: #1e293b;
    color: white;
    padding: 12px 24px;
    border-radius: 8px;
    font-weight: 500;
    z-index: 2000;
    opacity: 0;
    transition: all 0.3s ease;
}

.toast.show {
    transform: translateX(-50%) translateY(0);
    opacity: 1;
}

.toast.success {
    background: var(--success);
}

.toast.error {
    background: var(--danger);
}

.hidden {
    display: none !important;
}

.form-row {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 12px;
}

@media (max-width: 500px) {
    .form-row {
        grid-template-columns: 1fr;
    }
}

.modal-danger h3 {
    color: var(--danger);
}

.delete-warning {
    background: #fee2e2;
    border: 1px solid #fecaca;
    color: #991b1b;
    padding: 12px 14px;
    border-radius: 8px;
    margin-bottom: 16px;
    font-size: 0.95rem;
}

.delete-hint {
    margin-bottom: 12px;
    color: var(--text-muted);
    font-size: 0.9rem;
}

.delete-hint strong {
    color: #991b1b;
    font-size: 1.1rem;
    padding: 2px 8px;
    background: #fee2e2;
    border-radius: 4px;
}

#confirmDeleteBtn:disabled {
    opacity: 0.45;
    cursor: not-allowed;
}

#deleteConfirmInput:focus {
    border-color: var(--danger);
    box-shadow: 0 0 0 3px rgb(239 68 68 / 0.25);
}

.form-group input::placeholder {
    color: #94a3b8;
}

/* ===== بطاقات العمال — جوال وشاشات صغيرة ===== */
@media (max-width: 1024px) {
    .workers-table-wrapper {
        overflow-x: hidden;
    }

    .workers-table-wrapper table,
    .workers-table-wrapper thead,
    .workers-table-wrapper tbody,
    .workers-table-wrapper tr,
    .workers-table-wrapper td {
        display: block;
        width: 100%;
        max-width: 100%;
    }

    .workers-table-wrapper thead {
        display: none;
    }

    .workers-table-wrapper tbody tr.worker-row {
        background: var(--input-bg);
        border: 1px solid var(--border);
        border-radius: 12px;
        margin-bottom: 14px;
        padding: 16px;
        box-shadow: var(--shadow);
    }

    .workers-table-wrapper tbody tr.worker-row:hover {
        background: var(--input-bg);
    }

    .workers-table-wrapper tbody td {
        display: flex;
        align-items: flex-start;
        justify-content: space-between;
        gap: 10px;
        padding: 10px 0;
        border-bottom: 1px solid var(--border);
        text-align: end;
        word-break: break-word;
        overflow-wrap: anywhere;
    }

    .workers-table-wrapper tbody td.worker-name-cell {
        font-size: 1.15rem;
        font-weight: 700;
        color: var(--primary);
        border-bottom: 2px solid var(--border);
        padding-bottom: 12px;
        margin-bottom: 4px;
    }

    .workers-table-wrapper tbody td.worker-name-cell::before {
        display: none;
    }

    .workers-table-wrapper tbody td.col-rank-cell::before {
        display: none;
    }

    .workers-table-wrapper tbody td.col-rank-cell {
        justify-content: flex-start;
        padding-bottom: 8px;
    }

    .nat-input-row {
        flex-direction: column;
    }

    .nat-input-row .btn {
        width: 100%;
    }

    .workers-table-wrapper tbody td:first-child::before {
        display: none;
    }

    .workers-table-wrapper tbody td:last-child {
        border-bottom: none;
        padding-top: 14px;
    }

    .workers-table-wrapper tbody td::before {
        content: attr(data-label);
        font-weight: 600;
        color: var(--text-muted);
        font-size: 0.85rem;
        flex-shrink: 0;
        text-align: start;
        min-width: 90px;
    }

    .workers-table-wrapper tbody td.worker-actions {
        flex-direction: column;
        align-items: stretch;
    }

    .workers-table-wrapper tbody td.worker-actions::before {
        margin-bottom: 6px;
    }

    .workers-table-wrapper .actions-cell {
        width: 100%;
        flex-wrap: wrap;
    }

    .workers-table-wrapper .actions-cell .btn {
        flex: 1;
        min-width: 120px;
    }
}

/* ===== Mobile Responsive ===== */
@media (max-width: 768px) {
    .container {
        padding: 16px 12px;
        padding-bottom: max(16px, env(safe-area-inset-bottom));
    }

    header {
        margin-bottom: 20px;
    }

    .header-top {
        flex-direction: column;
        align-items: stretch;
    }

    .header-text {
        text-align: center;
    }

    #openNationalitiesBtn {
        width: 100%;
    }

    header h1 {
        font-size: 1.5rem;
    }

    header p {
        font-size: 0.9rem;
    }

    .layout {
        gap: 16px;
    }

    .card {
        padding: 16px;
        border-radius: 10px;
    }

    .card h2 {
        font-size: 1.1rem;
    }

    .btn {
        min-height: 44px;
        padding: 12px 16px;
    }

    .btn-sm {
        min-height: 40px;
        padding: 8px 14px;
        font-size: 0.9rem;
    }

    .establishment-item {
        flex-direction: column;
        align-items: stretch;
        gap: 10px;
        padding: 14px;
    }

    .establishment-item > span {
        font-size: 1rem;
        word-break: break-word;
    }

    .est-info {
        width: 100%;
    }

    .establishment-item .actions,
    .nationality-item {
        gap: 8px;
    }

    .nationality-item .btn-sm {
        flex-shrink: 0;
    }

    .workers-header {
        flex-direction: column;
        align-items: stretch;
    }

    .workers-header h2 {
        font-size: 1.1rem;
        line-height: 1.5;
    }

    .workers-header .btn-success {
        width: 100%;
    }

    .empty-state {
        padding: 32px 16px;
    }

    .empty-state svg {
        width: 48px;
        height: 48px;
    }

    .manage-item {
        flex-wrap: wrap;
        gap: 8px;
    }

    .manage-item .btn-sm {
        margin-right: auto;
    }

    .modal-overlay {
        align-items: flex-end;
        padding: 0;
    }

    .modal {
        max-width: 100%;
        border-radius: 16px 16px 0 0;
        max-height: 92vh;
        padding: 20px 16px;
        padding-bottom: max(20px, env(safe-area-inset-bottom));
    }

    .modal h3 {
        font-size: 1.1rem;
    }

    .form-row {
        grid-template-columns: 1fr;
    }

    .modal-actions {
        flex-direction: column-reverse;
    }

    .toast {
        left: 12px;
        right: 12px;
        transform: translateY(100px);
        text-align: center;
        width: auto;
    }

    .toast.show {
        transform: translateY(0);
    }
}

@media (max-width: 480px) {
    header h1 {
        font-size: 1.35rem;
    }

    .manage-list,
    .nationality-list {
        max-height: 50vh;
    }
}

@media (min-width: 1025px) {
    .workers-table-wrapper {
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
    }

    .workers-table-wrapper table {
        table-layout: auto;
        min-width: 1100px;
        width: 100%;
    }

    .workers-table-wrapper th,
    .workers-table-wrapper td {
        white-space: nowrap;
    }

    .workers-table-wrapper td.worker-name-cell {
        white-space: normal;
        min-width: 140px;
    }
}
