/* CSS Variables for light mode */
:root {
    --bg-color: #f5f5f5;
    --text-color: #333333;
    --primary-color: #4a90d9;
    --primary-hover: #357abd;
    --error-color: #d32f2f;
    --error-bg: #ffebee;
    --error-border: #ffcdd2;
    --user-bg: #e3f2fd;
    --assistant-bg: #ffffff;
    --thinking-bg: #fff8e1;
    --thinking-border: #ffecb3;
    --tool-bg: #f3e5f5;
    --tool-border: #e1bee7;
    --success-color: #4caf50;
    --border-color: #e0e0e0;
    --shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

/* Reset and base styles */
* {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
}

html, body {
    height: 100%;
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, sans-serif;
    font-size: 14px;
    color: var(--text-color);
    line-height: 1.5;
}

/* Layout: Flexbox column for full-height chat */
#app {
    display: flex;
    flex-direction: column;
    height: 100vh; /* Fallback for older browsers */
    height: 100dvh; /* Dynamic viewport height for mobile browsers */
    max-width: 800px;
    margin: 0 auto;
    background-color: var(--assistant-bg);
}

/* Navbar with title and new session button */
.navbar {
    flex-shrink: 0;
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0.75rem 1rem;
    background-color: #ffffff;
    color: #333333;
    min-height: 48px;
    gap: 0.5rem;
}

.nav-title {
    font-size: 1.1rem;
    font-weight: 600;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    flex: 1;
    text-align: center;
    margin: 0;
}

.nav-spacer {
    width: 32px;
    flex-shrink: 0;
}

/* New session button styling - circular button in navbar */
#new-session-btn {
    flex-shrink: 0;
    width: 32px;
    height: 32px;
    border-radius: 50%;
    background-color: var(--bg-color);
    color: var(--text-color);
    border: 1px solid #eee;
    cursor: pointer;
    transition: background-color 0.2s ease, transform 0.2s ease;
    display: flex;
    align-items: center;
    justify-content: center;
    position: relative;
}

/* Plus icon using CSS pseudo-element */
#new-session-btn::before {
    content: '';
    position: absolute;
    width: 14px;
    height: 2px;
    background-color: #333333;
    border-radius: 1px;
}

#new-session-btn::after {
    content: '';
    position: absolute;
    width: 2px;
    height: 14px;
    background-color: #333333;
    border-radius: 1px;
}

#new-session-btn:hover {
    background-color: #e0e0e0;
    transform: scale(1.05);
}

#new-session-btn:disabled {
    background-color: #f0f0f0;
    cursor: not-allowed;
    opacity: 0.6;
}

/* Header styling (legacy, may not be used) */
header {
    flex-shrink: 0;
    padding: 1rem 1.5rem;
    background-color: var(--primary-color);
    color: white;
    border-bottom: 1px solid var(--border-color);
    display: flex;
    justify-content: space-between;
    align-items: center;
}

header h1 {
    font-size: 1.25rem;
    font-weight: 600;
}

/* Messages area: scrollable content */
#messages {
    flex: 1;
    overflow-y: auto;
    padding: 1rem;
    display: flex;
    flex-direction: column;
    gap: 0.75rem;
}

/* Footer input area */
footer {
    flex-shrink: 0;
    padding: 1rem 1.5rem;
    padding-bottom: calc(1.5rem + env(safe-area-inset-bottom, 0rem));
    background-color: var(--assistant-bg);
}

/* Input container */
.input-container {
    display: flex;
    gap: 0.5rem;
    align-items: center;
    min-height: 44px;
}

.input-container.hidden {
    display: none;
}

/* Text input container */
#text-input-container {
    align-items: center;
    border-radius: 10px;
    box-shadow: 0 2px 12px rgba(0, 0, 0, 0.15);
    background-color: var(--assistant-bg);
}

/* Voice input container */
#voice-input-container {
    align-items: center;
    border-radius: 10px;
    box-shadow: 0 2px 12px rgba(0, 0, 0, 0.15);
    background-color: var(--assistant-bg);
}

