/* ═══════════════════════════════════════════════════════════════════
   M4 — AI Chat Widget
   Floating launcher (FAB) bottom-right + slide-up panel. Panel is
   fixed-positioned so it floats over every page. Mobile breakpoint
   makes it full-screen below 600px.

   CSS-isolation rule: the [hidden] HTML attribute is overridden by
   `display: flex` on the class — same gotcha as .modal-backdrop.
   The [hidden] override rules below restore the expected behaviour
   (matches the pattern that lives in forms.css for .modal-backdrop).
   ═══════════════════════════════════════════════════════════════════ */

.ai-chat-widget {
  position: fixed;
  bottom: 18px;
  right: 18px;
  z-index: 4500;
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  pointer-events: none; /* children re-enable */
}
.ai-chat-widget > * { pointer-events: auto; }
.ai-chat-widget[hidden] { display: none; }

.ai-chat-fab {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 12px 16px;
  border-radius: 999px;
  border: none;
  background: linear-gradient(135deg, var(--brand-teal-light), var(--brand-sage));
  color: #fff;
  font-weight: 600;
  font-size: 13px;
  letter-spacing: 0.02em;
  cursor: pointer;
  box-shadow: 0 8px 22px rgba(13, 100, 130, 0.35), 0 2px 6px rgba(0, 0, 0, 0.25);
  transition: transform .14s ease, box-shadow .14s ease;
}
.ai-chat-fab:hover {
  transform: translateY(-1px);
  box-shadow: 0 12px 28px rgba(13, 100, 130, 0.42), 0 3px 10px rgba(0, 0, 0, 0.30);
}
.ai-chat-fab svg { display: block; }
.ai-chat-fab-label { white-space: nowrap; }

/* Hide the FAB while the panel is open. JS toggles .is-panel-open on
   the widget in toggleAiChatPanel(). The panel's own close (X) button
   reopens the FAB. Without this rule the FAB sits on top of the panel
   like a duplicate launcher. */
.ai-chat-widget.is-panel-open .ai-chat-fab { display: none; }

.ai-chat-panel {
  /* Sized 25% wider + 30% taller than the original (420x620) per
     user request — gives more room for action cards + tables. */
  width: 525px;
  max-width: calc(100vw - 24px);
  height: min(85vh, 806px);
  background: var(--surface);
  border: 1px solid var(--border2);
  border-radius: 14px;
  box-shadow: 0 30px 60px rgba(0, 0, 0, 0.55), 0 6px 18px rgba(0, 0, 0, 0.35);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  margin-bottom: 12px;
  animation: ai-chat-rise 0.18s ease-out both;
}
.ai-chat-panel[hidden] { display: none; }

@keyframes ai-chat-rise {
  from { opacity: 0; transform: translateY(8px) scale(0.98); }
  to   { opacity: 1; transform: translateY(0)    scale(1); }
}

