:root {
    --font-main: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
    --font-mono: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace;

    --bg-base: #0f172a;
    --bg-surface: #1e293b;
    --bg-card: #0f172a;
    --border: #334155;
    --border-highlight: #475569;
    --accent: #38bdf8;
    --accent-hover: #0284c7;
    --text-main: #f8fafc;
    --text-muted: #94a3b8;
    --code-bg: #1e293b;
    --alert-red: #f87171;
    --alert-bg: rgba(248, 113, 113, 0.1);
}

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

/* Ensure the HTML hidden attribute always takes precedence over any display rules */
[hidden] { display: none !important; }

body {
    font-family: var(--font-main);
    background-color: var(--bg-base);
    color: var(--text-main);
    min-height: 100vh;
    display: flex;
    flex-direction: column;
    align-items: center;
    padding: 2rem 1rem;
    line-height: 1.6;
}

.layout-container {
    width: 100%;
    max-width: 900px;
    display: flex;
    flex-direction: column;
    gap: 1.5rem;
}

header {
    display: flex;
    flex-direction: column;
    gap: 0.25rem;
}

.header-top-row {
    display: flex;
    align-items: center;
    gap: 0.75rem;
    width: 100%;
}

.brand-logo {
    width: 44px;
    height: 44px;
    flex-shrink: 0;
    filter: drop-shadow(0 0 8px rgba(56, 189, 248, 0.4));
}

.brand-title {
    font-size: 1.75rem;
    font-weight: 700;
    letter-spacing: -0.025em;
    color: #ffffff;
    display: flex;
    align-items: center;
    gap: 0.5rem;
}

.brand-subtitle {
    font-size: 0.875rem;
    color: var(--text-muted);
}

.control-bar {
    background-color: var(--bg-surface);
    border: 1px solid var(--border);
    border-radius: 8px;
    padding: 1.25rem;
    display: flex;
    flex-direction: column;
    gap: 0.75rem;
}

.control-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 1rem;
}

.date-badge {
    background: rgba(56, 189, 248, 0.1);
    color: var(--accent);
    border: 1px solid rgba(56, 189, 248, 0.2);
    font-size: 0.75rem;
    font-weight: 600;
    padding: 0.25rem 0.6rem;
    border-radius: 20px;
    letter-spacing: 0.02em;
    white-space: nowrap;
}

.pills-label {
    font-size: 0.75rem;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    color: var(--text-muted);
    font-weight: 600;
}

.date-pills-container {
    display: flex;
    gap: 0.5rem;
    overflow-x: auto;
    padding-bottom: 0.4rem;
    scroll-behavior: smooth;
}

.date-pills-container::-webkit-scrollbar {
    height: 4px;
}

.date-pills-container::-webkit-scrollbar-thumb {
    background-color: var(--border);
    border-radius: 4px;
}

.date-pill {
    background-color: var(--bg-card);
    border: 1px solid var(--border);
    color: var(--text-muted);
    padding: 0.4rem 0.75rem;
    border-radius: 6px;
    font-size: 0.8rem;
    white-space: nowrap;
    cursor: pointer;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 0.1rem;
    transition: all 0.15s ease;
    flex-shrink: 0;
}

.date-pill:hover {
    border-color: var(--accent);
    color: var(--text-main);
}

.date-pill.active {
    background-color: var(--accent);
    border-color: var(--accent);
    color: #0f172a;
    font-weight: 600;
}

.date-pill .pill-sub {
    font-size: 0.7rem;
    opacity: 0.8;
}

/* Learn More Panel Styling */
.subtitle-row {
    display: flex;
    align-items: center;
    gap: 0.75rem;
    flex-wrap: wrap;
}

.learn-more-btn {
    background: rgba(56, 189, 248, 0.1);
    color: var(--accent);
    border: 1px solid rgba(56, 189, 248, 0.25);
    font-size: 0.75rem;
    font-weight: 600;
    padding: 0.2rem 0.6rem;
    border-radius: 4px;
    cursor: pointer;
    transition: all 0.15s ease;
    display: inline-flex;
    align-items: center;
    gap: 0.3rem;
}

