/* =====================================================================
   assets/css/app.css
   Custom styles. ALL colors are CSS variables defined for both themes,
   so every page, table (DataTables), modal and alert (SweetAlert2)
   switches automatically with Bootstrap 5.3's data-bs-theme.
   ===================================================================== */

/* ---------- Theme variables ---------- */
:root,
[data-bs-theme="light"] {
    --app-body-bg: #f3f5f9;
    --app-surface: #ffffff;
    --app-surface-2: #f8f9fb;
    --app-border: #e3e7ee;
    --app-shadow: 0 1px 2px rgba(16, 24, 40, .05), 0 1px 3px rgba(16, 24, 40, .08);

    --app-navbar-bg: #ffffff;
    --app-sidebar-bg: #ffffff;
    --app-sidebar-link: #495057;
    --app-sidebar-hover-bg: #eef2f7;
    --app-sidebar-active-bg: #e7f0ff;
    --app-sidebar-active-color: #0b5ed7;
    --app-sidebar-heading: #8a94a6;

    /* Read by theme.js for Chart.js */
    --app-chart-text: #495057;
    --app-chart-grid: rgba(0, 0, 0, .08);
    --app-chart-tooltip-bg: rgba(33, 37, 41, .92);
    --app-chart-tooltip-text: #ffffff;
    /* Categorical series (validated for color-vision deficiency on this surface) */
    --app-series-1: #2a78d6;
    --app-series-2: #eb6834;
}

[data-bs-theme="dark"] {
    --app-body-bg: #111418;
    --app-surface: #1a1e23;
    --app-surface-2: #20252b;
    --app-border: #2d333b;
    --app-shadow: 0 1px 2px rgba(0, 0, 0, .45);

    --app-navbar-bg: #161a1f;
    --app-sidebar-bg: #161a1f;
    --app-sidebar-link: #c3cad3;
    --app-sidebar-hover-bg: #232931;
    --app-sidebar-active-bg: rgba(13, 110, 253, .20);
    --app-sidebar-active-color: #8bb9fe;
    --app-sidebar-heading: #7d8794;

    --app-chart-text: #c3cad3;
    --app-chart-grid: rgba(255, 255, 255, .10);
    --app-chart-tooltip-bg: rgba(248, 249, 250, .95);
    --app-chart-tooltip-text: #212529;
    --app-series-1: #3987e5;
    --app-series-2: #d95926;
}

/* ---------- Base ---------- */
body {
    background-color: var(--app-body-bg);
}

.card {
    --bs-card-bg: var(--app-surface);
    --bs-card-border-color: var(--app-border);
    --bs-card-cap-bg: var(--app-surface-2);
    box-shadow: var(--app-shadow);
}

.modal-content {
    --bs-modal-bg: var(--app-surface);
    --bs-modal-border-color: var(--app-border);
}

.dropdown-item.active,
.dropdown-item:active {
    --bs-dropdown-link-active-bg: var(--app-sidebar-active-bg);
    --bs-dropdown-link-active-color: var(--app-sidebar-active-color);
}

/* ---------- Navbar ---------- */
.app-navbar {
    height: 56px;
    background-color: var(--app-navbar-bg);
    border-bottom: 1px solid var(--app-border);
    flex-wrap: nowrap;
}

/* ---------- Layout: sidebar + main ---------- */
.app-wrapper {
    display: flex;
    min-height: calc(100vh - 56px);
}

/* Bootstrap makes .offcanvas-lg transparent on large screens, so !important is needed */
.app-sidebar.offcanvas-lg {
    background-color: var(--app-sidebar-bg) !important;
}

@media (min-width: 992px) {
    .app-sidebar {
        width: 260px;
        flex-shrink: 0;
        position: sticky;
        top: 56px;
        height: calc(100vh - 56px);
        overflow-y: auto;
        border-right: 1px solid var(--app-border);
    }
}

.app-main {
    flex: 1 1 auto;
    min-width: 0; /* lets wide tables scroll instead of stretching the page */
    padding: 1rem;
    display: flex;
    flex-direction: column;
}

@media (min-width: 992px) {
    .app-main { padding: 1.5rem 2rem; }
}

.app-footer {
    margin-top: auto;
    padding-top: 1.5rem;
}

/* ---------- Sidebar menu ---------- */
.sidebar-nav { padding-bottom: 1rem; }

.sidebar-heading {
    padding: 1rem 1rem .35rem;
    font-size: .72rem;
    font-weight: 600;
    letter-spacing: .06em;
    text-transform: uppercase;
    color: var(--app-sidebar-heading);
    list-style: none;
}

.sidebar-nav .nav-link {
    display: flex;
    align-items: center;
    gap: .75rem;
    margin: 1px .5rem;
    padding: .5rem .75rem;
    border-radius: .5rem;
    color: var(--app-sidebar-link);
    font-size: .925rem;
}

