:root {
    --bg: #e9eef4;
    --panel: #ffffff;
    --panel-2: #f5f7fa;
    --input: #ffffff;
    --border: rgba(24, 36, 54, 0.12);
    --text: #182436;
    --muted: #667487;
    --accent: #d36142;
    --accent-strong: #b84c30;
    --danger: #d64545;
    --assistant: #eef2f7;
    --user: #e8f0fc;
    --radius: 14px;
    --font: "Instrument Sans", sans-serif;
    --mono: "IBM Plex Mono", monospace;
    --top-bar-height: 88px;
    --avatar-size: 96px;
    --shadow: 0 10px 30px rgba(24, 36, 54, 0.08);
}

*,
*::before,
*::after {
    box-sizing: border-box;
}

html,
body {
    margin: 0;
    height: 100%;
    font-family: var(--font);
    color: var(--text);
    background:
        radial-gradient(1100px 520px at 0% 0%, rgba(211, 97, 66, 0.1), transparent 55%),
        radial-gradient(900px 480px at 100% 100%, rgba(90, 130, 190, 0.1), transparent 50%),
        var(--bg);
}

.sr-only {
    position: absolute;
    width: 1px;
    height: 1px;
    padding: 0;
    margin: -1px;
    overflow: hidden;
    clip: rect(0, 0, 0, 0);
    white-space: nowrap;
    border: 0;
}

.app {
    position: relative;
    display: grid;
    grid-template-columns: minmax(320px, 420px) 1fr;
    height: 100vh;
    gap: 0;
}

.credits-badge {
    position: absolute;
    top: calc(var(--top-bar-height) / 2);
    left: 50%;
    transform: translate(-50%, -50%);
    z-index: 30;
    display: inline-flex;
    align-items: baseline;
    gap: 0.4rem;
    padding: 0.45rem 0.85rem;
    border-radius: 999px;
    background: #fff8db;
    border: 1px solid rgba(196, 149, 20, 0.28);
    box-shadow: var(--shadow);
    color: #8a6a00;
    font-family: var(--mono);
    font-weight: 500;
    font-size: 0.95rem;
    letter-spacing: 0.02em;
    pointer-events: none;
    white-space: nowrap;
}

.credits-label {
    color: rgba(138, 106, 0, 0.75);
    font-size: 0.72rem;
    text-transform: uppercase;
    letter-spacing: 0.08em;
}

.account-bar {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    justify-content: center;
    gap: 0.45rem;
    margin-top: 0.85rem;
}

.account-label {
    color: var(--muted);
    font-size: 0.8rem;
    margin-right: 0.15rem;
}

.project-bar {
    display: flex;
    gap: 0.45rem;
    width: 100%;
    margin-top: 0.75rem;
}

.project-select {
    flex: 1;
    min-width: 0;
    border: 1px solid var(--border);
    border-radius: 10px;
    background: var(--input);
    color: var(--text);
    padding: 0.45rem 0.65rem;
    font: inherit;
    font-size: 0.85rem;
}

.asset-tray {
    display: flex;
    gap: 0.45rem;
    overflow-x: auto;
    padding: 0.65rem 1.25rem;
    border-top: 1px solid var(--border);
    background: rgba(24, 36, 54, 0.04);
}

.admin-link[hidden],
#admin-link[hidden],
#buy-btn[hidden] {
    display: none !important;
}

.asset-chip {
    display: inline-flex;
    align-items: center;
    gap: 0.4rem;
    border: 1px solid var(--border);
    background: var(--input);
    color: var(--text);
    border-radius: 999px;
    padding: 0.25rem 0.65rem 0.25rem 0.25rem;
    font: inherit;
    font-size: 0.75rem;
    cursor: pointer;
    white-space: nowrap;
}

.asset-chip img {
    width: 28px;
    height: 28px;
    object-fit: cover;
    border-radius: 50%;
    background: #d7dde6;
}

.composer-left {
    display: flex;
    gap: 0.45rem;
}

.chat-panel,
.preview-panel {
    display: flex;
    flex-direction: column;
    min-height: 0;
}

.chat-panel {
    border-right: 1px solid var(--border);
    background: color-mix(in srgb, var(--panel) 92%, transparent);
    backdrop-filter: blur(10px);
}

.chat-header {
    display: flex;
    flex-direction: column;
    align-items: center;
    padding: 0 1.25rem 1.15rem;
    border-bottom: 1px solid var(--border);
    text-align: center;
}

