/* =========================================================
   Grace – Providence Christian College
   Brand: Providence Style Guide 2019
   ========================================================= */

/* ----- Variables ---------------------------------------- */
:root {
  --blue:           #004C97;   /* Providence Blue */
  --dark:           #041E42;   /* Providence Dark Blue */
  --red:            #DA291C;   /* Providence Red – alerts/errors only */
  --silver:         #B2B4B2;   /* borders, muted */
  --bg:             #F4F7FB;   /* page background */
  --surface:        #FFFFFF;
  --text-primary:   #041E42;
  --text-secondary: #6B7A8D;
  --border:         #D6DDE8;
}

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

html {
  font-size: 15px;
  line-height: 1.6;
  -webkit-text-size-adjust: 100%;
  height: 100%;
}

/* ----- Base --------------------------------------------- */
body {
  margin: 0;
  font-family: "Fira Sans", Calibri, sans-serif;
  font-size: 1rem;
  color: var(--text-primary);
  background: var(--bg);
  -webkit-font-smoothing: antialiased;
}

/* ----- Chat layout (index.html) ------------------------- */
body:not(.admin-page) {
  display: flex;
  flex-direction: column;
  align-items: center;
  height: 100svh; /* JS resize handler overrides this for keyboard handling */
}

.app-frame {
  display: flex;
  flex-direction: column;
  width: 100%;
  max-width: 520px;
  flex: 1;
  min-height: 0;
  background: var(--surface);
}

/* Subtle side border so the frame reads as a card on wide screens */
@media (min-width: 560px) {
  body:not(.admin-page) {
    background: var(--bg);
  }

  .app-frame {
    border-left: 1px solid var(--border);
    border-right: 1px solid var(--border);
  }
}

/* ----- Header ------------------------------------------- */
.app-header {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  height: 64px;
  padding: 0 16px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
  z-index: 10;
}

.header-logo {
  height: 40px;
  width: auto;
  display: block;
  max-width: calc(100vw - 160px); /* prevent overlap with nav link on narrow screens */
}

/* Back link sits at the right without shifting the centred logo */
.header-nav-link {
  position: absolute;
  right: 16px;
  font-size: 14px;
  color: var(--text-secondary);
  text-decoration: none;
  padding: 5px 12px;
  border: 1px solid var(--border);
  border-radius: 20px;
  line-height: 1.4;
  white-space: nowrap;
}

.header-nav-link:hover {
  color: var(--blue);
  border-color: var(--blue);
}

/* ----- Chat main --------------------------------------- */
.chat-main {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-height: 0; /* critical: allows flex children to shrink below content size */
}

/* ----- Messages ---------------------------------------- */
.messages {
  flex: 1;
  overflow-y: auto;
  padding: 20px 16px;
  display: flex;
  flex-direction: column;
  gap: 12px;
  min-height: 0;
  -webkit-overflow-scrolling: touch;
  overscroll-behavior: contain;
}

.message {
  display: flex;
  flex-direction: column;
  animation: msg-in 0.14s ease-out both;
}

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

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

/* Bot messages: row layout so the avatar sits to the left */
.assistant-message {
  flex-direction: row;
  align-items: flex-start;
  gap: 8px;
}

/* Grace avatar — aligned to the top of the bubble */
.assistant-message::before {
  content: '';
  display: block;
  width: 40px;
  height: 40px;
  border-radius: 50%;
  background: url('grace.png') center / cover no-repeat;
  flex-shrink: 0;
  margin-top: 2px;
}

/* ----- Message bubbles --------------------------------- */
.message-content {
  position: relative;
  max-width: 78%;
  padding: 10px 14px;
  border-radius: 18px;
  font-size: 1rem;
  line-height: 1.6;
  word-break: break-word;
}

.user-message .message-content {
  background: var(--blue);
  color: #fff;
  border-top-right-radius: 4px; /* flat top-right creates the "sent" tail corner */
}

/* Tail on user bubble — points up-right */
.user-message .message-content::after {
  content: '';
  position: absolute;
  top: 0;
  right: -8px;
  width: 0;
  height: 0;
  border-bottom: 8px solid transparent;
  border-left: 8px solid var(--blue);
}

.assistant-message .message-content {
  background: var(--surface);
  color: var(--text-primary);
  border: 1px solid var(--border);
  border-top-left-radius: 4px;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.07);
}

