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

body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, sans-serif;
    height: 100vh;
    background: #f0f2f5;
}

body:has(> #app > .sidebar) {
    overflow: hidden;
}

#app {
    display: flex;
    width: 100%;
    height: 100%;
    min-width: 0;
    min-height: 0;
}

.page {
    width: 100%;
    height: 100%;
    min-width: 0;
}

#main-page {
    min-height: 0;
}

/* Sidebar */
.sidebar {
    width: 280px;
    flex: 0 0 280px;
    min-height: 0;
    background: #2d3748;
    color: white;
    display: flex;
    flex-direction: column;
    padding: 1rem;
}

.sidebar h2 {
    margin-bottom: 1rem;
    font-size: 1.2rem;
}

.btn-primary,
.btn-secondary,
.btn-link {
    border: none;
    border-radius: 5px;
    font: inherit;
    min-height: 44px;
    cursor: pointer;
    transition: background 0.3s, color 0.3s;
}

.btn-primary:focus-visible,
.btn-secondary:focus-visible,
.btn-link:focus-visible {
    outline: 3px solid #f6ad55;
    outline-offset: 2px;
}

.btn-primary:disabled,
.btn-secondary:disabled,
.btn-link:disabled {
    opacity: 0.55;
    cursor: not-allowed;
}

.btn-secondary {
    background: #4a5568;
    color: white;
    padding: 0.75rem;
    margin-bottom: 1rem;
}

.btn-secondary:not(:disabled):hover {
    background: #718096;
}

.sidebar-header .btn-secondary {
    width: 100%;
}

.btn-link {
    width: 100%;
    padding: 0.5rem 0;
    background: transparent;
    color: #a0aec0;
    text-align: left;
}

.btn-link:not(:disabled):hover {
    color: white;
}

.session-list {
    flex: 1;
    overflow-y: auto;
}

.session-item {
    padding: 0.75rem;
    cursor: pointer;
    border-radius: 5px;
    margin-bottom: 0.25rem;
    transition: background 0.3s;
}

.session-item:hover {
    background: #4a5568;
}

.session-item.active {
    background: #4a5568;
}

.session-title {
    font-size: 0.9rem;
    margin-bottom: 0.25rem;
}

.session-time {
    font-size: 0.75rem;
    color: #a0aec0;
}

.sidebar-footer {
    margin-top: auto;
    padding-top: 1rem;
    border-top: 1px solid #4a5568;
}

.sidebar-footer a {
    color: #a0aec0;
    text-decoration: none;
    display: block;
    padding: 0.5rem 0;
    transition: color 0.3s;
}

.sidebar-footer a:hover {
    color: white;
}

/* Main Content */
.main-content {
    flex: 1;
    min-width: 0;
    min-height: 0;
    display: flex;
    flex-direction: column;
}

.chat-header {
    padding: 1rem 2rem;
    background: white;
    border-bottom: 1px solid #e2e8f0;
    font-weight: bold;
    color: #2d3748;
}

.message-container {
    flex: 1;
    min-height: 0;
    overflow-y: auto;
    padding: 2rem;
}

.message {
    margin-bottom: 1.5rem;
    max-width: 70%;
}

.message.user {
    margin-left: auto;
}

.message.system {
    margin: 0 auto;
    text-align: center;
    max-width: 90%;
}

.message-content {
    padding: 1rem;
    border-radius: 10px;
    line-height: 1.5;
    white-space: pre-wrap;
    word-wrap: break-word;
}

.message.user .message-content {
    background: #667eea;
    color: white;
    border-bottom-right-radius: 0;
}

.message.assistant .message-content {
    background: white;
    border: 1px solid #e2e8f0;
    border-bottom-left-radius: 0;
}

.message.system .message-content {
    background: #fed7d7;
    color: #c53030;
    border: 1px solid #fc8181;
}

.message-role {
    font-size: 0.8rem;
    color: #718096;
    margin-bottom: 0.25rem;
}

