:root {
    --bg: #f7f8f5;
    --panel: #ffffff;
    --panel-soft: #f1f5f1;
    --panel-active: #e8f3ef;
    --panel-ok: #edf7f3;
    --panel-warn: #fff7e8;
    --panel-danger: #fff5f2;
    --panel-error: #fff4f1;
    --panel-code: #f8faf8;
    --sidebar-bg: rgba(255, 255, 255, 0.78);
    --header-bg: rgba(247, 248, 245, 0.92);
    --composer-bg: rgba(255, 255, 255, 0.88);
    --page-header-bg: rgba(255, 255, 255, 0.82);
    --ink: #22272a;
    --muted: #687176;
    --line: #dce3df;
    --accent: #0f766e;
    --accent-strong: #0b5f59;
    --warn: #a15608;
    --danger: #b42318;
    --scrollbar-track: rgba(220, 227, 223, 0.34);
    --scrollbar-thumb: rgba(104, 113, 118, 0.34);
    --scrollbar-thumb-hover: rgba(15, 118, 110, 0.56);
    --shadow: 0 18px 42px rgba(38, 49, 55, 0.12);
}

:root[data-theme="dark"] {
    --bg: #101312;
    --panel: #171d1b;
    --panel-soft: #202826;
    --panel-active: #14332f;
    --panel-ok: #14332f;
    --panel-warn: #2d2416;
    --panel-danger: #321b19;
    --panel-error: #351d1b;
    --panel-code: #111715;
    --sidebar-bg: rgba(23, 29, 27, 0.88);
    --header-bg: rgba(16, 19, 18, 0.92);
    --composer-bg: rgba(23, 29, 27, 0.9);
    --page-header-bg: rgba(23, 29, 27, 0.86);
    --ink: #e7ece9;
    --muted: #9ca9a5;
    --line: #2b3633;
    --accent: #2fb9a9;
    --accent-strong: #62d2c6;
    --warn: #e0a34e;
    --danger: #f16d5f;
    --scrollbar-track: rgba(43, 54, 51, 0.38);
    --scrollbar-thumb: rgba(156, 169, 165, 0.34);
    --scrollbar-thumb-hover: rgba(98, 210, 198, 0.58);
    --shadow: 0 18px 42px rgba(0, 0, 0, 0.36);
}

* {
    box-sizing: border-box;
}

[hidden] {
    display: none !important;
}

html,
body {
    min-height: 100%;
    scrollbar-color: var(--scrollbar-thumb) var(--scrollbar-track);
    scrollbar-width: thin;
}

* {
    scrollbar-color: var(--scrollbar-thumb) transparent;
    scrollbar-width: thin;
}

::-webkit-scrollbar {
    width: 11px;
    height: 11px;
}

::-webkit-scrollbar-track {
    background: var(--scrollbar-track);
}

::-webkit-scrollbar-thumb {
    min-height: 42px;
    border: 3px solid transparent;
    border-radius: 999px;
    background: var(--scrollbar-thumb);
    background-clip: padding-box;
}

::-webkit-scrollbar-thumb:hover {
    background: var(--scrollbar-thumb-hover);
    background-clip: padding-box;
}

::-webkit-scrollbar-corner {
    background: transparent;
}

::selection {
    background: rgba(47, 185, 169, 0.24);
}

body {
    margin: 0;
    background: var(--bg);
    color: var(--ink);
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Microsoft YaHei", sans-serif;
    letter-spacing: 0;
}

button,
input,
select,
textarea {
    font: inherit;
}

button {
    cursor: pointer;
}

.app-shell {
    display: grid;
    grid-template-columns: minmax(280px, 340px) minmax(0, 1fr);
    min-height: 100dvh;
}

.sidebar {
    display: flex;
    flex-direction: column;
    gap: 18px;
    height: 100dvh;
    overflow: auto;
    border-right: 1px solid var(--line);
    background: var(--sidebar-bg);
    padding: 18px;
}

