/* ============================================================
   Chat Kittisak — Design tokens
   Palette: deep navy ground, one confident royal-blue accent,
   a warm signal color reserved only for the image-generation
   surface (kept out of ordinary chat chrome).
   Type: Noto Sans Thai for Thai text, Inter for Latin/numerals —
   distinct enough to read as a pairing, not a fallback.
   Layout: left-aligned sidebar + chat column, no card-soup;
   messages are typographic, not bubble-chrome on both sides.
   ============================================================ */

:root {
    --navy-950: #0b1424;
    --navy-900: #10192c;
    --navy-800: #182338;
    --navy-700: #263248;
    --line: #2a3650;
    --ink: #eef1f8;
    --ink-dim: #a9b4c9;
    --ink-faint: #6b7590;
    --blue: #3d63ff;
    --blue-dim: #2947b8;
    --paper: #f6f7fb;
    --paper-line: #dfe3ee;
    --ink-on-paper: #131a2b;
    --ink-on-paper-dim: #5b6478;
    --spark: #ffb454; /* reserved for image-generation only */
    --danger: #ff6b6b;
    --radius: 10px;
    --font-latin: 'Inter', system-ui, sans-serif;
    --font-thai: 'Noto Sans Thai', var(--font-latin);
}

* { box-sizing: border-box; }

html, body {
    margin: 0;
    padding: 0;
    background: var(--navy-950);
    color: var(--ink);
    font-family: var(--font-thai), var(--font-latin);
    font-size: 15px;
    line-height: 1.6;
    -webkit-font-smoothing: antialiased;
    height: 100%;
}

html {
    scroll-padding-top: env(safe-area-inset-top, 0px);
}

img { max-width: 100%; }

:focus-visible {
    outline: 2px solid var(--blue);
    outline-offset: 2px;
}

a { color: inherit; }

button {
    font-family: inherit;
    cursor: pointer;
}

/* ---------- Landing page ---------- */

.landing {
    max-width: 960px;
    margin: 0 auto;
    padding: 0 24px;
}

.landing-nav {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: calc(28px + env(safe-area-inset-top, 0px)) 0 28px;
}

.wordmark {
    font-weight: 700;
    font-size: 18px;
    letter-spacing: -0.01em;
}

.wordmark span { color: var(--blue); }

.nav-actions a {
    text-decoration: none;
    margin-left: 20px;
    color: var(--ink-dim);
    font-size: 14px;
}

.nav-actions a.primary {
    background: var(--blue);
    color: #fff;
    padding: 9px 18px;
    border-radius: var(--radius);
}

.hero {
    padding: 96px 0 64px;
}

.hero h1 {
    font-family: var(--font-thai), var(--font-latin);
    font-size: clamp(36px, 6vw, 58px);
    line-height: 1.15;
    font-weight: 700;
    letter-spacing: -0.02em;
    margin: 0 0 20px;
    max-width: 14ch;
}

.hero p {
    font-size: 18px;
    color: var(--ink-dim);
    max-width: 48ch;
    margin: 0 0 36px;
}

.hero-actions { display: flex; gap: 12px; flex-wrap: wrap; }

.btn {
    display: inline-block;
    padding: 13px 22px;
    border-radius: var(--radius);
    border: 1px solid var(--line);
    text-decoration: none;
    font-size: 15px;
    font-weight: 500;
    color: var(--ink);
    background: transparent;
}

.btn-primary {
    background: var(--blue);
    border-color: var(--blue);
    color: #fff;
}

.features {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
    gap: 1px;
    background: var(--line);
    border: 1px solid var(--line);
    border-radius: var(--radius);
    overflow: hidden;
    margin: 40px 0 96px;
}

.feature {
    background: var(--navy-900);
    padding: 26px 24px;
}

.feature .icon { font-size: 22px; display: block; margin-bottom: 12px; }
.feature h3 { font-size: 15px; margin: 0 0 6px; }
.feature p { font-size: 13.5px; color: var(--ink-faint); margin: 0; }

/* ---------- Auth pages ---------- */

