/* AI 助手面板样式 —— 复用主站 :root 变量，自动适配亮/暗与背景风格 */

/* 面板（右侧抽屉） */
.ai-panel {
    position: fixed;
    right: max(16px, env(safe-area-inset-right));
    bottom: max(96px, calc(env(safe-area-inset-bottom) + 84px));
    width: min(380px, calc(100vw - 24px));
    height: min(560px, calc(100dvh - 130px));
    display: flex;
    flex-direction: column;
    background: var(--modal-bg, rgba(255, 255, 255, 0.96));
    color: var(--modal-text-color, var(--text-color));
    border: 1px solid var(--modal-border-color, rgba(148, 163, 184, 0.35));
    border-radius: 16px;
    box-shadow: 0 20px 60px rgba(15, 23, 42, 0.28);
    backdrop-filter: blur(16px);
    -webkit-backdrop-filter: blur(16px);
    z-index: 300;
    overflow: hidden;
}
.ai-panel[hidden] { display: none; }

/* 全屏模式 */
.ai-panel.fullscreen {
    right: 0;
    left: 0;
    top: 0;
    bottom: 0;
    width: 100vw;
    height: 100dvh;
    max-width: none;
    max-height: none;
    border-radius: 0;
    border: none;
    box-shadow: none;
}
.ai-panel.fullscreen .ai-panel-head { padding: 12px max(16px, calc(50% - 420px)); }
.ai-panel.fullscreen .ai-body {
    padding: 18px max(16px, calc(50% - 420px)); /* 内容列居中，最大宽约 840px */
}
.ai-panel.fullscreen .ai-msg { max-width: 82%; }
.ai-panel.fullscreen .ai-msg-text { font-size: 0.98rem; line-height: 1.7; }
.ai-panel.fullscreen .ai-msg-img { max-width: 320px; max-height: 300px; }
.ai-panel.fullscreen .ai-gen-img { max-width: min(560px, 64vw); max-height: 64vh; }
.ai-panel.fullscreen .ai-input-area { padding: 12px max(16px, calc(50% - 420px)) 16px; }
.ai-panel.fullscreen textarea.ai-input { max-height: 200px; font-size: 0.95rem; }
.ai-panel.fullscreen .ai-center { max-width: 420px; }

.ai-panel-head {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 10px 12px;
    border-bottom: 1px solid var(--modal-border-color);
    flex: 0 0 auto;
}
.ai-panel-title {
    font-weight: 700;
    font-size: 0.95rem;
    color: var(--accent);
    white-space: nowrap;
}
.ai-panel-title i { margin-right: 4px; }

