/* ─── Design tokens ─── */
:root {
    --primary: #1565c0;
    --primary-light: #1877f2;
    --primary-dark: #0d47a1;
    --bubble-user-bg: #1877f2;
    --bubble-user-text: #fff;
    --bubble-assistant-bg: #ffffff;
    --bubble-assistant-text: #1a1a1a;
    --chat-bg: #e9edf0;
    --surface: #ffffff;
    --header-bg: #1565c0;
    --header-text: #ffffff;
    --border: #dde1e7;
    --muted: #6b7280;
    --error-bg: #fef2f2;
    --error-text: #dc2626;
    --safe-top: env(safe-area-inset-top, 0px);
    --safe-bottom: env(safe-area-inset-bottom, 0px);
    --safe-left: env(safe-area-inset-left, 0px);
    --safe-right: env(safe-area-inset-right, 0px);
    --radius-bubble: 18px;
    --radius-bubble-tight: 5px;
    --shadow-bubble: 0 1px 2px rgba(0,0,0,.12);
}

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

html, body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif;
    height: 100%;
    margin: 0;
    background: var(--chat-bg);
    color: var(--bubble-assistant-text);
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
}

#app {
    height: 100%;
    display: flex;
    flex-direction: column;
}

/* ─── Chat layout ─── */
.chat-container {
    display: flex;
    flex-direction: column;
    height: 100vh;          /* fallback */
    height: 100dvh;         /* dynamic viewport height — better on mobile */
    max-width: 720px;
    margin: 0 auto;
    background: var(--chat-bg);
    position: relative;
}

/* ─── Header ─── */
.chat-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: .65rem 1rem;
    padding-top: calc(.65rem + var(--safe-top));
    background: var(--header-bg);
    color: var(--header-text);
    position: sticky;
    top: 0;
    z-index: 20;
    box-shadow: 0 1px 3px rgba(0,0,0,.25);
    gap: .5rem;
}

.header-center {
    flex: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
    min-width: 0;
}

