/* =====================================================================
   NCO Portal — stylesheet
   One file, organised top-down: tokens → base → layout → components.
   Works identically in RTL and LTR by using logical properties
   (inline-start / inline-end) instead of left / right.
   ===================================================================== */

/* ---------- 1. Design tokens (light) --------------------------------- */
:root {
    --bg:            #f6f7f9;
    --surface:       #ffffff;
    --surface-2:     #f0f2f5;
    --border:        #dfe3e8;
    --border-strong: #c8cfd6;
    --text:          #14181d;
    --text-dim:      #5b6672;
    --text-faint:    #8a939d;

    --accent:        #1f6f4a;      /* NCO green */
    --accent-soft:   #e8f2ec;
    --accent-text:   #ffffff;

    --danger:        #b3261e;
    --danger-soft:   #fdeceb;
    --warn:          #8a6100;
    --warn-soft:     #fdf4e3;
    --ok:            #1f6f4a;
    --ok-soft:       #e8f2ec;

    --radius:        10px;
    --radius-sm:     7px;
    --shadow:        0 1px 2px rgba(16,24,32,.05), 0 4px 14px rgba(16,24,32,.05);
    --sidebar-w:     236px;
    --topbar-h:      56px;

    --font: -apple-system, BlinkMacSystemFont, "Segoe UI", "Noto Sans Arabic",
            "Helvetica Neue", Arial, sans-serif;
    --mono: ui-monospace, SFMono-Regular, "SF Mono", Menlo, monospace;
}

/* ---------- 2. Dark palette ------------------------------------------ */
@media (prefers-color-scheme: dark) {
    :root:not([data-theme="light"]) {
        --bg:            #0f1216;
        --surface:       #161a20;
        --surface-2:     #1d222a;
        --border:        #272d36;
        --border-strong: #39414d;
        --text:          #e8ecf1;
        --text-dim:      #a2acb8;
        --text-faint:    #6f7a87;

        --accent:        #3f9d70;
        --accent-soft:   #17281f;
        --accent-text:   #06120c;

        --danger:        #f2837b;
        --danger-soft:   #2a1614;
        --warn:          #e0b25a;
        --warn-soft:     #26200f;
        --ok:            #3f9d70;
        --ok-soft:       #17281f;

        --shadow:        0 1px 2px rgba(0,0,0,.4), 0 6px 18px rgba(0,0,0,.35);
    }
}
:root[data-theme="dark"] {
    --bg:            #0f1216;
    --surface:       #161a20;
    --surface-2:     #1d222a;
    --border:        #272d36;
    --border-strong: #39414d;
    --text:          #e8ecf1;
    --text-dim:      #a2acb8;
    --text-faint:    #6f7a87;
    --accent:        #3f9d70;
    --accent-soft:   #17281f;
    --accent-text:   #06120c;
    --danger:        #f2837b;
    --danger-soft:   #2a1614;
    --warn:          #e0b25a;
    --warn-soft:     #26200f;
    --ok:            #3f9d70;
    --ok-soft:       #17281f;
    --shadow:        0 1px 2px rgba(0,0,0,.4), 0 6px 18px rgba(0,0,0,.35);
}

/* ---------- 3. Base --------------------------------------------------- */
* { box-sizing: border-box; }
html, body { height: 100%; }
body {
    margin: 0;
    background: var(--bg);
    color: var(--text);
    font: 14px/1.55 var(--font);
    -webkit-font-smoothing: antialiased;
}
a { color: inherit; text-decoration: none; }
button { font: inherit; }
.icon { flex: none; }

.skip-link {
    position: absolute; inset-inline-start: -9999px; top: 0;
    background: var(--accent); color: var(--accent-text);
    padding: 8px 14px; z-index: 100;
}
.skip-link:focus { inset-inline-start: 0; }

/* ---------- 4. Top bar ------------------------------------------------ */
.topbar {
    height: var(--topbar-h);
    display: flex; align-items: center; gap: 14px;
    padding-inline: 16px;
    background: var(--surface);
    border-bottom: 1px solid var(--border);
    position: sticky; top: 0; z-index: 20;
}
.brand { display: flex; align-items: baseline; gap: 10px; min-width: 0; }
.brand-mark {
    font-weight: 700; letter-spacing: .08em; font-size: 16px; color: var(--accent);
}
.brand-sub { color: var(--text-faint); font-size: 12px; white-space: nowrap;
             overflow: hidden; text-overflow: ellipsis; }
