/* ============================================================================
   IRIS Chat UI - v8
   NJIT Office of Institutional Effectiveness
   ============================================================================ */

:root {
  --page-bg: #ffffff;
  --text: #111827;
  --muted: #6b7280;
  --panel: #ffffff;
  --panel-border: #e5e7eb;
  --bubble-user: #e8eefc;
  --bubble-assistant: #f3f4f6;
  --accent: #3b82f6;
  --header: #c8102e;
  --header-border: #b50e28;
  --header-ink: #ffffff;
  --error: #ef4444;
}

* { box-sizing: border-box; }

html, body { height: 100%; }

body {
  margin: 0;
  background: var(--page-bg);
  color: var(--text);
  font: 14px/1.45 system-ui, -apple-system, Segoe UI, Roboto, sans-serif;
}

/* ── Page Shell ──────────────────────────────────────────────────────────── */

.page {
  height: 100vh;
  display: grid;
  grid-template-rows: auto auto 1fr auto;
  border: 2px solid #89cff0;
  border-radius: 12px;
  overflow: hidden;
  position: relative;
  min-width: 350px;
  min-height: 400px;
  max-height: 100vh;
}

.page.maximized {
  position: fixed;
  inset: 0;
  width: 100vw !important;
  height: 100vh !important;
  border-radius: 0;
  border: none;
  z-index: 9999;
}

/* ── Resize Handles ──────────────────────────────────────────────────────── */

.resize-handle {
  position: absolute;
  width: 20px;
  height: 20px;
  right: 0;
  bottom: 0;
  cursor: nwse-resize;
  z-index: 1000;
}

.resize-handle::after {
  content: '';
  position: absolute;
  right: 3px;
  bottom: 3px;
  width: 12px;
  height: 12px;
  border-right: 2px solid #9ca3af;
  border-bottom: 2px solid #9ca3af;
}

.resize-edge {
  position: absolute;
  background: transparent;
  z-index: 999;
}
.resize-edge.top    { top: 0; left: 0; right: 0; height: 5px; cursor: ns-resize; }
.resize-edge.right  { top: 0; right: 0; bottom: 0; width: 5px; cursor: ew-resize; }
.resize-edge.bottom { bottom: 0; left: 0; right: 0; height: 5px; cursor: ns-resize; }
.resize-edge.left   { top: 0; left: 0; bottom: 0; width: 5px; cursor: ew-resize; }

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

.hdr {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 12px;
  background: var(--header);
  color: var(--header-ink);
}

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

