/* ContextGrade — Modal system (DS-04).

   One CSS block for every window. Section 1 applies to every dbc.Modal in the
   app, migrated or not; everything from section 3 on is scoped to `.cg-modal`,
   the class utils/modal.py::cg_modal puts on the OUTER .modal element
   (dbc 2.x `class_name`), with the shape as a second class: `.cg-confirm`,
   `.cg-dialog`, `.cg-workspace`, `.cg-sheet`. Sections are numbered as in the
   design study; the legacy width list that used to be section 2 is gone.

   Two facts every rule here respects:

   - Dash 4's dcc.Dropdown menu is a position:fixed popover. A fixed element
     is NOT clipped by an `overflow: auto` body, so a scrolling body is safe
     for dropdowns - but it IS re-anchored by any ancestor that forms a
     containing block (transform, filter, backdrop-filter). That is why no
     modal surface may keep a backdrop-filter, and why every open-state rule
     below ends at `transform: none`. Never "fix" a dropdown by flipping
     .modal-body to overflow: visible - that destroys the scroll container.
   - Specificity: `.cg-x .modal-dialog` (0,2,0) beats Bootstrap's own
     `@media (min-width: 576px) .modal-dialog { max-width }` (0,1,0) at every
     width, so the shapes need no !important. */

/* ---------- 1. Every window: fit the viewport, one surface ---------- */

.modal-content {
    background: var(--surface);
    backdrop-filter: none;
    border: 1px solid var(--glass-border);
    border-radius: 12px;
    box-shadow: var(--shadow-modal);
    color: var(--text-main);
    max-height: calc(100vh - 3.5rem);
    max-height: calc(100dvh - 3.5rem);
}

/* Bootstrap already lays .modal-content out as a flex column whose header and
   footer do not shrink; giving the body the overflow is all it takes for the
   title and the buttons to stay put while only the middle scrolls. */
.modal-body {
    overflow-y: auto;
    min-height: 0;
}

.modal-header {
    border-bottom: 1px solid var(--glass-border);
}

.modal-footer {
    border-top: 1px solid var(--glass-border);
}

.modal-title {
    color: var(--accent-secondary);
    font-weight: 700;
}

/* ---------- 3. Shapes ---------- */

.cg-confirm .modal-dialog {
    max-width: 420px;
}

.cg-dialog .modal-dialog {
    max-width: 640px;
}

.cg-workspace .modal-dialog {
    max-width: min(1120px, 100vw - 32px);
}

/* Side sheet: pinned to the right edge, full height, the page readable beside
   it under a light scrim. */
.cg-sheet .modal-dialog {
    position: absolute;
    inset: 0 0 0 auto;
    margin: 0;
    width: min(440px, 100%);
    max-width: none;
    height: 100%;
}

.cg-sheet .modal-content {
    height: 100%;
    max-height: none;
    border-radius: 12px 0 0 12px;
    border-right: 0;
}

.cg-sheet.fade .modal-dialog {
    transform: translateX(24px);
}

.cg-sheet.show .modal-dialog {
    transform: none;
}

.modal-backdrop.cg-backdrop {
    --bs-backdrop-opacity: 1;
    background-color: var(--scrim);
}

.modal-backdrop.cg-backdrop-light {
    background-color: var(--scrim-light);
}

/* ---------- 4. Chrome (.cg-modal only) ---------- */

.cg-modal .modal-header {
    min-height: 48px;
    padding: 0 8px 0 20px;
    align-items: center;
    gap: 8px;
}

.cg-modal .modal-header > * {
    flex: none;
}

