/**
 * Staff App — Mobile & Tablet (admin, bendahara, BK, koordinator)
 * Portal siswa memakai siswa-mobile.css terpisah.
 */
:root {
    --staff-safe-top: env(safe-area-inset-top, 0px);
    --staff-safe-bottom: env(safe-area-inset-bottom, 0px);
    --staff-navbar-h: 56px;
}

html {
    -webkit-text-size-adjust: 100%;
}

body.staff-app {
    overscroll-behavior-y: contain;
    -webkit-tap-highlight-color: transparent;
}

/* ===== NAVBAR MOBILE ===== */
@media (max-width: 991.98px) {
    .app-navbar {
        height: calc(var(--staff-navbar-h) + var(--staff-safe-top));
        padding-top: var(--staff-safe-top);
        padding-left: 0.75rem;
        padding-right: 0.75rem;
        backdrop-filter: blur(12px);
        background: rgba(255, 255, 255, 0.92);
    }

    .navbar-page-title {
        font-size: 0.9375rem;
    }

    .sidebar-toggle {
        display: inline-flex;
        width: 44px;
        height: 44px;
        display: inline-flex;
        align-items: center;
        justify-content: center;
        margin-right: 0.25rem;
        border-radius: 10px;
    }

    .sidebar-toggle:active {
        background: var(--body-bg);
    }

    .navbar-context-btn {
        padding: 0.5rem 0.65rem;
        min-height: 40px;
        border-radius: 10px;
        font-size: 0.75rem;
    }

    .user-dropdown {
        min-height: 44px;
        padding: 0.25rem;
    }

    .user-avatar {
        width: 40px;
        height: 40px;
    }

    .navbar-actions .dropdown-menu {
        border-radius: 14px;
        padding: 0.35rem;
        min-width: min(280px, calc(100vw - 2rem));
    }
}

/* ===== LAYOUT & CONTENT ===== */
@media (max-width: 991.98px) {
    .app-main {
        min-height: 100dvh;
        min-width: 0;
        width: 100%;
        max-width: 100%;
        margin-left: 0 !important;
    }

    .app-content {
        min-width: 0;
        width: 100%;
        max-width: 100%;
        box-sizing: border-box;
        padding: 1rem max(1rem, env(safe-area-inset-right, 0px)) calc(var(--staff-safe-bottom) + 1.25rem) max(1rem, env(safe-area-inset-left, 0px));
        overflow-x: hidden;
    }

    .app-content > .row,
    .app-content > form > .row,
    .app-content .card-modern .row {
        --bs-gutter-x: 1rem;
        max-width: 100%;
        min-width: 0;
    }

    .app-content .row > [class*="col-"] {
        min-width: 0;
    }

    .app-footer {
        display: none;
    }

    .page-header {
        margin-bottom: 1.125rem;
    }

    /* Toolbar: judul kiri, tombol kanan — tetap horizontal di mobile */
    .page-header.d-flex.justify-content-between {
        flex-direction: row;
        align-items: center !important;
        gap: 0.75rem !important;
    }

    .page-header.d-flex.justify-content-between > div:first-child {
        flex: 1;
        min-width: 0;
    }

    .page-header.d-flex.justify-content-between > .btn,
    .page-header.d-flex.justify-content-between > a.btn,
    .page-header.d-flex.justify-content-between > .page-header-actions {
        width: auto !important;
        flex-shrink: 0;
        align-self: center;
        min-height: 38px;
        padding: 0.45rem 0.875rem;
        font-size: 0.8125rem;
        white-space: nowrap;
    }

    .page-header-actions {
        display: flex;
        align-items: center;
        gap: 0.5rem;
        flex-shrink: 0;
    }

    .page-header-actions .btn {
        width: auto !important;
    }

    /* Header tanpa toolbar — tetap stack vertikal */
    .page-header.d-flex:not(.justify-content-between) {
        flex-direction: column;
        align-items: stretch !important;
        gap: 0.75rem !important;
    }

    .page-header:not(.justify-content-between) .btn,
    .page-header:not(.justify-content-between) .btn-group {
        width: 100%;
    }

    .page-header .btn-group .btn {
        flex: 1;
    }

    .card-modern .card-header.d-flex.justify-content-between {
        flex-direction: row !important;
        align-items: center !important;
        justify-content: space-between;
    }

    .card-modern .card-header.d-flex.justify-content-between .card-title {
        flex: 1;
        min-width: 0;
    }

    .card-modern .card-header.d-flex.justify-content-between .btn {
        width: auto !important;
        flex-shrink: 0;
        min-height: 34px;
        padding: 0.35rem 0.75rem;
        font-size: 0.8125rem;
        white-space: nowrap;
    }

    .page-title {
        font-size: 1.25rem;
        line-height: 1.3;
    }

    .page-subtitle {
        font-size: 0.8125rem;
    }
}

