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

body {
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
    background: #f5f5f5;
    color: #1a1a1a;
    height: 100dvh;
    display: flex;
    flex-direction: column;
    align-items: center;
}

header {
    width: 100%;
    background: #fff;
    border-bottom: 1px solid #e5e5e5;
    padding: 0.75rem 1.5rem;
    display: flex;
    align-items: center;
    gap: 1rem;
    flex-shrink: 0;
}

.back {
    font-size: 0.85rem;
    color: #888;
    text-decoration: none;
    white-space: nowrap;
    transition: color 0.15s;
}

.back:hover { color: #1a1a1a; }

.header-info {
    display: flex;
    align-items: center;
    gap: 0.75rem;
}

.avatar {
    width: 36px;
    height: 36px;
    background: #f0f0f0;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 1.1rem;
    flex-shrink: 0;
}

header h1 { font-size: 1rem; font-weight: 600; }
header p  { font-size: 0.8rem; color: #888; }

#chat {
    flex: 1;
    overflow-y: auto;
    width: 100%;
    max-width: 780px;
    padding: 1.5rem 1rem;
    display: flex;
    flex-direction: column;
    gap: 1.25rem;
    scroll-behavior: smooth;
}

.message {
    display: flex;
    gap: 0.75rem;
    align-items: flex-start;
    max-width: 85%;
    animation: fadeIn 0.2s ease;
}

@keyframes fadeIn {
    from { opacity: 0; transform: translateY(6px); }
    to   { opacity: 1; transform: translateY(0); }
}

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

.msg-icon {
    width: 30px;
    height: 30px;
    border-radius: 50%;
    flex-shrink: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 0.75rem;
    font-weight: 600;
    margin-top: 2px;
}

.message.user .msg-icon      { background: #1a1a1a; color: #fff; }
.message.assistant .msg-icon { background: #e5e5e5; color: #1a1a1a; }

.bubble {
    padding: 0.7rem 1rem;
    border-radius: 18px;
    line-height: 1.55;
    font-size: 0.95rem;
    white-space: pre-wrap;
    word-break: break-word;
}

.message.user .bubble {
    background: #1a1a1a;
    color: #fff;
    border-bottom-right-radius: 4px;
}

.message.assistant .bubble {
    background: #fff;
    border: 1px solid #e5e5e5;
    color: #1a1a1a;
    border-bottom-left-radius: 4px;
}

.typing .bubble::after {
    content: '';
    display: inline-block;
    width: 7px;
    height: 7px;
    margin-left: 4px;
    background: #aaa;
    border-radius: 50%;
    animation: blink 0.9s infinite;
    vertical-align: middle;
}

@keyframes blink {
    0%, 100% { opacity: 0.2; }
    50%       { opacity: 1; }
}

#input-area {
    width: 100%;
    max-width: 780px;
    padding: 1rem;
    flex-shrink: 0;
}

.input-box {
    display: flex;
    align-items: flex-end;
    gap: 0.5rem;
    background: #fff;
    border: 1px solid #e5e5e5;
    border-radius: 14px;
    padding: 0.5rem 0.5rem 0.5rem 1rem;
    box-shadow: 0 1px 4px rgba(0,0,0,0.06);
    transition: border-color 0.15s;
}

.input-box:focus-within { border-color: #aaa; }

#user-input {
    flex: 1;
    border: none;
    outline: none;
    font-size: 0.95rem;
    resize: none;
    max-height: 160px;
    line-height: 1.5;
    background: transparent;
    color: #1a1a1a;
    padding: 0.35rem 0;
    font-family: inherit;
}

#send-btn {
    width: 36px;
    height: 36px;
    background: #1a1a1a;
    color: #fff;
    border: none;
    border-radius: 10px;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
    transition: background 0.15s, opacity 0.15s;
}

#send-btn:hover:not(:disabled) { background: #333; }
#send-btn:disabled { opacity: 0.35; cursor: not-allowed; }
#send-btn svg { width: 16px; height: 16px; }

#stop-btn {
    width: 36px;
    height: 36px;
    background: #ef4444;
    color: #fff;
    border: none;
    border-radius: 10px;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
    transition: background 0.15s;
}

