/* ============================================================
   MedAbbrev AI — Core Stylesheet
   ============================================================ */

:root {
  --color-bg: #f7f9fa;
  --color-surface: #ffffff;
  --color-border: #e3e8ec;
  --color-text: #1b2430;
  --color-text-muted: #64748b;
  --color-primary: #0e7c86;      /* teal */
  --color-primary-dark: #0a5f67;
  --color-primary-light: #e3f4f5;
  --color-accent: #2f6fed;       /* blue accent */
  --color-success: #1f9254;
  --color-danger: #d1453b;
  --color-warning: #c47d12;
  --radius-sm: 8px;
  --radius-md: 14px;
  --radius-lg: 20px;
  --shadow-sm: 0 1px 2px rgba(16, 24, 32, 0.06);
  --shadow-md: 0 4px 16px rgba(16, 24, 32, 0.08);
  --sidebar-width: 264px;
  --sidebar-width-collapsed: 72px;
  --header-height: 64px;
  --font-sans: "Inter", -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
}

* { box-sizing: border-box; }

html, body {
  height: 100%;
  margin: 0;
  padding: 0;
}

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

a { color: inherit; text-decoration: none; }
button { font-family: inherit; cursor: pointer; }
input, textarea, select { font-family: inherit; }
.hidden { display: none !important; }

/* ============================================================
   APP SHELL — fills viewport, no page-level scroll
   ============================================================ */
.app-shell {
  display: flex;
  height: 100dvh;
  width: 100%;
  overflow: hidden;
}

/* ============================================================
   SIDEBAR
   ============================================================ */
.sidebar {
  width: var(--sidebar-width);
  flex-shrink: 0;
  background: var(--color-surface);
  border-right: 1px solid var(--color-border);
  display: flex;
  flex-direction: column;
  transition: width 0.2s ease, transform 0.2s ease;
  min-height: 0;
  z-index: 40;
}

.sidebar.collapsed { width: var(--sidebar-width-collapsed); }
.sidebar.collapsed .nav-label,
.sidebar.collapsed .sidebar-user-info,
.sidebar.collapsed .brand-text { display: none; }
.sidebar.collapsed .brand { justify-content: center; }
.sidebar.collapsed .nav-link { justify-content: center; }

.sidebar-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 18px 16px;
  border-bottom: 1px solid var(--color-border);
  flex-shrink: 0;
}

.brand { display: flex; align-items: center; gap: 10px; font-weight: 700; font-size: 16px; }
.brand-logo { font-size: 22px; }
.icon-btn {
  background: transparent;
  border: none;
  border-radius: var(--radius-sm);
  padding: 6px;
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--color-text-muted);
}
.icon-btn:hover { background: var(--color-primary-light); color: var(--color-primary-dark); }

.sidebar-new-chat {
  margin: 12px 16px;
  padding: 10px 14px;
  border-radius: var(--radius-sm);
  background: var(--color-primary);
  color: #fff;
  border: none;
  font-weight: 600;
  font-size: 14px;
  display: flex;
  align-items: center;
  gap: 8px;
  justify-content: center;
}
.sidebar-new-chat:hover { background: var(--color-primary-dark); }