.cg-modal .modal-title {
    flex: 1 1 auto;
    min-width: 0;
    font-size: 15px;
    font-weight: 650;
    line-height: 1.3;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.cg-ctx {
    margin-left: 8px;
    font-size: 13px;
    font-weight: 400;
    color: var(--text-muted);
}

.cg-ctx::before {
    content: "\00B7";
    margin-right: 8px;
    opacity: 0.6;
}

.cg-modal .modal-body {
    padding: 16px 20px;
}

.cg-modal .modal-footer {
    min-height: 56px;
    padding: 10px 16px;
    gap: 8px;
    flex-wrap: wrap;
    justify-content: flex-end;
}

.cg-modal .modal-footer > * {
    margin: 0;
}

/* Destructive action and any extra control sit on the left; the actions on
   the right end with the one primary button. */
.cg-foot-left {
    margin-right: auto;
    display: flex;
    align-items: center;
    gap: 8px;
    flex-wrap: wrap;
}

/* Confirm: one question, two answers - no header band, no footer border. */
.cg-confirm .modal-body {
    padding: 22px 20px 6px;
}

.cg-confirm .modal-footer {
    border-top: 0;
    min-height: 0;
    padding: 14px 16px 16px;
}

.cg-s-wrap {
    display: flex;
    gap: 14px;
}

.cg-s-ic {
    flex: none;
    width: 36px;
    height: 36px;
    border-radius: 50%;
    background: var(--danger-soft);
    color: var(--danger);
    border: 1px solid var(--danger-line);
    display: grid;
    place-items: center;
    font-size: 15px;
}

.cg-s-title {
    font-size: 15.5px;
    font-weight: 650;
    color: var(--text-main);
    margin: 0 0 4px;
}

.cg-s-body {
    min-width: 0;
    flex: 1 1 auto;
}

.cg-s-body > .cg-s-text + * {
    margin-top: 12px;
}

.cg-s-text {
    font-size: 13.5px;
    color: var(--text-muted);
    line-height: 1.5;
    margin: 0;
}

/* The X: a quiet 32px square that only shows its ground on hover. */
.cg-modal .modal-header .btn-close {
    margin: 0;
    padding: 8px;
    width: 32px;
    height: 32px;
    border-radius: 8px;
    background-size: 12px;
    opacity: 0.6;
}

.cg-modal .modal-header .btn-close:hover {
    background-color: var(--glass-bg);
    opacity: 1;
}

/* Controls inside a window share one rhythm: 34px tall, soft radii, the
   accent as the focus ring. Scoped so legacy windows keep the page styling
   until they migrate. */
.cg-modal .modal-body .form-control,
.cg-modal .modal-body .form-select,
.cg-modal .modal-body input.results-grade-input {
    min-height: 34px;
    border-radius: 7px;
    font-size: 14px;
}

.cg-modal .modal-body .form-control:focus,
.cg-modal .modal-body .form-select:focus {
    border-color: var(--accent-primary);
    box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent-primary) 20%, transparent);
}

.cg-modal .modal-body textarea.form-control {
    min-height: 90px;
}

.cg-modal .modal-footer .btn,
.cg-modal .modal-body .btn:not(.btn-sm, .btn-xs, .btn-link) {
    min-height: 34px;
    padding: 0 14px;
    border-radius: 8px;
    font-size: 13px;
    font-weight: 600;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 6px;
    white-space: nowrap;
}

/* A destructive action inside a window is quiet until it is the question. */
.cg-modal .cg-foot-left .btn-danger,
.cg-modal .modal-body .cg-btn-danger-outline {
    background: transparent;
    color: var(--danger);
    border: 1px solid var(--danger-line);
}

.cg-modal .cg-foot-left .btn-danger:hover,
.cg-modal .modal-body .cg-btn-danger-outline:hover {
    background: var(--danger-soft);
}

/* Open animations: a short pop for dialogs and confirms, a slide-in for the
   sheet, a slide-up for phones - all ending at transform: none (see the
   dropdown note at the top). Bootstrap's own reduced-motion rule turns the
   transition off. */
.cg-modal.fade:not(.cg-sheet) .modal-dialog {
    transform: translateY(-8px);
    transition: transform 0.16s ease-out;
}

.cg-modal.show:not(.cg-sheet) .modal-dialog {
    transform: none;
}

.cg-sheet.fade .modal-dialog {
    transition-duration: 0.2s;
}

/* ---------- 5. Field grid and two panes ---------- */

.cg-fg {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(170px, 1fr));
    gap: 12px 14px;
}

.cg-fg + .cg-fg,
.cg-fg + .cg-legend,
.cg-legend + .cg-fg {
    margin-top: 14px;
}

.cg-f {
    display: flex;
    flex-direction: column;
    gap: 4px;
    min-width: 0;
}