.topbar-actions { margin-inline-start: auto; display: flex; align-items: center; gap: 10px; }

.user-chip {
    display: flex; align-items: center; gap: 7px;
    padding: 5px 11px; border: 1px solid var(--border); border-radius: 999px;
    color: var(--text-dim); font-size: 13px; max-width: 190px;
}
.user-chip span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.seg { display: flex; border: 1px solid var(--border); border-radius: var(--radius-sm); overflow: hidden; }
.seg-btn {
    background: transparent; border: 0; cursor: pointer;
    padding: 6px 9px; color: var(--text-dim); display: grid; place-items: center;
}
.seg-btn:hover { background: var(--surface-2); color: var(--text); }
.seg-btn.is-on { background: var(--accent-soft); color: var(--accent); }

.ghost-btn {
    background: transparent; border: 1px solid var(--border); color: var(--text-dim);
    border-radius: var(--radius-sm); padding: 6px 12px; cursor: pointer; font-size: 12.5px;
}
.ghost-btn:hover { background: var(--surface-2); color: var(--text); }
.icon-btn {
    background: transparent; border: 1px solid var(--border); color: var(--text-dim);
    border-radius: var(--radius-sm); padding: 6px; cursor: pointer; display: grid; place-items: center;
}
.lang-form { margin: 0; }
.only-mobile { display: none; }

/* ---------- 5. Shell + sidebar ---------------------------------------- */
.shell { display: flex; min-height: calc(100vh - var(--topbar-h)); }
.sidebar {
    width: var(--sidebar-w); flex: none;
    background: var(--surface);
    border-inline-end: 1px solid var(--border);
    padding: 14px 10px; display: flex; flex-direction: column; gap: 4px;
    position: sticky; top: var(--topbar-h); height: calc(100vh - var(--topbar-h));
    overflow-y: auto;
}
.nav-item {
    display: flex; align-items: center; gap: 11px;
    padding: 9px 12px; border-radius: var(--radius-sm);
    color: var(--text-dim); font-size: 13.5px;
}
.nav-item:hover { background: var(--surface-2); color: var(--text); }
.nav-item.is-active { background: var(--accent-soft); color: var(--accent); font-weight: 600; }
.sidebar-foot { margin-top: auto; padding-top: 10px; border-top: 1px solid var(--border); }

.main { flex: 1; padding: 22px 24px 48px; min-width: 0; }

/* ---------- 6. Page furniture ----------------------------------------- */
.page-head { display: flex; align-items: flex-end; gap: 16px; flex-wrap: wrap; margin-bottom: 20px; }
.page-title { margin: 0; font-size: 22px; font-weight: 650; letter-spacing: -.01em; }
.page-sub { margin: 3px 0 0; color: var(--text-faint); font-size: 13px; }
.page-actions { margin-inline-start: auto; display: flex; gap: 8px; flex-wrap: wrap; }

.card {
    background: var(--surface); border: 1px solid var(--border);
    border-radius: var(--radius); box-shadow: var(--shadow);
}
.card-pad { padding: 18px; }
.card + .card { margin-top: 16px; }
.card-title { margin: 0 0 14px; font-size: 15px; font-weight: 600; }

/* stat tiles */
.tiles { display: grid; gap: 12px; grid-template-columns: repeat(auto-fit, minmax(190px, 1fr)); margin-bottom: 18px; }
.tile { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 15px 16px; min-width: 0; }
.tile-head { display: flex; align-items: center; justify-content: space-between; gap: 10px; color: var(--text-dim); font-size: 12.5px; }
.tile-head span:first-child { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; min-width: 0; }
/* A big figure (e.g. "Rp 128.450.000") must shrink to fit its card rather
   than spill past its edge — this is the one PMT flaw we deliberately
   never repeat. clamp() lets it start large and step down on its own. */