.learn-more-btn:hover {
    background: rgba(56, 189, 248, 0.2);
    border-color: var(--accent);
}

.info-panel {
    display: none;
    background-color: var(--bg-surface);
    border: 1px solid var(--border-highlight);
    border-left: 4px solid var(--accent);
    border-radius: 8px;
    padding: 1.25rem;
    margin-top: 0.5rem;
    font-size: 0.875rem;
    color: #cbd5e1;
    line-height: 1.6;
    animation: fadeIn 0.2s ease-in-out;
}

.info-panel.visible {
    display: block;
}

.info-panel h4 {
    color: #ffffff;
    font-size: 0.95rem;
    margin-bottom: 0.5rem;
}

.info-panel p {
    margin-bottom: 0.75rem;
}

.info-panel p:last-child {
    margin-bottom: 0;
}

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

button#fetchBtn {
    background-color: var(--accent);
    color: #0f172a;
    font-weight: 600;
    font-size: 0.875rem;
    padding: 0.5rem 1.25rem;
    border: none;
    border-radius: 6px;
    cursor: pointer;
    transition: background-color 0.15s ease, opacity 0.15s ease;
}

button#fetchBtn:hover { background-color: var(--accent-hover); color: #ffffff; }
button#fetchBtn:disabled { opacity: 0.5; cursor: not-allowed; }

.brief-card {
    background-color: var(--bg-surface);
    border: 1px solid var(--border);
    border-radius: 8px;
    padding: 2.5rem;
    min-height: 400px;
    box-shadow: 0 10px 15px -3px rgba(0, 0, 0, 0.3);
}

.brief-meta-bar {
    display: flex;
    align-items: center;
    gap: 0.75rem;
    margin-bottom: 1.5rem;
    padding-bottom: 1rem;
    border-bottom: 1px solid var(--border);
    font-size: 0.8rem;
    color: var(--text-muted);
}

.meta-badge {
    background: rgba(56, 189, 248, 0.1);
    color: var(--accent);
    padding: 0.2rem 0.5rem;
    border-radius: 4px;
    font-weight: 600;
    border: 1px solid rgba(56, 189, 248, 0.2);
}

.loading-state {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    height: 300px;
    gap: 1rem;
    color: var(--text-muted);
}

.spinner {
    width: 32px;
    height: 32px;
    border: 3px solid var(--border);
    border-top-color: var(--accent);
    border-radius: 50%;
    animation: spin 0.8s linear infinite;
}

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

/* Rendered Markdown Styling */
.markdown-body {
    font-family: var(--font-main);
}

.markdown-body h1 {
    font-size: 1.75rem;
    border-bottom: 1px solid var(--border);
    padding-bottom: 0.5rem;
    margin: 1.5rem 0 1rem 0;
    color: #ffffff;
}

.markdown-body h1:first-child { margin-top: 0; }

.markdown-body h2 {
    font-size: 1.35rem;
    margin: 1.5rem 0 0.75rem 0;
    color: var(--accent);
}

.markdown-body h3 {
    font-size: 1.1rem;
    margin: 1.25rem 0 0.5rem 0;
    color: var(--text-main);
}

.markdown-body p {
    margin-bottom: 1rem;
    color: #cbd5e1;
}

.markdown-body ul, .markdown-body ol {
    margin-bottom: 1rem;
    padding-left: 1.5rem;
}

.markdown-body li {
    margin-bottom: 0.4rem;
    color: #cbd5e1;
}

.markdown-body blockquote {
    border-left: 4px solid var(--accent);
    background: rgba(56, 189, 248, 0.05);
    padding: 0.75rem 1rem;
    margin-bottom: 1rem;
    border-radius: 0 6px 6px 0;
}

