/* --- Custom properties --- */
:root {
    --color-primary: #003f87;
    --color-primary-hover: #002d5f;
    --color-primary-disabled: #5a7da8;
    --color-text: #1a1a1a;
    --color-text-muted: #555;
    --color-text-light: #777;
    --color-border: #e0e0e0;
    --color-bg: #f5f5f5;
    --color-bg-hover: #f0f0f0;
    --color-bg-surface: #fff;
    --focus-outline: 2px solid var(--color-primary);
    --focus-offset: 2px;
}

*, *::before, *::after {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
}

body {
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
    background: var(--color-bg);
    color: var(--color-text);
    height: 100vh;
    height: 100dvh;
}

/* --- Focus-visible for all interactive elements --- */
.btn:focus-visible,
.login-btn:focus-visible,
.close-btn:focus-visible,
.doc-delete:focus-visible,
.docs-tab:focus-visible,
.drop-zone:focus-visible,
.user-menu-dropdown button:focus-visible,
.org-switcher-dropdown button:focus-visible,
.file-link:focus-visible,
.user-menu-dropdown a:focus-visible {
    outline: var(--focus-outline);
    outline-offset: var(--focus-offset);
}

/* --- Shared close button --- */
.close-btn {
    background: none;
    border: none;
    font-size: 1.5rem;
    cursor: pointer;
    color: var(--color-text-light);
    line-height: 1;
}

.close-btn:hover {
    color: var(--color-text);
}

/* Login page */
.login-container {
    max-width: 480px;
    margin: 15vh auto;
    padding: 2rem;
    text-align: center;
}

.login-container h1 {
    font-size: 2rem;
    margin-bottom: 1rem;
}

.login-container p {
    color: var(--color-text-muted);
    margin-bottom: 1rem;
    line-height: 1.5;
}

.login-btn {
    display: inline-block;
    margin-top: 1.5rem;
    padding: 0.75rem 2rem;
    background: var(--color-primary);
    color: var(--color-bg-surface);
    text-decoration: none;
    border-radius: 6px;
    font-size: 1rem;
    font-weight: 500;
}

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

/* Chat page */
.chat-container {
    display: flex;
    flex-direction: column;
    height: 100vh;
    height: 100dvh;
    max-width: 900px;
    margin: 0 auto;
    background: var(--color-bg-surface);
}

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

header h1 {
    font-size: 1.2rem;
}

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

/* About modal */
.about-overlay {
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.4);
    display: none;
    justify-content: center;
    align-items: center;
    z-index: 200;
}

.about-overlay.open {
    display: flex;
}

.about-modal {
    background: var(--color-bg-surface);
    border-radius: 10px;
    box-shadow: 0 4px 24px rgba(0, 0, 0, 0.15);
    width: 100%;
    max-width: 400px;
    max-height: 80vh;
    overflow-y: auto;
}

.about-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 1rem 1.25rem;
    border-bottom: 1px solid var(--color-border);
}

.about-header h2 {
    font-size: 1.1rem;
    margin: 0;
}

.about-body {
    padding: 1rem 1.25rem;
}

.about-section {
    margin-bottom: 1rem;
}

.about-section:last-child {
    margin-bottom: 0;
}

.about-section h3 {
    font-size: 0.75rem;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    color: #888;
    margin-bottom: 0.4rem;
}

.about-dl {
    display: grid;
    grid-template-columns: auto 1fr;
    gap: 0.2rem 0.75rem;
    font-size: 0.88rem;
}

.about-dl dt {
    color: var(--color-text-muted);
    font-weight: 500;
}

.about-dl dd {
    color: var(--color-text);
    word-break: break-all;
}

.about-privacy {
    font-size: 0.85rem;
    color: var(--color-text-muted);
    line-height: 1.5;
}

.session-warning {
    padding: 0.5rem 1.5rem;
    background: #fff3cd;
    color: #856404;
    font-size: 0.85rem;
    border-bottom: 1px solid #ffc107;
}

.session-warning a {
    color: #533f03;
    font-weight: 600;
}

.btn {
    padding: 0.4rem 0.8rem;
    border: 1px solid #ccc;
    border-radius: 4px;
    background: var(--color-bg-surface);
    cursor: pointer;
    font-size: 0.85rem;
    text-decoration: none;
    color: var(--color-text);
}

.btn:hover {
    background: var(--color-bg-hover);
}

.btn-secondary {
    background: var(--color-bg-surface);
    color: var(--color-text);
    border-color: #ccc;
}

.btn-secondary:hover {
    background: var(--color-bg-hover);
}

