/* =========================================================================
   AAA TOKEN — Web App (chat + image workbench)
   Uses only design tokens from tokens.css (light :root, dark .dark).
   ========================================================================= */

/* ---- Page frame: let the app own the viewport below the site header ---- */
main.container:has(> .apptool) {
    max-width: none;
    padding: 0;
}
body:has(.apptool) .floating-cta,
body:has(.apptool) .back-to-top { display: none !important; }

.apptool {
    --at-side-w: 288px;
    --at-col: 820px;
    --at-header: 65px;
    position: relative;
    display: grid;
    grid-template-columns: var(--at-side-w) minmax(0, 1fr);
    height: calc(100vh - var(--at-header));
    height: calc(100dvh - var(--at-header));
    min-height: 480px;
    background: var(--background);
    color: var(--foreground);
    border-bottom: 1px solid var(--border);
}

.apptool [hidden] { display: none !important; }

.apptool :focus-visible {
    outline: 2px solid var(--ring);
    outline-offset: 2px;
}

/* ============================== Sidebar ============================== */
.apptool__side {
    display: flex;
    flex-direction: column;
    gap: 14px;
    min-height: 0;
    padding: 18px 14px 14px;
    background: var(--sidebar);
    color: var(--sidebar-foreground);
    border-right: 1px solid var(--sidebar-border);
}

.apptool__brandrow { padding: 2px 6px 0; }
.apptool__title {
    margin: 0;
    font-family: var(--font-display);
    font-size: 1.08rem;
    font-weight: 700;
    letter-spacing: var(--tracking-tight);
    line-height: 1.3;
    background: var(--brand-gradient);
    -webkit-background-clip: text;
    background-clip: text;
    color: transparent;
}
.apptool__subtitle {
    margin: 4px 0 0;
    font-size: 0.8rem;
    line-height: 1.45;
    color: var(--muted-foreground);
}

.apptool__modes {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 4px;
    padding: 4px;
    background: var(--muted);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
}
.apptool__mode {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 6px;
    min-height: 38px;
    padding: 0 10px;
    border: 0;
    border-radius: var(--radius-xs);
    background: transparent;
    color: var(--muted-foreground);
    font: inherit;
    font-size: 0.88rem;
    font-weight: 600;
    cursor: pointer;
    transition: background 0.15s var(--ease), color 0.15s var(--ease), box-shadow 0.15s var(--ease);
}
.apptool__mode:hover { color: var(--foreground); }
.apptool__mode.is-active {
    background: var(--card);
    color: var(--foreground);
    box-shadow: var(--shadow-sm);
}
.apptool__mode-ico { font-size: 1rem; line-height: 1; }

/* ---- New chat (sidebar copy injected by JS) ---- */
.apptool__side-new {
    display: flex;
    align-items: center;
    gap: 8px;
    width: 100%;
    min-height: 40px;
    padding: 0 12px;
    border: 1px dashed var(--border);
    border-radius: var(--radius-sm);
    background: transparent;
    color: var(--foreground);
    font: inherit;
    font-size: 0.88rem;
    font-weight: 600;
    cursor: pointer;
    transition: background 0.15s var(--ease), border-color 0.15s var(--ease);
}
.apptool__side-new:hover { background: var(--sidebar-accent); border-color: var(--primary); }
.apptool__side-new svg { flex: none; }

