:root, [data-theme="light"] {
    --htm-red: #dc291e;
    --htm-red-dark: #b8211a;
    --htm-red-light: #fdf2f2;
    --htm-black: #1a1a1a;
    --bg: #f3f4f6;
    --bg-pattern: rgba(220, 41, 30, 0.03);
    --surface: rgba(255, 255, 255, 0.85);
    --surface-solid: #ffffff;
    --text: #1a1a2e;
    --text-muted: #6b7280;
    --border: #e5e7eb;
    --radius: 12px;
    --shadow: 0 1px 3px rgba(0, 0, 0, 0.08);
    --shadow-lg: 0 4px 12px rgba(0, 0, 0, 0.1);
    --accent-bar-height: 3px;
    --source-bg: #fef9f0;
    --source-border: #e8c87a;
    --source-text: #7c5e1e;
    --warning-bg: #fef2f2;
    --warning-border: var(--htm-red);
    --warning-text: #991b1b;
}

[data-theme="dark"] {
    --htm-red: #e8453a;
    --htm-red-dark: #dc291e;
    --htm-red-light: #2a1515;
    --htm-black: #f5f5f5;
    --bg: #0f1117;
    --bg-pattern: rgba(220, 41, 30, 0.05);
    --surface: rgba(30, 32, 40, 0.85);
    --surface-solid: #1e2028;
    --text: #e8e9ed;
    --text-muted: #9ca3af;
    --border: #2d3040;
    --shadow: 0 1px 3px rgba(0, 0, 0, 0.3);
    --shadow-lg: 0 4px 12px rgba(0, 0, 0, 0.4);
    --source-bg: #1e1c14;
    --source-border: #6b5a2e;
    --source-text: #d4b96a;
    --warning-bg: #1e1214;
    --warning-border: #e8453a;
    --warning-text: #f0a0a0;
}

*,
*::before,
*::after {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
}

html, body {
    margin: 0;
    padding: 0;
    overflow: hidden;
    -webkit-text-size-adjust: 100%;
}

html {
    height: var(--app-height, 100vh);
}

body {
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
    font-size: 16px;
    color: var(--text);
    background: var(--bg);
    height: var(--app-height, 100vh);
}

body::before {
    content: "";
    position: fixed;
    inset: 0;
    z-index: 0;
    pointer-events: none;
    background:
        radial-gradient(ellipse 80% 60% at 20% 10%, rgba(220, 41, 30, 0.06), transparent 60%),
        radial-gradient(ellipse 60% 80% at 85% 80%, rgba(220, 41, 30, 0.04), transparent 50%),
        radial-gradient(ellipse 50% 50% at 50% 50%, rgba(220, 41, 30, 0.02), transparent 70%);
}

[data-theme="dark"] body::before {
    background:
        radial-gradient(ellipse 80% 60% at 20% 10%, rgba(220, 41, 30, 0.08), transparent 60%),
        radial-gradient(ellipse 60% 80% at 85% 80%, rgba(220, 41, 30, 0.05), transparent 50%),
        radial-gradient(ellipse 50% 50% at 50% 50%, rgba(220, 41, 30, 0.03), transparent 70%);
}

body::after {
    content: "";
    position: fixed;
    inset: 0;
    z-index: 0;
    pointer-events: none;
    opacity: 0.3;
    background-image:
        repeating-linear-gradient(
            90deg,
            transparent,
            transparent 59px,
            var(--bg-pattern) 59px,
            var(--bg-pattern) 61px
        );
    mask-image: linear-gradient(180deg, transparent 0%, rgba(0,0,0,0.3) 30%, rgba(0,0,0,0.3) 70%, transparent 100%);
    -webkit-mask-image: linear-gradient(180deg, transparent 0%, rgba(0,0,0,0.3) 30%, rgba(0,0,0,0.3) 70%, transparent 100%);
}

