/* ==========================================================================
   mail.jarin.dev — AI assistant page (chat thread + automations sidebar)
   Owns only the .ai-* surfaces and the [data-view="ai"] grid switch. Every
   color is a token from styles.css, so html[data-theme="midnight"] flips the
   page with no second rule set here.
   ========================================================================== */

/* ---------- View switch --------------------------------------------------
   Belt-and-braces: the markup keeps a hidden attribute (styles.css' [hidden]
   rule is !important, so it always wins); this base rule only means the page
   can never paint outside the ai view even if that attribute is lost. */
.ai-page { display: none; }

.layout[data-view="ai"] .list-pane,
.layout[data-view="ai"] .reader-pane { display: none; }

.layout[data-view="ai"] .ai-page {
  grid-column: 2 / -1;
  display: grid;
  grid-template-columns: minmax(0, 1fr) 360px;
  height: 100%;
  min-height: 0;
}

/* ---------- Chat column -------------------------------------------------- */
.ai-chat {
  display: flex;
  flex-direction: column;
  min-width: 0;
  min-height: 0;
  background: var(--paper);
}

/* Both column heads are locked to the same height so their hairlines meet. */
.ai-chat-head {
  display: flex;
  align-items: center;
  gap: 10px;
  flex: none;
  min-height: 60px;
  padding: 14px 24px;
  background: var(--paper);
  border-bottom: var(--b) solid var(--ink);
}

.ai-title {
  font-family: var(--font-disp);
  font-weight: 800;
  font-size: 20px;
  letter-spacing: -.015em;
}

.ai-model {
  padding: 2px 8px;
  border: var(--b) solid var(--line);
  border-radius: 999px;
  color: var(--grey);
  font-size: 10px;
  white-space: nowrap;
}
/* A dead server-side key is a state, not a detail — say so in red. */
.ai-model[data-state="off"] {
  border-color: var(--red);
  color: var(--red);
}

.ai-head-gap { margin-left: auto; }

.ai-thread {
  flex: 1 1 auto;
  min-height: 0;
  overflow-y: auto;
  overscroll-behavior: contain;
  display: flex;
  flex-direction: column;
  gap: 12px;
  padding: 18px 24px 10px;
}

.ai-thread-empty {
  margin: auto;
  max-width: 44ch;
  padding: 24px 0;
  text-align: center;
  color: var(--grey);
  font-size: 13px;
}

/* ---------- Bubbles ------------------------------------------------------
   Wrapper is shrink-to-fit (max 75%) so short lines stay pill-like. */
.ai-msg {
  display: flex;
  flex-direction: column;
  max-width: 75%;
}
.ai-msg-user { align-self: flex-end; align-items: flex-end; }
.ai-msg-assistant { align-self: flex-start; align-items: flex-start; }

.ai-bubble {
  padding: 10px 12px;
  border: var(--b) solid var(--ink);
  border-radius: var(--r);
  background: var(--card);
  color: var(--ink);
  font-size: 14px;
  line-height: 1.55;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}

/* Text stays --ink (not --blue-d): on the midnight theme --blue-t is a dark
   blue, so a fixed dark text colour would vanish. */
.ai-msg-user .ai-bubble {
  background: var(--blue-t);
  border-color: var(--blue-d);
}

.ai-msg-pending { opacity: .6; }

/* Declared after .ai-msg-user so a failed turn wins on equal specificity. */
.ai-msg-error .ai-bubble {
  background: color-mix(in srgb, var(--red) 12%, var(--card));
  border-color: var(--red);
}

.ai-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-top: 6px;
  color: var(--grey);
}
.ai-action { color: var(--grey); }

/* ---------- Suggestion chips -------------------------------------------- */
.ai-suggest {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  flex: none;
  padding: 4px 24px 10px;
}

