/* ============================================================
   assets/style.css — Mobile-first, Open WebUI-inspired
   ============================================================ */

:root {
    --bg: #0f1117;
    --bg-2: #151823;
    --bg-3: #1b1f2e;
    --bg-4: #222739;
    --border: #232838;
    --text: #e6e9f2;
    --text-dim: #8b93a7;
    --accent: #7c5cff;
    --accent-2: #5b8cff;
    --radius: 12px;
    --sidebar-w: 300px;
    --topbar-h: 56px;
    --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);
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

html, body {
    margin: 0; padding: 0; height: 100%;
    background: var(--bg);
    color: var(--text);
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, "Noto Sans", sans-serif;
    font-size: 15px;
    line-height: 1.5;
    overflow: hidden;
    overscroll-behavior: none;
}

/* Tránh iOS zoom khi focus input */
@supports (-webkit-touch-callout: none) {
    input, textarea, select { font-size: 16px !important; }
}

.app { display: flex; height: 100dvh; height: 100vh; }

/* ==================== SIDEBAR ==================== */
.sidebar {
    width: var(--sidebar-w);
    background: var(--bg-2);
    border-right: 1px solid var(--border);
    display: flex; flex-direction: column;
    flex-shrink: 0;
    transition: transform .25s cubic-bezier(.4,0,.2,1);
    z-index: 50;
}

.sidebar-header {
    padding: calc(14px + var(--safe-top)) 14px 14px;
    border-bottom: 1px solid var(--border);
    display: flex; gap: 8px; align-items: center;
}

.btn-new {
    flex: 1;
    display: flex; align-items: center; justify-content: center; gap: 8px;
    padding: 12px 14px;
    background: linear-gradient(135deg, var(--accent), var(--accent-2));
    color: white; border: none; border-radius: 10px;
    font-size: 14px; font-weight: 600;
    cursor: pointer;
    transition: filter .15s;
    min-height: 44px;
}
.btn-new:hover { filter: brightness(1.1); }
.btn-new:active { transform: scale(.98); }

.btn-close-sidebar {
    display: none;
    background: transparent; border: none;
    color: var(--text-dim); cursor: pointer;
    width: 40px; height: 40px;
    border-radius: 8px;
    align-items: center; justify-content: center;
}
.btn-close-sidebar:hover { background: var(--bg-3); color: var(--text); }

.sidebar-list { flex: 1; overflow-y: auto; padding: 8px; -webkit-overflow-scrolling: touch; }

