:root {
    color-scheme: dark;
    --bg: #090d18;
    --panel: #111827;
    --panel-2: #172033;
    --border: #29344b;
    --text: #edf2ff;
    --muted: #9eabc3;
    --primary: #6c7cff;
    --primary-hover: #8390ff;
    --danger: #d85566;
    --success: #37b778;
}

* {
    box-sizing: border-box;
}

body {
    margin: 0;
    min-height: 100vh;
    background:
        radial-gradient(circle at top right, #17245a 0, transparent 38%),
        var(--bg);
    color: var(--text);
    font-family:
        system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI",
        "Microsoft YaHei", sans-serif;
}

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

button {
    border: 1px solid var(--border);
    border-radius: 9px;
    padding: 9px 14px;
    background: var(--panel-2);
    color: var(--text);
    cursor: pointer;
}

button:hover {
    border-color: #536184;
}

button:disabled {
    opacity: 0.55;
    cursor: not-allowed;
}

button.primary {
    background: var(--primary);
    border-color: var(--primary);
    color: white;
}

button.primary:hover {
    background: var(--primary-hover);
}

button.danger {
    background: transparent;
    border-color: var(--danger);
    color: #ff98a5;
}

button.full {
    width: 100%;
}

label {
    display: grid;
    gap: 7px;
    margin-bottom: 15px;
    color: var(--muted);
    font-size: 14px;
}

input,
textarea,
select {
    width: 100%;
    border: 1px solid var(--border);
    border-radius: 9px;
    padding: 10px 12px;
    background: #0c1322;
    color: var(--text);
    outline: none;
}

input:focus,
textarea:focus,
select:focus {
    border-color: var(--primary);
}

textarea {
    resize: vertical;
}

[hidden] {
    display: none !important;
}

.muted {
    color: var(--muted);
    line-height: 1.6;
}

.auth-view {
    min-height: 100vh;
    display: grid;
    place-items: center;
    padding: 24px;
}

.auth-card {
    width: min(420px, 100%);
    padding: 30px;
    border: 1px solid var(--border);
    border-radius: 18px;
    background: rgba(17, 24, 39, 0.95);
    box-shadow: 0 18px 60px rgba(0, 0, 0, 0.35);
}

.auth-card h1 {
    margin: 0 0 6px;
}

.tabs {
    display: flex;
    gap: 8px;
    margin: 24px 0;
}

.tab,
.nav-button {
    background: transparent;
}

.tab.active,
.nav-button.active {
    background: var(--panel-2);
    border-color: var(--primary);
}

.topbar {
    min-height: 62px;
    padding: 10px 22px;
    border-bottom: 1px solid var(--border);
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 15px;
    background: rgba(9, 13, 24, 0.92);
    backdrop-filter: blur(12px);
}

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

.badge {
    margin-left: 10px;
    padding: 3px 8px;
    border-radius: 999px;
    background: var(--panel-2);
    color: var(--muted);
    font-size: 12px;
}

.danger-text {
    color: #ff93a1;
}

.workspace {
    min-height: calc(100vh - 62px);
    display: grid;
    grid-template-columns: 260px minmax(0, 1fr);
}

.sidebar {
    padding: 16px;
    border-right: 1px solid var(--border);
    background: rgba(17, 24, 39, 0.75);
}

.sidebar h2 {
    margin: 22px 0 10px;
    font-size: 15px;
    color: var(--muted);
}

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

.list-item {
    width: 100%;
    display: grid;
    gap: 4px;
    text-align: left;
    padding: 11px;
    border-radius: 10px;
    background: var(--panel-2);
    overflow-wrap: anywhere;
}

.list-item.active {
    border-color: var(--primary);
}

.list-item-title {
    color: var(--text);
}

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

.chat-area {
    min-width: 0;
    display: grid;
    grid-template-rows: auto minmax(300px, 1fr) auto;
    max-height: calc(100vh - 62px);
}

.chat-toolbar {
    padding: 12px 18px;
    border-bottom: 1px solid var(--border);
    display: flex;
    gap: 12px;
    align-items: end;
    flex-wrap: wrap;
}

.chat-toolbar label {
    margin: 0;
}

.chat-toolbar label:first-child {
    min-width: 250px;
    flex: 1;
}

.chat-toolbar input[type="number"] {
    width: 100px;
}

.check-label {
    display: flex;
    align-items: center;
    gap: 8px;
}

.check-label input {
    width: auto;
}

.messages {
    overflow-y: auto;
    padding: 22px;
}

.message {
    max-width: 900px;
    margin: 0 auto 16px;
    padding: 14px 16px;
    border: 1px solid var(--border);
    border-radius: 13px;
    background: var(--panel);
    white-space: pre-wrap;
    overflow-wrap: anywhere;
    line-height: 1.65;
}

.message.user {
    border-color: #42519b;
    background: #162149;
}

.message.assistant {
    background: #111a2a;
}

.message-label {
    margin-bottom: 7px;
    color: var(--muted);
    font-size: 12px;
}

.empty-message {
    padding: 70px 20px;
    text-align: center;
    color: var(--muted);
}

.composer {
    padding: 15px 18px;
    border-top: 1px solid var(--border);
    display: grid;
    grid-template-columns: minmax(0, 1fr) auto;
    gap: 10px;
    align-items: end;
}

.composer textarea {
    min-height: 76px;
}

.config-panel {
    padding: 22px;
}

.config-layout {
    display: grid;
    grid-template-columns: minmax(280px, 0.8fr) minmax(350px, 1.2fr);
    gap: 18px;
}

.card {
    padding: 20px;
    border: 1px solid var(--border);
    border-radius: 14px;
    background: rgba(17, 24, 39, 0.88);
}

.section-heading,
.button-row {
    display: flex;
    justify-content: space-between;
    gap: 10px;
    align-items: center;
}

.warning {
    margin: 14px;
    padding: 13px;
    border: 1px solid #9b7733;
    border-radius: 10px;
    background: #3a2b10;
    color: #ffd486;
}

.toast {
    position: fixed;
    z-index: 100;
    top: 18px;
    left: 50%;
    transform: translateX(-50%);
    max-width: min(600px, calc(100% - 30px));
    padding: 12px 17px;
    border: 1px solid var(--border);
    border-radius: 10px;
    background: #172033;
    box-shadow: 0 10px 35px rgba(0, 0, 0, 0.4);
}

.toast.error {
    border-color: var(--danger);
    color: #ffb1bb;
}

@media (max-width: 800px) {
    .topbar {
        align-items: flex-start;
        flex-direction: column;
    }

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

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

    .chat-area {
        max-height: none;
        min-height: 70vh;
    }

    .config-layout {
        grid-template-columns: 1fr;
    }

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

/* Inline vision layout fix */
.composer {
    display: grid;
    grid-template-columns: minmax(0, 1fr) auto;
    grid-template-rows: auto auto;
    align-items: end;
    gap: 8px;
}

.composer .inline-vision-controls {
    grid-column: 1 / -1;
    grid-row: 1;
    width: 100%;
    min-width: 0;
    margin: 0;
    padding: 6px 0;
    display: flex;
    align-items: center;
    gap: 8px;
    flex-wrap: wrap;
}

.composer .inline-vision-controls button {
    width: auto;
    min-width: 0;
    flex: 0 0 auto;
    white-space: nowrap;
}

.composer .inline-vision-controls .inline-vision-preview {
    width: 72px;
    height: 72px;
    flex: 0 0 72px;
    object-fit: cover;
}

.composer .inline-vision-controls .inline-vision-file-name {
    max-width: 220px;
    min-width: 0;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.composer > textarea#messageInput {
    grid-column: 1;
    grid-row: 2;
    width: 100%;
    min-width: 0;
    margin: 0;
}

.composer > button[type="submit"] {
    grid-column: 2;
    grid-row: 2;
    width: auto;
    min-width: 72px;
    max-width: 110px;
    padding: 9px 14px;
    white-space: nowrap;
    align-self: end;
}

@media (max-width: 800px) {
    .composer {
        grid-template-columns: minmax(0, 1fr);
        grid-template-rows: auto auto auto;
    }

    .composer .inline-vision-controls {
        grid-column: 1;
        grid-row: 1;
    }

    .composer > textarea#messageInput {
        grid-column: 1;
        grid-row: 2;
    }

    .composer > button[type="submit"] {
        grid-column: 1;
        grid-row: 3;
        width: auto;
        max-width: 120px;
        justify-self: start;
    }
}

/* Final mobile layout improvements */
@media (max-width: 600px) {
    body {
        overflow-x: hidden;
    }

    .topbar {
        padding: 10px 12px;
        gap: 10px;
    }

    .topbar > div:first-child {
        width: 100%;
        display: flex;
        align-items: center;
        justify-content: space-between;
        gap: 8px;
    }

    .topbar nav {
        width: 100%;
        display: grid;
        grid-template-columns: repeat(2, minmax(0, 1fr));
        gap: 6px;
    }

    .topbar nav .nav-button,
    .topbar nav button,
    .topbar nav a {
        width: 100%;
        min-width: 0;
        padding: 8px 5px;
        overflow: hidden;
        font-size: 13px;
        text-align: center;
        text-overflow: ellipsis;
        white-space: nowrap;
    }

    .workspace {
        display: flex;
        min-height: calc(100vh - 118px);
        flex-direction: column;
    }

    .sidebar {
        width: 100%;
        padding: 10px 12px;
        border-right: 0;
        border-bottom: 1px solid var(--border);
    }

    .sidebar h2 {
        margin: 14px 0 8px;
    }

    .sidebar .list {
        display: flex;
        max-width: 100%;
        overflow-x: auto;
        gap: 8px;
        padding-bottom: 3px;
    }

    .sidebar .list-item {
        min-width: 190px;
        max-width: 230px;
    }

    .chat-area {
        width: 100%;
        min-height: calc(100vh - 330px);
        max-height: none;
    }

    .chat-toolbar {
        padding: 10px 12px;
        align-items: stretch;
        flex-direction: column;
    }

    .chat-toolbar label:first-child {
        min-width: 0;
        width: 100%;
    }

    .chat-toolbar label,
    .chat-toolbar select,
    .chat-toolbar input {
        width: 100%;
    }

    .chat-toolbar .check-label {
        width: auto;
        display: flex;
        align-items: center;
        flex-direction: row;
    }

    .messages {
        min-height: 360px;
        padding: 12px;
    }

    .message {
        max-width: 100%;
        margin-bottom: 12px;
        padding: 11px 12px;
        font-size: 14px;
        line-height: 1.6;
    }

    .composer {
        padding: 10px 12px;
        gap: 8px;
    }

    .composer .inline-vision-controls {
        align-items: flex-start;
        gap: 6px;
    }

    .composer .inline-vision-controls button {
        padding: 7px 9px;
        font-size: 13px;
    }

    .composer .inline-vision-controls .inline-vision-preview {
        width: 58px;
        height: 58px;
        flex-basis: 58px;
    }

    .composer .inline-vision-controls .inline-vision-file-name {
        max-width: 130px;
    }

    .composer > textarea#messageInput {
        min-height: 82px;
        width: 100%;
        font-size: 15px;
    }

    .composer > button[type="submit"] {
        width: auto;
        min-width: 82px;
        max-width: 110px;
        justify-self: start;
        padding: 8px 14px;
    }

    .image-generation-panel,
    .context-summary-panel {
        margin: 8px 12px 0;
        padding: 10px;
    }

    .image-generation-options,
    .context-summary-row {
        align-items: stretch;
        flex-direction: column;
    }

    .image-generation-options label,
    .image-generation-options select,
    .context-summary-row label,
    .context-summary-row input {
        width: 100%;
        min-width: 0;
    }

    .image-generation-result-image {
        max-width: 100%;
        max-height: 360px;
    }

    .files-page,
    .config-panel {
        padding: 12px;
    }

    .config-layout {
        grid-template-columns: 1fr;
        gap: 12px;
    }

    .card,
    .auth-card {
        padding: 15px;
    }

    .section-heading {
        align-items: stretch;
        flex-direction: column;
    }

    .section-heading button {
        width: 100%;
    }

    .model-tools .button-row,
    .button-row {
        align-items: stretch;
        flex-direction: column;
    }

    .model-tools .button-row button,
    .button-row button {
        width: 100%;
    }

    .storage-values {
        display: grid;
        grid-template-columns: repeat(2, minmax(0, 1fr));
        gap: 12px;
    }

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

/* Avoid iOS Safari zooming inputs smaller than 16px */
@media (max-width: 600px) {
    input,
    textarea,
    select {
        font-size: 16px;
    }
}

/* Large pasted text temporary attachments */
.pasted-text-attachments {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
    width: 100%;
    margin-bottom: 8px;
}

.pasted-text-attachment {
    display: inline-flex;
    align-items: center;
    gap: 8px;
    max-width: 100%;
    padding: 6px 10px;
    border: 1px solid rgba(148, 163, 184, 0.35);
    border-radius: 10px;
    background: rgba(15, 23, 42, 0.05);
    color: inherit;
    font-size: 13px;
    line-height: 1.4;
}

.pasted-text-attachment-name {
    font-weight: 600;
    white-space: nowrap;
}

.pasted-text-attachment-size {
    opacity: 0.7;
    white-space: nowrap;
}

.pasted-text-attachment-remove {
    border: none;
    background: transparent;
    color: inherit;
    cursor: pointer;
    font-size: 16px;
    line-height: 1;
    padding: 0 2px;
    opacity: 0.75;
}

.pasted-text-attachment-remove:hover {
    opacity: 1;
}

/* Message markdown-like code blocks */
.message-body {
    white-space: normal;
    word-break: break-word;
}

.message-text {
    white-space: pre-wrap;
    word-break: break-word;
}

.message-code-block {
    margin: 10px 0;
    overflow: hidden;
    border: 1px solid rgba(148, 163, 184, 0.35);
    border-radius: 10px;
    background: #0f172a;
    color: #e5e7eb;
}

.message-code-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    padding: 7px 10px;
    border-bottom: 1px solid rgba(148, 163, 184, 0.25);
    background: rgba(15, 23, 42, 0.95);
    color: #cbd5e1;
    font-size: 12px;
}

