/* UNCS AI Assistant — Chat UI Styles */

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

:root {
    --navy: #1a365d;
    --navy-light: #2a4a7f;
    --navy-dark: #0f2440;
    --navy-hover: #1e3f6e;
    --blue: #2563eb;
    --blue-light: #3b82f6;
    --gray-50: #f9fafb;
    --gray-100: #f3f4f6;
    --gray-200: #e5e7eb;
    --gray-300: #d1d5db;
    --gray-400: #9ca3af;
    --gray-500: #6b7280;
    --gray-700: #374151;
    --gray-800: #1f2937;
    --gray-900: #111827;
    --code-bg: #1e293b;
    --red-500: #ef4444;
    --red-50: #fef2f2;
    --red-100: #fee2e2;
    --red-700: #b91c1c;
    --green-500: #22c55e;
    --amber-50: #fffbeb;
    --amber-200: #fde68a;
    --amber-700: #b45309;
    --white: #ffffff;
    --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.05);
    --shadow-md: 0 4px 6px -1px rgba(0, 0, 0, 0.1);
    --shadow-lg: 0 10px 15px -3px rgba(0, 0, 0, 0.1);
    --shadow-xl: 0 20px 25px -5px rgba(0, 0, 0, 0.1), 0 8px 10px -6px rgba(0, 0, 0, 0.1);
    --sidebar-width: 260px;
}

html, body {
    height: 100%;
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen,
        Ubuntu, Cantarell, 'Fira Sans', 'Droid Sans', 'Helvetica Neue', sans-serif;
    font-size: 15px;
    line-height: 1.6;
    color: var(--gray-800);
    background: var(--white);
}

/* ── Login Overlay ──────────────────────────────────────────────────── */

.login-overlay {
    position: fixed;
    inset: 0;
    z-index: 1000;
    display: flex;
    align-items: center;
    justify-content: center;
    background: var(--navy-dark);
    /* WHY: Subtle gradient adds depth without being distracting */
    background: linear-gradient(135deg, var(--navy-dark) 0%, var(--navy) 50%, var(--navy-light) 100%);
}

.login-card {
    background: var(--white);
    border-radius: 16px;
    padding: 48px 40px;
    width: 100%;
    max-width: 380px;
    box-shadow: var(--shadow-xl);
    text-align: center;
    animation: cardIn 0.4s ease-out;
}

@keyframes cardIn {
    from {
        opacity: 0;
        transform: translateY(20px) scale(0.98);
    }
    to {
        opacity: 1;
        transform: translateY(0) scale(1);
    }
}

.login-brand {
    font-size: 28px;
    font-weight: 700;
    color: var(--navy);
    letter-spacing: 0.05em;
    margin-bottom: 4px;
}

.login-title {
    font-size: 18px;
    font-weight: 500;
    color: var(--gray-700);
    margin-bottom: 8px;
}

.login-subtitle {
    font-size: 14px;
    color: var(--gray-500);
    margin-bottom: 28px;
}

.pin-input {
    display: block;
    width: 100%;
    padding: 14px 16px;
    font-size: 24px;
    font-family: 'SF Mono', SFMono-Regular, Consolas, monospace;
    letter-spacing: 0.4em;
    text-align: center;
    border: 2px solid var(--gray-300);
    border-radius: 12px;
    outline: none;
    background: var(--gray-50);
    transition: border-color 0.15s, box-shadow 0.15s;
    /* WHY: -webkit-text-security ensures consistent masking across browsers */
}

.pin-input:focus {
    border-color: var(--blue);
    background: var(--white);
    box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.15);
}

.pin-input::placeholder {
    color: var(--gray-300);
    letter-spacing: 0.6em;
}

.login-btn {
    display: block;
    width: 100%;
    margin-top: 16px;
    padding: 12px;
    font-size: 15px;
    font-weight: 600;
    font-family: inherit;
    color: var(--white);
    background: var(--navy);
    border: none;
    border-radius: 10px;
    cursor: pointer;
    transition: background 0.15s;
}

.login-btn:hover {
    background: var(--navy-light);
}

.login-btn:disabled {
    background: var(--gray-300);
    cursor: not-allowed;
}

