/* ===== SW BackOffice — Design System ===== */
:root {
    --primary: #2563eb;
    --primary-dark: #1d4ed8;
    --primary-soft: #eff6ff;
    --success: #16a34a;
    --success-soft: #f0fdf4;
    --warning: #d97706;
    --warning-soft: #fffbeb;
    --info: #0891b2;
    --info-soft: #ecfeff;
    --danger: #dc2626;
    --danger-soft: #fef2f2;
    --ink: #0f172a;
    --ink-soft: #475569;
    --muted: #94a3b8;
    --line: #e2e8f0;
    --bg: #f1f5f9;
    --card: #ffffff;
    --radius: 12px;
    --shadow: 0 1px 3px rgba(15, 23, 42, .06), 0 4px 16px rgba(15, 23, 42, .05);
    --sidebar-w: 240px;
}

* { margin: 0; padding: 0; box-sizing: border-box; }

body {
    font-family: 'Sarabun', 'Leelawadee UI', 'Segoe UI', Tahoma, sans-serif;
    background: var(--bg);
    color: var(--ink);
    font-size: 15px;
    line-height: 1.55;
}

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

/* ===== Layout ===== */
.app { display: flex; min-height: 100vh; }

.sidebar {
    width: var(--sidebar-w);
    background: #0f172a;
    color: #cbd5e1;
    display: flex;
    flex-direction: column;
    position: fixed;
    inset: 0 auto 0 0;
    z-index: 50;
}

.main {
    flex: 1;
    margin-left: var(--sidebar-w);
    padding: 28px 32px 48px;
    max-width: 1280px;
}

.brand {
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 20px 24px;
    border-bottom: 1px solid rgba(255, 255, 255, .08);
}

.brand-img { width: 100%; max-width: 170px; height: auto; }

.nav { flex: 1; padding: 14px 12px; display: flex; flex-direction: column; gap: 2px; }

.nav-item {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 11px 14px;
    border-radius: 10px;
    color: #cbd5e1;
    font-weight: 500;
    transition: background .15s, color .15s;
}