.brand {
    display: flex;
    align-items: center;
    padding-bottom: 16px;
    border-bottom: 1px solid var(--line);
}

.brand h1,
.chat-header h2,
.side-section h2 {
    margin: 0;
}

.brand h1 {
    font-size: 20px;
    line-height: 1.2;
}

.brand p,
.eyebrow {
    margin: 4px 0 0;
    color: var(--muted);
    font-size: 13px;
}

.brand .beta-note,
.page-header .beta-note {
    color: var(--accent-strong);
    font-weight: 650;
}

.brand .issue-note,
.page-header .issue-note {
    font-size: 12px;
}

.brand .issue-note a,
.page-header .issue-note a {
    color: var(--accent-strong);
    text-decoration: none;
}

.brand .issue-note a:hover,
.page-header .issue-note a:hover {
    text-decoration: underline;
}

.app-nav {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
}

.app-nav a {
    min-height: 34px;
    border: 1px solid var(--line);
    border-radius: 8px;
    color: var(--muted);
    padding: 7px 11px;
    text-decoration: none;
}

.app-nav a:hover,
.app-nav a.is-active {
    border-color: rgba(15, 118, 110, 0.34);
    background: var(--panel-active);
    color: var(--accent-strong);
}

.theme-toggle {
    min-height: 34px;
    border: 1px solid var(--line);
    border-radius: 8px;
    background: var(--panel);
    color: var(--muted);
    padding: 7px 11px;
}

.theme-toggle:hover {
    border-color: rgba(15, 118, 110, 0.34);
    color: var(--accent-strong);
}

.side-section {
    display: grid;
    gap: 12px;
}

.section-head,
.row-actions,
.sidebar-footer,
.composer-actions {
    display: flex;
    align-items: center;
    gap: 10px;
}

.section-head {
    justify-content: space-between;
}

.side-section h2 {
    font-size: 14px;
    color: var(--muted);
    font-weight: 700;
}

.chat-list {
    display: grid;
    gap: 8px;
}

.chat-row {
    display: grid;
    grid-template-columns: minmax(0, 1fr) auto;
    align-items: stretch;
    gap: 8px;
    border: 1px solid transparent;
    border-radius: 8px;
}

.chat-row.is-active {
    border-color: rgba(15, 118, 110, 0.28);
    background: var(--panel-active);
}

.chat-item {
    width: 100%;
    min-height: 44px;
    border: 0;
    border-radius: 8px;
    background: transparent;
    color: var(--ink);
    padding: 9px 10px;
    text-align: left;
}

