/* --------------------------------------------------------------------------
   Modal
   -------------------------------------------------------------------------- */

.overlay {
    position: fixed;
    inset: 0;
    background: rgba(13, 24, 54, 0.68);
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 1.25rem;
    z-index: 60;
}

.modal {
    background: var(--surface);
    border: 3px solid var(--violet);
    border-radius: var(--r-lg);
    box-shadow: var(--float);
    width: 100%;
    max-width: 480px;
    max-height: 90vh;
    overflow-y: auto;
    padding: 1.6rem;
}

/* Admin forms need more room than the feedback box. */
.modal--wide { max-width: 720px; }

.modal__head {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 1rem;
    padding-bottom: 0.85rem;
    border-bottom: 3px dotted var(--lav-200);
    margin-bottom: 1.15rem;
}

.modal__close {
    flex: none;
    width: 34px;
    height: 34px;
    border: 0;
    border-radius: 50%;
    background: var(--surface-2);
    color: var(--violet-ink);
    font-size: 1.15rem;
    font-weight: 800;
    line-height: 1;
    cursor: pointer;
}
.modal__close:hover { background: var(--coral); color: #fff; }

/* --------------------------------------------------------------------------
   Study helper bubble
   Sits above the full-screen overlays (z 40) but below the modals (z 60), so a
   dialog is never blocked by it.
   -------------------------------------------------------------------------- */

.ai {
    position: fixed;
    right: 18px;
    bottom: 18px;
    z-index: 55;
    display: flex;
    flex-direction: column;
    align-items: flex-end;
    gap: 0.6rem;
}

.ai__bubble {
    width: 58px;
    height: 58px;
    flex: none;
    display: grid;
    place-items: center;
    border: 0;
    border-radius: 50%;
    background: var(--violet);
    color: #fff;
    cursor: pointer;
    box-shadow: 0 5px 0 var(--violet-d), var(--float);
    transition: transform 100ms ease, box-shadow 100ms ease, background 120ms ease;
}

.ai__bubble svg { width: 27px; height: 27px; }
.ai__bubble:hover { background: var(--violet-d); }
.ai__bubble:active { transform: translateY(4px); box-shadow: 0 1px 0 var(--violet-d); }

.ai__panel {
    width: min(370px, calc(100vw - 36px));
    max-height: min(80vh, 680px);
    display: flex;
    flex-direction: column;
    background: var(--surface);
    border: 3px solid var(--violet);
    border-radius: var(--r-lg);
    box-shadow: var(--float);
    overflow: hidden;
}

/* Everything except the scrolling middle keeps its natural height, so the
   composer can never be pushed out of the panel. */
.ai__head,
.ai__modes,
.ai__chips,
.ai__composer,
.ai__privacy { flex: none; }

.ai__head {
    display: flex;
    align-items: center;
    gap: 0.65rem;
    padding: 0.85rem 0.9rem;
    border-bottom: 3px dotted var(--lav-200);
}

.ai__badge {
    flex: none;
    width: 36px;
    height: 36px;
    display: grid;
    place-items: center;
    border-radius: 12px;
    background: var(--violet);
    color: #fff;
}

.ai__badge svg { width: 20px; height: 20px; }

.ai__heading { flex: 1; min-width: 0; }

.ai__title {
    display: block;
    font-family: var(--display);
    font-weight: 800;
    font-size: 1.1rem;
    line-height: 1.15;
    color: var(--heading);
}

.ai__subtitle { display: block; font-size: 0.8rem; font-weight: 600; color: var(--ink-mute); }

/* Helper / Gemini switch. */
.ai__modes {
    display: flex;
    gap: 0.3rem;
    padding: 0.5rem 0.9rem 0;
}

.ai-mode {
    flex: 1;
    border: 2px solid var(--lav-200);
    background: var(--surface);
    border-radius: var(--r-pill);
    padding: 0.35rem 0.6rem;
    font-family: var(--display);
    font-weight: 800;
    font-size: 0.85rem;
    color: var(--ink-soft);
    cursor: pointer;
    transition: background 120ms ease, color 120ms ease, border-color 120ms ease;
}

.ai-mode:hover { color: var(--violet-ink); }
.ai-mode.is-active { background: var(--violet); border-color: var(--violet); color: #fff; }

/* Bring-your-own-key card. */
.ai__setup {
    margin: 0.9rem 0.9rem 0;
    padding: 0.8rem;
    background: var(--surface);
    border: 2px solid var(--lav-200);
    border-radius: var(--r);
}

.ai__setup-title {
    margin: 0 0 0.3rem;
    font-family: var(--display);
    font-weight: 800;
    font-size: 0.98rem;
    color: var(--heading);
}

.ai__setup-intro { margin: 0 0 0.5rem; font-size: 0.82rem; font-weight: 600; color: var(--ink-soft); }

.ai__steps {
    margin: 0 0 0.6rem;
    padding-left: 1.2rem;
    font-size: 0.82rem;
    font-weight: 600;
    color: var(--ink-soft);
}

.ai__steps li { margin-bottom: 0.2rem; }

.ai__field { display: block; margin: 0.5rem 0; }

.ai__field span {
    display: block;
    font-size: 0.75rem;
    font-weight: 700;
    color: var(--ink-mute);
    margin-bottom: 0.2rem;
}

.ai__field input {
    width: 100%;
    font-family: var(--body);
    font-size: 0.88rem;
    font-weight: 600;
    color: var(--ink);
    background: var(--surface);
    border: 2px solid var(--lav-200);
    border-radius: var(--r-sm);
    padding: 0.45rem 0.6rem;
}

.ai__field input:focus { outline: none; border-color: var(--violet); }

.ai__setup-status { margin: 0.45rem 0 0; font-size: 0.8rem; font-weight: 700; color: var(--ink-mute); }
.ai__setup-status--bad { color: var(--bad-d); }

.ai__setup-done {
    margin: 0;
    display: flex;
    align-items: center;
    gap: 0.5rem;
    flex-wrap: wrap;
    font-size: 0.82rem;
    font-weight: 700;
    color: var(--good-d);
}

.ai__setup-model {
    font-family: var(--body);
    font-size: 0.72rem;
    font-weight: 700;
    color: var(--ink-mute);
    background: var(--surface-2);
    border-radius: var(--r-pill);
    padding: 0.05rem 0.5rem;
}

/* The one scrolling region. min-height: 0 is the important bit — without it a
   flex child refuses to shrink below its content, and the panel spills. */
.ai__body {
    flex: 1 1 auto;
    min-height: 90px;
    overflow-y: auto;
    background: var(--bg);
}

.ai__messages {
    padding: 0.9rem;
    display: flex;
    flex-direction: column;
    gap: 0.7rem;
}

.ai-msg { max-width: 92%; }
.ai-msg--you { align-self: flex-end; text-align: right; }

.ai-msg__who {
    display: block;
    font-family: var(--display);
    font-weight: 800;
    font-size: 0.68rem;
    letter-spacing: 0.1em;
    text-transform: uppercase;
    color: var(--ink-mute);
    margin-bottom: 0.2rem;
}

.ai-msg__body {
    text-align: left;
    background: var(--surface);
    border: 2px solid var(--lav-200);
    border-radius: var(--r);
    padding: 0.6rem 0.75rem;
    font-size: 0.92rem;
    font-weight: 600;
    line-height: 1.5;
}

.ai-msg__body p { margin: 0 0 0.5rem; }
.ai-msg__body p:last-child { margin-bottom: 0; }

.ai-msg--you .ai-msg__body {
    background: var(--violet);
    border-color: var(--violet);
    color: #fff;
}

/* Three dots while we wait for the model. */
.ai-typing { display: flex; gap: 5px; align-items: center; }

.ai-typing i {
    width: 7px;
    height: 7px;
    border-radius: 50%;
    background: var(--lav-300);
    animation: aiBlink 1s infinite ease-in-out;
}

.ai-typing i:nth-child(2) { animation-delay: 0.15s; }
.ai-typing i:nth-child(3) { animation-delay: 0.3s; }

@keyframes aiBlink {
    0%, 80%, 100% { opacity: 0.3; transform: translateY(0); }
    40% { opacity: 1; transform: translateY(-3px); }
}

.ai__chips { display: flex; flex-wrap: wrap; gap: 0.35rem; padding: 0.6rem 0.9rem 0; }

.ai-chip {
    border: 2px solid var(--lav-200);
    background: var(--surface-2);
    border-radius: var(--r-pill);
    padding: 0.3rem 0.7rem;
    font-family: var(--body);
    font-size: 0.8rem;
    font-weight: 700;
    color: var(--violet-ink);
    cursor: pointer;
}

.ai-chip:hover { border-color: var(--violet); }

.ai__composer {
    display: flex;
    gap: 0.5rem;
    align-items: flex-end;
    padding: 0.7rem 0.9rem 0.3rem;
}

.ai__composer textarea {
    flex: 1;
    min-width: 0;
    resize: none;
    font-family: var(--body);
    font-size: 0.92rem;
    font-weight: 600;
    color: var(--ink);
    background: var(--bg);
    border: 2px solid var(--lav-200);
    border-radius: var(--r);
    padding: 0.55rem 0.7rem;
}

.ai__composer textarea:focus { outline: none; border-color: var(--violet); background: var(--surface); }
.ai__composer textarea:disabled { opacity: 0.6; }

.ai__privacy {
    margin: 0;
    padding: 0.15rem 0.9rem 0.8rem;
    font-size: 0.72rem;
    font-weight: 600;
    color: var(--ink-mute);
}

/* On a phone the panel becomes a sheet across the width of the screen. */
@media (max-width: 520px) {
    .ai { left: 12px; right: 12px; bottom: 12px; align-items: stretch; }
    .ai__panel { width: 100%; max-height: 70vh; }
    .ai__bubble { align-self: flex-end; }
}