.markdown-body code {
    font-family: var(--font-mono);
    background-color: var(--code-bg);
    border: 1px solid var(--border);
    color: #f1f5f9;
    padding: 0.15rem 0.4rem;
    border-radius: 4px;
    font-size: 0.875em;
}

.markdown-body hr {
    border: none;
    border-top: 1px solid var(--border);
    margin: 2rem 0;
}

.markdown-body strong { color: #ffffff; font-weight: 600; }
.markdown-body a { color: var(--accent); text-decoration: none; }
.markdown-body a:hover { text-decoration: underline; }

/* Code Blocks & Tables Support */
.markdown-body pre {
    background-color: var(--code-bg);
    border: 1px solid var(--border);
    border-radius: 6px;
    padding: 1rem;
    overflow-x: auto;
    margin-bottom: 1.25rem;
}

.markdown-body pre code {
    background: none;
    border: none;
    padding: 0;
    font-size: 0.85rem;
    color: #e2e8f0;
}

.markdown-body table {
    width: 100%;
    border-collapse: collapse;
    font-size: 0.875rem;
    text-align: left;
    margin-bottom: 1.25rem;
    border: 1px solid var(--border);
    border-radius: 6px;
    overflow: hidden;
    display: block;
    overflow-x: auto;
    white-space: nowrap;
}

.markdown-body th {
    background-color: var(--bg-base);
    color: #ffffff;
    font-weight: 600;
    padding: 0.6rem 0.85rem;
    border-bottom: 1px solid var(--border);
}

.markdown-body td {
    padding: 0.6rem 0.85rem;
    border-bottom: 1px solid var(--border);
    color: #cbd5e1;
}

.markdown-body tr:last-child td {
    border-bottom: none;
}

.markdown-body tr:nth-child(even) {
    background-color: rgba(255, 255, 255, 0.02);
}

.error-banner {
    background-color: var(--alert-bg);
    border: 1px solid var(--alert-red);
    color: var(--alert-red);
    padding: 1rem;
    border-radius: 6px;
    font-size: 0.9rem;
}

/* Chat Toggle Button */
.chat-toggle-btn {
    position: fixed;
    bottom: 1.5rem;
    right: 1.5rem;
    width: 52px;
    height: 52px;
    border-radius: 50%;
    background-color: var(--accent);
    color: #0f172a;
    border: none;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.4);
    z-index: 100;
    transition: transform 0.2s ease, background-color 0.2s ease;
}

.chat-toggle-btn:hover {
    background-color: var(--accent-hover);
    color: #ffffff;
    transform: scale(1.05);
}

/* Chat Panel */
.chat-panel {
    position: fixed;
    top: 0;
    right: 0;
    width: 400px;
    height: 100vh;
    background-color: var(--bg-surface);
    border-left: 1px solid var(--border);
    display: flex;
    flex-direction: column;
    z-index: 200;
    transform: translateX(100%);
    transition: transform 0.3s ease;
}

.chat-panel.open {
    transform: translateX(0);
}

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

.chat-header h3 {
    font-size: 1rem;
    color: #ffffff;
    margin: 0;
}

.chat-close-btn {
    background: none;
    border: none;
    color: var(--text-muted);
    font-size: 1.5rem;
    cursor: pointer;
    padding: 0;
    line-height: 1;
}

.chat-close-btn:hover {
    color: #ffffff;
}

.chat-messages {
    flex: 1;
    overflow-y: auto;
    padding: 1rem;
    display: flex;
    flex-direction: column;
    gap: 0.75rem;
}

.chat-message {
    max-width: 85%;
    padding: 0.6rem 0.85rem;
    border-radius: 8px;
    font-size: 0.85rem;
    line-height: 1.5;
    word-wrap: break-word;
}

.chat-message.user {
    align-self: flex-end;
    background-color: var(--accent);
    color: #0f172a;
}

