/* Motors theme on top of Bootstrap 5.3. Navy + amber, soft cards, phone-first tables. */
:root {
    --m-navy: #14213d;
    --m-navy-2: #1d2f57;
    --m-amber: #fca311;
    --m-bg: #f3f5f9;
    --bs-primary: #1f4fd6;
    --bs-primary-rgb: 31, 79, 214;
    --bs-link-color: #1f4fd6;
    --bs-link-color-rgb: 31, 79, 214;
    --bs-border-radius: .6rem;
    --bs-border-color: #e3e7ef;
}
body { background: var(--m-bg); font-feature-settings: "tnum"; }
.btn-primary { --bs-btn-bg: #1f4fd6; --bs-btn-border-color: #1f4fd6; --bs-btn-hover-bg: #1a43b6; --bs-btn-hover-border-color: #1a43b6; --bs-btn-active-bg: #163a9e; }
.btn-outline-primary { --bs-btn-color: #1f4fd6; --bs-btn-border-color: #b9c8f3; --bs-btn-hover-bg: #1f4fd6; --bs-btn-hover-border-color: #1f4fd6; }

/* Navigation */
.app-nav { background: linear-gradient(90deg, var(--m-navy), var(--m-navy-2)); box-shadow: 0 2px 10px rgba(20, 33, 61, .25); }
.app-nav .navbar-brand { font-weight: 700; letter-spacing: .2px; }
.brand-bolt { color: var(--m-amber); }
.app-nav .nav-link { color: rgba(255, 255, 255, .78); border-radius: .45rem; padding: .45rem .75rem !important; }
.app-nav .nav-link:hover { color: #fff; background: rgba(255, 255, 255, .08); }
.app-nav .nav-link.active { color: #fff; background: rgba(252, 163, 17, .18); box-shadow: inset 0 -2px 0 var(--m-amber); }
.nav-divider { width: 1px; background: rgba(255, 255, 255, .2); margin: .5rem .5rem; }
.avatar { width: 28px; height: 28px; border-radius: 50%; background: var(--m-amber); color: var(--m-navy); font-weight: 700;
    display: inline-flex; align-items: center; justify-content: center; font-size: .85rem; }
@media (max-width: 991.98px) {
    .app-nav .navbar-collapse { padding-bottom: .5rem; }
    .app-nav .nav-link { padding: .7rem .75rem !important; }
    .nav-user { border-top: 1px solid rgba(255, 255, 255, .15); margin-top: .4rem; justify-content: space-between; }
}

/* Page building blocks */
h1.h3 { font-weight: 700; color: var(--m-navy); }
.card { border: 1px solid var(--bs-border-color); box-shadow: 0 1px 2px rgba(20, 33, 61, .04), 0 4px 14px rgba(20, 33, 61, .05); }
main .table-responsive, main > .table, main > form > .table-responsive, .table-card {
    background: #fff; border: 1px solid var(--bs-border-color); border-radius: var(--bs-border-radius);
    box-shadow: 0 1px 2px rgba(20, 33, 61, .04), 0 4px 14px rgba(20, 33, 61, .05);
}
main .table { margin-bottom: 0; }
main .table > thead th { font-size: .78rem; text-transform: uppercase; letter-spacing: .04em; color: #6b7487; background: #f8f9fc;
    border-bottom-width: 1px; white-space: nowrap; }
main .table > :not(caption) > * > * { padding: .6rem .75rem; }
.flash .alert { border: 0; box-shadow: 0 2px 8px rgba(20, 33, 61, .06); }
.narrow { max-width: 680px; }
.section-title { font-size: 1.05rem; font-weight: 700; color: var(--m-navy); margin: 1.5rem 0 .75rem; }
.stat { padding: .8rem 1rem; }
.stat small { color: #6b7487; text-transform: uppercase; font-size: .72rem; letter-spacing: .05em; }
.stat strong { display: block; font-size: 1.35rem; color: var(--m-navy); }
.tile { text-decoration: none; color: inherit; gap: 4px; transition: transform .12s, box-shadow .12s; border-top: 3px solid transparent; }
.tile:hover { transform: translateY(-2px); border-top-color: var(--m-amber); box-shadow: 0 8px 22px rgba(20, 33, 61, .1); }
.tile strong { color: var(--m-navy); font-size: 1.05rem; }
.tile span { color: var(--bs-secondary-color); font-size: .9rem; }
.perm-group .card-header { background: var(--bs-tertiary-bg); }
table.audit td.details { font-family: var(--bs-font-monospace); font-size: .8rem; max-width: 520px; word-break: break-word; }

/* Login */
.login-page { min-height: 100vh; background: radial-gradient(circle at 20% 0%, #2a4480 0, var(--m-navy) 55%); }
.login-box { max-width: 400px; margin: 0 auto; padding: 10vh 16px 40px; }
.login-logo { width: 64px; height: 64px; border-radius: 18px; margin: 0 auto; background: var(--m-amber); color: var(--m-navy);
    display: flex; align-items: center; justify-content: center; box-shadow: 0 8px 24px rgba(252, 163, 17, .35); }

/* SMS flags */
.sms-flag { font-size: .7rem; font-weight: 600; vertical-align: middle; }

/* Bills grid */
.bill-grid input.reading { width: 7.5rem; margin-inline-start: auto; }
.bill-grid thead.sticky-top th { z-index: 2; }
.bill-grid tr.missing td:first-child { box-shadow: inset 3px 0 0 var(--m-amber); }
.bill-grid tr.changed td { background: #fff6dd; }
.bill-grid tr:target td { background: var(--bs-info-bg-subtle); }
.save-bar { position: sticky; bottom: 0; z-index: 5; background: rgba(255, 255, 255, .96); backdrop-filter: blur(4px);
    border-top: 1px solid var(--bs-border-color); padding: 10px 12px; margin: 0 -12px; display: flex; align-items: center; }

/* Phones: tables marked .table-stack become one card per row, labels taken from data-label. */
@media (max-width: 767.98px) {
    h1.h3 { font-size: 1.3rem; }
    .form-control, .form-select { font-size: 16px; } /* stops iOS zooming into inputs */
    main .table-responsive:has(.table-stack) { background: transparent; border: 0; box-shadow: none; overflow: visible; }
    .table-stack thead { display: none; }
    .table-stack, .table-stack tbody { display: block; }
    .table-stack tr { display: grid; grid-template-columns: 1fr 1fr; gap: 2px 12px; background: #fff; border: 1px solid var(--bs-border-color);
        border-radius: .7rem; padding: .7rem .85rem; margin-bottom: .65rem; box-shadow: 0 1px 6px rgba(20, 33, 61, .06); }
    .table-stack > tbody > tr > td { display: block; border: 0; padding: .2rem 0 !important; text-align: start !important; background: transparent !important; }
    .table-stack td[data-label]::before { content: attr(data-label); display: block; font-size: .68rem; text-transform: uppercase;
        letter-spacing: .05em; color: #7a8396; }
    .table-stack td.cell-title { grid-column: 1 / -1; font-size: 1.05rem; font-weight: 600; }
    .table-stack td.cell-wide, .table-stack td.cell-actions { grid-column: 1 / -1; }
    .table-stack td.cell-actions { display: flex; flex-wrap: wrap; gap: 6px; justify-content: flex-end; padding-top: .5rem !important;
        border-top: 1px dashed var(--bs-border-color); margin-top: .3rem; }
    .table-stack td.cell-actions form { margin: 0; }
    .table-stack td:empty:not([data-label]) { display: none; }
    .bill-grid tr { grid-template-columns: 1fr 1fr 1fr; }
    .bill-grid tr.missing { box-shadow: inset 4px 0 0 var(--m-amber), 0 1px 6px rgba(20, 33, 61, .06); }
    .bill-grid tr.missing td:first-child { box-shadow: none; }
    .bill-grid tr.changed { background: #fff6dd; }
    .bill-grid tr.table-danger { background: #fde8e8; }
    .bill-grid td.cell-reading { grid-column: span 1; }
    .bill-grid input.reading { width: 100%; font-size: 18px; padding: .5rem .6rem; text-align: start !important; }
    .bill-grid td.cell-total { font-size: 1.05rem; }
    .save-bar { margin: 0 -16px; padding: 10px 16px; }
    .save-bar .btn { flex: 1; padding: .7rem; font-size: 1.05rem; }
}
.progress-thin { height: 6px; }

/* Sortable table headers */
th.sortable { cursor: pointer; user-select: none; white-space: nowrap; }
th.sortable::after { content: "\2195"; opacity: .3; margin-inline-start: .3em; font-size: .85em; }
th.sortable[aria-sort="ascending"]::after { content: "\2191"; opacity: .9; }
th.sortable[aria-sort="descending"]::after { content: "\2193"; opacity: .9; }
th.sortable:hover { color: var(--bs-primary, #1d3557); }

/* Receipt designer */
.rd-preview { overflow: hidden; }
.rd-wrap { position: relative; overflow: hidden; }
.rd-canvas { position: absolute; top: 0; left: 0; transform-origin: 0 0; background: #fff; color: #000; outline: 1px dashed #adb5bd; touch-action: none; }
.rd-canvas:focus { outline: 2px solid var(--bs-primary, #1d3557); }
.rd-canvas .ri { position: absolute; box-sizing: border-box; cursor: move; }
.rd-canvas .ri.t { padding: 2pt; white-space: nowrap; line-height: 1.15; unicode-bidi: plaintext; outline: 1px dotted rgba(0, 0, 0, .18); }
.rd-canvas .ri.box { border: 1pt solid #000; }
.rd-canvas .ri.hline { border-top: 1pt solid #000; height: 4px !important; }
.rd-canvas .ri.vline { border-left: 1pt solid #000; width: 4px !important; }
.rd-canvas .ri.sel { outline: 2px solid #f4a261; background: rgba(244, 162, 97, .15); z-index: 2; }
.rd-items .rd-num { width: 5.2em; }
.rd-items select.form-select-sm { min-width: 5.5em; }
.rd-items tr.table-active td { background: rgba(244, 162, 97, .18); }

.bill-grid .cell-pick { width: 2.2rem; }

/* Loader */
#pageLoader { position: fixed; top: 0; left: 0; right: 0; height: 3px; z-index: 2000; pointer-events: none; overflow: hidden; display: none; }
.is-loading #pageLoader { display: block; }
#pageLoader::before { content: ""; position: absolute; top: 0; bottom: 0; left: -40%; width: 40%; background: #f4a261;
    animation: page-loader 1.1s ease-in-out infinite; }
@keyframes page-loader { to { left: 100%; } }
.is-loading body { cursor: progress; }
.btn.is-busy { pointer-events: none; opacity: .75; }
.btn-spinner { margin-inline-end: .4em; vertical-align: -0.15em; }
@media (prefers-reduced-motion: reduce) { #pageLoader::before { animation-duration: 3s; } }

/* Dashboard charts. Village colours follow a fixed order (validated categorical palette). */
.viz { --series-1: #2a78d6; --series-2: #eb6834; --series-3: #1baf7a; --series-4: #eda100;
       --series-5: #e87ba4; --series-6: #008300; --series-7: #6250d6; --series-8: #e34948; }
.s1 { fill: var(--series-1, #2a78d6); background: #2a78d6; } .s2 { fill: var(--series-2, #eb6834); background: #eb6834; }
.s3 { fill: var(--series-3, #1baf7a); background: #1baf7a; } .s4 { fill: var(--series-4, #eda100); background: #eda100; }
.s5 { fill: var(--series-5, #e87ba4); background: #e87ba4; } .s6 { fill: var(--series-6, #008300); background: #008300; }
.s7 { fill: var(--series-7, #6250d6); background: #6250d6; } .s8 { fill: var(--series-8, #e34948); background: #e34948; }
.swatch { display: inline-block; width: .75em; height: .75em; border-radius: 3px; vertical-align: -0.05em; }
.viz-legend { list-style: none; padding: 0; margin: .5rem 0 .25rem; display: flex; flex-wrap: wrap; gap: .4rem 1rem; font-size: .875rem; color: #3b4252; }
.viz-svg { width: 100%; height: auto; display: block; }
.viz-grid line { stroke: #e6e9f0; stroke-width: 1; }
.viz-base { stroke: #9aa3b5; stroke-width: 1; }
.viz-axis { font-size: 13px; fill: #6b7385; font-family: system-ui, sans-serif; }
.viz-hit { fill: transparent; cursor: default; }
.viz-col:hover .viz-hit { fill: rgba(31, 79, 214, .06); }
.viz-tip { position: fixed; z-index: 1500; pointer-events: none; background: #fff; color: #14213d; border: 1px solid #dfe3ea;
           border-radius: 8px; box-shadow: 0 6px 20px rgba(20, 33, 61, .15); padding: .5rem .65rem; font-size: .85rem; min-width: 11rem; }
.viz-tip .tip-head { font-weight: 600; margin-bottom: .25rem; }
.viz-tip .tip-row { display: flex; align-items: center; gap: .4rem; justify-content: space-between; }
.viz-tip .tip-row span:first-child { display: flex; align-items: center; gap: .35rem; }
.viz-tip .tip-total { border-top: 1px solid #eef0f4; margin-top: .3rem; padding-top: .3rem; font-weight: 600; }
.amp-row { display: grid; grid-template-columns: 4.5rem 1fr 3rem; align-items: center; gap: .6rem; margin: .3rem 0; }
.amp-label { font-weight: 600; }
.amp-track { background: #eef1f6; border-radius: 4px; height: .7rem; overflow: hidden; }
.amp-bar { background: #2a78d6; height: 100%; border-radius: 0 4px 4px 0; }
.amp-count { text-align: end; font-variant-numeric: tabular-nums; }

.idle-warning { position: fixed; left: 50%; bottom: 1rem; transform: translateX(-50%); z-index: 2100; background: #14213d; color: #fff;
                border-radius: .6rem; padding: .6rem .8rem; display: flex; gap: .8rem; align-items: center; box-shadow: 0 8px 24px rgba(0,0,0,.25);
                max-width: calc(100% - 2rem); font-size: .95rem; }