/* Tail — only on real bubbles, never on the thinking indicator */
.assistant-message .message-content:not(.thinking)::before {
  content: '';
  position: absolute;
  top: -1px;
  left: -9px;
  width: 0;
  height: 0;
  border-bottom: 9px solid transparent;
  border-right: 9px solid var(--border);
}

.assistant-message .message-content:not(.thinking)::after {
  content: '';
  position: absolute;
  top: 0;
  left: -8px;
  width: 0;
  height: 0;
  border-bottom: 8px solid transparent;
  border-right: 8px solid var(--surface);
}

/* ----- Thinking indicator ------------------------------ */
/* Strip the bubble — "Grace is typing…" floats beside the avatar */
.assistant-message .message-content.thinking {
  background: transparent;
  border-color: transparent;
  box-shadow: none;
  border-radius: 0;
  max-width: none;
  padding: 4px 0;
  font-size: 14px;
  font-style: italic;
  color: var(--text-secondary);
  animation: typing-pulse 1.6s ease-in-out infinite;
}

/* Align the row so the label sits at the bottom of Grace's avatar */
.assistant-message:has(.thinking) {
  align-items: flex-end;
}

@keyframes typing-pulse {
  0%, 100% { opacity: 0.4; }
  50%       { opacity: 1; }
}

/* ----- Rich text inside bubbles ------------------------ */
.message-content p {
  margin: 0 0 0.7em;
}

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

.message-content ul {
  margin: 0.5em 0 0.7em 1.15em;
  padding: 0;
}

.message-content li + li {
  margin-top: 0.25em;
}

.message-content h1,
.message-content h2,
.message-content h3,
.message-content h4,
.message-content h5,
.message-content h6 {
  margin: 0 0 0.5em;
  font-size: 1rem;
  font-weight: 600;
  line-height: 1.3;
}

.message-content code {
  font-family: "SFMono-Regular", Consolas, "Liberation Mono", Menlo, monospace;
  background: rgba(4, 30, 66, 0.06);
  padding: 0.1em 0.35em;
  border-radius: 4px;
  font-size: 0.9em;
}

.message-content a {
  color: inherit;
  text-decoration: underline;
}

/* ----- Empty / intro state (chat) ---------------------- */
.messages .empty-state {
  padding: 4px 0;
}

/* Hide once real messages appear */
.messages:has(.message) .empty-state {
  display: none;
}

/* Intro bubble: same layout as .assistant-message */
.grace-intro {
  display: flex;
  flex-direction: row;
  align-items: flex-start;
  gap: 8px;
}

.grace-intro-avatar {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  background: url('grace.png') center / cover no-repeat;
  flex-shrink: 0;
  margin-top: 2px;
}

.grace-intro-bubble {
  position: relative;
  max-width: 78%;
  padding: 10px 14px;
  border-radius: 18px;
  border-top-left-radius: 4px;
  font-size: 1rem;
  line-height: 1.6;
  background: var(--surface);
  color: var(--text-primary);
  border: 1px solid var(--border);
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.07);
}

/* Matching tail on intro bubble */
.grace-intro-bubble::before {
  content: '';
  position: absolute;
  top: -1px;
  left: -9px;
  width: 0;
  height: 0;
  border-bottom: 9px solid transparent;
  border-right: 9px solid var(--border);
}

.grace-intro-bubble::after {
  content: '';
  position: absolute;
  top: 0;
  left: -8px;
  width: 0;
  height: 0;
  border-bottom: 8px solid transparent;
  border-right: 8px solid var(--surface);
}

/* ----- Input bar --------------------------------------- */
.input-bar {
  flex-shrink: 0;
  padding: 10px 16px;
  padding-bottom: calc(10px + env(safe-area-inset-bottom, 0px)); /* iPhone home indicator */
  display: flex;
  align-items: center;
  background: var(--surface);
  border-top: 1px solid var(--border);
}

#chat-form {
  display: flex;
  align-items: center;
  gap: 10px;
  width: 100%;
}

#chat-input {
  flex: 1;
  height: 44px;
  padding: 10px 16px;
  border: 1px solid var(--border);
  border-radius: 24px;
  font: inherit;
  font-size: 16px; /* 16px minimum prevents iOS Safari from zooming on focus */
  color: var(--text-primary);
  background: var(--bg);
  outline: none;
  transition: border-color 0.12s;
  -webkit-appearance: none;
}