.nav-item:hover { background: rgba(255, 255, 255, .07); color: #fff; text-decoration: none; }
.nav-item.active { background: var(--primary); color: #fff; }
.nav-icon { font-size: 17px; width: 22px; text-align: center; }

.sidebar-footer { padding: 16px 20px; border-top: 1px solid rgba(255, 255, 255, .08); font-size: 13px; }

.lang-switch { display: flex; gap: 6px; margin-bottom: 12px; }
.lang-switch a {
    flex: 1;
    text-align: center;
    padding: 5px 10px;
    border-radius: 8px;
    border: 1px solid rgba(255, 255, 255, .15);
    color: #94a3b8;
    font-size: 12.5px;
    font-weight: 600;
}
.lang-switch a:hover { color: #fff; text-decoration: none; }
.lang-switch a.active { background: var(--primary); border-color: var(--primary); color: #fff; }

.login-lang { display: flex; justify-content: center; gap: 14px; margin-top: 16px; font-size: 13.5px; }
.login-lang a.active { font-weight: 700; text-decoration: underline; }
.user-chip { color: #e2e8f0; }
.user-role { color: #64748b; font-size: 12px; margin: 1px 0 6px 26px; }
.logout-link { color: #94a3b8; font-size: 13px; }
.logout-link:hover { color: #fff; }

/* ===== Page header ===== */
.page-head {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 16px;
    margin-bottom: 22px;
    flex-wrap: wrap;
}

.page-title { font-size: 24px; font-weight: 700; }
.page-sub { color: var(--ink-soft); font-size: 14px; margin-top: 2px; }

/* ===== Cards ===== */
.card {
    background: var(--card);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    padding: 22px 24px;
    margin-bottom: 20px;
}

.card-title { font-size: 16px; font-weight: 600; margin-bottom: 14px; }

.stat-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
    gap: 16px;
    margin-bottom: 22px;
}

.stat {
    background: var(--card);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    padding: 18px 20px;
    display: flex;
    gap: 14px;
    align-items: center;
}

.stat-icon {
    width: 48px; height: 48px;
    border-radius: 12px;
    display: grid;
    place-items: center;
    font-size: 22px;
    flex-shrink: 0;
}

.stat-icon.blue { background: var(--primary-soft); }
.stat-icon.green { background: var(--success-soft); }
.stat-icon.amber { background: var(--warning-soft); }
.stat-icon.cyan { background: var(--info-soft); }

.stat-label { color: var(--ink-soft); font-size: 13px; }
.stat-value { font-size: 21px; font-weight: 700; letter-spacing: -.01em; }
.stat-value small { font-size: 13px; font-weight: 500; color: var(--muted); }

/* ===== Buttons ===== */
.btn {
    display: inline-flex;
    align-items: center;
    gap: 8px;
    padding: 9px 18px;
    border-radius: 10px;
    border: 1px solid transparent;
    font: inherit;
    font-weight: 600;
    font-size: 14px;
    cursor: pointer;
    transition: background .15s, box-shadow .15s;
    text-decoration: none;
}

.btn:hover { text-decoration: none; }
.btn-primary { background: var(--primary); color: #fff; }
.btn-primary:hover { background: var(--primary-dark); }
.btn-success { background: var(--success); color: #fff; }
.btn-success:hover { background: #15803d; }
.btn-outline { background: #fff; border-color: var(--line); color: var(--ink); }
.btn-outline:hover { background: var(--bg); }
.btn-danger-outline { background: #fff; border-color: #fecaca; color: var(--danger); }
.btn-danger-outline:hover { background: var(--danger-soft); }
.btn-sm { padding: 6px 12px; font-size: 13px; border-radius: 8px; }
.btn:disabled { opacity: .5; cursor: not-allowed; }

/* ===== Tables ===== */
.table-wrap { overflow-x: auto; }

table.table { width: 100%; border-collapse: collapse; }

.table th {
    text-align: left;
    font-size: 13px;
    font-weight: 600;
    color: var(--ink-soft);
    padding: 10px 12px;
    border-bottom: 2px solid var(--line);
    white-space: nowrap;
}

.table td {
    padding: 12px;
    border-bottom: 1px solid var(--line);
    vertical-align: middle;
}

.table tbody tr:hover { background: #f8fafc; }
.table .num, .table th.num { text-align: right; font-variant-numeric: tabular-nums; }
.table .center { text-align: center; }
.table .empty { text-align: center; color: var(--muted); padding: 36px 12px; }

/* ===== Badges ===== */
.badge {
    display: inline-block;
    padding: 3px 12px;
    border-radius: 999px;
    font-size: 12.5px;
    font-weight: 600;
    white-space: nowrap;
}

.badge-success { background: var(--success-soft); color: var(--success); }
.badge-warning { background: var(--warning-soft); color: var(--warning); }
.badge-info { background: var(--info-soft); color: var(--info); }
.badge-danger { background: var(--danger-soft); color: var(--danger); }
.badge-muted { background: #f1f5f9; color: var(--muted); }

/* ===== Forms ===== */
.form-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(240px, 1fr)); gap: 16px; }

.field { display: flex; flex-direction: column; gap: 6px; }
.field label { font-size: 13.5px; font-weight: 600; color: var(--ink-soft); }
.field .req { color: var(--danger); }

input[type=text], input[type=number], input[type=date], input[type=email],
input[type=password], input[type=tel], select, textarea {
    font: inherit;
    padding: 9px 12px;
    border: 1px solid var(--line);
    border-radius: 10px;
    background: #fff;
    color: var(--ink);
    width: 100%;
    transition: border-color .15s, box-shadow .15s;
}

input:focus, select:focus, textarea:focus {
    outline: none;
    border-color: var(--primary);
    box-shadow: 0 0 0 3px rgba(37, 99, 235, .12);
}

textarea { resize: vertical; min-height: 74px; }

.form-actions { display: flex; gap: 10px; margin-top: 20px; }

.search-bar { display: flex; gap: 10px; flex-wrap: wrap; align-items: center; margin-bottom: 16px; }
.search-bar input[type=text] { width: 260px; }
.search-bar select { width: auto; }

/* ===== Alerts ===== */
.alert {
    padding: 12px 18px;
    border-radius: 10px;
    margin-bottom: 18px;
    font-weight: 500;
}

.alert-success { background: var(--success-soft); color: #166534; border: 1px solid #bbf7d0; }
.alert-error { background: var(--danger-soft); color: #991b1b; border: 1px solid #fecaca; }
.alert-info { background: var(--info-soft); color: #155e75; border: 1px solid #a5f3fc; }

/* ===== Invoice item editor ===== */
.items-table input { border-radius: 8px; padding: 7px 10px; }
.items-table td { padding: 6px 6px; }
.items-table th { padding: 8px 6px; }
.item-remove {
    background: none; border: none; cursor: pointer;
    color: var(--muted); font-size: 17px; padding: 4px 8px; border-radius: 8px;
}
.item-remove:hover { color: var(--danger); background: var(--danger-soft); }

.totals-box { margin-left: auto; width: 320px; max-width: 100%; margin-top: 14px; }
.totals-box .row { display: flex; justify-content: space-between; padding: 6px 0; }
.totals-box .row.grand { border-top: 2px solid var(--ink); font-size: 18px; font-weight: 700; padding-top: 10px; margin-top: 6px; }

/* ===== Document (print view) ===== */
.doc {
    background: #fff;
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    padding: 40px 48px;
    max-width: 860px;
}

.doc-head { display: flex; justify-content: space-between; gap: 24px; margin-bottom: 26px; }
.doc-logo { height: 84px; width: auto; margin-bottom: 10px; display: block; }
.doc-company h2 { font-size: 19px; margin-bottom: 4px; }
.doc-company p { font-size: 13.5px; color: var(--ink-soft); white-space: pre-line; }
.doc-type { text-align: right; }
.doc-type h1 { font-size: 26px; color: var(--primary); letter-spacing: .02em; }
.doc-type .doc-no { font-size: 15px; font-weight: 600; margin-top: 6px; }
.doc-meta { font-size: 13.5px; color: var(--ink-soft); margin-top: 4px; }

.doc-parties {
    display: grid; grid-template-columns: 1fr 1fr; gap: 20px;
    background: #f8fafc; border-radius: 10px; padding: 16px 20px; margin-bottom: 24px;
}
.doc-parties h4 { font-size: 12.5px; color: var(--muted); text-transform: uppercase; margin-bottom: 6px; }
.doc-parties p { font-size: 14px; white-space: pre-line; }

.doc table.doc-items { width: 100%; border-collapse: collapse; margin-bottom: 18px; }
.doc-items th {
    background: var(--ink); color: #fff; font-size: 13px; font-weight: 600;
    padding: 9px 12px; text-align: left;
}
.doc-items th.num, .doc-items td.num { text-align: right; }
.doc-items th.center, .doc-items td.center { text-align: center; }
.doc-items td { padding: 9px 12px; border-bottom: 1px solid var(--line); font-size: 14px; }

.doc-totals { margin-left: auto; width: 300px; font-size: 14.5px; }
.doc-totals .row { display: flex; justify-content: space-between; padding: 4px 0; }
.doc-totals .row.grand { border-top: 2px solid var(--ink); font-weight: 700; font-size: 17px; padding-top: 8px; margin-top: 4px; }

.baht-text { margin-top: 14px; font-size: 13.5px; color: var(--ink-soft); }
.doc-note { margin-top: 18px; font-size: 13.5px; color: var(--ink-soft); }

.doc-sign { display: grid; grid-template-columns: 1fr 1fr; gap: 40px; margin-top: 56px; }
.sign-box { text-align: center; font-size: 13.5px; color: var(--ink-soft); }
.sign-line { border-bottom: 1px dotted var(--muted); margin-bottom: 8px; height: 44px; }

/* ===== Pagination ===== */
.list-summary { color: var(--ink-soft); font-size: 13.5px; margin: 12px 2px 0; }

.pagination {
    display: flex;
    gap: 6px;
    flex-wrap: wrap;
    margin-top: 14px;
    align-items: center;
}

.page-link {
    min-width: 36px;
    text-align: center;
    padding: 6px 10px;
    border: 1px solid var(--line);
    border-radius: 8px;
    background: #fff;
    color: var(--ink);
    font-size: 13.5px;
    font-weight: 500;
}

.page-link:hover { background: var(--primary-soft); border-color: var(--primary); color: var(--primary); text-decoration: none; }
.page-link.current { background: var(--primary); border-color: var(--primary); color: #fff; }
.page-link.disabled { color: var(--muted); pointer-events: none; background: var(--bg); }
.page-gap { color: var(--muted); padding: 0 2px; }

#ajax-list { transition: opacity .15s; }

/* ===== Bar chart (SVG) ===== */
.chart-bar { transition: opacity .15s; }
.chart-bar:hover { opacity: .75; }

/* ===== Login ===== */
.login-wrap { min-height: 100vh; display: grid; place-items: center; background: var(--bg); }
.login-card { width: 380px; max-width: calc(100vw - 32px); }
.login-logo { display: block; margin: 0 auto 6px; width: 190px; max-width: 100%; height: auto; }
.login-card h1 { text-align: center; font-size: 20px; margin-bottom: 4px; }
.login-card .page-sub { text-align: center; margin-bottom: 20px; }

/* ===== Progress bar (paid ratio) ===== */
.paybar { background: var(--line); border-radius: 999px; height: 7px; overflow: hidden; min-width: 90px; }
.paybar > div { height: 100%; border-radius: 999px; background: var(--success); }

/* ===== Mobile topbar + drawer menu ===== */
.topbar { display: none; }
.backdrop { display: none; }

@media (max-width: 900px) {
    .topbar {
        display: flex;
        align-items: center;
        gap: 12px;
        position: sticky;
        top: 0;
        z-index: 60;
        background: #0f172a;
        padding: 10px 14px;
        min-height: 54px;
    }

    .menu-btn {
        background: rgba(255, 255, 255, .08);
        border: none;
        color: #fff;
        font-size: 20px;
        line-height: 1;
        padding: 9px 13px;
        border-radius: 10px;
        cursor: pointer;
    }

    .menu-btn:active { background: rgba(255, 255, 255, .18); }
    .topbar-logo { height: 34px; width: auto; }

    /* sidebar กลายเป็นลิ้นชักเลื่อนจากซ้าย */
    .sidebar {
        width: min(280px, 82vw);
        transform: translateX(-100%);
        transition: transform .25s ease;
        z-index: 80;
        overflow-y: auto;
    }

    .sidebar.open {
        transform: translateX(0);
        box-shadow: 0 0 40px rgba(0, 0, 0, .45);
    }

    body.menu-open .backdrop {
        display: block;
        position: fixed;
        inset: 0;
        background: rgba(15, 23, 42, .5);
        z-index: 70;
    }

    body.menu-open { overflow: hidden; }

    .main { margin-left: 0; padding: 18px 16px 40px; }
    .doc { padding: 24px 20px; }
    .page-title { font-size: 20px; }
}

/* ===== ฟอร์มรายการใบแจ้งหนี้บนจอแคบ: แต่ละรายการเป็นบล็อกแนวตั้ง ===== */
@media (max-width: 700px) {
    .items-table thead { display: none; }

    .items-table tbody tr {
        display: grid;
        grid-template-columns: 1fr 1fr 1fr auto;
        gap: 8px 10px;
        align-items: end;
        padding: 14px 0;
        border-bottom: 1px solid var(--line);
    }

    .items-table tbody tr:hover { background: none; }

    .items-table td {
        display: block;
        padding: 0 !important;
        border: none !important;
        width: auto !important;
        text-align: left !important;
    }

    .items-table td[data-label]::before {
        content: attr(data-label);
        display: block;
        font-size: 12px;
        font-weight: 600;
        color: var(--ink-soft);
        margin-bottom: 4px;
    }

    /* ช่องรายละเอียดเต็มความกว้าง */
    .items-table td:first-child { grid-column: 1 / -1; }

    .items-table td.line-total {
        font-weight: 600;
        padding-bottom: 8px !important;
    }

    .item-remove { font-size: 19px; padding: 8px 10px; }
}

/* ===== Print ===== */
@media print {
    body { background: #fff; }
    .sidebar, .topbar, .backdrop, .no-print, .alert { display: none !important; }
    .main { margin: 0; padding: 0; max-width: none; }
    .doc { box-shadow: none; border-radius: 0; padding: 0; max-width: none; }
    .doc-items th { -webkit-print-color-adjust: exact; print-color-adjust: exact; }
    .doc-parties { -webkit-print-color-adjust: exact; print-color-adjust: exact; }
}