.login-error {
    margin-top: 14px;
    font-size: 13px;
    color: var(--red-500);
    min-height: 20px;
}

/* ── App Hidden State ───────────────────────────────────────────────── */

.app-hidden {
    display: none !important;
}

/* ── Layout ─────────────────────────────────────────────────────────── */

#app {
    display: flex;
    height: 100vh;
    max-height: 100vh;
    overflow: hidden;
}

/* ── Sidebar ────────────────────────────────────────────────────────── */

.sidebar {
    width: var(--sidebar-width);
    min-width: var(--sidebar-width);
    height: 100vh;
    background: var(--navy-dark);
    display: flex;
    flex-direction: column;
    transition: margin-left 0.25s ease;
    z-index: 20;
    overflow: hidden;
}

.sidebar.collapsed {
    margin-left: calc(-1 * var(--sidebar-width));
}

.sidebar-header {
    padding: 12px;
    flex-shrink: 0;
}

.new-chat-btn {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    width: 100%;
    padding: 10px 16px;
    font-size: 14px;
    font-weight: 600;
    font-family: inherit;
    color: var(--white);
    background: var(--navy-light);
    border: 1px solid rgba(255, 255, 255, 0.15);
    border-radius: 8px;
    cursor: pointer;
    transition: background 0.15s;
}

.new-chat-btn:hover {
    background: var(--navy-hover);
    border-color: rgba(255, 255, 255, 0.25);
}

.conversation-list {
    flex: 1;
    overflow-y: auto;
    padding: 4px 8px;
}

.conversation-list::-webkit-scrollbar {
    width: 4px;
}

.conversation-list::-webkit-scrollbar-track {
    background: transparent;
}

.conversation-list::-webkit-scrollbar-thumb {
    background: rgba(255, 255, 255, 0.15);
    border-radius: 2px;
}

.conv-item {
    display: block;
    width: 100%;
    padding: 10px 12px;
    margin-bottom: 2px;
    background: transparent;
    border: none;
    border-radius: 6px;
    cursor: pointer;
    text-align: left;
    font-family: inherit;
    transition: background 0.12s;
    position: relative;
    border-left: 3px solid transparent;
}

.conv-item:hover {
    background: rgba(255, 255, 255, 0.08);
}

.conv-item.active {
    background: rgba(255, 255, 255, 0.12);
    border-left-color: var(--blue-light);
}

.conv-item-title {
    display: block;
    font-size: 13px;
    font-weight: 500;
    color: rgba(255, 255, 255, 0.9);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    line-height: 1.3;
}

.conv-item-meta {
    display: flex;
    align-items: center;
    gap: 8px;
    margin-top: 3px;
}

.conv-item-time {
    font-size: 11px;
    color: rgba(255, 255, 255, 0.4);
}

.conv-item-count {
    font-size: 11px;
    color: rgba(255, 255, 255, 0.3);
}

.conv-empty {
    padding: 24px 16px;
    text-align: center;
    color: rgba(255, 255, 255, 0.35);
    font-size: 13px;
    line-height: 1.6;
}

/* ── Sidebar backdrop (mobile overlay) ─────────────────────────────── */

.sidebar-backdrop {
    display: none;
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.5);
    z-index: 15;
}

.sidebar-backdrop.visible {
    display: block;
}

/* ── Context menu ──────────────────────────────────────────────────── */

.context-menu {
    display: none;
    position: fixed;
    z-index: 100;
    background: var(--white);
    border: 1px solid var(--gray-200);
    border-radius: 8px;
    box-shadow: var(--shadow-lg);
    padding: 4px;
    min-width: 140px;
}

.context-menu.visible {
    display: block;
}

.context-menu-item {
    display: block;
    width: 100%;
    padding: 8px 12px;
    font-size: 13px;
    font-family: inherit;
    color: var(--gray-700);
    background: none;
    border: none;
    border-radius: 4px;
    cursor: pointer;
    text-align: left;
}

.context-menu-item:hover {
    background: var(--gray-100);
}

.context-menu-danger {
    color: var(--red-500);
}

