/* ========================================
   Variables
   ======================================== */
:root {
    --bg-primary: #f8f9fa;
    --bg-secondary: #ffffff;
    --bg-tertiary: #f1f3f4;
    --text-primary: #202124;
    --text-secondary: #5f6368;
    --text-tertiary: #9aa0a6;
    --accent: #1a73e8;
    --accent-hover: #1557b0;
    --border: #dadce0;
    --shadow: rgba(0, 0, 0, 0.1);
    --msg-user-bg: #e8f0fe;
    --msg-user-text: #1a1a1a;
    --msg-assistant-bg: #ffffff;
    --msg-assistant-text: #1a1a1a;
    --code-bg: #f5f5f5;
    --scrollbar-thumb: #dadce0;
    --scrollbar-track: #f1f1f1;
}

@media (prefers-color-scheme: dark) {
    :root {
        --bg-primary: #202124;
        --bg-secondary: #2d2d30;
        --bg-tertiary: #3c4043;
        --text-primary: #e8eaed;
        --text-secondary: #9aa0a6;
        --text-tertiary: #5f6368;
        --accent: #8ab4f8;
        --accent-hover: #aecbfa;
        --border: #3c4043;
        --shadow: rgba(0, 0, 0, 0.3);
        --msg-user-bg: #3c5180;
        --msg-user-text: #e8eaed;
        --msg-assistant-bg: #2d2d30;
        --msg-assistant-text: #e8eaed;
        --code-bg: #1e1e1e;
        --scrollbar-thumb: #5f6368;
        --scrollbar-track: #3c4043;
    }
}

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

html,
body {
    height: 100%;
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;
    background: var(--bg-primary);
    color: var(--text-primary);
    line-height: 1.6;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
}

/* ========================================
   App Container
   ======================================== */
#app {
    display: flex;
    flex-direction: column;
    height: 100vh;
    width: 100%;
    background: var(--bg-secondary);
}

/* ========================================
   Connect Screen
   ======================================== */
#connect-screen {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    min-height: 100vh;
    padding: 2rem 1.5rem;
    text-align: center;
}

#connect-screen h1 {
    font-size: 2.5rem;
    font-weight: 600;
    color: var(--text-primary);
    margin-bottom: 0.5rem;
    letter-spacing: -0.5px;
}

.subtitle {
    color: var(--text-secondary);
    font-size: 1rem;
    margin-bottom: 2rem;
}

.input-group {
    width: 100%;
    max-width: 400px;
    margin-bottom: 1.5rem;
    text-align: left;
}

.input-group label {
    display: block;
    margin-bottom: 0.5rem;
    color: var(--text-secondary);
    font-size: 0.875rem;
    font-weight: 500;
}

.input-group input {
    width: 100%;
    padding: 0.875rem 1rem;
    border: 1px solid var(--border);
    border-radius: 8px;
    font-size: 1rem;
    background: var(--bg-primary);
    color: var(--text-primary);
    transition: border-color 0.2s, box-shadow 0.2s;
}

.input-group input:focus {
    outline: none;
    border-color: var(--accent);
    box-shadow: 0 0 0 3px rgba(26, 115, 232, 0.15);
}

.error-message {
    color: #ff4757;
    font-size: 0.875rem;
    margin-bottom: 1rem;
    text-align: center;
}

.btn {
    padding: 0.875rem 1.75rem;
    border: none;
    border-radius: 8px;
    cursor: pointer;
    font-size: 1rem;
    font-weight: 500;
    transition: all 0.2s;
    background: var(--accent);
    color: #fff;
}

.btn:hover {
    background: var(--accent-hover);
    transform: translateY(-1px);
    box-shadow: 0 4px 12px var(--shadow);
}

.btn:active {
    transform: translateY(0);
}

.btn-sm {
    padding: 0.5rem 0.875rem;
    font-size: 0.875rem;
    background: var(--bg-tertiary);
    color: var(--text-primary);
}

.btn-sm:hover {
    background: var(--border);
}

/* ========================================
   Chat Screen
   ======================================== */
#chat-screen {
    display: flex;
    flex-direction: column;
    height: 100vh;
}

#chat-screen.hidden,
#connect-screen.hidden {
    display: none;
}

header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 1rem 1.5rem;
    background: var(--bg-secondary);
    border-bottom: 1px solid var(--border);
    flex-shrink: 0;
}

.header-left {
    display: flex;
    align-items: center;
    gap: 0.75rem;
}

.header-right {
    display: flex;
    align-items: center;
    gap: 0.75rem;
}

#session-display {
    font-weight: 500;
    color: var(--text-primary);
    font-size: 0.95rem;
}

.status {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    color: var(--text-secondary);
    font-size: 0.875rem;
}

.status::before {
    content: '';
    display: block;
    width: 8px;
    height: 8px;
    border-radius: 50%;
    background: #34d399;
}

.status.disconnected::before {
    background: #ff4757;
}

.status.disconnected {
    color: #ff4757;
}

/* ========================================
   Messages Area
   ======================================== */
.messages {
    flex: 1;
    overflow-y: auto;
    padding: 1.5rem;
    scroll-behavior: smooth;
}

.messages::-webkit-scrollbar {
    width: 6px;
}

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

.messages::-webkit-scrollbar-thumb {
    background: var(--scrollbar-thumb);
    border-radius: 3px;
}

.message {
    margin-bottom: 1rem;
    display: flex;
    animation: messageIn 0.3s ease-out;
}

@keyframes messageIn {
    from {
        opacity: 0;
        transform: translateY(10px);
    }
    to {
        opacity: 1;
        transform: translateY(0);
    }
}