.ai-chip {
  padding: 5px 12px;
  border: var(--b) solid var(--ink);
  border-radius: 999px;
  background: var(--card);
  color: var(--ink);
  font-family: var(--font-ui);
  font-size: 12px;
  font-weight: 500;
  line-height: 1.2;
  box-shadow: 3px 3px 0 var(--ink);
  transition: transform .12s ease, box-shadow .12s ease;
}
.ai-chip:hover {
  transform: translate(-1px, -1px);
  box-shadow: 4px 4px 0 var(--ink);
}
.ai-chip:active {
  transform: translate(2px, 2px);
  box-shadow: none;
}
.ai-chip:disabled {
  opacity: .6;
  cursor: default;
  transform: none;
  box-shadow: 3px 3px 0 var(--ink);
}

/* ---------- Input row + status ------------------------------------------ */
.ai-inputrow {
  display: flex;
  align-items: flex-end;
  gap: 10px;
  flex: none;
  padding: 0 24px;
}

.ai-inputrow textarea {
  flex: 1 1 auto;
  min-width: 0;
  min-height: 46px;
  max-height: 160px;
  padding: 10px 12px;
  border: var(--b) solid var(--ink);
  border-radius: var(--r);
  background: var(--card);
  color: var(--ink);
  line-height: 1.5;
  resize: none;
}
.ai-inputrow textarea:focus { border-color: var(--blue); }
.ai-inputrow textarea:disabled { opacity: .6; }
.ai-inputrow .btn { flex: none; }

.ai-status {
  flex: none;
  min-height: 15px;   /* reserves the line so the thread never jumps */
  padding: 8px 24px 12px;
  color: var(--grey);
}
.ai-status[data-state="error"] { color: var(--red); }

/* ---------- Automations side -------------------------------------------- */
.ai-side {
  display: flex;
  flex-direction: column;
  min-width: 0;
  min-height: 0;
  overflow-y: auto;
  overscroll-behavior: contain;
  padding: 0 14px 24px;
  background: var(--card);
  border-left: var(--b) solid var(--ink);
}

.ai-side-head {
  position: sticky;
  top: 0;
  z-index: 1;   /* the head stays put while autos + activity scroll under it */
  display: flex;
  align-items: center;
  gap: 8px;
  min-height: 60px;
  padding: 14px 2px 10px;
  background: var(--card);
  border-bottom: var(--b) solid var(--ink);
}

.ai-side-title {
  font-family: var(--font-disp);
  font-weight: 700;
  font-size: 15px;
  letter-spacing: -.01em;
}

.ai-side-count {
  margin-left: auto;
  color: var(--grey);
  white-space: nowrap;
}

.ai-autos {
  display: flex;
  flex-direction: column;
  gap: 12px;
  padding-top: 14px;
}

.ai-auto {
  padding: 12px;
  background: var(--card);
  border: var(--b) solid var(--ink);
  border-radius: var(--r);
  box-shadow: 3px 3px 0 var(--ink);
}
.ai-auto.paused { opacity: .75; }

.ai-auto-head {
  display: flex;
  align-items: center;
  gap: 8px;
}

.ai-auto-name {
  flex: 1 1 auto;
  min-width: 0;
  font-family: var(--font-disp);
  font-weight: 700;
  font-size: 14px;
  letter-spacing: -.01em;
  overflow-wrap: anywhere;
}

.ai-auto-status {
  flex: none;
  padding: 1px 8px;
  border: var(--b) solid var(--blue-d);
  border-radius: 999px;
  background: var(--blue-t);
  color: var(--blue-d);
  font-size: 10px;
}
.ai-auto.paused .ai-auto-status {
  border-color: var(--line);
  background: var(--line2);
  color: var(--grey);
}

.ai-auto-desc {
  margin-top: 6px;
  color: var(--grey);
  font-size: 12px;
  line-height: 1.45;
}

.ai-auto-facts {
  display: flex;
  flex-direction: column;
  gap: 6px;
  margin-top: 10px;
}
.ai-auto-facts > div {
  display: flex;
  flex-direction: column;
  gap: 1px;
  min-width: 0;
}
.ai-auto-facts dt {
  color: var(--grey);
  font-size: 10px;
  text-transform: uppercase;
  letter-spacing: .12em;
}
.ai-auto-facts dd {
  color: var(--ink);
  font-size: 12px;
  letter-spacing: .02em;
  overflow-wrap: anywhere;
}

