:root {
    --topbar-bg: #1f2937;
    --topbar-fg: #f9fafb;
    --sidebar-bg: #f3f4f6;
    --border: #e5e7eb;
    --accent: #0f6cbd;
}

* { box-sizing: border-box; }

html, body {
    margin: 0;
    height: 100%;
}

body {
    font-family: "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
    font-size: 14px;
    color: #111827;
}

/* ---- App shell ---- */

.topbar {
    display: flex;
    align-items: center;
    gap: 16px;
    height: 48px;
    padding: 0 16px;
    background: var(--topbar-bg);
    color: var(--topbar-fg);
}

.topbar .brand {
    font-size: 17px;
    font-weight: 600;
    letter-spacing: 0.5px;
}

.topbar .spacer { flex: 1; }

.topbar .username { opacity: 0.85; }

.topbar form { margin: 0; }

.company-switcher select {
    max-width: 340px;
    padding: 4px 8px;
    border-radius: 4px;
    border: 1px solid #4b5563;
    background: #374151;
    color: var(--topbar-fg);
}

button.linklike {
    background: none;
    border: none;
    color: var(--topbar-fg);
    cursor: pointer;
    text-decoration: underline;
    font-size: 13px;
    opacity: 0.85;
}
button.linklike:hover { opacity: 1; }

.shell {
    display: flex;
    min-height: calc(100vh - 48px);
}

/* ---- Drawer side menu (ported from MPKjPrisma) ---- */

.topbar .menu-btn {
    background: none;
    border: none;
    color: var(--topbar-fg);
    font-size: 20px;
    cursor: pointer;
    padding: 6px 10px;
    border-radius: 6px;
    margin-left: -6px;
    line-height: 1;
    display: flex;
    align-items: center;
    transition: background 0.2s;
}
.topbar .menu-btn:hover { background: rgba(255,255,255,0.15); }

/* Fixed 48px topbar; drawer (sidebar + content) fills the remaining height. */
#layout-drawer {
    height: calc(100vh - 48px);
    overflow: hidden;
}
#layout-drawer .dx-drawer-wrapper { height: 100% !important; }
.dx-drawer-shrink .dx-drawer-panel-content {
    border-right: 1px solid var(--border) !important;
    height: 100% !important;
    background: var(--sidebar-bg);
}

/* Content area scrolls independently of the nav panel. */
.drawer-content {
    display: flex;
    flex-direction: column;
    height: 100%;
    overflow-y: auto;
    animation: contentFadeIn 0.25s ease-out;
}
.drawer-content main { flex-grow: 1; }
@keyframes contentFadeIn {
    from { opacity: 0; }
    to { opacity: 1; }
}
body.page-leaving .drawer-content {
    opacity: 0;
    transition: opacity 0.15s ease-in;
}

.drawer-nav-panel {
    width: 240px;
    height: 100%;
    overflow-y: auto;
    padding-top: 8px;
}

