/* ============================================
   LabGPT — Stanford Identity + Chat UI
   ============================================ */

:root {
  /* Stanford Identity Colors */
  --stanford-cardinal: #8C1515;
  --stanford-cardinal-light: #B83A4B;
  --stanford-cardinal-dark: #820000;
  --stanford-digital-red: #B1040E;
  --stanford-digital-red-light: #E50808;
  --stanford-digital-blue: #006CB8;
  --stanford-digital-blue-light: #6FC3FF;
  --stanford-digital-blue-dark: #00548f;
  --stanford-digital-green: #008566;
  --stanford-black: #2E2D29;
  --stanford-cool-grey: #53565A;
  --stanford-grey-80: #585754;
  --stanford-grey-60: #767674;
  --stanford-grey-40: #ABABA9;
  --stanford-grey-20: #D5D5D4;
  --stanford-grey-10: #EAEAEA;
  --stanford-white: #FFFFFF;

  /* App theme mapped to Stanford palette */
  --color-primary: var(--stanford-cardinal);
  --color-primary-hover: var(--stanford-cardinal-dark);
  --color-accent: var(--stanford-digital-red);
  --color-danger: var(--stanford-digital-red);
  --color-link: var(--stanford-digital-blue);
  --color-success: var(--stanford-digital-green);
  --color-bg: #F4F2F0;
  --color-surface: var(--stanford-white);
  --color-sidebar: var(--stanford-black);
  --color-sidebar-hover: #43423E;
  --color-sidebar-text: var(--stanford-grey-20);
  --color-text: var(--stanford-black);
  --color-text-light: var(--stanford-cool-grey);
  --color-border: var(--stanford-grey-20);
  --color-user-bubble: var(--stanford-cardinal);
  --color-user-text: var(--stanford-white);
  --color-ai-bubble: var(--stanford-grey-10);
  --color-ai-text: var(--stanford-black);
  --font: "Source Sans Pro", system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  --font-mono: "SF Mono", SFMono-Regular, ui-monospace, Menlo, monospace;
  --font-serif: "Source Serif Pro", Georgia, serif;
  --radius: 12px;
  --radius-sm: 8px;
  --shadow: 0 1px 3px rgba(0, 0, 0, 0.08);
  --shadow-lg: 0 4px 16px rgba(0, 0, 0, 0.12);
  --sidebar-width: 280px;
  --header-height: 56px;
}

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

html, body {
  height: 100%;
  overflow: hidden;
}

body {
  font-family: var(--font);
  font-size: 15px;
  color: var(--color-text);
  background: var(--color-bg);
  -webkit-font-smoothing: antialiased;
}

button {
  cursor: pointer;
  border: none;
  background: none;
  font-family: inherit;
  font-size: inherit;
}

a {
  color: var(--color-link);
  text-decoration: none;
}

/* ---- Focus Styles ---- */
*:focus-visible {
  outline: 2px solid var(--stanford-digital-blue);
  outline-offset: 2px;
}

*:focus:not(:focus-visible) {
  outline: none;
}

#header *:focus-visible {
  outline-color: var(--stanford-white);
}

.sidebar *:focus-visible {
  outline-color: var(--stanford-digital-blue-light);
}

/* ---- Utilities ---- */
.hidden {
  display: none !important;
}

.screen {
  width: 100%;
  height: 100%;
}

/* ============================================
   LOGIN SCREEN
   ============================================ */
#login-screen {
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, var(--stanford-cardinal-dark) 0%, var(--stanford-cardinal) 50%, var(--stanford-cardinal-light) 100%);
}

.login-card {
  background: var(--color-surface);
  padding: 48px 40px;
  border-radius: 16px;
  box-shadow: var(--shadow-lg);
  text-align: center;
  max-width: 400px;
  width: 90%;
}

.login-logo {
  color: var(--stanford-cardinal);
  margin-bottom: 16px;
}

.login-card h1 {
  font-size: 28px;
  font-weight: 700;
  color: var(--stanford-cardinal);
  margin-bottom: 4px;
}

.login-subtitle {
  color: var(--color-text-light);
  margin-bottom: 32px;
}

.google-btn {
  display: inline-flex;
  align-items: center;
  gap: 12px;
  padding: 12px 24px;
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-sm);
  font-size: 15px;
  font-weight: 500;
  color: var(--color-text);
  transition: background 0.15s, box-shadow 0.15s;
}

.google-btn:hover {
  background: #f8f6f4;
  box-shadow: var(--shadow);
}

.login-error {
  margin-top: 16px;
  color: var(--color-danger);
  font-size: 13px;
}

/* ============================================
   CHAT SCREEN LAYOUT
   ============================================ */
#chat-screen {
  display: flex;
  flex-direction: column;
}