/* Accent bar top */
.accent-bar {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    height: var(--accent-bar-height);
    background: linear-gradient(90deg, var(--htm-red), #e8453a, var(--htm-red));
    z-index: 200;
    box-shadow: 0 1px 6px rgba(220, 41, 30, 0.3);
}

.app {
    display: flex;
    flex-direction: column;
    height: var(--app-height, 100dvh);
    height: var(--app-height, 100vh);
    max-width: min(900px, 100%);
    width: 100%;
    margin: 0 auto;
    position: relative;
    z-index: 1;
    padding-top: var(--accent-bar-height);
    overflow: hidden;
}

/* Header */
.header {
    background: var(--surface);
    backdrop-filter: blur(16px);
    -webkit-backdrop-filter: blur(16px);
    border-bottom: 1px solid var(--border);
    padding: 10px 16px;
    box-shadow: var(--shadow);
    flex-shrink: 0;
}

.header-content {
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.logo {
    display: flex;
    align-items: center;
    gap: 10px;
    min-width: 0;
}

.logo-icon {
    width: 36px;
    height: 36px;
    flex-shrink: 0;
}

.logo h1 {
    font-size: 1rem;
    font-weight: 700;
    color: var(--text);
    line-height: 1.2;
}

.subtitle {
    font-size: 0.7rem;
    color: var(--text-muted);
    font-weight: 400;
}

.header-actions {
    display: flex;
    align-items: center;
    gap: 6px;
    flex-shrink: 0;
}

.sync-btn {
    display: flex;
    align-items: center;
    gap: 6px;
    padding: 8px 12px;
    min-height: 40px;
    min-width: 40px;
    border: 1px solid var(--border);
    border-radius: 8px;
    background: var(--surface);
    color: var(--text-muted);
    cursor: pointer;
    font-size: 0.8125rem;
    transition: all 0.2s;
}

.sync-btn:hover:not(:disabled) {
    border-color: var(--htm-red);
    color: var(--htm-red);
    background: var(--htm-red-light);
}

.sync-btn:disabled {
    opacity: 0.4;
    cursor: not-allowed;
}

.sync-btn.syncing .sync-icon {
    animation: spin 1s linear infinite;
}

.sync-icon {
    width: 16px;
    height: 16px;
}

/* Theme toggle */
.theme-toggle {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 40px;
    min-width: 40px;
    height: 40px;
    min-height: 40px;
    border: 1px solid var(--border);
    border-radius: 8px;
    background: var(--surface);
    color: var(--text-muted);
    cursor: pointer;
    transition: all 0.2s;
    padding: 0;
}

.theme-toggle:hover {
    border-color: var(--htm-red);
    color: var(--htm-red);
    background: var(--htm-red-light);
}

.theme-toggle svg {
    width: 18px;
    height: 18px;
    transition: transform 0.3s ease;
}

.theme-toggle:hover svg {
    transform: rotate(20deg);
}

.theme-icon-sun,
.theme-icon-moon {
    display: none;
}

[data-theme="dark"] .theme-icon-moon { display: block; }
[data-theme="dark"] .theme-icon-sun { display: none; }
[data-theme="light"] .theme-icon-sun { display: block; }
[data-theme="light"] .theme-icon-moon { display: none; }
:root .theme-icon-sun { display: block; }
:root .theme-icon-moon { display: none; }

@keyframes spin {
    from { transform: rotate(0deg); }
    to { transform: rotate(360deg); }
}

/* Chat Area */
.chat-area {
    flex: 1;
    min-height: 0;
    overflow-y: auto;
    overflow-x: hidden;
    -webkit-overflow-scrolling: touch;
    padding: 16px;
    display: flex;
    flex-direction: column;
    gap: 12px;
}

.welcome-message {
    text-align: center;
    padding: 40px 16px;
    max-width: min(460px, 100%);
    margin: auto;
}

.welcome-message h2 {
    font-size: 1.25rem;
    font-weight: 700;
    margin-bottom: 8px;
    color: var(--text);
}

.welcome-message p {
    color: var(--text-muted);
    font-size: 0.875rem;
    line-height: 1.6;
    margin-bottom: 20px;
}

.suggestions {
    display: flex;
    flex-direction: column;
    gap: 8px;
    width: 100%;
}

.suggestion {
    padding: 10px 16px;
    min-height: 44px;
    border: 1px solid var(--border);
    border-radius: 12px;
    background: var(--surface);
    backdrop-filter: blur(8px);
    -webkit-backdrop-filter: blur(8px);
    color: var(--text);
    font-size: 0.8125rem;
    cursor: pointer;
    transition: all 0.2s;
    -webkit-tap-highlight-color: transparent;
    text-align: left;
}

.suggestion:hover {
    border-color: var(--htm-red);
    background: var(--htm-red-light);
    color: var(--htm-red);
}

/* Messages */
.message {
    display: flex;
    gap: 10px;
    max-width: min(85%, calc(100% - 16px));
    animation: fadeIn 0.3s ease;
}

.message.user {
    align-self: flex-end;
}

.message.assistant {
    align-self: flex-start;
    max-width: min(90%, calc(100% - 16px));
}

.message-avatar {
    width: 32px;
    height: 32px;
    min-width: 32px;
    min-height: 32px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
    font-size: 10px;
    font-weight: 700;
    letter-spacing: -0.03em;
    margin-top: 2px;
}

.message.assistant .message-avatar {
    background: var(--border);
    color: var(--text);
}

.message-content {
    padding: 10px 14px;
    border-radius: var(--radius);
    font-size: 0.875rem;
    line-height: 1.6;
    word-break: break-word;
    overflow-wrap: break-word;
    max-width: 100%;
    min-width: 0;
}

.message.user .message-content {
    background: linear-gradient(135deg, var(--htm-red), #e8453a);
    color: white;
    border-bottom-right-radius: 4px;
    white-space: pre-wrap;
}

.message.assistant .message-content {
    background: var(--surface);
    backdrop-filter: blur(8px);
    -webkit-backdrop-filter: blur(8px);
    border: 1px solid var(--border);
    border-bottom-left-radius: 4px;
    box-shadow: var(--shadow);
}

/* Formatted assistant message content */
.message.assistant .message-content p {
    margin: 0 0 8px 0;
}

.message.assistant .message-content p:last-child {
    margin-bottom: 0;
}

.message.assistant .message-content .msg-list {
    margin: 6px 0 10px 0;
    padding-left: 20px;
}

.message.assistant .message-content .msg-list li {
    margin-bottom: 4px;
    line-height: 1.5;
}

.message.assistant .message-content .msg-list li:last-child {
    margin-bottom: 0;
}

.message.assistant .message-content .source-ref {
    margin-top: 12px;
    padding: 8px 12px;
    background: var(--source-bg);
    border-left: 3px solid var(--source-border);
    border-radius: 0 6px 6px 0;
    font-size: 0.8rem;
    color: var(--source-text);
    line-height: 1.5;
}

.message.assistant .message-content .safety-warning {
    margin: 8px 0;
    padding: 8px 12px;
    background: var(--warning-bg);
    border-left: 3px solid var(--warning-border);
    border-radius: 0 6px 6px 0;
    font-size: 0.8125rem;
    color: var(--warning-text);
    font-weight: 500;
    line-height: 1.5;
}

/* Loading indicator */
.typing-indicator {
    display: flex;
    gap: 4px;
    padding: 12px 16px;
}

.typing-dot {
    width: 8px;
    height: 8px;
    border-radius: 50%;
    background: var(--text-muted);
    animation: typing 1.4s infinite;
}

.typing-dot:nth-child(2) { animation-delay: 0.2s; }
.typing-dot:nth-child(3) { animation-delay: 0.4s; }

@keyframes typing {
    0%, 60%, 100% { transform: translateY(0); opacity: 0.4; }
    30% { transform: translateY(-6px); opacity: 1; }
}

@keyframes fadeIn {
    from { opacity: 0; transform: translateY(8px); }
    to { opacity: 1; transform: translateY(0); }
}

/* Input Area */
.input-area {
    padding: 10px 16px;
    padding-bottom: max(12px, env(safe-area-inset-bottom));
    background: var(--surface);
    backdrop-filter: blur(16px);
    -webkit-backdrop-filter: blur(16px);
    border-top: 1px solid var(--border);
    flex-shrink: 0;
}

.input-form {
    display: flex;
    align-items: flex-end;
    gap: 8px;
    background: var(--surface-solid);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 6px 6px 6px 14px;
    transition: border-color 0.2s, box-shadow 0.2s;
}

.input-form:focus-within {
    border-color: var(--htm-red);
    box-shadow: 0 0 0 3px rgba(220, 41, 30, 0.1);
}

.input-field {
    flex: 1;
    min-width: 0;
    border: none;
    background: none;
    font-size: 16px;
    font-family: inherit;
    color: var(--text);
    resize: none;
    outline: none;
    max-height: 100px;
    line-height: 1.5;
    padding: 6px 0;
}

.input-field::placeholder {
    color: var(--text-muted);
}

.send-btn {
    width: 40px;
    min-width: 40px;
    height: 40px;
    min-height: 40px;
    border: none;
    border-radius: 8px;
    background: var(--htm-red);
    color: white;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
    transition: all 0.2s;
    -webkit-tap-highlight-color: transparent;
}

.send-btn:hover:not(:disabled) {
    background: var(--htm-red-dark);
    transform: scale(1.05);
}

.send-btn:active:not(:disabled) {
    transform: scale(0.95);
}

.send-btn:disabled {
    background: var(--border);
    cursor: not-allowed;
}

.send-btn svg {
    width: 18px;
    height: 18px;
}

/* Toast */
.toast {
    position: fixed;
    bottom: max(80px, calc(70px + env(safe-area-inset-bottom)));
    left: 50%;
    transform: translateX(-50%) translateY(20px);
    padding: 10px 20px;
    border-radius: 8px;
    font-size: 0.8rem;
    box-shadow: var(--shadow-lg);
    opacity: 0;
    transition: all 0.3s;
    pointer-events: none;
    z-index: 100;
    max-width: min(90vw, 400px);
    text-align: center;
}

.toast.show {
    opacity: 1;
    transform: translateX(-50%) translateY(0);
}

.toast.success {
    background: #059669;
    color: white;
}

.toast.error {
    background: var(--htm-red);
    color: white;
}

.toast.info {
    background: var(--text);
    color: white;
}

/* Scrollbar */
.chat-area::-webkit-scrollbar {
    width: 6px;
}

.chat-area::-webkit-scrollbar-track {
    background: transparent;
}

.chat-area::-webkit-scrollbar-thumb {
    background: var(--border);
    border-radius: 3px;
}

.chat-area::-webkit-scrollbar-thumb:hover {
    background: var(--text-muted);
}

/* Responsive - small screens */
@media (max-width: 640px) {
    .header {
        padding: 8px 12px;
    }

    .logo-icon {
        width: 32px;
        height: 32px;
    }

    .chat-area {
        padding: 12px;
        gap: 10px;
    }

    .welcome-message {
        padding: 24px 12px;
    }

    .welcome-message h2 {
        font-size: 1.1rem;
    }

    .message {
        gap: 8px;
        max-width: 95%;
    }

    .message.assistant {
        max-width: 95%;
    }

    .message-avatar {
        width: 28px;
        height: 28px;
        min-width: 28px;
        min-height: 28px;
        font-size: 8px;
    }

    .message-content {
        padding: 8px 12px;
        font-size: 0.8125rem;
    }

    .input-area {
        padding: 8px 10px;
        padding-bottom: max(10px, env(safe-area-inset-bottom));
    }

    .input-form {
        padding: 4px 4px 4px 12px;
    }

    .sync-btn span {
        display: none;
    }
}

@media (max-width: 380px) {
    .logo h1 {
        font-size: 0.875rem;
    }

    .header-actions {
        gap: 4px;
    }

    .sync-btn {
        min-width: 36px;
        min-height: 36px;
        padding: 6px;
    }

    .theme-toggle {
        width: 36px;
        min-width: 36px;
        height: 36px;
        min-height: 36px;
    }
}

/* Desktop large */
@media (min-width: 641px) {
    .suggestions {
        flex-direction: row;
        flex-wrap: wrap;
        justify-content: center;
    }

    .suggestion {
        width: auto;
        text-align: center;
        border-radius: 20px;
    }
}