.sidebar-nav .nav-link .bi { font-size: 1.05rem; }

.sidebar-nav .nav-link:hover:not(.disabled) {
    background-color: var(--app-sidebar-hover-bg);
}

.sidebar-nav .nav-link.active {
    background-color: var(--app-sidebar-active-bg);
    color: var(--app-sidebar-active-color);
    font-weight: 600;
}

.sidebar-nav .nav-link.disabled {
    opacity: .55;
}

.sidebar-nav .nav-link .badge { font-size: .65rem; }

/* ---------- Auth pages (login / forced password change) ---------- */
.auth-main {
    min-height: 100vh;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-direction: column;
    padding: 1rem;
}

.auth-footer { margin-top: 1.5rem; max-width: 640px; }

.auth-card {
    width: 100%;
    max-width: 420px;
}

.auth-logo {
    width: 56px;
    height: 56px;
    border-radius: 14px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    font-size: 1.75rem;
    background-color: var(--app-sidebar-active-bg);
    color: var(--app-sidebar-active-color);
}

/* ---------- Dashboard ---------- */
.stat-card .stat-icon {
    width: 48px;
    height: 48px;
    border-radius: 12px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    font-size: 1.4rem;
    flex-shrink: 0;
}

.stat-card .stat-value {
    font-size: 1.6rem;
    font-weight: 700;
    line-height: 1.1;
}

.chart-box {
    position: relative;
    height: 260px;
}

/* Step 14 dashboards */
.stat-card .stat-icon.stat-icon-sm {
    width: 36px;
    height: 36px;
    border-radius: 10px;
    font-size: 1.05rem;
}
.stat-card .stat-value { font-size: 1.35rem; }
a.stat-card:hover,
a.stat-card:focus-visible { box-shadow: 0 0 0 2px var(--bs-primary), var(--app-shadow); }

/* Friendly empty state inside a chart/table card */
.dash-empty {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: .35rem;
    min-height: 140px;
    padding: 1rem;
    text-align: center;
    font-size: .875rem;
    color: var(--bs-secondary-color);
    border: 1px dashed var(--bs-border-color);
    border-radius: .5rem;
}
.dash-empty .bi { font-size: 1.6rem; opacity: .7; }

/* Coverage heatmap: cells carry inline fills (one sequential hue) + the count */
.dash-heat td, .dash-heat th { border-color: var(--bs-border-color); }
.dash-heat .tos-period-row th { background: var(--bs-tertiary-bg); font-weight: 600; }
.dash-swatch {
    display: inline-block;
    width: 1.25rem;
    height: .8rem;
    border-radius: 2px;
    border: 1px solid var(--bs-border-color);
}

/* Clickable subject cards (Question Bank) */
.subject-card {
    transition: box-shadow .15s ease, transform .15s ease;
}

.subject-card:hover,
.subject-card:focus-visible {
    box-shadow: 0 0 0 2px var(--bs-primary), var(--app-shadow);
    transform: translateY(-1px);
}

/* Row opened from a link like #req-12 (Topic Requests -> Teachers & Subjects) */
tr:target > td {
    box-shadow: inset 0 0 0 9999px rgba(var(--bs-primary-rgb), .15);
}
tr[id] {
    scroll-margin-top: 80px;
}