.chat-item strong,
.chat-item span {
    display: block;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.chat-item strong {
    font-size: 14px;
}

.chat-item span {
    margin-top: 2px;
    color: var(--muted);
    font-size: 12px;
}

.chat-delete {
    align-self: center;
    min-width: 48px;
    min-height: 34px;
    border: 1px solid transparent;
    border-radius: 8px;
    background: transparent;
    color: var(--muted);
    font-size: 12px;
}

.chat-delete:hover {
    border-color: rgba(180, 35, 24, 0.24);
    background: var(--panel-danger);
    color: var(--danger);
}

.chat-delete:disabled {
    cursor: not-allowed;
    opacity: 0.45;
}

.settings label {
    display: grid;
    gap: 6px;
    color: var(--muted);
    font-size: 13px;
}

.settings input,
.settings select,
.settings textarea,
.composer textarea {
    width: 100%;
    border: 1px solid var(--line);
    border-radius: 8px;
    background: var(--panel);
    color: var(--ink);
    outline: none;
}

.settings input,
.settings select {
    min-height: 38px;
    padding: 7px 10px;
}

.model-picker {
    position: relative;
    display: grid;
    grid-template-columns: minmax(0, 1fr) 40px;
}

.model-picker input {
    border-bottom-right-radius: 0;
    border-top-right-radius: 0;
}

.model-menu-button {
    min-height: 38px;
    border: 1px solid var(--line);
    border-left: 0;
    border-radius: 0 8px 8px 0;
    background: var(--panel);
    color: var(--muted);
}

.model-menu-button:hover {
    color: var(--accent-strong);
}

.model-menu-button:disabled {
    cursor: not-allowed;
    opacity: 0.5;
}

.model-menu {
    position: absolute;
    z-index: 10;
    top: calc(100% + 6px);
    right: 0;
    left: 0;
    display: grid;
    gap: 4px;
    max-height: 220px;
    overflow: auto;
    border: 1px solid var(--line);
    border-radius: 8px;
    background: var(--panel);
    box-shadow: var(--shadow);
    padding: 6px;
}

.model-menu[hidden] {
    display: none;
}

.model-option {
    width: 100%;
    min-height: 34px;
    border: 0;
    border-radius: 6px;
    background: transparent;
    color: var(--ink);
    overflow-wrap: anywhere;
    padding: 7px 9px;
    text-align: left;
}

.model-option:hover,
.model-option:focus {
    background: var(--panel-soft);
    outline: none;
}

.model-option.is-selected {
    background: var(--panel-active);
    color: var(--accent-strong);
    font-weight: 700;
}

.settings textarea {
    resize: vertical;
    min-height: 82px;
    padding: 9px 10px;
}

.settings input:focus,
.settings select:focus,
.settings textarea:focus,
.composer textarea:focus {
    border-color: rgba(15, 118, 110, 0.56);
    box-shadow: 0 0 0 3px rgba(15, 118, 110, 0.12);
}

.check-row {
    display: flex !important;
    grid-template-columns: none !important;
    align-items: center;
    gap: 9px !important;
}

.check-row input {
    width: 16px;
    min-height: 16px;
}

.request-preview,
.connection-feedback {
    border: 1px solid var(--line);
    border-radius: 8px;
    background: var(--panel-soft);
    color: var(--muted);
    font-size: 12px;
    line-height: 1.55;
    overflow-wrap: anywhere;
    padding: 8px 10px;
}

.request-preview {
    white-space: pre-line;
}

.connection-feedback:empty {
    display: none;
}

.connection-feedback.is-ok {
    border-color: rgba(15, 118, 110, 0.22);
    background: var(--panel-ok);
    color: var(--accent-strong);
}

.connection-feedback.is-warn {
    border-color: rgba(161, 86, 8, 0.24);
    background: var(--panel-warn);
    color: var(--warn);
}

.connection-feedback.is-error {
    border-color: rgba(180, 35, 24, 0.22);
    background: var(--panel-error);
    color: var(--danger);
}

.preview-line {
    display: block;
}

.settings-grid {
    display: grid;
    grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
    gap: 10px;
}

.row-actions,
.sidebar-footer {
    justify-content: stretch;
}

.row-actions button,
.sidebar-footer button {
    flex: 1;
}

.ghost-button,
.send-button {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    min-height: 38px;
    border-radius: 8px;
    padding: 8px 13px;
    border: 1px solid var(--line);
    background: var(--panel);
    color: var(--ink);
    text-decoration: none;
}

.ghost-button:hover {
    border-color: rgba(15, 118, 110, 0.42);
    color: var(--accent-strong);
}

.ghost-button:disabled,
.send-button:disabled {
    cursor: not-allowed;
    opacity: 0.55;
}

.ghost-button.danger {
    color: var(--danger);
}

.send-button {
    border-color: var(--accent);
    background: var(--accent);
    color: #fff;
    font-weight: 700;
}

.send-button:hover {
    background: var(--accent-strong);
}

.chat-panel {
    display: grid;
    grid-template-rows: auto minmax(0, 1fr) auto;
    min-width: 0;
    height: 100dvh;
}

.image-shell {
    display: grid;
    grid-template-columns: minmax(280px, 340px) minmax(0, 1fr);
    min-height: 100dvh;
}

.image-panel {
    display: grid;
    grid-template-rows: auto minmax(0, 1fr);
    min-width: 0;
    height: 100dvh;
}

.chat-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 18px;
    min-height: 74px;
    border-bottom: 1px solid var(--line);
    padding: 16px 22px;
    background: var(--header-bg);
}