.message.user {
    justify-content: flex-end;
}

.message.assistant {
    justify-content: flex-start;
}

.message-content {
    max-width: 85%;
    padding: 0.875rem 1rem;
    border-radius: 12px;
    line-height: 1.5;
    word-wrap: break-word;
    overflow-wrap: break-word;
}

.message.user .message-content {
    background: var(--msg-user-bg);
    color: var(--msg-user-text);
    border-bottom-right-radius: 4px;
}

.message.assistant .message-content {
    background: var(--msg-assistant-bg);
    color: var(--msg-assistant-text);
    border: 1px solid var(--border);
    border-bottom-left-radius: 4px;
}

/* Markdown Content Styles */
.message-content h1,
.message-content h2,
.message-content h3,
.message-content h4,
.message-content h5,
.message-content h6 {
    margin: 1rem 0 0.5rem;
    font-weight: 600;
    line-height: 1.4;
}

.message-content h1 { font-size: 1.5rem; }
.message-content h2 { font-size: 1.3rem; }
.message-content h3 { font-size: 1.15rem; }
.message-content p {
    margin: 0.75rem 0;
}

.message-content p:first-child {
    margin-top: 0;
}

.message-content p:last-child {
    margin-bottom: 0;
}

.message-content a {
    color: var(--accent);
    text-decoration: none;
}

.message-content a:hover {
    text-decoration: underline;
}

.message-content ul,
.message-content ol {
    margin: 0.5rem 0;
    padding-left: 1.5rem;
}

.message-content li {
    margin: 0.25rem 0;
}

.message-content blockquote {
    border-left: 3px solid var(--accent);
    padding-left: 1rem;
    margin: 0.75rem 0;
    color: var(--text-secondary);
}

.message-content hr {
    border: none;
    border-top: 1px solid var(--border);
    margin: 1rem 0;
}

.message-content strong {
    font-weight: 600;
}

.message-content em {
    font-style: italic;
}

/* Code Blocks */
.message-content pre {
    background: var(--code-bg);
    border: 1px solid var(--border);
    border-radius: 8px;
    padding: 1rem;
    margin: 0.75rem 0;
    overflow-x: auto;
    position: relative;
}

.message-content code {
    font-family: 'SF Mono', Monaco, 'Cascadia Code', 'Roboto Mono', Consolas, 'Courier New', monospace;
    font-size: 0.875rem;
    background: var(--code-bg);
    padding: 0.2em 0.4em;
    border-radius: 4px;
    border: 1px solid var(--border);
}

.message-content pre > code {
    background: none;
    padding: 0;
    border: none;
}

.message-content table {
    border-collapse: collapse;
    width: 100%;
    margin: 0.75rem 0;
    font-size: 0.875rem;
}

.message-content th,
.message-content td {
    border: 1px solid var(--border);
    padding: 0.5rem 0.75rem;
    text-align: left;
}

.message-content th {
    background: var(--bg-tertiary);
    font-weight: 600;
}

.message-content tr:nth-child(even) {
    background: var(--bg-tertiary);
}

/* ========================================
   Input Area
   ======================================== */
.input-area {
    display: flex;
    gap: 0.75rem;
    padding: 1rem 1.5rem;
    background: var(--bg-secondary);
    border-top: 1px solid var(--border);
    flex-shrink: 0;
}

.input-area textarea {
    flex: 1;
    padding: 0.875rem 1rem;
    border: 1px solid var(--border);
    border-radius: 12px;
    font-size: 1rem;
    resize: none;
    font-family: inherit;
    background: var(--bg-primary);
    color: var(--text-primary);
    transition: border-color 0.2s, box-shadow 0.2s;
    line-height: 1.5;
}

.input-area textarea:focus {
    outline: none;
    border-color: var(--accent);
    box-shadow: 0 0 0 3px rgba(26, 115, 232, 0.15);
}

.input-area textarea::placeholder {
    color: var(--text-tertiary);
}

/* ========================================
   Loading Animation
   ======================================== */
.typing {
    display: inline-flex;
    gap: 4px;
    padding: 0.5rem 0;
}

.typing span {
    width: 8px;
    height: 8px;
    background: var(--text-tertiary);
    border-radius: 50%;
    animation: typing 1.4s infinite ease-in-out;
}

.typing span:nth-child(2) { animation-delay: 0.2s; }
.typing span:nth-child(3) { animation-delay: 0.4s; }

@keyframes typing {
    0%, 60%, 100% { transform: translateY(0); opacity: 0.5; }
    30% { transform: translateY(-8px); opacity: 1; }
}

/* ========================================
   Responsive
   ======================================== */
@media (max-width: 640px) {
    #app {
        max-width: 100%;
        box-shadow: none;
    }

    #connect-screen h1 {
        font-size: 2rem;
    }

    .message-content {
        max-width: 85%;
    }

    .messages {
        padding: 1rem;
    }

    .input-area {
        padding: 0.875rem 1rem;
    }

    header {
        padding: 0.875rem 1rem;
    }
}

@media (max-width: 480px) {
    #connect-screen {
        padding: 1.5rem 1rem;
    }

    #connect-screen h1 {
        font-size: 1.75rem;
    }

    .input-group {
        margin-bottom: 1rem;
    }

    .message-content {
        max-width: 90%;
        padding: 0.75rem 0.875rem;
    }

    .input-group input,
    .input-area textarea {
        padding: 0.75rem 0.875rem;
    }

    .btn {
        padding: 0.75rem 1.5rem;
    }
}