.message-code-lang {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.message-code-copy {
    flex: 0 0 auto;
    border: 1px solid rgba(148, 163, 184, 0.45);
    border-radius: 7px;
    padding: 3px 8px;
    background: rgba(30, 41, 59, 0.9);
    color: #e5e7eb;
    cursor: pointer;
    font-size: 12px;
    line-height: 1.4;
}

.message-code-copy:hover {
    background: rgba(51, 65, 85, 1);
}

.message-code-block pre {
    margin: 0;
    padding: 12px;
    overflow-x: auto;
    white-space: pre;
    font-size: 13px;
    line-height: 1.55;
}

.message-code-block code {
    font-family:
        ui-monospace,
        SFMono-Regular,
        Menlo,
        Monaco,
        Consolas,
        "Liberation Mono",
        "Courier New",
        monospace;
}

/* ===== 对话页面固定视口与独立滚动修复 2026-09-09 ===== */
@media (min-width: 769px) {
    html,
    body {
        width: 100%;
        height: 100%;
        overflow: hidden !important;
    }

    body > section {
        display: flex !important;
        flex-direction: column !important;
        width: 100%;
        height: 100vh;
        height: 100dvh;
        min-height: 0 !important;
        overflow: hidden !important;
    }

    body > section > header {
        flex: 0 0 auto;
    }

    body > section > main {
        display: flex !important;
        flex: 1 1 auto !important;
        flex-direction: column !important;
        min-height: 0 !important;
        overflow: hidden !important;
    }

    #chatPanel.workspace {
        flex: 1 1 auto !important;
        height: 100% !important;
        min-height: 0 !important;
        overflow: hidden !important;
    }

    #chatPanel .sidebar {
        display: flex !important;
        flex-direction: column !important;
        height: 100% !important;
        min-height: 0 !important;
        overflow: hidden !important;
    }

    #chatPanel .sidebar > h2 {
        flex: 0 0 auto;
    }

    #conversationList.list {
        flex: 1 1 auto !important;
        min-height: 0 !important;
        overflow-x: hidden !important;
        overflow-y: auto !important;
        overscroll-behavior: contain;
        scrollbar-gutter: stable;
        padding-right: 6px;
    }

    #chatPanel .chat-area {
        display: grid !important;
        grid-template-rows: auto minmax(0, 1fr) auto !important;
        height: 100% !important;
        min-width: 0 !important;
        min-height: 0 !important;
        overflow: hidden !important;
    }

    #chatPanel .chat-toolbar {
        min-height: 0;
        overflow-x: auto;
        overflow-y: visible;
    }

    #messageList.messages {
        min-height: 0 !important;
        overflow-x: hidden !important;
        overflow-y: auto !important;
        overscroll-behavior: contain;
        scrollbar-gutter: stable;
    }

    #chatForm.composer {
        min-height: 0;
        flex: 0 0 auto;
    }

    #conversationList::-webkit-scrollbar,
    #messageList::-webkit-scrollbar {
        width: 9px;
    }

    #conversationList::-webkit-scrollbar-track,
    #messageList::-webkit-scrollbar-track {
        background: transparent;
    }

    #conversationList::-webkit-scrollbar-thumb,
    #messageList::-webkit-scrollbar-thumb {
        background: rgba(148, 163, 184, 0.48);
        border: 2px solid transparent;
        border-radius: 999px;
        background-clip: padding-box;
    }

    #conversationList::-webkit-scrollbar-thumb:hover,
    #messageList::-webkit-scrollbar-thumb:hover {
        background: rgba(148, 163, 184, 0.75);
        border: 2px solid transparent;
        background-clip: padding-box;
    }

    #conversationList,
    #messageList {
        scrollbar-width: thin;
        scrollbar-color: rgba(148, 163, 184, 0.58) transparent;
    }
}