.input-area {
    padding: 1rem 2rem;
    background: white;
    border-top: 1px solid #e2e8f0;
    display: flex;
    gap: 1rem;
}

.input-area textarea {
    flex: 1;
    min-width: 0;
    resize: none;
    padding: 0.75rem;
    border: 1px solid #e2e8f0;
    border-radius: 5px;
    font-family: inherit;
    font-size: 1rem;
    line-height: 1.5;
    outline: none;
    transition: border-color 0.3s;
}

.input-area textarea:focus {
    border-color: #667eea;
}

.btn-primary {
    padding: 0.75rem 1.5rem;
    background: #667eea;
    color: white;
}

.btn-primary:not(:disabled):hover {
    background: #5a67d8;
}

/* Settings Page */
.settings-container {
    max-width: 600px;
    margin: 2rem auto;
    padding: 2rem;
    background: white;
    border-radius: 10px;
    box-shadow: 0 2px 10px rgba(0,0,0,0.1);
}

.settings-container h1 {
    margin-bottom: 1.5rem;
    color: #2d3748;
}

.form-group {
    margin-bottom: 1.5rem;
}

.form-group label {
    display: block;
    margin-bottom: 0.5rem;
    color: #4a5568;
    font-weight: 500;
}

.form-group input,
.form-group textarea {
    width: 100%;
    padding: 0.75rem;
    border: 1px solid #e2e8f0;
    border-radius: 5px;
    font-size: 1rem;
    outline: none;
    transition: border-color 0.3s;
}

.form-group input:focus,
.form-group textarea:focus {
    border-color: #667eea;
}

.form-group textarea {
    resize: vertical;
    min-height: 120px;
}

.settings-container button[type="submit"] {
    width: 100%;
    padding: 0.75rem;
    background: #667eea;
    color: white;
    border: none;
    border-radius: 5px;
    font-size: 1rem;
    cursor: pointer;
    transition: background 0.3s;
}

.settings-container button[type="submit"]:hover {
    background: #5a67d8;
}

.button-group {
    display: flex;
    gap: 0.75rem;
}

.button-group .btn-primary,
.button-group .btn-secondary {
    flex: 1;
    width: auto;
    margin-bottom: 0;
}

#settings-message {
    margin-top: 1rem;
    padding: 0.75rem;
    border-radius: 5px;
    text-align: center;
}

#settings-message.success-message {
    background: #c6f6d5;
    color: #276749;
    border: 1px solid #9ae6b4;
}

#settings-message.error-message {
    background: #fed7d7;
    color: #c53030;
    border: 1px solid #fc8181;
}

/* Login Page */
.mode-selector {
    margin: 0;
    padding: 0;
    border: 0;
}

.mode-selector legend {
    margin-bottom: 0.75rem;
    color: #1a1a1a;
    font-weight: 600;
}

.mode-options {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 0.75rem;
}

.mode-option {
    display: grid;
    grid-template-columns: auto 1fr;
    gap: 0.25rem 0.65rem;
    padding: 1rem;
    color: #1a1a1a;
    background: #ffffff;
    border: 1px solid #d4d4d4;
    border-radius: 8px;
    cursor: pointer;
    transition: border-color 0.2s, background 0.2s;
}

.mode-selector:disabled .mode-option {
    opacity: 0.6;
    cursor: wait;
    pointer-events: none;
}

.mode-option:hover {
    border-color: #737373;
    background: #fafafa;
}

.mode-option:has(input:checked) {
    color: #ffffff;
    background: #171717;
    border-color: #171717;
}

.mode-option input {
    grid-row: 1 / span 2;
    margin: 0.2rem 0 0;
    accent-color: #171717;
}

.mode-option:has(input:checked) input {
    accent-color: #ffffff;
}

.mode-option-title {
    font-weight: 600;
}