/* Single line text input styling */
#message-input {
    flex: 1;
    height: 44px;
    padding: 0 1rem;
    border: none;
    border-radius: 0;
    font-size: 1rem;
    font-family: inherit;
    outline: none;
    background-color: transparent;
}

#message-input:focus {
    outline: none;
}

#message-input::placeholder {
    color: #999999;
}

#message-input:disabled {
    background-color: transparent;
    cursor: not-allowed;
    opacity: 0.5;
}

/* Input mode toggle button */
#input-mode-toggle,
#voice-mode-toggle {
    flex-shrink: 0;
    width: 44px;
    height: 44px;
    padding: 0;
    background-color: transparent;
    color: var(--text-color);
    border: none;
    font-size: 1.5rem;
    cursor: pointer;
    transition: transform 0.2s ease;
    display: flex;
    align-items: center;
    justify-content: center;
}

#input-mode-toggle:hover,
#voice-mode-toggle:hover {
    transform: scale(1.05);
}

#input-mode-toggle:disabled,
#voice-mode-toggle:disabled {
    color: #999;
    cursor: not-allowed;
    opacity: 0.5;
}

/* Iconfont icons in toggle buttons */
#input-mode-toggle .iconfont,
#voice-mode-toggle .iconfont {
    font-size: 24px;
    color: #333;
}

/* Send arrow icon when input has text */
#input-mode-toggle.has-text .iconfont {
    color: var(--primary-color);
}

/* Voice input button in voice mode - long horizontal button */
#voice-input-btn {
    flex: 1;
    height: 44px;
    padding: 0 1.5rem;
    background-color: transparent;
    color: #333;
    border: none;
    border-radius: 0;
    font-size: 1rem;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 0.5rem;
}

#voice-input-btn:hover {
    background-color: transparent;
}

#voice-input-btn:active,
#voice-input-btn.pressed {
    background-color: transparent;
}

#voice-input-btn:disabled {
    background-color: transparent;
    color: #999;
    cursor: not-allowed;
    opacity: 0.5;
}

#voice-input-btn:focus {
    outline: none;
}

#voice-input-btn.recording {
    background-color: var(--error-color);
    color: white;
    animation: pulse 1s ease-in-out infinite;
}

/* Pulse animation for recording */
@keyframes pulse {
    0%, 100% { opacity: 1; }
    50% { opacity: 0.3; }
}

/* Message bubbles */
.message {
    padding: 0.75rem 1rem;
    border-radius: 12px;
    max-width: 85%;
    word-wrap: break-word;
}

.message.user {
    align-self: flex-end;
    background-color: var(--user-bg);
}

.message.assistant {
    align-self: flex-start;
    background-color: #f8f8f7;
}

.message .content {
    font-size: 1rem;
}

/* Markdown styling for assistant messages */
.message.assistant .content p {
    margin: 0 0 0.5rem 0;
}

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

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

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

.message.assistant .content code {
    background-color: rgba(0, 0, 0, 0.1);
    padding: 0.125rem 0.25rem;
    border-radius: 3px;
    font-family: 'Consolas', 'Monaco', monospace;
    font-size: 0.875em;
}

.message.assistant .content pre {
    background-color: rgba(0, 0, 0, 0.05);
    padding: 0.5rem;
    border-radius: 4px;
    overflow-x: auto;
    margin: 0.5rem 0;
}

.message.assistant .content pre code {
    background-color: transparent;
    padding: 0;
}

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

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

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

/* Markdown table styling - horizontal lines only */
.message.assistant .content table {
    width: 100%;
    border-collapse: collapse;
    margin: 0.5rem 0;
    font-size: 0.9em;
}

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

.message.assistant .content th {
    font-weight: 600;
    border-bottom-width: 2px;
}

.message.assistant .content tr:last-child td {
    border-bottom: none;
}

/* Thinking block: collapsible details/summary */
.thinking-block {
    align-self: flex-start;
    background-color: var(--thinking-bg);
    border-radius: 8px;
    padding: 0.5rem 0.75rem;
    max-width: 85%;
}

.thinking-block[open] {
    background-color: #fff3cd;
    border-left: 3px solid #f57c00;
}

.thinking-block.expanded {
    background-color: #fff3cd;
    border-left: 3px solid #f57c00;
}

