/* ---------- Theme ---------- */
:root {
    --app-font: 'Inter', 'IBM Plex Sans Arabic', system-ui, -apple-system, 'Segoe UI', Roboto, sans-serif;
    --app-navy: #1e2a4a;
    --app-indigo: #4f46e5;
    --app-bg: #f4f6fb;
    --app-radius: .75rem;
    --app-shadow: 0 1px 2px rgba(16, 24, 40, .05), 0 1px 3px rgba(16, 24, 40, .08);

    --bs-body-font-family: var(--app-font);
    --bs-primary: var(--app-indigo);
    --bs-primary-rgb: 79, 70, 229;
    --bs-link-color: var(--app-indigo);
    --bs-link-color-rgb: 79, 70, 229;
    --bs-link-hover-color: #3730a3;
    --bs-border-radius: .5rem;
}

/* Arabic first when the page is Arabic, so Arabic glyph metrics drive line height */
[dir="rtl"] {
    --app-font: 'IBM Plex Sans Arabic', 'Inter', system-ui, Tahoma, sans-serif;
}

body {
    background: var(--app-bg);
    min-height: 100vh;
    font-family: var(--app-font);
    -webkit-font-smoothing: antialiased;
}

.btn-primary {
    --bs-btn-bg: var(--app-indigo);
    --bs-btn-border-color: var(--app-indigo);
    --bs-btn-hover-bg: #4338ca;
    --bs-btn-hover-border-color: #4338ca;
    --bs-btn-active-bg: #3730a3;
    --bs-btn-active-border-color: #3730a3;
    --bs-btn-disabled-bg: var(--app-indigo);
    --bs-btn-disabled-border-color: var(--app-indigo);
}

.btn-outline-primary {
    --bs-btn-color: var(--app-indigo);
    --bs-btn-border-color: var(--app-indigo);
    --bs-btn-hover-bg: var(--app-indigo);
    --bs-btn-hover-border-color: var(--app-indigo);
    --bs-btn-active-bg: var(--app-indigo);
}

.btn { font-weight: 500; }

.form-control:focus,
.form-select:focus,
.form-check-input:focus {
    border-color: #a5b4fc;
    box-shadow: 0 0 0 .2rem rgba(79, 70, 229, .15);
}

.form-check-input:checked {
    background-color: var(--app-indigo);
    border-color: var(--app-indigo);
}

.text-bg-primary { background-color: var(--app-indigo) !important; }

h1, .h1, h2, .h2, h3, .h3, .page-title {
    font-weight: 700;
    letter-spacing: -.01em;
}

.page-title { font-size: 1.6rem; }