/* ---- Header ---- */
#header {
  height: var(--header-height);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 16px;
  background: var(--stanford-cardinal);
  color: var(--stanford-white);
  flex-shrink: 0;
  z-index: 10;
}

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

.header-title {
  font-size: 18px;
  font-weight: 700;
  color: var(--stanford-white);
}

.header-right {
  display: flex;
  align-items: center;
  gap: 10px;
}

.avatar {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  object-fit: cover;
  border: 2px solid rgba(255, 255, 255, 0.3);
}

#user-name {
  font-size: 13px;
  color: rgba(255, 255, 255, 0.85);
}

/* ---- Buttons ---- */
.icon-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 44px;
  height: 44px;
  border-radius: var(--radius-sm);
  color: inherit;
  transition: background 0.15s, color 0.15s;
}

#header .icon-btn {
  color: rgba(255, 255, 255, 0.8);
}

#header .icon-btn:hover {
  background: rgba(255, 255, 255, 0.15);
  color: var(--stanford-white);
}

.chat-main .icon-btn {
  color: var(--color-text-light);
}

.chat-main .icon-btn:hover {
  background: var(--stanford-grey-10);
  color: var(--color-text);
}

.text-btn {
  padding: 6px 12px;
  border-radius: var(--radius-sm);
  font-size: 13px;
  font-weight: 500;
  color: rgba(255, 255, 255, 0.8);
  transition: background 0.15s, color 0.15s;
}

.text-btn:hover {
  background: rgba(255, 255, 255, 0.15);
  color: var(--stanford-white);
}

.text-btn.danger {
  color: var(--color-danger);
}

.text-btn.danger:hover {
  background: #fef2f2;
}

/* ---- App Body ---- */
.app-body {
  display: flex;
  flex: 1;
  overflow: hidden;
  height: calc(100% - var(--header-height));
}

/* ============================================
   SIDEBAR
   ============================================ */
.sidebar {
  width: var(--sidebar-width);
  background: var(--color-sidebar);
  display: flex;
  flex-direction: column;
  flex-shrink: 0;
  overflow: hidden;
  transition: margin-left 0.25s ease;
}

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

.new-chat-btn {
  display: flex;
  align-items: center;
  gap: 8px;
  margin: 12px;
  padding: 10px 16px;
  background: var(--stanford-cardinal);
  color: #fff;
  border-radius: var(--radius-sm);
  font-size: 14px;
  font-weight: 500;
  transition: background 0.15s;
}

.new-chat-btn:hover {
  background: var(--stanford-cardinal-light);
}

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

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

.chat-list::-webkit-scrollbar-thumb {
  background: #585754;
  border-radius: 2px;
}

.chat-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 12px;
  margin-bottom: 2px;
  border-radius: var(--radius-sm);
  border-left: 3px solid transparent;
  cursor: pointer;
  color: var(--color-sidebar-text);
  font-size: 13px;
  transition: background 0.15s;
}

.chat-item:hover {
  background: var(--color-sidebar-hover);
}

.chat-item.active {
  background: var(--color-sidebar-hover);
  color: #fff;
  border-left: 3px solid var(--stanford-cardinal-light);
}

