:root {
    color-scheme: light;
    --background: #f7f7f8;
    --surface: #ffffff;
    --text: #1d1d1f;
    --muted: #86868b;
    --border: #e5e5ea;
    --accent: #1d1d1f;
    --danger: #b42318;
}

* {
    box-sizing: border-box;
}

html,
body {
    width: 100%;
    height: 100%;
    margin: 0;
    overflow: hidden;
}

body {
    background: var(--background);
    color: var(--text);
    font-family: -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", "PingFang SC", "Microsoft YaHei", sans-serif;
    -webkit-font-smoothing: antialiased;
    text-rendering: optimizeLegibility;
}

button,
textarea {
    font: inherit;
}

button {
    cursor: pointer;
    touch-action: manipulation;
    -webkit-tap-highlight-color: transparent;
}

button:focus-visible,
textarea:focus-visible {
    outline: 2px solid #6e6e73;
    outline-offset: 2px;
}

[hidden] {
    display: none !important;
}

.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-shell {
    width: 100%;
    max-width: 960px;
    height: 100vh;
    height: var(--app-height, 100dvh);
    margin: 0 auto;
    display: flex;
    flex-direction: column;
    overflow: hidden;
    background: var(--background);
}

.topbar {
    flex: 0 0 auto;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 16px;
    padding: calc(10px + env(safe-area-inset-top)) max(18px, env(safe-area-inset-right)) 10px max(18px, env(safe-area-inset-left));
    background: rgba(247, 247, 248, 0.96);
    border-bottom: 1px solid var(--border);
    z-index: 2;
}

.identity {
    display: flex;
    align-items: center;
    min-width: 0;
    gap: 11px;
}

.identity-avatar,
.message-avatar {
    display: flex;
    align-items: center;
    justify-content: center;
    flex: 0 0 auto;
    border-radius: 50%;
    background: var(--accent);
    color: #fff;
    font-weight: 600;
}

.identity-avatar {
    width: 38px;
    height: 38px;
    font-size: 16px;
    overflow: hidden;
}

.identity-avatar img,
.message-avatar img {
    display: block;
    width: 100%;
    height: 100%;
    object-fit: cover;
}

.identity-text h1.is-typing {
    font-size: 14px;
    white-space: nowrap;
}

.identity-text h1 {
    margin: 0;
    font-size: 17px;
    line-height: 1.25;
    font-weight: 600;
}

.identity-text p {
    margin: 2px 0 0;
    color: var(--muted);
    font-size: 12px;
    line-height: 1.2;
}

.new-chat-button {
    min-width: 44px;
    min-height: 44px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 6px;
    padding: 0 10px;
    border: 0;
    border-radius: 12px;
    background: transparent;
    color: #48484a;
    font-size: 14px;
}

.new-chat-button:active {
    background: #ececee;
}

.new-chat-button svg {
    width: 20px;
    height: 20px;
    fill: none;
    stroke: currentColor;
    stroke-width: 1.8;
    stroke-linecap: round;
}

.messages {
    flex: 1 1 auto;
    min-height: 0;
    overflow-y: auto;
    overscroll-behavior: contain;
    -webkit-overflow-scrolling: touch;
    padding: 22px max(20px, env(safe-area-inset-right)) 18px max(20px, env(safe-area-inset-left));
    scroll-behavior: smooth;
}

.message {
    display: flex;
    align-items: flex-start;
    min-width: 0;
    gap: 10px;
    margin: 0 0 20px;
}

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

.message-avatar {
    width: 30px;
    height: 30px;
    margin-top: 2px;
    font-size: 12px;
}

.message.user .message-avatar {
    background: #d9d9de;
    color: #48484a;
}

.message-bubble {
    min-width: 0;
    max-width: min(78%, 720px);
    padding: 10px 13px;
    border: 1px solid var(--border);
    border-radius: 5px 18px 18px 18px;
    background: var(--surface);
    box-shadow: 0 1px 1px rgba(0, 0, 0, 0.02);
    overflow-wrap: anywhere;
}

.message.user .message-bubble {
    border-color: var(--accent);
    border-radius: 18px 5px 18px 18px;
    background: var(--accent);
    color: #fff;
    box-shadow: none;
}