.thinking-block:not([open]) {
    background-color: var(--thinking-bg);
}

.thinking-block.collapsed {
    background-color: var(--thinking-bg);
}

.thinking-block summary {
    cursor: pointer;
    font-size: 0.875rem;
    font-weight: 500;
    color: #f57c00;
    list-style: none;
    display: flex;
    align-items: center;
    gap: 0.5rem;
}

.thinking-block summary::before {
    content: '';
    display: inline-block;
    width: 16px;
    height: 16px;
    background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='%23f57c00' stroke-width='2'%3E%3Cpath d='M9.663 17h4.673M12 3v1m6.364 1.636l-.707.707M21 12h-1M4 12H3m3.343-5.657l-.707-.707m2.828 9.9a5 5 0 117.072 0l-.548.547A3.984 3.984 0 0016 18H8a3.984 3.984 0 00-2.879-1.121l-.548-.547z'/%3E%3C/svg%3E");
    background-size: contain;
    background-repeat: no-repeat;
}

.thinking-block[open] summary::before {
    transform: rotate(90deg);
}

.thinking-block .thinking-content {
    margin-top: 0.5rem;
    font-size: 0.875rem;
    color: #666666;
    white-space: pre-wrap;
}

/* Streaming indicator for thinking blocks */
.thinking-block.streaming summary {
    font-weight: 600;
}

.thinking-block.streaming .thinking-content::after {
    content: '';
    display: inline-block;
    width: 8px;
    height: 8px;
    background-color: #f57c00;
    border-radius: 50%;
    animation: pulse 1s infinite;
    margin-left: 0.25rem;
}

/* Tool card styling - collapsible details/summary layout */
details.tool-card {
    align-self: flex-start;
    background-color: var(--tool-bg);
    border-radius: 8px;
    padding: 0.5rem 0.75rem;
    max-width: 85%;
}

details.tool-card > summary {
    cursor: pointer;
    list-style: none;
    display: flex;
    align-items: center;
    gap: 0.5rem;
    font-size: 0.875rem;
}

details.tool-card > summary::-webkit-details-marker {
    display: none;
}

details.tool-card .tool-name {
    font-weight: 500;
    color: #7b1fa2;
}

details.tool-card .failed-text {
    color: #666666;
    margin-left: 0.25rem;
}

details.tool-card .tool-card-content {
    margin-top: 0.5rem;
    padding-top: 0.5rem;
    border-top: 1px solid rgba(0, 0, 0, 0.1);
    font-size: 0.75rem;
    max-width: 100%;
}

details.tool-card .tool-card-label {
    font-weight: 500;
    color: #666666;
}

details.tool-card .tool-card-value {
    color: var(--text-color);
    overflow-wrap: break-word;
    word-break: break-word;
}

details.tool-card .tool-card-id {
    margin-bottom: 0.25rem;
}

details.tool-card .tool-card-args {
    margin-bottom: 0.25rem;
}

details.tool-card .tool-card-args-json {
    font-family: 'Consolas', 'Monaco', monospace;
    font-size: 0.7rem;
    background-color: rgba(0, 0, 0, 0.05);
    padding: 0.25rem 0.5rem;
    border-radius: 4px;
    overflow-x: auto;
    margin: 0.25rem 0;
    white-space: pre-wrap;
    word-break: break-all;
}

details.tool-card .tool-card-result {
    margin-top: 0.25rem;
}

/* Running state with spinner animation - border only when expanded */
details.tool-card.running[open] {
    background-color: #f3e5f5;
    border-left: 3px solid #9c27b0;
}

/* Collapsed running state - no border */
details.tool-card.running:not([open]) {
    background-color: var(--tool-bg);
}

/* Success state with checkmark - purple theme when expanded */
details.tool-card.success[open] {
    background-color: #f3e5f5;
    border-left: 3px solid #9c27b0;
}

/* Collapsed success state - no border */
details.tool-card.success:not([open]) {
    background-color: var(--tool-bg);
}

/* Failure state with X - border only when expanded */
details.tool-card.failure[open],
details.tool-card.error[open] {
    background-color: #ffebee;
    border-left: 3px solid var(--error-color);
}

