/* =====================================================================
   Agency Dashboard — Design System v2
   A single source of truth for the whole app. Pages only use these
   shared classes, so restyling here restyles every screen at once.
   ===================================================================== */

:root {
    /* Brand */
    --brand:        #4f46e5;   /* indigo-600 */
    --brand-strong: #4338ca;   /* indigo-700 */
    --brand-soft:   #eef2ff;   /* indigo-50  */

    /* Sidebar */
    --sidebar-w:      248px;
    --sidebar-bg:     #0f172a;  /* slate-900 */
    --sidebar-bg2:    #1e293b;  /* slate-800 */
    --sidebar-text:   #e2e8f0;
    --sidebar-muted:  #94a3b8;

    /* Surfaces & text */
    --content-bg: #f1f5f9;   /* slate-100 */
    --surface:    #ffffff;
    --border:     #e2e8f0;   /* slate-200 */
    --border-soft:#eef2f6;
    --ink:        #0f172a;   /* headings */
    --ink-body:   #334155;   /* body      */
    --ink-muted:  #94a3b8;   /* muted     */

    /* Semantic */
    --success: #059669; --success-soft: #ecfdf5;
    --warning: #d97706; --warning-soft: #fffbeb;
    --danger:  #e11d48; --danger-soft:  #fff1f2;
    --info:    #0284c7; --info-soft:    #f0f9ff;

    /* Shape & depth */
    --radius:    14px;
    --radius-sm: 10px;
    --shadow-sm: 0 1px 2px rgba(15,23,42,.06), 0 1px 3px rgba(15,23,42,.04);
    --shadow-md: 0 4px 6px -1px rgba(15,23,42,.07), 0 2px 4px -2px rgba(15,23,42,.05);
    --shadow-lg: 0 20px 25px -5px rgba(15,23,42,.12), 0 8px 10px -6px rgba(15,23,42,.08);

    --font: 'Inter', 'IBM Plex Sans Thai', -apple-system, BlinkMacSystemFont,
            "Segoe UI", "Sarabun", "Noto Sans Thai", Tahoma, sans-serif;
}

* { box-sizing: border-box; }

body {
    font-family: var(--font);
    background: var(--content-bg);
    color: var(--ink-body);
    margin: 0;
    -webkit-font-smoothing: antialiased;
    text-rendering: optimizeLegibility;
}

h1, h2, h3, h4, h5, h6 { color: var(--ink); }
a { color: var(--brand); }
.text-muted { color: var(--ink-muted) !important; }

/* ---------- Layout: top navbar + full-width content ---------- */
.layout { min-height: 100vh; }