.ai-chat-head {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 12px 14px;
  border-bottom: 1px solid var(--divider);
  background: var(--surface-elev);
}
.ai-chat-head-titlewrap { flex: 1; min-width: 0; }
.ai-chat-head-title {
  font-size: 13px;
  font-weight: 600;
  color: var(--text);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.ai-chat-head-sub { font-size: 11px; color: var(--muted); margin-top: 2px; }
.ai-chat-head-actions { display: flex; gap: 4px; }
.ai-chat-icon-btn {
  width: 28px; height: 28px;
  border: 1px solid var(--border);
  background: transparent;
  color: var(--text-secondary);
  border-radius: 6px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: background .14s ease, color .14s ease, border-color .14s ease;
}
.ai-chat-icon-btn:hover {
  background: var(--surface2);
  color: var(--text);
  border-color: var(--border2);
}

.ai-chat-body {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-height: 0;
}
.ai-chat-empty {
  margin: auto;
  text-align: center;
  padding: 28px 24px;
  color: var(--muted);
}
.ai-chat-empty-title {
  font-size: 15px;
  font-weight: 600;
  color: var(--text-secondary);
  margin-bottom: 6px;
}
.ai-chat-empty-hint { font-size: 12px; line-height: 1.55; }
.ai-chat-empty-hint em {
  color: var(--text-secondary);
  font-style: normal;
  background: var(--surface2);
  padding: 1px 6px;
  border-radius: 4px;
}

.ai-chat-messages {
  flex: 1;
  overflow-y: auto;
  padding: 12px 14px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}
.ai-chat-msg {
  max-width: 88%;
  padding: 9px 12px;
  border-radius: 10px;
  font-size: 13px;
  line-height: 1.5;
  word-wrap: break-word;
  white-space: pre-wrap;
}
.ai-chat-msg.is-user {
  align-self: flex-end;
  background: linear-gradient(135deg, var(--brand-teal-light), var(--brand-teal));
  color: #fff;
  border-bottom-right-radius: 4px;
}
.ai-chat-msg.is-assistant {
  align-self: flex-start;
  background: var(--surface-elev);
  color: var(--text);
  border: 1px solid var(--border);
  border-bottom-left-radius: 4px;
  white-space: normal;
}
.ai-chat-msg.is-tool {
  align-self: flex-start;
  background: transparent;
  color: var(--muted);
  font-size: 11px;
  padding: 4px 8px;
  font-style: italic;
}
.ai-chat-msg .ai-chat-msg-text { white-space: pre-wrap; }
.ai-chat-msg.is-assistant .ai-chat-msg-text { white-space: normal; }
.ai-chat-msg.is-assistant p:first-child { margin-top: 0; }
.ai-chat-msg.is-assistant p:last-child  { margin-bottom: 0; }
.ai-chat-msg.is-assistant code {
  background: var(--surface3);
  padding: 1px 5px;
  border-radius: 3px;
  font-size: 12px;
  font-family: 'DM Mono', ui-monospace, monospace;
}
.ai-chat-msg.is-assistant pre {
  background: var(--surface3);
  padding: 8px 10px;
  border-radius: 6px;
  overflow-x: auto;
  font-size: 12px;
}
.ai-chat-msg.is-assistant pre code { padding: 0; background: transparent; }
.ai-chat-msg.is-assistant ul,
.ai-chat-msg.is-assistant ol { padding-left: 22px; margin: 6px 0; }
.ai-chat-msg.is-assistant a { color: var(--accent2); }

/* Markdown tables — for list-of-things responses (tasks, employees, etc.).
   System prompt encourages the LLM to use tables for 3+ rows. marked
   already parses GFM tables; these styles polish the output. */
.ai-chat-msg.is-assistant table {
  width: 100%;
  border-collapse: collapse;
  margin: 8px 0;
  font-size: 12px;
  background: var(--surface2);
  border-radius: 6px;
  overflow: hidden;
  border: 1px solid var(--border);
}
.ai-chat-msg.is-assistant table th,
.ai-chat-msg.is-assistant table td {
  text-align: left;
  padding: 6px 9px;
  border-bottom: 1px solid var(--border);
  vertical-align: top;
  word-break: normal;
}
.ai-chat-msg.is-assistant table th {
  background: var(--surface3);
  color: var(--text-secondary);
  font-weight: 600;
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.04em;
}
.ai-chat-msg.is-assistant table tr:last-child td { border-bottom: none; }
.ai-chat-msg.is-assistant table tr:hover td { background: var(--surface3); }

/* ── Pending-action cards ─────────────────────────────────────
   Rendered when a write tool stages an action awaiting click-confirm.
   The card sits in the message stream after the assistant bubble.
   Three states: preview (default), editing, resolved (confirmed/cancelled). */

.ai-chat-action {
  align-self: stretch;
  border: 1px solid var(--border2);
  background: var(--surface-elev);
  border-radius: 10px;
  padding: 12px 14px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  font-size: 13px;
  color: var(--text);
  border-left: 3px solid var(--accent);
}
.ai-chat-action.is-confirmed { border-left-color: var(--accent2); opacity: 0.85; }
.ai-chat-action.is-cancelled { border-left-color: var(--muted); opacity: 0.7; }
.ai-chat-action.is-busy      { opacity: 0.7; pointer-events: none; }

.ai-chat-action-head {
  display: flex;
  align-items: center;
  gap: 8px;
  font-weight: 600;
  font-size: 13px;
  color: var(--text);
}
.ai-chat-action-icon {
  width: 22px; height: 22px;
  border-radius: 6px;
  background: rgba(95, 181, 185, 0.15);
  color: var(--accent2);
  display: inline-flex;
  align-items: center;
  justify-content: center;
}
.ai-chat-action.is-confirmed .ai-chat-action-icon {
  background: rgba(95, 181, 185, 0.25);
  color: var(--accent2);
}
.ai-chat-action.is-cancelled .ai-chat-action-icon {
  background: var(--surface3);
  color: var(--muted);
}
.ai-chat-action-status {
  margin-left: auto;
  font-size: 11px;
  font-weight: 500;
  padding: 2px 8px;
  border-radius: 999px;
  background: var(--surface2);
  color: var(--muted);
}
.ai-chat-action.is-confirmed .ai-chat-action-status {
  background: rgba(95, 181, 185, 0.18);
  color: var(--accent2);
}
.ai-chat-action.is-cancelled .ai-chat-action-status {
  background: rgba(239, 68, 68, 0.12);
  color: var(--red);
}

.ai-chat-action-fields {
  display: grid;
  grid-template-columns: minmax(80px, max-content) 1fr;
  gap: 4px 12px;
  font-size: 12px;
}
.ai-chat-action-fields dt {
  color: var(--muted);
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  align-self: start;
  padding-top: 2px;
}
.ai-chat-action-fields dd {
  color: var(--text);
  margin: 0;
  word-break: break-word;
  white-space: pre-wrap;
}
.ai-chat-action-fields dd.is-empty { color: var(--muted); font-style: italic; }

.ai-chat-action-buttons {
  display: flex;
  gap: 6px;
  flex-wrap: wrap;
}
.ai-chat-action-btn {
  border: 1px solid var(--border);
  background: var(--surface2);
  color: var(--text);
  border-radius: 6px;
  padding: 6px 12px;
  font-size: 12px;
  font-weight: 500;
  cursor: pointer;
  transition: background .14s ease, border-color .14s ease;
}
.ai-chat-action-btn:hover {
  background: var(--surface3);
  border-color: var(--border2);
}
.ai-chat-action-btn.is-primary {
  background: var(--accent);
  color: #fff;
  border-color: var(--accent);
}
.ai-chat-action-btn.is-primary:hover { background: var(--brand-teal-light); }
.ai-chat-action-btn.is-danger {
  color: var(--red);
  border-color: rgba(239, 68, 68, 0.4);
}
.ai-chat-action-btn.is-danger:hover {
  background: rgba(239, 68, 68, 0.1);
}
.ai-chat-action-btn[disabled] {
  opacity: 0.5;
  cursor: not-allowed;
}

.ai-chat-action-edit {
  display: grid;
  grid-template-columns: minmax(80px, max-content) 1fr;
  gap: 6px 12px;
  font-size: 12px;
}
.ai-chat-action-edit label {
  color: var(--muted);
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  align-self: center;
}
.ai-chat-action-edit input,
.ai-chat-action-edit textarea,
.ai-chat-action-edit select {
  background: var(--surface);
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: 6px;
  padding: 5px 8px;
  font-size: 12px;
  font-family: inherit;
  width: 100%;
}
.ai-chat-action-edit textarea { resize: vertical; min-height: 36px; max-height: 120px; }
.ai-chat-action-edit input:focus,
.ai-chat-action-edit textarea:focus,
.ai-chat-action-edit select:focus {
  outline: none;
  border-color: var(--accent);
}

.ai-chat-action-error {
  font-size: 11px;
  color: var(--red);
  margin-top: -4px;
}

/* System bubbles for action results (✓ Done / ✗ Cancelled). Smaller
   than full assistant bubbles but stand out from the chat noise. */
.ai-chat-msg.is-system-event {
  align-self: center;
  background: var(--surface2);
  color: var(--text-secondary);
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 4px 12px;
  font-size: 11px;
  white-space: normal;
}
.ai-chat-msg.is-system-event.is-success {
  background: rgba(95, 181, 185, 0.15);
  border-color: rgba(95, 181, 185, 0.4);
  color: var(--accent2);
}
.ai-chat-msg.is-system-event.is-error {
  background: rgba(239, 68, 68, 0.12);
  border-color: rgba(239, 68, 68, 0.35);
  color: var(--red);
}

.ai-chat-toolchip {
  align-self: flex-start;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 4px 8px;
  font-size: 11px;
  color: var(--muted);
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: 999px;
}
.ai-chat-toolchip.is-running {
  color: var(--accent2);
  border-color: rgba(95, 181, 185, 0.4);
}
.ai-chat-toolchip.is-running::before {
  content: '';
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--accent2);
  animation: ai-toolchip-pulse 1.2s ease-in-out infinite;
}
.ai-chat-toolchip.is-error {
  color: var(--red);
  border-color: rgba(239, 68, 68, 0.35);
}
@keyframes ai-toolchip-pulse {
  0%, 100% { opacity: 0.4; transform: scale(0.85); }
  50%      { opacity: 1;   transform: scale(1); }
}

