/* =========================================
   Main Stylesheet
   ========================================= */

/* ===== BIẾN TOÀN CỤC ===== */
:root {
    --primary-dark: #0f172a;
    --danger:       #dc2626;
    --danger-light: #fee2e2;
    --card-radius:  1rem;
    --safe-bottom:  env(safe-area-inset-bottom, 0px);
}

/* ===== CĂN BẢN ===== */
* { box-sizing: border-box; }

html {
    overscroll-behavior: none;
    background-color: #0f172a; /* Prevent white flash on overscroll */
}

body {
    font-family: 'Be Vietnam Pro', sans-serif;
    background-color: #f1f5f9;
    min-height: 100vh;
    min-height: 100dvh;
    -webkit-tap-highlight-color: transparent;
    -webkit-font-smoothing: antialiased;
    overscroll-behavior: none;
    overflow-x: hidden;
}

/* ===== PWA: iOS Safe Areas ===== */
@supports (padding: env(safe-area-inset-bottom)) {
    .btm-nav {
        padding-bottom: calc(0.25rem + var(--safe-bottom)) !important;
        height: calc(4.2rem + var(--safe-bottom)) !important;
    }
    #main-app > .flex > main {
        padding-bottom: calc(6rem + var(--safe-bottom));
    }
}

/* ===== NAVBAR ===== */
.navbar {
    background: var(--primary-dark);
    height: 3.5rem;
    min-height: 3.5rem;
    padding: 0.5rem 0.75rem;
    z-index: 50;
}

/* Fix: dropdown ẩn vẫn chặn click trên mobile */
.dropdown:not(:focus-within):not(.dropdown-open) > .dropdown-content {
    pointer-events: none !important;
    visibility: hidden !important;
}

@media (min-width: 768px) {
    .navbar { padding: 0.5rem 1.5rem; }
}

/* Nội dung không bị navbar che */
[id^="view-"] {
    scroll-margin-top: 4rem;
}

/* Main content padding-top khi scroll */
#main-app > .flex > main {
    min-height: calc(100vh - 3.5rem);
}

/* ===== SIDEBAR NAV ===== */
.nav-item {
    transition: background 0.2s, color 0.2s;
    border-radius: 0.75rem;
    cursor: pointer;
}
.nav-item.active {
    background-color: #e2e8f0 !important;
    color: #1e293b !important;
    font-weight: 700;
}

/* ===== THẺ HỒ SƠ ===== */
.profile-card {
    background: white;
    border-radius: var(--card-radius);
    border: 1px solid #e2e8f0;
    box-shadow: 0 1px 4px rgba(0,0,0,0.06);
    transition: transform 0.2s, box-shadow 0.2s;
    cursor: pointer;
    overflow: hidden;
}

/* Mobile: không hover effect (touch devices) */
@media (hover: hover) {
    .profile-card:hover {
        transform: translateY(-3px);
        box-shadow: 0 8px 24px rgba(0,0,0,0.12);
    }
}

/* Mobile: card touch feedback */
.profile-card:active {
    transform: scale(0.98);
    transition: transform 0.1s;
}

/* ===== BADGE TRẠNG THÁI ===== */
.status-badge {
    display: inline-flex;
    align-items: center;
    gap: 4px;
    padding: 2px 8px;
    border-radius: 999px;
    font-size: 0.65rem;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 0.03em;
    white-space: nowrap;
}

@media (min-width: 768px) {
    .status-badge {
        padding: 3px 10px;
        font-size: 0.7rem;
        letter-spacing: 0.05em;
    }
}