.cg-f > label {
    font-size: 12px;
    font-weight: 550;
    color: var(--text-muted);
    margin: 0;
}

.cg-span {
    grid-column: 1 / -1;
}

.cg-hint {
    font-size: 12px;
    color: var(--text-muted);
}

.cg-legend {
    display: flex;
    align-items: center;
    gap: 10px;
    font-size: 11px;
    letter-spacing: 0.07em;
    text-transform: uppercase;
    font-weight: 600;
    color: var(--text-muted);
}

.cg-legend::after {
    content: "";
    flex: 1;
    height: 1px;
    background: var(--glass-border);
}

.cg-legend-danger {
    color: var(--danger);
}

.cg-ws {
    display: grid;
    grid-template-columns: minmax(0, 1.1fr) minmax(0, 1fr);
    gap: 20px;
}

.cg-ws-main {
    min-width: 0;
}

.cg-ws-side {
    display: flex;
    flex-direction: column;
    gap: 10px;
    padding-left: 20px;
    border-left: 1px solid var(--glass-border);
    min-width: 0;
}

/* Radio cards: two or three choices that need a sentence each (the copy-setup
   window's "add what is missing" vs "replace completely"). */
.cg-rc {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
    gap: 10px;
}

.cg-rc label {
    display: flex;
    gap: 10px;
    align-items: flex-start;
    padding: 10px 12px;
    border: 1px solid var(--glass-border);
    border-radius: 9px;
    cursor: pointer;
    font-size: 13px;
    line-height: 1.4;
    background: var(--surface);
    margin: 0;
}

.cg-rc label:has(input:checked) {
    border-color: var(--accent-primary);
    background: color-mix(in srgb, var(--accent-primary) 12%, transparent);
}

.cg-rc input {
    margin: 2px 0 0;
    accent-color: var(--accent-primary);
}

.cg-rc small {
    display: block;
    color: var(--text-muted);
    font-size: 12px;
    margin-top: 2px;
}

/* The same cards built from dbc.RadioItems, which renders the input and the
   label as siblings inside .form-check rather than the input inside the label. */
.cg-rc.form-check-inline,
.cg-rc .form-check {
    display: flex;
    gap: 10px;
    align-items: flex-start;
    padding: 10px 12px 10px 34px;
    border: 1px solid var(--glass-border);
    border-radius: 9px;
    background: var(--surface);
    font-size: 13px;
    line-height: 1.4;
    margin: 0;
    position: relative;
}

.cg-rc .form-check .form-check-input {
    position: absolute;
    left: 12px;
    top: 12px;
    margin: 0;
    accent-color: var(--accent-primary);
}

.cg-rc .form-check:has(input:checked) {
    border-color: var(--accent-primary);
    background: color-mix(in srgb, var(--accent-primary) 12%, transparent);
}

.cg-rc .form-check-label {
    cursor: pointer;
    display: block;
    width: 100%;
}

.cg-rc .form-check-label small {
    display: block;
    color: var(--text-muted);
    font-size: 12px;
    margin-top: 2px;
}

/* Segmented control: a two-way switch that reads as one control (Weekly /
   One date), sized like the inputs beside it. */
.cg-seg {
    display: inline-flex;
    height: 34px;
    padding: 2px;
    border: 1px solid var(--glass-border);
    border-radius: 8px;
    background: var(--glass-bg);
}

.cg-modal .modal-body .cg-seg .btn {
    min-height: 28px;
    height: 28px;
    padding: 0 10px;
    border: 0;
    border-radius: 6px;
    background: transparent;
    color: var(--text-muted);
    font-size: 13px;
    font-weight: 500;
    box-shadow: none;
}

.cg-modal .modal-body .cg-seg .btn.active {
    background: var(--surface);
    color: var(--text-main);
    font-weight: 600;
    box-shadow: 0 1px 3px rgba(15, 23, 42, 0.14);
}

/* Tabs inside a pane (the event window's "Every lesson" / "Only this date"). */
.cg-tabs {
    display: flex;
    gap: 2px;
    border-bottom: 1px solid var(--glass-border);
}