.context-menu-danger:hover {
    background: var(--red-50);
}

/* ── Main content ──────────────────────────────────────────────────── */

.main-content {
    flex: 1;
    display: flex;
    flex-direction: column;
    min-width: 0;
    height: 100vh;
    max-height: 100vh;
    overflow: hidden;
    transition: margin-left 0.25s ease;
}

/* ── Header ─────────────────────────────────────────────────────────── */

.header {
    background: var(--navy);
    color: var(--white);
    padding: 14px 24px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    flex-shrink: 0;
    box-shadow: var(--shadow-md);
    z-index: 10;
}

.header-left {
    display: flex;
    align-items: center;
    gap: 12px;
    min-width: 0;
}

.sidebar-toggle {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 36px;
    height: 36px;
    background: none;
    border: none;
    color: rgba(255, 255, 255, 0.7);
    cursor: pointer;
    border-radius: 6px;
    transition: background 0.15s, color 0.15s;
    flex-shrink: 0;
}

.sidebar-toggle:hover {
    background: rgba(255, 255, 255, 0.1);
    color: var(--white);
}

.header-title {
    font-size: 18px;
    font-weight: 600;
    letter-spacing: -0.01em;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    cursor: default;
}

/* WHY: Editable title uses contenteditable — these styles make the
   edit state visually distinct so the user knows they're editing. */
.header-title.editable {
    cursor: text;
    border-bottom: 1px dashed rgba(255, 255, 255, 0.4);
    padding-bottom: 1px;
}

.header-title.editing {
    background: rgba(255, 255, 255, 0.1);
    border-radius: 4px;
    padding: 2px 6px;
    outline: none;
    border-bottom: none;
}

.header h1 {
    font-size: 18px;
    font-weight: 600;
    letter-spacing: -0.01em;
}

.header-right {
    display: flex;
    align-items: center;
    gap: 12px;
    flex-shrink: 0;
}

.role-badge {
    font-size: 11px;
    font-weight: 600;
    color: var(--white);
    padding: 3px 10px;
    border-radius: 12px;
    letter-spacing: 0.03em;
    text-transform: uppercase;
    /* WHY: Background color is set dynamically via JS based on role */
}

.signout-btn {
    font-size: 12px;
    font-weight: 500;
    font-family: inherit;
    color: rgba(255, 255, 255, 0.7);
    background: none;
    border: 1px solid rgba(255, 255, 255, 0.2);
    border-radius: 6px;
    padding: 4px 10px;
    cursor: pointer;
    transition: color 0.15s, border-color 0.15s;
}

.signout-btn:hover {
    color: var(--white);
    border-color: rgba(255, 255, 255, 0.5);
}

/* ── Chat area ──────────────────────────────────────────────────────── */

.chat-area {
    flex: 1;
    overflow-y: auto;
    padding: 24px 16px;
    scroll-behavior: smooth;
}

.chat-area::-webkit-scrollbar {
    width: 6px;
}

.chat-area::-webkit-scrollbar-track {
    background: transparent;
}

.chat-area::-webkit-scrollbar-thumb {
    background: var(--gray-300);
    border-radius: 3px;
}

/* ── Messages ───────────────────────────────────────────────────────── */

.messages {
    max-width: 800px;
    margin: 0 auto;
    display: flex;
    flex-direction: column;
    gap: 16px;
}

.message {
    display: flex;
    flex-direction: column;
    animation: messageIn 0.3s ease-out;
}

@keyframes messageIn {
    from {
        opacity: 0;
        transform: translateY(12px);
    }
    to {
        opacity: 1;
        transform: translateY(0);
    }
}

.message.user {
    align-items: flex-end;
}

.message.assistant {
    align-items: flex-start;
}

.message-bubble {
    max-width: 85%;
    padding: 12px 16px;
    border-radius: 16px;
    word-wrap: break-word;
    overflow-wrap: break-word;
}

.message.user .message-bubble {
    background: var(--blue);
    color: var(--white);
    border-bottom-right-radius: 4px;
}

.message.assistant .message-bubble {
    background: var(--gray-100);
    color: var(--gray-800);
    border-bottom-left-radius: 4px;
}