.ai-seg {
    display: flex;
    background: rgba(128, 128, 128, 0.12);
    border-radius: 999px;
    padding: 3px;
    margin-left: auto;
}
.ai-seg button {
    border: 0;
    background: transparent;
    color: inherit;
    font: inherit;
    font-size: 0.8rem;
    padding: 3px 12px;
    border-radius: 999px;
    cursor: pointer;
    opacity: 0.75;
    transition: all 0.15s ease;
}
.ai-seg button.on {
    background: var(--card-solid, #fff);
    color: var(--accent);
    opacity: 1;
    box-shadow: 0 1px 4px rgba(0, 0, 0, 0.12);
}

.ai-head-actions {
    display: flex;
    gap: 2px;
    margin-left: 6px;
}
.ai-head-actions button {
    border: 0;
    background: transparent;
    color: inherit;
    opacity: 0.65;
    font-size: 0.9rem;
    width: 28px;
    height: 28px;
    border-radius: 8px;
    cursor: pointer;
    display: inline-flex;
    align-items: center;
    justify-content: center;
}
.ai-head-actions button:hover { opacity: 1; background: rgba(128, 128, 128, 0.15); }

/* 会话区 */
.ai-body {
    flex: 1 1 auto;
    overflow-y: auto;
    padding: 12px;
    display: flex;
    flex-direction: column;
    gap: 10px;
    scroll-behavior: smooth;
    overscroll-behavior: contain;
}
.ai-center {
    margin: auto;
    text-align: center;
    color: var(--modal-text-color, var(--text-color));
    max-width: 260px;
    padding: 12px;
}
.ai-center .ai-big { font-size: 1rem; font-weight: 700; margin: 0 0 8px; }
.ai-center .ai-sub { font-size: 0.82rem; opacity: 0.72; margin: 0 0 12px; line-height: 1.6; }
.ai-center .ai-pass { margin-bottom: 10px; text-align: center; }
.ai-center .ai-err { color: var(--danger-bg, #ef4444); opacity: 1; }

.ai-msg {
    max-width: 86%;
    display: flex;
    flex-direction: column;
    gap: 4px;
}
.ai-msg.user { align-self: flex-end; align-items: flex-end; }
.ai-msg.ai { align-self: flex-start; align-items: flex-start; }
.ai-msg-text {
    font-size: 0.9rem;
    line-height: 1.6;
    padding: 8px 12px;
    border-radius: 14px;
    word-break: break-word;
    white-space: normal;
}
.ai-msg.user .ai-msg-text {
    background: var(--accent, #6366f1);
    color: #fff;
    border-bottom-right-radius: 4px;
}
.ai-msg.ai .ai-msg-text {
    background: var(--card-background, rgba(255, 255, 255, 0.72));
    border: 1px solid var(--card-border, rgba(0, 0, 0, 0.05));
    border-bottom-left-radius: 4px;
}
.ai-msg-img {
    max-width: 180px;
    max-height: 160px;
    border-radius: 12px;
    border: 1px solid var(--card-border);
    object-fit: cover;
}
.ai-code {
    background: rgba(0, 0, 0, 0.08);
    border-radius: 8px;
    padding: 8px 10px;
    font-size: 0.8rem;
    overflow-x: auto;
    margin: 6px 0;
    white-space: pre-wrap;
}
.ai-msg-text code {
    background: rgba(0, 0, 0, 0.08);
    border-radius: 4px;
    padding: 1px 5px;
    font-size: 0.82em;
}

/* 生图 */
.ai-gen-box { display: flex; flex-direction: column; gap: 8px; max-width: 100%; }
.ai-gen-img {
    max-width: 260px;
    max-height: 260px;
    border-radius: 12px;
    border: 1px solid var(--card-border);
    box-shadow: 0 6px 20px rgba(0, 0, 0, 0.15);
}
.ai-gen-actions { display: flex; gap: 8px; }
.ai-gen-intro { margin: auto; }

/* 按钮 */
.ai-btn {
    border: 1px solid var(--modal-border-color);
    background: var(--card-background);
    color: var(--modal-text-color, var(--text-color));
    font: inherit;
    font-size: 0.85rem;
    padding: 7px 14px;
    border-radius: 10px;
    cursor: pointer;
    display: inline-flex;
    align-items: center;
    gap: 6px;
    transition: all 0.15s ease;
    text-decoration: none;
}
.ai-btn:hover { border-color: var(--accent); color: var(--accent); }
.ai-btn.primary {
    background: var(--accent, #6366f1);
    border-color: transparent;
    color: #fff;
}
.ai-btn.primary:hover { filter: brightness(1.08); color: #fff; }
.ai-btn:disabled { opacity: 0.5; cursor: not-allowed; }

/* 输入区 */
.ai-input-area {
    flex: 0 0 auto;
    border-top: 1px solid var(--modal-border-color);
    padding: 10px 12px;
    display: flex;
    flex-direction: column;
    gap: 8px;
    background: var(--modal-bg, transparent);
}
.ai-input {
    width: 100%;
    box-sizing: border-box;
    font: inherit;
    font-size: 0.9rem;
    color: var(--modal-text-color, var(--text-color));
    background: var(--card-background, rgba(255, 255, 255, 0.72));
    border: 1px solid var(--modal-border-color);
    border-radius: 10px;
    padding: 9px 12px;
    resize: none;
    outline: none;
    transition: border-color 0.15s ease, box-shadow 0.15s ease;
}
.ai-input:focus { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-glow, rgba(99, 102, 241, 0.25)); }
.ai-input::placeholder { opacity: 0.5; }
.ai-input:disabled { opacity: 0.6; }
textarea.ai-input { min-height: 44px; max-height: 120px; }
.ai-input-tools {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 8px;
}
.ai-input-tools button {
    border: 0;
    background: transparent;
    color: inherit;
    opacity: 0.7;
    width: 34px;
    height: 34px;
    border-radius: 10px;
    cursor: pointer;
    font-size: 1rem;
    display: inline-flex;
    align-items: center;
    justify-content: center;
}
.ai-input-tools button:hover { opacity: 1; background: rgba(128, 128, 128, 0.15); }
.ai-send {
    margin-left: auto;
    background: var(--accent, #6366f1) !important;
    color: #fff !important;
    opacity: 1 !important;
    border-radius: 50% !important;
}
.ai-send:disabled { opacity: 0.5 !important; }

.ai-attach-row {
    display: flex;
    align-items: center;
    gap: 8px;
    font-size: 0.8rem;
    background: var(--card-background);
    border: 1px dashed var(--modal-border-color);
    border-radius: 10px;
    padding: 6px 10px;
}
.ai-attach-name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; flex: 1; }
.ai-attach-remove {
    border: 0;
    background: transparent;
    color: inherit;
    opacity: 0.6;
    cursor: pointer;
}

/* 设置弹层 */
.ai-settings-overlay {
    position: fixed;
    inset: 0;
    background: var(--modal-overlay-bg, rgba(15, 23, 42, 0.45));
    z-index: 400;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 14px;
}
.ai-settings-overlay[hidden] { display: none; }
.ai-settings {
    width: min(440px, 100%);
    max-height: min(88dvh, 700px);
    display: flex;
    flex-direction: column;
    background: var(--modal-bg, rgba(255, 255, 255, 0.96));
    color: var(--modal-text-color, var(--text-color));
    border: 1px solid var(--modal-border-color);
    border-radius: 16px;
    box-shadow: 0 24px 70px rgba(15, 23, 42, 0.35);
    overflow: hidden;
}
.ai-settings-head {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 12px 16px;
    border-bottom: 1px solid var(--modal-border-color);
    font-weight: 700;
}
.ai-settings-head button {
    border: 0;
    background: transparent;
    color: inherit;
    opacity: 0.65;
    cursor: pointer;
    font-size: 0.95rem;
    width: 28px;
    height: 28px;
    border-radius: 8px;
}
.ai-settings-head button:hover { opacity: 1; background: rgba(128, 128, 128, 0.15); }
.ai-set-body { overflow-y: auto; padding: 14px 16px 18px; }
.ai-set-fields { display: flex; flex-direction: column; gap: 6px; }
.ai-set-fields h4 {
    margin: 10px 0 2px;
    font-size: 0.85rem;
    color: var(--accent);
}
.ai-set-fields label { font-size: 0.8rem; opacity: 0.8; margin-top: 6px; }
.ai-set-fields .ai-input { margin-top: 2px; }
.ai-hint { font-size: 0.76rem; opacity: 0.68; line-height: 1.6; margin: 8px 0 0; }
.ai-hint code { background: rgba(0, 0, 0, 0.08); border-radius: 4px; padding: 0 4px; }
.ai-set-actions {
    display: flex;
    gap: 10px;
    margin-top: 16px;
}
.ai-set-actions .ai-btn { flex: 1; justify-content: center; }
.ai-err { color: var(--danger-bg, #ef4444) !important; opacity: 1 !important; }

/* 测试连接结果 */
.ai-test-result {
    margin: 10px 0 0;
    padding: 10px 12px;
    border-radius: 8px;
    font-size: 0.78rem;
    line-height: 1.55;
    word-break: break-all;
    background: rgba(128, 128, 128, 0.08);
    border: 1px solid transparent;
}
.ai-test-result.ai-ok  { color: #16a34a; background: rgba(22, 163, 74, 0.10); border-color: rgba(22, 163, 74, 0.25); }
.ai-test-result.ai-err { color: var(--danger-bg, #ef4444); background: rgba(239, 68, 68, 0.08); border-color: rgba(239, 68, 68, 0.25); }
.ai-err-label {
    display: inline-block;
    font-size: 0.72rem;
    font-weight: 700;
    padding: 2px 8px;
    margin-bottom: 6px;
    border-radius: 6px;
    color: #fff;
    background: var(--danger-bg, #ef4444);
}

/* 开关 */
.ai-toggle-row {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-top: 10px;
    font-size: 0.85rem;
}
.ai-switch { position: relative; display: inline-block; width: 40px; height: 22px; }
.ai-switch input { display: none; }
.ai-switch span {
    position: absolute;
    inset: 0;
    background: rgba(128, 128, 128, 0.35);
    border-radius: 999px;
    transition: background 0.2s ease;
    cursor: pointer;
}
.ai-switch span::before {
    content: '';
    position: absolute;
    width: 16px;
    height: 16px;
    left: 3px;
    top: 3px;
    background: #fff;
    border-radius: 50%;
    transition: transform 0.2s ease;
    box-shadow: 0 1px 3px rgba(0, 0, 0, 0.3);
}
.ai-switch input:checked + span { background: var(--accent, #6366f1); }
.ai-switch input:checked + span::before { transform: translateX(18px); }

/* 轻提示 */
.ai-toast {
    position: fixed;
    left: 50%;
    bottom: 96px;
    transform: translateX(-50%) translateY(12px);
    background: var(--modal-bg, #1e293b);
    color: var(--modal-text-color, #fff);
    padding: 10px 18px;
    border-radius: 999px;
    font-size: 0.85rem;
    box-shadow: 0 10px 30px rgba(0, 0, 0, 0.3);
    opacity: 0;
    transition: opacity 0.25s ease, transform 0.25s ease;
    z-index: 500;
    pointer-events: none;
    max-width: 80vw;
    text-align: center;
}
.ai-toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }
.ai-toast.err { background: var(--danger-bg, #ef4444); color: #fff; }

/* 无 .controls-area 时的兜底定位 */
.ai-launcher-fallback {
    position: fixed;
    right: max(14px, env(safe-area-inset-right));
    bottom: max(16px, env(safe-area-inset-bottom));
    z-index: 100;
    width: 44px;
    height: 44px;
    border-radius: 50%;
    border: 1px solid var(--card-border);
    background: var(--card-background);
    color: var(--accent);
    box-shadow: var(--card-shadow);
    cursor: pointer;
    font-size: 1.15rem;
}

/* 移动端 */
@media (max-width: 640px) {
    .ai-panel {
        right: 10px;
        bottom: max(76px, calc(env(safe-area-inset-bottom) + 66px));
        width: calc(100vw - 20px);
        height: min(600px, calc(100dvh - 96px));
    }
    .ai-msg { max-width: 92%; }
    .ai-toast { bottom: 84px; }
}