.tile-value {
    font-size: clamp(16px, 1.6vw + 12px, 27px);
    font-weight: 650; margin-top: 6px; letter-spacing: -.02em;
    overflow-wrap: anywhere; word-break: break-word;
    line-height: 1.2;
}
.tile-foot { color: var(--text-faint); font-size: 11.5px; margin-top: 2px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.tile-ic { display: grid; place-items: center; width: 30px; height: 30px; border-radius: 8px; background: var(--surface-2); color: var(--text-dim); }
.tile.is-danger .tile-ic { background: var(--danger-soft); color: var(--danger); }
.tile.is-warn   .tile-ic { background: var(--warn-soft);   color: var(--warn); }
.tile.is-ok     .tile-ic { background: var(--ok-soft);     color: var(--ok); }

/* stat bars — simple horizontal breakdowns (category / ledger / trend) */
.bars { display: flex; flex-direction: column; gap: 10px; }
.bar-row { display: grid; grid-template-columns: minmax(90px, 34%) 1fr auto; align-items: center; gap: 10px; font-size: 13px; }
.bar-label { color: var(--text-dim); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.bar-track { position: relative; height: 8px; border-radius: 999px; background: var(--surface-2); overflow: hidden; }
.bar-fill { position: absolute; inset-block: 0; inset-inline-start: 0; background: var(--accent); border-radius: 999px; }
.bar-fill.is-danger { background: var(--danger); }
.bar-value { font-family: var(--mono); font-size: 12px; color: var(--text-dim); white-space: nowrap; text-align: end; }

/* ---------- 7. Tables -------------------------------------------------- */
.table-wrap { overflow-x: auto; }
table.data { width: 100%; border-collapse: collapse; font-size: 13.5px; }
table.data th, table.data td { padding: 11px 13px; text-align: start; border-bottom: 1px solid var(--border); }
table.data thead th {
    color: var(--text-faint); font-weight: 600; font-size: 11.5px;
    letter-spacing: .05em; text-transform: uppercase; white-space: nowrap;
}
table.data tbody tr:hover { background: var(--surface-2); }
table.data tbody tr:last-child td { border-bottom: 0; }
.mono { font-family: var(--mono); font-size: 12px; color: var(--text-dim); }
.num { font-variant-numeric: tabular-nums; text-align: end; }

/* ---------- 8. Badges & pills ------------------------------------------ */
.badge {
    display: inline-flex; align-items: center; gap: 6px;
    padding: 3px 9px; border-radius: 999px; font-size: 11.5px; font-weight: 600;
    background: var(--surface-2); color: var(--text-dim);
}
.badge::before { content: ""; width: 6px; height: 6px; border-radius: 50%; background: currentColor; }
.badge.is-danger { background: var(--danger-soft); color: var(--danger); }
.badge.is-warn   { background: var(--warn-soft);   color: var(--warn); }
.badge.is-ok     { background: var(--ok-soft);     color: var(--ok); }
.badge.is-plain::before { display: none; }

/* ---------- 9. Forms --------------------------------------------------- */
.field { margin-bottom: 14px; }
.field label { display: block; font-size: 12.5px; color: var(--text-dim); margin-bottom: 5px; }
.input, select.input, textarea.input {
    width: 100%; padding: 9px 11px;
    background: var(--surface); color: var(--text);
    border: 1px solid var(--border-strong); border-radius: var(--radius-sm);
    font: inherit;
}
.input:focus { outline: 2px solid var(--accent); outline-offset: 1px; border-color: var(--accent); }
.hint { color: var(--text-faint); font-size: 11.5px; margin-top: 4px; }

.btn {
    display: inline-flex; align-items: center; gap: 8px;
    padding: 9px 16px; border-radius: var(--radius-sm); border: 1px solid transparent;
    background: var(--accent); color: var(--accent-text); font-weight: 600; font-size: 13.5px;
    cursor: pointer;
}
.btn:hover { filter: brightness(1.06); }
.btn.secondary { background: var(--surface); color: var(--text); border-color: var(--border-strong); }
.btn.secondary:hover { background: var(--surface-2); filter: none; }
.btn.danger { background: var(--danger); color: #fff; }

.alert {
    padding: 11px 14px; border-radius: var(--radius-sm); font-size: 13px;
    border: 1px solid var(--border); background: var(--surface-2); margin-bottom: 16px;
    display: flex; gap: 9px; align-items: flex-start;
}
.alert.is-error { background: var(--danger-soft); border-color: var(--danger); color: var(--danger); }
.alert.is-ok    { background: var(--ok-soft);     border-color: var(--ok);     color: var(--ok); }
.alert.is-warn  { background: var(--warn-soft);   border-color: var(--warn);   color: var(--warn); }

/* ---------- 10. Auth / setup screens ----------------------------------- */
body.bare { display: grid; place-items: center; padding: 24px; }
.auth-card { width: 100%; max-width: 400px; }
.auth-brand { text-align: center; margin-bottom: 22px; }
.auth-brand .brand-mark { font-size: 26px; }
.auth-brand p { color: var(--text-faint); font-size: 13px; margin: 4px 0 0; }

/* ---------- 11. Permission grid ---------------------------------------- */
.perm-module { border: 1px solid var(--border); border-radius: var(--radius); margin-bottom: 12px; }
.perm-module h3 {
    margin: 0; padding: 11px 15px; font-size: 13px; font-weight: 650;
    border-bottom: 1px solid var(--border); background: var(--surface-2);
    border-radius: var(--radius) var(--radius) 0 0;
}
.perm-list { padding: 6px 8px; display: grid; gap: 2px; }
.perm-row {
    display: flex; align-items: center; gap: 10px; padding: 8px 10px;
    border-radius: var(--radius-sm); font-size: 13.5px;
}
.perm-row:hover { background: var(--surface-2); }
.perm-row input { width: 16px; height: 16px; accent-color: var(--accent); }
.perm-row .tag-sensitive {
    margin-inline-start: auto; font-size: 10.5px; font-weight: 700; letter-spacing: .04em;
    color: var(--danger); background: var(--danger-soft); padding: 2px 7px; border-radius: 999px;
}

/* ---------- 12. Financial statements & journal (Rates / Accounting) --- */
.stmt-group { margin-bottom: 18px; }
.stmt-group:last-child { margin-bottom: 0; }
.stmt-group-title { font-size: 12px; font-weight: 650; color: var(--text-dim); text-transform: uppercase; letter-spacing: .05em; margin: 0 0 8px; }
.stmt-row { display: flex; justify-content: space-between; align-items: baseline; gap: 12px; padding: 7px 0; border-bottom: 1px solid var(--border); font-size: 13.5px; }
.stmt-row:last-child { border-bottom: 0; }
.stmt-row .stmt-label { min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.stmt-row .stmt-value { font-family: var(--mono); font-variant-numeric: tabular-nums; white-space: nowrap; }
.stmt-row.is-total { font-weight: 700; border-top: 1px solid var(--border-strong); border-bottom: 0; margin-top: 4px; padding-top: 10px; }
.stmt-row.is-grand { font-weight: 700; font-size: 15px; color: var(--accent); }

.balance-check {
    display: flex; align-items: center; gap: 8px; padding: 10px 14px;
    border-radius: var(--radius-sm); font-size: 13px; font-weight: 600;
    background: var(--surface-2); color: var(--text-dim);
}
.balance-check.is-ok    { background: var(--ok-soft);     color: var(--ok); }
.balance-check.is-error { background: var(--danger-soft); color: var(--danger); }

/* ---------- 13. Responsive --------------------------------------------- */
@media (max-width: 860px) {
    .only-mobile { display: grid; }
    .sidebar {
        position: fixed; inset-block: var(--topbar-h) 0; inset-inline-start: 0;
        z-index: 30; transform: translateX(calc(var(--nav-hide, -110%)));
        transition: transform .18s ease;
    }
    html[dir="rtl"] .sidebar { --nav-hide: 110%; }
    .sidebar.is-open { transform: translateX(0); }
    .brand-sub { display: none; }
    .main { padding: 16px 14px 40px; }
}

/* ---------- 14. Print --------------------------------------------------- */
@media print {
    .topbar, .sidebar, .page-actions, .no-print { display: none !important; }
    body { background: #fff; }
    .main { padding: 0; }
    .card { border: 0; box-shadow: none; }
    table.data th, table.data td { border-color: #ccc; }
    @page { margin: 14mm; }
}
