/* ================================================================
   Mandaya Healthcare — WebUI Chat Interface
   Issue #34: Modernize with Mandaya Hospital Group branding.

   Dependency-free design system (no external CDN / no web-font
   request) — premium, responsive, WCAG 2.1 AA. Every component
   reads from the tokens on :root (single source of truth).
   ================================================================ */

/* ── Design tokens ─────────────────────────────────────────────── */
:root {
    /* Brand — Mandaya (navy primary + teal accent) */
    --brand-navy:      #0a2a5e;
    --brand-navy-700:  #0e3a7b;
    --brand-navy-050:  #eef3fb;
    --brand-teal:      #0ea5a0;   /* accent — focus, active, send hover */
    --brand-teal-600:  #0b8781;
    --brand-teal-700:  #0a6e69;   /* link text on white (AA) */

    /* Surfaces & neutrals */
    --bg:              #f5f8fc;
    --surface:         #ffffff;
    --surface-muted:   #eef3f9;   /* assistant bubble, hovered rows */
    --sidebar-bg:      #0a2a5e;   /* navy sidebar */
    --sidebar-fg:      #dbe6f5;
    --sidebar-fg-dim:  #9fb4d4;
    --sidebar-border:  rgba(255, 255, 255, 0.10);
    --sidebar-hover:   rgba(255, 255, 255, 0.08);
    --sidebar-active:  rgba(14, 165, 160, 0.18);

    --text:            #132238;
    --text-muted:      #55647c;
    --border:          #dce5f0;
    --success:         #15803d;

    /* Typography */
    --font-sans: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto,
                 Helvetica, Arial, "Noto Sans", sans-serif;
    --fs-xs:   0.72rem;
    --fs-sm:   0.82rem;
    --fs-base: 0.92rem;
    --fs-md:   1rem;
    --fs-lg:   1.25rem;
    --fs-xl:   1.6rem;

    /* Spacing scale (4px base) */
    --space-1: 0.25rem;
    --space-2: 0.5rem;
    --space-3: 0.75rem;
    --space-4: 1rem;
    --space-5: 1.5rem;
    --space-6: 2rem;

    /* Radii */
    --radius-sm: 10px;
    --radius:    14px;
    --radius-lg: 20px;
    --radius-full: 999px;

    /* Elevation — soft, low-alpha navy shadows */
    --shadow-sm: 0 1px 2px rgba(10, 42, 94, 0.06);
    --shadow-md: 0 4px 16px rgba(10, 42, 94, 0.08);
    --shadow-lg: 0 12px 32px rgba(10, 42, 94, 0.14);

    /* Motion */
    --dur-fast: 0.15s;
    --dur-base: 0.25s;
    --ease: cubic-bezier(0.4, 0, 0.2, 1);

    --sidebar-w: 280px;
}

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

html, body {
    height: 100%;
    max-width: 100%;
    overflow-x: hidden;
    -webkit-text-size-adjust: 100%;
}

body {
    font-family: var(--font-sans);
    background: var(--bg);
    color: var(--text);
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
}

/* ── App shell (sidebar + main) ────────────────────────────────── */
.app-shell {
    display: flex;
    height: 100dvh;
    width: 100%;
    max-width: 100%;
    overflow: hidden;
}

/* ── Sidebar ───────────────────────────────────────────────────── */
.sidebar {
    width: var(--sidebar-w);
    flex-shrink: 0;
    background: var(--sidebar-bg);
    color: var(--sidebar-fg);
    display: flex;
    flex-direction: column;
    padding: var(--space-4);
    padding-top: calc(var(--space-4) + env(safe-area-inset-top, 0px));
    gap: var(--space-4);
    border-right: 1px solid var(--sidebar-border);
}