/* ---- History ---- */
.apptool__history {
    display: flex;
    flex-direction: column;
    flex: 1 1 auto;
    min-height: 0;
}
.apptool__history-head {
    margin: 0 0 6px;
    padding: 0 6px;
    font-size: 0.72rem;
    font-weight: 700;
    letter-spacing: var(--tracking-kicker);
    text-transform: uppercase;
    color: var(--muted-foreground);
}
.apptool__history-list {
    flex: 1 1 auto;
    min-height: 0;
    overflow-y: auto;
    margin: 0 -4px;
    padding: 0 4px;
    scrollbar-width: thin;
}
.apptool__history-empty {
    margin: 4px 6px;
    font-size: 0.84rem;
    color: var(--muted-foreground);
}
.apptool__hitem {
    position: relative;
    display: flex;
    align-items: center;
    border-radius: var(--radius-xs);
    transition: background 0.12s var(--ease);
}
.apptool__hitem:hover { background: var(--muted); }
.apptool__hitem.is-active { background: var(--sidebar-accent); }
.apptool__hitem.is-active .apptool__hopen { color: var(--sidebar-accent-foreground); }
.apptool__hopen {
    flex: 1 1 auto;
    min-width: 0;
    display: flex;
    flex-direction: column;
    gap: 1px;
    padding: 8px 36px 8px 10px;
    border: 0;
    background: transparent;
    color: var(--foreground);
    font: inherit;
    text-align: left;
    cursor: pointer;
    border-radius: var(--radius-xs);
}
.apptool__htitle {
    overflow: hidden;
    white-space: nowrap;
    text-overflow: ellipsis;
    font-size: 0.88rem;
    line-height: 1.35;
}
.apptool__hdate {
    font-size: 0.72rem;
    color: var(--muted-foreground);
}
.apptool__hdel {
    position: absolute;
    right: 4px;
    top: 50%;
    transform: translateY(-50%);
    display: inline-grid;
    place-items: center;
    width: 30px;
    height: 30px;
    border: 0;
    border-radius: var(--radius-xs);
    background: transparent;
    color: var(--muted-foreground);
    cursor: pointer;
    opacity: 0;
    transition: opacity 0.12s var(--ease), background 0.12s var(--ease), color 0.12s var(--ease);
}
.apptool__hitem:hover .apptool__hdel,
.apptool__hitem.is-active .apptool__hdel,
.apptool__hdel:focus-visible { opacity: 1; }
.apptool__hdel:hover { color: var(--destructive); background: var(--card); }
.apptool__hskel {
    height: 36px;
    margin: 4px 0;
    border-radius: var(--radius-xs);
    background: linear-gradient(90deg, var(--muted) 0%, var(--secondary) 50%, var(--muted) 100%);
    background-size: 200% 100%;
    animation: apptool-shimmer 1.4s linear infinite;
}

/* ---- Balance ---- */
.apptool__balance {
    margin-top: auto;
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: 4px 10px;
    padding: 12px;
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    background: var(--card);
}
.apptool__history + .apptool__balance { margin-top: 0; }
.apptool__balance-label {
    flex: 1 0 100%;
    font-size: 0.72rem;
    font-weight: 700;
    letter-spacing: var(--tracking-kicker);
    text-transform: uppercase;
    color: var(--muted-foreground);
}
.apptool__balance-val {
    font-family: var(--font-mono);
    font-size: 1.05rem;
    font-weight: 700;
    font-variant-numeric: tabular-nums;
    color: var(--foreground);
    transition: color 0.3s var(--ease);
}
.apptool__balance-val.is-flash { color: var(--brand-gold); }
.apptool__balance-val.is-low { color: var(--destructive); }
.apptool__topup {
    margin-left: auto;
    padding: 4px 12px;
    border-radius: var(--radius-pill);
    background: var(--brand-gradient);
    color: var(--primary-foreground);
    font-size: 0.8rem;
    font-weight: 700;
    text-decoration: none;
}
.apptool__topup:hover { color: var(--primary-foreground); text-decoration: none; filter: brightness(1.08); }
.apptool__guest { margin: 0; font-size: 0.84rem; line-height: 1.5; color: var(--muted-foreground); }
.apptool__guest-actions { display: flex; flex-wrap: wrap; gap: 8px; width: 100%; }
.apptool__guest-actions .btn { flex: 1 1 auto; }

/* ============================== Main ============================== */
.apptool__main {
    position: relative;
    display: flex;
    flex-direction: column;
    min-width: 0;
    min-height: 0;
}
.apptool__view {
    display: flex;
    flex-direction: column;
    flex: 1 1 auto;
    min-height: 0;
    padding-bottom: calc(10px + env(safe-area-inset-bottom));
    outline: none;
}

/* ---- Toolbar ---- */
.apptool__toolbar {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 10px 20px;
    border-bottom: 1px solid var(--border);
    background: color-mix(in srgb, var(--background) 88%, transparent);
    backdrop-filter: blur(8px);
    -webkit-backdrop-filter: blur(8px);
}
.apptool__menu {
    display: none;
    place-items: center;
    flex: none;
    width: 40px;
    height: 40px;
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    background: var(--card);
    color: var(--foreground);
    cursor: pointer;
}
.apptool__field {
    display: flex;
    align-items: center;
    gap: 8px;
    min-width: 0;
}
.apptool__field-label {
    font-size: 0.78rem;
    font-weight: 600;
    color: var(--muted-foreground);
    white-space: nowrap;
}
.apptool__field-hint {
    font-size: 0.76rem;
    color: var(--muted-foreground);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}