.chat-header h2 {
    font-size: 18px;
    line-height: 1.25;
}

.status-pill {
    flex: 0 0 auto;
    border: 1px solid rgba(15, 118, 110, 0.24);
    border-radius: 999px;
    background: var(--panel-active);
    color: var(--accent-strong);
    padding: 7px 11px;
    font-size: 12px;
    font-weight: 700;
}

.status-pill.is-warn {
    border-color: rgba(161, 86, 8, 0.28);
    background: var(--panel-warn);
    color: var(--warn);
}

.status-pill.is-error {
    border-color: rgba(180, 35, 24, 0.25);
    background: var(--panel-error);
    color: var(--danger);
}

.messages {
    overflow: auto;
    padding: 24px clamp(16px, 4vw, 48px);
}

.empty-state {
    display: grid;
    place-items: center;
    min-height: 100%;
    color: var(--muted);
    text-align: center;
}

.empty-state p {
    max-width: 360px;
    margin: 0;
    line-height: 1.7;
}

.message {
    display: grid;
    grid-template-columns: minmax(62px, 84px) minmax(0, 780px);
    gap: 14px;
    margin: 0 0 18px;
}

.message-role {
    color: var(--muted);
    font-size: 12px;
    font-weight: 700;
    padding-top: 12px;
    text-align: right;
}

.message-body {
    border: 1px solid var(--line);
    border-radius: 8px;
    background: var(--panel);
    box-shadow: 0 8px 20px rgba(38, 49, 55, 0.05);
    padding: 13px 15px;
    white-space: pre-wrap;
    overflow-wrap: anywhere;
    line-height: 1.65;
}

.markdown-body {
    white-space: normal;
}

.markdown-body > :first-child,
.reasoning-block .markdown-body > :first-child {
    margin-top: 0;
}

.markdown-body > :last-child,
.reasoning-block .markdown-body > :last-child {
    margin-bottom: 0;
}

.markdown-body p,
.markdown-body ul,
.markdown-body ol,
.markdown-body pre,
.markdown-body blockquote {
    margin: 0 0 12px;
}

.markdown-body ul,
.markdown-body ol {
    padding-left: 22px;
}

.markdown-body code {
    border-radius: 5px;
    background: var(--panel-soft);
    padding: 1px 5px;
    font-family: "Cascadia Code", Consolas, monospace;
    font-size: 0.92em;
}

.markdown-body pre {
    overflow: auto;
    border: 1px solid var(--line);
    border-radius: 8px;
    background: var(--panel-code);
    padding: 11px 12px;
}

.markdown-body pre code {
    display: block;
    background: transparent;
    padding: 0;
}

.markdown-body table {
    width: 100%;
    margin: 0 0 12px;
    border-collapse: collapse;
    font-size: 0.96em;
}

.markdown-body th,
.markdown-body td {
    border: 1px solid var(--line);
    padding: 7px 9px;
    text-align: left;
    vertical-align: top;
}

.markdown-body th {
    background: var(--panel-soft);
    color: var(--ink);
    font-weight: 700;
}

.markdown-body blockquote {
    border-left: 3px solid rgba(15, 118, 110, 0.34);
    color: var(--muted);
    padding-left: 12px;
}

.reasoning-block {
    margin: 0 0 12px;
    border: 1px solid rgba(161, 86, 8, 0.18);
    border-radius: 8px;
    background: var(--panel-warn);
    color: var(--ink);
    padding: 8px 10px;
}