#chat-input:focus {
  border-color: var(--blue);
}

#chat-input::placeholder {
  color: var(--text-secondary);
}

#chat-input:disabled {
  opacity: 0.55;
  cursor: not-allowed;
}

#chat-submit {
  width: 38px;
  height: 38px;
  border-radius: 50%;
  border: none;
  background: var(--blue);
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  flex-shrink: 0;
  padding: 0;
  font: inherit;
  transition: background 0.12s;
}

#chat-submit:hover {
  background: #003d7a;
}

#chat-submit:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}

/* ----- Status indicator dot ---------------------------- */
#status-banner,
#admin-status-banner {
  position: fixed;
  right: 14px;
  bottom: 14px;
  z-index: 100;
  width: 11px;
  height: 11px;
  border-radius: 50%;
  background: var(--silver);
  box-shadow: 0 0 0 3px var(--surface), 0 1px 3px rgba(0, 0, 0, 0.08);
}

#status-banner[data-status="pending"],
#admin-status-banner[data-status="pending"] {
  background: var(--silver);
}

#status-banner[data-status="online"],
#admin-status-banner[data-status="online"] {
  background: var(--blue);
}

#status-banner[data-status="offline"],
#admin-status-banner[data-status="offline"] {
  background: var(--red);
}

#status-banner[data-clickable="true"] {
  cursor: pointer;
}

#status-banner[data-clickable="true"]:focus-visible {
  outline: 2px solid var(--blue);
  outline-offset: 3px;
}

/* =========================================================
   Admin page
   ========================================================= */

body.admin-page {
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
}

.admin-main {
  flex: 1;
  padding: 24px 16px 48px;
  max-width: 800px;
  width: 100%;
  margin: 0 auto;
}

/* ----- Admin panel card -------------------------------- */
.admin-panel {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  overflow: hidden;
}

.admin-panel-header {
  padding: 16px 20px;
  background: var(--bg);
  border-bottom: 1px solid var(--border);
}

.admin-panel-title {
  margin: 0 0 2px;
  font-size: 15px;
  font-weight: 600;
  color: var(--blue);
}

.admin-panel-note {
  margin: 0;
  font-size: 13px;
  color: var(--text-secondary);
  line-height: 1.5;
}

.panel-section {
  padding: 20px;
}

.panel-section + .panel-section {
  border-top: 1px solid var(--border);
}

.panel-section h3 {
  margin: 0 0 14px;
  font-size: 15px;
  font-weight: 600;
  color: var(--text-primary);
}

/* Summary / limits text */
.workspace-label {
  margin: 0 0 4px;
  font-size: 13px;
  font-weight: 600;
  color: var(--blue);
}

.workspace-note,
.panel-copy,
#documents-summary,
#documents-limits {
  margin: 0;
  font-size: 14px;
  color: var(--text-secondary);
  line-height: 1.5;
}

#documents-limits {
  margin-top: 4px;
}

/* ----- Forms (admin) ----------------------------------- */
.stack-form {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

#admin-password,
#admin-user-email,
#upload-tags,
#upload-files,
#upload-metadata {
  width: 100%;
  padding: 10px 14px;
  border: 1px solid var(--border);
  border-radius: 8px;
  font: inherit;
  font-size: 1rem;
  color: var(--text-primary);
  background: var(--bg);
  outline: none;
  transition: border-color 0.12s;
  -webkit-appearance: none;
}

#admin-password:focus,
#admin-user-email:focus,
#upload-tags:focus,
#upload-metadata:focus {
  border-color: var(--blue);
}

#admin-password::placeholder,
#admin-user-email::placeholder,
#upload-tags::placeholder {
  color: var(--text-secondary);
}

#upload-metadata {
  min-height: 96px;
  resize: vertical;
  line-height: 1.5;
}

#upload-metadata::placeholder {
  color: var(--text-secondary);
}

#upload-files {
  padding: 8px 14px;
  cursor: pointer;
}

/* ----- Buttons ----------------------------------------- */
button {
  font: inherit;
  font-size: 1rem;
  cursor: pointer;
  padding: 10px 18px;
  border-radius: 8px;
  border: none;
  background: var(--blue);
  color: #fff;
  font-weight: 600;
  line-height: 1.4;
  transition: background 0.12s;
}