.chat-avatar {
    display: flex;
    align-items: flex-end;
    justify-content: center;
    width: calc(100% + 2.5rem);
    height: var(--top-bar-height);
    margin: 0 -1.25rem calc(var(--avatar-size) / 2 + 0.75rem);
    border-bottom: 1px solid var(--border);
    box-sizing: border-box;
}

.chat-logo-link {
    position: relative;
    z-index: 1;
    display: block;
    margin-bottom: calc(var(--avatar-size) / -2);
    line-height: 0;
    text-decoration: none;
    color: inherit;
}

.chat-logo {
    display: block;
    width: var(--avatar-size);
    height: var(--avatar-size);
    object-fit: cover;
    background: var(--panel);
    border-radius: 50%;
    box-shadow: 0 0 0 4px var(--panel);
}

.chat-title {
    margin: 0;
    font-size: 1.2rem;
    font-weight: 700;
    letter-spacing: -0.03em;
}

.chat-tagline {
    margin: 0.45rem 0 0;
    max-width: 34ch;
    color: var(--muted);
    font-size: 0.88rem;
    line-height: 1.45;
}

.preview-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 1rem;
    height: var(--top-bar-height);
    padding: 0 1.25rem;
    border-bottom: 1px solid var(--border);
    box-sizing: border-box;
}

.messages {
    flex: 1;
    overflow-y: auto;
    padding: 1.25rem;
    display: flex;
    flex-direction: column;
    gap: 0.85rem;
}

.message {
    display: flex;
    max-width: 95%;
}

.message.user {
    align-self: flex-end;
}

.message.assistant {
    align-self: flex-start;
}

.bubble {
    padding: 0.8rem 0.95rem;
    border-radius: 12px;
    line-height: 1.45;
    font-size: 0.95rem;
    white-space: pre-wrap;
    word-break: break-word;
}

.message.assistant .bubble {
    background: var(--assistant);
    border: 1px solid var(--border);
}

.message.user .bubble {
    background: var(--user);
    border: 1px solid rgba(70, 120, 200, 0.22);
}

.message.error .bubble {
    background: rgba(214, 69, 69, 0.1);
    border: 1px solid rgba(214, 69, 69, 0.28);
    color: #9f1f1f;
}

.composer {
    padding: 1rem 1.25rem 1.25rem;
    border-top: 1px solid var(--border);
    background: var(--panel-2);
}

.composer textarea {
    width: 100%;
    resize: none;
    min-height: 84px;
    max-height: 220px;
    border: 1px solid var(--border);
    border-radius: 12px;
    background: var(--input);
    color: var(--text);
    padding: 0.85rem 0.95rem;
    font: inherit;
    line-height: 1.4;
}

.composer textarea:focus {
    outline: 2px solid rgba(211, 97, 66, 0.28);
    outline-offset: 1px;
    border-color: rgba(211, 97, 66, 0.45);
}

.composer-actions {
    display: flex;
    justify-content: space-between;
    gap: 0.75rem;
    margin-top: 0.75rem;
}

.btn {
    appearance: none;
    border: 1px solid var(--border);
    background: transparent;
    color: var(--text);
    border-radius: 10px;
    padding: 0.65rem 1rem;
    font: inherit;
    font-weight: 600;
    cursor: pointer;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 0.5rem;
}

.btn.small {
    padding: 0.4rem 0.7rem;
    font-size: 0.8rem;
    font-weight: 500;
}

.btn.ghost:hover {
    background: rgba(24, 36, 54, 0.05);
}

.btn.primary {
    background: linear-gradient(180deg, var(--accent), var(--accent-strong));
    color: #fff;
    border-color: transparent;
}

.btn.primary:hover {
    filter: brightness(1.06);
}

.btn:disabled {
    opacity: 0.5;
    cursor: not-allowed;
}

.btn-spinner {
    width: 1rem;
    height: 1rem;
    border: 2px solid rgba(255, 255, 255, 0.35);
    border-top-color: #fff;
    border-radius: 50%;
    animation: spin 0.7s linear infinite;
}

@keyframes spin {
    to {
        transform: rotate(360deg);
    }
}

.preview-panel {
    background: var(--panel);
}

.preview-label {
    font-family: var(--mono);
    font-size: 0.75rem;
    letter-spacing: 0.08em;
    text-transform: uppercase;
    color: var(--muted);
}

.preview-actions {
    display: flex;
    gap: 0.5rem;
}

.preview-frame-wrap {
    position: relative;
    flex: 1;
    min-height: 0;
    background: #f0f3f7;
}

#preview {
    width: 100%;
    height: 100%;
    border: 0;
    background: #fff;
    display: none;
}

#preview.has-content {
    display: block;
}