.btn-primary {
    background: var(--color-primary);
    color: var(--color-bg-surface);
    border-color: var(--color-primary);
    padding: 0.6rem 1.5rem;
    font-size: 0.95rem;
}

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

.btn-primary:disabled {
    background: var(--color-primary-disabled);
    cursor: not-allowed;
}

/* Messages */
.messages {
    flex: 1;
    overflow-y: auto;
    padding: 1.5rem;
    display: flex;
    flex-direction: column;
    gap: 1rem;
}

.message {
    max-width: 85%;
    line-height: 1.5;
}

.message.user {
    align-self: flex-end;
}

.message.assistant {
    align-self: flex-start;
}

.message-content {
    padding: 0.75rem 1rem;
    border-radius: 12px;
    overflow-wrap: break-word;
}

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

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

/* --- Markdown content styling --- */

.message-content p {
    margin-bottom: 0.5rem;
}

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

/* Headings */
.message-content h1,
.message-content h2,
.message-content h3,
.message-content h4 {
    margin: 0.75rem 0 0.35rem;
    line-height: 1.3;
}

.message-content h1:first-child,
.message-content h2:first-child,
.message-content h3:first-child,
.message-content h4:first-child {
    margin-top: 0;
}

.message-content h1 { font-size: 1.2em; }
.message-content h2 { font-size: 1.1em; }
.message-content h3 { font-size: 1em; }
.message-content h4 { font-size: 0.95em; color: #444; }

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

.message-content li {
    margin-bottom: 0.2rem;
}

.message-content li > ul,
.message-content li > ol {
    margin: 0.15rem 0 0;
}

/* Bold & italic */
.message-content strong { font-weight: 600; }
.message-content em { font-style: italic; }

/* Links */
.message-content a {
    color: var(--color-primary);
    text-decoration: underline;
}

.message.user .message-content a {
    color: #b3d4ff;
}

/* Blockquotes */
.message-content blockquote {
    border-left: 3px solid #ccc;
    padding: 0.25rem 0 0.25rem 0.75rem;
    margin: 0.5rem 0;
    color: var(--color-text-muted);
}

/* Horizontal rules */
.message-content hr {
    border: none;
    border-top: 1px solid #ddd;
    margin: 0.75rem 0;
}

/* Tables */
.message-content table {
    border-collapse: collapse;
    margin: 0.5rem 0;
    font-size: 0.9rem;
    display: block;
    overflow-x: auto;
    max-width: 100%;
}

.message-content th,
.message-content td {
    border: 1px solid #ccc;
    padding: 0.35rem 0.6rem;
    text-align: left;
}

.message-content th {
    background: #e0e0e0;
    font-weight: 600;
}

.message-content tr:nth-child(even) {
    background: rgba(0, 0, 0, 0.02);
}

/* Code */
.message-content code {
    background: rgba(0, 0, 0, 0.08);
    padding: 0.1rem 0.3rem;
    border-radius: 3px;
    font-size: 0.88em;
}

.message-content pre {
    background: rgba(0, 0, 0, 0.06);
    padding: 0.75rem;
    border-radius: 6px;
    overflow-x: auto;
    margin: 0.5rem 0;
}

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

.tool-indicator {
    font-size: 0.8rem;
    color: var(--color-text-light);
    padding: 0.25rem 0;
    font-style: italic;
}

/* Expandable tool call details */
.tool-detail {
    font-size: 0.82rem;
    color: var(--color-text-muted);
    border: 1px solid var(--color-border);
    border-radius: 8px;
    padding: 0;
    max-width: 85%;
}

.tool-detail summary {
    padding: 0.4rem 0.75rem;
    cursor: pointer;
    user-select: none;
    list-style: none;
    display: flex;
    align-items: center;
    gap: 0.4rem;
}

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

.tool-detail summary::after {
    content: "▸";
    font-size: 0.7rem;
    margin-left: auto;
    color: var(--color-text-light);
    transition: transform 0.15s;
}

.tool-detail[open] summary::after {
    transform: rotate(90deg);
}

.tool-status {
    display: inline-block;
    width: 8px;
    height: 8px;
    border-radius: 50%;
    flex-shrink: 0;
}

.tool-status.running {
    background: #f0ad4e;
    animation: pulse 1.2s ease-in-out infinite;
}

.tool-status.done {
    background: #5cb85c;
}

@keyframes pulse {
    0%, 100% { opacity: 1; }
    50% { opacity: 0.4; }
}

@media (prefers-reduced-motion: reduce) {
    .tool-status.running {
        animation: none;
    }
}

.tool-detail-body {
    border-top: 1px solid var(--color-border);
    padding: 0.5rem 0.75rem;
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
}

.tool-detail-label {
    font-size: 0.72rem;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.03em;
    color: var(--color-text-light);
    margin-bottom: 0.2rem;
}

.tool-detail-data {
    background: rgba(0, 0, 0, 0.04);
    padding: 0.5rem;
    border-radius: 4px;
    font-size: 0.75rem;
    overflow-x: auto;
    max-height: 200px;
    overflow-y: auto;
    margin: 0;
    white-space: pre-wrap;
    word-break: break-all;
}

/* File download links */
.message-files {
    display: flex;
    flex-wrap: wrap;
    gap: 0.5rem;
    margin-top: 0.75rem;
}

.file-link {
    display: inline-flex;
    align-items: center;
    gap: 0.25rem;
    padding: 0.35rem 0.75rem;
    background: var(--color-bg-surface);
    border: 1px solid var(--color-border);
    border-radius: 6px;
    font-size: 0.85rem;
    color: var(--color-primary) !important;
    text-decoration: none !important;
    max-width: 100%;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.file-link:hover,
.file-link:active {
    background: var(--color-bg-hover);
    border-color: var(--color-primary);
}

.status-indicator {
    font-size: 0.85rem;
    color: var(--color-text-muted);
    padding: 0.5rem 0;
}

.loading-dots::after {
    content: '';
    animation: dots 1.5s steps(4, end) infinite;
}

@keyframes dots {
    0%   { content: ''; }
    25%  { content: '.'; }
    50%  { content: '..'; }
    75%  { content: '...'; }
}

@media (prefers-reduced-motion: reduce) {
    .loading-dots::after {
        animation: none;
        content: '...';
    }
}

/* Input area */
.input-area {
    display: flex;
    gap: 0.75rem;
    padding: 1rem 1.5rem;
    border-top: 1px solid var(--color-border);
    background: var(--color-bg-surface);
    align-items: center;
}

.input-wrap {
    flex: 1;
    position: relative;
}

#message-input {
    width: 100%;
    padding: 0.6rem 0.8rem;
    border: 1px solid #ccc;
    border-radius: 8px;
    font-family: inherit;
    font-size: 0.95rem;
    resize: none;
    outline: none;
    max-height: 150px;
}

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

#message-input:focus-visible {
    outline: var(--focus-outline);
    outline-offset: var(--focus-offset);
}