.apptool__select {
    min-width: 0;
    max-width: 260px;
    min-height: 38px;
    padding: 0 34px 0 12px;
    border: 1px solid var(--input);
    border-radius: var(--radius-sm);
    background-color: var(--card);
    background-image: linear-gradient(45deg, transparent 50%, var(--muted-foreground) 50%), linear-gradient(135deg, var(--muted-foreground) 50%, transparent 50%);
    background-position: calc(100% - 17px) 50%, calc(100% - 12px) 50%;
    background-size: 5px 5px, 5px 5px;
    background-repeat: no-repeat;
    color: var(--foreground);
    font: inherit;
    font-size: 0.88rem;
    font-weight: 600;
    -webkit-appearance: none;
    appearance: none;
    cursor: pointer;
}
.apptool__select:hover { border-color: var(--primary); }
.apptool__select option { background: var(--popover); color: var(--popover-foreground); }
.apptool__select option:disabled { color: var(--muted-foreground); }

.apptool__newchat {
    margin-left: auto;
    display: inline-flex;
    align-items: center;
    gap: 6px;
    flex: none;
    min-height: 38px;
    padding: 0 14px;
    border: 1px solid var(--border);
    border-radius: var(--radius-pill);
    background: var(--card);
    color: var(--foreground);
    font: inherit;
    font-size: 0.85rem;
    font-weight: 600;
    cursor: pointer;
    transition: background 0.15s var(--ease), border-color 0.15s var(--ease);
}
.apptool__newchat:hover { background: var(--secondary); border-color: var(--primary); }

/* ---- Message stream ---- */
.apptool__streamwrap {
    position: relative;
    flex: 1 1 auto;
    min-height: 0;
    display: flex;
}
.apptool__stream {
    flex: 1 1 auto;
    min-height: 0;
    overflow-y: auto;
    overscroll-behavior: contain;
    padding: 24px 20px 32px;
    scroll-padding-bottom: 32px;
}
.apptool__stream > * {
    max-width: var(--at-col);
    margin-left: auto;
    margin-right: auto;
}

/* ---- Empty states ---- */
.apptool__empty {
    display: flex;
    flex-direction: column;
    align-items: center;
    text-align: center;
    padding: clamp(24px, 7vh, 72px) 8px 16px;
}
.apptool__empty-ico {
    display: grid;
    place-items: center;
    width: 64px;
    height: 64px;
    margin-bottom: 14px;
    border-radius: 18px;
    background: var(--brand-gradient);
    font-size: 1.8rem;
    box-shadow: var(--shadow-lg);
}
.apptool__empty-title {
    margin: 0;
    font-family: var(--font-display);
    font-size: clamp(1.3rem, 3vw, 1.7rem);
    font-weight: 700;
    letter-spacing: var(--tracking-tight);
}
.apptool__empty-sub {
    margin: 6px 0 0;
    max-width: 520px;
    font-size: 0.95rem;
    color: var(--muted-foreground);
}
.apptool__suggest {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 10px;
    width: 100%;
    max-width: 640px;
    margin-top: 28px;
}
.apptool__suggest-head {
    grid-column: 1 / -1;
    margin: 0;
    font-size: 0.74rem;
    font-weight: 700;
    letter-spacing: var(--tracking-kicker);
    text-transform: uppercase;
    color: var(--muted-foreground);
    text-align: left;
}
.apptool__suggest-btn {
    min-height: 56px;
    padding: 12px 14px;
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    background: var(--card);
    color: var(--foreground);
    font: inherit;
    font-size: 0.88rem;
    line-height: 1.45;
    text-align: left;
    cursor: pointer;
    transition: border-color 0.15s var(--ease), transform 0.15s var(--ease), box-shadow 0.15s var(--ease);
}
.apptool__suggest-btn:hover {
    border-color: var(--primary);
    box-shadow: var(--shadow-md);
    transform: translateY(-1px);
}