.ai-chat-feedback {
  display: flex;
  gap: 6px;
  margin-top: 6px;
  align-items: center;
}
.ai-chat-feedback-btn {
  width: 24px; height: 24px;
  border: 1px solid var(--border);
  background: transparent;
  color: var(--muted);
  border-radius: 5px;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-size: 12px;
  transition: all .14s ease;
}
.ai-chat-feedback-btn:hover {
  background: var(--surface2);
  color: var(--text-secondary);
}
.ai-chat-feedback-btn.is-active.is-up {
  background: rgba(95, 181, 185, 0.18);
  border-color: rgba(95, 181, 185, 0.5);
  color: var(--accent2);
}
.ai-chat-feedback-btn.is-active.is-down {
  background: rgba(239, 68, 68, 0.15);
  border-color: rgba(239, 68, 68, 0.45);
  color: var(--red);
}
.ai-chat-feedback-comment {
  display: none;
  margin-top: 6px;
  width: 100%;
}
.ai-chat-feedback-comment.is-open { display: flex; gap: 6px; }
.ai-chat-feedback-comment textarea {
  flex: 1;
  background: var(--surface2);
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: 6px;
  padding: 5px 8px;
  font-size: 12px;
  resize: vertical;
  min-height: 28px;
  max-height: 80px;
}
.ai-chat-feedback-comment button {
  background: var(--accent);
  color: #fff;
  border: none;
  border-radius: 6px;
  padding: 0 10px;
  font-size: 11px;
  cursor: pointer;
}