.reasoning-block summary {
    cursor: pointer;
    color: var(--warn);
    font-weight: 700;
}

.reasoning-block .markdown-body {
    margin-top: 8px;
    color: var(--muted);
}

.reasoning-preview {
    display: none;
    max-height: 4.8em;
    overflow: hidden;
    margin: 8px 0 0;
    white-space: pre-wrap;
    color: var(--muted);
    font: inherit;
    line-height: 1.55;
}

.reasoning-block.is-live-preview:not(.is-user-open) .reasoning-preview {
    display: block;
}

.reasoning-block.is-live-preview:not(.is-user-open) .markdown-body {
    display: none;
}

.reasoning-block.is-live-preview:not(.is-user-open) summary::after {
    content: " · 实时预览";
    color: var(--muted);
    font-weight: 500;
}

.message.user .message-body {
    border-color: rgba(15, 118, 110, 0.24);
    background: var(--panel-active);
}

.message.error .message-body {
    border-color: rgba(180, 35, 24, 0.24);
    background: var(--panel-danger);
    color: var(--danger);
}

.composer {
    display: grid;
    grid-template-columns: minmax(0, 1fr) auto;
    gap: 12px;
    align-items: end;
    border-top: 1px solid var(--line);
    background: var(--composer-bg);
    padding: 16px clamp(16px, 4vw, 48px);
}

.attachment-strip {
    grid-column: 1 / -1;
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
}

.attachment-strip[hidden] {
    display: none;
}

.attachment-chip {
    display: inline-grid;
    grid-template-columns: 44px minmax(0, 1fr) auto;
    align-items: center;
    gap: 8px;
    max-width: min(100%, 320px);
    border: 1px solid var(--line);
    border-radius: 8px;
    background: var(--panel);
    padding: 6px;
}

.attachment-chip img {
    width: 44px;
    height: 44px;
    border-radius: 6px;
    object-fit: cover;
}

.attachment-chip span {
    overflow: hidden;
    color: var(--muted);
    font-size: 12px;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.remove-attachment {
    min-width: 30px;
    min-height: 30px;
    padding: 4px 8px;
}

.composer textarea {
    resize: none;
    min-height: 44px;
    max-height: 180px;
    padding: 11px 13px;
    line-height: 1.5;
}

.message-images {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
    margin-top: 10px;
}

.message-images img {
    width: min(180px, 100%);
    max-height: 180px;
    border: 1px solid var(--line);
    border-radius: 8px;
    object-fit: cover;
}

.message-actions {
    display: flex;
    flex-wrap: wrap;
    gap: 6px;
    margin-top: 10px;
}

.message-action {
    min-height: 28px;
    border: 1px solid var(--line);
    border-radius: 7px;
    background: var(--panel-soft);
    color: var(--muted);
    font-size: 12px;
    padding: 4px 8px;
}

.message-action:hover {
    border-color: rgba(15, 118, 110, 0.32);
    color: var(--accent-strong);
}

.message-action.danger:hover {
    border-color: rgba(180, 35, 24, 0.38);
    color: var(--danger);
}

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

.message-editor {
    display: grid;
    gap: 10px;
}

.message-editor textarea {
    width: 100%;
    min-height: 100px;
    resize: vertical;
    border: 1px solid var(--line);
    border-radius: 8px;
    background: var(--panel);
    color: var(--ink);
    line-height: 1.6;
    outline: none;
    padding: 10px 11px;
}

.settings-shell {
    min-height: 100dvh;
}

.page-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 18px;
    border-bottom: 1px solid var(--line);
    background: var(--page-header-bg);
    padding: 18px clamp(18px, 4vw, 44px);
}

.page-header h1 {
    margin: 0;
    font-size: 22px;
    line-height: 1.2;
}

.settings-page {
    display: grid;
    grid-template-columns: minmax(240px, 340px) minmax(0, 560px);
    gap: 18px;
    padding: 22px clamp(18px, 4vw, 44px);
}