.mode-option-description {
    color: #737373;
    font-size: 0.85rem;
    line-height: 1.4;
}

.mode-option:has(input:checked) .mode-option-description {
    color: #d4d4d4;
}

.setting-help {
    margin: 0.65rem 0 0;
    color: #737373;
    font-size: 0.85rem;
    line-height: 1.5;
}

@media (max-width: 560px) {
    .mode-options {
        grid-template-columns: 1fr;
    }
}
.login-container {
    display: flex;
    align-items: center;
    justify-content: center;
    height: 100vh;
    width: 100vw;
    flex: 1;
    background: linear-gradient(135deg, #0f4c81 0%, #2563eb 100%);
}

.login-card {
    background: white;
    padding: 2rem;
    border-radius: 10px;
    box-shadow: 0 10px 25px rgba(0,0,0,0.1);
    width: 100%;
    max-width: 400px;
}

.login-card h1 {
    text-align: center;
    color: #333;
    margin-bottom: 1.5rem;
}

.login-card .form-group {
    margin-bottom: 1rem;
}

.login-card label {
    display: block;
    margin-bottom: 0.5rem;
    color: #555;
}

.login-card input[type="password"] {
    width: 100%;
    padding: 0.75rem;
    border: 1px solid #ddd;
    border-radius: 5px;
    box-sizing: border-box;
    font-size: 1rem;
}

.login-card button {
    width: 100%;
    padding: 0.75rem;
    background: #667eea;
    color: white;
    border: none;
    border-radius: 5px;
    font-size: 1rem;
    cursor: pointer;
    transition: background 0.3s;
}

.login-card button:hover {
    background: #5a67d8;
}

.error-message {
    color: #e53e3e;
    margin-top: 0.5rem;
    text-align: center;
}

/* Scrollbar */
::-webkit-scrollbar {
    width: 8px;
}

::-webkit-scrollbar-track {
    background: transparent;
}

::-webkit-scrollbar-thumb {
    background: #cbd5e0;
    border-radius: 4px;
}

::-webkit-scrollbar-thumb:hover {
    background: #a0aec0;
}

@media (max-width: 768px) {
    #app,
    #main-page {
        flex-direction: column;
    }

    .sidebar {
        width: 100%;
        flex: 0 0 auto;
        max-height: 40vh;
    }

    .main-content {
        width: 100%;
    }

    .chat-header,
    .message-container,
    .input-area {
        padding-left: 1rem;
        padding-right: 1rem;
    }

    .message {
        max-width: 90%;
    }

    .input-area {
        gap: 0.5rem;
    }

    .input-area .btn-primary {
        padding-left: 1rem;
        padding-right: 1rem;
    }

    .settings-container {
        margin: 1rem;
        padding: 1.25rem;
    }
}

@media (max-width: 480px) {
    .sidebar {
        max-height: 35vh;
    }

    .input-area {
        flex-direction: column;
    }

    .input-area .btn-primary {
        width: 100%;
    }
}

/* R2 conversation management */
#session-search { width:100%; padding:10px; margin-bottom:12px; border:1px solid #718096; border-radius:6px; font:inherit; }
button.session-item { display:block; width:100%; border:0; background:transparent; color:inherit; text-align:left; font:inherit; }
button.session-item.active, button.session-item:hover { background:#4a5568; }
button.session-item:focus-visible { outline:2px solid #f6ad55; }
.session-title { overflow-wrap:anywhere; }
.chat-header { display:flex; align-items:center; justify-content:space-between; gap:12px; flex-wrap:wrap; }
.chat-header h3 { overflow-wrap:anywhere; min-width:0; }
.chat-actions { display:flex; gap:8px; flex-wrap:wrap; }
.chat-actions .btn-secondary { margin:0; padding:8px 12px; }
#chat-notice { padding:8px 16px; color:#4a5568; font-size:0.875rem; }
#chat-notice:empty { display:none; }
.retry-message { margin:8px 0 0; }