.status-theo-doi { background: #dcfce7; color: #15803d; }
.status-cai-tap-trung { background: #dbeafe; color: #1d4ed8; }
.status-cai-tai-gia { background: #ede9fe; color: #6d28d9; }
.status-vang-mat { background: #fee2e2; color: #dc2626; animation: pulse-red 2s infinite; }
.status-thi-hanh-an { background: #fef3c7; color: #92400e; }

@keyframes pulse-red {
    0%, 100% { opacity: 1; }
    50% { opacity: 0.6; }
}

/* ===== DANGER LEVEL BADGE ===== */
.danger-thap    { background: #dcfce7; color: #166534; }
.danger-trung   { background: #fef9c3; color: #854d0e; }
.danger-cao     { background: #fed7aa; color: #9a3412; }
.danger-rat-cao { background: #fee2e2; color: #7f1d1d; font-weight: 900; }

/* ===== TABS TRONG MODAL ===== */
.tab-panel {
    animation: fadeIn 0.2s ease;
}
@keyframes fadeIn {
    from { opacity: 0; transform: translateY(6px); }
    to   { opacity: 1; transform: translateY(0); }
}

/* ===== LOGBOOK TABLE ===== */
.logbook-table th {
    background: #f8fafc;
    font-size: 0.65rem;
    text-transform: uppercase;
    letter-spacing: 0.08em;
    color: #64748b;
}

/* ===== SECTION HEADERS TRONG DETAIL ===== */
.detail-section-title {
    font-size: 0.75rem;
    font-weight: 900;
    text-transform: uppercase;
    letter-spacing: 0.08em;
    color: #64748b;
    display: flex;
    align-items: center;
    gap: 0.5rem;
    margin-bottom: 0.75rem;
}

/* ===== LOADING SKELETON ===== */
.skeleton-card {
    background: white;
    border-radius: var(--card-radius);
    border: 1px solid #e2e8f0;
    padding: 1.5rem;
    animation: shimmer 1.5s infinite;
}
@keyframes shimmer {
    0%   { opacity: 1; }
    50%  { opacity: 0.5; }
    100% { opacity: 1; }
}

/* ===== TOAST ===== */
.toast-item {
    border-radius: 1rem;
    box-shadow: 0 8px 24px rgba(0,0,0,0.15);
    transition: all 0.3s ease;
    transform: translateX(20px);
    opacity: 0;
    max-width: calc(100vw - 2rem);
    font-size: 0.85rem;
}
.toast-item.show {
    transform: translateX(0);
    opacity: 1;
}

/* Mobile toast positioning */
@media (max-width: 640px) {
    .toast {
        left: 0.5rem;
        right: 0.5rem;
        bottom: 5rem;
        top: auto !important;
    }
    .toast-item {
        width: 100%;
    }
}

/* ===== AVATAR ===== */
.profile-avatar {
    border-radius: 1rem;
    object-fit: cover;
    border: 3px solid rgba(255,255,255,0.15);
}

/* ===== CARD BỐ CỤC CHI TIẾT ===== */
.detail-header {
    background: linear-gradient(135deg, #0f172a 0%, #1e293b 100%);
    border-radius: var(--card-radius) var(--card-radius) 0 0;
    padding: 1.25rem;
}

.detail-body {
    background: white;
    border-radius: 0 0 var(--card-radius) var(--card-radius);
    padding: 1.25rem;
}

@media (min-width: 768px) {
    .detail-header { padding: 2rem; border-radius: 1.5rem 1.5rem 0 0; }
    .detail-body { padding: 2rem; border-radius: 0 0 1.5rem 1.5rem; }
}

/* ===== MÀN HÌNH ĐĂNG NHẬP ===== */
#login-screen {
    background: radial-gradient(ellipse at center, #1e1b2e 0%, #0a0a0f 70%);
}

/* ===== MOBILE BOTTOM NAV ===== */
.btm-nav {
    height: calc(4.2rem + var(--safe-bottom));
    padding-bottom: calc(0.25rem + var(--safe-bottom));
    background: rgba(255,255,255,0.95);
    backdrop-filter: blur(12px);
    -webkit-backdrop-filter: blur(12px);
    border-top: 1px solid rgba(0,0,0,0.08);
}

.btm-nav button {
    font-size: 0.65rem;
    gap: 0.2rem;
    transition: color 0.2s;
}

.btm-nav button.active,
.btm-nav .nav-item.active {
    color: #b91c1c;
}

/* ===== MOBILE PORTRAIT (phones) ===== */
@media (max-width: 767px) {

    /* Content padding cho bottom nav */
    #main-app > .flex > main {
        padding: 0.75rem;
        padding-bottom: 5.5rem;
    }

    /* Navbar: compact single line */
    .navbar {
        height: 3rem;
        min-height: 3rem;
        padding: 0.25rem 0.5rem;
        gap: 0.35rem;
    }
    .navbar h1 {
        font-size: 0.75rem;
        line-height: 1.15;
        white-space: normal;
        word-break: keep-all;
    }
    .navbar p {
        font-size: 0.45rem;
        display: none;
    }
    .navbar .bg-red-600 {
        padding: 0.25rem;
    }
    .navbar .bg-red-600 img {
        width: 22px;
        height: 22px;
    }

    /* Sidebar ẩn hoàn toàn */
    #main-app > .flex > div:first-child {
        display: none !important;
    }

    /* Modal: TRUE full-screen trên phone */
    .modal-bottom .modal-box,
    .modal .modal-box {
        border-radius: 0.75rem 0.75rem 0 0;
        max-height: calc(100vh - 3rem);
        max-height: calc(100dvh - 3rem);
        width: 100% !important;
        max-width: 100% !important;
        margin: 0 !important;
        padding: 1rem;
    }

    /* Modal title compact */
    .modal-box h3 {
        font-size: 1rem;
        margin-bottom: 0.25rem;
    }
    .modal-box > p {
        font-size: 0.75rem;
        margin-bottom: 0.75rem;
    }

    /* Tabs: horizontal scroll, compact */
    .tabs-boxed {
        flex-wrap: nowrap;
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
        scrollbar-width: none;
        -ms-overflow-style: none;
        padding: 0.2rem;
        gap: 0;
        margin-bottom: 0.75rem !important;
    }
    .tabs-boxed::-webkit-scrollbar {
        display: none;
    }
    .tabs-boxed .tab {
        flex-shrink: 0;
        font-size: 0.6rem;
        padding: 0.35rem 0.5rem;
        min-height: 1.75rem;
        white-space: nowrap;
        gap: 0.15rem;
    }
    .tabs-boxed .tab i {
        width: 11px;
        height: 11px;
    }

    /* Form: tighter spacing on mobile */
    .form-control {
        margin-bottom: 0;
    }
    .label {
        padding: 0.25rem 0;
    }
    .label-text {
        font-size: 0.8rem;
    }
    .label-text-alt {
        font-size: 0.65rem;
    }
    .input, .select, .textarea, .file-input {
        font-size: 16px; /* Prevents iOS zoom */
        height: 2.5rem;
        min-height: 2.5rem;
    }
    .textarea {
        height: auto;
        min-height: 3rem;
    }
    .space-y-4 > * + * {
        margin-top: 0.5rem;
    }
    .gap-4 {
        gap: 0.5rem;
    }

    /* Profile cards compact */
    .profile-card {
        margin-bottom: 0.5rem;
        border-radius: 0.75rem;
    }
    .profile-card .p-4 {
        padding: 0.75rem;
    }
    .profile-card .p-3 {
        padding: 0.5rem 0.75rem;
    }

    /* Tables horizontal scroll */
    .overflow-x-auto {
        -webkit-overflow-scrolling: touch;
    }
    .table {
        font-size: 0.7rem;
    }

    /* Dashboard stat cards */
    .grid.grid-cols-2 > div[class*="rounded-2xl"] {
        padding: 0.75rem;
        border-radius: 0.75rem;
    }
    .grid.grid-cols-2 > div .text-3xl {
        font-size: 1.5rem;
    }
    .grid.grid-cols-2 > div .text-xs {
        font-size: 0.6rem;
    }

    /* Dashboard section cards */
    .grid.grid-cols-1 > div[class*="rounded-2xl"] {
        padding: 0.75rem;
    }

    /* Page headings */
    .text-xl {
        font-size: 1rem;
    }

    /* Detail view compact */
    .detail-header {
        padding: 1rem;
    }
    .detail-body {
        padding: 0.75rem;
    }
    .detail-header .w-28 {
        width: 4.5rem;
        height: 4.5rem;
    }
    .detail-header .text-2xl {
        font-size: 1.1rem;
    }

    /* Toast */
    .toast {
        left: 0.5rem;
        right: 0.5rem;
        bottom: 5rem;
        top: auto !important;
    }
    .toast-item {
        width: 100%;
        font-size: 0.8rem;
    }

    /* View transitions smoother */
    [id^="view-"] {
        scroll-margin-top: 3.5rem;
    }

    /* Table compact on mobile */
    #profiles-table {
        font-size: 0.75rem;
    }
    #profiles-table th,
    #profiles-table td {
        padding: 0.4rem 0.35rem;
        white-space: nowrap;
    }
    #profiles-table .status-badge {
        font-size: 0.55rem;
        padding: 1px 5px;
    }
    
    /* Filter area compact */
    .filter-status-cb {
        width: 14px;
        height: 14px;
    }
}

/* ===== VERY SMALL PHONES (< 375px) ===== */
@media (max-width: 374px) {
    .navbar h1 {
        font-size: 0.7rem;
    }
    .grid.grid-cols-2 > div .text-3xl {
        font-size: 1.25rem;
    }
    .tabs-boxed .tab {
        font-size: 0.55rem;
        padding: 0.3rem 0.4rem;
    }
    .btm-nav {
        height: 3.8rem;
    }
    .btm-nav button {
        font-size: 0.55rem;
    }
}

/* ===== ADMIN-ONLY ELEMENTS ===== */
.admin-only { display: none !important; }
.admin-only.visible { display: block !important; }
.admin-only-inline { display: none !important; }

/* ===== USER-ONLY ELEMENTS ===== */
.user-only { display: none !important; }
.user-only.visible { display: block !important; }
.admin-only-inline.visible { display: inline-flex !important; }

/* btm-nav buttons cần flex, không phải block */
.btm-nav .admin-only.visible { display: flex !important; }
.btm-nav .user-only.visible { display: flex !important; }
/* sidebar li cần list-item */
li.admin-only.visible { display: list-item !important; }

/* Khi admin-only có class hidden (bị switchView ẩn), hidden phải thắng */
.admin-only.visible.hidden { display: none !important; }
.admin-only-inline.visible.hidden { display: none !important; }

/* ===== EMPTY STATE ===== */
.empty-state {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: 3rem 1.5rem;
    color: #94a3b8;
    text-align: center;
}

@media (min-width: 768px) {
    .empty-state { padding: 4rem 2rem; }
}

/* ===== SCROLLBAR ===== */
::-webkit-scrollbar { width: 4px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: #cbd5e1; border-radius: 2px; }
::-webkit-scrollbar-thumb:hover { background: #94a3b8; }

/* ===== COLLAPSIBLE SECTIONS ===== */
.collapse-chevron {
    transition: transform 0.25s ease;
}

/* ===== DETAIL VIEW MOBILE FIX ===== */
#view-detail,
#detail-content {
    overflow-x: hidden;
    max-width: 100%;
    width: 100%;
    min-width: 0;
}

.detail-header {
    overflow: hidden;
    word-break: break-word;
}

.detail-body {
    overflow-wrap: break-word;
    word-break: break-word;
    max-width: 100%;
}

@media (max-width: 767px) {
    .detail-header {
        padding: 1rem !important;
    }
    .detail-header .btn {
        font-size: 0.7rem;
        padding: 0.3rem 0.5rem;
        min-height: unset;
        height: auto;
    }
    .detail-header h2 {
        font-size: 1.2rem;
    }
    .status-badge {
        max-width: 100%;
        overflow: hidden;
        text-overflow: ellipsis;
    }
}

/* ===== USER MANAGEMENT: Mobile card / Desktop table ===== */
.user-row-mobile { display: none; }
.user-row-desktop { display: table-row; }

@media (max-width: 767px) {
    .user-row-desktop { display: none !important; }
    .user-row-mobile { display: table-row !important; }

    /* Hide desktop table header on mobile */
    #view-users thead { display: none; }
}

.user-card {
    background: white;
    border: 1px solid #e2e8f0;
    border-radius: 0.75rem;
    padding: 0.75rem;
    margin: 0.35rem 0.5rem;
}
.user-card--self {
    background: #eff6ff;
    border-color: #bfdbfe;
}

/* ===== VERY SMALL NAVBAR ===== */
@media (max-width: 374px) {
    .navbar h1 {
        font-size: 0.65rem;
    }
}

/* ===== TERRITORY MAP (Light Theme) ===== */
.map-container {
    background: #ffffff;
    border-radius: 1rem;
    color: #334155;
}
.map-container .map-header {
    background: rgba(248, 250, 252, 0.9);
    backdrop-filter: blur(12px);
    border-bottom: 1px solid #e2e8f0;
}
.map-sidebar {
    background: #ffffff;
    border-right: 1px solid #e2e8f0;
}
.map-sidebar::-webkit-scrollbar { width: 4px; }
.map-sidebar::-webkit-scrollbar-track { background: transparent; }
.map-sidebar::-webkit-scrollbar-thumb { background: #cbd5e1; border-radius: 10px; }
.map-sidebar::-webkit-scrollbar-thumb:hover { background: #94a3b8; }

.map-cell {
    border-radius: 6px;
    transition: all 0.2s ease;
    cursor: pointer;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    min-height: 44px;
    position: relative;
}
.map-cell:hover {
    transform: scale(1.15);
    z-index: 10;
    box-shadow: 0 4px 20px rgba(0,0,0,0.5);
}
.map-cell.selected {
    outline: 2px solid #3b82f6;
    outline-offset: 2px;
    transform: scale(1.1);
}

.map-zone-label {
    position: absolute;
    left: 8px;
    font-size: 10px;
    font-weight: 800;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    color: #475569;
}

.map-landmark {
    display: flex;
    align-items: center;
    gap: 6px;
    font-size: 10px;
    padding: 4px 8px;
    border-radius: 6px;
    background: #f1f5f9;
    border: 1px solid #e2e8f0;
    white-space: nowrap;
    color: #475569;
    font-weight: 500;
}

@keyframes map-pulse {
    0%, 100% { box-shadow: 0 0 0 0 rgba(239,68,68,0.4); }
    50% { box-shadow: 0 0 0 8px rgba(239,68,68,0); }
}
.hotspot-pulse {
    animation: map-pulse 2s ease-in-out infinite;
}

.map-detail-panel {
    background: #ffffff;
    border-left: 1px solid #e2e8f0;
    min-height: 420px;
}

.zone-item {
    padding: 10px 12px;
    border-radius: 10px;
    border: 1px solid #f1f5f9;
    cursor: pointer;
    transition: all 0.15s ease;
}
.zone-item:hover { background: #f8fafc; border-color: #e2e8f0; }
.zone-item.active {
    background: rgba(59, 130, 246, 0.05);
    border-color: rgba(59, 130, 246, 0.3);
}

/* ===== END ===== */
