/* ==========================================================================
   Justus Assistant — chat UI design system
   Visual identity: Justus Restaurant Group (dark zinc + gold/amber)
   Contrast (WCAG 2.1 AA, verified):
     #ffffff on #18181b ≈ 16.9:1   #9ca3af on #18181b ≈ 7.0:1
     #121215 on #fbbf24 ≈ 11.9:1   #fbbf24 on #121215 ≈ 10.9:1
   ========================================================================== */

:root {
    /* Brand tokens */
    --jus-bg: #121215;
    --jus-surface: #18181b;
    --jus-surface-2: #27272a;
    --jus-border: #3f3f46;
    --jus-gold: #fbbf24;
    --jus-gold-strong: #f59e0b;
    --jus-gold-deep: #d97706;
    --jus-gold-soft: #fde68a;
    --jus-text: #ffffff;
    --jus-text-muted: #9ca3af;
    --jus-text-on-gold: #121215;
    --jus-success: #34d399;
    --jus-danger: #f43f5e;

    /* Typography */
    --font-heading: 'Montserrat', 'Figtree', ui-sans-serif, system-ui, -apple-system, sans-serif;
    --font-body: 'Figtree', ui-sans-serif, system-ui, -apple-system, 'Segoe UI', sans-serif;

    /* Shape & depth */
    --radius-bubble: 18px;
    --radius-panel: 16px;
    --radius-btn: 12px;
    --shadow-soft: 0 4px 16px rgba(0, 0, 0, 0.35);
    --shadow-gold: 0 2px 12px rgba(251, 191, 36, 0.25);
    --focus-ring: 0 0 0 3px rgba(251, 191, 36, 0.35);

    /* Layout */
    --sidebar-width: 300px;
    --sidebar-rail: 72px;
    --chat-max-width: 860px;
}

/* --------------------------------------------------------------------------
   Base
   -------------------------------------------------------------------------- */

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

html {
    height: 100%;
    /* Prevent iOS Safari from auto-boosting text size on orientation change,
       which desyncs our layout from the rendered font sizes. */
    -webkit-text-size-adjust: 100%;
    text-size-adjust: 100%;
}

body {
    font-family: var(--font-body);
    font-size: 0.9375rem;
    line-height: 1.6;
    color: var(--jus-text);
    background: var(--jus-bg);
    height: 100vh;
    height: 100dvh;
    overflow: hidden;
}

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

/* The display rules on .message/.btn-stop etc. would otherwise beat the UA's
   [hidden] { display: none } — JS relies on the hidden attribute throughout. */
[hidden] {
    display: none !important;
}

::selection {
    background: var(--jus-gold);
    color: var(--jus-text-on-gold);
}

.skip-link {
    position: absolute;
    left: 8px;
    top: -48px;
    z-index: 100;
    padding: 0.5rem 1rem;
    background: var(--jus-gold);
    color: var(--jus-text-on-gold);
    border-radius: var(--radius-btn);
    font-weight: 600;
    text-decoration: none;
    transition: top 0.15s ease;
}

.skip-link:focus {
    top: 8px;
}

/* Thin, dark scrollbars */
.chat-scroll,
.history {
    scrollbar-width: thin;
    scrollbar-color: var(--jus-border) transparent;
}

.chat-scroll::-webkit-scrollbar,
.history::-webkit-scrollbar {
    width: 8px;
}

.chat-scroll::-webkit-scrollbar-thumb,
.history::-webkit-scrollbar-thumb {
    background: var(--jus-border);
    border-radius: 4px;
}

/* --------------------------------------------------------------------------
   Shared components
   -------------------------------------------------------------------------- */

.icon-btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 40px;
    height: 40px;
    border: none;
    border-radius: var(--radius-btn);
    background: transparent;
    color: var(--jus-text-muted);
    cursor: pointer;
    transition: background 0.15s ease, color 0.15s ease;
}

.icon-btn:hover {
    background: var(--jus-surface-2);
    color: var(--jus-text);
}

.icon-btn svg {
    width: 20px;
    height: 20px;
    fill: currentColor;
}

.btn-primary {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 0.5rem;
    padding: 0.625rem 1rem;
    border: 1px solid var(--jus-gold);
    border-radius: var(--radius-btn);
    background: transparent;
    color: var(--jus-gold);
    font-family: var(--font-body);
    font-size: 0.9375rem;
    font-weight: 600;
    text-decoration: none;
    cursor: pointer;
    transition: background 0.15s ease, color 0.15s ease, box-shadow 0.15s ease;
}