.chat-item-title {
  flex: 1;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.chat-item .delete-btn {
  display: inline-flex;
  visibility: hidden;
  width: 24px;
  height: 24px;
  border-radius: 4px;
  color: var(--color-sidebar-text);
  flex-shrink: 0;
}

.chat-item:hover .delete-btn {
  visibility: visible;
}

.chat-item .delete-btn:hover {
  background: rgba(255, 255, 255, 0.1);
  color: var(--color-danger);
}

/* ============================================
   CHAT MAIN AREA
   ============================================ */
.chat-main {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  position: relative;
}

/* ---- Welcome ---- */
.welcome {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  text-align: center;
  padding: 40px;
  color: var(--color-text-light);
}

.welcome-icon {
  color: var(--stanford-cardinal);
  opacity: 0.35;
  margin-bottom: 20px;
}

.welcome h2 {
  font-size: 22px;
  color: var(--color-text);
  margin-bottom: 8px;
}

.welcome-new-chat-btn {
  margin-top: 24px;
}

/* ---- Messages ---- */
.messages {
  flex: 1;
  overflow-y: auto;
  padding: 24px 0;
  scroll-behavior: smooth;
}

.messages::-webkit-scrollbar {
  width: 6px;
}

.messages::-webkit-scrollbar-thumb {
  background: var(--stanford-grey-40);
  border-radius: 3px;
}

.message {
  display: flex;
  gap: 12px;
  padding: 8px 24px;
  max-width: 800px;
  margin: 0 auto 16px;
  width: 100%;
}

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

.message-avatar {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 12px;
  font-weight: 700;
  letter-spacing: 0.5px;
}

.message.assistant .message-avatar {
  background: var(--stanford-cardinal);
  color: #fff;
}

.message.user .message-avatar {
  background: var(--stanford-cool-grey);
  color: #fff;
}

.message-content {
  max-width: 75%;
  min-width: 0;
}

.message-bubble {
  padding: 12px 16px;
  border-radius: var(--radius);
  line-height: 1.6;
  word-wrap: break-word;
  overflow-wrap: break-word;
}

.message.assistant .message-bubble {
  background: var(--color-ai-bubble);
  color: var(--color-ai-text);
  border-top-left-radius: 4px;
}

.message.user .message-bubble {
  background: var(--color-user-bubble);
  color: var(--color-user-text);
  border-top-right-radius: 4px;
}

/* Markdown inside messages */
.message-bubble p {
  margin-bottom: 8px;
}

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

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

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

.message-bubble code {
  font-family: var(--font-mono);
  font-size: 13px;
  padding: 2px 6px;
  background: rgba(0, 0, 0, 0.06);
  border-radius: 4px;
}

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

.message-bubble pre {
  margin: 8px 0;
  padding: 12px 16px;
  background: var(--stanford-black);
  color: #e2e8f0;
  border-radius: var(--radius-sm);
  overflow-x: auto;
  font-size: 13px;
  line-height: 1.5;
}

.message-bubble pre code {
  background: none;
  padding: 0;
  color: inherit;
  font-size: inherit;
}

.message-bubble blockquote {
  border-left: 3px solid var(--stanford-cardinal-light);
  padding-left: 12px;
  margin: 8px 0;
  color: var(--color-text-light);
}

.message-bubble table {
  border-collapse: collapse;
  margin: 8px 0;
  font-size: 13px;
  width: 100%;
}

.message-bubble th,
.message-bubble td {
  border: 1px solid var(--color-border);
  padding: 6px 10px;
  text-align: left;
}

.message-bubble th {
  background: var(--color-bg);
  font-weight: 600;
}

.message-bubble h1,
.message-bubble h2,
.message-bubble h3 {
  margin: 12px 0 6px;
  font-weight: 600;
}

.message-bubble h1 { font-size: 18px; }
.message-bubble h2 { font-size: 16px; }
.message-bubble h3 { font-size: 15px; }

.message-bubble a {
  color: var(--color-link);
  text-decoration: underline;
}

.message.user .message-bubble a {
  color: #93c5fd;
}

/* ============================================
   ATTACHMENT CARDS (inline in messages)
   ============================================ */
.message-attachments {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-bottom: 8px;
}

.message-attachment-img {
  max-width: 240px;
  max-height: 180px;
  border-radius: var(--radius-sm);
  cursor: pointer;
  transition: opacity 0.15s;
}

.message-attachment-img:hover {
  opacity: 0.85;
}

.attachment-card {
  display: inline-flex;
  align-items: center;
  gap: 10px;
  padding: 10px 14px;
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-sm);
  text-decoration: none;
  color: var(--color-text);
  transition: background 0.15s, box-shadow 0.15s;
  max-width: 260px;
  box-shadow: var(--shadow);
}

.attachment-card:hover {
  background: #f8f6f4;
  box-shadow: var(--shadow-lg);
}

.attachment-card-icon {
  font-size: 22px;
  line-height: 1;
  flex-shrink: 0;
  width: 28px;
  text-align: center;
}

.attachment-card-info {
  display: flex;
  flex-direction: column;
  min-width: 0;
}

.attachment-card-name {
  font-size: 13px;
  font-weight: 500;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.attachment-card-type {
  font-size: 11px;
  color: var(--stanford-cool-grey);
  text-transform: uppercase;
  letter-spacing: 0.5px;
  font-weight: 500;
}

/* ============================================
   COPY BUTTON & MESSAGE ACTIONS
   ============================================ */
.message-actions {
  display: flex;
  gap: 4px;
  margin-top: 4px;
  opacity: 0;
  transition: opacity 0.15s;
}

.message:hover .message-actions,
.message:focus-within .message-actions {
  opacity: 1;
}

.copy-btn {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 4px 8px;
  border-radius: 4px;
  font-size: 12px;
  color: var(--color-text-light);
  transition: background 0.15s, color 0.15s;
  cursor: pointer;
}

.copy-btn:hover {
  background: var(--stanford-grey-10);
  color: var(--color-text);
}

.copy-btn svg {
  flex-shrink: 0;
}

/* Streaming cursor */
.streaming-cursor::after {
  content: "";
  display: inline-block;
  width: 7px;
  height: 16px;
  background: var(--color-text);
  margin-left: 2px;
  vertical-align: text-bottom;
  animation: blink 0.8s infinite;
}

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

/* Typing indicator */
.typing-indicator {
  display: flex;
  gap: 4px;
  padding: 12px 16px;
}

.typing-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--color-text-light);
  animation: typing 1.2s infinite;
}

