/* === Reset & Base === */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
    /* Forrest Green family */
    --primary: #3C5E3E;
    --primary-soft: #5F7D61;
    --primary-light: #A9BBAA;
    /* Antique Gold family */
    --accent: #D6C17C;
    --accent-soft: #E4D7A1;
    --accent-light: #F6F1DC;
    /* Neutrals */
    --text: #333333;
    --text-muted: #5F7D61;
    --bg: #F9F9F9;
    --surface: #ffffff;
    --black: #1a1a1a;
    --border: #ddd8cd;
    /* Semantic */
    --user-bubble: #3C5E3E;
    --user-text: #ffffff;
    --bot-bubble: #ffffff;
    --bot-text: #333333;
    --error: #b91c1c;
    --radius: 12px;
    --max-width: 780px;
}

html, body {
    height: 100%;
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
    background: var(--bg);
    color: var(--text);
    line-height: 1.5;
}

a { color: var(--primary-soft); }

/* === App Shell === */
#app {
    display: flex;
    flex-direction: column;
    height: 100vh;
}

/* === Header === */
header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 12px 16px;
    border-bottom: 1px solid var(--border);
    background: var(--surface);
    width: 100%;
}

#setup-panel,
#chat-panel {
    max-width: var(--max-width);
    width: 100%;
    margin: 0 auto;
    align-self: center;
}

.header-left {
    display: flex;
    align-items: center;
    gap: 10px;
}

.header-logo {
    width: 54px;
    height: 54px;
    object-fit: contain;
}

header h1 {
    font-size: 1.35rem;
    color: var(--primary);
    font-weight: 700;
    letter-spacing: 0.02em;
}

.tagline {
    font-size: 0.8rem;
    color: var(--primary-soft);
    margin-top: 0;
    font-weight: 500;
}

.subtitle {
    font-size: 0.68rem;
    color: var(--text-muted);
    margin-top: 1px;
}

.subtitle a {
    color: var(--text-muted);
    text-decoration: none;
}

.subtitle a:hover {
    color: var(--primary);
    text-decoration: underline;
}

.header-github {
    color: var(--text-muted);
    transition: color 0.15s;
    display: flex;
    align-items: center;
    flex-shrink: 0;
}

.header-github:hover {
    color: var(--primary);
}

/* === Setup Panel === */
#setup-panel {
    flex: 1;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 24px 16px;
}

.setup-card {
    background: var(--surface);
    border-radius: var(--radius);
    padding: 32px 28px;
    width: 100%;
    max-width: 400px;
    box-shadow: 0 1px 3px rgba(0,0,0,0.08);
}

.setup-card h2 {
    font-size: 1.15rem;
    margin-bottom: 6px;
}

.setup-card > p {
    color: var(--text-muted);
    font-size: 0.88rem;
    margin-bottom: 20px;
}

.form-group {
    margin-bottom: 14px;
}

.form-group label {
    display: block;
    font-size: 0.82rem;
    font-weight: 600;
    margin-bottom: 5px;
    color: var(--text-muted);
}

.form-group select,
.form-group input {
    width: 100%;
    padding: 10px 12px;
    border: 1px solid var(--border);
    border-radius: 8px;
    font-size: 0.92rem;
    background: var(--surface);
    color: var(--text);
    outline: none;
    transition: border-color 0.15s;
}

.form-group select:focus,
.form-group input:focus {
    border-color: var(--primary);
}

#connect-btn {
    width: 100%;
    padding: 11px;
    background: var(--primary);
    color: #fff;
    border: none;
    border-radius: 8px;
    font-size: 0.95rem;
    font-weight: 600;
    cursor: pointer;
    transition: background 0.15s;
    margin-top: 4px;
}

#connect-btn:hover { background: var(--black); }
#connect-btn:disabled { opacity: 0.6; cursor: not-allowed; }

.error {
    color: var(--error);
    font-size: 0.84rem;
    margin-top: 10px;
}

.setup-help {
    margin-top: 20px;
    padding-top: 16px;
    border-top: 1px solid var(--border);
    font-size: 0.82rem;
    color: var(--text-muted);
}

.setup-help ul {
    margin-top: 6px;
    padding-left: 18px;
}

.setup-help li { margin-bottom: 3px; }

.setup-card hr {
    border: none;
    border-top: 1px solid var(--border);
    margin-bottom: 16px;
}

/* === Chat Panel === */
#chat-panel[hidden] { display: none !important; }
#setup-panel[hidden] { display: none !important; }

#chat-panel {
    flex: 1;
    display: flex;
    flex-direction: column;
    min-height: 0;
    overflow: hidden;
}

/* Messages */
#chat-messages {
    flex: 1;
    overflow-y: auto;
    padding: 20px 16px;
    display: flex;
    flex-direction: column;
    gap: 14px;
}

.message {
    max-width: 85%;
    padding: 12px 16px;
    border-radius: var(--radius);
    font-size: 0.92rem;
    line-height: 1.6;
    word-wrap: break-word;
}

.message.bot {
    align-self: flex-start;
    background: var(--bot-bubble);
    color: var(--bot-text);
    border: 1px solid var(--border);
    border-left: 3px solid var(--accent);
    border-bottom-left-radius: 4px;
    padding-left: 16px;
}

.bot-avatar {
    width: 22px;
    height: 22px;
    object-fit: contain;
    vertical-align: middle;
    margin-right: 5px;
}

.message.user {
    align-self: flex-end;
    background: var(--user-bubble);
    color: var(--user-text);
    border-bottom-right-radius: 4px;
}