/* Collapsed failure state - no border */
details.tool-card.failure:not([open]),
details.tool-card.error:not([open]) {
    background-color: var(--tool-bg);
}

/* Spinner animation for running state */
details.tool-card .spinner {
    display: inline-block;
    width: 14px;
    height: 14px;
    border: 2px solid #e1bee7;
    border-top-color: #9c27b0;
    border-radius: 50%;
    animation: spin 1s linear infinite;
    flex-shrink: 0;
}

@keyframes spin {
    to { transform: rotate(360deg); }
}

/* Success icon for completed tools */
details.tool-card .success-icon {
    display: inline-block;
    width: 14px;
    height: 14px;
    flex-shrink: 0;
}

details.tool-card .success-icon::before {
    content: '';
    display: block;
    width: 14px;
    height: 14px;
    background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='%234caf50'%3E%3Cpath d='M9 16.17L4.83 12l-1.42 1.41L9 19 21 7l-1.41-1.41L9 16.17z'/%3E%3C/svg%3E");
    background-size: contain;
    background-repeat: no-repeat;
}

/* Error icon for failed tools */
details.tool-card .error-icon {
    display: inline-block;
    width: 14px;
    height: 14px;
    flex-shrink: 0;
}

details.tool-card .error-icon::before {
    content: '';
    display: block;
    width: 14px;
    height: 14px;
    background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='%23d32f2f'%3E%3Cpath d='M19 6.41L17.59 5 12 10.59 6.41 5 5 6.41 10.59 12 5 17.59 6.41 19 12 13.41 17.59 19 19 17.59 13.41 12 19 6.41z'/%3E%3C/svg%3E");
    background-size: contain;
    background-repeat: no-repeat;
}

/* Error card styling */
.error-card {
    align-self: flex-start;
    background-color: var(--error-bg);
    border: 1px solid var(--error-border);
    border-radius: 8px;
    padding: 0.75rem 1rem;
    max-width: 85%;
    display: flex;
    align-items: flex-start;
    gap: 0.75rem;
}

.error-card .icon {
    flex-shrink: 0;
    width: 24px;
    height: 24px;
    display: flex;
    align-items: center;
    justify-content: center;
    background-color: var(--error-color);
    color: white;
    border-radius: 4px;
}

.error-card .icon::before {
    content: '';
    display: block;
    width: 16px;
    height: 16px;
    background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='white' stroke-width='2'%3E%3Ccircle cx='12' cy='12' r='10'/%3E%3Cline x1='12' y1='8' x2='12' y2='12'/%3E%3Cline x1='12' y1='16' x2='12.01' y2='16'/%3E%3C/svg%3E");
    background-size: contain;
    background-repeat: no-repeat;
}

.error-card .message {
    flex: 1;
    font-size: 0.875rem;
    color: var(--error-color);
}

/* Welcome placeholder styling - shown when session is empty */
.welcome-placeholder {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: 2rem 1rem;
    text-align: center;
    color: #666666;
    gap: 1rem;
    animation: fadeIn 0.3s ease;
}

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

.welcome-placeholder .welcome-title {
    font-size: 1.25rem;
    font-weight: 600;
    color: var(--text-color);
    margin: 0;
}

.welcome-placeholder .welcome-subtitle {
    font-size: 0.9rem;
    color: #888888;
    margin: 0;
    max-width: 280px;
}

.welcome-placeholder .welcome-tips {
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
    margin-top: 0.5rem;
}

.welcome-placeholder .welcome-tip {
    font-size: 0.85rem;
    color: #999999;
    padding: 0.5rem 1rem;
    background-color: var(--bg-color);
    border-radius: 8px;
    max-width: 260px;
}

/* Login modal styling (per D-32-02) */
#login-modal {
    width: 90%;
    max-width: 400px;
    padding: 2rem;
    border-radius: 12px;
    background-color: var(--assistant-bg);
    border: 1px solid var(--border-color);
    box-shadow: var(--shadow);
    position: fixed;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
}

#login-modal::backdrop {
    background-color: rgba(0, 0, 0, 0.5);
}

#login-modal h2 {
    font-size: 1.5rem;
    font-weight: 600;
    margin-bottom: 0.5rem;
    color: var(--text-color);
}