.chat-message.assistant {
    align-self: flex-start;
    background-color: var(--bg-card);
    border: 1px solid var(--border);
    color: #cbd5e1;
}

/* Typing indicator */
.typing-indicator {
    display: flex;
    gap: 4px;
    padding: 0.8rem 1rem;
}

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

.typing-indicator span:nth-child(1) { animation-delay: -0.32s; }
.typing-indicator span:nth-child(2) { animation-delay: -0.16s; }
.typing-indicator span:nth-child(3) { animation-delay: 0s; }

@keyframes typingBounce {
    0%, 80%, 100% { transform: scale(0.6); opacity: 0.4; }
    40% { transform: scale(1); opacity: 1; }
}

/* Markdown styling inside chat messages */
.chat-message.assistant p {
    margin-bottom: 0.5rem;
}

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

.chat-message.assistant code {
    font-family: var(--font-mono);
    background-color: rgba(0, 0, 0, 0.3);
    padding: 0.1rem 0.3rem;
    border-radius: 3px;
    font-size: 0.8rem;
}

.chat-message.assistant pre {
    background-color: rgba(0, 0, 0, 0.3);
    padding: 0.5rem;
    border-radius: 4px;
    overflow-x: auto;
    font-size: 0.8rem;
    margin: 0.5rem 0;
}

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

.chat-message.assistant ul, .chat-message.assistant ol {
    padding-left: 1.2rem;
    margin: 0.3rem 0;
}

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

.chat-input-area {
    display: flex;
    gap: 0.5rem;
    padding: 0.75rem 1rem;
    border-top: 1px solid var(--border);
}

.chat-input-area textarea {
    flex: 1;
    background-color: var(--bg-card);
    border: 1px solid var(--border);
    border-radius: 6px;
    color: var(--text-main);
    padding: 0.5rem;
    font-family: var(--font-main);
    font-size: 0.85rem;
    resize: none;
    outline: none;
}

.chat-input-area textarea:focus {
    border-color: var(--accent);
}

.chat-send-btn {
    background-color: var(--accent);
    color: #0f172a;
    border: none;
    border-radius: 6px;
    padding: 0.5rem 0.75rem;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: background-color 0.15s ease;
}

.chat-send-btn:hover {
    background-color: var(--accent-hover);
    color: #ffffff;
}

.chat-send-btn:disabled {
    opacity: 0.5;
    cursor: not-allowed;
}

/* Large screens: side panel open by default */
@media (min-width: 1024px) {
    .chat-panel.open {
        transform: translateX(0);
        width: 380px;
    }

    .chat-panel:not(.open) {
        transform: translateX(100%);
    }

    body.chat-open .layout-container {
        margin-right: 380px;
        max-width: calc(100% - 380px);
    }

    .chat-toggle-btn {
        display: none;
    }
}

/* Small screens: overlay */
@media (max-width: 1023px) {
    .chat-panel {
        width: 100%;
    }

    .chat-toggle-btn {
        display: flex;
    }

    .chat-close-btn {
        display: block;
    }
}

@media (max-width: 640px) {
    .brief-card {
        padding: 1.25rem;
    }
}

/* Mobile-responsive table handling */
@media (max-width: 768px) {
    .markdown-body table {
        display: block;
        overflow-x: auto;
        white-space: nowrap;
        min-width: 600px;
    }
    
    .markdown-body th,
    .markdown-body td {
        padding: 0.5rem 0.75rem;
        font-size: 0.8rem;
    }
}

/* Improved mobile navbar overlap fix using CSS viewport units and env() */
@media (max-width: 768px) {
    .chat-input-area {
        padding-bottom: calc(50px + env(safe-area-inset-bottom, 0px));
    }
    
    .chat-panel {
        padding-bottom: calc(50px + env(safe-area-inset-bottom, 0px));
    }
    
    .chat-messages {
        padding-bottom: calc(50px + env(safe-area-inset-bottom, 0px));
    }
}
