:root {
    --color-bg: #f7f9fb;
    --color-surface: #ffffff;
    --color-surface-alt: #f2f4f6;
    --color-surface-hover: #eceef0;
    --color-border: #e2e8f0;
    --color-table-header: #f1f5f9;

    --color-text: #191c1e;
    --color-text-secondary: #505f76;
    --color-text-muted: #757682;

    --color-primary: #1e3a8a;
    --color-primary-dark: #00236f;
    --color-primary-hover: #16316f;
    --color-primary-soft: #e8edfb;
    --color-on-primary: #ffffff;

    --color-success: #10b981;
    --color-success-soft: #e3f9f1;
    --color-warning: #f59e0b;
    --color-warning-soft: #fef3e2;
    --color-danger: #e11d48;
    --color-danger-soft: #fde8ee;
    --color-info: #2563eb;
    --color-info-soft: #dbeafe;

    --radius-sm: 4px;
    --radius: 8px;
    --radius-lg: 12px;
    --radius-full: 9999px;

    --shadow-sm: 0 1px 2px rgba(16, 24, 40, 0.04), 0 1px 3px rgba(16, 24, 40, 0.06);
    --shadow-md: 0 4px 6px -1px rgba(16, 24, 40, 0.08), 0 2px 4px -2px rgba(16, 24, 40, 0.06);

    --font-sans: "Inter", "Sarabun", "Segoe UI", Tahoma, sans-serif;

    --sidebar-width: 264px;
    --topheader-height: 56px;
}

* { box-sizing: border-box; }

html, body { height: 100%; }

body {
    margin: 0;
    font-family: var(--font-sans);
    background: var(--color-bg);
    color: var(--color-text);
    font-size: 14px;
    line-height: 1.5;
    overflow-wrap: break-word;
    -webkit-font-smoothing: antialiased;
}

a { color: var(--color-primary); text-decoration: none; }
a:hover { text-decoration: underline; }

h1, h2, h3, h4 { color: var(--color-text); line-height: 1.3; }

.custom-scrollbar::-webkit-scrollbar { width: 8px; height: 8px; }
.custom-scrollbar::-webkit-scrollbar-track { background: transparent; }
.custom-scrollbar::-webkit-scrollbar-thumb { background: var(--color-border); border-radius: var(--radius-full); }
.custom-scrollbar::-webkit-scrollbar-thumb:hover { background: var(--color-text-muted); }