.btn-primary:hover {
    background: linear-gradient(135deg, var(--jus-gold), var(--jus-gold-deep));
    color: var(--jus-text-on-gold);
    box-shadow: var(--shadow-gold);
}

.btn-primary:active {
    background: var(--jus-gold-deep);
    color: var(--jus-text-on-gold);
}

.btn-primary svg {
    width: 16px;
    height: 16px;
    fill: currentColor;
    flex-shrink: 0;
}

.chip {
    display: inline-flex;
    align-items: center;
    padding: 0.625rem 1rem;
    border: 1px solid var(--jus-border);
    border-radius: 999px;
    background: var(--jus-surface-2);
    color: var(--jus-text);
    font-family: var(--font-body);
    font-size: 0.875rem;
    text-align: left;
    cursor: pointer;
    transition: border-color 0.15s ease, background 0.15s ease, transform 0.15s ease;
}

.chip:hover {
    border-color: var(--jus-gold);
    background: var(--jus-surface);
    transform: translateY(-1px);
}

.chip:disabled {
    opacity: 0.5;
    cursor: not-allowed;
    transform: none;
}

/* --------------------------------------------------------------------------
   App layout
   -------------------------------------------------------------------------- */

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

.main {
    flex: 1;
    min-width: 0;
    display: flex;
    flex-direction: column;
    background: var(--jus-surface);
}

.chat-column {
    width: 100%;
    max-width: var(--chat-max-width);
    margin: 0 auto;
}

/* --------------------------------------------------------------------------
   Sidebar
   -------------------------------------------------------------------------- */

.sidebar {
    display: flex;
    flex-direction: column;
    width: var(--sidebar-width);
    flex-shrink: 0;
    background: var(--jus-bg);
    border-right: 1px solid var(--jus-border);
    transition: width 0.2s ease, transform 0.25s ease;
}

.sidebar-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 0.5rem;
    padding: 1.25rem 1rem 1rem;
}

.brand {
    display: flex;
    align-items: center;
    gap: 0.75rem;
    min-width: 0;
    text-decoration: none;
    color: var(--jus-text);
    border-radius: var(--radius-btn);
}

.brand-logo {
    width: 40px;
    height: 40px;
    border-radius: 50%;
    object-fit: cover;
    flex-shrink: 0;
    border: 2px solid var(--jus-gold);
}

.brand-name {
    font-family: var(--font-heading);
    font-size: 1.0625rem;
    font-weight: 700;
    letter-spacing: 0.01em;
    white-space: nowrap;
    overflow: hidden;
}

.brand-accent {
    color: var(--jus-gold);
}

.sidebar-close {
    display: none;
}

.btn-new-chat {
    margin: 0.25rem 1rem 1rem;
}

.history {
    flex: 1;
    overflow-y: auto;
    padding: 0 1rem 1rem;
    min-height: 0;
}

.history-title {
    font-family: var(--font-heading);
    font-size: 0.75rem;
    font-weight: 600;
    letter-spacing: 0.08em;
    text-transform: uppercase;
    color: var(--jus-text-muted);
    margin: 0.5rem 0 0.75rem;
}

.history-list {
    list-style: none;
    display: flex;
    flex-direction: column;
    gap: 0.375rem;
}

.history-item a {
    display: block;
    padding: 0.625rem 0.75rem;
    border-radius: var(--radius-btn);
    border-left: 3px solid transparent;
    text-decoration: none;
    color: var(--jus-text);
    transition: background 0.15s ease, border-color 0.15s ease;
}

.history-item a:hover {
    background: var(--jus-surface-2);
}

.history-item.active a {
    background: var(--jus-surface-2);
    border-left-color: var(--jus-gold);
}