.sidebar-nav {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  padding: 4px 10px;
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.nav-link {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 12px;
  border-radius: var(--radius-sm);
  color: var(--color-text);
  font-size: 14px;
  font-weight: 500;
}
.nav-link:hover { background: var(--color-primary-light); }
.nav-link.active { background: var(--color-primary-light); color: var(--color-primary-dark); font-weight: 600; }
.nav-icon { font-size: 18px; width: 22px; text-align: center; flex-shrink: 0; }

.sidebar-footer {
  border-top: 1px solid var(--color-border);
  padding: 12px 16px;
  flex-shrink: 0;
  display: flex;
  align-items: center;
  gap: 10px;
}
.sidebar-user-avatar {
  width: 34px; height: 34px;
  border-radius: 50%;
  background: var(--color-primary);
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 700;
  font-size: 14px;
  flex-shrink: 0;
  overflow: hidden;
}
.sidebar-user-avatar img { width: 100%; height: 100%; object-fit: cover; }
.sidebar-user-info { min-width: 0; flex: 1; }
.sidebar-user-info .name { font-size: 13px; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.sidebar-user-info .email { font-size: 12px; color: var(--color-text-muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

.sidebar-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(10, 15, 20, 0.4);
  z-index: 30;
}
.sidebar-overlay.visible { display: block; }

/* ============================================================
   MAIN AREA
   ============================================================ */
.main-area {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  height: 100%;
  min-height: 0;
}

.main-header {
  height: var(--header-height);
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 0 20px;
  border-bottom: 1px solid var(--color-border);
  background: var(--color-surface);
  z-index: 10;
}

.main-header-left { display: flex; align-items: center; gap: 12px; min-width: 0; }
.main-header-title { font-weight: 700; font-size: 15px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.main-header-subtitle { font-size: 12px; color: var(--color-text-muted); }
.main-header-right { display: flex; align-items: center; gap: 8px; flex-shrink: 0; }

.mobile-menu-btn { display: none; }

.btn {
  padding: 8px 14px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--color-border);
  background: var(--color-surface);
  font-size: 13px;
  font-weight: 600;
  color: var(--color-text);
  display: inline-flex;
  align-items: center;
  gap: 6px;
}
.btn:hover { background: var(--color-primary-light); border-color: var(--color-primary); }
.btn-primary { background: var(--color-primary); color: #fff; border-color: var(--color-primary); }
.btn-primary:hover { background: var(--color-primary-dark); }
.btn:disabled { opacity: 0.5; cursor: not-allowed; }
.btn-sm { padding: 6px 10px; font-size: 12px; }

.select {
  padding: 7px 10px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--color-border);
  background: var(--color-surface);
  font-size: 13px;
}

/* ============================================================
   CHAT LAYOUT — the critical part
   ============================================================ */
.chat-container {
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
  overflow: hidden; /* page itself never scrolls */
}

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

.chat-inner {
  max-width: 820px;
  margin: 0 auto;
  padding: 0 24px;
  display: flex;
  flex-direction: column;
  gap: 20px;
}

.msg-row { display: flex; gap: 12px; align-items: flex-start; }
.msg-row.user { flex-direction: row-reverse; }
.msg-avatar {
  width: 32px; height: 32px; border-radius: 50%;
  flex-shrink: 0;
  display: flex; align-items: center; justify-content: center;
  font-size: 15px; font-weight: 700; color: #fff;
}
.msg-avatar.ai { background: var(--color-primary); }
.msg-avatar.user { background: var(--color-accent); }

.msg-bubble {
  max-width: 78%;
  padding: 12px 16px;
  border-radius: var(--radius-md);
  line-height: 1.6;
  font-size: 14.5px;
}
.msg-row.user .msg-bubble { background: var(--color-primary); color: #fff; border-bottom-right-radius: 4px; }
.msg-row.ai .msg-bubble { background: var(--color-surface); border: 1px solid var(--color-border); border-bottom-left-radius: 4px; }

.msg-bubble p { margin: 0 0 10px; }
.msg-bubble p:last-child { margin-bottom: 0; }
.msg-bubble ul, .msg-bubble ol { margin: 8px 0; padding-left: 22px; }
.msg-bubble table { border-collapse: collapse; width: 100%; margin: 10px 0; font-size: 13.5px; }
.msg-bubble th, .msg-bubble td { border: 1px solid var(--color-border); padding: 6px 10px; text-align: left; }
.msg-bubble th { background: var(--color-primary-light); }
.msg-bubble code { background: rgba(0,0,0,0.06); padding: 1px 5px; border-radius: 4px; font-size: 13px; }
.msg-bubble pre { background: #1b2430; color: #e3e8ec; padding: 12px; border-radius: var(--radius-sm); overflow-x: auto; }
.msg-bubble pre code { background: none; color: inherit; }

.typing-indicator { display: flex; gap: 4px; padding: 6px 2px; }
.typing-dot { width: 7px; height: 7px; border-radius: 50%; background: var(--color-text-muted); animation: typingBounce 1.2s infinite ease-in-out; }
.typing-dot:nth-child(2) { animation-delay: 0.15s; }
.typing-dot:nth-child(3) { animation-delay: 0.3s; }
@keyframes typingBounce { 0%, 60%, 100% { transform: translateY(0); opacity: 0.5; } 30% { transform: translateY(-4px); opacity: 1; } }

.scroll-to-latest {
  position: absolute;
  bottom: 110px;
  left: 50%;
  transform: translateX(-50%);
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  box-shadow: var(--shadow-md);
  border-radius: 999px;
  padding: 7px 14px;
  font-size: 12.5px;
  font-weight: 600;
  display: none;
  align-items: center;
  gap: 6px;
  z-index: 15;
}
.scroll-to-latest.visible { display: inline-flex; }

/* Starter prompts */
.starter-wrap {
  max-width: 820px;
  margin: 0 auto;
  padding: 40px 24px;
  text-align: center;
}
.starter-wrap h1 { font-size: 26px; margin-bottom: 6px; }
.starter-wrap p { color: var(--color-text-muted); margin-bottom: 28px; }
.starter-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 12px;
  text-align: left;
}
.starter-card {
  border: 1px solid var(--color-border);
  background: var(--color-surface);
  border-radius: var(--radius-md);
  padding: 14px 16px;
  font-size: 13.5px;
  font-weight: 500;
}
.starter-card:hover { border-color: var(--color-primary); background: var(--color-primary-light); }

/* ============================================================
   CHAT INPUT — fixed at bottom
   ============================================================ */
.chat-input-area {
  flex-shrink: 0;
  border-top: 1px solid var(--color-border);
  background: linear-gradient(to top, var(--color-bg) 60%, rgba(247,249,250,0));
  padding: 14px 24px 18px;
}
.chat-input-inner {
  max-width: 820px;
  margin: 0 auto;
  display: flex;
  align-items: flex-end;
  gap: 10px;
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-lg);
  padding: 8px 8px 8px 16px;
  box-shadow: var(--shadow-sm);
}
.chat-input-inner:focus-within { border-color: var(--color-primary); box-shadow: 0 0 0 3px var(--color-primary-light); }

#chat-textarea {
  flex: 1;
  border: none;
  outline: none;
  resize: none;
  background: transparent;
  font-size: 14.5px;
  line-height: 1.5;
  max-height: 160px;
  min-height: 24px;
  padding: 8px 0;
  color: var(--color-text);
}

.send-btn {
  width: 38px; height: 38px;
  border-radius: 50%;
  border: none;
  background: var(--color-primary);
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  font-size: 16px;
}
.send-btn:disabled { background: var(--color-border); color: var(--color-text-muted); }

.chat-hint { text-align: center; font-size: 11.5px; color: var(--color-text-muted); margin-top: 8px; }

/* ============================================================
   GENERIC PAGE CONTENT (dictionary, practice, profile, sections)
   ============================================================ */
.page-scroll {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
}
.page-inner { max-width: 1040px; margin: 0 auto; padding: 24px; }

.card {
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-md);
  padding: 18px;
  box-shadow: var(--shadow-sm);
}

.grid-2 { display: grid; grid-template-columns: repeat(2, 1fr); gap: 16px; }
.grid-3 { display: grid; grid-template-columns: repeat(3, 1fr); gap: 16px; }

.toolbar { display: flex; gap: 10px; flex-wrap: wrap; align-items: center; margin-bottom: 18px; }
.search-input {
  flex: 1;
  min-width: 180px;
  padding: 10px 14px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--color-border);
  font-size: 14px;
}

