/* ==========================================================================
   app.css — CSS หลักของแอป (โครงหน้า: sidebar / topbar / content / footer)
   --------------------------------------------------------------------------
   โครงสร้างหน้า (app shell):

     .app-shell
     ├── .app-sidebar        เมนูซ้าย
     ├── .sidebar-overlay    เง่ามือถือ
     └── .app-main
         ├── .app-topbar     แถบบน
         ├── .app-content    เนื้อหา (ทุกหน้าใส่เนื้อหาในส่วนนี้)
         └── .app-footer

   CSS เฉพาะหน้าอื่น ๆ แยกไฟล์ (เช่น items.css) แล้วประกาศผ่าน $pageCss
   ========================================================================== */

/* ---------- Design tokens (ปรับธีมที่นี่ที่เดียว) ---------- */
:root {
    --app-primary: #4f46e5;
    --app-primary-dark: #4338ca;
    --app-success: #16a34a;
    --app-danger: #dc2626;
    --app-warning: #d97706;
    --app-info: #0891b2;

    --app-bg: #f1f5f9;
    --app-surface: #ffffff;
    --app-border: #e2e8f0;
    --app-text: #1e293b;
    --app-text-muted: #64748b;

    --app-sidebar-width: 260px;
    --app-sidebar-collapsed: 76px;
    --app-topbar-height: 64px;

    --app-radius: 12px;
    --app-font: "Sarabun", system-ui, -apple-system, "Segoe UI", sans-serif;
}

/* ---------- Reset เบื้องต้น ---------- */
*,
*::before,
*::after {
    box-sizing: border-box;
}

html,
body {
    margin: 0;
    padding: 0;
}

body.app-body {
    font-family: var(--app-font);
    background: var(--app-bg);
    color: var(--app-text);
    font-size: 15px;
    line-height: 1.6;
}

a {
    text-decoration: none;
}

/* ==========================================================================
   App Shell
   ========================================================================== */

.app-shell {
    display: flex;
    min-height: 100vh;
}

/* --------------------------------------------------------------------------
   Sidebar
   -------------------------------------------------------------------------- */