.typing-dot:nth-child(2) { animation-delay: 0.2s; }
.typing-dot:nth-child(3) { animation-delay: 0.4s; }

@keyframes typing {
  0%, 60%, 100% { opacity: 0.3; transform: translateY(0); }
  30% { opacity: 1; transform: translateY(-4px); }
}

/* ============================================
   INPUT AREA
   ============================================ */
.input-area {
  padding: 12px 24px 20px;
  max-width: 800px;
  margin: 0 auto;
  width: 100%;
}

.input-row {
  display: flex;
  align-items: center;
  gap: 8px;
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius);
  padding: 4px 8px 4px 4px;
  box-shadow: var(--shadow);
  transition: border-color 0.15s;
}

.input-row:focus-within {
  border-color: var(--stanford-cardinal);
}

#message-input {
  flex: 1;
  border: none;
  outline: none;
  resize: none;
  font-family: var(--font);
  font-size: 15px;
  line-height: 1.5;
  padding: 4px 8px;
  max-height: 160px;
  background: transparent;
  color: var(--color-text);
}

#message-input::placeholder {
  color: var(--color-text-light);
}

.attach-btn {
  color: var(--color-text-light);
}

.send-btn {
  color: var(--stanford-cardinal);
}

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

/* ---- File previews (staging area) ---- */
.file-previews {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-bottom: 8px;
}

.file-previews:empty {
  display: none;
}

.file-preview {
  position: relative;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 6px 28px 6px 10px;
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-sm);
  font-size: 12px;
  color: var(--color-text);
}

.file-preview img {
  width: 36px;
  height: 36px;
  border-radius: 4px;
  object-fit: cover;
}

.file-preview-name {
  max-width: 120px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.file-preview .remove-file {
  position: absolute;
  top: -6px;
  right: -6px;
  width: 20px;
  height: 20px;
  border-radius: 50%;
  background: var(--color-text);
  color: #fff;
  font-size: 12px;
  display: flex;
  align-items: center;
  justify-content: center;
  line-height: 1;
}

.file-preview .remove-file:hover {
  background: var(--color-danger);
}

/* ============================================
   CONFIRM DIALOG
   ============================================ */
.dialog-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.4);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
}

.dialog {
  background: var(--color-surface);
  padding: 24px;
  border-radius: var(--radius);
  box-shadow: var(--shadow-lg);
  max-width: 360px;
  width: 90%;
}

.dialog p {
  margin-bottom: 20px;
  font-size: 15px;
}

.dialog-actions {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
}

.dialog-actions .text-btn {
  color: var(--color-text-light);
}

.dialog-actions .text-btn:hover {
  background: var(--stanford-grey-10);
  color: var(--color-text);
}

.dialog-actions .text-btn.danger {
  color: var(--color-danger);
}

.dialog-actions .text-btn.danger:hover {
  background: #fef2f2;
}

/* ============================================
   IMAGE LIGHTBOX
   ============================================ */
.lightbox-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.85);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 200;
  cursor: zoom-out;
}

.lightbox-overlay img {
  max-width: 90%;
  max-height: 90%;
  border-radius: var(--radius-sm);
}

/* ============================================
   TOAST NOTIFICATIONS
   ============================================ */
.toast {
  position: fixed;
  bottom: 24px;
  left: 50%;
  transform: translateX(-50%) translateY(100%);
  padding: 12px 24px;
  border-radius: var(--radius-sm);
  font-size: 14px;
  font-weight: 500;
  color: var(--stanford-white);
  background: var(--stanford-black);
  box-shadow: var(--shadow-lg);
  z-index: 300;
  opacity: 0;
  transition: transform 0.3s ease, opacity 0.3s ease;
  pointer-events: none;
}

.toast.visible {
  transform: translateX(-50%) translateY(0);
  opacity: 1;
}

.toast.error {
  background: var(--stanford-digital-red);
}

.toast.success {
  background: var(--stanford-digital-green);
}

/* ---- Upload Loading State ---- */
.file-previews.uploading {
  opacity: 0.6;
  pointer-events: none;
  position: relative;
}

.file-previews.uploading::after {
  content: "Uploading...";
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 13px;
  font-weight: 500;
  color: var(--color-text-light);
  background: rgba(255, 255, 255, 0.7);
  border-radius: var(--radius-sm);
}

/* ============================================
   RESPONSIVE
   ============================================ */
@media (max-width: 768px) {
  .sidebar {
    position: fixed;
    top: var(--header-height);
    left: 0;
    bottom: 0;
    z-index: 20;
    box-shadow: 4px 0 16px rgba(0, 0, 0, 0.2);
  }

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

  .message {
    padding: 8px 16px;
  }

  .message-content {
    max-width: 85%;
  }

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

  #user-name {
    display: none;
  }

  .message-actions {
    opacity: 1;
  }
}