.message.error .message-bubble {
    background: var(--red-50);
    color: var(--red-500);
    border: 1px solid var(--red-500);
}

/* WHY: Messages loaded from history don't need the slide-in animation
   since they appear all at once when loading a conversation. */
.message.no-animate {
    animation: none;
}

/* ── Access Denied Messages ─────────────────────────────────────────── */
/* WHY: Visually distinct from normal messages so users immediately understand
   this is an access restriction, not an error or a normal answer. */

.message.access-denied .message-bubble {
    background: var(--amber-50);
    color: var(--amber-700);
    border: 1px solid var(--amber-200);
    border-left: 4px solid var(--amber-700);
    border-bottom-left-radius: 4px;
}

.lock-icon {
    font-size: 1.1em;
    margin-right: 4px;
}

/* ── Markdown content inside assistant messages ─────────────────────── */

.message-bubble h1,
.message-bubble h2,
.message-bubble h3,
.message-bubble h4 {
    margin-top: 12px;
    margin-bottom: 6px;
    font-weight: 600;
    line-height: 1.3;
}

.message-bubble h1 { font-size: 1.3em; }
.message-bubble h2 { font-size: 1.15em; }
.message-bubble h3 { font-size: 1.05em; }

.message-bubble h1:first-child,
.message-bubble h2:first-child,
.message-bubble h3:first-child {
    margin-top: 0;
}

.message-bubble p {
    margin-bottom: 8px;
}

.message-bubble p:last-child {
    margin-bottom: 0;
}

.message-bubble strong {
    font-weight: 600;
}

.message-bubble code {
    font-family: 'SF Mono', SFMono-Regular, Consolas, 'Liberation Mono', Menlo, monospace;
    font-size: 0.88em;
    background: rgba(0, 0, 0, 0.06);
    padding: 2px 5px;
    border-radius: 4px;
}

.message.user .message-bubble code {
    background: rgba(255, 255, 255, 0.2);
}

.message-bubble pre {
    margin: 8px 0;
    border-radius: 8px;
    overflow-x: auto;
    background: var(--code-bg);
    padding: 12px 16px;
}

.message-bubble pre code {
    background: none;
    padding: 0;
    color: #e2e8f0;
    font-size: 0.85em;
    line-height: 1.5;
}

/* SQL keywords highlighted in code blocks */
.message-bubble pre code .sql-keyword {
    color: #93c5fd;
    font-weight: 600;
}

.message-bubble ul,
.message-bubble ol {
    margin: 6px 0;
    padding-left: 24px;
}

.message-bubble li {
    margin-bottom: 4px;
}

/* Tables in messages */
.message-bubble table {
    border-collapse: collapse;
    margin: 8px 0;
    font-size: 0.9em;
    width: 100%;
    overflow-x: auto;
    display: block;
}

.message-bubble table th,
.message-bubble table td {
    border: 1px solid var(--gray-300);
    padding: 6px 10px;
    text-align: left;
    white-space: nowrap;
}

.message-bubble table th {
    background: var(--gray-200);
    font-weight: 600;
}

.message-bubble table tr:nth-child(even) {
    background: var(--gray-50);
}

/* ── SQL viewer collapsible ─────────────────────────────────────────── */

.sql-viewer {
    max-width: 85%;
    margin-top: 4px;
}

.sql-toggle {
    font-size: 12px;
    color: var(--gray-500);
    background: none;
    border: none;
    cursor: pointer;
    padding: 4px 8px;
    border-radius: 4px;
    font-family: inherit;
}

.sql-toggle:hover {
    background: var(--gray-100);
    color: var(--gray-700);
}

.sql-content {
    display: none;
    margin-top: 4px;
}

.sql-content.open {
    display: block;
}

.sql-content pre {
    background: var(--code-bg);
    color: #e2e8f0;
    padding: 10px 14px;
    border-radius: 8px;
    font-size: 0.82em;
    overflow-x: auto;
    font-family: 'SF Mono', SFMono-Regular, Consolas, 'Liberation Mono', Menlo, monospace;
    line-height: 1.5;
}

/* ── Thinking indicator ─────────────────────────────────────────────── */