/* ----- Login page ----- */
.login-page {
    display: flex;
    align-items: center;
    justify-content: center;
    min-height: 100vh;
    background: radial-gradient(circle at top left, #16316f, var(--color-primary-dark) 60%);
    padding: 16px;
}
.login-box {
    background: var(--color-surface);
    padding: 40px;
    border-radius: var(--radius-lg);
    box-shadow: 0 20px 45px rgba(0, 15, 60, 0.3);
    width: 100%;
    max-width: 380px;
}
.login-brand-mark {
    width: 48px;
    height: 48px;
    border-radius: var(--radius);
    background: var(--color-primary);
    color: var(--color-on-primary);
    display: flex;
    align-items: center;
    justify-content: center;
    margin: 0 auto 16px;
}
.login-box h1 {
    font-size: 19px;
    font-weight: 700;
    margin: 0 0 28px;
    text-align: center;
    color: var(--color-primary-dark);
    line-height: 1.4;
}
@media (max-width: 400px) {
    .login-box { padding: 28px 20px; }
}

/* ----- App shell / layout ----- */
.app-shell {
    display: flex;
    min-height: 100vh;
}

.sidebar {
    display: flex;
    flex-direction: column;
    width: var(--sidebar-width);
    flex-shrink: 0;
    background: var(--color-surface-alt);
    border-right: 1px solid var(--color-border);
    padding: 20px 12px;
    position: sticky;
    top: 0;
    height: 100vh;
    overflow-y: auto;
}

.sidebar-brand {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 0 8px;
    margin-bottom: 18px;
}
.sidebar-brand-mark {
    width: 36px;
    height: 36px;
    border-radius: var(--radius);
    background: var(--color-primary);
    color: var(--color-on-primary);
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
}
.sidebar-brand-text a {
    color: var(--color-text);
    font-weight: 700;
    font-size: 15px;
    line-height: 1.3;
    display: block;
}
.sidebar-brand-text a:hover { text-decoration: none; color: var(--color-primary); }

.sidebar-cta { padding: 0 8px; margin-bottom: 16px; }

.sidebar-nav {
    list-style: none;
    margin: 0;
    padding: 0 8px;
    flex: 1;
    overflow-y: auto;
}
.sidebar-section-divider {
    height: 1px;
    background: var(--color-border);
    margin: 10px 4px;
    list-style: none;
}
.sidebar-section-divider:first-child { display: none; }

.sidebar-link {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 9px 10px;
    margin-bottom: 2px;
    border-radius: var(--radius);
    color: var(--color-text-secondary);
    font-size: 13.5px;
    font-weight: 500;
    transition: background-color .15s ease, color .15s ease;
}
.sidebar-link:hover {
    background: var(--color-surface-hover);
    color: var(--color-text);
    text-decoration: none;
}
.sidebar-link.active {
    background: var(--color-primary-soft);
    color: var(--color-primary-dark);
    font-weight: 600;
}
.sidebar-link-icon { width: 19px; height: 19px; flex-shrink: 0; }

.sidebar-footer {
    padding: 12px 8px 0;
    margin-top: 12px;
    border-top: 1px solid var(--color-border);
    display: flex;
    align-items: center;
    gap: 8px;
}
.sidebar-user { display: flex; align-items: center; gap: 10px; flex: 1; min-width: 0; color: inherit; text-decoration: none; }
.sidebar-user:hover { text-decoration: none; }
.sidebar-user-avatar {
    width: 34px;
    height: 34px;
    border-radius: var(--radius-full);
    background: var(--color-primary);
    color: var(--color-on-primary);
    display: flex;
    align-items: center;
    justify-content: center;
    font-weight: 700;
    font-size: 14px;
    flex-shrink: 0;
}
.sidebar-user-avatar-photo { object-fit: cover; }
.sidebar-user-info { display: flex; flex-direction: column; min-width: 0; }
.sidebar-user-name {
    font-size: 13px;
    font-weight: 600;
    color: var(--color-text);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}
.sidebar-user-role { font-size: 12px; color: var(--color-text-muted); }
.sidebar-logout {
    width: 32px;
    height: 32px;
    border-radius: var(--radius);
    display: flex;
    align-items: center;
    justify-content: center;
    color: var(--color-text-secondary);
    flex-shrink: 0;
}
.sidebar-logout:hover { background: var(--color-danger-soft); color: var(--color-danger); text-decoration: none; }

.sidebar-backdrop { display: none; }

.main-wrap {
    flex: 1;
    display: flex;
    flex-direction: column;
    min-width: 0;
}

.topheader {
    display: none;
    align-items: center;
    gap: 12px;
    height: var(--topheader-height);
    padding: 0 16px;
    background: var(--color-surface);
    border-bottom: 1px solid var(--color-border);
    position: sticky;
    top: 0;
    z-index: 20;
}
.nav-toggle {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 36px;
    height: 36px;
    padding: 0;
    background: transparent;
    border: 1px solid var(--color-border);
    border-radius: var(--radius);
    color: var(--color-text);
    cursor: pointer;
}
.topheader-brand { font-weight: 700; font-size: 15px; color: var(--color-primary-dark); }

.page-content {
    flex: 1;
    padding: 24px;
    max-width: 1400px;
    width: 100%;
    margin: 0 auto;
}

@media (max-width: 900px) {
    .sidebar {
        position: fixed;
        left: 0;
        top: 0;
        z-index: 40;
        transform: translateX(-100%);
        transition: transform .2s ease;
        box-shadow: var(--shadow-md);
    }
    .sidebar.is-open { transform: translateX(0); }
    .sidebar-backdrop {
        display: block;
        position: fixed;
        inset: 0;
        background: rgba(15, 23, 42, 0.4);
        z-index: 30;
        opacity: 0;
        pointer-events: none;
        transition: opacity .2s ease;
    }
    .sidebar-backdrop.is-open { opacity: 1; pointer-events: auto; }
    .topheader { display: flex; }
    .page-content { padding: 16px; }
}

/* ----- Page header ----- */
.page-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    flex-wrap: wrap;
    gap: 12px;
    margin-bottom: 20px;
}
.page-header h1 {
    margin: 0;
    font-size: 26px;
    font-weight: 700;
    letter-spacing: -0.01em;
}
.page-header h3 { margin: 0; font-size: 16px; font-weight: 600; }