.plain-text,
.markdown {
    font-size: 15px;
    line-height: 1.65;
}

.plain-text {
    white-space: pre-wrap;
    overflow-wrap: anywhere;
}

.markdown > :first-child {
    margin-top: 0;
}

.markdown > :last-child {
    margin-bottom: 0;
}

.markdown p {
    margin: 0 0 10px;
}

.markdown h1,
.markdown h2,
.markdown h3,
.markdown h4,
.markdown h5,
.markdown h6 {
    margin: 18px 0 8px;
    line-height: 1.35;
}

.markdown h1 { font-size: 22px; }
.markdown h2 { font-size: 20px; }
.markdown h3 { font-size: 18px; }
.markdown h4,
.markdown h5,
.markdown h6 { font-size: 16px; }

.markdown ul,
.markdown ol {
    margin: 8px 0;
    padding-left: 24px;
}

.markdown li {
    margin: 4px 0;
}

.markdown blockquote {
    margin: 10px 0;
    padding: 6px 12px;
    border-left: 3px solid #c7c7cc;
    background: #f2f2f4;
    color: #555558;
}

.markdown code {
    padding: 2px 5px;
    border-radius: 5px;
    background: #eeeeF1;
    font-family: "SFMono-Regular", Consolas, "Liberation Mono", monospace;
    font-size: 0.9em;
}

.markdown pre {
    max-width: 100%;
    margin: 10px 0;
    padding: 13px;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    border-radius: 10px;
    background: #202023;
    color: #f5f5f7;
}

.markdown pre code {
    padding: 0;
    background: transparent;
    color: inherit;
    white-space: pre;
}

.markdown a {
    color: #1769aa;
    text-decoration: underline;
    text-underline-offset: 2px;
}

.markdown img {
    display: block;
    max-width: 100%;
    height: auto;
    margin: 8px 0;
    border-radius: 10px;
}

.markdown table {
    display: block;
    max-width: 100%;
    margin: 10px 0;
    overflow-x: auto;
    border-collapse: collapse;
    -webkit-overflow-scrolling: touch;
}

.markdown th,
.markdown td {
    padding: 7px 10px;
    border: 1px solid var(--border);
    text-align: left;
    white-space: nowrap;
}

.markdown hr {
    margin: 16px 0;
    border: 0;
    border-top: 1px solid var(--border);
}

.typing-message .message-bubble {
    min-width: 58px;
    padding: 14px 15px;
}

.typing-dots {
    display: flex;
    align-items: center;
    gap: 5px;
    height: 10px;
}

.typing-dots span {
    width: 6px;
    height: 6px;
    border-radius: 50%;
    background: #8e8e93;
    animation: typing 1.15s infinite ease-in-out;
}

.typing-dots span:nth-child(2) { animation-delay: 0.16s; }
.typing-dots span:nth-child(3) { animation-delay: 0.32s; }

@keyframes typing {
    0%, 65%, 100% { opacity: 0.3; transform: translateY(0); }
    30% { opacity: 1; transform: translateY(-3px); }
}

.error-bar {
    flex: 0 0 auto;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 12px;
    padding: 8px max(16px, env(safe-area-inset-right)) 0 max(16px, env(safe-area-inset-left));
    color: var(--danger);
    font-size: 13px;
}

.error-bar span {
    min-width: 0;
    overflow-wrap: anywhere;
}

.error-bar button {
    flex: 0 0 auto;
    min-width: 44px;
    min-height: 36px;
    border: 0;
    background: transparent;
    color: var(--danger);
    font-weight: 600;
}

.composer-wrap {
    flex: 0 0 auto;
    padding: 10px max(14px, env(safe-area-inset-right)) calc(7px + env(safe-area-inset-bottom)) max(14px, env(safe-area-inset-left));
    border-top: 1px solid var(--border);
    background: rgba(247, 247, 248, 0.98);
    z-index: 2;
}

.composer {
    display: flex;
    align-items: flex-end;
    gap: 6px;
    width: 100%;
    padding: 5px 5px 5px 5px;
    border: 1px solid #d9d9de;
    border-radius: 23px;
    background: var(--surface);
}

