/*
 * Apply It — the student player (apply-it-player.js) and the teacher editor.
 * Tokens live on :root (not on a page shell) because _PortalLayout moves every
 * .modal to <body>, where shell-scoped tokens stop resolving. The Digital Work
 * page (Views/Work/Index.cshtml) re-points these tokens at its own, which follow
 * the device's light/dark setting.
 */
:root {
    --ai-ink: #111827;
    --ai-muted: #4b5563;
    --ai-surface: #ffffff;
    --ai-surface-2: #f8fafc;
    --ai-border: rgba(15, 23, 42, 0.14);
    --ai-strong-border: rgba(15, 23, 42, 0.32);
    --ai-accent: #2563eb;
    --ai-accent-soft: rgba(37, 99, 235, 0.09);
    --ai-head: #eef2f7;
    --ai-given: #f1f5f9;
    --ai-right: #dcfce7;
    --ai-right-ink: #166534;
    --ai-wrong: #fee2e2;
    --ai-wrong-ink: #991b1b;
    --ai-arrow: #334155;
    --ai-warn-bg: #fffbeb;
    --ai-warn-border: #f59e0b;
    --ai-warn-ink: #78350f;
}

[data-theme="dark"] {
    --ai-ink: #e8e8e8;
    --ai-muted: #a5a5a5;
    --ai-surface: #1b1b1b;
    --ai-surface-2: #141414;
    --ai-border: rgba(255, 255, 255, 0.12);
    --ai-strong-border: rgba(255, 255, 255, 0.3);
    --ai-accent: #6b93ff;
    --ai-accent-soft: rgba(107, 147, 255, 0.14);
    --ai-head: #282828;
    --ai-given: #232323;
    --ai-right: #14532d;
    --ai-right-ink: #bbf7d0;
    --ai-wrong: #7f1d1d;
    --ai-wrong-ink: #fecaca;
    --ai-arrow: #d6d6d6;
    --ai-warn-bg: rgba(245, 158, 11, 0.12);
    --ai-warn-border: #f59e0b;
    --ai-warn-ink: #fde68a;
}

/* ── Player ────────────────────────────────────────────────────────── */
.ai-player { color: var(--ai-ink); }
.ai-player *, .ai-player *::before, .ai-player *::after { box-sizing: border-box; }

.ai-card {
    background: var(--ai-surface);
    border: 1px solid var(--ai-border);
    border-radius: 12px;
    padding: 18px 20px;
    margin-bottom: 16px;
}

.ai-label { font-weight: 700; margin: 0 0 6px; }
.ai-brief p { margin: 0 0 6px; }
.ai-brief ul { margin: 0; padding-left: 20px; }
.ai-reading summary { cursor: pointer; font-weight: 700; }
.ai-reading p { margin: 10px 0 0; }

.ai-sound {
    display: inline-block;
    padding: 6px 12px;
    border: 1px solid var(--ai-border);
    border-radius: 999px;
    background: var(--ai-surface);
    color: var(--ai-muted);
    font: inherit;
    font-size: 0.85rem;
    cursor: pointer;
}
.ai-sound:hover { color: var(--ai-ink); }
.ai-toolbar { display: flex; justify-content: flex-end; margin-bottom: 10px; }

.ai-problem-head { display: flex; flex-wrap: wrap; align-items: baseline; gap: 4px 10px; margin-bottom: 6px; }
.ai-problem-num { font-size: 0.78rem; font-weight: 700; letter-spacing: 0.04em; color: var(--ai-muted); text-transform: uppercase; }
.ai-problem-title { font-size: 1.12rem; font-weight: 700; margin: 0; }
.ai-scenario { margin: 0 0 8px; }
.ai-prompt { font-weight: 600; margin: 0 0 12px; }
.ai-caption { color: var(--ai-muted); font-size: 0.86rem; margin: 0 0 8px; }
.ai-tally { margin-left: auto; font-size: 0.82rem; font-weight: 700; padding: 2px 10px; border-radius: 999px; background: var(--ai-accent-soft); color: var(--ai-accent); }
.ai-tally.is-done { background: var(--ai-right); color: var(--ai-right-ink); }