/* ----- Cards & grid ----- */
.card {
    background: var(--color-surface);
    border: 1px solid var(--color-border);
    border-radius: var(--radius-lg);
    padding: 20px;
    margin-bottom: 20px;
    box-shadow: var(--shadow-sm);
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
}
.card h2, .card h3 { margin-top: 0; }

.grid { display: grid; gap: 16px; }
.grid-cols-5 { grid-template-columns: repeat(5, 1fr); }
.grid-cols-4 { grid-template-columns: repeat(4, 1fr); }
.grid-cols-3 { grid-template-columns: repeat(3, 1fr); }
.grid-cols-2 { grid-template-columns: repeat(2, 1fr); }
@media screen and (max-width: 960px) {
    .grid-cols-5, .grid-cols-4, .grid-cols-3 { grid-template-columns: repeat(2, 1fr); }
}
@media screen and (max-width: 700px) {
    .grid-cols-5, .grid-cols-4, .grid-cols-3, .grid-cols-2 { grid-template-columns: 1fr; }
}

.stat-card {
    display: flex;
    flex-direction: column;
    background: var(--color-surface);
    border: 1px solid var(--color-border);
    border-radius: var(--radius-lg);
    padding: 16px 18px;
    transition: border-color .15s ease;
}
.stat-card:hover { border-color: var(--color-primary); }
.stat-card .stat-value { order: 2; font-size: 28px; font-weight: 700; color: var(--color-text); line-height: 1.2; }
.stat-card .stat-label { order: 1; color: var(--color-text-secondary); font-size: 13px; margin-bottom: 6px; }
.stat-card.warning { border-color: rgba(245, 158, 11, 0.35); }
.stat-card.warning:hover { border-color: var(--color-warning); }
.stat-card.warning .stat-value { color: var(--color-warning); }
.stat-card.danger { border-color: rgba(225, 29, 72, 0.35); }
.stat-card.danger:hover { border-color: var(--color-danger); }
.stat-card.danger .stat-value { color: var(--color-danger); }

/* ----- Forms ----- */
.form-group { margin-bottom: 16px; }
.form-group label { display: block; margin-bottom: 6px; font-weight: 600; font-size: 13px; color: var(--color-text); }
.form-row { display: flex; gap: 16px; flex-wrap: wrap; }
.form-row .form-group { flex: 1; min-width: 200px; }

input[type=text], input[type=email], input[type=password], input[type=number],
input[type=date], input[type=file], select, textarea {
    width: 100%;
    padding: 9px 12px;
    border: 1px solid var(--color-border);
    border-radius: var(--radius);
    font-size: 14px;
    font-family: inherit;
    color: var(--color-text);
    background: var(--color-surface);
}
input[type=file] { padding: 6px 8px; border-style: dashed; }
textarea { resize: vertical; min-height: 80px; }
input:focus, select:focus, textarea:focus {
    outline: none;
    border-color: var(--color-primary);
    box-shadow: 0 0 0 3px rgba(30, 58, 138, 0.15);
}

