:root {
    --sidebar-w: 264px;
    --brand: #16263f;
    --brand-2: #1f3a5f;
    --brand-3: #2c5282;
    --accent: #3182ce;
    --accent-2: #4299e1;
    --ok: #38a169;
    --danger: #e53e3e;
    --warn: #dd6b20;
    --ink: #1a202c;
    --muted: #718096;
    --bg: #eef2f7;
    --card-bg: #ffffff;
    --radius: 16px;
    --radius-sm: 12px;
    --shadow-sm: 0 2px 6px rgba(20,35,63,.06);
    --shadow: 0 8px 24px rgba(20,35,63,.08);
    --shadow-lg: 0 18px 45px rgba(20,35,63,.16);
    --t-fast: .18s cubic-bezier(.4,0,.2,1);
    --t: .28s cubic-bezier(.4,0,.2,1);
}

* { font-family: "Segoe UI", Tahoma, "Cairo", sans-serif; }

html { scroll-behavior: smooth; }

body {
    margin: 0;
    background: var(--bg);
    color: var(--ink);
}

::-webkit-scrollbar { width: 9px; height: 9px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: rgba(20,35,63,.22); border-radius: 6px; }
::-webkit-scrollbar-thumb:hover { background: rgba(20,35,63,.38); }

/* ===== Keyframes ===== */
@keyframes fadeInUp {
    from { opacity: 0; transform: translateY(14px); }
    to   { opacity: 1; transform: translateY(0); }
}
@keyframes fadeIn { from { opacity: 0; } to { opacity: 1; } }
@keyframes slideInRight {
    from { opacity: 0; transform: translateX(20px); }
    to   { opacity: 1; transform: translateX(0); }
}
@keyframes popIn {
    0%   { opacity: 0; transform: scale(.94); }
    100% { opacity: 1; transform: scale(1); }
}
@keyframes shimmer {
    0% { background-position: -400px 0; }
    100% { background-position: 400px 0; }
}

/* ===== Shell ===== */
.app-shell { display: flex; min-height: 100vh; }

.app-sidebar {
    width: var(--sidebar-w);
    background: linear-gradient(180deg, var(--brand) 0%, var(--brand-2) 60%, var(--brand-3) 100%);
    color: #e2e8f0;
    flex-shrink: 0;
    position: sticky;
    top: 0;
    align-self: flex-start;
    height: 100vh;
    overflow-y: auto;
    box-shadow: 2px 0 18px rgba(20,35,63,.18);
    z-index: 20;
}

.app-sidebar .brand {
    font-size: 1.28rem;
    font-weight: 700;
    padding: 20px 22px;
    display: flex;
    align-items: center;
    gap: 11px;
    border-bottom: 1px solid rgba(255,255,255,.12);
    color: #fff;
    letter-spacing: .2px;
}
.app-sidebar .brand i {
    font-size: 1.5rem;
    background: rgba(255,255,255,.14);
    width: 40px; height: 40px;
    display: flex; align-items: center; justify-content: center;
    border-radius: 11px;
    box-shadow: inset 0 0 0 1px rgba(255,255,255,.12);
}

.app-sidebar .nav { padding: 10px 0; }

.app-sidebar .nav-link {
    color: #c2cfe0;
    padding: 11px 22px;
    margin: 2px 10px;
    border-radius: 11px;
    display: flex;
    align-items: center;
    gap: 11px;
    position: relative;
    transition: background var(--t-fast), color var(--t-fast), transform var(--t-fast);
    font-size: .95rem;
}
.app-sidebar .nav-link:hover {
    background: rgba(255,255,255,.09);
    color: #fff;
    transform: translateX(-4px);
}
.app-sidebar .nav-link.active {
    background: linear-gradient(90deg, rgba(99,179,237,.28), rgba(99,179,237,.08));
    color: #fff;
    font-weight: 600;
    box-shadow: inset 0 0 0 1px rgba(99,179,237,.35);
}
.app-sidebar .nav-link.active::before {
    content: "";
    position: absolute;
    right: -10px; top: 50%; transform: translateY(-50%);
    width: 4px; height: 60%;
    background: #63b3ed;
    border-radius: 4px;
}
.app-sidebar .nav-link i { font-size: 1.1rem; width: 22px; text-align: center; }

.app-main { flex: 1; display: flex; flex-direction: column; min-width: 0; }