/* ---- Messages ---- */
.apptool__msg {
    display: flex;
    align-items: flex-start;
    gap: 12px;
    margin-bottom: 22px;
    animation: apptool-in 0.22s var(--ease) both;
}
.apptool__msg--user { flex-direction: row-reverse; }
.apptool__avatar {
    display: grid;
    place-items: center;
    flex: none;
    width: 32px;
    height: 32px;
    border-radius: 50%;
    font-size: 0.72rem;
    font-weight: 800;
    letter-spacing: 0.02em;
    user-select: none;
}
.apptool__msg--ai .apptool__avatar,
.apptool__msg--error .apptool__avatar {
    background: var(--brand-gradient);
    color: var(--primary-foreground);
    box-shadow: var(--shadow-sm);
}
.apptool__msg--user .apptool__avatar {
    background: var(--secondary);
    color: var(--secondary-foreground);
    border: 1px solid var(--border);
}
.apptool__bubble {
    min-width: 0;
    max-width: calc(100% - 44px);
}
.apptool__msg--user .apptool__bubble {
    padding: 10px 16px;
    border-radius: 18px 18px 4px 18px;
    background: var(--accent);
    color: var(--accent-foreground);
    border: 1px solid color-mix(in srgb, var(--primary) 22%, transparent);
}
.apptool__msg--user .apptool__md { color: var(--foreground); }
.apptool__msg--ai .apptool__bubble { flex: 1 1 auto; padding-top: 4px; }
.apptool__msg--error .apptool__bubble {
    flex: 1 1 auto;
    padding: 12px 14px;
    border: 1px solid color-mix(in srgb, var(--destructive) 45%, transparent);
    border-radius: var(--radius-sm);
    background: color-mix(in srgb, var(--destructive) 9%, var(--card));
}
.apptool__err-title { margin: 0; font-weight: 600; color: var(--foreground); }
.apptool__err-detail {
    margin: 4px 0 0;
    font-family: var(--font-mono);
    font-size: 0.74rem;
    color: var(--muted-foreground);
}
.apptool__err-actions { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 10px; }

/* ---- Message footer (cost, copy, retry) ---- */
.apptool__msg-foot {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: 6px 10px;
    margin-top: 8px;
    min-height: 28px;
    font-size: 0.76rem;
    color: var(--muted-foreground);
}
.apptool__cost {
    display: inline-flex;
    align-items: center;
    gap: 4px;
    padding: 2px 8px;
    border-radius: var(--radius-pill);
    background: var(--muted);
    border: 1px solid var(--border);
    font-family: var(--font-mono);
    font-variant-numeric: tabular-nums;
}
.apptool__cost b { color: var(--brand-gold); font-weight: 700; }
.apptool__model-tag { font-family: var(--font-mono); opacity: 0.85; }
.apptool__stopped { font-style: italic; }
.apptool__iconbtn {
    display: inline-flex;
    align-items: center;
    gap: 5px;
    min-height: 28px;
    padding: 0 9px;
    border: 1px solid transparent;
    border-radius: var(--radius-xs);
    background: transparent;
    color: var(--muted-foreground);
    font: inherit;
    font-size: 0.76rem;
    cursor: pointer;
    transition: background 0.12s var(--ease), color 0.12s var(--ease), border-color 0.12s var(--ease);
}
.apptool__iconbtn:hover { background: var(--muted); color: var(--foreground); border-color: var(--border); }
.apptool__iconbtn.is-done { color: var(--success); }
.apptool__iconbtn--solid {
    border-color: var(--border);
    background: var(--card);
    color: var(--foreground);
    min-height: 32px;
    padding: 0 12px;
    font-weight: 600;
    text-decoration: none;
}
.apptool__iconbtn--solid:hover { text-decoration: none; }
.apptool__iconbtn--primary {
    border-color: transparent;
    background: var(--primary);
    color: var(--primary-foreground);
}
.apptool__iconbtn--primary:hover { background: var(--primary-hover); color: var(--primary-foreground); }

/* ---- Attachment chips ---- */
.apptool__attachments {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
    width: 100%;
    max-width: calc(var(--at-col) + 40px);
    margin: 0 auto;
    padding: 0 20px 8px;
}
.apptool__chipfile {
    display: inline-flex;
    align-items: center;
    gap: 8px;
    max-width: 260px;
    min-height: 40px;
    padding: 4px 6px 4px 6px;
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    background: var(--card);
    font-size: 0.8rem;
    box-shadow: var(--shadow-sm);
}
.apptool__chipfile.is-loading { opacity: 0.75; }
.apptool__chipfile.is-error { border-color: var(--destructive); }
.apptool__chipfile-thumb {
    display: grid;
    place-items: center;
    flex: none;
    width: 30px;
    height: 30px;
    border-radius: var(--radius-xs);
    background: var(--muted);
    color: var(--muted-foreground);
    font-size: 0.62rem;
    font-weight: 800;
    text-transform: uppercase;
    overflow: hidden;
}
.apptool__chipfile-thumb img { width: 100%; height: 100%; object-fit: cover; }
.apptool__chipfile-name {
    min-width: 0;
    overflow: hidden;
    white-space: nowrap;
    text-overflow: ellipsis;
    color: var(--foreground);
}
.apptool__chipfile-meta { color: var(--muted-foreground); white-space: nowrap; }
.apptool__chipfile-x {
    display: grid;
    place-items: center;
    flex: none;
    width: 26px;
    height: 26px;
    border: 0;
    border-radius: 50%;
    background: transparent;
    color: var(--muted-foreground);
    cursor: pointer;
}
.apptool__chipfile-x:hover { background: var(--muted); color: var(--destructive); }
.apptool__msg-att { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 8px; }
.apptool__msg-att .apptool__chipfile { box-shadow: none; min-height: 34px; background: var(--card); }
.apptool__msg-att img.apptool__msg-img {
    display: block;
    max-width: 220px;
    max-height: 180px;
    border-radius: var(--radius-sm);
    border: 1px solid var(--border);
    object-fit: cover;
}