.thinking {
    display: flex;
    align-items: center;
    gap: 4px;
    padding: 12px 16px;
    animation: messageIn 0.3s ease-out;
}

.thinking-dot {
    width: 8px;
    height: 8px;
    background: var(--gray-400);
    border-radius: 50%;
    animation: bounce 1.4s ease-in-out infinite;
}

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

.thinking-dot:nth-child(3) {
    animation-delay: 0.32s;
}

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

/* ── Input area ─────────────────────────────────────────────────────── */

.input-area {
    flex-shrink: 0;
    padding: 16px;
    background: var(--white);
    border-top: 1px solid var(--gray-200);
    box-shadow: 0 -2px 8px rgba(0, 0, 0, 0.04);
}

.input-wrapper {
    max-width: 800px;
    margin: 0 auto;
    display: flex;
    gap: 10px;
    align-items: center;
}

.input-wrapper input {
    flex: 1;
    padding: 12px 18px;
    border: 1px solid var(--gray-300);
    border-radius: 24px;
    font-size: 15px;
    font-family: inherit;
    outline: none;
    transition: border-color 0.15s;
    background: var(--gray-50);
}

.input-wrapper input:focus {
    border-color: var(--blue);
    background: var(--white);
    box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.1);
}

.input-wrapper input::placeholder {
    color: var(--gray-400);
}

.send-btn {
    width: 44px;
    height: 44px;
    border-radius: 50%;
    border: none;
    background: var(--blue);
    color: var(--white);
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
    transition: background 0.15s;
}

.send-btn:hover {
    background: var(--blue-light);
}

.send-btn:disabled {
    background: var(--gray-300);
    cursor: not-allowed;
}

.send-btn svg {
    width: 20px;
    height: 20px;
}

/* ── Welcome message ────────────────────────────────────────────────── */

.welcome {
    text-align: center;
    color: var(--gray-500);
    padding: 40px 20px;
    max-width: 500px;
    margin: 0 auto;
}

.welcome h2 {
    color: var(--navy);
    font-size: 20px;
    margin-bottom: 12px;
    font-weight: 600;
}

.welcome p {
    font-size: 14px;
    line-height: 1.7;
    margin-bottom: 8px;
}

.welcome .try-it {
    margin-top: 16px;
    padding: 10px 16px;
    background: var(--gray-50);
    border: 1px solid var(--gray-200);
    border-radius: 10px;
    font-size: 13px;
    color: var(--gray-700);
    cursor: pointer;
    transition: background 0.15s;
}

.welcome .try-it:hover {
    background: var(--gray-100);
}

/* ── Responsive ─────────────────────────────────────────────────────── */

@media (max-width: 768px) {
    /* WHY: On mobile, the sidebar overlays the content instead of pushing it.
       This avoids squeezing the chat area to an unusable width. */
    .sidebar {
        position: fixed;
        left: 0;
        top: 0;
        z-index: 25;
        transition: transform 0.25s ease;
        transform: translateX(0);
    }

    .sidebar.collapsed {
        margin-left: 0;
        transform: translateX(calc(-1 * var(--sidebar-width)));
    }

    .header {
        padding: 12px 16px;
    }

    .header-title {
        font-size: 16px;
    }

    .chat-area {
        padding: 16px 12px;
    }

    .message-bubble {
        max-width: 92%;
    }

    .sql-viewer {
        max-width: 92%;
    }

    .input-area {
        padding: 12px;
    }

    .input-wrapper input {
        padding: 10px 14px;
        font-size: 16px; /* WHY: Prevents iOS zoom on input focus */
    }

    .login-card {
        margin: 16px;
        padding: 36px 28px;
    }

    .pin-input {
        font-size: 20px;
    }
}

/* ── Print ──────────────────────────────────────────────────────────── */

@media print {
    .header,
    .input-area,
    .sql-viewer,
    .thinking,
    .login-overlay,
    .sidebar,
    .sidebar-backdrop {
        display: none !important;
    }

    .chat-area {
        overflow: visible;
    }

    .message-bubble {
        max-width: 100%;
        break-inside: avoid;
    }
}