/* ---------- Create Test wizard stepper ---------- */
.wizard-steps .wizard-link {
    display: flex;
    align-items: center;
    gap: .5rem;
    padding: .3rem .8rem .3rem .35rem;
    border: 1px solid var(--app-border);
    border-radius: 2rem;
    color: var(--bs-body-color);
    font-size: .9rem;
    text-decoration: none;
}
a.wizard-link:hover { background-color: var(--app-sidebar-hover-bg); }
.wizard-num {
    width: 1.6rem;
    height: 1.6rem;
    border-radius: 50%;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    font-size: .8rem;
    font-weight: 600;
    background-color: var(--app-surface-2);
    border: 1px solid var(--app-border);
}
.wizard-step.is-current .wizard-link {
    background-color: var(--app-sidebar-active-bg);
    color: var(--app-sidebar-active-color);
    border-color: var(--bs-primary);
    font-weight: 600;
}
.wizard-step.is-current .wizard-num { background-color: var(--bs-primary); border-color: var(--bs-primary); color: #fff; }
.wizard-step.is-done .wizard-num { background-color: var(--bs-success); border-color: var(--bs-success); color: #fff; }
.wizard-step.is-locked .wizard-link { opacity: .55; cursor: not-allowed; }

/* ---------- Template editor ---------- */
.rich-editor {
    min-height: 7rem;
    max-height: 18rem;
    overflow-y: auto;
}
.rich-editor p, .rich-editor ul, .rich-editor ol { margin-bottom: .35rem; }
.js-sortable li[draggable="true"] { cursor: grab; }
.js-sortable li.is-dragging { opacity: .5; background-color: var(--app-sidebar-active-bg); }
.drag-handle { cursor: grab; }
.tpl-preview-frame {
    display: block;
    width: 100%;
    min-height: 480px;
    border: 0;
    border-radius: 0 0 var(--bs-border-radius) var(--bs-border-radius);
    background: #d9dde3; /* the paper preview is always light */
}
@media (min-width: 1200px) {
    .tpl-preview-card { position: sticky; top: 72px; max-height: calc(100vh - 88px); display: flex; flex-direction: column; }
    .tpl-preview-card .card-body { overflow-y: auto; }
}

/* ---------- TOS table ---------- */
.tos-table .tos-cell {
    width: 4.25rem;
    padding: .15rem .25rem;
}
.tos-table .tos-cell.is-manual {
    border-color: var(--bs-warning);
    background-color: var(--bs-warning-bg-subtle);
    color: var(--bs-warning-text-emphasis);
    font-weight: 600;
}
/* Grading-period groups: header row + subtotal row (theme-aware) */
.tos-table .tos-period-row th {
    background-color: var(--bs-tertiary-bg);
    color: var(--bs-emphasis-color);
}
.tos-table .tos-subtotal-row td {
    background-color: var(--bs-secondary-bg);
    font-style: italic;
}

/* ---------- DataTables (Bootstrap 5 styling) ---------- */
div.dt-container,
div.dataTables_wrapper {
    color: var(--bs-body-color);
}

table.dataTable {
    --bs-table-bg: var(--app-surface);
    --bs-table-border-color: var(--app-border);
}

div.dt-container .dt-input,
div.dataTables_wrapper .form-control,
div.dataTables_wrapper .form-select {
    background-color: var(--bs-body-bg);
    color: var(--bs-body-color);
    border-color: var(--app-border);
}

/* ---------- SweetAlert2 ---------- */
.swal2-popup {
    background: var(--app-surface) !important;
    color: var(--bs-body-color) !important;
    border: 1px solid var(--app-border);
}

.swal2-title,
.swal2-html-container {
    color: var(--bs-body-color) !important;
}

.swal2-input,
.swal2-textarea,
.swal2-select {
    background: var(--bs-body-bg) !important;
    color: var(--bs-body-color) !important;
    border-color: var(--app-border) !important;
}

/* ---------- Scan & Check: corrected sheet with ✔ / ✘ ---------- */
.scan-sheet { position: relative; background: #fff; }
.scan-sheet img { display: block; width: 100%; height: auto; }
.scan-mark {
    position: absolute; transform: translateY(-15%);
    min-width: 1.35em; padding: 0 .2em; border-radius: .3em;
    font-size: clamp(.6rem, 1.6vw, .85rem); font-weight: 700; line-height: 1.35; text-align: center;
    color: #fff; box-shadow: 0 0 0 1px rgba(0,0,0,.25);
}
.scan-mark-ok   { background: #198754; }
.scan-mark-bad  { background: #dc3545; }
.scan-mark-flag { background: #ffc107; color: #000; }
.scan-items td, .scan-items th { vertical-align: top; }
.scan-items .scan-key { min-width: 9rem; max-width: 14rem; overflow-wrap: break-word; }
/* flagged rows: theme-aware (Bootstrap's .table-warning stays light in dark mode) */
tr.scan-flag-row > * {
    --bs-table-bg: var(--bs-warning-bg-subtle);
    --bs-table-color: var(--bs-emphasis-color);
}
tr.chk-key-row > * {                                   /* the answer key in evidence tables */
    --bs-table-bg: var(--bs-success-bg-subtle);
    --bs-table-color: var(--bs-emphasis-color);
}
.scan-items tr.scan-flag-row > th:first-child { box-shadow: inset 4px 0 0 var(--bs-warning); }

/* ---------- Policy pages and footer links ---------- */
.policy-links a { color: inherit; text-decoration: none; white-space: nowrap; }
.policy-links a:hover, .policy-links a:focus-visible { text-decoration: underline; }
.policy-links { line-height: 1.9; }
.public-main { max-width: 960px; }
.policy-doc { max-width: 820px; line-height: 1.65; }
.policy-doc h2 { font-size: 1.3rem; margin-top: 2rem; }
.policy-doc h3 { font-size: 1.1rem; margin-top: 1.5rem; }
.policy-doc .table { font-size: .9rem; }
.policy-quote { border-left: 4px solid var(--app-border, var(--bs-border-color)); padding: .25rem 1rem; color: var(--bs-secondary-color); }
.policy-placeholder {
    background-color: var(--bs-warning-bg-subtle);
    color: var(--bs-warning-text-emphasis);
    border: 1px dashed var(--bs-warning-border-subtle);
    border-radius: .25rem;
    padding: 0 .3rem;
}
.policy-draft-print { display: none; }

/* ---------- Help → Product Requirements Document (modules/help/prd.php) ---------- */
.prd-doc { max-width: none; }
.prd-doc h2, .prd-doc h3 { scroll-margin-top: 80px; }
.prd-doc h2 { border-bottom: 1px solid var(--bs-border-color); padding-bottom: .35rem; }
.prd-toc { position: sticky; top: 72px; }
.prd-toc a { display: block; padding: .15rem 0; color: var(--bs-secondary-color); text-decoration: none; }
.prd-toc a:hover, .prd-toc a:focus-visible { color: var(--bs-link-color); text-decoration: underline; }
.prd-meta th { font-weight: 600; padding-right: 1.5rem; white-space: nowrap; }
.prd-req .prd-id { width: 6.5rem; }
.prd-req .prd-pri { width: 5.5rem; }
.prd-pri-must   { background: var(--bs-primary-bg-subtle);   color: var(--bs-primary-text-emphasis); }
.prd-pri-should { background: var(--bs-info-bg-subtle);      color: var(--bs-info-text-emphasis); }
.prd-pri-could  { background: var(--bs-secondary-bg-subtle); color: var(--bs-secondary-text-emphasis); }
.prd-flow li { margin-bottom: .35rem; }
.prd-glossary dt { font-weight: 600; }
@media (max-width: 991.98px) { .prd-toc { position: static; } }
/* TRD architecture diagram (modules/help/trd.php) */
.trd-diagram { display: flex; align-items: center; gap: .75rem; margin: 1rem 0; flex-wrap: wrap; }
.trd-box { border: 1px solid var(--bs-border-color); border-radius: .5rem; padding: .6rem .8rem; background: var(--bs-body-bg); flex: 1 1 160px; line-height: 1.4; }
.trd-box-main { border-color: var(--bs-primary-border-subtle); background: var(--bs-primary-bg-subtle); }
.trd-stack { display: flex; flex-direction: column; gap: .5rem; flex: 1 1 200px; }
.trd-stack .trd-box { flex: 0 0 auto; }
.trd-arrow { font-size: .75rem; text-align: center; color: var(--bs-secondary-color); flex: 0 0 auto; }
@media (max-width: 767.98px) {
    .trd-diagram { flex-direction: column; align-items: stretch; }
    .trd-diagram > .trd-box, .trd-stack { flex: 0 0 auto; }
    .trd-arrow-glyph { display: inline-block; transform: rotate(90deg); }
}
@media print {
    .prd-doc, .prd-doc * { color: #000 !important; background: #fff !important; border-color: #888 !important; }
    .prd-doc .badge { border: 1px solid #888; }
    .prd-doc h2 { break-after: avoid; }
    .prd-doc tr { break-inside: avoid; }
}

/* ---------- Printing from the browser: always light ---------- */
@media print {
    :root { color-scheme: light; }
    body { background: #fff !important; color: #000 !important; }
    .app-navbar, .app-sidebar, .app-footer, .theme-toggle, .no-print { display: none !important; }
    .app-main { padding: 0 !important; }
    .card { box-shadow: none !important; }
    .public-main { max-width: none; padding: 0 !important; }
    .policy-doc, .policy-doc * { color: #000 !important; background: #fff !important; border-color: #888 !important; }
    .policy-draft-print { display: block; border: 2px solid #000; padding: .4rem .6rem; }
    .policy-doc a { text-decoration: none; }
}

/* ---------------------------------------------------------------
   Live camera with auto-capture (assets/js/scan_camera.js).
   A camera viewfinder is always dark, in both themes.
   --------------------------------------------------------------- */
html.scan-cam-open { overflow: hidden; }
.scan-cam {
    position: fixed; inset: 0; z-index: 2000;
    display: none; flex-direction: column;
    background: #000; color: #fff;
}
.scan-cam.show { display: flex; }
.scan-cam-stage { position: relative; flex: 1 1 auto; min-height: 0; }
.scan-cam-stage video,
.scan-cam-overlay { position: absolute; inset: 0; width: 100%; height: 100%; }
.scan-cam-stage video { object-fit: contain; background: #000; }
.scan-cam-status {
    padding: .5rem 1rem; text-align: center; font-size: .95rem;
    background: rgba(0, 0, 0, .85); min-height: 2.75rem;
}
.scan-cam-bar {
    display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: .5rem;
    padding: .75rem 1rem calc(.75rem + env(safe-area-inset-bottom));
    background: #111;
}
.scan-cam-bar .form-check-label { color: #fff; }
.scan-cam-bar [data-act="torch"].active { background: #fff; color: #000; }