/* ---- Typing indicator ---- */
.apptool__typing {
    display: inline-flex;
    align-items: center;
    gap: 10px;
    min-height: 28px;
    color: var(--muted-foreground);
    font-size: 0.86rem;
}
.apptool__dots { display: inline-flex; gap: 4px; }
.apptool__dots i {
    width: 7px;
    height: 7px;
    border-radius: 50%;
    background: var(--primary);
    animation: apptool-bounce 1.2s infinite ease-in-out;
}
.apptool__dots i:nth-child(2) { animation-delay: 0.15s; background: var(--brand-cyan); }
.apptool__dots i:nth-child(3) { animation-delay: 0.3s; background: var(--brand-gold); }
.apptool__md.is-streaming > :last-child::after {
    content: "";
    display: inline-block;
    width: 0.5em;
    height: 1.05em;
    margin-left: 2px;
    vertical-align: text-bottom;
    background: var(--primary);
    border-radius: 1px;
    animation: apptool-caret 1s steps(2, start) infinite;
}
.apptool__md.is-streaming > .apptool__code:last-child::after { display: none; }

/* ============================== Markdown ============================== */
.apptool__md {
    font-size: 0.97rem;
    line-height: 1.72;
    overflow-wrap: anywhere;
    word-break: normal;
}
.apptool__md > :first-child { margin-top: 0; }
.apptool__md > :last-child { margin-bottom: 0; }
.apptool__md p { margin: 0 0 0.85em; }
.apptool__md h1, .apptool__md h2, .apptool__md h3,
.apptool__md h4, .apptool__md h5, .apptool__md h6 {
    margin: 1.3em 0 0.5em;
    font-family: var(--font-display);
    line-height: 1.3;
    letter-spacing: var(--tracking-tight);
}
.apptool__md h1 { font-size: 1.35rem; }
.apptool__md h2 { font-size: 1.2rem; }
.apptool__md h3 { font-size: 1.07rem; }
.apptool__md h4, .apptool__md h5, .apptool__md h6 { font-size: 0.98rem; }
.apptool__md ul, .apptool__md ol { margin: 0 0 0.9em; padding-left: 1.5em; }
.apptool__md li { margin: 0.22em 0; }
.apptool__md li > ul, .apptool__md li > ol { margin: 0.2em 0 0.2em; }
.apptool__md li::marker { color: var(--muted-foreground); }
.apptool__md a { color: var(--primary); text-decoration: underline; text-underline-offset: 2px; }
.apptool__md a:hover { color: var(--primary-hover); }
.apptool__md strong { font-weight: 700; color: var(--foreground); }
.apptool__md blockquote {
    margin: 0 0 0.9em;
    padding: 6px 14px;
    border-left: 3px solid var(--brand-gold);
    background: var(--muted);
    border-radius: 0 var(--radius-xs) var(--radius-xs) 0;
    color: var(--muted-foreground);
}
.apptool__md hr { border: 0; border-top: 1px solid var(--border); margin: 1.2em 0; }
.apptool__md :not(pre) > code {
    padding: 0.12em 0.4em;
    border-radius: var(--radius-xs);
    background: var(--muted);
    border: 1px solid var(--border);
    font-family: var(--font-mono);
    font-size: 0.86em;
}
.apptool__md .apptool__tablewrap {
    margin: 0 0 0.9em;
    overflow-x: auto;
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
}
.apptool__md table { width: 100%; border-collapse: collapse; font-size: 0.9rem; }
.apptool__md th, .apptool__md td {
    padding: 8px 12px;
    border-bottom: 1px solid var(--border);
    text-align: left;
    vertical-align: top;
}
.apptool__md th { background: var(--muted); font-weight: 600; }
.apptool__md tr:last-child td { border-bottom: 0; }