.app-topbar {
    background: rgba(255,255,255,.85);
    backdrop-filter: saturate(180%) blur(8px);
    height: 60px;
    display: flex;
    align-items: center;
    padding: 0 22px;
    box-shadow: 0 2px 12px rgba(20,35,63,.06);
    position: sticky;
    top: 0;
    z-index: 10;
    border-bottom: 1px solid rgba(20,35,63,.05);
}
.app-topbar .user-chip {
    display: flex; align-items: center; gap: 8px;
    background: #f1f5fb; padding: 6px 14px; border-radius: 999px;
    color: #4a5568; font-size: .85rem; font-weight: 600;
}
.app-topbar .user-chip i { color: var(--accent); font-size: 1.05rem; }

.app-content { padding: 26px; flex: 1; animation: fadeInUp .4s var(--t) both; }

/* ===== Headings ===== */
.page-title {
    font-size: 1.5rem;
    font-weight: 700;
    margin-bottom: 20px;
    color: var(--brand);
    display: flex; align-items: center; gap: 10px;
}
.page-title::before {
    content: ""; width: 6px; height: 26px;
    background: linear-gradient(180deg, var(--accent), var(--brand-3));
    border-radius: 4px; display: inline-block;
}

/* ===== Cards / stats ===== */
.stat-card {
    background: var(--card-bg);
    border-radius: var(--radius);
    padding: 20px;
    box-shadow: var(--shadow-sm);
    display: flex;
    align-items: center;
    gap: 16px;
    height: 100%;
    position: relative;
    overflow: hidden;
    transition: transform var(--t), box-shadow var(--t);
    animation: fadeInUp .5s var(--t) both;
}
.stat-card::after {
    content: ""; position: absolute; inset: 0;
    background: radial-gradient(circle at 100% 0%, rgba(49,130,206,.06), transparent 60%);
    pointer-events: none;
}
.stat-card:hover { transform: translateY(-5px); box-shadow: var(--shadow-lg); }

.stat-card .icon {
    width: 56px; height: 56px; border-radius: 15px;
    display: flex; align-items: center; justify-content: center;
    font-size: 1.6rem; color: #fff; flex-shrink: 0;
    box-shadow: 0 8px 18px rgba(20,35,63,.18);
    transition: transform var(--t);
}
.stat-card:hover .icon { transform: scale(1.08) rotate(-4deg); }

.stat-card .value { font-size: 1.5rem; font-weight: 700; line-height: 1.2; }
.stat-card .label { color: var(--muted); font-size: .85rem; }