.composer:focus-within {
    border-color: #aeaeb2;
    box-shadow: 0 0 0 3px rgba(0, 0, 0, 0.035);
}

#messageInput {
    flex: 1 1 auto;
    min-width: 0;
    height: 44px;
    min-height: 44px;
    max-height: 120px;
    padding: 10px 0;
    resize: none;
    overflow-y: auto;
    border: 0;
    outline: 0;
    background: transparent;
    color: var(--text);
    font-size: 16px;
    line-height: 1.45;
    -webkit-appearance: none;
}

#messageInput::placeholder {
    color: #8e8e93;
}

.send-button {
    flex: 0 0 44px;
    width: 44px;
    height: 44px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    padding: 0;
    border: 0;
    border-radius: 50%;
    background: var(--accent);
    color: #fff;
    transition: opacity 0.15s ease, transform 0.15s ease;
}

.send-button:active:not(:disabled) {
    transform: scale(0.94);
}

.send-button:disabled {
    cursor: default;
    opacity: 0.28;
}

.send-button svg {
    width: 21px;
    height: 21px;
    fill: none;
    stroke: currentColor;
    stroke-width: 1.7;
    stroke-linecap: round;
    stroke-linejoin: round;
}

.local-note {
    padding-top: 5px;
    color: var(--muted);
    font-size: 11px;
    line-height: 1.25;
    text-align: center;
}

.attach-button {
    flex: 0 0 40px;
    width: 40px;
    height: 44px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    padding: 0;
    border: 0;
    border-radius: 50%;
    background: transparent;
    color: #6e6e73;
}

.attach-button:active:not(:disabled) {
    background: #ececee;
}

.attach-button:disabled {
    cursor: default;
    opacity: 0.35;
}

.attach-button svg {
    width: 22px;
    height: 22px;
    fill: none;
    stroke: currentColor;
    stroke-width: 1.6;
    stroke-linecap: round;
    stroke-linejoin: round;
}

.attachments {
    display: flex;
    gap: 8px;
    padding: 0 2px 8px;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
}

.attachment {
    position: relative;
    flex: 0 0 auto;
    width: 62px;
    height: 62px;
    border-radius: 12px;
    overflow: hidden;
    background: #ececee;
}

.attachment img {
    display: block;
    width: 100%;
    height: 100%;
    object-fit: cover;
}

.attachment-remove {
    position: absolute;
    top: 2px;
    right: 2px;
    width: 22px;
    height: 22px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    padding: 0;
    border: 0;
    border-radius: 50%;
    background: rgba(0, 0, 0, 0.58);
    color: #fff;
    font-size: 14px;
    line-height: 1;
}

.message-images {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 4px;
    margin-bottom: 6px;
}

.message-images.single {
    grid-template-columns: minmax(0, 1fr);
}

.message-images img {
    display: block;
    width: 100%;
    max-height: 260px;
    object-fit: cover;
    border-radius: 10px;
    background: #ececee;
}

.message-bubble.has-images {
    padding: 6px;
}

.message-bubble.has-images .plain-text {
    padding: 2px 7px 5px;
}

.image-note {
    padding: 2px 0 4px;
    font-size: 12px;
    opacity: 0.6;
}

@media (max-width: 600px) {
    .topbar {
        padding-bottom: 8px;
    }

    .messages {
        padding-top: 17px;
        padding-left: max(12px, env(safe-area-inset-left));
        padding-right: max(12px, env(safe-area-inset-right));
    }

    .message {
        gap: 8px;
        margin-bottom: 16px;
    }

    .message-bubble {
        max-width: 84%;
        padding: 9px 12px;
    }

    .message-avatar {
        width: 28px;
        height: 28px;
    }

    .new-chat-button {
        padding: 0 7px;
    }
}

@media (max-width: 360px) {
    .new-chat-button span {
        display: none;
    }

    .message-bubble {
        max-width: 86%;
    }
}

@media (prefers-reduced-motion: reduce) {
    *,
    *::before,
    *::after {
        scroll-behavior: auto !important;
        transition: none !important;
        animation: none !important;
    }
}