.abbr-table { width: 100%; border-collapse: collapse; }
.abbr-table th, .abbr-table td { padding: 10px 12px; border-bottom: 1px solid var(--color-border); text-align: left; font-size: 13.5px; }
.abbr-table th { color: var(--color-text-muted); font-weight: 600; font-size: 12px; text-transform: uppercase; letter-spacing: 0.04em; }
.abbr-table tr { cursor: pointer; }
.abbr-table tr:hover { background: var(--color-primary-light); }
.tag { display: inline-block; padding: 2px 9px; border-radius: 999px; background: var(--color-primary-light); color: var(--color-primary-dark); font-size: 11.5px; font-weight: 600; }

.modal-backdrop {
  position: fixed; inset: 0; background: rgba(10,15,20,0.45);
  display: flex; align-items: center; justify-content: center;
  z-index: 100; padding: 20px;
}
.modal {
  background: var(--color-surface);
  border-radius: var(--radius-lg);
  max-width: 560px; width: 100%;
  max-height: 85vh;
  overflow-y: auto;
  padding: 24px;
  box-shadow: var(--shadow-md);
}
.modal h2 { margin-top: 0; }
.modal-close { float: right; background: none; border: none; font-size: 20px; color: var(--color-text-muted); }

.section-grid { display: grid; grid-template-columns: repeat(2, 1fr); gap: 14px; }
.section-card {
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-md);
  padding: 18px;
  cursor: pointer;
  transition: border-color 0.15s, transform 0.1s;
}
.section-card:hover { border-color: var(--color-primary); transform: translateY(-1px); }
.section-card .icon { font-size: 26px; margin-bottom: 8px; }
.section-card h3 { margin: 0 0 6px; font-size: 15px; }
.section-card p { margin: 0; font-size: 13px; color: var(--color-text-muted); }

/* Flashcards */
.flashcard-stage { display: flex; flex-direction: column; align-items: center; gap: 18px; padding: 10px 0 30px; }
.flashcard {
  width: 100%; max-width: 480px; height: 260px;
  perspective: 1200px;
}
.flashcard-inner {
  position: relative; width: 100%; height: 100%;
  transition: transform 0.5s;
  transform-style: preserve-3d;
}
.flashcard.flipped .flashcard-inner { transform: rotateY(180deg); }
.flashcard-face {
  position: absolute; inset: 0;
  backface-visibility: hidden;
  border-radius: var(--radius-lg);
  border: 1px solid var(--color-border);
  background: var(--color-surface);
  box-shadow: var(--shadow-md);
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  padding: 24px; text-align: center; gap: 8px;
}
.flashcard-face.back { transform: rotateY(180deg); }
.flashcard-face .abbr-big { font-size: 38px; font-weight: 800; color: var(--color-primary); }
.flashcard-controls { display: flex; gap: 10px; align-items: center; }