.chat-title {
    font-size: 1rem;
    font-weight: 700;
    letter-spacing: .2px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.sync-badge {
    font-size: .68rem;
    opacity: .8;
    margin-top: 1px;
    white-space: nowrap;
}

.sync-badge.syncing {
    opacity: 1;
    animation: pulse-opacity 1.5s ease-in-out infinite;
}

@keyframes pulse-opacity {
    0%, 100% { opacity: .6; }
    50% { opacity: 1; }
}

/* Icon buttons in header */
.btn-icon {
    background: rgba(255,255,255,.15);
    border: none;
    color: var(--header-text);
    width: 38px;
    height: 38px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    flex-shrink: 0;
    transition: background .15s;
    padding: 0;
}
.btn-icon:hover { background: rgba(255,255,255,.28); }
.btn-icon:disabled { opacity: .45; cursor: default; }
.btn-icon svg { width: 20px; height: 20px; }

/* ─── Hamburger drop-down ─── */
.header-left {
    position: relative;
    display: flex;
    align-items: center;
}

.hamburger-menu {
    position: absolute;
    top: calc(100% + .5rem);
    left: 0;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 12px;
    box-shadow: 0 6px 20px rgba(0,0,0,.18);
    min-width: 175px;
    z-index: 100;
    overflow: hidden;
}

.menu-item {
    display: flex;
    align-items: center;
    gap: .6rem;
    width: 100%;
    background: none;
    border: none;
    padding: .75rem 1rem;
    text-align: left;
    font-size: .9rem;
    color: #1a1a1a;
    cursor: pointer;
    transition: background .1s;
}
.menu-item:hover:not(:disabled) { background: #f3f4f6; }
.menu-item:disabled { opacity: .5; cursor: default; }

/* ─── Messages area ─── */
.chat-messages {
    flex: 1;
    overflow-y: auto;
    padding: .75rem .75rem 0;
    display: flex;
    flex-direction: column;
    gap: 0;
    scroll-behavior: smooth;
    overscroll-behavior: contain;
    -webkit-overflow-scrolling: touch;
}

.chat-empty {
    flex: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    color: var(--muted);
    text-align: center;
    padding: 2rem 1.5rem;
    gap: .75rem;
}

.chat-empty .empty-icon {
    font-size: 3rem;
    line-height: 1;
}

.chat-empty p {
    margin: 0;
    font-size: .95rem;
    line-height: 1.5;
    max-width: 260px;
}

/* ─── Date separator ─── */
.date-separator {
    display: flex;
    align-items: center;
    gap: .75rem;
    padding: .75rem 0 .4rem;
    text-align: center;
}
.date-separator::before,
.date-separator::after {
    content: '';
    flex: 1;
    height: 1px;
    background: rgba(0,0,0,.12);
}
.date-separator span {
    font-size: .72rem;
    color: var(--muted);
    white-space: nowrap;
    background: var(--chat-bg);
    padding: 0 .25rem;
}

/* ─── Message rows ─── */
.message-row {
    display: flex;
    align-items: flex-end;
    margin-bottom: 2px;
    animation: msg-in .2s ease-out;
}

.message-row.row-user {
    flex-direction: row-reverse;
    margin-left: 20%;
}

.message-row.row-assistant {
    flex-direction: row;
    margin-right: 20%;
}

/* Extra spacing between messages from different senders */
.message-row.group-start {
    margin-top: .5rem;
}

@keyframes msg-in {
    from { opacity: 0; transform: translateY(6px); }
    to   { opacity: 1; transform: translateY(0); }
}

/* ─── Avatars ─── */
.message-avatar {
    width: 30px;
    height: 30px;
    border-radius: 50%;
    background: var(--primary);
    color: #fff;
    font-size: .85rem;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
    margin-right: .4rem;
    margin-bottom: 2px;
    box-shadow: 0 1px 3px rgba(0,0,0,.2);
}

.message-avatar-spacer {
    width: 30px;
    flex-shrink: 0;
    margin-right: .4rem;
}

/* ─── Bubble wrapper ─── */
.message-content {
    display: flex;
    flex-direction: column;
    max-width: 100%;
    min-width: 0;
}

/* ─── Bubbles ─── */
.chat-bubble {
    padding: .55rem .9rem;
    line-height: 1.48;
    word-break: break-word;
    border-radius: var(--radius-bubble);
    box-shadow: var(--shadow-bubble);
    position: relative;
}

/* User bubble */
.bubble-user {
    background: var(--bubble-user-bg);
    color: var(--bubble-user-text);
    align-self: flex-end;
}

/* Assistant bubble */
.bubble-assistant {
    background: var(--bubble-assistant-bg);
    color: var(--bubble-assistant-text);
    align-self: flex-start;
}

/* Grouped bubble radius tweaks */
.bubble-user.bubble-top    { border-bottom-right-radius: var(--radius-bubble-tight); }
.bubble-user.bubble-middle { border-top-right-radius: var(--radius-bubble-tight); border-bottom-right-radius: var(--radius-bubble-tight); }
.bubble-user.bubble-bottom { border-top-right-radius: var(--radius-bubble-tight); }

.bubble-assistant.bubble-top    { border-bottom-left-radius: var(--radius-bubble-tight); }
.bubble-assistant.bubble-middle { border-top-left-radius: var(--radius-bubble-tight); border-bottom-left-radius: var(--radius-bubble-tight); }
.bubble-assistant.bubble-bottom { border-top-left-radius: var(--radius-bubble-tight); }

.bubble-text { white-space: pre-wrap; }

/* ─── Bubble timestamp ─── */
.bubble-time {
    font-size: .67rem;
    color: var(--muted);
    margin-top: 3px;
    padding: 0 .2rem;
}
.time-right { text-align: right; }
.time-left  { text-align: left; }
.bubble-user + .bubble-time { color: rgba(255,255,255,.65); }

/* ─── Markdown content in assistant bubbles ─── */
.markdown-content { line-height: 1.5; }
.markdown-content p { margin: 0 0 .5em; }
.markdown-content p:last-child { margin-bottom: 0; }
.markdown-content h1, .markdown-content h2, .markdown-content h3,
.markdown-content h4, .markdown-content h5, .markdown-content h6 {
    margin: .75em 0 .35em;
    font-weight: 700;
    line-height: 1.25;
}
.markdown-content h1 { font-size: 1.2em; }
.markdown-content h2 { font-size: 1.08em; }
.markdown-content h3 { font-size: 1em; }
.markdown-content ul, .markdown-content ol { padding-left: 1.4em; margin: .35em 0; }
.markdown-content li { margin: .15em 0; }
.markdown-content code {
    background: rgba(0,0,0,.07);
    border-radius: 4px;
    padding: .1em .35em;
    font-size: .875em;
    font-family: 'Fira Code', 'Cascadia Code', 'Courier New', Courier, monospace;
}
.markdown-content pre {
    background: #1e2a3a;
    color: #e2e8f0;
    border-radius: 8px;
    padding: .75rem 1rem;
    overflow-x: auto;
    margin: .5em 0;
}
.markdown-content pre code { background: none; padding: 0; font-size: .85em; color: inherit; }
.markdown-content blockquote {
    border-left: 3px solid #93c5fd;
    margin: .5em 0;
    padding-left: .75em;
    color: #4b5563;
}
.markdown-content table {
    border-collapse: collapse;
    width: 100%;
    margin: .5em 0;
    font-size: .88em;
    display: block;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
}
.markdown-content th, .markdown-content td {
    border: 1px solid #e5e7eb;
    padding: .35em .65em;
    text-align: left;
}
.markdown-content th { background: #f9fafb; font-weight: 600; }
.markdown-content a { color: var(--primary-light); text-decoration: underline; }

/* ─── Typing indicator ─── */
.bubble-typing {
    display: flex;
    gap: 5px;
    align-items: center;
    padding: .3rem .2rem;
}
.bubble-typing span {
    width: 7px;
    height: 7px;
    border-radius: 50%;
    background: #9ca3af;
    animation: bounce .9s infinite;
}
.bubble-typing span:nth-child(2) { animation-delay: .15s; }
.bubble-typing span:nth-child(3) { animation-delay: .3s; }

@keyframes bounce {
    0%, 60%, 100% { transform: translateY(0); }
    30% { transform: translateY(-5px); }
}

/* ─── Load older bar ─── */
.load-older-bar {
    text-align: center;
    padding: .3rem 0 .5rem;
}
.btn-load-older {
    background: rgba(255,255,255,.85);
    border: 1px solid var(--border);
    border-radius: 999px;
    color: var(--muted);
    font-size: .78rem;
    padding: .3rem 1rem;
    cursor: pointer;
    transition: background .15s, box-shadow .15s;
    box-shadow: 0 1px 3px rgba(0,0,0,.08);
}
.btn-load-older:hover:not(:disabled) {
    background: #fff;
    box-shadow: 0 2px 6px rgba(0,0,0,.12);
}
.btn-load-older:disabled { opacity: .6; cursor: default; }

/* ─── Input area ─── */
.chat-input-area {
    background: var(--surface);
    border-top: 1px solid var(--border);
    padding: .6rem .75rem;
    padding-bottom: calc(.6rem + var(--safe-bottom));
    position: sticky;
    bottom: 0;
    z-index: 10;
}

.chat-input-row {
    display: flex;
    align-items: flex-end;
    gap: .5rem;
    background: #f3f4f6;
    border-radius: 24px;
    padding: .35rem .35rem .35rem .85rem;
    border: 1.5px solid transparent;
    transition: border-color .15s, box-shadow .15s;
}

.chat-input-row:focus-within {
    border-color: var(--primary-light);
    box-shadow: 0 0 0 3px rgba(24,119,242,.12);
    background: var(--surface);
}

.chat-input {
    flex: 1;
    border: none;
    background: transparent;
    padding: .3rem 0;
    font-size: .95rem;
    resize: none;
    max-height: 120px;
    min-height: 24px;
    overflow-y: auto;
    line-height: 1.45;
    outline: none;
    font-family: inherit;
    color: inherit;
}
.chat-input:disabled { opacity: .6; }
.chat-input::placeholder { color: #9ca3af; }

.btn-send {
    width: 36px;
    height: 36px;
    border-radius: 50%;
    background: var(--primary-light);
    color: #fff;
    border: none;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    flex-shrink: 0;
    transition: background .15s, transform .1s, opacity .15s;
    padding: 0;
}
.btn-send svg { width: 18px; height: 18px; }
.btn-send:hover:not(:disabled) { background: var(--primary); transform: scale(1.06); }
.btn-send:disabled { background: #d1d5db; cursor: default; opacity: .7; }

/* ─── Error banner ─── */
.chat-error {
    background: var(--error-bg);
    color: var(--error-text);
    font-size: .84rem;
    padding: .55rem 1rem;
    display: flex;
    justify-content: space-between;
    align-items: center;
    border-top: 1px solid #fecaca;
    padding-bottom: calc(.55rem + var(--safe-bottom));
}
.chat-error button {
    background: none;
    border: none;
    cursor: pointer;
    color: var(--error-text);
    font-size: 1rem;
    line-height: 1;
    padding: 0 .25rem;
}

/* ─── Blazor error UI ─── */
#blazor-error-ui {
    background: lightyellow;
    bottom: 0;
    box-shadow: 0 -1px 2px rgba(0,0,0,.2);
    display: none;
    left: 0;
    padding: .6rem 1.25rem .7rem;
    position: fixed;
    width: 100%;
    z-index: 1000;
}
#blazor-error-ui .dismiss { cursor: pointer; position: absolute; right: .75rem; top: .5rem; }

/* ─── Loading progress ─── */
.loading-progress {
    position: relative;
    display: block;
    width: 8rem;
    height: 8rem;
    margin: 20vh auto 1rem;
}
.loading-progress circle {
    fill: none;
    stroke: #e0e0e0;
    stroke-width: .6rem;
    transform-origin: 50% 50%;
    transform: rotate(-90deg);
}
.loading-progress circle:last-child {
    stroke: var(--primary);
    stroke-dasharray: calc(3.141 * var(--blazor-load-percentage, 0%) * 0.8), 500%;
    transition: stroke-dasharray .05s ease-in-out;
}
.loading-progress-text {
    position: absolute;
    text-align: center;
    font-weight: bold;
    inset: calc(20vh + 3.25rem) 0 auto;
    font-size: 1.8rem;
}
.loading-progress-text::after { content: var(--blazor-load-percentage-text, "Loading"); }