.auth-shell {
    min-height: 100dvh;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: calc(24px + env(safe-area-inset-top, 0px)) 24px calc(24px + env(safe-area-inset-bottom, 0px));
}

.auth-card {
    width: 100%;
    max-width: 380px;
}

.auth-card h1 {
    font-size: 22px;
    margin: 0 0 6px;
}

.auth-card .sub {
    color: var(--ink-faint);
    font-size: 14px;
    margin: 0 0 28px;
}

.field { margin-bottom: 16px; }

.field label {
    display: block;
    font-size: 13px;
    color: var(--ink-dim);
    margin-bottom: 6px;
}

.field input {
    width: 100%;
    padding: 11px 13px;
    border-radius: var(--radius);
    border: 1px solid var(--line);
    background: var(--navy-800);
    color: var(--ink);
    font-size: 14px;
    font-family: inherit;
}

.field input:focus {
    outline: 2px solid var(--blue);
    outline-offset: 1px;
}

.form-error {
    background: rgba(255, 107, 107, 0.12);
    border: 1px solid rgba(255, 107, 107, 0.3);
    color: var(--danger);
    padding: 10px 13px;
    border-radius: var(--radius);
    font-size: 13.5px;
    margin-bottom: 16px;
    display: none;
}

.form-error.show { display: block; }

.auth-submit {
    width: 100%;
    padding: 12px;
    border: none;
    border-radius: var(--radius);
    background: var(--blue);
    color: #fff;
    font-size: 15px;
    font-weight: 600;
}

.auth-submit:disabled { opacity: 0.6; cursor: default; }

.auth-switch {
    text-align: center;
    margin-top: 18px;
    font-size: 13.5px;
    color: var(--ink-faint);
}

.auth-switch a { color: var(--blue); text-decoration: none; }

/* ---------- Chat application ---------- */

.app {
    display: flex;
    height: 100dvh;
    overflow: hidden;
}

.sidebar {
    width: 264px;
    flex-shrink: 0;
    background: var(--navy-900);
    border-right: 1px solid var(--line);
    display: flex;
    flex-direction: column;
    padding: calc(16px + env(safe-area-inset-top, 0px)) 12px 16px;
    transition: transform 0.22s ease;
}

.sidebar-overlay {
    display: none;
}

.mobile-topbar {
    display: none;
}

.sidebar-logo {
    font-weight: 700;
    font-size: 16px;
    padding: 4px 8px 18px;
}

.sidebar-logo span { color: var(--blue); }

.new-chat-btn {
    display: flex;
    align-items: center;
    gap: 8px;
    width: 100%;
    padding: 10px 12px;
    border-radius: var(--radius);
    border: 1px solid var(--line);
    background: var(--navy-800);
    color: var(--ink);
    font-size: 14px;
    margin-bottom: 14px;
}

.new-chat-btn:hover { border-color: var(--blue); }

.sidebar-search {
    width: 100%;
    padding: 8px 11px;
    border-radius: var(--radius);
    border: 1px solid var(--line);
    background: transparent;
    color: var(--ink);
    font-size: 13px;
    margin-bottom: 14px;
}

.conv-list {
    flex: 1;
    overflow-y: auto;
    display: flex;
    flex-direction: column;
    gap: 2px;
}

.conv-item {
    padding: 9px 10px;
    border-radius: 8px;
    font-size: 13.5px;
    color: var(--ink-dim);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    cursor: pointer;
    transition: background 0.12s ease;
}

.conv-item:hover { background: var(--navy-800); }
.conv-item.active { background: var(--navy-800); color: var(--ink); }

.conv-empty {
    color: var(--ink-faint);
    font-size: 12.5px;
    padding: 12px 10px;
    text-align: center;
}

.sidebar-footer {
    border-top: 1px solid var(--line);
    padding-top: 12px;
    margin-top: 8px;
    display: flex;
    align-items: center;
    gap: 10px;
    font-size: 13px;
}