#login-modal .modal-subtitle {
    font-size: 0.875rem;
    color: #666666;
    margin-bottom: 1.5rem;
}

#auth-form {
    display: flex;
    flex-direction: column;
    gap: 1rem;
}

#auth-form .form-group {
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
}

#auth-form label {
    font-size: 0.875rem;
    font-weight: 500;
    color: var(--text-color);
}

#auth-form input {
    padding: 0.75rem 1rem;
    border: 1px solid var(--border-color);
    border-radius: 8px;
    font-size: 1rem;
    font-family: inherit;
    outline: none;
    transition: border-color 0.2s ease;
}

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

.auth-error {
    padding: 0.75rem;
    background-color: var(--error-bg);
    border: 1px solid var(--error-border);
    border-radius: 4px;
    color: var(--error-color);
    font-size: 0.875rem;
}

.auth-buttons {
    display: flex;
    gap: 0.75rem;
    margin-top: 0.5rem;
}

#login-btn {
    flex: 1;
    padding: 0.75rem 1rem;
    background-color: var(--primary-color);
    color: white;
    border: none;
    border-radius: 8px;
    font-size: 1rem;
    font-weight: 500;
    cursor: pointer;
    transition: background-color 0.2s ease;
}

#login-btn:hover {
    background-color: var(--primary-hover);
}

#register-btn {
    flex: 1;
    padding: 0.75rem 1rem;
    background-color: var(--assistant-bg);
    color: var(--primary-color);
    border: 1px solid var(--primary-color);
    border-radius: 8px;
    font-size: 1rem;
    font-weight: 500;
    cursor: pointer;
    transition: background-color 0.2s ease, color 0.2s ease;
}

#register-btn:hover {
    background-color: var(--primary-color);
    color: white;
}


/* ============================================================================
   Responsive Layout - Per UI-08
   ============================================================================ */

/* Mobile: max-width 480px */
@media (max-width: 480px) {
    #app {
        max-width: 100%;
    }

    .navbar {
        padding: 0.5rem 0.75rem;
    }

    .nav-title {
        font-size: 1rem;
    }

    #new-session-btn {
        width: 28px;
        height: 28px;
    }

    #new-session-btn::before {
        width: 12px;
        height: 2px;
    }

    #new-session-btn::after {
        width: 2px;
        height: 12px;
    }

    .nav-spacer {
        width: 28px;
    }

    footer {
        padding: 1rem 1.5rem;
        padding-bottom: calc(1.5rem + env(safe-area-inset-bottom, 0rem));
    }

    .input-container {
        gap: 0.5rem;
    }

    #message-input {
        flex: 1;
        height: 40px;
        padding: 0 0.8rem;
        font-size: 0.95rem;
    }

    #input-mode-toggle,
    #voice-mode-toggle {
        width: 40px;
        height: 40px;
        font-size: 1.1rem;
    }

    #input-mode-toggle .iconfont,
    #voice-mode-toggle .iconfont {
        font-size: 24px;
    }

    #voice-input-btn {
        height: 40px;
        padding: 0 1rem;
        font-size: 0.95rem;
    }

    /* Fullscreen login modal on mobile */
    #login-modal {
        width: 100%;
        height: 100%;
        max-width: none;
        max-height: none;
        border: none;
        border-radius: 0;
    }

    .message {
        max-width: 95%;
    }

    .thinking-block,
    details.tool-card,
    .error-card {
        max-width: 95%;
    }
}

/* Tablet: 481px to 768px */
@media (min-width: 481px) and (max-width: 768px) {
    .navbar {
        padding: 0.6rem 1rem;
    }

    #app {
        max-width: 800px;
    }

    footer {
        padding: 1rem 1.5rem;
        padding-bottom: calc(1.5rem + env(safe-area-inset-bottom, 0rem));
    }

    /* Centered login modal on tablet */
    #login-modal {
        width: 90%;
        max-width: 400px;
        max-height: 80vh;
    }
}

/* Desktop: min-width 1024px */
@media (min-width: 1024px) {
    /* Existing default styles apply */
    #app {
        max-width: 800px;
    }
}