/* ---------- App shell ---------- */

/* Height, not min-height: the chat scrolls inside <main>, so the page itself must
   not grow past the viewport. --kb-inset (viewport_keyboard.js) lifts the
   composer above the iOS on-screen keyboard, which dvh doesn't account for. */
.page-app {
    display: flex;
    flex-direction: column;
    height: calc(100dvh - var(--kb-inset, 0px));
    overflow: hidden;
}

.chat-main {
    display: flex;
    flex-direction: column;
    flex: 1 1 0%;
    min-height: 0;
    overflow: hidden;
    width: 100%;
    max-width: var(--width-chat);
    margin-inline: auto;
}

.chat-log {
    flex: 1 1 0%;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    padding: var(--space-4);
}
.chat-log > * + * { margin-top: var(--space-4); }
.chat-log::-webkit-scrollbar { width: var(--size-scrollbar); }
.chat-log::-webkit-scrollbar-thumb {
    background: var(--color-scrollbar-thumb);
    border-radius: var(--radius-xs);
}

/* Safe area insets for notched phones and gesture bars. */
.site-footer {
    flex-shrink: 0;
    padding-top: var(--space-2);
    padding-bottom: max(var(--space-2), env(safe-area-inset-bottom));
    padding-left: max(var(--space-4), env(safe-area-inset-left));
    padding-right: max(var(--space-4), env(safe-area-inset-right));
    border-top: 1px solid var(--color-border-subtle);
    text-align: center;
    font-size: var(--font-size-xs);
    line-height: var(--line-height-xs);
    color: var(--color-text-secondary);
}

/* Short screens (landscape phones, 400% zoom): give the transcript the room. */
@media (max-height: 500px) {
    .short-hide { display: none; }
}

/* ---------- Welcome ---------- */

.welcome {
    padding-block: var(--space-4);
    text-align: center;
}

.azulejo-panel {
    position: relative;
    max-width: var(--width-welcome);
    margin-inline: auto;
    padding: var(--space-6);
    background: var(--color-bg-surface);
    border: 1px solid var(--color-border-card);
    border-radius: var(--radius-2xl);
    box-shadow: var(--shadow-sm);
}
.azulejo-panel::before,
.azulejo-panel::after {
    content: '';
    display: block;
    height: var(--size-azulejo-strip);
    border-radius: var(--radius-xs);
    background: repeating-linear-gradient(
        90deg,
        var(--color-azulejo-blue) 0px, var(--color-azulejo-blue) 4px,
        var(--color-azulejo-pale) 4px, var(--color-azulejo-pale) 8px,
        var(--color-azulejo-deep) 8px, var(--color-azulejo-deep) 12px,
        var(--color-azulejo-pale) 12px, var(--color-azulejo-pale) 16px,
        var(--color-azulejo-light) 16px, var(--color-azulejo-light) 20px,
        var(--color-azulejo-pale) 20px, var(--color-azulejo-pale) 24px
    );
    opacity: 0.7;
}
.azulejo-panel::before { margin-bottom: var(--space-5); }
.azulejo-panel::after { margin-top: var(--space-5); }

.welcome-flag {
    margin-inline: auto;
    margin-bottom: var(--space-4);
    border-radius: var(--radius-sm);
    box-shadow: var(--shadow-md);
}
.welcome-title {
    margin-bottom: var(--space-2);
    font-size: var(--font-size-lg);
    line-height: var(--line-height-lg);
    font-weight: var(--font-weight-semibold);
    color: var(--color-text-strong);
}
.welcome-text {
    font-size: var(--font-size-sm);
    line-height: var(--line-height-relaxed);
    color: var(--color-text-secondary);
}
.welcome-text--lead { margin-bottom: var(--space-3); }
.welcome-text--body { margin-bottom: var(--space-2); }
.welcome-text--hook {
    margin-bottom: var(--space-3);
    line-height: var(--line-height-sm);
    font-weight: var(--font-weight-medium);
    color: var(--color-text-strong);
}