/* ---------- Top navbar (brand + horizontal menu + user) ---------- */
.topbar {
    display: flex;
    align-items: center;
    gap: 1rem;
    padding: .5rem 1.25rem;
    background: linear-gradient(90deg, var(--sidebar-bg) 0%, var(--sidebar-bg2) 100%);
    color: #fff;
    position: sticky; top: 0; z-index: 1035;
    box-shadow: var(--shadow-sm);
}
.topbar-brand {
    display: flex; align-items: center; gap: .55rem;
    font-weight: 700; font-size: 1.05rem; color: #fff; text-decoration: none; white-space: nowrap;
}
.topbar-brand i {
    background: linear-gradient(135deg, #6366f1, #818cf8); color: #fff;
    width: 32px; height: 32px; border-radius: 9px;
    display: grid; place-items: center; font-size: 1rem;
    box-shadow: 0 4px 10px rgba(99,102,241,.4);
}

.topnav { display: flex; align-items: center; gap: 3px; flex: 1; min-width: 0; }
.topnav .nav-link {
    color: var(--sidebar-muted); padding: .5rem .8rem; border-radius: 9px;
    font-size: .9rem; font-weight: 500; white-space: nowrap;
    display: flex; align-items: center; gap: .45rem;
    transition: background .15s ease, color .15s ease;
}
.topnav .nav-link:hover { color: #fff; background: rgba(255,255,255,.08); }
.topnav .nav-link.active {
    background: linear-gradient(135deg, var(--brand) 0%, #6366f1 100%);
    color: #fff; box-shadow: 0 6px 14px -4px rgba(79,70,229,.6);
}
.topnav .nav-link i { width: 16px; text-align: center; }

.topbar-user { display: flex; align-items: center; gap: .55rem; margin-left: auto; white-space: nowrap; }
.topbar-user .tu-name { color: #fff; font-weight: 600; font-size: .9rem; }
.topbar-user .btn-outline-light { border-color: rgba(255,255,255,.3); color: #fff; }
.topbar-user .btn-outline-light:hover { background: rgba(255,255,255,.12); }

.topbar-burger {
    display: none;   /* desktop: hidden — the nav is inline */
    background: rgba(255,255,255,.1);
    border: 0; color: #fff;
    width: 40px; height: 40px; border-radius: 10px;
    font-size: 1.25rem; cursor: pointer; place-items: center;
}

.content {
    padding: 1.6rem 2rem;
    max-width: 100%;
    min-width: 0;
    overflow-x: hidden;
}

/* ---------- Page header ---------- */
.page-header {
    display: flex; align-items: center; justify-content: space-between;
    margin-bottom: 1.4rem; flex-wrap: wrap; gap: .75rem;
    padding-bottom: 1rem;
    border-bottom: 1px solid var(--border);
}
.page-header h1 { font-size: 1.45rem; font-weight: 700; margin: 0; letter-spacing: -.01em; }
.page-header .subtitle { color: var(--ink-muted); font-size: .9rem; margin-top: .15rem; }

/* ---------- Stat cards ---------- */
.stat-card {
    background: var(--surface);
    border-radius: var(--radius);
    padding: 1.25rem 1.3rem;
    box-shadow: var(--shadow-sm);
    border: 1px solid var(--border);
    height: 100%;
    position: relative;
    transition: box-shadow .18s ease, transform .18s ease;
}
.stat-card:hover { box-shadow: var(--shadow-md); transform: translateY(-1px); }
.stat-card .label {
    color: var(--ink-muted); font-size: .78rem; font-weight: 600;
    text-transform: uppercase; letter-spacing: .04em;
}
.stat-card .value { font-size: 1.7rem; font-weight: 700; margin-top: .3rem; color: var(--ink);
    font-variant-numeric: tabular-nums; letter-spacing: -.02em; }
.stat-card .delta { font-size: .85rem; margin-top: .3rem; }
.stat-card.primary { border-left: 4px solid var(--brand); }
.stat-card.success { border-left: 4px solid var(--success); }
.stat-card.warning { border-left: 4px solid var(--warning); }
.stat-card.danger  { border-left: 4px solid var(--danger); }

/* ---------- Cards ---------- */
.table-card {
    background: var(--surface); border-radius: var(--radius); border: 1px solid var(--border);
    overflow: hidden; box-shadow: var(--shadow-sm);
}
.table-card .table { margin: 0; }
.table-card .table thead { background: #f8fafc; }
.table-card .table thead th {
    font-size: .76rem; text-transform: uppercase; letter-spacing: .04em;
    color: var(--ink-muted); font-weight: 600; border-bottom: 1px solid var(--border);
    padding-top: .85rem; padding-bottom: .85rem;
}
.table-card .table tbody td { vertical-align: middle; font-size: .92rem; border-color: var(--border-soft); }
.table-card .table tbody tr:last-child td { border-bottom: 0; }

.section-card {
    background: var(--surface); border-radius: var(--radius); border: 1px solid var(--border);
    padding: 1.4rem; box-shadow: var(--shadow-sm); margin-bottom: 1.25rem;
}
.section-card h5 { margin-bottom: 1rem; font-weight: 700; font-size: 1.05rem; }

/* Forms rendered inside a modal: flatten the section cards (no nested white
   boxes) and tidy the field grid so it reads as one orderly form. */
#form-modal-body .section-card {
    background: transparent; border: 0; box-shadow: none;
    padding: 1rem 0 0; margin-bottom: 0;
    border-top: 1px solid var(--border-soft);
}
#form-modal-body .section-card:first-child { border-top: 0; padding-top: .25rem; }
#form-modal-body .section-card h5 { font-size: .95rem; margin-bottom: .75rem; color: var(--ink); }
#form-modal-body .ba-row { background: var(--content-bg); }
#form-modal-body .form-label { margin-bottom: .25rem; font-size: .88rem; }

/* ---------- Tables (shared polish) ---------- */
.table { color: var(--ink-body); }
.table thead th { font-weight: 600; }
.table-hover tbody tr { transition: background .12s ease; }
.table-hover tbody tr:hover { background: var(--brand-soft); }

/* Responsive tables get a subtle right-edge fade hinting at scroll */
.table-responsive { position: relative; -webkit-overflow-scrolling: touch; }

/* ---------- Buttons ---------- */
.btn { font-weight: 500; border-radius: var(--radius-sm); }
.btn-primary {
    background: var(--brand); border-color: var(--brand);
    box-shadow: 0 1px 2px rgba(79,70,229,.25);
}
.btn-primary:hover, .btn-primary:focus { background: var(--brand-strong); border-color: var(--brand-strong); }
.btn-outline-primary { color: var(--brand); border-color: var(--brand); }
.btn-outline-primary:hover { background: var(--brand); border-color: var(--brand); }
.btn-sm { border-radius: 8px; }
a.btn { text-decoration: none; }

/* ---------- Badges (pill, refined) ---------- */
.badge { font-weight: 600; border-radius: 999px; padding: .35em .7em; letter-spacing: .01em; }

/* ---------- Forms ---------- */
.form-control, .form-select { border-color: var(--border); border-radius: var(--radius-sm); }
.form-control:focus, .form-select:focus {
    border-color: #c7d2fe; box-shadow: 0 0 0 .2rem rgba(99,102,241,.18);
}
.form-label { font-weight: 600; color: var(--ink-body); }
.form-control.is-invalid, .form-select.is-invalid { border-color: var(--danger); }
.input-group .btn { border-radius: 0 var(--radius-sm) var(--radius-sm) 0; }

/* ---------- Empty states ---------- */
.empty-state { text-align: center; padding: 2.5rem 1rem; color: var(--ink-muted); }
.empty-state .empty-icon {
    width: 60px; height: 60px; border-radius: 16px;
    background: var(--brand-soft); color: var(--brand);
    display: grid; place-items: center; margin: 0 auto .85rem;
    font-size: 1.7rem;
}
.empty-state .empty-title { font-weight: 600; color: var(--ink-body); margin-bottom: .2rem; }
.empty-state .empty-hint { font-size: .88rem; }
td .empty-state { padding: 2rem 1rem; }

/* ---------- Confirm modal (replaces browser confirm) ---------- */
.confirm-overlay {
    position: fixed; inset: 0; z-index: 1100;
    background: rgba(15,23,42,.5);
    display: flex; align-items: center; justify-content: center;
    padding: 1rem;
    opacity: 0; visibility: hidden; transition: opacity .18s ease, visibility .18s ease;
}
.confirm-overlay.show { opacity: 1; visibility: visible; }
.confirm-box {
    position: relative;
    background: #fff; border-radius: var(--radius); box-shadow: var(--shadow-lg);
    width: 100%; max-width: 400px; padding: 1.5rem;
    transform: translateY(8px) scale(.98); transition: transform .18s ease;
}
.confirm-overlay.show .confirm-box { transform: translateY(0) scale(1); }
/* กากบาทปิด modal (มุมขวาบน) — modal แก้ไข/สร้างปิดด้วยปุ่มนี้เท่านั้น ไม่ปิดเมื่อคลิกนอกกรอบ */
.confirm-close {
    position: absolute; top: .6rem; right: .6rem;
    width: 30px; height: 30px; padding: 0;
    border: 0; border-radius: 8px; background: transparent;
    color: var(--ink-body); font-size: 1.05rem; line-height: 1;
    display: grid; place-items: center; cursor: pointer;
    transition: background .15s ease, color .15s ease;
}
.confirm-close:hover { background: rgba(15,23,42,.07); color: var(--ink); }
.confirm-box .confirm-icon {
    width: 52px; height: 52px; border-radius: 50%;
    background: var(--danger-soft); color: var(--danger);
    display: grid; place-items: center; font-size: 1.5rem; margin-bottom: 1rem;
}
.confirm-box .confirm-title { font-weight: 700; font-size: 1.1rem; color: var(--ink); margin-bottom: .35rem; padding-right: 2rem; }
.confirm-box .confirm-msg { color: var(--ink-body); font-size: .93rem; margin-bottom: 1.25rem; }
.confirm-box .confirm-actions { display: flex; gap: .6rem; justify-content: flex-end; }

/* ---------- Login ---------- */
.login-bg {
    min-height: 100vh;
    background: linear-gradient(135deg, #312e81 0%, #4f46e5 45%, #7c3aed 100%);
    display: flex; align-items: center; justify-content: center;
    padding: 1rem;
}
.login-card {
    background: #fff; border-radius: 18px; padding: 2.25rem; width: 100%; max-width: 390px;
    box-shadow: var(--shadow-lg);
}
.login-card .login-logo {
    width: 60px; height: 60px; border-radius: 16px; margin: 0 auto;
    background: linear-gradient(135deg, var(--brand), #7c3aed); color: #fff;
    display: grid; place-items: center; font-size: 1.7rem;
    box-shadow: 0 10px 20px -6px rgba(79,70,229,.5);
}

/* ---------- Nested vendor block under a project item ---------- */
.item-vendors-cell {
    background: #f8fafc;
    border-left: 3px solid #c7d2fe;
    padding-left: 1.75rem !important;
    padding-top: .6rem !important;
    padding-bottom: .8rem !important;
}
.vendor-subtable { background: #fff; border: 1px solid var(--border); border-radius: 10px; overflow: hidden; }
.vendor-subtable thead th { font-size: .7rem; background: #fff; }
.vendor-subtable > tbody > tr:last-child > td { border-bottom: 0; }

/* Auto running number per vendor sub-table (CSS counter — survives add/delete).
   The hidden detail rows and the empty placeholder don't increment. */
.vendor-subtable > tbody { counter-reset: vrow; }
.vendor-subtable > tbody > tr:not(.assignment-detail):not(.no-vendor-row) { counter-increment: vrow; }
.seq-col { width: 38px; text-align: center; color: var(--ink-muted); font-size: .8rem; }
.vendor-subtable > tbody > tr > td.seq-col::before { content: counter(vrow); }

/* Ranked list (executive summary top-vendors) */
.rank-body { counter-reset: rrow; }
.rank-body > tr { counter-increment: rrow; }
.rank-body > tr > td.seq-col::before { content: counter(rrow); }

/* ---------- File upload chips ---------- */
.file-chip {
    position: relative; width: 84px; height: 84px;
    border: 1px solid var(--border); border-radius: 10px;
    overflow: hidden; background: #f8fafc; flex: 0 0 auto;
}
.file-chip img { width: 100%; height: 100%; object-fit: cover; display: block; }
.file-chip-doc {
    width: 100%; height: 100%; display: grid; place-items: center;
    color: var(--danger); font-size: 1.8rem;
}
.file-chip-del {
    position: absolute; top: 3px; right: 3px;
    width: 22px; height: 22px; border-radius: 50%;
    background: rgba(15,23,42,.7); color: #fff;
    display: grid; place-items: center; font-size: .85rem;
    text-decoration: none; line-height: 1;
}
.file-chip-del:hover { background: var(--danger); color: #fff; }
.file-chip a > img { cursor: zoom-in; }

/* ---------- Image lightbox (in-page preview for file thumbnails) ---------- */
.img-lightbox {
    display: none; position: fixed; inset: 0; z-index: 1200;   /* above modals (1100) */
    background: rgba(15,23,42,.85); padding: 24px;
    align-items: center; justify-content: center;
}
.img-lightbox.show { display: flex; }
.img-lightbox img {
    max-width: 95vw; max-height: 90vh; object-fit: contain;
    background: #fff; border-radius: 8px; box-shadow: 0 12px 48px rgba(0,0,0,.55);
}
.img-lightbox-close {
    position: absolute; top: 16px; right: 20px;
    width: 42px; height: 42px; border: 0; border-radius: 50%;
    background: rgba(255,255,255,.15); color: #fff; font-size: 1.15rem;
    cursor: pointer; display: grid; place-items: center;
}
.img-lightbox-close:hover { background: rgba(255,255,255,.3); }

/* ---------- Utilities ---------- */
.lift { transition: box-shadow .18s ease, transform .18s ease; }
.lift:hover { box-shadow: var(--shadow-md); transform: translateY(-2px); }
.text-money { font-variant-numeric: tabular-nums; }
.text-mono  { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: .85rem; }
.table .actions { white-space: nowrap; }
.table .actions a, .table .actions button { margin-right: .2rem; }

/* Month group header band inside the items table */
.month-head td {
    background: var(--brand-soft);
    border-top: 2px solid var(--brand);
    padding-top: .5rem; padding-bottom: .5rem;
}
.month-head-title { font-weight: 700; color: var(--brand-strong); }
.month-head-meta  { color: var(--ink-muted); font-weight: 500; font-size: .85rem; margin-left: .75rem; }

/* ---------- Row edit ---------- */
[data-edit] {
    position: relative;
    border-radius: 4px;
    transition: background .15s ease, box-shadow .15s ease;
}
/* Cells that belong to a row currently being edited */
[data-edit].row-edit-cell { background: #fff; box-shadow: inset 0 0 0 2px var(--brand); }
.item-detail [data-edit].row-edit-cell.form-control { border-color: var(--brand); box-shadow: inset 0 0 0 2px var(--brand); }
[data-edit] .inline-input {
    border: 0; background: transparent; outline: none;
    width: 100%; font: inherit; padding: 0; text-align: inherit;
}
[data-edit] .inline-edit-wrap { display: flex; align-items: center; gap: 4px; }
[data-edit] .inline-edit-wrap .inline-input { flex: 1 1 auto; min-width: 0; width: auto; }
.inline-actions { display: inline-flex; flex: 0 0 auto; gap: 2px; }
.inline-btn {
    border: 0; border-radius: 4px; padding: 1px 5px; line-height: 1.2;
    font-size: .8rem; cursor: pointer; display: inline-flex; align-items: center;
}
.inline-btn-save        { background: var(--success); color: #fff; }
.inline-btn-save:hover  { background: #047857; }
.inline-btn-cancel       { background: #e5e7eb; color: #4b5563; }
.inline-btn-cancel:hover { background: #d1d5db; }

/* Draft row — an unsaved new row awaiting save/cancel */
.draft-row > td { background: var(--warning-soft); box-shadow: inset 0 0 0 1px #fde68a; }

[data-edit] .inline-spinner i { animation: spin-soft 1s linear infinite; color: #6b7280; }
[data-edit].edit-success { background: var(--success-soft) !important; box-shadow: inset 0 0 0 1px var(--success) !important; }
[data-edit].edit-error   { background: var(--danger-soft) !important; box-shadow: inset 0 0 0 1px var(--danger) !important; }
.edit-hint { color: var(--ink-muted); font-size: .75rem; margin-left: .25rem; user-select: none; }

/* ---------- Responsive ---------- */
/* Collapse the inline menu into a burger dropdown once the links no longer
   comfortably fit alongside the brand + user (narrow desktop / tablet down). */
@media (max-width: 1200px) {
    .topbar-burger { display: grid; order: 3; }
    .topnav {
        display: none;
        position: absolute; top: 100%; left: 0; right: 0;
        flex-direction: column; align-items: stretch;
        gap: 2px; padding: .5rem;
        background: var(--sidebar-bg2);
        box-shadow: var(--shadow-lg);
        max-height: calc(100vh - 56px); overflow-y: auto;
    }
    .topbar.nav-open .topnav { display: flex; }
    .topnav .nav-link { padding: .7rem .85rem; font-size: .95rem; }
}
@media (max-width: 992px) {
    .content { padding: 1.1rem; }
    .topbar-user .tu-name { display: none; }
}
@media (max-width: 480px) {
    .content { padding: .85rem; }
    .page-header h1 { font-size: 1.25rem; }
    .stat-card .value { font-size: 1.4rem; }
}

/* ---------- Print ---------- */
@media print {
    .topbar, .page-header .btn, .alert, .update-toast, .confirm-overlay { display: none !important; }
    .content { padding: 0; }
    .section-card, .table-card, .stat-card {
        box-shadow: none !important; border: 1px solid #ddd !important;
        break-inside: avoid; page-break-inside: avoid;
    }
    body { background: #fff; }
    .table thead th { background: #f5f5f5 !important; }
    a { color: #000 !important; text-decoration: none !important; }
}

/* ---------- Toast messages ---------- */
.toast-msg {
    position: fixed; bottom: 1.5rem; right: 1.5rem; z-index: 1090;
    padding: .8rem 1.25rem; border-radius: 12px; box-shadow: var(--shadow-lg);
    color: #fff; transform: translateY(20px); opacity: 0; transition: all .25s ease;
    min-width: 250px; max-width: 400px; font-size: .95rem;
    display: flex; align-items: center; gap: .5rem;
}
.toast-msg.show { transform: translateY(0); opacity: 1; }
.toast-msg.toast-error   { background: var(--danger); }
.toast-msg.toast-info    { background: var(--brand); }
.toast-msg.toast-success { background: var(--success); }
.toast-msg.toast-warn    { background: var(--warning); }
.toast-msg .bi { font-size: 1.1rem; }
@media (max-width: 480px) {
    .toast-msg { left: 1rem; right: 1rem; max-width: none; bottom: 1rem; }
}

/* ---------- Update notification toast ---------- */
.update-toast {
    position: fixed; top: 1rem; right: 1rem; z-index: 1080; max-width: 360px;
    transform: translateY(-20px); opacity: 0;
    transition: transform .25s ease, opacity .25s ease;
}
.update-toast.show { transform: translateY(0); opacity: 1; }
.update-toast-inner {
    background: #fff; border-radius: 12px;
    box-shadow: var(--shadow-lg); padding: .9rem 1rem;
    display: flex; gap: .75rem; align-items: flex-start;
    border-left: 4px solid var(--brand);
}
.update-toast-icon {
    width: 36px; height: 36px; border-radius: 50%;
    background: var(--brand-soft); color: var(--brand);
    display: grid; place-items: center; font-size: 1.1rem; flex-shrink: 0;
    animation: spin-soft 2s linear infinite;
}
@keyframes spin-soft { from { transform: rotate(0); } to { transform: rotate(360deg); } }
.update-toast-body { flex: 1; min-width: 0; }
.update-toast-title { font-weight: 600; font-size: .95rem; color: var(--ink); }
.update-toast-msg   { font-size: .85rem; color: var(--ink-muted); margin-top: 2px; }
.update-toast-actions { display: flex; gap: .4rem; margin-top: .6rem; flex-wrap: wrap; }
@media (max-width: 480px) {
    .update-toast { left: 1rem; right: 1rem; max-width: none; }
}