.preview-empty {
    position: absolute;
    inset: 0;
    display: grid;
    place-items: center;
    color: var(--muted);
    pointer-events: none;
    padding: 2rem;
    text-align: center;
}

.preview-empty[hidden] {
    display: none;
}

.btn.full {
    width: 100%;
}

.modal[hidden] {
    display: none;
}

.modal {
    position: fixed;
    inset: 0;
    z-index: 100;
    display: grid;
    place-items: center;
    padding: 1.25rem;
}

.modal-backdrop {
    position: absolute;
    inset: 0;
    background: rgba(24, 36, 54, 0.42);
    backdrop-filter: blur(4px);
}

.modal-card {
    position: relative;
    z-index: 1;
    width: min(420px, 100%);
    background: var(--panel);
    border: 1px solid var(--border);
    border-radius: 16px;
    padding: 1.4rem 1.35rem 1.25rem;
    box-shadow: 0 24px 60px rgba(24, 36, 54, 0.18);
}

.modal-card:has(#modal-edit-text:not([hidden])) {
    width: min(560px, 100%);
}

.modal-actions {
    display: flex;
    justify-content: flex-end;
    gap: 0.55rem;
    margin-top: 1rem;
}

.rte-toolbar {
    display: flex;
    gap: 0.4rem;
    margin-bottom: 0.55rem;
}

.rte-editor {
    min-height: 140px;
    max-height: 320px;
    overflow: auto;
    padding: 0.75rem 0.85rem;
    border: 1px solid var(--border);
    border-radius: 10px;
    background: #fff;
    font-size: 0.95rem;
    line-height: 1.45;
}

.rte-editor:focus {
    outline: 2px solid color-mix(in srgb, var(--accent) 35%, transparent);
    outline-offset: 1px;
}

#edit-toggle.is-active {
    background: color-mix(in srgb, var(--accent) 14%, #fff);
    border-color: color-mix(in srgb, var(--accent) 40%, var(--border));
    color: var(--accent-strong);
}

.modal-close {
    position: absolute;
    top: 0.65rem;
    right: 0.75rem;
    border: 0;
    background: transparent;
    color: var(--muted);
    font-size: 1.4rem;
    line-height: 1;
    cursor: pointer;
}

.modal-card h2 {
    margin: 0 0 0.45rem;
    font-size: 1.2rem;
    letter-spacing: -0.02em;
}

.modal-copy {
    margin: 0 0 1rem;
    color: var(--muted);
    font-size: 0.92rem;
    line-height: 1.45;
}

.modal-note {
    margin: 0.85rem 0 0;
    color: var(--muted);
    font-size: 0.78rem;
    line-height: 1.4;
}

.tabs {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 0.35rem;
    margin-bottom: 1rem;
    padding: 0.25rem;
    border-radius: 10px;
    background: rgba(24, 36, 54, 0.06);
}

.tab {
    border: 0;
    background: transparent;
    color: var(--muted);
    border-radius: 8px;
    padding: 0.55rem 0.7rem;
    font: inherit;
    font-weight: 600;
    cursor: pointer;
}

.tab.active {
    background: #fff;
    color: var(--text);
    box-shadow: 0 1px 3px rgba(24, 36, 54, 0.08);
}

.auth-form {
    display: flex;
    flex-direction: column;
    gap: 0.75rem;
}

.auth-form[hidden] {
    display: none !important;
}

.auth-form label {
    display: flex;
    flex-direction: column;
    gap: 0.35rem;
    font-size: 0.82rem;
    color: var(--muted);
}

.field-hint {
    font-size: 0.75rem;
    color: var(--muted);
}

.field-hint code {
    color: var(--accent-strong);
    font-family: var(--mono);
    font-size: 0.74rem;
}

.auth-form input {
    border: 1px solid var(--border);
    border-radius: 10px;
    background: var(--input);
    color: var(--text);
    padding: 0.7rem 0.8rem;
    font: inherit;
}

.auth-form input:focus {
    outline: 2px solid rgba(211, 97, 66, 0.28);
    border-color: rgba(211, 97, 66, 0.45);
}

.form-error {
    margin: 0.75rem 0 0;
    color: #9f1f1f;
    font-size: 0.85rem;
}

.btn.ghost.full {
    margin-top: 0.5rem;
}

@media (max-width: 900px) {
    .app {
        grid-template-columns: 1fr;
        grid-template-rows: 55vh 45vh;
        height: 100dvh;
    }

    .chat-panel {
        border-right: 0;
        border-bottom: 1px solid var(--border);
    }

    .credits-badge {
        top: 0.75rem;
        transform: translateX(-50%);
    }
}