.starter-link {
    padding: 0;
    border: none;
    background: transparent;
    font-size: var(--font-size-sm);
    line-height: var(--line-height-sm);
    font-style: italic;
    font-weight: var(--font-weight-medium);
    color: var(--color-text-strong);
    text-decoration: underline;
    text-decoration-color: var(--color-decoration-link);
    transition: color var(--duration-fast) var(--ease-standard),
                text-decoration-color var(--duration-fast) var(--ease-standard);
}
.starter-link:hover {
    color: var(--color-text-accent);
    text-decoration-color: var(--color-text-accent);
}

.starters-label {
    margin-top: var(--space-4);
    font-size: var(--font-size-xs);
    line-height: var(--line-height-xs);
    color: var(--color-text-secondary);
}
.starters-list {
    display: flex;
    flex-wrap: wrap;
    justify-content: center;
    gap: var(--space-2);
    margin-top: var(--space-2);
}
.starter-chip {
    min-height: var(--size-tap-sm);
    padding: var(--space-1-5) var(--space-3);
    background: var(--color-bg-surface);
    border: 1px solid var(--color-border-control);
    border-radius: var(--radius-pill);
    font-size: var(--font-size-sm);
    line-height: var(--line-height-sm);
    color: var(--color-text-strong);
    transition: background-color var(--duration-fast) var(--ease-standard),
                border-color var(--duration-fast) var(--ease-standard);
}
.starter-chip:hover {
    background: var(--color-bg-accent-subtle);
    border-color: var(--color-border-accent-soft);
}
.welcome-links {
    margin-top: var(--space-5);
    font-size: var(--font-size-xs);
    line-height: var(--line-height-xs);
    color: var(--color-text-secondary);
}

/* ---------- Messages ---------- */

.msg-row {
    display: flex;
}
.msg-row--user { justify-content: flex-end; }
.msg-row--assistant { justify-content: flex-start; }

.msg-bubble {
    padding: var(--space-2-5) var(--space-4);
    border-radius: var(--radius-2xl);
    font-size: var(--font-size-sm);
    line-height: var(--line-height-sm);
    overflow-wrap: anywhere;
}
.msg-bubble--user {
    max-width: 80%;
    border-bottom-right-radius: var(--radius-md);
    background: var(--color-accent-primary);
    color: var(--color-text-inverse);
    white-space: pre-wrap;
}
.msg-bubble--assistant {
    max-width: 88%;
    border: 1px solid var(--color-border-card);
    border-bottom-left-radius: var(--radius-md);
    background: var(--color-bg-surface);
    color: var(--color-text-primary);
    box-shadow: var(--shadow-sm);
}
@media (min-width: 640px) {
    .msg-bubble--assistant { max-width: 80%; }
}

.msg-note {
    font-style: italic;
    color: var(--color-text-muted);
}
.msg-note--small {
    font-size: var(--font-size-xs);
    line-height: var(--line-height-xs);
}
.msg-note--error { color: var(--color-text-danger); }

.typing-indicator {
    display: flex;
    align-items: center;
    gap: var(--space-1);
    padding-block: var(--space-1);
}
.typing-dot {
    width: var(--size-typing-dot);
    height: var(--size-typing-dot);
    border-radius: var(--radius-pill);
    background: var(--color-typing-dot);
    animation: typing var(--duration-typing) infinite both;
}
.typing-dot:nth-child(2) { animation-delay: var(--delay-typing-1); }
.typing-dot:nth-child(3) { animation-delay: var(--delay-typing-2); }
@keyframes typing {
    0%, 80%, 100% { transform: scale(0.6); opacity: 0.4; }
    40% { transform: scale(1); opacity: 1; }
}
.wait-cue {
    margin-left: var(--space-2);
    font-size: var(--font-size-xs);
    line-height: var(--line-height-xs);
    color: var(--color-text-muted);
}

