:root {
    --sidebar-w: 240px;
    --sidebar-w-mobile: 280px;
    --sidebar-bg: #1e293b;
    --sidebar-fg: #e2e8f0;
    --sidebar-hover: #334155;
    --sidebar-active: #0ea5e9;
    --topbar-h: 56px;
    --topbar-h-mobile: 52px;
    --primary: #0ea5e9;
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
    font-family: -apple-system, "Segoe UI", Roboto, Arial, sans-serif;
    background: #f1f5f9;
    margin: 0;
    -webkit-tap-highlight-color: transparent;
    overflow-x: hidden;
}

/* ===== LOGIN ===== */
.bg-login { background: linear-gradient(135deg, #0ea5e9 0%, #1e293b 100%); min-height: 100vh; }
.login-wrap { min-height: 100vh; display: flex; align-items: center; justify-content: center; padding: 16px; }
.login-card { background: #fff; padding: 32px; border-radius: 12px; box-shadow: 0 20px 60px rgba(0,0,0,.2); width: 100%; max-width: 420px; }
.login-brand { display: flex; align-items: center; gap: 14px; margin-bottom: 24px; padding-bottom: 16px; border-bottom: 1px solid #e2e8f0; }
.login-brand .logo { width: 48px; height: 48px; background: var(--primary); color: #fff; border-radius: 10px; display: grid; place-items: center; font-weight: 700; }
.login-brand h2 { margin: 0; font-size: 20px; }
.login-brand small { color: #64748b; }

/* ===== APP SHELL ===== */
.app-shell { display: flex; min-height: 100vh; }
.app-sidebar {
    width: var(--sidebar-w);
    background: var(--sidebar-bg);
    color: var(--sidebar-fg);
    position: fixed; left: 0; top: 0; bottom: 0;
    overflow-y: auto;
    transition: transform .2s ease;
    z-index: 200;
}
.app-sidebar .brand { display: flex; align-items: center; gap: 12px; padding: 16px; border-bottom: 1px solid rgba(255,255,255,.08); }
.app-sidebar .brand .logo { width: 36px; height: 36px; background: var(--primary); border-radius: 8px; display: grid; place-items: center; font-weight: 700; }
.app-sidebar .brand strong { display: block; }
.app-sidebar .brand small { color: #94a3b8; font-size: 11px; }

.menu { padding: 8px 0; }
.menu-item {
    display: flex; align-items: center; gap: 12px;
    padding: 10px 16px; color: var(--sidebar-fg); text-decoration: none;
    border-left: 3px solid transparent; font-size: 14px;
}
.menu-item:hover { background: var(--sidebar-hover); color: #fff; }
.menu-item.active { background: var(--sidebar-hover); color: #fff; border-left-color: var(--sidebar-active); }
.menu-item i { width: 18px; }

.sidebar-backdrop {
    display: none;
    position: fixed; inset: 0;
    background: rgba(15, 23, 42, .55);
    z-index: 199;
    backdrop-filter: blur(2px);
}
body.sidebar-open .sidebar-backdrop { display: block; }

.app-main { margin-left: var(--sidebar-w); flex: 1; min-width: 0; }
.app-topbar {
    height: var(--topbar-h);
    background: #fff;
    border-bottom: 1px solid #e2e8f0;
    display: flex; align-items: center; padding: 0 16px; gap: 8px;
    position: sticky; top: 0; z-index: 100;
}
.app-topbar .sidebar-toggle {
    padding: 6px 10px;
    font-size: 22px; line-height: 1;
    color: #0f172a;
}
.topbar-title { font-size: 18px; margin: 0; flex: 1; font-weight: 600; color: #0f172a;
    white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.topbar-user .btn { white-space: nowrap; }
.app-content { padding: 24px; }

/* ===== CARDS ===== */
.card-stat {
    background: #fff; padding: 20px; border-radius: 10px; box-shadow: 0 1px 3px rgba(0,0,0,.05);
    display: flex; align-items: center; gap: 16px;
}
.card-stat .icon { width: 48px; height: 48px; border-radius: 10px; display: grid; place-items: center; font-size: 22px; color: #fff; flex-shrink: 0; }
.card-stat .icon.bg-primary { background: var(--primary); }
.card-stat .icon.bg-success { background: #10b981; }
.card-stat .icon.bg-warning { background: #f59e0b; }
.card-stat .icon.bg-danger { background: #ef4444; }
.card-stat .icon.bg-info { background: #6366f1; }
.card-stat .label { color: #64748b; font-size: 13px; }
.card-stat .value { font-size: 22px; font-weight: 700; color: #0f172a; line-height: 1.2; }

.section-card { background: #fff; border-radius: 10px; box-shadow: 0 1px 3px rgba(0,0,0,.05); padding: 20px; margin-bottom: 16px; }
.section-card h3 { font-size: 16px; margin: 0 0 16px; color: #0f172a; font-weight: 600; }

/* ===== TABLES ===== */
.table-clean { width: 100%; border-collapse: collapse; }
.table-clean th { background: #f8fafc; font-size: 12px; text-transform: uppercase; letter-spacing: .5px; color: #64748b; text-align: left; }
.table-clean td, .table-clean th { padding: 10px 12px; border-bottom: 1px solid #e2e8f0; vertical-align: middle; }

/* Validation indicator */
.field-status { display: inline-flex; align-items: center; font-size: 12px; margin-left: 8px; }
.field-status.ok { color: #10b981; }
.field-status.err { color: #ef4444; }
.field-status.warn { color: #f59e0b; }

/* Status badges */
.badge-status-0 { background: #94a3b8; }
.badge-status-1 { background: #f59e0b; }
.badge-status-2 { background: #0ea5e9; }
.badge-status-3 { background: #10b981; }
.badge-status-9 { background: #ef4444; }

/* Toolbar */
.toolbar { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; margin-bottom: 16px; }
.toolbar .ms-auto { margin-left: auto; }

/* =====================================================
   RESPONSIVE - tu nhan dien thiet bi
   - Tablet:  <= 991.98px
   - Mobile:  <= 767.98px
   - Tiny:    <= 575.98px
   ===================================================== */

/* ----- Tablet ----- */
@media (max-width: 991.98px) {
    .app-content { padding: 18px; }
    .section-card { padding: 16px; }
    .card-stat { padding: 16px; gap: 12px; }
    .card-stat .icon { width: 42px; height: 42px; font-size: 20px; }
    .card-stat .value { font-size: 20px; }
}

/* ----- Mobile (sidebar an, layout 1 cot) ----- */
@media (max-width: 767.98px) {
    .app-sidebar {
        width: var(--sidebar-w-mobile);
        transform: translateX(-100%);
        box-shadow: 4px 0 20px rgba(0,0,0,.35);
    }
    body.sidebar-open .app-sidebar { transform: translateX(0); }
    .app-main { margin-left: 0; }

    .app-topbar {
        height: var(--topbar-h-mobile);
        padding: 0 10px; gap: 6px;
    }
    .topbar-title { font-size: 16px; }
    .topbar-user .btn { padding: 6px 10px; font-size: 13px; }
    .topbar-user .btn .badge { display: none; }

    .app-content { padding: 12px; }
    .section-card { padding: 14px 12px; border-radius: 8px; margin-bottom: 12px; }
    .section-card h3 { font-size: 15px; margin-bottom: 12px; }

    .card-stat { padding: 12px; gap: 10px; }
    .card-stat .icon { width: 38px; height: 38px; font-size: 17px; border-radius: 8px; }
    .card-stat .value { font-size: 18px; }
    .card-stat .label { font-size: 12px; }

    /* Tables: scroll ngang trong section-card */
    .section-card { overflow-x: auto; -webkit-overflow-scrolling: touch; }
    .table-clean { min-width: 560px; }
    .table-clean td, .table-clean th { padding: 8px 10px; font-size: 13px; }
    .table-clean th { font-size: 11px; }

    /* Toolbar stack ben duoi nhau */
    .toolbar { flex-direction: column; align-items: stretch; gap: 8px; }
    .toolbar > * { width: 100%; }
    .toolbar form { width: 100%; flex-wrap: wrap; gap: 6px !important; }
    .toolbar form > * { flex: 1 1 auto; min-width: 0; }
    .toolbar .ms-auto { margin-left: 0 !important; }

    /* Override inline width="..." cua input/select tren mobile */
    .toolbar input,
    .toolbar select,
    .form-control[style*="width"],
    .form-select[style*="width"] { width: auto !important; max-width: 100% !important; }

    /* Tap target lon hon */
    .menu-item { padding: 14px 18px; font-size: 15px; }
    .menu-item i { font-size: 18px; }
    .btn { min-height: 38px; }
    .btn-sm { min-height: 34px; }
    .form-check-input { width: 1.4em; height: 1.4em; }

    /* Tranh iOS auto-zoom khi focus input < 16px */
    .form-control, .form-select, input[type="text"], input[type="number"],
    input[type="date"], input[type="email"], input[type="password"], textarea {
        font-size: 16px;
    }
    /* Modal full-width tren mobile */
    .modal-dialog { margin: 10px; }

    /* Toast khit le mobile */
    #toast-host { left: 10px !important; right: 10px !important; top: 10px !important; }
    #toast-host > div { min-width: 0 !important; width: 100% !important; }

    /* Don alerts gon hon */
    .alert { padding: 10px 12px; font-size: 14px; }
}

/* ----- Tiny phone (<=575.98px) ----- */
@media (max-width: 575.98px) {
    .app-topbar { padding: 0 8px; }
    .topbar-title { font-size: 15px; }
    .topbar-user .dropdown-toggle::after { display: none; }
    .topbar-user .btn span.dn-xs,
    .topbar-user .btn .username-text { display: none; }

    .app-content { padding: 10px; }
    .section-card { padding: 12px 10px; }
    .section-card h3 { font-size: 14px; }

    .login-card { padding: 22px 18px; border-radius: 10px; }
    .login-brand h2 { font-size: 18px; }

    /* Stack flex hang doc tren tiny */
    .d-flex.gap-2 > .btn,
    .d-flex.gap-3 > * { min-width: 0; }

    /* Group buttons trong table de chong overflow */
    .table-clean td .btn { padding: 4px 8px; font-size: 12px; }
}

/* Print: bo sidebar va topbar */
@media print {
    .app-sidebar, .app-topbar, .sidebar-backdrop { display: none !important; }
    .app-main { margin-left: 0; }
    .app-content { padding: 0; }
    .section-card { box-shadow: none; }
}