.cg-modal .modal-body .cg-tabs .btn {
    min-height: 0;
    padding: 6px 10px 8px;
    border: 0;
    border-bottom: 2px solid transparent;
    border-radius: 0;
    background: none;
    color: var(--text-muted);
    font-size: 13px;
    font-weight: 500;
    margin-bottom: -1px;
    box-shadow: none;
}

.cg-modal .modal-body .cg-tabs .btn.active {
    border-bottom-color: var(--accent-primary);
    color: var(--text-main);
    font-weight: 600;
}

/* A small control and the button that acts on it, on one line. */
.cg-inline-row {
    display: flex;
    gap: 8px;
    align-items: flex-end;
}

.cg-inline-row > .cg-f {
    flex: 0 0 96px;
}

.cg-inline-row > .btn {
    flex: 1 1 auto;
}

/* A short description under the title, before the first field. */
.cg-desc {
    font-size: 13px;
    color: var(--text-muted);
    line-height: 1.5;
    margin: 0;
}

/* ---------- 6. Inline confirm strip ----------
   Mounted hidden inside the footer by utils/modal.py::build_confirm_strip and
   shown by a callback writing `hidden=False`. While it shows, the footer turns
   red-tinted and the normal actions disappear, so the question is answered
   next to the thing it changes instead of in a second window. React omits the
   attribute entirely for hidden=False, which is what makes :not([hidden])
   reliable - never switch this to style.display. */
.cg-confirm-strip {
    display: flex;
    flex: 1 1 100%;
    align-items: center;
    gap: 8px;
    flex-wrap: wrap;
}

.cg-cs-text {
    flex: 1 1 220px;
    font-size: 13px;
    line-height: 1.4;
}

.modal-footer:has(.cg-confirm-strip:not([hidden])) {
    background: var(--danger-soft);
    border-top-color: var(--danger-line);
}

.modal-footer:has(.cg-confirm-strip:not([hidden])) > :not(.cg-confirm-strip) {
    display: none;
}

/* The dirty-form guard's refusal (assets/18_modal_guard.js): a short shake of
   the footer when Esc or a scrim click would have thrown away edits. */
.cg-guard-flash {
    animation: cg-guard-shake 0.4s ease-out;
}

@keyframes cg-guard-shake {
    0%, 100% { transform: translateX(0); }
    20% { transform: translateX(-4px); }
    40% { transform: translateX(4px); }
    60% { transform: translateX(-3px); }
    80% { transform: translateX(3px); }
}

@media (prefers-reduced-motion: reduce) {
    .cg-guard-flash {
        animation: none;
        background: var(--danger-soft);
    }
}

/* ---------- 7. Phones: every shape becomes a bottom sheet ----------
   640px is the app's own phone cut (the calendar drawer flips to a bottom
   sheet there too, assets/04_calendar.css), not Bootstrap's 576, so a window
   and the drawer change behaviour at the same width. */
@media (max-width: 640px) {
    .cg-modal .modal-dialog,
    .cg-sheet .modal-dialog {
        position: absolute;
        inset: auto 0 0 0;
        margin: 0;
        width: 100%;
        max-width: none;
        height: auto;
        min-height: 0;
        max-height: 92vh;
        max-height: 92dvh;
    }

    .cg-modal .modal-content {
        height: auto;
        max-height: 92vh;
        max-height: 92dvh;
        border-radius: 16px 16px 0 0;
        border-right: 1px solid var(--glass-border);
        border-bottom: 0;
    }

    .cg-modal .modal-content::before {
        content: "";
        position: absolute;
        top: 6px;
        left: 50%;
        width: 36px;
        height: 4px;
        margin-left: -18px;
        border-radius: 2px;
        background: var(--glass-border);
    }

    .cg-modal .modal-header {
        padding-top: 8px;
        min-height: 54px;
    }

    .cg-confirm .modal-body {
        padding-top: 26px;
    }

    .cg-modal.fade .modal-dialog {
        transform: translateY(24px);
    }

    .cg-modal.show .modal-dialog {
        transform: none;
    }

    .cg-ws {
        grid-template-columns: 1fr;
    }

    .cg-ws-side {
        border-left: 0;
        padding-left: 0;
        border-top: 1px solid var(--glass-border);
        padding-top: 14px;
    }
}