/* ---- Code blocks ---- */
.apptool__code {
    margin: 0 0 1em;
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    background: var(--surface-warm);
    overflow: hidden;
}
.apptool__code-head {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 8px;
    min-height: 36px;
    padding: 0 6px 0 14px;
    border-bottom: 1px solid var(--border);
    background: var(--muted);
    font-family: var(--font-mono);
    font-size: 0.74rem;
    color: var(--muted-foreground);
}
.apptool__code pre {
    margin: 0;
    padding: 14px 16px;
    overflow-x: auto;
    font-family: var(--font-mono);
    font-size: 0.85rem;
    line-height: 1.6;
    tab-size: 4;
    color: var(--foreground);
    background: transparent;
}
.apptool__code code { font-family: inherit; white-space: pre; }

/* ============================== Composer ============================== */
.apptool__composer {
    position: relative;
    display: flex;
    flex-wrap: wrap;
    align-items: flex-end;
    gap: 6px;
    width: calc(100% - 40px);
    max-width: var(--at-col);
    margin: 0 auto;
    padding: 8px 8px 6px;
    border: 1px solid var(--input);
    border-radius: 22px;
    background: var(--card);
    box-shadow: var(--shadow-lg);
    transition: border-color 0.15s var(--ease), box-shadow 0.15s var(--ease);
}
.apptool__composer:focus-within {
    border-color: var(--ring);
    box-shadow: var(--shadow-lg), 0 0 0 3px color-mix(in srgb, var(--ring) 22%, transparent);
}
.apptool__composer.is-drop {
    border-style: dashed;
    border-color: var(--primary);
    background: color-mix(in srgb, var(--primary) 6%, var(--card));
}
.apptool__input {
    flex: 1 1 200px;
    min-width: 0;
    min-height: 40px;
    max-height: 220px;
    padding: 9px 8px;
    border: 0;
    outline: none;
    resize: none;
    background: transparent;
    color: var(--foreground);
    font: inherit;
    font-size: 0.98rem;
    line-height: 1.5;
    overflow-y: auto;
}
.apptool__input::placeholder { color: var(--muted-foreground); opacity: 0.85; }
.apptool__input:focus-visible { outline: none; }
.apptool__attach,
.apptool__send {
    display: grid;
    place-items: center;
    flex: none;
    width: 40px;
    height: 40px;
    border: 0;
    border-radius: 50%;
    cursor: pointer;
    transition: background 0.15s var(--ease), color 0.15s var(--ease), transform 0.15s var(--ease), opacity 0.15s var(--ease);
}
.apptool__attach { background: transparent; color: var(--muted-foreground); }
.apptool__attach:hover { background: var(--muted); color: var(--foreground); }
.apptool__attach[aria-disabled="true"] { opacity: 0.55; }
.apptool__send {
    background: var(--primary);
    color: var(--primary-foreground);
    box-shadow: var(--shadow-sm);
}
.apptool__send:hover { background: var(--primary-hover); transform: translateY(-1px); }
.apptool__send:disabled { opacity: 0.4; cursor: not-allowed; transform: none; }
.apptool__send.is-stop { background: var(--foreground); color: var(--background); }
.apptool__send.is-busy { cursor: progress; }
.apptool__send.is-busy svg { animation: apptool-spin 0.9s linear infinite; }

.apptool__composer-meta {
    flex: 1 0 100%;
    display: flex;
    justify-content: space-between;
    gap: 12px;
    padding: 0 8px;
    font-size: 0.72rem;
    color: var(--muted-foreground);
}
.apptool__enter-hint kbd {
    padding: 1px 5px;
    border: 1px solid var(--border);
    border-bottom-width: 2px;
    border-radius: 4px;
    background: var(--muted);
    font-family: var(--font-mono);
    font-size: 0.68rem;
}
.apptool__counter { font-family: var(--font-mono); font-variant-numeric: tabular-nums; }
.apptool__counter.is-near { color: var(--warning); }
.apptool__counter.is-max { color: var(--destructive); font-weight: 700; }

.apptool__hint {
    width: calc(100% - 40px);
    max-width: var(--at-col);
    min-height: 1.4em;
    margin: 6px auto 0;
    padding: 0 8px;
    font-size: 0.76rem;
    line-height: 1.4;
    color: var(--muted-foreground);
    text-align: center;
}
.apptool__hint.is-error { color: var(--destructive); }
.apptool__hint.is-ok { color: var(--success); }
.apptool__hint a { color: var(--primary); }