.ai-chat-budget {
  padding: 8px 14px;
  background: rgba(239, 68, 68, 0.12);
  color: var(--red);
  font-size: 12px;
  border-top: 1px solid rgba(239, 68, 68, 0.3);
  text-align: center;
}
.ai-chat-budget[hidden] { display: none; }

.ai-chat-input-row {
  display: flex;
  gap: 6px;
  padding: 10px 12px;
  border-top: 1px solid var(--divider);
  background: var(--surface);
  align-items: flex-end;
}
.ai-chat-input {
  flex: 1;
  background: var(--surface2);
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: 8px;
  padding: 8px 10px;
  font-size: 13px;
  resize: none;
  max-height: 120px;
  font-family: inherit;
  line-height: 1.45;
}
.ai-chat-input:focus {
  outline: none;
  border-color: var(--accent);
  background: var(--surface);
}
.ai-chat-send {
  width: 36px;
  height: 36px;
  border-radius: 8px;
  border: none;
  background: var(--accent);
  color: #fff;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  transition: background .14s ease;
  flex-shrink: 0;
}
.ai-chat-send:hover { background: var(--brand-teal-light); }
.ai-chat-send[disabled] {
  background: var(--surface3);
  cursor: not-allowed;
  color: var(--muted);
}

/* ── Mic button (Whisper voice input) ─────────────────────────── */
/* Idle: ghost-style button matching other icon buttons.
   Recording: pulses red + shows the elapsed timer.
   Transcribing: shows a soft spinning border ("waiting on Groq").
   Unavailable: hidden in JS, but the class exists for CSS-driven debug. */
.ai-chat-mic {
  width: 36px;
  height: 36px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--surface2);
  color: var(--text-secondary);
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  position: relative;
  transition: background .14s ease, color .14s ease, border-color .14s ease;
  flex-shrink: 0;
}
.ai-chat-mic:hover {
  background: var(--surface3);
  color: var(--text);
  border-color: var(--border2);
}
.ai-chat-mic-ico { display: block; }
.ai-chat-mic-dot {
  position: absolute;
  top: 6px;
  right: 6px;
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--red);
  opacity: 0;
  pointer-events: none;
}
.ai-chat-mic-timer {
  position: absolute;
  bottom: -16px;
  left: 50%;
  transform: translateX(-50%);
  font-size: 10px;
  color: var(--red);
  font-family: 'DM Mono', ui-monospace, monospace;
  white-space: nowrap;
  display: none;
}