.app-sidebar {
    position: fixed;
    inset-block: 0;
    inset-inline-start: 0;
    z-index: 1040;

    display: flex;
    flex-direction: column;

    width: var(--app-sidebar-width);

    background: linear-gradient(180deg, #1e293b 0%, #0f172a 100%);
    color: #cbd5e1;

    transition: transform 0.25s ease, width 0.25s ease;
}

.sidebar-header {
    display: flex;
    align-items: center;
    justify-content: space-between;

    padding: 16px;
    border-bottom: 1px solid rgba(255, 255, 255, 0.08);
}

.sidebar-brand {
    display: flex;
    align-items: center;
    gap: 10px;
    color: #fff;
}

.sidebar-brand-icon {
    display: grid;
    place-items: center;

    width: 38px;
    height: 38px;
    border-radius: 10px;

    background: var(--app-primary);
    color: #fff;
    font-size: 18px;
}

.sidebar-brand-text {
    display: flex;
    flex-direction: column;
    line-height: 1.25;
}

.sidebar-brand-text strong {
    font-size: 15px;
    color: #fff;
}

.sidebar-brand-text small {
    font-size: 11px;
    color: #94a3b8;
}

.sidebar-close {
    background: none;
    border: 0;
    color: #94a3b8;
    font-size: 18px;
    cursor: pointer;
}

.sidebar-content {
    flex: 1;
    overflow-y: auto;
    padding: 12px;
}

.sidebar-nav {
    display: flex;
    flex-direction: column;
    gap: 2px;
}

.sidebar-section-title {
    padding: 14px 12px 6px;
    font-size: 11px;
    font-weight: 600;
    letter-spacing: 0.08em;
    text-transform: uppercase;
    color: #64748b;
}

.sidebar-link {
    display: flex;
    align-items: center;
    gap: 10px;

    padding: 10px 12px;
    border-radius: 8px;

    color: #cbd5e1;
    font-size: 14px;
    transition: background 0.15s ease, color 0.15s ease;
}

.sidebar-link i {
    font-size: 16px;
    flex-shrink: 0;
}

.sidebar-link:hover {
    background: rgba(255, 255, 255, 0.06);
    color: #fff;
}

.sidebar-link.active {
    background: var(--app-primary);
    color: #fff;
}

.sidebar-footer {
    padding: 14px 16px;
    border-top: 1px solid rgba(255, 255, 255, 0.08);
}

.sidebar-overlay {
    position: fixed;
    inset: 0;
    z-index: 1035;

    display: none;

    background: rgba(15, 23, 42, 0.5);
}

/* --------------------------------------------------------------------------
   Main / Topbar / Content / Footer
   -------------------------------------------------------------------------- */

.app-main {
    display: flex;
    flex-direction: column;
    flex: 1;
    min-width: 0;
    margin-inline-start: var(--app-sidebar-width);
    transition: margin-inline-start 0.25s ease;
}

.app-topbar {
    position: sticky;
    top: 0;
    z-index: 1020;

    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;

    min-height: var(--app-topbar-height);
    padding: 10px 20px;

    background: var(--app-surface);
    border-bottom: 1px solid var(--app-border);
}

.topbar-left {
    display: flex;
    align-items: center;
    gap: 12px;
    min-width: 0;
}

.topbar-page-info h1 {
    margin: 0;
    font-size: 18px;
    font-weight: 700;
    color: var(--app-text);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.topbar-page-info p {
    margin: 0;
    font-size: 12.5px;
    color: var(--app-text-muted);
}

.topbar-right {
    display: flex;
    align-items: center;
    gap: 8px;
}

.topbar-icon-button {
    display: grid;
    place-items: center;

    width: 38px;
    height: 38px;
    border-radius: 8px;

    background: none;
    border: 1px solid transparent;
    color: var(--app-text-muted);
    font-size: 17px;
    cursor: pointer;
}

.topbar-icon-button:hover {
    background: var(--app-bg);
    color: var(--app-text);
}

.topbar-user {
    display: flex;
    align-items: center;
    gap: 10px;

    padding: 4px 10px 4px 4px;
    border-radius: 999px;

    background: none;
    border: 0;
    cursor: pointer;
}

.topbar-user:hover {
    background: var(--app-bg);
}

.topbar-user-avatar {
    display: grid;
    place-items: center;

    width: 34px;
    height: 34px;
    border-radius: 50%;

    background: var(--app-primary);
    color: #fff;
    font-weight: 600;
}

.topbar-user-info {
    flex-direction: column;
    align-items: flex-start;
    line-height: 1.2;
    text-align: start;
}

.topbar-user-info strong {
    font-size: 13.5px;
    color: var(--app-text);
}

.topbar-user-info small {
    font-size: 11.5px;
    color: var(--app-text-muted);
}

.app-content {
    flex: 1;
    padding: 20px;
}

.app-footer {
    display: flex;
    align-items: center;
    justify-content: space-between;

    padding: 12px 20px;

    background: var(--app-surface);
    border-top: 1px solid var(--app-border);
    color: var(--app-text-muted);
    font-size: 12.5px;
}

/* --------------------------------------------------------------------------
   Desktop: ยุบ sidebar
   -------------------------------------------------------------------------- */

@media (min-width: 992px) {
    body.sidebar-collapsed .app-sidebar {
        width: var(--app-sidebar-collapsed);
    }

    body.sidebar-collapsed .sidebar-brand-text,
    body.sidebar-collapsed .sidebar-link span,
    body.sidebar-collapsed .sidebar-section-title,
    body.sidebar-collapsed .sidebar-footer {
        display: none;
    }

    body.sidebar-collapsed .sidebar-link {
        justify-content: center;
    }

    body.sidebar-collapsed .app-main {
        margin-inline-start: var(--app-sidebar-collapsed);
    }
}

/* Mobile: sidebar ซ่อน กด toggle ให้โผล่ */

@media (max-width: 991.98px) {
    .app-sidebar {
        transform: translateX(-100%);
    }

    body.sidebar-open .app-sidebar {
        transform: translateX(0);
    }

    body.sidebar-open .sidebar-overlay {
        display: block;
    }

    .app-main {
        margin-inline-start: 0;
    }
}

/* ==========================================================================
   องค์ประกอบที่ใช้ร่วมกันหลายหน้า
   ========================================================================== */

/* การ์ดมาตรฐาน */
.app-card {
    border: 1px solid var(--app-border);
    border-radius: var(--app-radius);
    background: var(--app-surface);
    box-shadow: 0 1px 2px rgba(15, 23, 42, 0.04);
}

.app-card > .card-header {
    background: var(--app-surface);
    border-bottom: 1px solid var(--app-border);
    border-radius: var(--app-radius) var(--app-radius) 0 0 !important;
    font-weight: 600;
}

/* Flash message */
.app-flash {
    border-radius: var(--app-radius);
    margin-bottom: 16px;
}

/* Badge สถานะ */
.status-badge {
    display: inline-flex;
    align-items: center;
    gap: 6px;

    padding: 4px 10px;
    border-radius: 999px;

    font-size: 12px;
    font-weight: 600;
}

.status-badge::before {
    content: "";
    width: 6px;
    height: 6px;
    border-radius: 50%;
    background: currentColor;
}

.status-badge.is-active {
    background: #dcfce7;
    color: #15803d;
}

.status-badge.is-inactive {
    background: #f1f5f9;
    color: #64748b;
}

/* สถานะเอกสารลา — ไม่พึ่ง bg-*-subtle ซึ่งยังไม่มีใน Bootstrap 5.2 */
.leave-status-badge {
    display: inline-flex;
    align-items: center;
    gap: 5px;
    padding: 5px 10px;
    border: 1px solid transparent;
    border-radius: 999px;
    font-size: 12px;
    font-weight: 600;
    line-height: 1.2;
    white-space: nowrap;
}

.leave-status-badge.is-pending {
    border-color: #f5d66f;
    background: #fff7d6;
    color: #946200;
}

.leave-status-badge.is-step-one {
    border-color: #7dd3fc;
    background: #e0f2fe;
    color: #0369a1;
}

.leave-status-badge.is-approved {
    border-color: #86efac;
    background: #dcfce7;
    color: #15803d;
}

.leave-status-badge.is-rejected {
    border-color: #fca5a5;
    background: #fee2e2;
    color: #b91c1c;
}

.leave-status-badge.is-cancelled,
.leave-status-badge.is-unknown {
    border-color: #cbd5e1;
    background: #f1f5f9;
    color: #64748b;
}

/* ตาราง */
.app-card table thead th {
    background: #f8fafc;
    color: var(--app-text-muted);
    font-size: 12.5px;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.04em;
    white-space: nowrap;
}

/* ปุ่มหลักของแอป (override bootstrap เล็กน้อย) */
.btn-primary {
    --bs-btn-bg: var(--app-primary);
    --bs-btn-border-color: var(--app-primary);
    --bs-btn-hover-bg: var(--app-primary-dark);
    --bs-btn-hover-border-color: var(--app-primary-dark);
    --bs-btn-active-bg: var(--app-primary-dark);
    --bs-btn-active-border-color: var(--app-primary-dark);
    font-weight: 600;
}

/* ฟอร์ม */
.form-control:focus,
.form-select:focus {
    border-color: var(--app-primary);
    box-shadow: 0 0 0 0.2rem rgba(79, 70, 229, 0.15);
}

/* จุดหมุนครอบจอ */
.app-loading-overlay {
    position: fixed;
    inset: 0;
    z-index: 2000;
    display: grid;
    place-items: center;
    padding: 20px;
    visibility: hidden;
    opacity: 0;
    pointer-events: none;
    background: rgba(15, 23, 42, 0.38);
    backdrop-filter: blur(1.5px);
    transition: opacity .18s ease, visibility .18s ease;
}

.app-loading-overlay.is-blocking {
    visibility: visible;
    pointer-events: all;
}

.app-loading-overlay.is-visible {
    opacity: 1;
}

.app-loading-panel {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 13px;
    min-width: 178px;
    min-height: 116px;
    padding: 22px 28px;
    border: 1px solid rgba(226, 232, 240, .95);
    border-radius: 16px;
    background: rgba(255, 255, 255, .97);
    box-shadow: 0 18px 50px rgba(15, 23, 42, .22);
    transform: translateY(5px) scale(.98);
    transition: transform .18s ease;
}

.app-loading-overlay.is-visible .app-loading-panel {
    transform: translateY(0) scale(1);
}

.app-loading-animation {
    display: flex;
    align-items: center;
    gap: 10px;
    height: 34px;
}

.app-loading-ring {
    width: 30px;
    height: 30px;
    border: 4px solid #e2e8f0;
    border-top-color: var(--app-primary);
    border-right-color: var(--app-primary);
    border-radius: 50%;
    animation: app-loading-spin .72s linear infinite;
}

.app-loading-dots {
    display: flex;
    align-items: center;
    gap: 4px;
}

.app-loading-dots i {
    display: block;
    width: 7px;
    height: 7px;
    border-radius: 50%;
    background: var(--app-primary);
    animation: app-loading-bounce .9s ease-in-out infinite;
}

.app-loading-dots i:nth-child(2) { animation-delay: .12s; }
.app-loading-dots i:nth-child(3) { animation-delay: .24s; }

.app-loading-label {
    color: var(--app-text, #334155);
    font-size: 13.5px;
    font-weight: 600;
}

.app-is-loading,
.app-is-loading body {
    cursor: progress !important;
}

.is-app-busy {
    opacity: .58;
    pointer-events: none !important;
    transition: opacity .16s ease;
}

.is-app-button-loading {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    cursor: wait !important;
}

.app-button-spinner {
    display: inline-block;
    width: 1rem;
    height: 1rem;
    border: 2px solid currentColor;
    border-right-color: transparent;
    border-radius: 50%;
    animation: app-loading-spin .65s linear infinite;
}

@keyframes app-loading-spin {
    to { transform: rotate(360deg); }
}

@keyframes app-loading-bounce {
    0%, 60%, 100% { transform: translateY(0); opacity: .45; }
    30% { transform: translateY(-5px); opacity: 1; }
}

@media (prefers-reduced-motion: reduce) {
    .app-loading-overlay,
    .app-loading-panel {
        transition: none;
    }

    .app-loading-ring {
        animation-duration: 1.4s;
    }

    .app-loading-dots i {
        animation: none;
        opacity: .8;
    }
}