/* ---- Scroll-to-bottom ---- */
.apptool__scrolldown {
    position: absolute;
    left: 50%;
    bottom: 12px;
    z-index: 3;
    display: grid;
    place-items: center;
    width: 38px;
    height: 38px;
    border: 1px solid var(--border);
    border-radius: 50%;
    background: var(--card);
    color: var(--foreground);
    box-shadow: var(--shadow-md);
    cursor: pointer;
    opacity: 0;
    visibility: hidden;
    transform: translate(-50%, 8px);
    transition: opacity 0.18s var(--ease), transform 0.18s var(--ease), visibility 0.18s;
}
.apptool__scrolldown.is-shown { opacity: 1; visibility: visible; transform: translate(-50%, 0); }

/* ============================== Image mode ============================== */
.apptool__imgwrap {
    flex: 1 1 auto;
    min-height: 0;
    overflow-y: auto;
    padding: 24px 20px;
}
.apptool__imggrid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(240px, 1fr));
    gap: 16px;
    max-width: 1040px;
    margin: 0 auto;
}
.apptool__imgcard {
    margin: 0;
    display: flex;
    flex-direction: column;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    background: var(--card);
    overflow: hidden;
    box-shadow: var(--shadow-sm);
    animation: apptool-in 0.25s var(--ease) both;
}
.apptool__imgcard-media {
    position: relative;
    display: block;
    background: var(--muted);
    aspect-ratio: 1 / 1;
}
.apptool__imgcard-media[data-ratio="1024x1536"] { aspect-ratio: 2 / 3; }
.apptool__imgcard-media[data-ratio="1536x1024"] { aspect-ratio: 3 / 2; }
.apptool__imgcard-media img {
    display: block;
    width: 100%;
    height: 100%;
    object-fit: cover;
}
.apptool__imgcard-media.is-loading {
    display: grid;
    place-items: center;
    background: linear-gradient(110deg, var(--muted) 0%, var(--secondary) 40%, var(--muted) 80%);
    background-size: 220% 100%;
    animation: apptool-shimmer 1.5s linear infinite;
}
.apptool__imgcard-media.is-loading span {
    display: inline-flex;
    align-items: center;
    gap: 10px;
    padding: 6px 12px;
    border-radius: var(--radius-pill);
    background: var(--card);
    font-size: 0.8rem;
    color: var(--muted-foreground);
    box-shadow: var(--shadow-sm);
}
.apptool__imgcard-body { display: flex; flex-direction: column; gap: 8px; padding: 12px; }
.apptool__imgcard-prompt {
    margin: 0;
    display: -webkit-box;
    -webkit-line-clamp: 3;
    -webkit-box-orient: vertical;
    overflow: hidden;
    font-size: 0.84rem;
    line-height: 1.5;
    color: var(--foreground);
}
.apptool__imgcard-actions { display: flex; flex-wrap: wrap; align-items: center; gap: 6px; }
.apptool__imgcard-actions .apptool__cost { margin-left: auto; font-size: 0.72rem; }
.apptool__imgerr {
    max-width: 640px;
    margin: 0 auto 16px;
}

.apptool__imgkits {
    display: flex;
    flex-wrap: wrap;
    justify-content: center;
    gap: 8px;
    width: calc(100% - 40px);
    max-width: var(--at-col);
    margin: 0 auto 8px;
}
.apptool__chip {
    min-height: 34px;
    padding: 0 14px;
    border: 1px solid var(--border);
    border-radius: var(--radius-pill);
    background: var(--card);
    color: var(--foreground);
    font: inherit;
    font-size: 0.82rem;
    cursor: pointer;
    transition: border-color 0.15s var(--ease), background 0.15s var(--ease);
}
.apptool__chip:hover, .apptool__chip.is-active { border-color: var(--brand-gold); background: var(--muted); }
.apptool__img-tip {
    width: calc(100% - 40px);
    max-width: var(--at-col);
    margin: 0 auto 8px;
    font-size: 0.76rem;
    color: var(--muted-foreground);
    text-align: center;
}

/* ============================== Drawer backdrop ============================== */
.apptool__backdrop {
    display: none;
}

/* ============================== Animations ============================== */
@keyframes apptool-in {
    from { opacity: 0; transform: translateY(6px); }
    to { opacity: 1; transform: none; }
}
@keyframes apptool-bounce {
    0%, 80%, 100% { transform: translateY(0); opacity: 0.45; }
    40% { transform: translateY(-5px); opacity: 1; }
}
@keyframes apptool-caret { to { visibility: hidden; } }
@keyframes apptool-shimmer {
    from { background-position: 200% 0; }
    to { background-position: -20% 0; }
}
@keyframes apptool-spin { to { transform: rotate(360deg); } }