.char-count {
    display: block;
    font-size: 0.75rem;
    color: var(--color-text-light);
    text-align: right;
    margin-top: 0.25rem;
}

.char-count.over {
    color: #dc3545;
    font-weight: 600;
}

/* Documents panel (slide-over) */
.docs-panel {
    position: fixed;
    top: 0;
    right: -380px;
    width: 380px;
    height: 100vh;
    height: 100dvh;
    background: var(--color-bg-surface);
    border-left: 1px solid var(--color-border);
    box-shadow: -2px 0 8px rgba(0, 0, 0, 0.1);
    display: flex;
    flex-direction: column;
    transition: right 0.25s ease;
    z-index: 100;
}

.docs-panel.open {
    right: 0;
}

.docs-panel-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0.75rem 1rem;
    border-bottom: 1px solid var(--color-border);
}

.docs-panel-header h2 {
    font-size: 1.1rem;
    margin: 0;
}

.docs-tabs {
    display: flex;
    border-bottom: 1px solid var(--color-border);
}

.docs-tab {
    flex: 1;
    padding: 0.5rem;
    background: none;
    border: none;
    border-bottom: 2px solid transparent;
    cursor: pointer;
    font-size: 0.85rem;
    color: var(--color-text-muted);
}

.docs-tab.active {
    color: var(--color-primary);
    border-bottom-color: var(--color-primary);
    font-weight: 600;
}

.docs-tab:hover {
    background: var(--color-bg);
}

.docs-pane {
    flex: 1;
    overflow-y: auto;
    padding: 1rem;
}

/* Drop zone */
.drop-zone {
    border: 2px dashed #ccc;
    border-radius: 8px;
    padding: 1.25rem;
    text-align: center;
    cursor: pointer;
    transition: border-color 0.2s, background 0.2s;
    margin-bottom: 1rem;
}

.drop-zone:hover,
.drop-zone:focus-visible,
.drop-zone.drag-over {
    border-color: var(--color-primary);
    background: #f0f5ff;
}

.drop-zone p {
    margin: 0;
    color: var(--color-text-muted);
    font-size: 0.9rem;
}