/* ===== STAT CARDS ===== */
@media (max-width: 991.98px) {
    .stat-card {
        padding: 1rem;
        gap: 0.75rem;
    }

    .stat-icon {
        width: 44px;
        height: 44px;
        font-size: 1.25rem;
    }

    .stat-value {
        font-size: 1.125rem;
    }

    .stat-value-sm {
        font-size: 0.9375rem;
    }

    .stat-label {
        font-size: 0.75rem;
    }
}

@media (max-width: 575.98px) {
    .row.g-4.mb-4 > [class*="col-"],
    .row.g-4 > [class*="col-"] {
        flex: 0 0 50%;
        max-width: 50%;
    }

    .row.g-4.mb-4 > .col-xxl-2,
    .row.g-4 > .col-xxl-2 {
        flex: 0 0 50%;
        max-width: 50%;
    }
}

@media (max-width: 399.98px) {
    .row.g-4.mb-4 > [class*="col-"],
    .row.g-4 > [class*="col-"] {
        flex: 0 0 100%;
        max-width: 100%;
    }
}

/* ===== CARDS & TABLES ===== */
@media (max-width: 991.98px) {
    .card-modern {
        border-radius: 14px;
        max-width: 100%;
        min-width: 0;
    }

    .card-modern .card-header {
        padding: 0.875rem 1rem;
        flex-wrap: wrap;
        gap: 0.5rem;
    }

    .card-modern .card-body:not(.p-0) {
        padding: 1rem;
    }

    /* Kontainer scroll horizontal — lebar dibatasi agar bisa digeser */
    .table-responsive,
    .bk-table-scroll,
    .spp-matrix-wrap,
    .backup-restore-panel .br-table-wrap {
        display: block;
        width: 100%;
        max-width: 100%;
        overflow-x: auto !important;
        -webkit-overflow-scrolling: touch;
        touch-action: pan-x pan-y;
        overscroll-behavior-x: contain;
        background: var(--card-bg, #fff);
    }

    .card-modern .card-body.p-0 {
        width: 100%;
        max-width: 100%;
        background: var(--card-bg, #fff);
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
        touch-action: pan-x pan-y;
    }

    /* Jika ada wrapper scroll di dalam, parent hanya memotong lebar */
    .card-modern .card-body.p-0:has(> .table-responsive),
    .card-modern .card-body.p-0:has(> .bk-table-scroll),
    .card-modern .card-body.p-0:has(> .table-scroll-hint) {
        overflow: hidden;
    }

    .card-modern .data-table {
        background: var(--card-bg, #fff);
        margin-bottom: 0;
    }

    .card-modern .data-table thead th {
        background: #f8fafc;
    }

    .card-modern .data-table tbody td {
        background: var(--card-bg, #fff);
    }

    .data-table {
        min-width: 540px;
        width: max-content;
    }

    .data-table thead th,
    .data-table tbody td {
        padding: 0.75rem 1rem;
    }

    .data-table .btn-sm {
        min-height: 36px;
        padding: 0.35rem 0.65rem;
    }

    .table-scroll-hint {
        display: flex;
        align-items: center;
        gap: 0.35rem;
        padding: 0.5rem 1rem;
        font-size: 0.6875rem;
        color: var(--text-secondary);
        background: var(--card-bg, #fff);
        border-bottom: 1px solid var(--border-color);
        flex-shrink: 0;
    }

    .table-scroll-hint i {
        font-size: 0.875rem;
    }
}

/* ===== FORMS & BUTTONS ===== */
@media (max-width: 991.98px) {
    .btn {
        min-height: 44px;
        display: inline-flex;
        align-items: center;
        justify-content: center;
    }

    .btn-sm {
        min-height: 38px;
    }

    .btn-group .btn {
        min-height: 44px;
    }

    .form-control,
    .form-select {
        min-height: 44px;
        font-size: 16px;
    }

    .form-control-sm,
    .form-select-sm {
        min-height: 40px;
        font-size: 14px;
    }

    .form-label {
        font-size: 0.875rem;
        margin-bottom: 0.35rem;
    }

    .row.g-3 > [class*="col-"],
    .row.g-4 > [class*="col-"]:not([class*="col-xxl"]) {
        margin-bottom: 0;
    }

    .d-flex.flex-wrap.gap-2:not(.justify-content-between),
    .d-flex.flex-wrap.gap-3:not(.justify-content-between) {
        flex-direction: column;
    }

    .page-header .d-flex.flex-wrap.gap-2:not(.justify-content-between),
    .page-header .d-flex.flex-wrap.gap-3:not(.justify-content-between),
    .card-header .d-flex.flex-wrap.gap-2:not(.justify-content-between),
    .card-header .d-flex.flex-wrap.gap-3:not(.justify-content-between),
    .backup-restore-panel .br-actions.d-flex,
    .backup-restore-panel .br-toolbar.d-flex {
        flex-direction: column;
    }

    .page-header .d-flex.flex-wrap.gap-2:not(.justify-content-between) .btn,
    .page-header .d-flex.flex-wrap.gap-3:not(.justify-content-between) .btn,
    .card-header .d-flex.flex-wrap.gap-2:not(.justify-content-between) .btn,
    .card-header .d-flex.flex-wrap.gap-3:not(.justify-content-between) .btn,
    .backup-restore-panel .br-actions .btn-rdm,
    .backup-restore-panel .br-actions .btn-rdm-outline {
        width: 100%;
    }
}

/* ===== MODALS ===== */
@media (max-width: 767.98px) {
    .modal-dialog {
        margin: 0.5rem;
        max-width: calc(100% - 1rem);
    }

    .modal-dialog.modal-xl,
    .modal-dialog.modal-lg {
        max-width: calc(100% - 1rem);
    }

    .modal-modern .modal-header,
    .modal-modern .modal-body,
    .modal-modern .modal-footer {
        padding-left: 1rem;
        padding-right: 1rem;
    }

    .modal-upload-header {
        flex-wrap: wrap;
        gap: 0.75rem;
    }

    .modal-upload-actions {
        width: 100%;
        justify-content: space-between;
    }

    .upload-dropzone {
        margin: 1rem;
        min-height: 120px;
    }
}

@media (max-width: 575.98px) {
    .modal-dialog {
        margin: 0;
        max-width: 100%;
        min-height: 100dvh;
        align-items: stretch;
    }

    .modal-content {
        min-height: 100dvh;
        border-radius: 0 !important;
        border: none;
    }

    .modal-header {
        padding-top: calc(0.75rem + var(--staff-safe-top));
    }

    .modal-footer {
        padding-bottom: calc(0.75rem + var(--staff-safe-bottom));
    }
}

/* ===== SIDEBAR DRAWER ===== */
@media (max-width: 991.98px) {
    .app-sidebar {
        width: min(300px, 88vw);
        padding-bottom: var(--staff-safe-bottom);
        box-shadow: 4px 0 24px rgba(0, 0, 0, 0.2);
    }

    .sidebar-link {
        min-height: 48px;
        padding: 0.75rem 1rem;
        font-size: 0.9375rem;
    }

    .sidebar-sublink {
        min-height: 44px;
    }

    .sidebar-brand {
        padding-top: calc(1rem + var(--staff-safe-top));
    }

    .sidebar-overlay.show {
        backdrop-filter: blur(2px);
    }
}

/* ===== TOOLBARS ===== */
@media (max-width: 991.98px) {
    .tunggakan-toolbar {
        padding: 0.875rem 1rem;
    }

    .tunggakan-toolbar-inner {
        flex-direction: column;
        align-items: stretch;
    }

    .tunggakan-toolbar-left {
        margin-right: 0;
        width: 100%;
    }

    .tunggakan-select,
    .tunggakan-search-input {
        width: 100%;
        min-width: 0;
    }

    .tunggakan-search-wrap {
        width: 100%;
    }

    .backup-restore-panel .br-header {
        flex-direction: column;
        align-items: stretch;
        padding: 1rem;
    }

    .backup-restore-panel .br-actions {
        width: 100%;
    }

    .backup-restore-panel .br-actions .btn-rdm {
        flex: 1 1 auto;
        justify-content: center;
    }

    .backup-restore-panel .br-toolbar {
        flex-direction: column;
        align-items: stretch;
        padding: 0.75rem 1rem;
    }

    .backup-restore-panel .br-toolbar input[type="search"] {
        width: 100%;
        min-width: 0;
    }

    .backup-restore-panel .br-footer {
        flex-direction: column;
        align-items: stretch;
        text-align: center;
        padding: 0.75rem 1rem;
    }

    .spp-filter-form .row {
        row-gap: 0.75rem;
    }

    .spp-legend {
        gap: 0.5rem 1rem;
    }

    .module-grid {
        grid-template-columns: repeat(2, 1fr);
        gap: 0.75rem;
    }

    .quick-link-card {
        flex-direction: column;
        text-align: center;
        padding: 1rem 0.75rem;
    }

    .quick-link-card i {
        font-size: 1.5rem;
    }
}

@media (max-width: 575.98px) {
    .module-grid {
        grid-template-columns: 1fr;
    }
}

/* ===== DASHBOARD CHARTS ===== */
@media (max-width: 991.98px) {
    .card-modern canvas {
        max-height: 240px;
    }
}

/* Staff: tanpa bottom nav di mobile — navigasi lewat sidebar (☰) */
.staff-bottom-nav {
    display: none !important;
}

/* ===== AUTH LOGIN MOBILE ===== */
@media (max-width: 991.98px) {
    body.auth-body {
        background: #fff;
    }

    .auth-panel {
        min-height: auto;
    }
}

@media (max-width: 767.98px) {
    .auth-info {
        display: none;
    }

    .auth-split {
        min-height: 100dvh;
    }

    .auth-panel {
        flex: 1;
        min-height: 100dvh;
        padding: calc(1.5rem + env(safe-area-inset-top, 0px)) 1.25rem calc(1.5rem + env(safe-area-inset-bottom, 0px));
        display: flex;
        align-items: center;
        justify-content: center;
    }

    .auth-panel-inner {
        width: 100%;
        max-width: 420px;
    }

    .auth-form .form-control,
    .auth-form .form-select,
    .auth-form .btn {
        min-height: 48px;
        font-size: 16px;
    }
}

/* ===== GLOBAL MOBILE & TABLET ===== */
@media (max-width: 991.98px) {
    body.staff-app,
    body.auth-body,
    body.install-body {
        overflow-x: hidden;
        width: 100%;
        max-width: 100%;
    }

    body.staff-app.sidebar-open {
        overflow: hidden;
    }

    body.staff-app.sidebar-open .app-content {
        touch-action: none;
    }

    .app-wrapper {
        width: 100%;
        max-width: 100%;
        min-width: 0;
        overflow-x: hidden;
    }

    .app-navbar {
        width: 100%;
        max-width: 100%;
        box-sizing: border-box;
    }

    .app-content img,
    .app-content video,
    .app-content canvas,
    .app-content svg {
        max-width: 100%;
        height: auto;
    }

    .input-group {
        flex-wrap: wrap;
    }

    .input-group > .form-control,
    .input-group > .form-select {
        min-width: 0;
        flex: 1 1 auto;
    }

    .btn-toolbar {
        flex-wrap: wrap;
        gap: 0.5rem;
    }

    .btn-toolbar .btn,
    .btn-toolbar .btn-group {
        flex: 1 1 auto;
    }

    .alert {
        font-size: 0.875rem;
        padding: 0.75rem 1rem;
    }

    .pagination {
        flex-wrap: wrap;
        justify-content: center;
        gap: 0.25rem;
    }

    .page-link {
        min-width: 40px;
        min-height: 40px;
        display: inline-flex;
        align-items: center;
        justify-content: center;
    }

    /* Tabel backup/update panel */
    .backup-restore-panel .br-table-wrap {
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
        touch-action: pan-x pan-y;
    }

    .backup-restore-panel .br-table {
        min-width: 520px;
    }

    .backup-restore-panel .br-table thead th,
    .backup-restore-panel .br-table tbody td {
        padding: 0.65rem 0.75rem;
        font-size: 0.8125rem;
    }

    .backup-restore-panel .br-actions-cell {
        white-space: nowrap;
    }

    /* Filter bar umum */
    .filter-bar,
    .toolbar-row {
        flex-direction: column !important;
        align-items: stretch !important;
        gap: 0.5rem !important;
    }

    .filter-bar .form-control,
    .filter-bar .form-select,
    .toolbar-row .form-control,
    .toolbar-row .form-select {
        width: 100% !important;
    }

    /* Badge di tabel tidak pecah layout */
    .data-table .badge,
    .br-table .badge {
        white-space: nowrap;
    }

    /* Dropdown lebar penuh di layar kecil */
    .dropdown-menu {
        max-width: calc(100vw - 2rem);
    }
}

/* ===== PHONE KECIL ===== */
@media (max-width: 575.98px) {
    .app-content {
        padding-left: max(0.75rem, env(safe-area-inset-left, 0px));
        padding-right: max(0.75rem, env(safe-area-inset-right, 0px));
    }

    .navbar-page-title {
        font-size: 0.875rem;
        max-width: 42vw;
    }

    .navbar-context-btn {
        padding: 0.4rem 0.5rem !important;
        font-size: 0.6875rem !important;
    }

    .navbar-context-btn .bi {
        margin-right: 0 !important;
    }

    .user-dropdown .bi-chevron-down {
        display: none;
    }

    .card-modern .card-title {
        font-size: 0.9375rem;
    }

    .row > [class*="col-md-"],
    .row > [class*="col-lg-"] {
        margin-bottom: 0.75rem;
    }

    .install-main {
        padding: 1rem 0.75rem calc(1rem + env(safe-area-inset-bottom, 0px));
    }

    .install-main-inner {
        max-width: 100%;
    }
}

/* ===== TABLET LANDSCAPE ===== */
@media (min-width: 768px) and (max-width: 991.98px) {
    .app-content {
        padding-left: 1.25rem;
        padding-right: 1.25rem;
    }

    .row.g-4.mb-4 > [class*="col-md-4"],
    .row.g-4 > [class*="col-md-4"] {
        flex: 0 0 50%;
        max-width: 50%;
    }
}

/* ===== GURU BK — MOBILE ===== */
@media (max-width: 991.98px) {
    .bk-table-scroll .data-table {
        min-width: 540px;
    }

    .bk-table-scroll .table-sm.data-table {
        min-width: 320px;
    }

    /* Toolbar pencarian riwayat siswa */
    .riwayat-siswa-toolbar {
        display: flex;
        justify-content: stretch;
    }

    .riwayat-siswa-search {
        width: 100%;
        max-width: none;
    }

    .riwayat-siswa-search .input-group {
        border: 1px solid var(--border-color, #dee2e6);
        border-radius: 8px;
        overflow: hidden;
    }

    .riwayat-siswa-search .input-group-text {
        border: none;
        background: #fff;
        padding-right: 0.25rem;
    }

    .riwayat-siswa-search .form-control {
        border: none;
        box-shadow: none;
        font-size: 16px;
        padding-left: 0.25rem;
    }

    .riwayat-siswa-search .form-control:focus {
        box-shadow: none;
        outline: none;
    }

    .riwayat-siswa-search .input-group:focus-within {
        border-color: #cbd5e1;
    }

    /* Autocomplete siswa di form pelanggaran */
    .card-overflow-visible {
        overflow: visible !important;
    }

    .siswa-autocomplete-list {
        z-index: 1060;
        max-height: min(240px, 40dvh);
    }

    .siswa-autocomplete-item {
        min-height: 44px;
        display: flex;
        flex-direction: column;
        justify-content: center;
    }

    /* Tombol aksi di halaman detail */
    .bk-action-bar .btn {
        width: 100%;
    }

    /* Quick link dashboard BK */
    .quick-link-card {
        min-height: 56px;
    }
}

@media (max-width: 575.98px) {
    /* Detail pelanggaran / rekomendasi — stack label & nilai */
    .bk-detail-card dl.row dt {
        flex: 0 0 100%;
        max-width: 100%;
        margin-top: 0.75rem;
        padding-bottom: 0.15rem;
        font-size: 0.75rem;
        text-transform: uppercase;
        letter-spacing: 0.02em;
    }

    .bk-detail-card dl.row dt:first-child {
        margin-top: 0;
    }

    .bk-detail-card dl.row dd {
        flex: 0 0 100%;
        max-width: 100%;
        margin-bottom: 0;
    }

    .bk-detail-card .p-3.bg-light.rounded {
        font-size: 0.875rem;
        padding: 0.75rem !important;
    }
}