.notice {
    max-width: 90%;
    padding: var(--space-3) var(--space-6);
    border: 1px solid;
    border-radius: var(--radius-xl);
    font-size: var(--font-size-sm);
    line-height: var(--line-height-sm);
    text-align: center;
}
.notice-row {
    display: flex;
    justify-content: center;
}
.notice--slate {
    background: var(--color-bg-page);
    border-color: var(--color-border-control);
    color: var(--color-text-strong);
}
.notice--amber {
    background: var(--color-bg-warning-subtle);
    border-color: var(--color-border-warning-subtle);
    color: var(--color-text-warning-strong);
}
.notice-action {
    display: block;
    min-height: var(--size-tap-sm);
    margin: var(--space-2) auto 0;
    padding: var(--space-1-5) var(--space-3);
    background: var(--color-bg-surface);
    border: 1px solid var(--color-border-warning);
    border-radius: var(--radius-lg);
    color: var(--color-text-warning-strong);
}
.notice-action:hover { background: var(--color-bg-warning-hover); }

/* Per-message copy buttons sit beside the bubble in the flex row (never over the
   text). Always visible on touch screens and to keyboard focus; hover-revealed
   only where a mouse can hover. */
.copy-msg-btn {
    align-self: flex-start;
    flex-shrink: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    min-width: var(--size-tap-sm);
    min-height: var(--size-tap-sm);
    margin: 0 var(--space-px);
    background: none;
    border: none;
    border-radius: var(--radius-md);
    font-size: var(--font-size-sm);
    line-height: 1;
    opacity: 0.7;
    transition: opacity var(--duration-fast);
}
@media (hover: hover) {
    .copy-msg-btn { opacity: 0; }
    .msg-row:hover .copy-msg-btn { opacity: 0.7; }
}
.msg-row .copy-msg-btn:hover,
.msg-row .copy-msg-btn:focus {
    opacity: 1;
    background: var(--color-bg-hover-subtle);
}

/* ---------- Markdown inside assistant bubbles ----------
   Model headings are shifted down two levels by sanitize.js. */

.msg-content p { margin-bottom: var(--space-3); }
.msg-content p:last-child { margin-bottom: 0; }
.msg-content h3, .msg-content h4, .msg-content h5, .msg-content h6 {
    margin-top: var(--space-4);
    margin-bottom: var(--space-2);
    font-weight: var(--font-weight-semibold);
}
.msg-content h3 { font-size: var(--font-size-base); }
.msg-content h4, .msg-content h5, .msg-content h6 { font-size: var(--font-size-md); }
.msg-content strong { font-weight: var(--font-weight-semibold); }
.msg-content code {
    padding: 0.15rem 0.35rem;
    background: var(--color-bg-code-inline);
    border-radius: var(--radius-sm);
    font-size: var(--font-size-code);
    word-break: break-word;
}
.msg-content pre {
    margin: var(--space-3) 0;
    padding: var(--space-3);
    background: var(--color-bg-code-block);
    color: var(--color-text-code-block);
    border-radius: var(--radius-lg);
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    font-size: var(--font-size-code);
}
.msg-content pre code {
    padding: 0;
    background: none;
    color: inherit;
    word-break: normal;
}
.msg-content ul, .msg-content ol {
    margin: var(--space-3) 0;
    padding-left: var(--space-5);
}
.msg-content ul { list-style: disc; }
.msg-content ol { list-style: decimal; }
.msg-content li { margin-bottom: var(--space-1-5); }
.msg-content li:last-child { margin-bottom: 0; }
/* Tables keep their semantics; the wrapper from sanitize.js does the scrolling. */
.msg-content .table-scroll {
    margin: var(--space-3) 0;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
}
.msg-content table { width: 100%; border-collapse: collapse; }
.msg-content th, .msg-content td {
    padding: var(--space-1-5) var(--space-2-5);
    border: 1px solid var(--color-border-card);
    text-align: left;
    font-size: var(--font-size-code);
}
.msg-content th {
    background: var(--color-bg-subtle);
    font-weight: var(--font-weight-semibold);
}
.msg-content blockquote {
    margin: var(--space-3) 0;
    padding-left: var(--space-3);
    border-left: 3px solid var(--color-accent-bar);
    color: var(--color-text-secondary);
}
.msg-content a {
    color: var(--color-text-accent);
    text-decoration: underline;
}
.msg-content img { max-width: 100%; height: auto; }