.conv-item {
    display: flex; align-items: center; gap: 10px;
    padding: 12px 14px; margin-bottom: 4px;
    border-radius: 10px;
    cursor: pointer;
    font-size: 14px;
    color: var(--text-dim);
    transition: background .12s, color .12s;
    position: relative;
    min-height: 44px;
}
.conv-item:hover { background: var(--bg-3); color: var(--text); }
.conv-item.active { background: var(--bg-3); color: var(--text); }
.conv-item .name {
    flex: 1;
    white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.conv-item .del {
    opacity: 0; flex-shrink: 0;
    background: transparent; border: none; cursor: pointer;
    color: var(--text-dim);
    padding: 6px; border-radius: 6px;
    transition: opacity .12s, color .12s, background .12s;
}
.conv-item:hover .del { opacity: 1; }
.conv-item .del:hover { color: #ff6b6b; background: rgba(255,107,107,.1); }
.conv-item .del:active { background: rgba(255,107,107,.2); }

.sidebar-footer {
    padding: 12px 16px calc(12px + var(--safe-bottom));
    border-top: 1px solid var(--border);
    font-size: 12px; color: var(--text-dim);
}
.badge {
    display: inline-block; padding: 4px 10px;
    background: var(--bg-3); border-radius: 6px;
    font-family: ui-monospace, monospace;
    max-width: 100%;
    white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}

/* Overlay khi mở sidebar mobile */
.sidebar-overlay {
    display: none;
    position: fixed; inset: 0;
    background: rgba(0,0,0,.55);
    backdrop-filter: blur(2px);
    -webkit-backdrop-filter: blur(2px);
    z-index: 40;
    opacity: 0;
    transition: opacity .25s;
}
.sidebar-overlay.show { opacity: 1; }

/* ==================== MAIN ==================== */
.main {
    flex: 1; display: flex; flex-direction: column; min-width: 0;
    min-height: 0;
}

.topbar {
    display: flex; align-items: center; gap: 8px;
    padding: calc(8px + var(--safe-top)) 12px 8px;
    min-height: var(--topbar-h);
    border-bottom: 1px solid var(--border);
    background: var(--bg);
    flex-shrink: 0;
}
.topbar .title {
    flex: 1;
    font-weight: 600; font-size: 15px;
    white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
    text-align: center;
    padding: 0 4px;
}
.icon-btn {
    background: transparent; border: none;
    color: var(--text); cursor: pointer;
    width: 40px; height: 40px; flex-shrink: 0;
    border-radius: 10px;
    display: flex; align-items: center; justify-content: center;
    transition: background .12s;
}
.icon-btn:hover { background: var(--bg-3); }
.icon-btn:active { background: var(--bg-4); }

/* ==================== CHAT AREA ==================== */
.chat {
    flex: 1;
    overflow-y: auto;
    overflow-x: hidden;
    padding: 16px 0 8px;
    -webkit-overflow-scrolling: touch;
    overscroll-behavior: contain;
}

.welcome {
    text-align: center;
    padding: 40px 20px 24px;
    color: var(--text-dim);
    max-width: 720px;
    margin: 0 auto;
}
.welcome-logo {
    font-size: 56px;
    background: linear-gradient(135deg, var(--accent), var(--accent-2));
    -webkit-background-clip: text; background-clip: text;
    -webkit-text-fill-color: transparent;
    margin-bottom: 8px;
    line-height: 1;
}
.welcome h1 { color: var(--text); font-size: 26px; margin: 0 0 8px; font-weight: 700; }
.welcome p { margin: 0 0 24px; font-size: 14px; }

/* Suggestion chips */
.suggestions {
    display: grid;
    grid-template-columns: repeat(2, 1fr);
    gap: 10px;
    margin-top: 8px;
}
.suggestion {
    display: flex; flex-direction: column; align-items: flex-start; gap: 6px;
    padding: 14px;
    background: var(--bg-2);
    border: 1px solid var(--border);
    border-radius: 12px;
    color: var(--text);
    font-size: 13px;
    text-align: left;
    cursor: pointer;
    transition: background .15s, border-color .15s, transform .1s;
    min-height: 72px;
}
.suggestion:hover { background: var(--bg-3); border-color: #333a52; }
.suggestion:active { transform: scale(.98); }
.suggestion-ico { font-size: 18px; }
.suggestion span:last-child { color: var(--text-dim); line-height: 1.35; }

/* ==================== MESSAGES ==================== */
.msg { padding: 4px 0; }
.msg-inner {
    max-width: 820px;
    margin: 0 auto;
    padding: 0 16px;
    display: flex; gap: 12px;
    align-items: flex-start;
}
.avatar {
    width: 32px; height: 32px; border-radius: 8px;
    flex-shrink: 0;
    display: flex; align-items: center; justify-content: center;
    font-weight: 700; font-size: 13px;
}
.msg.user .avatar { background: var(--accent); color: #fff; }
.msg.assistant .avatar {
    background: #2a3245;
    color: var(--accent-2);
    font-size: 16px;
}

.bubble {
    flex: 1;
    padding: 2px 0;
    line-height: 1.65;
    overflow-wrap: break-word;
    word-wrap: break-word;
    min-width: 0;
    font-size: 15px;
}
.msg.user .bubble { white-space: pre-wrap; }

/* Markdown */
.bubble > *:first-child { margin-top: 0; }
.bubble > *:last-child { margin-bottom: 0; }
.bubble p { margin: 0 0 12px; }
.bubble h1, .bubble h2, .bubble h3 { margin: 20px 0 10px; line-height: 1.3; }
.bubble h1 { font-size: 22px; }
.bubble h2 { font-size: 19px; }
.bubble h3 { font-size: 17px; }
.bubble ul, .bubble ol { margin: 0 0 12px; padding-left: 22px; }
.bubble li { margin: 5px 0; }
.bubble a { color: var(--accent-2); text-decoration: none; }
.bubble a:hover { text-decoration: underline; }
.bubble strong { color: #fff; }
.bubble code {
    background: #0b0e16;
    padding: 2px 6px;
    border-radius: 5px;
    font-family: ui-monospace, "SF Mono", Consolas, Monaco, monospace;
    font-size: 13px;
}
.bubble pre {
    background: #0b0e16;
    padding: 14px 16px;
    border-radius: 10px;
    overflow-x: auto;
    margin: 12px 0;
    border: 1px solid var(--border);
    -webkit-overflow-scrolling: touch;
    font-size: 13px;
}
.bubble pre code { background: transparent; padding: 0; font-size: 13px; }
.bubble blockquote {
    border-left: 3px solid var(--border);
    padding: 2px 0 2px 14px;
    margin: 12px 0; color: var(--text-dim);
}
.bubble table {
    border-collapse: collapse; margin: 12px 0;
    display: block; overflow-x: auto;
    max-width: 100%;
}
.bubble th, .bubble td {
    border: 1px solid var(--border);
    padding: 8px 12px;
    white-space: nowrap;
}
.bubble img { max-width: 100%; height: auto; border-radius: 8px; }
.bubble hr { border: none; border-top: 1px solid var(--border); margin: 16px 0; }

/* Typing indicator */
.dots { display: inline-flex; gap: 4px; padding: 4px 0; }
.dots span {
    width: 7px; height: 7px; border-radius: 50%;
    background: var(--text-dim);
    animation: blink 1.4s infinite both;
}
.dots span:nth-child(2) { animation-delay: .2s; }
.dots span:nth-child(3) { animation-delay: .4s; }
@keyframes blink { 0%,80%,100%{opacity:.25;} 40%{opacity:1;} }

/* ==================== COMPOSER ==================== */
.composer {
    padding: 10px 16px calc(12px + var(--safe-bottom));
    border-top: 1px solid var(--border);
    background: var(--bg);
    flex-shrink: 0;
}
.composer form {
    max-width: 820px; margin: 0 auto;
    display: flex; align-items: flex-end; gap: 8px;
    background: var(--bg-2);
    border: 1px solid var(--border);
    border-radius: 16px;
    padding: 6px 6px 6px 14px;
    transition: border-color .15s, box-shadow .15s;
}
.composer form:focus-within {
    border-color: var(--accent);
    box-shadow: 0 0 0 3px rgba(124,92,255,.12);
}

.composer textarea {
    flex: 1;
    background: transparent;
    border: none; outline: none; resize: none;
    color: var(--text);
    font-family: inherit; font-size: 15px;
    line-height: 1.5;
    padding: 10px 0;
    max-height: 160px;
    min-height: 22px;
    -webkit-appearance: none;
}
.composer textarea::placeholder { color: var(--text-dim); }

.composer button[type="submit"] {
    background: linear-gradient(135deg, var(--accent), var(--accent-2));
    border: none; color: #fff;
    width: 40px; height: 40px;
    border-radius: 12px;
    display: flex; align-items: center; justify-content: center;
    cursor: pointer; flex-shrink: 0;
    transition: filter .15s, transform .1s, opacity .15s;
}
.composer button[type="submit"]:hover:not(:disabled) { filter: brightness(1.1); }
.composer button[type="submit"]:active:not(:disabled) { transform: scale(.94); }
.composer button[type="submit"]:disabled { opacity: .4; cursor: not-allowed; }

.composer-hint {
    max-width: 820px; margin: 8px auto 0;
    text-align: center; font-size: 11px; color: var(--text-dim);
}

/* Scrollbar */
::-webkit-scrollbar { width: 6px; height: 6px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: #2a3144; border-radius: 3px; }
::-webkit-scrollbar-thumb:hover { background: #3a4358; }

/* ============================================================
   MOBILE (≤ 768px)
   ============================================================ */
@media (max-width: 768px) {
    .sidebar {
        position: fixed;
        top: 0; left: 0; bottom: 0;
        width: min(88vw, 320px);
        transform: translateX(-100%);
        box-shadow: 8px 0 32px rgba(0,0,0,.5);
        z-index: 60;
    }
    .sidebar.open { transform: translateX(0); }
    .btn-close-sidebar { display: flex; }

    .sidebar-overlay { display: block; pointer-events: none; }
    .sidebar-overlay.show { pointer-events: auto; }

    .msg-inner { padding: 0 12px; gap: 10px; }
    .avatar { width: 28px; height: 28px; font-size: 12px; border-radius: 7px; }
    .bubble { font-size: 15px; }

    .welcome { padding: 32px 16px 20px; }
    .welcome-logo { font-size: 48px; }
    .welcome h1 { font-size: 22px; }
    .suggestions { grid-template-columns: 1fr; }

    .composer { padding: 8px 12px calc(10px + var(--safe-bottom)); }
    .composer form { padding: 4px 4px 4px 12px; border-radius: 14px; }
    .composer textarea { padding: 9px 0; max-height: 120px; }
    .composer button[type="submit"] { width: 38px; height: 38px; border-radius: 11px; }
    .composer-hint { display: none; }

    .chat { padding: 12px 0 8px; }
}

/* Rất nhỏ (≤ 380px) */
@media (max-width: 380px) {
    .topbar { padding-left: 8px; padding-right: 8px; }
    .topbar .title { font-size: 14px; }
    .icon-btn { width: 36px; height: 36px; }
    .msg-inner { padding: 0 10px; gap: 8px; }
    .avatar { width: 26px; height: 26px; font-size: 11px; }
    .bubble { font-size: 14px; }
}

/* Tablet & desktop */
@media (min-width: 769px) {
    .sidebar-overlay { display: none !important; }
}

/* Landscape phone — thu gọn padding */
@media (max-height: 500px) and (orientation: landscape) {
    .welcome { padding: 16px; }
    .welcome-logo { font-size: 36px; }
    .welcome h1 { font-size: 20px; }
    .suggestions { grid-template-columns: repeat(2, 1fr); }
}