.brand-lockup {
    display: flex;
    align-items: center;
    gap: var(--space-3);
    padding: var(--space-1) var(--space-1) var(--space-3);
    border-bottom: 1px solid var(--sidebar-border);
}
.brand-emblem {
    width: 40px; height: 40px;
    flex-shrink: 0;
    border-radius: var(--radius-sm);
    background: linear-gradient(135deg, var(--brand-teal), var(--brand-teal-600));
    display: flex; align-items: center; justify-content: center;
    color: #fff;
    box-shadow: var(--shadow-sm);
}
.brand-emblem svg { width: 22px; height: 22px; }
.brand-name { font-size: var(--fs-md); font-weight: 700; line-height: 1.15; color: #fff; }
.brand-sub  { font-size: var(--fs-xs); color: var(--sidebar-fg-dim); margin-top: 2px; }

.btn-new-chat {
    display: flex; align-items: center; justify-content: center;
    gap: var(--space-2);
    width: 100%;
    padding: 0.7rem var(--space-4);
    border: 1px solid var(--sidebar-border);
    border-radius: var(--radius);
    background: rgba(255, 255, 255, 0.06);
    color: #fff;
    font: inherit;
    font-size: var(--fs-sm);
    font-weight: 600;
    cursor: pointer;
    transition: background var(--dur-fast) var(--ease), border-color var(--dur-fast) var(--ease);
}
.btn-new-chat svg { width: 16px; height: 16px; }
.btn-new-chat:hover { background: var(--sidebar-hover); border-color: rgba(255, 255, 255, 0.28); }

.history {
    flex: 1;
    min-height: 0;
    overflow-y: auto;
    display: flex;
    flex-direction: column;
    gap: var(--space-1);
}
.history-label {
    font-size: var(--fs-xs);
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.06em;
    color: var(--sidebar-fg-dim);
    padding: var(--space-2) var(--space-2) var(--space-1);
}
.history-list { list-style: none; display: flex; flex-direction: column; gap: 2px; }
.history-empty { font-size: var(--fs-sm); color: var(--sidebar-fg-dim); padding: var(--space-2); line-height: 1.5; }

.history-item {
    display: flex; align-items: center; gap: var(--space-2);
    width: 100%;
    padding: 0.6rem var(--space-3);
    border: none;
    border-radius: var(--radius-sm);
    background: transparent;
    color: var(--sidebar-fg);
    font: inherit;
    font-size: var(--fs-sm);
    text-align: left;
    cursor: pointer;
    transition: background var(--dur-fast) var(--ease);
}
.history-item svg { width: 15px; height: 15px; flex-shrink: 0; opacity: 0.7; }
.history-item .history-title {
    flex: 1; min-width: 0;
    overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.history-item:hover { background: var(--sidebar-hover); }
.history-item[aria-current="true"] {
    background: var(--sidebar-active);
    color: #fff;
    box-shadow: inset 3px 0 0 var(--brand-teal);
}

.sidebar-foot {
    font-size: var(--fs-xs);
    color: var(--sidebar-fg-dim);
    border-top: 1px solid var(--sidebar-border);
    padding-top: var(--space-3);
    line-height: 1.5;
}

/* Sidebar backdrop (mobile drawer) */
.sidebar-backdrop {
    display: none;
    position: fixed;
    inset: 0;
    background: rgba(10, 42, 94, 0.45);
    z-index: 40;
    opacity: 0;
    transition: opacity var(--dur-base) var(--ease);
}

/* ── Main column ───────────────────────────────────────────────── */
.main {
    flex: 1;
    min-width: 0;
    max-width: 100%;
    overflow-x: clip;   /* iOS Safari honors clip here even when body overflow-x is ignored */
    display: flex;
    flex-direction: column;
    background: var(--surface);
}

.chat-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: var(--space-3);
    padding: 0.85rem var(--space-5);
    padding-top: calc(0.85rem + env(safe-area-inset-top, 0px));
    border-bottom: 1px solid var(--border);
    background: var(--surface);
    flex-shrink: 0;
}
.header-left { display: flex; align-items: center; gap: var(--space-3); min-width: 0; }

.icon-btn {
    width: 38px; height: 38px;
    display: flex; align-items: center; justify-content: center;
    border: 1px solid var(--border);
    background: var(--surface);
    border-radius: var(--radius-sm);
    color: var(--text-muted);
    cursor: pointer;
    flex-shrink: 0;
    transition: color var(--dur-fast) var(--ease), border-color var(--dur-fast) var(--ease),
                background var(--dur-fast) var(--ease);
}
.icon-btn svg { width: 18px; height: 18px; }
.icon-btn:hover { color: var(--brand-teal-700); border-color: var(--brand-teal); background: var(--brand-navy-050); }

.menu-btn { display: none; }

.header-avatar {
    width: 42px; height: 42px;
    border-radius: var(--radius-full);
    background: linear-gradient(135deg, var(--brand-navy), var(--brand-navy-700));
    display: flex; align-items: center; justify-content: center;
    color: #fff;
    flex-shrink: 0;
    box-shadow: var(--shadow-sm);
}
.header-avatar svg { width: 22px; height: 22px; }
.header-info { min-width: 0; overflow: hidden; }
.header-info h1 {
    font-size: var(--fs-md); font-weight: 700; line-height: 1.2; color: var(--text);
    overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.header-subtitle {
    font-size: var(--fs-xs); color: var(--text-muted);
    overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}

.header-right { display: flex; align-items: center; gap: var(--space-3); flex-shrink: 0; }
.connection-badge {
    display: flex; align-items: center; gap: var(--space-1);
    padding: 0.3rem 0.65rem;
    border-radius: var(--radius-full);
    font-size: var(--fs-xs); font-weight: 600;
    color: var(--success);
    background: rgba(21, 128, 61, 0.10);
}
.connection-dot {
    width: 7px; height: 7px; border-radius: 50%;
    background: var(--success);
}

/* ── Messages ──────────────────────────────────────────────────── */
.chat-messages {
    flex: 1;
    overflow-y: auto;
    overflow-x: hidden;
    padding: var(--space-5);
    scroll-behavior: smooth;
    overscroll-behavior: contain;
}
.chat-inner { width: 100%; max-width: 780px; margin: 0 auto; }

.welcome { text-align: center; padding: var(--space-5) 0; }
.welcome-emblem {
    width: 68px; height: 68px;
    margin: 0 auto var(--space-4);
    border-radius: var(--radius-lg);
    background: linear-gradient(135deg, var(--brand-navy), var(--brand-teal-600));
    display: flex; align-items: center; justify-content: center;
    color: #fff;
    box-shadow: var(--shadow-md);
}
.welcome-emblem svg { width: 34px; height: 34px; }
.welcome h2 { font-size: var(--fs-xl); margin-bottom: var(--space-2); color: var(--text); }
.welcome-desc {
    color: var(--text-muted); font-size: var(--fs-base); line-height: 1.6;
    max-width: 460px; margin: 0 auto var(--space-5);
    overflow-wrap: anywhere;
}

.suggestion-grid {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: var(--space-3);
    width: 100%;
    max-width: 560px;
    margin: 0 auto;
}
.suggestion-card {
    display: flex; align-items: center; gap: var(--space-3);
    width: 100%;
    min-width: 0;
    padding: 0.9rem var(--space-4);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    background: var(--surface);
    color: var(--text);
    font: inherit;
    font-size: var(--fs-sm);
    text-align: left;
    cursor: pointer;
    box-shadow: var(--shadow-sm);
    transition: border-color var(--dur-fast) var(--ease), transform var(--dur-fast) var(--ease),
                box-shadow var(--dur-fast) var(--ease);
}
.suggestion-card svg { width: 18px; height: 18px; color: var(--brand-teal-600); flex-shrink: 0; }
.suggestion-card span {
    min-width: 0;
    white-space: normal;
    overflow-wrap: anywhere;
    line-height: 1.45;
}
.suggestion-card:hover {
    border-color: var(--brand-teal);
    transform: translateY(-1px);
    box-shadow: var(--shadow-md);
}

/* ── Message bubbles ───────────────────────────────────────────── */
.msg {
    display: flex;
    align-items: flex-start;
    gap: var(--space-3);
    margin-bottom: var(--space-4);
    animation: msg-in var(--dur-base) var(--ease);
}
.msg.user { flex-direction: row-reverse; }

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

.msg-avatar {
    width: 34px; height: 34px;
    border-radius: var(--radius-full);
    display: flex; align-items: center; justify-content: center;
    flex-shrink: 0;
}
.msg-avatar svg { width: 18px; height: 18px; }
.msg-avatar.bot  { background: linear-gradient(135deg, var(--brand-navy), var(--brand-navy-700)); color: #fff; }
.msg-avatar.user { background: var(--surface-muted); color: var(--brand-navy); }

.msg-bubble {
    max-width: 78%;
    padding: 0.75rem var(--space-4);
    border-radius: var(--radius-lg);
    font-size: var(--fs-base);
    line-height: 1.6;
    white-space: pre-wrap;
    overflow-wrap: anywhere;
    box-shadow: var(--shadow-sm);
}
.msg.bot .msg-bubble {
    background: var(--surface-muted);
    color: var(--text);
    border-top-left-radius: 6px;
}
.msg.user .msg-bubble {
    background: var(--brand-navy);
    color: #fff;
    border-top-right-radius: 6px;
}

/* ── Typing indicator ──────────────────────────────────────────── */
.typing-row { display: flex; align-items: flex-start; gap: var(--space-3); margin-bottom: var(--space-4); }
.typing-row[hidden] { display: none; }
.typing-bubble {
    display: flex; align-items: center; gap: var(--space-3);
    padding: 0.75rem var(--space-4);
    background: var(--surface-muted);
    border-radius: var(--radius-lg);
    border-top-left-radius: 6px;
}
.typing-label { font-size: var(--fs-sm); color: var(--text-muted); }
.typing-dots { display: flex; gap: 4px; }
.typing-dots span {
    width: 7px; height: 7px; border-radius: 50%;
    background: var(--brand-teal);
    animation: bounce 1.4s infinite ease-in-out both;
}
.typing-dots span:nth-child(1) { animation-delay: -0.32s; }
.typing-dots span:nth-child(2) { animation-delay: -0.16s; }
@keyframes bounce {
    0%, 80%, 100% { transform: scale(0.6); opacity: 0.4; }
    40%           { transform: scale(1);   opacity: 1;   }
}

/* ── Input area ────────────────────────────────────────────────── */
.chat-input-area {
    padding: var(--space-3) var(--space-5) var(--space-2);
    border-top: 1px solid var(--border);
    background: var(--surface);
    flex-shrink: 0;
}
.input-wrap {
    display: flex;
    align-items: flex-end;
    gap: var(--space-2);
    width: 100%;
    max-width: 780px;
    margin: 0 auto;
    border: 1.5px solid var(--border);
    border-radius: var(--radius);
    padding: 0.3rem 0.3rem 0.3rem 0.9rem;
    background: var(--surface);
    box-shadow: var(--shadow-sm);
    transition: border-color var(--dur-fast) var(--ease), box-shadow var(--dur-fast) var(--ease);
}
.input-wrap:focus-within {
    border-color: var(--brand-teal);
    box-shadow: 0 0 0 3px rgba(14, 165, 160, 0.18);
}
.input-field {
    flex: 1 1 auto;
    min-width: 0;                 /* allow the flex textarea to shrink — prevents overflow */
    width: 100%;                  /* Safari won't shrink a textarea below intrinsic size without this */
    border: none; background: transparent;
    resize: none;
    font: inherit;
    font-size: var(--fs-base);
    color: var(--text);
    padding: 0.35rem 0;
    max-height: 150px;
    line-height: 1.4;
}
/* The wrap already shows the focus state; suppress the redundant nested outline. */
.input-field:focus,
.input-field:focus-visible { outline: none; }
.input-field::placeholder { color: var(--text-muted); }

.btn-send {
    width: 36px; height: 36px;
    display: flex; align-items: center; justify-content: center;
    border: none;
    border-radius: var(--radius-sm);
    background: var(--brand-navy);
    color: #fff;
    cursor: pointer;
    flex-shrink: 0;
    transition: background var(--dur-fast) var(--ease), opacity var(--dur-fast) var(--ease);
}
.btn-send svg { width: 17px; height: 17px; }
.btn-send:hover:not(:disabled) { background: var(--brand-navy-700); }
.btn-send:disabled { opacity: 0.4; cursor: not-allowed; }

.disclaimer {
    max-width: 780px;
    margin: var(--space-2) auto 0;
    font-size: var(--fs-xs);
    color: var(--text-muted);
    text-align: center;
    line-height: 1.5;
}

/* ── Footer: Powered by MOVA AI ────────────────────────────────── */
.chat-footer {
    display: flex; align-items: center; justify-content: center;
    gap: var(--space-3);
    padding: var(--space-2) var(--space-5);
    padding-bottom: calc(var(--space-2) + env(safe-area-inset-bottom, 0px));
    background: var(--surface);
    flex-shrink: 0;
}
.footer-line { width: 28px; height: 1px; background: var(--border); }
.footer-brand {
    display: flex; align-items: center; gap: 0.35rem;
    text-decoration: none;
    transition: opacity var(--dur-fast) var(--ease);
}
.footer-brand:hover { opacity: 0.75; }
.footer-powered {
    font-size: var(--fs-xs); color: var(--text-muted);
}
.footer-brand-text {
    font-weight: 700;
    font-size: var(--fs-xs);
    letter-spacing: 0.12em;
    text-transform: uppercase;
    color: var(--brand-navy);
}
.footer-sub { font-size: var(--fs-xs); color: var(--brand-teal-700); font-weight: 700; }

/* ── Accessibility helpers ─────────────────────────────────────── */
.sr-only {
    position: absolute; width: 1px; height: 1px;
    padding: 0; margin: -1px; overflow: hidden;
    clip: rect(0, 0, 0, 0); border: 0;
}
:focus-visible {
    outline: 2px solid var(--brand-teal);
    outline-offset: 2px;
    border-radius: 4px;
}
.history-item:focus-visible { outline-offset: -2px; }

/* ── Responsive ────────────────────────────────────────────────── */
/* Tablet & below: sidebar becomes an off-canvas drawer */
@media (max-width: 1023px) {
    .menu-btn { display: flex; }
    .sidebar {
        position: fixed;
        top: 0; left: 0; bottom: 0;
        z-index: 50;
        transform: translateX(-100%);
        transition: transform var(--dur-base) var(--ease);
        box-shadow: var(--shadow-lg);
    }
    .app-shell.sidebar-open .sidebar { transform: translateX(0); }
    .app-shell.sidebar-open .sidebar-backdrop { display: block; opacity: 1; }
}

/* Mobile */
@media (max-width: 767px) {
    .sidebar { width: min(84vw, var(--sidebar-w)); }
    .chat-messages { padding: var(--space-4); }
    .chat-header {
        padding: 0.6rem var(--space-4);
        padding-left: calc(var(--space-4) + env(safe-area-inset-left, 0px));
        padding-right: calc(var(--space-4) + env(safe-area-inset-right, 0px));
    }
    .chat-input-area {
        padding: var(--space-3) var(--space-4) var(--space-2);
        padding-left: calc(var(--space-4) + env(safe-area-inset-left, 0px));
        padding-right: calc(var(--space-4) + env(safe-area-inset-right, 0px));
    }
    .suggestion-grid { grid-template-columns: 1fr; }
    .header-subtitle { display: none; }
    .msg-bubble { max-width: 84%; }
    .chat-footer {
        padding: var(--space-2) var(--space-4);
        padding-left: calc(var(--space-4) + env(safe-area-inset-left, 0px));
        padding-right: calc(var(--space-4) + env(safe-area-inset-right, 0px));
        padding-bottom: calc(var(--space-2) + env(safe-area-inset-bottom, 0px));
    }
    .input-field { font-size: 16px; }
    /* Comfortable tap targets without inflating the input height */
    .icon-btn { width: 40px; height: 40px; }
    .btn-send { width: 38px; height: 38px; }
}

/* Large desktop: a touch more breathing room */
@media (min-width: 1440px) {
    .chat-inner, .input-wrap, .disclaimer { max-width: 860px; }
}

/* Respect reduced-motion preferences */
@media (prefers-reduced-motion: reduce) {
    *, *::before, *::after {
        animation-duration: 0.001ms !important;
        animation-iteration-count: 1 !important;
        transition-duration: 0.001ms !important;
        scroll-behavior: auto !important;
    }
}