/* ----- Buttons ----- */
.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 6px;
    padding: 9px 16px;
    border-radius: var(--radius);
    border: 1px solid transparent;
    font-family: inherit;
    font-size: 13.5px;
    font-weight: 600;
    cursor: pointer;
    text-decoration: none !important;
    transition: background-color .15s ease, border-color .15s ease, color .15s ease, opacity .15s ease;
}
.btn:active { transform: scale(0.98); }
.btn-primary { background: var(--color-primary); color: var(--color-on-primary); }
.btn-primary:hover { background: var(--color-primary-hover); }
.btn-secondary { background: var(--color-surface); color: var(--color-text-secondary); border-color: var(--color-border); }
.btn-secondary:hover { background: var(--color-surface-alt); border-color: var(--color-text-secondary); color: var(--color-text); }
.btn-success { background: var(--color-success); color: #fff; }
.btn-success:hover { opacity: 0.9; }
.btn-danger { background: var(--color-danger); color: #fff; }
.btn-danger:hover { opacity: 0.9; }
.btn-sm { padding: 5px 10px; font-size: 12.5px; }
.btn-block { width: 100%; }
.btn + .btn { margin-left: 6px; }

/* ----- Table ----- */
table { width: 100%; border-collapse: collapse; }
th, td { padding: 10px 12px; border-bottom: 1px solid var(--color-border); text-align: left; font-size: 13.5px; }
th {
    color: var(--color-text-secondary);
    font-weight: 600;
    font-size: 12.5px;
    background: var(--color-table-header);
    white-space: nowrap;
}
th a { color: inherit; text-decoration: none; }
th a:hover { text-decoration: underline; }
tbody tr:hover td, tr:hover td { background: var(--color-surface-alt); }
.text-right { text-align: right; }
.text-center { text-align: center; }
.text-muted { color: var(--color-text-muted); }

/* ----- Badges & alerts ----- */
.badge {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    padding: 3px 10px;
    border-radius: var(--radius-full);
    font-size: 12px;
    font-weight: 600;
    line-height: 1.6;
}
.badge::before {
    content: "";
    width: 6px;
    height: 6px;
    border-radius: var(--radius-full);
    background: currentColor;
    flex-shrink: 0;
}
.badge-warning { background: var(--color-warning-soft); color: #b45309; }
.badge-info { background: var(--color-info-soft); color: #1e40af; }
.badge-danger { background: var(--color-danger-soft); color: var(--color-danger); }
.badge-success { background: var(--color-success-soft); color: #0d9166; }
.badge-muted { background: var(--color-border); color: var(--color-text-secondary); }

.alert { padding: 12px 16px; border-radius: var(--radius); margin-bottom: 16px; font-size: 14px; border: 1px solid transparent; }
.alert-success { background: var(--color-success-soft); color: #0d9166; border-color: rgba(16, 185, 129, 0.25); }
.alert-error, .alert-danger { background: var(--color-danger-soft); color: var(--color-danger); border-color: rgba(225, 29, 72, 0.25); }
.alert-info { background: var(--color-info-soft); color: #1e40af; border-color: rgba(37, 99, 235, 0.25); }
.alert-warning { background: var(--color-warning-soft); color: #b45309; border-color: rgba(245, 158, 11, 0.3); }

.asset-thumb {
    width: 40px;
    height: 40px;
    border-radius: var(--radius);
    object-fit: cover;
    border: 1px solid var(--color-border);
    background: var(--color-surface-alt);
    display: flex;
    align-items: center;
    justify-content: center;
    color: var(--color-text-muted);
    flex-shrink: 0;
}

.low-stock { color: var(--color-danger); font-weight: 600; }
.pm-overdue { color: var(--color-danger); font-weight: 600; }
.pm-due-soon { color: #b45309; font-weight: 600; }

.filter-bar { display: flex; gap: 10px; margin-bottom: 16px; flex-wrap: wrap; align-items: center; }
.filter-bar label { font-size: 13px; color: var(--color-text-secondary); font-weight: 500; display: flex; align-items: center; gap: 6px; }
.filter-bar input, .filter-bar select { width: auto; }

.item-row { display: flex; gap: 10px; align-items: flex-end; margin-bottom: 10px; }
.item-row .form-group { margin-bottom: 0; }
.item-row select { min-width: 220px; }
.item-row input[type=number] { width: 100px; }
@media (max-width: 700px) {
    .item-row { flex-wrap: wrap; }
    .item-row .form-group { flex: 1 1 100%; min-width: 0; }
    .item-row select,
    .item-row input[type=number] { min-width: 0; width: 100%; }
    .item-row .remove-row { margin-left: auto; }
}

/* Lightbox: คลิกรูปภาพ (.zoomable) เพื่อขยายดูแบบเต็มจอ */
.zoomable { cursor: zoom-in; }
.lightbox-overlay {
    display: none;
    position: fixed;
    inset: 0;
    background: rgba(15, 23, 42, 0.85);
    z-index: 1000;
    align-items: center;
    justify-content: center;
    padding: 24px;
}
.lightbox-overlay.is-open { display: flex; }
.lightbox-overlay img {
    max-width: 100%;
    max-height: 100%;
    border-radius: var(--radius);
    box-shadow: 0 20px 60px rgba(0, 0, 0, 0.4);
    cursor: zoom-out;
}
.lightbox-close {
    position: absolute;
    top: 20px;
    right: 24px;
    width: 40px;
    height: 40px;
    border-radius: 50%;
    border: none;
    background: rgba(255, 255, 255, 0.15);
    color: #fff;
    font-size: 22px;
    line-height: 1;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
}
.lightbox-close:hover { background: rgba(255, 255, 255, 0.3); }

/* พิมพ์ / บันทึกเป็น PDF: ซ่อนแถบเมนู ปุ่ม export ฯลฯ (.no-print) ให้เหลือแต่เนื้อหารายงาน
   ขนาดกระดาษ A4 ค่าเริ่มต้น — หน้าที่ต้องการแนวนอน (เช่น reports/index.php ที่ตารางกว้างหลายคอลัมน์)
   ตั้ง $printOrientation = 'landscape' ก่อน require header.php เพื่อ override ค่านี้ (ดู includes/header.php) */
@media print {
    @page { size: A4; margin: 12mm; }
    .sidebar, .sidebar-backdrop, .topheader, .no-print { display: none !important; }
    .app-shell, .main-wrap, .page-content { display: block; margin: 0; padding: 0; }
    /* ไม่ตั้ง break-inside:avoid ที่การ์ด เพราะการ์ดที่สูงกว่าที่เหลือของหน้าแรกจะถูกดันไปทั้งใบที่หน้า 2
       เหลือหน้าแรกว่างเปล่ามีแต่หัวข้อ — ให้แถวข้อมูล (tbody tr ด้านล่าง) กันการตัดกลางแถวแทน */
    .card { box-shadow: none; border: none; overflow-x: visible; }
    .print-only { display: block !important; }
    /* ตารางกว้างหลายคอลัมน์บนจอ scroll แนวนอนได้ (.card overflow-x:auto) แต่หน้าพิมพ์ scroll ไม่ได้
       เดิมหัวตาราง (th) ตั้ง white-space:nowrap ไว้เสมอ ทำให้ตารางกว้างเกินหน้ากระดาษจนถูกตัดขอบขวาแทนที่จะย่อพอดี */
    table { font-size: 10.5px; }
    th, td { white-space: normal; word-break: break-word; padding: 4px 6px; }
    /* ตารางยาวข้ามหน้า: ให้แถวหัวตาราง (thead) ซ้ำทุกหน้า และห้ามตัดกลางแถวข้อมูล */
    thead { display: table-header-group; }
    tbody tr { break-inside: avoid; }
    .grid-cols-5, .grid-cols-4, .grid-cols-3, .grid-cols-2 { grid-template-columns: repeat(auto-fit, minmax(110px, 1fr)); }
    /* พิมพ์เฉพาะการ์ดรายงานที่เลือก (ปุ่ม "พิมพ์ PDF" รายส่วน) ซ่อนการ์ดอื่นทั้งหมด */
    body.print-section-mode .card:not(.print-target) { display: none !important; }
}