button:hover {
  background: #003d7a;
}

button:disabled {
  opacity: 0.48;
  cursor: not-allowed;
}

/* Secondary – outlined */
.secondary-button {
  background: transparent;
  color: var(--blue);
  border: 1px solid var(--blue);
}

.secondary-button:hover {
  background: rgba(0, 76, 151, 0.06);
}

/* Destructive – delete buttons */
.document-delete.secondary-button {
  color: var(--red);
  border-color: var(--red);
}

.document-delete.secondary-button:hover {
  background: rgba(218, 41, 28, 0.06);
}

/* ----- Admin state badge ------------------------------- */
.admin-state {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 4px 10px;
  border-radius: 20px;
  font-size: 13px;
  font-weight: 600;
  background: rgba(4, 30, 66, 0.06);
  color: var(--text-secondary);
  margin-bottom: 14px;
}

.admin-state::before {
  content: '';
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: currentColor;
  flex-shrink: 0;
}

.admin-state[data-state="unlocked"] {
  background: rgba(0, 76, 151, 0.08);
  color: var(--blue);
}

/* ----- Inline actions row ------------------------------ */
.inline-actions {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  flex-wrap: wrap;
}

/* ----- Error text -------------------------------------- */
.inline-error {
  font-size: 14px;
  color: var(--red);
  min-height: 1.2em;
  margin-top: 8px;
}

/* ----- Document list ----------------------------------- */
.documents-list {
  display: flex;
  flex-direction: column;
  margin-top: 14px;
}

.admin-users-form {
  margin-top: 14px;
}

.admin-users-list {
  display: flex;
  flex-direction: column;
  gap: 10px;
  margin-top: 14px;
}

.admin-user-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 12px 14px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--bg);
}

.admin-user-email {
  font-size: 14px;
  color: var(--text-primary);
  word-break: break-word;
}

.admin-user-badge {
  display: inline-block;
  margin-left: 8px;
  padding: 2px 8px;
  border-radius: 999px;
  background: rgba(0, 76, 151, 0.08);
  color: var(--blue);
  font-size: 12px;
  font-weight: 600;
  vertical-align: middle;
}

.admin-user-remove {
  flex-shrink: 0;
}

.admin-user-fixed {
  flex-shrink: 0;
  font-size: 12px;
  font-weight: 600;
  color: var(--text-secondary);
  padding: 6px 10px;
  border-radius: 999px;
  background: rgba(4, 30, 66, 0.06);
}

.document-card {
  padding: 14px 0;
  border-bottom: 1px solid var(--border);
}

.document-card:last-child {
  border-bottom: none;
  padding-bottom: 0;
}

.document-card:first-child {
  padding-top: 0;
}

.document-card-header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 6px;
}

.document-filename {
  font-size: 15px;
  font-weight: 600;
  color: var(--text-primary);
  line-height: 1.4;
}

.document-meta-line {
  font-size: 13px;
  color: var(--text-secondary);
  line-height: 1.5;
  margin-top: 2px;
  word-break: break-word;
}

.document-status {
  font-weight: 600;
}

.document-status[data-status="completed"] {
  color: var(--blue);
}

.document-status[data-status="failed"] {
  color: var(--red);
}

.document-status[data-status="in_progress"] {
  color: var(--text-secondary);
}

.document-error {
  font-size: 13px;
  color: var(--red);
  margin-top: 6px;
}

/* ----- Empty state (documents list) -------------------- */
.documents-list .empty-state {
  padding: 20px 0;
  text-align: center;
  color: var(--text-secondary);
  font-size: 14px;
}

/* ----- Admin footer ------------------------------------ */
.admin-footer {
  padding: 16px;
  text-align: center;
  font-size: 13px;
  color: var(--text-secondary);
}

.admin-footer p {
  margin: 0;
}

/* =========================================================
   Responsive
   ========================================================= */

@media (max-width: 600px) {
  .admin-main {
    padding: 16px 12px 40px;
  }

  .panel-section {
    padding: 16px;
  }

  .admin-panel-header {
    padding: 14px 16px;
  }

  .document-card-header {
    flex-direction: column;
    gap: 8px;
  }

  .inline-actions {
    flex-wrap: wrap;
  }
}

@media (max-width: 390px) {
  .messages {
    padding: 16px 12px;
  }

  .input-bar {
    padding: 0 12px;
  }
}