.title {
  margin: 0;
  font-weight: 700;
  font-size: 16px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

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

.icon {
  color: var(--header-ink);
  background: transparent;
  border: 0;
  cursor: pointer;
  font-size: 18px;
  line-height: 1;
  padding: 2px 6px;
  border-radius: 6px;
}

.icon:hover { background: rgba(255, 255, 255, .12); }
.kebab { font-size: 20px; }

/* ── Status Bar ──────────────────────────────────────────────────────────── */

.status-bar {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
  padding: 8px 12px;
  font-size: 12px;
  font-family: 'Courier New', monospace;
  display: none;
  animation: slideDown 0.3s ease-out;
}

.status-bar.active { display: block; }
.status-bar.error { background: linear-gradient(135deg, #ef4444 0%, #dc2626 100%); }

@keyframes slideDown {
  from { opacity: 0; transform: translateY(-10px); }
  to   { opacity: 1; transform: translateY(0); }
}

/* ── Chat Area ───────────────────────────────────────────────────────────── */

.chat {
  display: flex;
  flex-direction: column;
  background: var(--panel);
  padding: 12px;
  overflow-y: auto;
  overflow-x: hidden;
  min-height: 0;
}

.message-row {
  display: flex;
  align-items: flex-start;
  gap: 10px;
  margin: 6px 0;
  max-width: 95%;
}

.message-row.assistant { align-self: flex-start; }
.message-row.user      { align-self: flex-end; }

.avatar {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  flex-shrink: 0;
  margin-top: 2px;
}

/* ── Bubbles ─────────────────────────────────────────────────────────────── */

.bubble {
  padding: 10px 12px;
  border-radius: 14px;
  word-wrap: break-word;
  overflow-wrap: break-word;
  line-height: 1.5;
  max-width: 100%;
  white-space: pre-wrap;
}

.bubble.user      { background: var(--bubble-user); }
.bubble.assistant { background: var(--bubble-assistant); font-family: system-ui, -apple-system, Segoe UI, Roboto, sans-serif; }
.bubble.error     { background: #fef2f2; border: 1px solid #fecaca; color: var(--error); }

.bubble a { color: var(--accent); text-decoration: underline; }
.bubble a:hover { color: #1d4ed8; }

.bubble.assistant a {
  color: var(--accent);
  text-decoration: none;
  border-bottom: 1px dashed var(--accent);
  padding-bottom: 1px;
  transition: all 0.2s;
}
.bubble.assistant a:hover { color: #1d4ed8; border-bottom-style: solid; }

/* ── Markdown Tables ─────────────────────────────────────────────────────── */

.bubble.assistant table.response-table {
  border-collapse: collapse;
  margin: 12px 0;
  font-size: 13px;
  width: 100%;
  background: white;
  border: 1px solid #d1d5db;
  border-radius: 6px;
  overflow: hidden;
  display: table;
}

.bubble.assistant table.response-table thead  { display: table-header-group; }
.bubble.assistant table.response-table tbody  { display: table-row-group; }
.bubble.assistant table.response-table tr     { display: table-row; }

.bubble.assistant table.response-table th,
.bubble.assistant table.response-table td {
  border: 1px solid #d1d5db;
  padding: 10px 12px;
  text-align: left;
  vertical-align: top;
  display: table-cell;
}

.bubble.assistant table.response-table th {
  background: #f3f4f6;
  font-weight: 600;
  color: var(--text);
  border-bottom: 2px solid #d1d5db;
}

.bubble.assistant table.response-table td          { background: white; }
.bubble.assistant table.response-table tr:nth-child(even) td { background: #f9fafb; }
.bubble.assistant table.response-table tr:hover td { background: #eff6ff; }

/* ── Code ────────────────────────────────────────────────────────────────── */

.bubble.assistant code {
  background: #f1f5f9;
  padding: 2px 6px;
  border-radius: 4px;
  font-family: 'Courier New', monospace;
  font-size: 12px;
  color: #e11d48;
}

.bubble.assistant pre {
  background: #1e293b;
  color: #e2e8f0;
  padding: 12px;
  border-radius: 6px;
  overflow-x: auto;
  margin: 10px 0;
  white-space: pre-wrap;
  word-wrap: break-word;
  overflow-wrap: break-word;
  font-family: 'Courier New', monospace;
  font-size: 13px;
}

.bubble.assistant pre code { background: none; padding: 0; color: inherit; }

/* ── Lists & Headers ─────────────────────────────────────────────────────── */

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

.bubble.assistant li { margin: 4px 0; }

.bubble.assistant h3,
.bubble.assistant h4 { margin: 12px 0 8px 0; color: var(--text); }

.bubble.assistant h3 { font-size: 15px; }
.bubble.assistant h4 { font-size: 14px; }

/* ── Feedback Buttons ────────────────────────────────────────────────────── */

.feedback-row {
  display: flex;
  gap: 6px;
  margin-top: 4px;
  margin-left: 50px; /* align under bubble, past avatar */
}

.feedback-btn {
  background: transparent;
  border: 1px solid var(--panel-border);
  border-radius: 6px;
  padding: 3px 8px;
  font-size: 14px;
  cursor: pointer;
  transition: all 0.15s;
  line-height: 1;
}

.feedback-btn:hover:not(:disabled) {
  background: var(--bubble-assistant);
  border-color: #9ca3af;
  transform: scale(1.1);
}

.feedback-btn:disabled { cursor: default; }

/* ── Footer / Input ──────────────────────────────────────────────────────── */

footer {
  display: flex;
  gap: 8px;
  padding: 10px 12px;
  background: #fff;
  position: relative;
  flex-shrink: 0;
}

input {
  flex: 1;
  padding: 10px 12px;
  border-radius: 12px;
  border: 1px solid var(--panel-border);
  background: #fff;
  color: var(--text);
  font-size: 14px;
  line-height: 1.4;
}

input:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 2px rgba(59, 130, 246, 0.1);
}

.char-counter {
  position: absolute;
  right: 60px;
  bottom: 18px;
  font-size: 11px;
  color: var(--muted);
}

.char-counter.warning { color: #f59e0b; }
.char-counter.error   { color: var(--error); }

button#send {
  padding: 10px;
  width: 40px;
  height: 40px;
  font-size: 16px;
  line-height: 1;
  border-radius: 12px;
  border: none;
  background: var(--accent);
  color: #fff;
  font-weight: 600;
  cursor: pointer;
  flex-shrink: 0;
  transition: background 0.2s;
}

button#send:hover:not(:disabled) { background: #2563eb; }
button:disabled { opacity: .5; cursor: not-allowed; }

/* ── Dropdown Menu ───────────────────────────────────────────────────────── */

.menu {
  position: absolute;
  left: 12px;
  top: 48px;
  background: var(--panel);
  color: var(--text);
  border: 1px solid var(--panel-border);
  border-radius: 10px;
  display: flex;
  flex-direction: column;
  min-width: 280px;
  box-shadow: 0 12px 30px rgba(0, 0, 0, .15);
  font-size: 14px;
  z-index: 1001;
}

.menu.is-hidden { display: none; }

.menu-items { padding: 8px; display: flex; flex-direction: column; gap: 4px; }

.menu button {
  background: transparent;
  color: var(--text);
  border: 0;
  text-align: left;
  padding: 10px 12px;
  border-radius: 8px;
  cursor: pointer;
  text-decoration: none;
  display: flex;
  align-items: center;
  gap: 12px;
}

.menu button:hover { background: #f3f4f6; }
.menu svg { width: 20px; height: 20px; color: var(--muted); }

/* ── Loading Indicator ───────────────────────────────────────────────────── */

.loading { display: flex; align-items: center; gap: 5px; padding: 2px 0; }

.loading span {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background-color: #9ca3af;
  animation: bounce 1.4s infinite ease-in-out both;
}

.loading span:nth-child(1) { animation-delay: -0.32s; }
.loading span:nth-child(2) { animation-delay: -0.16s; }

@keyframes bounce {
  0%, 80%, 100% { transform: scale(0); }
  40%           { transform: scale(1.0); }
}

/* ── Clarification Workspace Buttons ─────────────────────────────────────── */

.workspace-options {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-top: 12px;
  padding-top: 12px;
  border-top: 1px solid var(--panel-border);
}

.workspace-btn {
  padding: 8px 14px;
  background: white;
  border: 1px solid var(--panel-border);
  border-radius: 8px;
  cursor: pointer;
  font-size: 13px;
  color: var(--text);
  transition: all 0.2s;
}

.workspace-btn:hover {
  background: var(--accent);
  color: white;
  border-color: var(--accent);
}

.workspace-label { font-size: 12px; color: var(--muted); margin-bottom: 6px; width: 100%; }

/* ── Retry Button ────────────────────────────────────────────────────────── */

.retry-btn {
  margin-top: 8px;
  padding: 6px 12px;
  background: var(--accent);
  color: white;
  border: none;
  border-radius: 6px;
  cursor: pointer;
  font-size: 12px;
}

.retry-btn:hover { background: #2563eb; }