/* Inputs */
.ai-input {
    width: 100%;
    min-width: 4.5em;
    padding: 7px 8px;
    border: 1.5px dashed var(--ai-strong-border);
    border-radius: 8px;
    background: var(--ai-surface);
    color: var(--ai-ink);
    font: inherit;
    text-align: center;
}
.ai-input:focus { outline: 2px solid var(--ai-accent); outline-offset: 1px; border-style: solid; }
.ai-input.is-filled { border-style: solid; border-color: var(--ai-accent); background: var(--ai-accent-soft); }
.ai-input.is-right { border-style: solid; border-color: var(--ai-right-ink); background: var(--ai-right); color: var(--ai-right-ink); opacity: 1; }
.ai-input.is-wrong { border-style: solid; border-color: var(--ai-wrong-ink); background: var(--ai-wrong); color: var(--ai-wrong-ink); opacity: 1; }
select.ai-input { text-align: left; }
.ai-mark { font-weight: 700; margin-left: 4px; }
.ai-mark.correct { color: var(--ai-right-ink); }
.ai-mark.wrong { color: var(--ai-wrong-ink); }

/* Tables */
.ai-scroll { overflow-x: auto; margin-bottom: 12px; }
.ai-grid { border-collapse: separate; border-spacing: 0; min-width: 100%; }
.ai-grid td, .ai-grid th {
    border: 1px solid var(--ai-border);
    padding: 6px;
    vertical-align: middle;
    min-width: 5.5em;
}
.ai-grid th { background: var(--ai-head); font-weight: 700; text-align: center; }
.ai-grid td.is-given { background: var(--ai-given); text-align: center; }
.ai-grid.is-punnett { min-width: 0; width: auto; }
.ai-grid.is-punnett td, .ai-grid.is-punnett th { width: 5.2em; min-width: 4.2em; height: 3.4em; text-align: center; }
.ai-grid.is-punnett tr:first-child td, .ai-grid.is-punnett td:first-child { background: var(--ai-head); }
.ai-grid.is-punnett .ai-corner { background: transparent; border-color: transparent; }
.ai-grid.is-punnett .ai-input { font-weight: 700; letter-spacing: 0.04em; }

/* Step-by-step chain */
.ai-steps { margin-bottom: 12px; }
.ai-step { border: 1px solid var(--ai-border); border-radius: 10px; padding: 10px 12px; background: var(--ai-surface-2); }
.ai-step-label { font-weight: 700; font-size: 0.92rem; margin-bottom: 6px; }
.ai-step-cells { display: flex; flex-wrap: wrap; gap: 6px; }
.ai-step-cells > .ai-box { flex: 0 1 6.5em; min-width: 4.5em; }
.ai-step-cells > .ai-box.is-wide { flex: 1 1 12em; }
.ai-box-given { display: block; padding: 7px 8px; border-radius: 8px; background: var(--ai-given); border: 1px solid var(--ai-border); text-align: center; font-weight: 600; letter-spacing: 0.03em; }
.ai-box-unit { display: block; font-size: 0.78rem; color: var(--ai-muted); text-align: center; margin-top: 2px; }
.ai-step-arrow { display: flex; align-items: center; gap: 8px; padding: 4px 0 4px 18px; color: var(--ai-muted); font-size: 0.86rem; }
.ai-step-arrow::before { content: "\2193"; font-size: 1.25rem; font-weight: 700; color: var(--ai-arrow); }
.ai-step-gap { height: 12px; }

/* "Now answer" boxes */
.ai-blanks { margin-bottom: 12px; }
.ai-blanks-h { font-weight: 700; font-size: 0.92rem; margin-bottom: 6px; }
.ai-blank { display: flex; flex-wrap: wrap; align-items: center; gap: 6px 12px; padding: 8px 10px; border: 1px solid var(--ai-border); border-radius: 9px; margin-bottom: 6px; }
.ai-blank-label { flex: 1 1 240px; min-width: 0; }
.ai-blank .ai-box { flex: 0 1 12em; min-width: 6em; }

/* Codon chart */
.ai-codon-wrap { margin-bottom: 12px; }
.ai-codon-wrap summary { cursor: pointer; font-weight: 700; margin-bottom: 6px; }
.ai-codon { border-collapse: collapse; font-size: 0.78rem; min-width: 100%; }
.ai-codon th, .ai-codon td { border: 1px solid var(--ai-border); padding: 2px 6px; text-align: center; white-space: nowrap; }
.ai-codon th { background: var(--ai-head); }
.ai-codon td.ai-codon-base { font-weight: 700; background: var(--ai-head); }
.ai-codon .ai-codon-stop { font-weight: 700; color: var(--ai-wrong-ink); }