/* Exercises */
.question-card { margin-bottom: 18px; }
.option-btn {
  display: block; width: 100%; text-align: left;
  padding: 12px 14px; margin-bottom: 8px;
  border: 1px solid var(--color-border); border-radius: var(--radius-sm);
  background: var(--color-surface); font-size: 14px;
}
.option-btn:hover { border-color: var(--color-primary); }
.option-btn.correct { border-color: var(--color-success); background: #e9f8ef; }
.option-btn.incorrect { border-color: var(--color-danger); background: #fcebea; }
.option-btn:disabled { cursor: default; }
.explanation-box { margin-top: 10px; padding: 12px 14px; border-radius: var(--radius-sm); background: var(--color-primary-light); font-size: 13.5px; }
.score-bar { font-weight: 700; color: var(--color-primary-dark); }

/* Profile */
.profile-header { display: flex; align-items: center; gap: 18px; margin-bottom: 24px; }
.profile-avatar { width: 72px; height: 72px; border-radius: 50%; background: var(--color-primary); color: #fff; display: flex; align-items: center; justify-content: center; font-size: 28px; font-weight: 700; overflow: hidden; }
.profile-avatar img { width: 100%; height: 100%; object-fit: cover; }
.profile-field { display: flex; justify-content: space-between; padding: 12px 0; border-bottom: 1px solid var(--color-border); font-size: 14px; }
.profile-field .label { color: var(--color-text-muted); }

/* ============================================================
   AUTH PAGE
   ============================================================ */
.auth-shell {
  min-height: 100dvh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
  background: linear-gradient(145deg, #f7f9fa 0%, #e3f4f5 100%);
}
.auth-card {
  width: 100%; max-width: 400px;
  background: var(--color-surface);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-md);
  padding: 32px 28px;
}
.auth-logo { text-align: center; font-size: 30px; margin-bottom: 6px; }
.auth-title { text-align: center; font-weight: 700; font-size: 19px; margin-bottom: 2px; }
.auth-subtitle { text-align: center; color: var(--color-text-muted); font-size: 13px; margin-bottom: 22px; }
.form-group { margin-bottom: 14px; }
.form-group label { display: block; font-size: 13px; font-weight: 600; margin-bottom: 6px; }
.form-group input {
  width: 100%; padding: 10px 12px;
  border: 1px solid var(--color-border); border-radius: var(--radius-sm);
  font-size: 14px;
}
.form-group input:focus { outline: none; border-color: var(--color-primary); box-shadow: 0 0 0 3px var(--color-primary-light); }
.btn-block { width: 100%; justify-content: center; padding: 11px; font-size: 14px; }
.auth-divider { display: flex; align-items: center; gap: 10px; margin: 18px 0; color: var(--color-text-muted); font-size: 12px; }
.auth-divider::before, .auth-divider::after { content: ""; flex: 1; height: 1px; background: var(--color-border); }
.auth-switch { text-align: center; font-size: 13px; margin-top: 16px; color: var(--color-text-muted); }
.auth-switch a { color: var(--color-primary); font-weight: 600; }
.auth-message { margin-bottom: 14px; padding: 10px 12px; border-radius: var(--radius-sm); font-size: 13px; display: none; }
.auth-message.error { background: #fcebea; color: var(--color-danger); }
.auth-message.success { background: #e9f8ef; color: var(--color-success); }
.forgot-link { font-size: 12.5px; text-align: right; display: block; margin-top: -8px; margin-bottom: 14px; color: var(--color-primary); }

/* ============================================================
   RESPONSIVE
   ============================================================ */
@media (max-width: 900px) {
  .sidebar {
    position: fixed;
    left: 0; top: 0; bottom: 0;
    transform: translateX(-100%);
    box-shadow: var(--shadow-md);
  }
  .sidebar.open { transform: translateX(0); }
  .sidebar.collapsed { width: var(--sidebar-width); }
  .sidebar.collapsed .nav-label,
  .sidebar.collapsed .sidebar-user-info,
  .sidebar.collapsed .brand-text { display: block; }
  #sidebar-collapse-btn { display: none; }
  .mobile-menu-btn { display: flex; }
  .grid-2, .grid-3, .section-grid, .starter-grid { grid-template-columns: 1fr; }
  .msg-bubble { max-width: 88%; }
  .main-header { padding: 0 14px; }
  .chat-inner, .chat-input-inner, .page-inner, .starter-wrap { padding-left: 14px; padding-right: 14px; }
}

@supports (padding: max(0px)) {
  .auth-shell { padding-top: max(20px, env(safe-area-inset-top)); padding-bottom: max(20px, env(safe-area-inset-bottom)); }
}