/* ---------- Composer ---------- */

.composer {
    flex-shrink: 0;
    padding: var(--space-3) var(--space-4);
    background: var(--color-bg-surface);
    border-top: 1px solid var(--color-border-card);
}
.composer-inner {
    max-width: var(--width-chat);
    margin-inline: auto;
}
.composer-row {
    display: flex;
    gap: var(--space-2);
}
.composer-input {
    flex: 1 1 0%;
    resize: none;
    max-height: var(--size-composer-max);
}
.composer-input:disabled {
    background: var(--color-bg-subtle);
    color: var(--color-text-muted);
}
.composer-send { flex-shrink: 0; }
.composer-send--stop { background: var(--color-bg-neutral-strong); }
.composer-send--stop:hover { background: var(--color-bg-neutral-strong-hover); }
.composer-meta {
    display: flex;
    align-items: flex-start;
    justify-content: space-between;
    gap: var(--space-3);
    margin-top: var(--space-2);
}
.composer-note {
    font-size: var(--font-size-xs);
    line-height: var(--line-height-relaxed);
    color: var(--color-text-secondary);
}
.pt-phrase { font-style: italic; }

/* ---------- Reset bar and export controls ---------- */

.reset-bar {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    justify-content: space-between;
    gap: var(--space-2);
    margin-bottom: var(--space-2);
}
.reset-actions {
    display: flex;
    align-items: center;
    gap: var(--space-1);
}
.copy-feedback {
    font-size: var(--font-size-xs);
    line-height: var(--line-height-xs);
    color: var(--color-text-success);
}
.toolbar-btn {
    display: flex;
    align-items: center;
    gap: var(--space-1);
    min-height: var(--size-tap-sm);
    font-size: var(--font-size-xs);
    line-height: var(--line-height-xs);
    color: var(--color-text-secondary);
    transition: color var(--duration-fast) var(--ease-standard),
                background-color var(--duration-fast) var(--ease-standard);
}
.toolbar-btn:hover { color: var(--color-text-accent); }
.toolbar-btn--clear { padding-inline: var(--space-1); }
.toolbar-btn--action {
    padding-inline: var(--space-2);
    border-radius: var(--radius-md);
}
.toolbar-btn--action:hover { background: var(--color-bg-subtle); }
/* Set by content-export.js while a reply streams. */
.toolbar-btn[aria-disabled="true"] {
    opacity: 0.5;
    cursor: not-allowed;
}

.download-menu { position: relative; }
.download-list {
    position: absolute;
    right: 0;
    bottom: 100%;
    z-index: var(--z-dropdown);
    min-width: var(--width-menu-min);
    margin-bottom: var(--space-1);
    background: var(--color-bg-surface);
    border: 1px solid var(--color-border-control);
    border-radius: var(--radius-lg);
    box-shadow: var(--shadow-lg);
}
.download-item {
    width: 100%;
    min-height: var(--size-tap-md);
    padding-inline: var(--space-3);
    text-align: left;
    font-size: var(--font-size-xs);
    line-height: var(--line-height-xs);
    color: var(--color-text-strong);
    transition: background-color var(--duration-fast) var(--ease-standard);
}
.download-item:first-child {
    border-top-left-radius: var(--radius-lg);
    border-top-right-radius: var(--radius-lg);
}
.download-item:last-child {
    border-bottom-left-radius: var(--radius-lg);
    border-bottom-right-radius: var(--radius-lg);
}
.download-item:hover { background: var(--color-bg-accent-subtle); }

/* Off-screen target for the execCommand copy fallback. */
.clipboard-shim {
    position: fixed;
    left: -9999px;
    top: -9999px;
}

@media (prefers-reduced-motion: reduce) {
    .typing-dot { animation: none; opacity: 1; transform: none; }
    .copy-msg-btn,
    .starter-link,
    .starter-chip,
    .toolbar-btn,
    .download-item { transition: none; }
}