.drop-zone-hint {
    color: #999 !important;
    font-size: 0.8rem !important;
    margin-top: 0.25rem !important;
}

/* File list */
.doc-file-list {
    display: flex;
    flex-direction: column;
    gap: 0.25rem;
}

.doc-item {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0.4rem 0.5rem;
    border-radius: 4px;
    font-size: 0.85rem;
}

.doc-item:hover {
    background: var(--color-bg);
}

.doc-name {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    flex: 1;
    min-width: 0;
}

.doc-delete {
    background: none;
    border: none;
    cursor: pointer;
    color: #999;
    font-size: 0.9rem;
    padding: 0 0.25rem;
    flex-shrink: 0;
}

.doc-delete:hover {
    color: #dc3545;
}

.docs-empty {
    color: #999;
    font-size: 0.85rem;
    text-align: center;
    padding: 1rem;
}

.docs-loading {
    color: var(--color-text-light);
    font-size: 0.85rem;
    text-align: center;
    padding: 1rem;
}

.doc-upload-status {
    font-size: 0.8rem;
    padding: 0.4rem 0.5rem;
    border-radius: 4px;
    background: var(--color-bg-hover);
    margin-bottom: 0.25rem;
}

.doc-upload-status.success {
    background: #d4edda;
    color: #155724;
}

.doc-upload-status.error {
    background: #f8d7da;
    color: #721c24;
}

/* Org switcher dropdown */
.org-switcher {
    position: relative;
}

.org-switcher-dropdown {
    display: none;
    position: absolute;
    top: 100%;
    right: 0;
    margin-top: 0.25rem;
    background: var(--color-bg-surface);
    border: 1px solid var(--color-border);
    border-radius: 6px;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
    min-width: 200px;
    max-height: 300px;
    overflow-y: auto;
    z-index: 50;
}

.org-switcher-dropdown.open {
    display: block;
}

.org-option {
    display: block;
    width: 100%;
    padding: 0.5rem 1rem;
    background: none;
    border: none;
    text-align: left;
    font-size: 0.85rem;
    color: var(--color-text);
    cursor: pointer;
}

.org-option:hover {
    background: var(--color-bg-hover);
}

.org-option.active {
    color: var(--color-primary);
    font-weight: 600;
    cursor: default;
}

/* User menu dropdown */
.user-menu {
    position: relative;
}

.user-menu-dropdown {
    display: none;
    position: absolute;
    top: 100%;
    right: 0;
    margin-top: 0.25rem;
    background: var(--color-bg-surface);
    border: 1px solid var(--color-border);
    border-radius: 6px;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
    min-width: 140px;
    z-index: 50;
    overflow: hidden;
}

.user-menu-dropdown.open {
    display: block;
}

.user-menu-dropdown button,
.user-menu-dropdown a {
    display: block;
    width: 100%;
    padding: 0.5rem 1rem;
    background: none;
    border: none;
    text-align: left;
    font-size: 0.85rem;
    color: var(--color-text);
    cursor: pointer;
    text-decoration: none;
}

.user-menu-dropdown button:hover,
.user-menu-dropdown a:hover {
    background: var(--color-bg-hover);
}

/* Responsive */
@media (max-width: 600px) {
    .login-container {
        margin: 5vh auto;
        padding: 1.5rem;
    }
    .chat-container {
        max-width: 100%;
    }
    header h1 {
        display: none;
    }
    .header-actions {
        flex: 1;
        justify-content: flex-end;
        gap: 0.5rem;
        flex-wrap: wrap;
    }
    .message {
        max-width: 95%;
    }
    .message-content {
        overflow-x: auto;
    }
    header {
        padding: 0.5rem 1rem;
    }
    .btn {
        padding: 0.6rem 0.8rem;
    }
    .btn-primary {
        padding: 0.6rem 1rem;
    }
    #message-input {
        padding: 0.75rem;
        font-size: 1rem;
    }
    .messages {
        padding: 1rem;
    }
    .input-area {
        padding: 0.75rem 1rem;
    }
    .session-warning {
        padding: 0.5rem 1rem;
    }
    .about-modal {
        margin: 0 1rem;
        max-width: calc(100% - 2rem);
    }
    .user-menu-dropdown {
        min-width: 160px;
        right: -0.5rem;
    }
    #org-switcher-toggle {
        max-width: 150px;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
    }
    .docs-panel {
        width: 100%;
        right: -100%;
    }
}

/* Docs panel backdrop (visible on mobile when panel is open) */
.docs-backdrop {
    display: none;
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.4);
    z-index: 99;
}

.docs-backdrop.open {
    display: block;
}