/* Visually hidden helper */
.apptool__sr {
    position: absolute !important;
    width: 1px;
    height: 1px;
    padding: 0;
    margin: -1px;
    overflow: hidden;
    clip: rect(0 0 0 0);
    white-space: nowrap;
    border: 0;
}

/* ===================== Layout guards =====================
   app-extra.css loads after this file; these slightly more specific rules keep
   the shell's centering/hiding intact while letting it restyle colors & chips. */
.apptool .apptool__attachments { margin: 0 auto; }
.apptool .apptool__attachments > .apptool__chipfile {
    padding: 4px 6px;
    border-radius: var(--radius-sm);
    background: var(--card);
}
.apptool .apptool__imgkits { margin: 0 auto 8px; justify-content: center; }
.apptool .apptool__img-tip { margin: 0 auto 8px; }
.apptool .apptool__toolbar .apptool__field-hint { margin-top: 0; min-width: 0; }

/* ============================== Responsive ============================== */
@media (max-width: 1100px) {
    .apptool { --at-side-w: 256px; }
    .apptool .apptool__toolbar .apptool__field-hint { display: none; }
}

@media (max-width: 860px) {
    .apptool { grid-template-columns: minmax(0, 1fr); }
    .apptool__side {
        position: fixed;
        top: 0;
        bottom: 0;
        left: 0;
        z-index: 80;
        width: min(86vw, 320px);
        padding-top: calc(18px + env(safe-area-inset-top));
        padding-bottom: calc(14px + env(safe-area-inset-bottom));
        box-shadow: var(--shadow-lg);
        transform: translateX(-104%);
        visibility: hidden;
        transition: transform 0.24s var(--ease), visibility 0.24s;
    }
    .apptool.is-side-open .apptool__side { transform: none; visibility: visible; }
    .apptool__backdrop {
        position: fixed;
        inset: 0;
        z-index: 79;
        display: block;
        background: rgb(0 0 0 / 0.45);
        opacity: 0;
        visibility: hidden;
        transition: opacity 0.2s var(--ease), visibility 0.2s;
    }
    .apptool.is-side-open .apptool__backdrop { opacity: 1; visibility: visible; }
    .apptool__menu { display: grid; }
    .apptool__toolbar { padding: 8px 12px; gap: 8px; }
    .apptool__field-label { display: none; }
    .apptool__select { max-width: none; }
    .apptool__field { flex: 1 1 auto; }
    .apptool__field .apptool__select { flex: 1 1 auto; width: 100%; }
    .apptool__stream { padding: 18px 14px 24px; }
    .apptool__composer, .apptool__hint, .apptool__imgkits, .apptool__img-tip { width: calc(100% - 24px); }
    .apptool__attachments { padding: 0 12px 8px; }
    .apptool__imgwrap { padding: 16px 12px; }
}

@media (max-width: 560px) {
    .apptool__newchat-label { display: none; }
    .apptool__newchat { padding: 0; width: 40px; justify-content: center; border-radius: var(--radius-sm); }
    .apptool__suggest { grid-template-columns: minmax(0, 1fr); margin-top: 20px; }
    .apptool__suggest-btn { min-height: 48px; }
    .apptool__empty { padding-top: 20px; }
    .apptool__empty-ico { width: 52px; height: 52px; font-size: 1.5rem; border-radius: 14px; }
    .apptool__msg { gap: 8px; margin-bottom: 18px; }
    .apptool__avatar { width: 28px; height: 28px; font-size: 0.66rem; }
    .apptool__bubble { max-width: calc(100% - 36px); }
    .apptool__msg--user .apptool__bubble { padding: 9px 13px; }
    .apptool__enter-hint { display: none; }
    .apptool__composer-meta { justify-content: flex-end; }
    .apptool__hdel { opacity: 1; }
    .apptool__imggrid { grid-template-columns: minmax(0, 1fr); }
    .apptool__toolbar .apptool__field + .apptool__field .apptool__select { max-width: 130px; }
    .apptool__input { font-size: 16px; } /* prevent iOS zoom */
}

@media (hover: none) {
    .apptool__hdel { opacity: 1; }
}

@media (prefers-reduced-motion: reduce) {
    .apptool *,
    .apptool *::before,
    .apptool *::after {
        animation-duration: 0.01ms !important;
        animation-iteration-count: 1 !important;
        transition-duration: 0.01ms !important;
    }
    .apptool__md.is-streaming > :last-child::after { animation: none; }
    .apptool__dots i { animation: none; opacity: 0.8; }
}