/* Plain HTML nav menu built by initNavPanel() in _Layout */
.nav-menu { list-style: none; margin: 0; padding: 0; }
.nav-submenu { display: none; }
.nav-parent.expanded > .nav-submenu { display: block; }
.nav-link { display: flex; align-items: center; padding: 8px 16px; cursor: pointer; text-decoration: none; color: #111827; user-select: none; }
.nav-link:visited { color: #111827; }
.nav-link:hover { background: rgba(0,0,0,0.05); text-decoration: none; }
.nav-link i:first-child { width: 24px; font-size: 18px; text-align: center; margin-right: 8px; opacity: 0.7; }
.nav-link.active { background: rgba(15,108,189,0.12); color: var(--accent); font-weight: 500; }
.nav-link.active:visited { color: var(--accent); }
.nav-link.active i:first-child { opacity: 1; }
.nav-chevron { margin-left: auto; font-size: 14px; transition: transform 0.2s ease; opacity: 0.5; }
.nav-parent.expanded > .nav-link > .nav-chevron { transform: rotate(180deg); }
.nav-submenu .nav-link { padding-left: 48px; }

.content {
    flex: 1;
    padding: 20px 24px;
    min-width: 0;
}

.content h2 { margin-top: 0; }

.active-company { font-size: 15px; }

/* ---- Login page ---- */

.login-body {
    display: flex;
    align-items: center;
    justify-content: center;
    min-height: 100vh;
    background: #f3f4f6;
}

.login-card {
    width: 340px;
    background: #fff;
    border: 1px solid var(--border);
    border-radius: 8px;
    padding: 32px;
    box-shadow: 0 4px 14px rgba(0, 0, 0, 0.06);
}

.login-card h1 {
    margin: 0 0 4px;
    font-size: 22px;
    color: var(--topbar-bg);
}

.login-subtitle {
    margin: 0 0 20px;
    color: #6b7280;
}

.login-card label { display: block; margin: 12px 0 4px; }

.login-card label.remember {
    display: flex;
    align-items: center;
    gap: 6px;
    margin: 14px 0;
}

.login-card input[type="text"],
.login-card input[type="password"] {
    width: 100%;
    padding: 8px 10px;
    border: 1px solid #d1d5db;
    border-radius: 4px;
}

.login-card button {
    width: 100%;
    padding: 9px;
    background: var(--accent);
    color: #fff;
    border: none;
    border-radius: 4px;
    font-size: 14px;
    cursor: pointer;
}
.login-card button:hover { filter: brightness(1.08); }

.validation-summary { color: #b91c1c; }
.validation-summary ul { margin: 0 0 8px; padding-left: 18px; }

/* ---- Common bits ---- */

.hint { color: #6b7280; font-size: 13px; }
.error { color: #b91c1c; font-weight: 600; }
.badge {
    display: inline-block;
    margin-left: 8px;
    padding: 2px 8px;
    border-radius: 10px;
    background: #eef2ff;
    color: #3730a3;
    font-size: 12px;
}

.toolbar-row {
    display: flex;
    align-items: center;
    gap: 10px;
    margin: 12px 0;
}

.btn-primary, .btn-danger, .btn-plain {
    display: inline-block;
    padding: 7px 14px;
    border-radius: 4px;
    border: 1px solid transparent;
    font-size: 14px;
    cursor: pointer;
    text-decoration: none;
}
.btn-primary { background: var(--accent); color: #fff; }
.btn-danger { background: #b91c1c; color: #fff; }
.btn-plain { background: #fff; color: #111827; border-color: #d1d5db; }
.btn-primary:hover, .btn-danger:hover { filter: brightness(1.08); }
.btn-plain:hover { background: #f3f4f6; }

.jv-header { display: flex; gap: 20px; margin-bottom: 14px; }
.jv-header .jv-desc { flex: 1; }
.jv-header label { display: block; margin-bottom: 4px; color: #374151; }

.detail-table { border-collapse: collapse; margin-bottom: 16px; }
.detail-table th { text-align: left; padding: 4px 18px 4px 0; color: #6b7280; font-weight: 500; }
.detail-table td { padding: 4px 0; }

/* ---- Financial statements ---- */

.report-header { text-align: center; margin: 8px 0 16px; }
.report-header h2 { margin: 0; font-size: 20px; }
.report-header h3 { margin: 2px 0; font-size: 16px; font-weight: 600; }
.report-header p { margin: 2px 0; color: #374151; }

.fs-table {
    border-collapse: collapse;
    width: 100%;
    max-width: 950px;
    font-size: 13.5px;
}
.fs-table th, .fs-table td { padding: 3px 8px; }
.fs-table thead th {
    border-bottom: 2px solid #111827;
    text-align: left;
    font-weight: 700;
}
.fs-table thead th.num { text-align: right; }
.fs-table .num { text-align: right; white-space: nowrap; font-variant-numeric: tabular-nums; }
.fs-table .pct { color: #4b5563; min-width: 46px; }
.fs-table .fs-code { white-space: nowrap; width: 110px; }
.fs-table .indent-2 { padding-left: 26px; }
.fs-table .indent-name-2 { padding-left: 18px; }
.fs-table .fs-section td { font-weight: 700; text-decoration: underline; padding-top: 10px; }
.fs-table .topline { border-top: 1px solid #111827; }
.fs-table .fs-sectiontotal td, .fs-table .fs-computed td { font-weight: 600; }
.fs-table .fs-computed.strong td { font-weight: 700; }
.fs-table .fs-grouptotal td { color: #111827; }
.lines-table { max-width: 950px; }
.total-row td { font-weight: 700; border-top: 2px solid #111827; }

/* ---- Printable documents (invoice / quotation) ---- */

.doc {
    max-width: 900px;
    background: #fff;
    border: 1px solid var(--border);
    padding: 28px 32px;
    font-size: 13.5px;
}

.doc-head {
    display: flex;
    justify-content: space-between;
    align-items: flex-start;
    margin-bottom: 14px;
}

.doc-company { font-size: 19px; font-weight: 700; }
.doc-regno { font-size: 12px; font-weight: 400; }
.doc-type { font-size: 26px; font-weight: 800; letter-spacing: 1px; }

.doc-columns {
    display: flex;
    justify-content: space-between;
    gap: 24px;
    margin: 14px 0;
}

.doc-addr { line-height: 1.45; }
.doc-meta { margin: 10px 0; }
.doc-subject { font-weight: 700; text-decoration: underline; }

.doc-table {
    border-collapse: collapse;
    width: 100%;
    margin: 14px 0;
    /* Honour the per-column pixel widths exactly and give ALL remaining width
       to the Description column (auto layout stretches the numeric columns). */
    table-layout: fixed;
    word-wrap: break-word;
}
.doc-table th, .doc-table td {
    border: 1px solid #111827;
    padding: 5px 8px;
    vertical-align: top;
}
.doc-table thead th { background: #f3f4f6; text-align: left; }
/* Headers may wrap ("Subtotal with SST (RM)" breaks onto a 2nd line inside its
   fixed-width column); the amount CELLS below keep nowrap. */
.doc-table thead th.num { text-align: right; white-space: normal; }
.doc-table .num { text-align: right; white-space: nowrap; font-variant-numeric: tabular-nums; }
.doc-table .total-row td { font-weight: 600; }

.pre-line { white-space: pre-line; }
.doc-terms { margin-top: 16px; line-height: 1.5; }
.doc-thanks { text-align: center; font-weight: 700; margin-top: 22px; }

/* ---- Payslip ---- */

.payslip { max-width: 860px; }
.payslip-id { width: 100%; margin: 10px 0; }
.payslip-id > tbody > tr > td { vertical-align: top; width: 50%; }
.payslip-period { float: right; font-weight: 700; }

.payslip-grid {
    border-collapse: collapse;
    width: 100%;
    margin-top: 8px;
}
.payslip-grid > tbody > tr > th,
.payslip-grid > tbody > tr > td {
    border: 1px solid #111827;
    padding: 8px 10px;
    vertical-align: top;
    text-align: left;
}
.payslip-lines { width: 100%; border-collapse: collapse; }
.payslip-lines td { padding: 2px 0; }
.payslip-lines .num { text-align: right; white-space: nowrap; font-variant-numeric: tabular-nums; }
.payslip-lines .strongline td { border-top: 1px solid #111827; padding-top: 4px; }
.payslip-lines .netpay { border: 1.5px solid #111827; padding: 2px 6px; }
.payslip-signature { text-align: center; vertical-align: bottom !important; padding-bottom: 18px !important; }

/* ---- Reconciliation workbench ---- */

.recon-grids {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 18px;
    align-items: start;
}
.recon-grids h3 { margin: 4px 0 8px; }
@media (max-width: 1200px) {
    .recon-grids { grid-template-columns: 1fr; }
}

/* ---- Mobile (leave self-service is the primary phone use case) ---- */

@media (max-width: 768px) {
    .topbar {
        flex-wrap: wrap;
        height: auto;
        min-height: 48px;
        padding: 8px 12px;
        gap: 8px 12px;
    }
    .company-switcher select { max-width: 48vw; }
    .topbar .username { display: none; }

    .shell { flex-direction: column; }

    /* The wrapping topbar makes the chrome taller than 48px; size the drawer
       against the real remaining space instead of the desktop constant. */
    #layout-drawer { height: auto; min-height: calc(100vh - 48px); }
    .drawer-nav-panel { width: 200px; }

    .content { padding: 12px; }
    .toolbar-row { display: flex; flex-wrap: wrap; gap: 8px; }

    /* Grids size themselves against the viewport height minus the desktop
       chrome; with the stacked layout that goes wrong — pin a sane height.
       (!important is needed to beat the widget's inline style.) */
    #myLeaveGrid { height: 480px !important; }
}

/* ---- Print ---- */

@media print {
    .topbar, .no-print, .dx-drawer-panel-content { display: none !important; }
    /* Un-fix the drawer's screen heights so printed pages flow normally. */
    #layout-drawer,
    #layout-drawer .dx-drawer-wrapper,
    #layout-drawer .dx-drawer-content,
    .drawer-content {
        height: auto !important;
        overflow: visible !important;
    }
    .content { padding: 0; }
    body { font-size: 12px; }
    .fs-table { max-width: none; }
    .doc { border: none; padding: 0; max-width: none; }
    /* Browsers repeat <tfoot> on every printed page; treat it as an ordinary
       row group so SUBTOTAL / TOTAL PAYABLE print once, on the last page. */
    .doc-table tfoot { display: table-row-group; }
    .doc-table tr { break-inside: avoid; }
}

/* ---- Dashboard (Phase 6) ---- */
.kpi-row { display: grid; grid-template-columns: repeat(auto-fit, minmax(210px, 1fr)); gap: 14px; margin: 14px 0 18px; }
.kpi-card { background: #fff; border: 1px solid #e3e6ea; border-radius: 8px; padding: 14px 16px; box-shadow: 0 1px 2px rgba(16, 24, 40, .04); }
.kpi-label { font-size: 12px; text-transform: uppercase; letter-spacing: .04em; color: #6b7280; }
.kpi-value { font-size: 26px; font-weight: 700; margin: 4px 0 2px; font-variant-numeric: tabular-nums; }
.kpi-value.neg { color: #c0392b; }
.kpi-sub { font-size: 12px; color: #6b7280; }
.dash-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; margin-bottom: 18px; }
@media (max-width: 1100px) { .dash-grid { grid-template-columns: 1fr; } }
.dash-panel { background: #fff; border: 1px solid #e3e6ea; border-radius: 8px; padding: 12px 16px 16px; }
.dash-panel h3 { margin: 2px 0 10px; font-size: 15px; }
.dash-table td { padding: 4px 6px; border-bottom: 1px solid #f1f3f5; }
.dash-desc { max-width: 260px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* ---- Consolidated statements ---- */
.fs-table .company-col { min-width: 130px; }