.avatar-dot {
    width: 28px;
    height: 28px;
    border-radius: 50%;
    background: var(--blue-dim);
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 12px;
    font-weight: 700;
    flex-shrink: 0;
}

.sidebar-footer .who { flex: 1; overflow: hidden; }
.sidebar-footer .name { color: var(--ink); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.sidebar-footer .plan { color: var(--ink-faint); font-size: 11.5px; }
.sidebar-footer a { color: var(--ink-faint); font-size: 12px; text-decoration: none; }

.main {
    flex: 1;
    display: flex;
    flex-direction: column;
    background: var(--paper);
    color: var(--ink-on-paper);
    min-width: 0;
}

.chat-scroll {
    flex: 1;
    overflow-y: auto;
    padding: 32px 0 8px;
}

.chat-inner {
    max-width: 720px;
    margin: 0 auto;
    padding: 0 24px;
}

.empty-state {
    max-width: 640px;
    margin: 10vh auto 0;
    padding: 0 24px;
    text-align: left;
}

.empty-state h1 {
    font-size: 26px;
    margin: 0 0 6px;
}

.empty-state .lead {
    color: var(--ink-on-paper-dim);
    margin: 0 0 28px;
    font-size: 15px;
}

.suggestions {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 10px;
}

.suggestion {
    text-align: left;
    padding: 13px 15px;
    border: 1px solid var(--paper-line);
    border-radius: var(--radius);
    background: #fff;
    font-size: 13.5px;
    color: var(--ink-on-paper);
}

.suggestion:hover { border-color: var(--blue); }

.msg {
    margin-bottom: 28px;
    display: flex;
    gap: 12px;
}

.msg.user {
    justify-content: flex-end;
}

.msg.user .bubble {
    background: var(--blue);
    color: #fff;
    padding: 10px 15px;
    border-radius: 14px 14px 3px 14px;
    max-width: 80%;
}

.msg.assistant .bubble {
    max-width: 100%;
    padding-top: 2px;
}

.msg.assistant .mark {
    width: 26px;
    height: 26px;
    border-radius: 7px;
    background: var(--navy-950);
    color: #fff;
    font-size: 11px;
    font-weight: 700;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
    margin-top: 2px;
}

.bubble p:first-child { margin-top: 0; }
.bubble p:last-child { margin-bottom: 0; }
.bubble pre {
    background: var(--navy-950);
    color: #d9e0f0;
    padding: 14px;
    border-radius: 8px;
    overflow-x: auto;
    font-size: 13px;
}
.bubble code {
    background: rgba(0,0,0,0.06);
    padding: 1px 5px;
    border-radius: 4px;
    font-size: 0.9em;
}
.bubble pre code { background: none; padding: 0; }

.msg-actions {
    display: flex;
    gap: 6px;
    margin-top: 8px;
    opacity: 0.5;
}

.msg-actions button {
    border: none;
    background: none;
    font-size: 13px;
    padding: 6px 8px;
    border-radius: 6px;
    color: var(--ink-on-paper-dim);
}

.msg-actions button:hover { background: var(--paper-line); opacity: 1; }

/* Touch devices have no hover — keep actions legible instead of hidden at 0.5 opacity. */
@media (hover: none) {
    .msg-actions { opacity: 0.85; }
}

.thinking {
    color: var(--ink-on-paper-dim);
    font-size: 13.5px;
    font-style: italic;
}

.composer-wrap {
    border-top: 1px solid var(--paper-line);
    padding: 16px 24px calc(20px + env(safe-area-inset-bottom, 0px));
    background: var(--paper);
}

.composer {
    max-width: 720px;
    margin: 0 auto;
    display: flex;
    align-items: flex-end;
    gap: 8px;
    background: #fff;
    border: 1px solid var(--paper-line);
    border-radius: 16px;
    padding: 8px 8px 8px 14px;
    box-shadow: 0 1px 2px rgba(19, 26, 43, 0.04), 0 8px 24px rgba(19, 26, 43, 0.06);
    transition: border-color 0.15s ease;
}

.composer:focus-within { border-color: var(--blue); }

.composer textarea {
    flex: 1;
    border: none;
    resize: none;
    font-family: inherit;
    font-size: 14.5px;
    max-height: 160px;
    padding: 8px 0;
    outline: none;
    color: var(--ink-on-paper);
}

.composer-icon-btn {
    border: none;
    background: var(--paper);
    width: 38px;
    height: 38px;
    border-radius: 10px;
    font-size: 16px;
    flex-shrink: 0;
}

.composer-send {
    border: none;
    background: var(--blue);
    color: #fff;
    width: 38px;
    height: 38px;
    border-radius: 10px;
    font-size: 15px;
    flex-shrink: 0;
}

.composer-send:disabled { opacity: 0.4; }

.composer-hint {
    max-width: 720px;
    margin: 8px auto 0;
    font-size: 11.5px;
    color: var(--ink-faint);
    text-align: center;
}

.error-banner {
    max-width: 720px;
    margin: 0 auto 16px;
    background: rgba(255,107,107,0.1);
    border: 1px solid rgba(255,107,107,0.3);
    color: #c94444;
    padding: 10px 14px;
    border-radius: var(--radius);
    font-size: 13.5px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
}

.error-banner button {
    border: 1px solid rgba(255,107,107,0.4);
    background: none;
    color: #c94444;
    padding: 4px 10px;
    border-radius: 6px;
    font-size: 12.5px;
    flex-shrink: 0;
}

/* ============================================================
   Responsive layers
   Desktop  : fixed 264px sidebar, sidebar + chat side by side
   Tablet   : sidebar narrows and collapses to icon rail by default,
              toggled open with the same drawer mechanism as mobile
   Mobile   : sidebar is an off-canvas drawer behind a topbar
   ============================================================ */

@media (max-width: 1024px) {
    .sidebar {
        position: fixed;
        top: 0;
        bottom: 0;
        left: 0;
        width: min(300px, 84vw);
        z-index: 30;
        transform: translateX(-100%);
        box-shadow: 12px 0 32px rgba(0, 0, 0, 0.28);
    }
    .sidebar.open { transform: translateX(0); }

    .sidebar-overlay {
        display: block;
        position: fixed;
        inset: 0;
        background: rgba(6, 10, 20, 0.5);
        z-index: 25;
        opacity: 0;
        pointer-events: none;
        transition: opacity 0.2s ease;
    }
    .sidebar-overlay.open { opacity: 1; pointer-events: auto; }

    .mobile-topbar {
        display: flex;
        align-items: center;
        gap: 12px;
        padding: calc(12px + env(safe-area-inset-top, 0px)) 16px 12px;
        background: var(--paper);
        border-bottom: 1px solid var(--paper-line);
        flex-shrink: 0;
    }
    .mobile-topbar .menu-btn {
        border: 1px solid var(--paper-line);
        background: #fff;
        width: 36px;
        height: 36px;
        border-radius: 9px;
        font-size: 16px;
        color: var(--ink-on-paper);
        flex-shrink: 0;
    }
    .mobile-topbar .title {
        font-weight: 700;
        font-size: 14.5px;
    }
    .mobile-topbar .title span { color: var(--blue); }

    .main { min-width: 0; }
    .chat-inner, .composer, .composer-hint, .error-banner { max-width: 100%; }
    .empty-state { margin-top: 6vh; }
}

@media (max-width: 760px) {
    .suggestions { grid-template-columns: 1fr; }
    .hero-actions { flex-direction: column; align-items: stretch; }
    .hero-actions .btn { text-align: center; width: 100%; }
    .landing-nav { flex-wrap: wrap; gap: 10px; row-gap: 12px; }
    .msg.user .bubble { max-width: 88%; }
    .composer-wrap { padding-left: 16px; padding-right: 16px; }
}

@media (min-width: 761px) and (max-width: 1024px) {
    .sidebar { width: 240px; }
}

/* Respect reduced-motion preferences. */
@media (prefers-reduced-motion: reduce) {
    .sidebar, .sidebar-overlay, .conv-item, .composer { transition: none; }
}