/* ---------- Navbar ---------- */
.app-navbar {
    background: linear-gradient(90deg, var(--app-navy), #2b3a67);
    box-shadow: 0 2px 8px rgba(16, 24, 40, .15);
}

.app-navbar .navbar-brand {
    font-weight: 700;
    letter-spacing: -.01em;
}

.app-navbar .nav-link {
    border-radius: .5rem;
    padding-inline: .75rem !important;
}

.app-navbar .nav-link.active,
.app-navbar .nav-link:hover {
    background: rgba(255, 255, 255, .1);
}

.app-logo { color: #a5b4fc; flex: none; }

.avatar {
    display: inline-grid;
    place-items: center;
    width: 1.75rem;
    height: 1.75rem;
    border-radius: 50%;
    background: #a5b4fc;
    color: var(--app-navy);
    font-weight: 700;
    font-size: .8rem;
}

.role-badge {
    background: rgba(255, 255, 255, .15);
    font-weight: 500;
}

/* ---------- Cards & alerts ---------- */
.card {
    border: 0;
    border-radius: var(--app-radius);
    box-shadow: var(--app-shadow);
}

.card-header {
    background: #fff;
    border-bottom: 1px solid #eef0f5;
    border-radius: var(--app-radius) var(--app-radius) 0 0 !important;
    padding-block: .85rem;
}

.card-footer {
    background: #fafbfd;
    border-top: 1px solid #eef0f5;
}

.app-alert {
    border: 0;
    border-inline-start: 4px solid currentColor;
    border-radius: var(--app-radius);
    box-shadow: var(--app-shadow);
    padding-block: .6rem;
}

.table { --bs-table-bg: transparent; }

.table > thead th {
    font-size: .75rem;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: .04em;
    color: #667085;
    background: #f8f9fc;
    border-bottom-width: 1px;
    white-space: nowrap;
}

[dir="rtl"] .table > thead th {
    text-transform: none;
    letter-spacing: 0;
    font-size: .85rem;
}

/* ---------- Stats ---------- */
.stat-card {
    display: flex;
    flex-direction: column;
    gap: .15rem;
    padding: 1rem 1.25rem;
    border-radius: var(--app-radius);
    background: #fff;
    box-shadow: var(--app-shadow);
    text-decoration: none;
    color: inherit;
    border-inline-start: 4px solid var(--tone);
    transition: transform .12s ease, box-shadow .12s ease;
}

.stat-card:hover { transform: translateY(-1px); box-shadow: 0 4px 12px rgba(16, 24, 40, .1); }
.stat-card.is-active { outline: 2px solid var(--tone); outline-offset: -2px; }
.stat-value { font-size: 1.75rem; font-weight: 700; line-height: 1.1; color: var(--tone); }
.stat-label { color: #667085; font-size: .9rem; }
.stat-primary { --tone: var(--app-indigo); }
.stat-info { --tone: #0891b2; }
.stat-warning { --tone: #d97706; }

/* ---------- Device table (DataTables) ---------- */
.code-pill {
    font-family: ui-monospace, 'SF Mono', Menlo, Consolas, monospace;
    font-weight: 600;
    background: #eef2ff;
    color: #3730a3;
    padding: .1rem .45rem;
    border-radius: .35rem;
}

.table-card .dt-container .dt-search input {
    min-width: 16rem;
    margin-inline-start: 0;
}

.table-card .dt-buttons .btn {
    background: #fff;
    border: 1px solid var(--bs-border-color);
    color: var(--bs-body-color);
}

.table-card .dt-buttons .btn:hover { background: #f2f4f7; }

.table-card .dt-length select { width: auto; display: inline-block; }

.selection-bar {
    padding: .5rem .75rem;
    background: #f8f9fc;
    border-radius: .5rem;
}

@media (max-width: 575.98px) {
    .table-card .card-body { padding: .75rem; }
    .table-card .dt-container .dt-search input { min-width: 0; width: 100%; }
    .table-card .dt-layout-row { gap: .5rem; }
}

/* ---------- Scan page ---------- */
.scan-hero {
    background: linear-gradient(135deg, var(--app-navy), var(--app-indigo));
    color: #fff;
    border-radius: 1rem;
    padding: 2rem 1.5rem;
    box-shadow: 0 8px 24px rgba(79, 70, 229, .25);
}

.scan-hero .text-body-secondary { color: rgba(255, 255, 255, .75) !important; }

.scan-reader {
    width: 100%;
    max-width: 520px;
    margin: 0 auto;
    border-radius: .75rem;
    overflow: hidden;
    background: #000;
}

.scan-button {
    font-size: 1.2rem;
    padding: 1rem 2rem;
    background: #fff;
    color: var(--app-navy);
    border: 0;
    font-weight: 600;
}

.scan-button:hover { background: #eef2ff; color: var(--app-navy); }

/* ---------- Codes & photos ---------- */
.code-qr { width: 160px; height: 160px; }

.code-barcode svg {
    width: 100%;
    max-width: 280px;
    height: 60px;
}

.photo-thumb {
    aspect-ratio: 4 / 3;
    object-fit: cover;
    width: 100%;
    transition: transform .15s ease;
}

.photo-thumb:hover { transform: scale(1.02); }

/* ---------- Login ---------- */
.login-page {
    background:
        radial-gradient(circle at 20% 10%, rgba(165, 180, 252, .35), transparent 40%),
        radial-gradient(circle at 80% 90%, rgba(79, 70, 229, .25), transparent 45%),
        var(--app-bg);
}

.login-logo .app-logo {
    width: 56px;
    height: 56px;
    color: var(--app-indigo);
}

/* ---------- Other tables: stacked cards on phones ---------- */
@media (max-width: 767.98px) {
    .table-stack thead { display: none; }

    .table-stack,
    .table-stack tbody,
    .table-stack tr,
    .table-stack td {
        display: block;
        width: 100%;
    }

    .table-stack tr {
        background: #fff;
        border-radius: var(--app-radius);
        box-shadow: var(--app-shadow);
        margin-bottom: .75rem;
        padding: .5rem .75rem;
    }

    .table-stack td { border: 0; padding: .2rem 0; }

    .table-stack td[data-label]::before {
        content: attr(data-label) ": ";
        color: var(--bs-secondary-color);
        font-size: .85em;
    }

    .table-stack td.cell-title { font-weight: 600; font-size: 1.05rem; }
}

.audit-changes { font-size: .85rem; }
.audit-changes td { padding: .1rem .5rem; }

.text-old {
    color: var(--bs-danger);
    text-decoration: line-through;
}