.message.bot .sender {
    font-weight: 600;
    font-size: 0.78rem;
    color: var(--primary);
    margin-bottom: 4px;
}

/* Markdown rendering inside messages */
.code-block-wrapper {
    position: relative;
    margin: 8px 0;
}

.copy-btn {
    position: absolute;
    top: 6px;
    right: 6px;
    background: rgba(255,255,255,0.1);
    border: 1px solid rgba(255,255,255,0.15);
    color: #94a3b8;
    border-radius: 5px;
    padding: 3px 8px;
    font-size: 0.72rem;
    cursor: pointer;
    transition: background 0.15s, color 0.15s;
    font-family: inherit;
    line-height: 1.4;
}

.copy-btn:hover {
    background: rgba(255,255,255,0.18);
    color: #e2e8f0;
}

.copy-btn.copied {
    color: var(--accent);
    border-color: rgba(214,193,124,0.4);
}

.message pre {
    background: var(--black);
    color: #e8e6e0;
    padding: 12px;
    padding-top: 14px;
    border-radius: 8px;
    overflow-x: auto;
    margin: 0;
    font-size: 0.84rem;
    line-height: 1.45;
}

.message code {
    font-family: "SF Mono", "Fira Code", "Consolas", monospace;
    font-size: 0.86em;
}

.message :not(pre) > code {
    background: var(--accent-light);
    padding: 2px 5px;
    border-radius: 4px;
    color: var(--primary);
}

.message.user :not(pre) > code {
    background: rgba(255,255,255,0.2);
    color: #fff;
}

.message h1, .message h2, .message h3, .message h4 {
    margin: 12px 0 6px;
    line-height: 1.3;
}
.message h1 { font-size: 1.15rem; }
.message h2 { font-size: 1.05rem; }
.message h3 { font-size: 0.98rem; }
.message h4 { font-size: 0.92rem; }
.message h1:first-child, .message h2:first-child,
.message h3:first-child, .message h4:first-child { margin-top: 0; }

.message p { margin-bottom: 8px; }
.message p:last-child { margin-bottom: 0; }
.message ul, .message ol { padding-left: 20px; margin-bottom: 8px; }
.message li { margin-bottom: 2px; }
.message strong { font-weight: 600; }
.message em { font-style: italic; }

/* Typing indicator */
.typing-indicator span {
    display: inline-block;
    width: 7px;
    height: 7px;
    background: var(--text-muted);
    border-radius: 50%;
    margin-right: 4px;
    animation: bounce 1.2s infinite;
}
.typing-indicator span:nth-child(2) { animation-delay: 0.2s; }
.typing-indicator span:nth-child(3) { animation-delay: 0.4s; }

@keyframes bounce {
    0%, 60%, 100% { transform: translateY(0); }
    30% { transform: translateY(-6px); }
}

/* === File Upload Preview === */
#file-preview {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 6px 16px;
    background: var(--surface);
    border-top: 1px solid var(--border);
}

#file-preview[hidden] { display: none; }

#file-name {
    display: inline-flex;
    align-items: center;
    gap: 5px;
    background: var(--accent-light);
    color: var(--primary);
    font-size: 0.8rem;
    font-weight: 500;
    padding: 4px 10px;
    border-radius: 6px;
    border: 1px solid var(--accent-soft);
    max-width: 280px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

#file-name::before {
    content: "📎";
    font-size: 0.85rem;
}

#file-remove {
    background: none;
    border: none;
    color: var(--text-muted);
    font-size: 1.1rem;
    cursor: pointer;
    padding: 0 4px;
    line-height: 1;
}

#file-remove:hover { color: var(--error); }

/* === Input Area === */
#chat-input-area {
    display: flex;
    align-items: flex-end;
    gap: 8px;
    padding: 12px 16px;
    border-top: 1px solid var(--border);
    background: var(--surface);
}

#attach-btn {
    width: 40px;
    height: 40px;
    border: 1px solid var(--border);
    border-radius: 10px;
    background: var(--surface);
    color: var(--text-muted);
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
    transition: border-color 0.15s, color 0.15s;
}

#attach-btn:hover {
    border-color: var(--primary);
    color: var(--primary);
}

#chat-input {
    flex: 1;
    padding: 10px 14px;
    border: 1px solid var(--border);
    border-radius: 10px;
    font-size: 0.92rem;
    font-family: inherit;
    resize: none;
    max-height: 120px;
    outline: none;
    line-height: 1.45;
    transition: border-color 0.15s;
}

#chat-input:focus { border-color: var(--primary); }

#send-btn {
    width: 40px;
    height: 40px;
    border: none;
    border-radius: 10px;
    background: var(--primary);
    color: #fff;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
    transition: background 0.15s;
}

#send-btn:hover { background: var(--black); }
#send-btn:disabled { opacity: 0.5; cursor: not-allowed; }

/* === Footer === */
#chat-footer {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 6px 16px 10px;
    font-size: 0.78rem;
    background: var(--surface);
}

#provider-badge {
    color: var(--text-muted);
    font-weight: 500;
}

#disconnect-btn {
    background: none;
    border: none;
    color: var(--error);
    font-size: 0.78rem;
    cursor: pointer;
    font-weight: 500;
}

#disconnect-btn:hover { text-decoration: underline; }

/* === Responsive === */
@media (max-width: 600px) {
    .setup-card { padding: 24px 18px; }
    .message { max-width: 92%; }
}