.ai-chat-mic.is-recording {
  background: rgba(239, 68, 68, 0.12);
  border-color: rgba(239, 68, 68, 0.5);
  color: var(--red);
  animation: ai-chat-mic-pulse 1.4s ease-in-out infinite;
}
.ai-chat-mic.is-recording .ai-chat-mic-dot {
  opacity: 1;
  animation: ai-chat-mic-dot-pulse 1s ease-in-out infinite;
}
.ai-chat-mic.is-recording .ai-chat-mic-timer { display: block; }

.ai-chat-mic.is-transcribing {
  background: var(--surface2);
  color: var(--accent2);
  border-color: rgba(95, 181, 185, 0.5);
  cursor: progress;
}
.ai-chat-mic.is-transcribing .ai-chat-mic-ico {
  animation: ai-chat-mic-spin 1.1s linear infinite;
}

@keyframes ai-chat-mic-pulse {
  0%, 100% { box-shadow: 0 0 0 0 rgba(239, 68, 68, 0.3); }
  50%      { box-shadow: 0 0 0 5px rgba(239, 68, 68, 0); }
}
@keyframes ai-chat-mic-dot-pulse {
  0%, 100% { opacity: 0.4; }
  50%      { opacity: 1; }
}
@keyframes ai-chat-mic-spin {
  from { transform: rotate(0deg); }
  to   { transform: rotate(360deg); }
}

.ai-chat-history {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow-y: auto;
}
.ai-chat-history[hidden] { display: none; }
.ai-chat-history-head {
  padding: 10px 14px 4px 14px;
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--muted);
}
.ai-chat-history-list { padding: 4px 8px 12px 8px; }
.ai-chat-history-row {
  padding: 8px 10px;
  border-radius: 6px;
  cursor: pointer;
  display: flex;
  flex-direction: column;
  gap: 2px;
  transition: background .14s ease;
}
.ai-chat-history-row:hover { background: var(--surface2); }
.ai-chat-history-title {
  font-size: 13px;
  color: var(--text);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.ai-chat-history-meta { font-size: 11px; color: var(--muted); }
.ai-chat-history-row .ai-chat-history-del {
  align-self: flex-end;
  background: transparent;
  border: none;
  color: var(--muted);
  font-size: 11px;
  cursor: pointer;
  padding: 2px 6px;
}
.ai-chat-history-row .ai-chat-history-del:hover { color: var(--red); }

/* ── AI Feedback admin page ─────────────────────────────────── */
.ai-feedback-page .ai-fb-row {
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 12px;
  margin-bottom: 10px;
  background: var(--surface);
}
.ai-feedback-page .ai-fb-row.is-down { border-left: 3px solid var(--red); }
.ai-feedback-page .ai-fb-row.is-up   { border-left: 3px solid var(--accent2); }
.ai-feedback-page .ai-fb-meta {
  display: flex;
  gap: 12px;
  align-items: center;
  font-size: 12px;
  color: var(--muted);
  margin-bottom: 8px;
  flex-wrap: wrap;
}
.ai-feedback-page .ai-fb-meta .pill {
  padding: 1px 8px;
  border-radius: 999px;
  font-size: 11px;
  background: var(--surface2);
}
.ai-feedback-page .ai-fb-meta .pill.is-down {
  background: rgba(239, 68, 68, 0.18);
  color: var(--red);
}
.ai-feedback-page .ai-fb-meta .pill.is-up {
  background: rgba(95, 181, 185, 0.18);
  color: var(--accent2);
}
.ai-feedback-page .ai-fb-comment {
  font-size: 13px;
  color: var(--text);
  background: var(--surface2);
  border-radius: 6px;
  padding: 8px 10px;
  margin-bottom: 8px;
}
.ai-feedback-page .ai-fb-content {
  font-size: 12px;
  color: var(--text-secondary);
  line-height: 1.5;
  background: var(--surface-elev);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 8px 10px;
  white-space: pre-wrap;
  max-height: 220px;
  overflow-y: auto;
}

@media (max-width: 600px) {
  .ai-chat-widget { bottom: 0; right: 0; left: 0; align-items: stretch; }
  .ai-chat-fab { margin: 12px; align-self: flex-end; }
  .ai-chat-panel {
    width: auto;
    max-width: none;
    height: 100vh;
    margin: 0;
    border-radius: 0;
    border-left: none;
    border-right: none;
    border-bottom: none;
  }
}