.history-item-title {
    display: block;
    font-size: 0.875rem;
    font-weight: 500;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.history-item-preview {
    display: block;
    font-size: 0.75rem;
    color: var(--jus-text-muted);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.history-empty {
    font-size: 0.8125rem;
    color: var(--jus-text-muted);
    padding: 0.25rem 0.125rem;
}

.sidebar-footer {
    padding: 1rem;
    border-top: 1px solid var(--jus-border);
    text-align: center;
}

.powered-by {
    font-size: 0.8125rem;
    color: var(--jus-text-muted);
    text-decoration: none;
    white-space: nowrap;
}

.powered-by strong {
    color: var(--jus-gold);
    font-weight: 600;
}

.powered-by:hover strong {
    text-decoration: underline;
}

/* Collapsed icon rail (tablet default, desktop toggle) */
.sidebar.collapsed {
    width: var(--sidebar-rail);
}

.sidebar.collapsed .brand-name,
.sidebar.collapsed .btn-new-chat span,
.sidebar.collapsed .history,
.sidebar.collapsed .powered-by {
    display: none;
}

.sidebar.collapsed .sidebar-header {
    justify-content: center;
    padding-left: 0.5rem;
    padding-right: 0.5rem;
}

.sidebar.collapsed .btn-new-chat {
    margin: 0.25rem 0.75rem 1rem;
    padding: 0.625rem;
}

.sidebar.collapsed .sidebar-footer {
    padding: 0.75rem 0.5rem;
}

.sidebar.collapsed .sidebar-footer::after {
    content: 'MOVA';
    font-size: 0.6875rem;
    font-weight: 600;
    letter-spacing: 0.06em;
    color: var(--jus-gold);
}

/* Mobile off-canvas scrim */
.scrim {
    display: none;
}

/* --------------------------------------------------------------------------
   Chat header
   -------------------------------------------------------------------------- */

.chat-header {
    display: flex;
    align-items: center;
    gap: 0.875rem;
    padding: 0.875rem 1.5rem;
    background: var(--jus-surface);
    border-bottom: 1px solid var(--jus-border);
}

.menu-btn {
    flex-shrink: 0;
}

.header-avatar {
    width: 44px;
    height: 44px;
    border-radius: 50%;
    object-fit: cover;
    border: 2px solid var(--jus-gold);
    flex-shrink: 0;
}

.header-info {
    min-width: 0;
    flex: 1;
}

.header-info h1 {
    font-family: var(--font-heading);
    font-size: 1.0625rem;
    font-weight: 700;
    line-height: 1.3;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.header-info p {
    font-size: 0.8125rem;
    color: var(--jus-text-muted);
    line-height: 1.3;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.lang-select {
    flex-shrink: 0;
}

.lang-select select {
    appearance: none;
    -webkit-appearance: none;
    background: var(--jus-surface-2);
    color: var(--jus-text);
    border: 1px solid var(--jus-border);
    border-radius: var(--radius-btn);
    font-family: var(--font-body);
    font-size: 0.8125rem;
    font-weight: 500;
    padding: 0.375rem 1.75rem 0.375rem 0.625rem;
    cursor: pointer;
    background-image: url("data:image/svg+xml;charset=utf-8,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 512 512'%3E%3Cpath fill='%239ca3af' d='M233.4 406.6c12.5 12.5 32.8 12.5 45.3 0l192-192c12.5-12.5 12.5-32.8 0-45.3s-32.8-12.5-45.3 0L256 338.7 86.6 169.4c-12.5-12.5-32.8-12.5-45.3 0s-12.5 32.8 0 45.3l192 192z'/%3E%3C/svg%3E");
    background-repeat: no-repeat;
    background-position: right 0.5rem center;
    background-size: 0.7em;
}

.lang-select select:focus-visible {
    outline: none;
    box-shadow: var(--focus-ring);
}

.chat-status {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    font-size: 0.8125rem;
    font-weight: 500;
    color: var(--jus-success);
    flex-shrink: 0;
}

.chat-status.offline {
    color: var(--jus-danger);
}

.status-dot {
    width: 8px;
    height: 8px;
    border-radius: 50%;
    background: currentColor;
}

/* --------------------------------------------------------------------------
   Messages
   -------------------------------------------------------------------------- */

.chat-scroll {
    flex: 1;
    overflow-y: auto;
    scroll-behavior: smooth;
    padding: 1.5rem;
}

.welcome {
    padding: 2.5rem 0 2rem;
    text-align: center;
}

.welcome-logo {
    width: 72px;
    height: 72px;
    border-radius: 50%;
    object-fit: cover;
    border: 3px solid var(--jus-gold);
    box-shadow: var(--shadow-gold);
    margin-bottom: 1.25rem;
}

.welcome h2 {
    font-family: var(--font-heading);
    font-size: 1.5rem;
    font-weight: 700;
    margin-bottom: 0.5rem;
}

.welcome p {
    color: var(--jus-text-muted);
    max-width: 32rem;
    margin: 0 auto 1.75rem;
}

.welcome-chips {
    display: flex;
    flex-wrap: wrap;
    justify-content: center;
    gap: 0.625rem;
    max-width: 40rem;
    margin: 0 auto;
}

.message {
    display: flex;
    align-items: flex-end;
    gap: 0.625rem;
    margin-bottom: 1.25rem;
    animation: msg-in 0.2s ease-out;
}

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

.message.user {
    flex-direction: row-reverse;
}

.avatar {
    width: 34px;
    height: 34px;
    border-radius: 50%;
    flex-shrink: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    overflow: hidden;
}

.message.assistant .avatar {
    border: 1.5px solid var(--jus-gold);
}

.avatar img {
    width: 100%;
    height: 100%;
    object-fit: cover;
}

.message.user .avatar {
    background: linear-gradient(135deg, var(--jus-gold), var(--jus-gold-deep));
    color: var(--jus-text-on-gold);
}

.message.user .avatar svg {
    width: 16px;
    height: 16px;
    fill: currentColor;
}

.bubble {
    max-width: 72%;
    padding: 0.75rem 1rem 0.5rem;
    border-radius: var(--radius-bubble);
    box-shadow: var(--shadow-soft);
    overflow-wrap: break-word;
}

.message.user .bubble {
    background: linear-gradient(135deg, var(--jus-gold), var(--jus-gold-deep));
    color: var(--jus-text-on-gold);
    border-bottom-right-radius: 4px;
}

.message.assistant .bubble {
    background: var(--jus-surface-2);
    color: var(--jus-text);
    border: 1px solid var(--jus-border);
    border-bottom-left-radius: 4px;
}

.bubble-content a {
    color: inherit;
    font-weight: 600;
    text-decoration: underline;
}

.message.assistant .bubble-content a {
    color: var(--jus-gold);
}

.bubble-content p + p {
    margin-top: 0.375rem;
}

.bubble-content ul,
.bubble-content ol {
    margin: 0.375rem 0;
    padding-left: 1.375rem;
}

/* Contextual images inside assistant bubbles (work item #11) */
.bubble-images {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(160px, 1fr));
    gap: 0.5rem;
    margin-top: 0.625rem;
}

.bubble-images img {
    width: 100%;
    max-width: 100%;
    max-height: 260px;
    object-fit: cover;
    border-radius: var(--radius-btn);
    border: 1px solid var(--jus-border);
    background: var(--jus-surface-2);
}

.msg-time {
    display: block;
    font-size: 0.6875rem;
    margin-top: 0.25rem;
    opacity: 0.75;
}

.message.assistant .msg-time {
    color: var(--jus-text-muted);
    opacity: 1;
}

/* Typing indicator */
.typing-dots {
    display: flex;
    align-items: center;
    gap: 0.3rem;
    padding: 0.375rem 0;
}

.typing-dot {
    width: 8px;
    height: 8px;
    border-radius: 50%;
    background: var(--jus-gold);
    animation: typing 1.4s infinite ease-in-out;
}

.typing-dot:nth-child(1) { animation-delay: -0.32s; }
.typing-dot:nth-child(2) { animation-delay: -0.16s; }

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

/* Streaming */
.streaming-cursor {
    display: inline-block;
    width: 2px;
    height: 1em;
    vertical-align: text-bottom;
    background: var(--jus-gold);
    margin-left: 2px;
    animation: blink 1s infinite;
}

@keyframes blink {
    0%, 50% { opacity: 1; }
    51%, 100% { opacity: 0; }
}

/* --------------------------------------------------------------------------
   Input bar
   -------------------------------------------------------------------------- */

.input-bar {
    padding: 0.75rem 1.5rem 1rem;
    background: var(--jus-surface);
    border-top: 1px solid var(--jus-border);
}

.btn-stop {
    display: inline-flex;
    align-items: center;
    gap: 0.5rem;
    margin: 0 auto 0.625rem;
    padding: 0.375rem 0.875rem;
    border: 1px solid var(--jus-border);
    border-radius: 999px;
    background: var(--jus-surface-2);
    color: var(--jus-text);
    font-family: var(--font-body);
    font-size: 0.8125rem;
    font-weight: 500;
    cursor: pointer;
    transition: border-color 0.15s ease;
}

.btn-stop:hover {
    border-color: var(--jus-danger);
    color: var(--jus-danger);
}

.btn-stop svg {
    width: 12px;
    height: 12px;
    fill: currentColor;
}

.stop-row {
    display: flex;
    justify-content: center;
}

.composer {
    display: flex;
    align-items: flex-end;
    gap: 0.5rem;
    padding: 0.5rem 0.5rem 0.5rem 1.125rem;
    background: var(--jus-bg);
    border: 1.5px solid var(--jus-border);
    border-radius: 24px;
    transition: border-color 0.15s ease, box-shadow 0.15s ease;
}

.composer:focus-within {
    border-color: var(--jus-gold);
    box-shadow: var(--focus-ring);
}

.composer textarea {
    flex: 1;
    resize: none;
    border: none;
    outline: none;
    background: transparent;
    color: var(--jus-text);
    font-family: var(--font-body);
    /* Must stay >= 16px: iOS Safari force-zooms the whole page on focus for
       any text input/textarea with a computed font-size below 16px, which
       breaks the layout until the user manually pinch-zooms back out. */
    font-size: 1rem;
    line-height: 1.5;
    padding: 0.375rem 0;
    max-height: 9rem;
}

.composer textarea::placeholder {
    color: var(--jus-text-muted);
}

.composer textarea:disabled {
    opacity: 0.6;
}

.btn-send {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 40px;
    height: 40px;
    flex-shrink: 0;
    border: none;
    border-radius: 50%;
    background: linear-gradient(135deg, var(--jus-gold), var(--jus-gold-deep));
    color: var(--jus-text-on-gold);
    cursor: pointer;
    transition: transform 0.15s ease, box-shadow 0.15s ease, opacity 0.15s ease;
}

.btn-send:hover:not(:disabled) {
    transform: scale(1.06);
    box-shadow: var(--shadow-gold);
}

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

.btn-send svg {
    width: 18px;
    height: 18px;
    fill: currentColor;
}

.btn-send .spinner {
    width: 18px;
    height: 18px;
    border: 2px solid rgba(18, 18, 21, 0.3);
    border-top-color: var(--jus-text-on-gold);
    border-radius: 50%;
    animation: spin 0.7s linear infinite;
}

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

.input-hint {
    margin-top: 0.5rem;
    text-align: center;
    font-size: 0.75rem;
    color: var(--jus-text-muted);
}

/* --------------------------------------------------------------------------
   Responsive
   -------------------------------------------------------------------------- */

/* Laptop */
@media (max-width: 1439px) {
    :root {
        --sidebar-width: 260px;
    }
}

/* Tablet: rail collapses by default (JS adds .collapsed) */
@media (max-width: 1023px) {
    .chat-header {
        padding: 0.75rem 1rem;
    }

    .chat-scroll {
        padding: 1rem;
    }

    .input-bar {
        padding: 0.625rem 1rem 0.875rem;
    }
}

/* Mobile: off-canvas sidebar */
@media (max-width: 767px) {
    .sidebar {
        position: fixed;
        top: 0;
        left: 0;
        height: 100vh;
        height: 100dvh;
        z-index: 40;
        width: min(85vw, 320px);
        transform: translateX(-105%);
        box-shadow: var(--shadow-soft);
    }

    .sidebar.collapsed {
        /* rail mode does not apply on mobile */
        width: min(85vw, 320px);
    }

    .sidebar.collapsed .brand-name,
    .sidebar.collapsed .btn-new-chat span,
    .sidebar.collapsed .history,
    .sidebar.collapsed .powered-by {
        display: revert;
    }

    .sidebar.collapsed .history {
        display: block;
    }

    .sidebar.collapsed .sidebar-footer::after {
        content: none;
    }

    .sidebar.open {
        transform: translateX(0);
    }

    .sidebar-close {
        display: inline-flex;
    }

    .scrim {
        display: block;
        position: fixed;
        inset: 0;
        z-index: 30;
        background: rgba(0, 0, 0, 0.6);
        opacity: 0;
        pointer-events: none;
        transition: opacity 0.25s ease;
    }

    .scrim.visible {
        opacity: 1;
        pointer-events: auto;
    }

    .bubble {
        max-width: 85%;
    }

    .header-info p {
        display: none;
    }

    .chat-status .status-text {
        display: none;
    }

    .lang-select select {
        max-width: 5.5rem;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
    }

    .input-hint {
        display: none;
    }
}

/* --------------------------------------------------------------------------
   Reduced motion
   -------------------------------------------------------------------------- */

@media (prefers-reduced-motion: reduce) {
    *,
    *::before,
    *::after {
        animation-duration: 0.01ms !important;
        animation-iteration-count: 1 !important;
        transition-duration: 0.01ms !important;
    }

    .chat-scroll {
        scroll-behavior: auto;
    }
}