.preset-panel {
    display: grid;
    align-content: start;
    gap: 12px;
    min-width: 0;
}

.segmented-control {
    display: inline-flex;
    overflow: hidden;
    border: 1px solid var(--line);
    border-radius: 8px;
}

.segmented-control button {
    min-height: 34px;
    border: 0;
    background: var(--panel);
    color: var(--muted);
    padding: 7px 11px;
}

.segmented-control button.is-active {
    background: var(--panel-active);
    color: var(--accent-strong);
    font-weight: 700;
}

.preset-list {
    display: grid;
    gap: 8px;
}

.preset-item {
    min-height: 40px;
    border: 1px solid var(--line);
    border-radius: 8px;
    background: var(--panel);
    color: var(--ink);
    overflow-wrap: anywhere;
    padding: 9px 10px;
    text-align: left;
}

.preset-item.is-active {
    border-color: rgba(15, 118, 110, 0.34);
    background: var(--panel-active);
    color: var(--accent-strong);
    font-weight: 700;
}

.image-workbench {
    display: grid;
    grid-template-columns: minmax(280px, 420px) minmax(0, 1fr);
    gap: 18px;
    overflow: auto;
    padding: 20px clamp(16px, 4vw, 44px);
}

.image-composer {
    display: grid;
    align-content: start;
    gap: 12px;
    min-width: 0;
}

.image-composer label {
    display: grid;
    gap: 6px;
    color: var(--muted);
    font-size: 13px;
}

.image-composer textarea {
    width: 100%;
    min-height: 150px;
    resize: vertical;
    border: 1px solid var(--line);
    border-radius: 8px;
    background: var(--panel);
    color: var(--ink);
    line-height: 1.55;
    outline: none;
    padding: 11px 13px;
}

.image-results {
    display: grid;
    align-content: start;
    gap: 16px;
    min-width: 0;
}

.image-job {
    display: grid;
    gap: 10px;
    border: 1px solid var(--line);
    border-radius: 8px;
    background: var(--panel);
    padding: 12px;
}

.image-job p {
    margin: 0;
    color: var(--ink);
    line-height: 1.6;
    overflow-wrap: anywhere;
}

.image-job-meta {
    color: var(--muted);
    font-size: 12px;
}

.image-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(180px, 1fr));
    gap: 10px;
}

.image-grid a {
    display: block;
    min-width: 0;
}

.image-grid img {
    display: block;
    width: 100%;
    aspect-ratio: 1;
    border: 1px solid var(--line);
    border-radius: 8px;
    object-fit: cover;
}

@media (max-width: 860px) {
    .app-shell {
        grid-template-columns: 1fr;
    }

    .image-shell,
    .settings-page,
    .image-workbench {
        grid-template-columns: 1fr;
    }

    .sidebar {
        height: auto;
        max-height: none;
        border-right: 0;
        border-bottom: 1px solid var(--line);
    }

    .chat-panel {
        height: auto;
        min-height: 70dvh;
    }

    .image-panel {
        height: auto;
        min-height: 70dvh;
    }

    .message {
        grid-template-columns: 1fr;
        gap: 6px;
    }

    .message-role {
        padding-top: 0;
        text-align: left;
    }

    .composer {
        grid-template-columns: 1fr;
    }

    .composer-actions {
        justify-content: flex-end;
    }
}

@media (max-width: 520px) {
    .sidebar,
    .chat-header,
    .messages,
    .composer {
        padding-left: 14px;
        padding-right: 14px;
    }

    .chat-header {
        align-items: flex-start;
        flex-direction: column;
    }

    .page-header {
        align-items: flex-start;
        flex-direction: column;
    }

    .settings-grid {
        grid-template-columns: 1fr;
    }

    .composer-actions {
        display: grid;
        grid-template-columns: 1fr 1fr;
    }
}