.bg-grad-blue { background: linear-gradient(135deg,#4299e1,#3182ce); }
.bg-grad-green { background: linear-gradient(135deg,#48bb78,#38a169); }
.bg-grad-red { background: linear-gradient(135deg,#f56565,#e53e3e); }
.bg-grad-orange { background: linear-gradient(135deg,#ed8936,#dd6b20); }
.bg-grad-purple { background: linear-gradient(135deg,#9f7aea,#805ad5); }
.bg-grad-teal { background: linear-gradient(135deg,#38b2ac,#319795); }

.card {
    border: none;
    border-radius: var(--radius);
    box-shadow: var(--shadow-sm);
    transition: box-shadow var(--t), transform var(--t);
    animation: fadeInUp .5s var(--t) both;
}
.card:hover { box-shadow: var(--shadow); }
.card-header {
    background: #fff;
    border-bottom: 1px solid #eef2f7;
    font-weight: 700;
    color: var(--brand);
    padding: 16px 20px;
    border-radius: var(--radius) var(--radius) 0 0 !important;
}
.card-body { padding: 20px; }

/* ===== Tables ===== */
.table { margin-bottom: 0; }
.table thead th {
    background: #f7fafc;
    color: #4a5568;
    font-weight: 700;
    white-space: nowrap;
    border-bottom: 2px solid #e2e8f0;
    font-size: .85rem;
    letter-spacing: .2px;
}
.table tbody tr { transition: background var(--t-fast); }
.table-hover tbody tr:hover { background: #f1f7fd; }
.table td, .table th { vertical-align: middle; }

.table-trial th, .table-trial td { text-align: center; vertical-align: middle; font-size: .85rem; }
.table-trial thead th { background: var(--brand); color: #fff; border-color: var(--brand-3); }

/* card-table wrapper */
.table-card {
    background: #fff; border-radius: var(--radius);
    box-shadow: var(--shadow-sm); overflow: hidden;
    animation: fadeInUp .5s var(--t) both;
}
.table-card .table { margin: 0; }

/* ===== Buttons ===== */
.btn {
    border-radius: 10px;
    font-weight: 600;
    transition: transform var(--t-fast), box-shadow var(--t-fast), filter var(--t-fast);
    display: inline-flex; align-items: center; gap: 6px;
}
.btn:active { transform: translateY(1px) scale(.98); }
.btn-primary {
    background: linear-gradient(135deg, var(--accent-2), var(--accent));
    border: none;
    box-shadow: 0 6px 16px rgba(49,130,206,.32);
}
.btn-primary:hover { filter: brightness(1.06); box-shadow: 0 9px 22px rgba(49,130,206,.42); transform: translateY(-2px); }
.btn-success { background: linear-gradient(135deg,#48bb78,#38a169); border: none; box-shadow: 0 6px 16px rgba(56,161,105,.3); }
.btn-success:hover { transform: translateY(-2px); filter: brightness(1.05); }
.btn-danger  { background: linear-gradient(135deg,#f56565,#e53e3e); border: none; box-shadow: 0 6px 16px rgba(229,62,62,.28); }
.btn-danger:hover { transform: translateY(-2px); filter: brightness(1.05); }
.btn-outline-secondary:hover, .btn-outline-primary:hover, .btn-outline-danger:hover { transform: translateY(-2px); }
.btn-sm { border-radius: 8px; }

/* ===== Forms ===== */
.form-label { font-weight: 600; color: #4a5568; font-size: .88rem; margin-bottom: .3rem; }
.form-control, .form-select {
    border-radius: 11px;
    border: 1.5px solid #e2e8f0;
    padding: .55rem .9rem;
    transition: border-color var(--t-fast), box-shadow var(--t-fast), background var(--t-fast);
    background: #fbfcfe;
}
.form-control:focus, .form-select:focus {
    border-color: var(--accent-2);
    box-shadow: 0 0 0 .22rem rgba(66,153,225,.16);
    background: #fff;
}
.form-check-input { width: 1.15em; height: 1.15em; cursor: pointer; }
.form-check-input:checked { background-color: var(--accent); border-color: var(--accent); }

/* ===== Badges ===== */
.badge { border-radius: 8px; font-weight: 600; padding: .42em .7em; letter-spacing: .2px; }

/* ===== Alerts ===== */
.alert {
    border: none;
    border-radius: var(--radius-sm);
    box-shadow: var(--shadow-sm);
    animation: slideInRight .4s var(--t) both;
    display: flex; align-items: center; gap: 8px;
}
.alert-success { background: #e9f8ef; color: #276749; }
.alert-danger  { background: #fdecec; color: #9b2c2c; }
.alert .btn-close { margin-inline-start: auto; }

/* ===== Auth ===== */
.auth-wrap {
    min-height: 100vh;
    display: flex;
    align-items: center;
    justify-content: center;
    background: linear-gradient(135deg, var(--brand) 0%, var(--brand-2) 50%, var(--brand-3) 100%);
    padding: 20px;
    position: relative;
    overflow: hidden;
}
.auth-wrap::before, .auth-wrap::after {
    content: ""; position: absolute; border-radius: 50%;
    background: radial-gradient(circle, rgba(99,179,237,.25), transparent 70%);
}
.auth-wrap::before { width: 460px; height: 460px; top: -160px; right: -120px; }
.auth-wrap::after  { width: 380px; height: 380px; bottom: -140px; left: -100px; }

.auth-card {
    background: #fff;
    border-radius: 22px;
    padding: 40px;
    width: 100%;
    max-width: 420px;
    box-shadow: var(--shadow-lg);
    position: relative;
    z-index: 1;
    animation: popIn .45s var(--t) both;
}
.auth-card h1 { font-size: 1.5rem; font-weight: 700; color: var(--brand); text-align: center; }

/* ===== Utilities ===== */
.hover-lift { transition: transform var(--t), box-shadow var(--t); }
.hover-lift:hover { transform: translateY(-4px); box-shadow: var(--shadow); }

/* staggered list animation for table rows / cards */
.stagger > * { animation: fadeInUp .45s var(--t) both; }
.stagger > *:nth-child(1) { animation-delay: .03s; }
.stagger > *:nth-child(2) { animation-delay: .06s; }
.stagger > *:nth-child(3) { animation-delay: .09s; }
.stagger > *:nth-child(4) { animation-delay: .12s; }
.stagger > *:nth-child(5) { animation-delay: .15s; }
.stagger > *:nth-child(6) { animation-delay: .18s; }

/* ===== Responsive ===== */
@media (max-width: 767.98px) {
    .app-sidebar { position: fixed; right: -300px; z-index: 1050; transition: right var(--t); }
    body.sidebar-open .app-sidebar { right: 0; }
    body.sidebar-open::after {
        content: ""; position: fixed; inset: 0; background: rgba(20,35,63,.45); z-index: 1040;
    }
    .app-content { padding: 18px; }
}

@media print {
    .app-sidebar, .app-topbar, .no-print, .btn { display: none !important; }
    .app-content { padding: 0; animation: none; }
    body { background: #fff; }
    .card, .stat-card, .table-card { box-shadow: none; animation: none; }
}

/* Respect reduced motion */
@media (prefers-reduced-motion: reduce) {
    *, *::before, *::after { animation: none !important; transition: none !important; }
}

/* ===== Tom Select ===== */
.ts-wrapper { direction: rtl; }

.ts-control {
    border: 1.5px solid #e2e8f0 !important;
    border-radius: 11px !important;
    background: #fbfcfe !important;
    padding: .5rem .9rem !important;
    min-height: 42px;
    box-shadow: none !important;
    transition: border-color var(--t-fast), box-shadow var(--t-fast);
    font-size: .95rem;
    cursor: pointer;
    display: flex; align-items: center; flex-wrap: wrap; gap: 4px;
}
.ts-wrapper.focus .ts-control {
    border-color: var(--accent-2) !important;
    box-shadow: 0 0 0 .22rem rgba(66,153,225,.16) !important;
    background: #fff !important;
}

.ts-control input {
    font-size: .93rem !important;
    color: var(--ink) !important;
    background: transparent !important;
    flex: 1; min-width: 80px;
    padding: 0 !important;
    margin: 0 !important;
    border: none !important;
    outline: none !important;
}
.ts-control input::placeholder { color: #a0aec0; }

/* arrow */
.ts-wrapper:not(.multi) .ts-control::after {
    border-color: #718096 transparent transparent;
    border-width: 6px 5px 0;
    margin-inline-start: auto;
}
.ts-wrapper:not(.multi).dropdown-active .ts-control::after {
    border-color: transparent transparent #718096;
    border-width: 0 5px 6px;
}

/* dropdown */
.ts-dropdown {
    border: 1.5px solid #c9d8ee !important;
    border-radius: 12px !important;
    box-shadow: 0 12px 32px rgba(20,35,63,.13) !important;
    margin-top: 4px !important;
    padding: 6px 0 !important;
    background: #fff !important;
    animation: fadeInUp .18s ease both;
    z-index: 1060 !important;
    direction: rtl;
}
.ts-dropdown-content { max-height: 240px; padding: 0 !important; }

.ts-dropdown .option {
    padding: 9px 16px !important;
    font-size: .93rem !important;
    color: #2d3748 !important;
    transition: background var(--t-fast), color var(--t-fast);
    cursor: pointer;
}
.ts-dropdown .option:hover,
.ts-dropdown .option.active {
    background: #eef5ff !important;
    color: var(--accent) !important;
}
.ts-dropdown .option.selected {
    background: #dbeafe !important;
    color: var(--brand) !important;
    font-weight: 600;
}
.ts-dropdown .create { color: var(--ok); }
.ts-no-results {
    padding: 10px 16px;
    color: var(--muted);
    font-size: .88rem;
    text-align: center;
}

/* highlight matched text */
.ts-dropdown .highlight { background: transparent; color: var(--accent); font-weight: 700; }

/* hide the original select when Tom Select takes over */
.ts-hidden-accessible { position: absolute !important; visibility: hidden !important; }
/* reset Bootstrap select arrow on the wrapper */
.ts-wrapper.form-select, .ts-wrapper.form-select-sm {
    padding: 0 !important;
    background-image: none !important;
    border: none !important;
}

/* small variant (journal lines) */
.form-select-sm + .ts-wrapper .ts-control,
.ts-wrapper.form-select-sm .ts-control {
    min-height: 34px !important;
    padding: .3rem .7rem !important;
    font-size: .85rem !important;
    border-radius: 8px !important;
}

/* ===== Autocomplete ===== */
.ac-dropdown {
    position: absolute;
    top: 100%;
    right: 0; left: 0;
    z-index: 1060;
    background: #fff;
    border: 1.5px solid #c9d8ee;
    border-radius: 12px;
    box-shadow: 0 12px 32px rgba(20,35,63,.14);
    list-style: none;
    margin: 4px 0 0;
    padding: 6px 0;
    max-height: 240px;
    overflow-y: auto;
    animation: fadeInUp .18s ease both;
}
.ac-item {
    padding: 9px 16px;
    cursor: pointer;
    font-size: .93rem;
    color: #2d3748;
    transition: background var(--t-fast), color var(--t-fast);
    border-radius: 0;
}
.ac-item:hover, .ac-item.ac-active {
    background: #eef5ff;
    color: var(--accent);
}
.ac-item mark {
    background: transparent;
    color: var(--accent);
    font-weight: 700;
    padding: 0;
}