.ai-auto-foot {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-top: 12px;
  padding-top: 10px;
  border-top: 1px solid var(--line2);
}
/* Compact stamp buttons — the 3px offset would dwarf a 26px-tall control. */
.ai-auto-foot .btn {
  padding: 5px 12px;
  font-size: 13px;
  box-shadow: 2px 2px 0 var(--ink);
}
.ai-auto-foot .btn:hover { box-shadow: 3px 3px 0 var(--ink); }
.ai-auto-foot .btn:disabled { box-shadow: 2px 2px 0 var(--ink); }

/* Quiet, unfilled, red — the destructive one should not advertise itself. */
.ai-auto-delete { color: var(--red); }
.ai-auto-delete:hover { background: color-mix(in srgb, var(--red) 10%, var(--card)); }

.ai-empty {
  padding: 18px 2px;
  color: var(--grey);
}
.ai-empty-title {
  font-family: var(--font-disp);
  font-weight: 700;
  font-size: 14px;
  color: var(--ink);
}
.ai-empty-sub {
  margin-top: 2px;
  font-size: 12px;
}

.ai-side-sub {
  margin-top: 18px;
  padding: 0 2px 8px;
  border-bottom: var(--b) solid var(--line);
  font-family: var(--font-mono);
  font-size: 10px;
  font-weight: 400;
  letter-spacing: .12em;
  text-transform: uppercase;
  color: var(--grey);
}

/* ---------- Activity log ------------------------------------------------- */
.ai-activity {
  display: flex;
  flex-direction: column;
  padding-top: 8px;
}

.ai-activity-row {
  display: grid;
  grid-template-columns: auto minmax(0, 1fr) auto;
  align-items: baseline;
  gap: 8px;
  padding: 5px 2px;
  color: var(--grey);
}

.ai-activity-badge {
  padding: 1px 6px;
  border: var(--b) solid var(--line);
  border-radius: 999px;
  font-size: 9px;
  text-transform: uppercase;
  letter-spacing: .1em;
  white-space: nowrap;
}
.ai-activity-replied {
  border-color: var(--blue-d);
  background: var(--blue-t);
  color: var(--blue-d);
}
.ai-activity-queued {
  border-color: var(--line);
  background: var(--line2);
}
.ai-activity-skipped {
  border-color: var(--line2);
  background: none;
}
.ai-activity-error {
  border-color: var(--red);
  background: color-mix(in srgb, var(--red) 12%, var(--card));
  color: var(--red);
}

.ai-activity-detail {
  min-width: 0;
  overflow-wrap: anywhere;
}
.ai-activity-when {
  flex: none;
  opacity: .85;
  white-space: nowrap;
}

.ai-activity-empty {
  padding: 6px 2px;
  color: var(--grey);
}

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

/* ---------- <=1100px: chat on top, automations underneath ----------------- */
@media (max-width: 1100px) {
  .layout[data-view="ai"] .ai-page {
    grid-column: 1 / -1;
    grid-template-columns: 1fr;
    grid-template-rows: minmax(0, 1fr) auto;
  }

  .ai-side {
    max-height: 40vh;
    padding-bottom: 16px;
    border-left: 0;
    border-top: var(--b) solid var(--ink);
  }
}

/* ---------- <=680px: one column, tighter gutters ------------------------- */
@media (max-width: 680px) {
  .ai-chat-head { padding: 12px 14px; }
  .ai-thread { padding: 14px 14px 8px; }
  .ai-suggest { padding: 4px 14px 10px; }
  .ai-inputrow { padding: 0 14px; }
  .ai-msg { max-width: 88%; }
  .ai-side { max-height: 42vh; }

  /* The shell already subtracts the tab bar and its safe-area inset from the
     layout height; this keeps the last line clear of a thumb. */
  .ai-status { padding: 8px 14px calc(10px + env(safe-area-inset-bottom, 0px)); }
}

/* ---------- Print: the ai page is a screen surface, never a document ------ */
@media print {
  .ai-page { display: none !important; }
}

/* Reduced motion is handled globally by styles.css (all transitions are
   collapsed); nothing here animates or scrolls smoothly on its own. */