#stop-btn:hover { background: #dc2626; }
#stop-btn svg { width: 14px; height: 14px; }

.stopped { font-size: 0.75rem; color: #aaa; margin-left: 0.4rem; }

.icon-btn {
    width: 32px;
    height: 32px;
    background: none;
    border: none;
    border-radius: 8px;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    color: #aaa;
    flex-shrink: 0;
    transition: color 0.15s, background 0.15s;
    padding: 0;
}

.icon-btn:hover:not(:disabled) { color: #1a1a1a; background: #f0f0f0; }
.icon-btn:disabled { opacity: 0.35; cursor: not-allowed; }
.icon-btn svg { width: 17px; height: 17px; }

.file-chip {
    display: inline-flex;
    align-items: center;
    gap: 0.4rem;
    background: #f0f0f0;
    border: 1px solid #e0e0e0;
    border-radius: 99px;
    padding: 0.25rem 0.6rem;
    font-size: 0.78rem;
    color: #444;
    max-width: 100%;
}

.file-name {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    max-width: 220px;
}

.file-remove {
    background: none;
    border: none;
    cursor: pointer;
    color: #aaa;
    font-size: 0.75rem;
    padding: 0;
    line-height: 1;
    flex-shrink: 0;
}

.file-remove:hover { color: #e00; }

.file-tag {
    margin-top: 0.35rem;
    font-size: 0.75rem;
    color: #888;
    display: block;
}

.download-btn {
    display: inline-block;
    margin-top: 0.75rem;
    padding: 0.5rem 1rem;
    background: #1A7A8A;
    color: #fff;
    border-radius: 8px;
    text-decoration: none;
    font-size: 0.85rem;
    font-weight: 600;
    transition: background 0.15s;
}

.download-btn:hover { background: #145F6D; }

.hint {
    text-align: center;
    font-size: 0.75rem;
    color: #bbb;
    margin-top: 0.5rem;
}

#empty-state {
    flex: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    color: #bbb;
    gap: 0.5rem;
    text-align: center;
}

#empty-state svg { width: 40px; height: 40px; opacity: 0.4; }
#empty-state p   { font-size: 0.9rem; }

/* ── MARKDOWN RENDERING ── */
.message.assistant .bubble h1,
.message.assistant .bubble h2,
.message.assistant .bubble h3 {
    font-weight: 700;
    margin: 0.75rem 0 0.35rem;
    line-height: 1.3;
}

.message.assistant .bubble h1 { font-size: 1.1rem; }
.message.assistant .bubble h2 { font-size: 1rem; }
.message.assistant .bubble h3 { font-size: 0.95rem; }

.message.assistant .bubble strong { font-weight: 600; }
.message.assistant .bubble em     { font-style: italic; }

.message.assistant .bubble code {
    background: #f0f0f0;
    padding: 0.1em 0.4em;
    border-radius: 4px;
    font-family: monospace;
    font-size: 0.88em;
}

.message.assistant .bubble hr {
    border: none;
    border-top: 1px solid #e5e5e5;
    margin: 0.75rem 0;
}

.message.assistant .bubble ul {
    padding-left: 1.25rem;
    margin: 0.4rem 0;
    display: flex;
    flex-direction: column;
    gap: 0.2rem;
}

.message.assistant .bubble li { font-size: 0.93rem; }

.message.assistant .bubble table {
    width: 100%;
    border-collapse: collapse;
    margin: 0.6rem 0;
    font-size: 0.85rem;
}

.message.assistant .bubble th,
.message.assistant .bubble td {
    border: 1px solid #e5e5e5;
    padding: 0.4rem 0.65rem;
    text-align: left;
}

.message.assistant .bubble th {
    background: #f5f5f5;
    font-weight: 600;
}

.message.assistant .bubble tr:nth-child(even) td { background: #fafafa; }