/* Arrow diagrams */
.ai-connect { position: relative; margin-bottom: 10px; padding: 10px 6px; border: 1px solid var(--ai-border); border-radius: 10px; background: var(--ai-surface-2); }
.ai-connect-cols { position: relative; z-index: 1; display: grid; gap: 28px; align-items: center; }
.ai-connect-col { display: flex; flex-direction: column; gap: 14px; align-items: center; justify-content: center; }
.ai-node {
    max-width: 100%;
    padding: 9px 12px;
    border: 2px solid var(--ai-strong-border);
    border-radius: 10px;
    background: var(--ai-surface);
    color: var(--ai-ink);
    font: inherit;
    font-weight: 600;
    cursor: pointer;
    overflow-wrap: break-word;
    hyphens: auto;
}
.ai-node:hover { border-color: var(--ai-accent); }
.ai-node.is-from { border-color: var(--ai-accent); background: var(--ai-accent-soft); box-shadow: 0 0 0 3px var(--ai-accent-soft); }
.ai-node.is-right { border-color: var(--ai-right-ink); background: var(--ai-right); color: var(--ai-right-ink); }
.ai-node.is-wrong { border-color: var(--ai-wrong-ink); background: var(--ai-wrong); color: var(--ai-wrong-ink); }
.ai-node:disabled { cursor: default; opacity: 1; }
.ai-connect-svg { position: absolute; inset: 0; width: 100%; height: 100%; pointer-events: none; z-index: 0; overflow: visible; }
.ai-connect-svg path { fill: none; stroke: var(--ai-arrow); stroke-width: 2.2; }
.ai-connect-svg marker path { fill: var(--ai-arrow); stroke: none; }
.ai-connect-help { color: var(--ai-muted); font-size: 0.86rem; margin: 0 0 8px; }
.ai-connect-form { display: flex; flex-wrap: wrap; align-items: center; gap: 6px 8px; margin-bottom: 8px; }
.ai-connect-form select { flex: 1 1 9em; min-width: 0; padding: 6px 8px; border: 1px solid var(--ai-border); border-radius: 8px; background: var(--ai-surface); color: var(--ai-ink); font: inherit; }
.ai-link-list { list-style: none; padding: 0; margin: 0 0 10px; display: flex; flex-wrap: wrap; gap: 6px; }
.ai-link-list li { display: inline-flex; align-items: center; gap: 6px; padding: 3px 4px 3px 10px; border: 1px solid var(--ai-border); border-radius: 999px; font-size: 0.86rem; background: var(--ai-surface); }
.ai-link-list button { border: none; background: transparent; color: var(--ai-muted); cursor: pointer; font: inherit; padding: 0 6px; }
.ai-link-list button:hover { color: var(--ai-wrong-ink); }
.ai-btn-small { padding: 6px 12px; border: 1px solid var(--ai-accent); border-radius: 8px; background: var(--ai-accent); color: #fff; font: inherit; font-size: 0.88rem; cursor: pointer; }
.ai-btn-small:disabled { opacity: 0.55; cursor: not-allowed; }

/* Follow-up questions */
.ai-q { border-top: 1px solid var(--ai-border); padding-top: 12px; margin-top: 12px; }
.ai-q-text { font-weight: 600; margin-bottom: 8px; }
.ai-choice { display: flex; align-items: flex-start; gap: 10px; padding: 9px 12px; border: 1px solid var(--ai-border); border-radius: 9px; margin-bottom: 6px; cursor: pointer; }
.ai-choice:hover { border-color: var(--ai-accent); }
.ai-choice input { margin-top: 4px; flex-shrink: 0; }
.ai-choice.is-picked { border-color: var(--ai-accent); background: var(--ai-accent-soft); }
.ai-choice.is-right { border-color: var(--ai-right-ink); background: var(--ai-right); }
.ai-choice.is-wrong { border-color: var(--ai-wrong-ink); background: var(--ai-wrong); }
.ai-letter { font-weight: 700; color: var(--ai-muted); min-width: 1.2em; }

/* ── Teacher pages ─────────────────────────────────────────────────── */
.ai-shell { max-width: 1200px; margin: 0 auto; padding: 0 16px 48px; color: var(--ai-ink); }
.ai-page-head { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: 12px; padding: 18px 0; }
.ai-page-head h1 { font-size: 1.5rem; font-weight: 800; margin: 0; }
.ai-page-head p { margin: 2px 0 0; color: var(--ai-muted); }
.ai-icon { display: inline-flex; align-items: center; justify-content: center; width: 46px; height: 46px; border-radius: 12px; background: var(--ai-accent-soft); color: var(--ai-accent); font-size: 1.25rem; flex-shrink: 0; }
.ai-muted { color: var(--ai-muted); }
.ai-note { background: var(--ai-warn-bg); border: 1px solid var(--ai-warn-border); color: var(--ai-warn-ink); border-radius: 10px; padding: 10px 14px; }
.ai-btn-accent { background: var(--ai-accent); border-color: var(--ai-accent); color: #fff; }
.ai-btn-accent:hover { filter: brightness(1.08); color: #fff; }
.ai-kind-options { display: flex; flex-wrap: wrap; gap: 10px; }
.ai-kind-option { flex: 1 1 200px; border: 1px solid var(--ai-border); border-radius: 10px; padding: 10px 12px; background: var(--ai-surface-2); }
.ai-kind-option .form-check-label { font-weight: 700; }
.ai-kind-option small { display: block; color: var(--ai-muted); margin-top: 2px; }
.ai-gap-list { max-height: 220px; overflow-y: auto; border: 1px solid var(--ai-border); border-radius: 8px; padding: 8px 10px; }
.ai-gap-note { display: block; font-size: 0.8rem; color: var(--ai-muted); }

.ai-edit-problem { border: 1px solid var(--ai-border); border-radius: 12px; padding: 16px; margin-bottom: 16px; background: var(--ai-surface); }
.ai-edit-head { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; margin-bottom: 10px; }
.ai-edit-head h3 { font-size: 1.05rem; font-weight: 700; margin: 0; }
.ai-pill { display: inline-block; padding: 2px 10px; border-radius: 999px; font-size: 0.76rem; font-weight: 700; background: var(--ai-accent-soft); color: var(--ai-accent); }
.ai-edit-grid { border-collapse: collapse; }
.ai-edit-grid td { border: 1px solid var(--ai-border); padding: 4px; vertical-align: top; min-width: 8.5em; }
.ai-edit-cell { display: flex; flex-direction: column; gap: 3px; }
.ai-edit-cell.is-blank input[data-field="answer"] { border-style: dashed; border-color: var(--ai-accent); background: var(--ai-accent-soft); }
.ai-edit-cell .ai-cell-tools { display: flex; gap: 4px; align-items: center; font-size: 0.74rem; }
.ai-edit-cell .ai-cell-tools label { display: inline-flex; gap: 3px; align-items: center; margin: 0; }
.ai-edit-cell input[data-field="accepted"] { font-size: 0.76rem; }
.ai-edit-step { border: 1px solid var(--ai-border); border-radius: 10px; padding: 10px; margin-bottom: 8px; background: var(--ai-surface-2); }
.ai-edit-cells { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 6px; }
.ai-edit-cells .ai-edit-cell { flex: 0 1 10em; }
.ai-edit-q { border-top: 1px solid var(--ai-border); padding-top: 10px; margin-top: 10px; }
.ai-edit-q .input-group-text { min-width: 2.4em; justify-content: center; }
.ai-calc { border: 1px dashed var(--ai-accent); border-radius: 10px; padding: 10px 12px; background: var(--ai-accent-soft); margin-bottom: 10px; }
.ai-savebar { position: sticky; bottom: 0; z-index: 5; display: flex; justify-content: flex-end; align-items: center; gap: 12px; padding: 12px 0; background: var(--bs-body-bg, var(--ai-surface)); border-top: 1px solid var(--ai-border); margin-top: 16px; }

@media (max-width: 640px) {
    .ai-card { padding: 14px 14px; }
    .ai-connect-cols { gap: 12px; }
    .ai-connect { padding: 10px 4px; }
    .ai-node { padding: 6px 5px; font-size: 0.78rem; border-radius: 8px; }
}
