/* ============================================================
   TradeRail Field Portal — PR360 TRai Chat Engine styles
   Brand: Primary Orange #FF6B00, Wordmark Dark #1A1A2E.
   No emoji anywhere. Icons are SVG/CSS only.

   Color scheme (INVERTED):
   - Outer shell / header / sidebar: #1A1A2E (dark navy)
   - Chat thread area: #FFFFFF (white)
   - User bubbles: #FF6B00 orange, white text
   - TRai bubbles: #F4F4F7 light card, #1A1A2E text, orange left border
   - Orange buttons throughout
   ============================================================ */

/* Default: hidden. Only shown when host is .trai-authorized. */
.trai-float-btn { display: none; }
.trai-authorized .trai-float-btn { display: block; }

/* ---- Floating button ---- */
.trai-float-btn {
  position: fixed;
  right: 24px;
  bottom: 24px;
  width: 84px;
  height: 84px;
  border-radius: 50%;
  background: none;
  border: none;
  padding: 0;
  cursor: pointer;
  z-index: 8900;
  transition: transform 160ms ease, box-shadow 160ms ease;
  overflow: hidden;
  box-shadow: 0 0 0 3px #FF6B00, 0 4px 18px rgba(255,107,0,0.45), 0 2px 10px rgba(0,0,0,0.35);
}
.trai-float-btn img {
  width: 100%;
  height: 100%;
  display: block;
  object-fit: cover;
  mix-blend-mode: normal;
}
.trai-float-btn:hover {
  transform: scale(1.08);
  box-shadow: 0 0 0 4px #FF6B00, 0 6px 24px rgba(255,107,0,0.6), 0 4px 14px rgba(0,0,0,0.4);
}
.trai-float-label { display: none; }

.trai-badge {
  position: absolute;
  top: 2px;
  right: 2px;
  min-width: 18px;
  height: 18px;
  padding: 0 4px;
  border-radius: 9px;
  background: #FF6B00;
  color: #fff;
  font-size: 10px;
  line-height: 18px;
  text-align: center;
  font-weight: bold;
  box-shadow: 0 1px 4px rgba(0,0,0,0.3);
  pointer-events: none;
}

/* ---- TRai Panel — full-page overlay ---- */
.trai-panel {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  width: 100%;
  /* Wider when doc preview is open */
  max-width: 1100px;
  margin: 0 auto;
  background: #1A1A2E;
  border: none;
  border-left: 1px solid rgba(255,107,0,0.2);
  border-right: 1px solid rgba(255,107,0,0.2);
  z-index: 9200;
  display: flex;
  flex-direction: column;
  transform: translateY(100%);
  transition: transform 280ms cubic-bezier(0.4, 0, 0.2, 1);
  font-family: Arial, sans-serif;
  box-shadow: -4px 0 40px rgba(0,0,0,0.5), 4px 0 40px rgba(0,0,0,0.5);
}
.trai-panel.open { transform: translateY(0); }
.trai-panel.open::before {
  content: '';
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.55);
  z-index: -1;
  pointer-events: none;
}
.trai-panel.minimized { transform: translateY(calc(100% - 52px)); }

@media (max-width: 900px) {
  .trai-panel { max-width: 100%; border-left: none; border-right: none; }
}

/* ---- Header — dark navy, TradeRail wordmark left ---- */
.trai-header {
  background: #1A1A2E;
  border-bottom: 2px solid #FF6B00;
  padding: 0 20px;
  height: 54px;
  display: flex;
  align-items: center;
  gap: 14px;
  flex-shrink: 0;
}
/* Wordmark SVG container */
.trai-wordmark {
  display: flex;
  align-items: center;
  flex-shrink: 0;
}
.trai-wordmark svg {
  height: 26px;
  width: auto;
}
/* Divider between wordmark and TRai label */
.trai-header-divider {
  width: 1px;
  height: 22px;
  background: rgba(255,255,255,0.18);
  flex-shrink: 0;
}
.trai-title {
  color: #FF6B00;
  font-weight: bold;
  font-size: 18px;
  font-family: Arial, sans-serif;
  flex: 1;
  letter-spacing: 0.5px;
}
.trai-context-line {
  color: rgba(255,255,255,0.45);
  font-size: 12px;
  white-space: nowrap;
}
.trai-header-actions {
  display: flex;
  gap: 6px;
  align-items: center;
}
.trai-min-btn, .trai-close-btn {
  background: rgba(255,255,255,0.08);
  border: none;
  color: rgba(255,255,255,0.7);
  font-size: 16px;
  cursor: pointer;
  width: 32px;
  height: 32px;
  border-radius: 6px;
  display: flex;
  align-items: center;
  justify-content: center;
}
.trai-min-btn:hover, .trai-close-btn:hover {
  color: #FF6B00;
  background: rgba(255,107,0,0.18);
}

/* ---- Rate-limit banner ---- */
.trai-rate-banner {
  padding: 8px 20px;
  font-size: 12px;
  background: rgba(255,107,0,0.12);
  color: #FFB366;
  border-bottom: 1px solid rgba(255,107,0,0.25);
  flex-shrink: 0;
}
.trai-rate-banner.hard-stop {
  background: rgba(220,53,69,0.15);
  color: #FF8080;
  border-bottom-color: rgba(220,53,69,0.3);
}

/* ---- Body wrap: history | main chat | doc preview ---- */
.trai-body-wrap {
  display: flex;
  flex: 1 1 auto;
  overflow: hidden;
}

/* ---- Recent chats sidebar — dark navy ---- */
.trai-history {
  width: 210px;
  min-width: 210px;
  background: #13132A;
  border-right: 1px solid rgba(255,107,0,0.12);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  flex-shrink: 0;
}
.trai-history-header {
  padding: 12px 14px 8px;
  font-size: 10px;
  text-transform: uppercase;
  letter-spacing: 0.9px;
  color: rgba(255,255,255,0.3);
  font-weight: bold;
  border-bottom: 1px solid rgba(255,255,255,0.05);
  flex-shrink: 0;
}
.trai-history-new-btn {
  display: block;
  width: calc(100% - 24px);
  margin: 10px 12px 6px;
  padding: 8px 0;
  background: #FF6B00;
  border: none;
  border-radius: 6px;
  color: #fff;
  font-size: 12px;
  font-weight: bold;
  cursor: pointer;
  text-align: center;
  letter-spacing: 0.3px;
  flex-shrink: 0;
}
.trai-history-new-btn:hover { background: #e05e00; }
.trai-history-list {
  flex: 1 1 auto;
  overflow-y: auto;
  padding: 4px 0;
}
.trai-history-list::-webkit-scrollbar { width: 3px; }
.trai-history-list::-webkit-scrollbar-track { background: transparent; }
.trai-history-list::-webkit-scrollbar-thumb { background: rgba(255,107,0,0.2); border-radius: 2px; }
.trai-history-item {
  padding: 9px 14px;
  font-size: 12px;
  color: rgba(255,255,255,0.5);
  cursor: pointer;
  border-left: 2px solid transparent;
  line-height: 1.3;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  transition: background 120ms, color 120ms;
}
.trai-history-item:hover {
  background: rgba(255,107,0,0.08);
  color: rgba(255,255,255,0.85);
  border-left-color: rgba(255,107,0,0.4);
}
.trai-history-item.active {
  background: rgba(255,107,0,0.14);
  color: #FF6B00;
  border-left-color: #FF6B00;
}
.trai-history-empty {
  padding: 16px 14px;
  font-size: 11px;
  color: rgba(255,255,255,0.22);
  text-align: center;
  line-height: 1.6;
}
@media (max-width: 640px) { .trai-history { display: none; } }

/* ---- Main chat column — WHITE background ---- */
.trai-main {
  flex: 1 1 auto;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  background: #FFFFFF;
  /* Thin orange top stripe under header to signal the white zone */
  border-top: none;
}

/* ---- Messages — white background ---- */
.trai-messages {
  flex: 1 1 auto;
  overflow-y: auto;
  padding: 24px 28px;
  box-sizing: border-box;
  background: #FFFFFF;
}
.trai-messages::-webkit-scrollbar { width: 5px; }
.trai-messages::-webkit-scrollbar-track { background: #f0f0f0; }
.trai-messages::-webkit-scrollbar-thumb { background: rgba(255,107,0,0.3); border-radius: 3px; }

.trai-msg {
  margin-bottom: 16px;
  font-size: 14px;
  line-height: 1.5;
  display: flex;
}
/* User bubbles — orange, white text, right-aligned */
.trai-msg-user { justify-content: flex-end; }
.trai-msg-user .trai-msg-body {
  display: inline-block;
  background: #FF6B00;
  color: #fff;
  border-radius: 16px 16px 4px 16px;
  padding: 10px 14px;
  max-width: 72%;
  font-weight: 500;
  box-shadow: 0 2px 8px rgba(255,107,0,0.25);
}
/* TRai bubbles — light gray card, dark text, orange left accent */
.trai-msg-assistant { justify-content: flex-start; }
.trai-msg-assistant .trai-msg-body {
  display: inline-block;
  background: #F4F4F7;
  color: #1A1A2E;
  border-radius: 16px 16px 16px 4px;
  padding: 10px 14px;
  max-width: 78%;
  border-left: 3px solid #FF6B00;
  box-shadow: 0 1px 4px rgba(0,0,0,0.08);
  line-height: 1.55;
}
.trai-msg-assistant .trai-msg-label {
  display: block;
  font-size: 10px;
  text-transform: uppercase;
  letter-spacing: 0.8px;
  color: #FF6B00;
  margin-bottom: 4px;
  font-weight: bold;
}

/* File attachment tag */
.trai-attachment-tag {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  background: rgba(255,107,0,0.08);
  border: 1px solid rgba(255,107,0,0.3);
  border-radius: 6px;
  padding: 3px 8px;
  font-size: 11px;
  color: #FF6B00;
  margin-top: 6px;
}

/* ---- Quick-action chips — on white background ---- */
.trai-chips {
  padding: 0 28px 10px;
  display: flex;
  flex-wrap: wrap;
  gap: 7px;
  flex-shrink: 0;
  background: #FFFFFF;
  border-top: 1px solid #F0F0F0;
  padding-top: 10px;
}
.trai-chip {
  border: 1.5px solid #FF6B00;
  color: #FF6B00;
  background: #fff;
  border-radius: 16px;
  padding: 5px 13px;
  font-size: 12px;
  cursor: pointer;
  transition: background 120ms, color 120ms;
  white-space: nowrap;
  font-weight: 500;
}
.trai-chip:hover {
  background: #FF6B00;
  color: #fff;
}

/* ---- Two-tap confirm card ---- */
.trai-confirm-card {
  background: #FFF3E0;
  border-left: 4px solid #FF6B00;
  padding: 12px 14px;
  border-radius: 8px;
  margin: 8px 0;
}
.trai-confirm-desc { font-size: 13px; color: #1A1A2E; margin-bottom: 10px; }
.trai-confirm-actions { display: flex; gap: 8px; }
.trai-confirm-yes {
  background: #FF6B00;
  color: #fff;
  border: none;
  border-radius: 6px;
  padding: 6px 16px;
  font-size: 12px;
  font-weight: bold;
  cursor: pointer;
}
.trai-confirm-yes:hover { background: #e05e00; }
.trai-confirm-no {
  background: #fff;
  color: #1A1A2E;
  border: 1px solid #ddd;
  border-radius: 6px;
  padding: 6px 16px;
  font-size: 12px;
  cursor: pointer;
}
.trai-confirm-no:hover { background: #f5f5f5; }

/* ---- Input row — dark navy, matching outer shell ---- */
.trai-input-row {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 12px 20px 14px;
  border-top: 1px solid rgba(255,107,0,0.15);
  background: #1A1A2E;
  flex-shrink: 0;
}
.trai-upload-btn {
  flex: 0 0 auto;
  width: 38px;
  height: 38px;
  border-radius: 50%;
  background: rgba(255,107,0,0.12);
  border: 1px solid rgba(255,107,0,0.35);
  color: #FF6B00;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background 120ms;
  position: relative;
}
.trai-upload-btn:hover { background: rgba(255,107,0,0.24); }
.trai-upload-input {
  position: absolute;
  inset: 0;
  opacity: 0;
  cursor: pointer;
  width: 100%;
  height: 100%;
  font-size: 0;
}
.trai-pending-file {
  display: flex;
  align-items: center;
  gap: 6px;
  background: rgba(255,107,0,0.1);
  border: 1px solid rgba(255,107,0,0.3);
  border-radius: 20px;
  padding: 4px 10px;
  font-size: 12px;
  color: #FF6B00;
  margin-bottom: 6px;
  max-width: 260px;
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
}
.trai-pending-file-remove {
  cursor: pointer;
  color: rgba(255,107,0,0.6);
  font-size: 14px;
  flex-shrink: 0;
  line-height: 1;
  background: none;
  border: none;
  padding: 0;
}
.trai-pending-file-remove:hover { color: #FF6B00; }
.trai-input-wrap {
  flex: 1 1 auto;
  display: flex;
  flex-direction: column;
}
.trai-input {
  width: 100%;
  border: 1.5px solid rgba(255,255,255,0.15);
  border-radius: 24px;
  padding: 11px 16px;
  font-size: 14px;
  font-family: Arial, sans-serif;
  outline: none;
  background: rgba(255,255,255,0.08);
  color: rgba(255,255,255,0.92);
  box-sizing: border-box;
  transition: border-color 160ms, background 160ms;
}
.trai-input::placeholder { color: rgba(255,255,255,0.3); }
.trai-input:focus {
  border-color: #FF6B00;
  background: rgba(255,255,255,0.11);
}
.trai-send-btn {
  flex: 0 0 auto;
  width: 42px;
  height: 42px;
  border-radius: 50%;
  background: #FF6B00;
  color: #fff;
  border: none;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 2px 8px rgba(255,107,0,0.4);
  transition: background 120ms;
}
.trai-send-btn:hover { background: #e05e00; }
.trai-send-btn:disabled { background: rgba(255,255,255,0.12); cursor: default; box-shadow: none; }

/* ---- Document preview pane ---- */
.trai-doc-pane {
  width: 0;
  min-width: 0;
  background: #FAFAFA;
  border-left: 2px solid #FF6B00;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  flex-shrink: 0;
  transition: width 260ms cubic-bezier(0.4, 0, 0.2, 1), min-width 260ms cubic-bezier(0.4, 0, 0.2, 1);
}
.trai-doc-pane.open {
  width: 380px;
  min-width: 380px;
}
@media (max-width: 900px) {
  .trai-doc-pane.open {
    position: fixed;
    inset: 0;
    width: 100%;
    min-width: 100%;
    z-index: 9300;
    border-left: none;
  }
}
.trai-doc-header {
  background: #1A1A2E;
  padding: 12px 16px;
  display: flex;
  align-items: center;
  gap: 10px;
  border-bottom: 2px solid #FF6B00;
  flex-shrink: 0;
}
.trai-doc-title {
  flex: 1;
  color: #fff;
  font-size: 13px;
  font-weight: bold;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
/* "Send to Approve" button — injects confirm card into chat */
.trai-doc-approve-btn {
  background: #FF6B00;
  color: #fff;
  border: none;
  border-radius: 6px;
  padding: 6px 12px;
  font-size: 12px;
  font-weight: bold;
  cursor: pointer;
  white-space: nowrap;
}
.trai-doc-approve-btn:hover { background: #e05e00; }
.trai-doc-close-btn {
  background: rgba(255,255,255,0.08);
  border: none;
  color: rgba(255,255,255,0.6);
  width: 28px;
  height: 28px;
  border-radius: 6px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 14px;
}
.trai-doc-close-btn:hover { color: #FF6B00; background: rgba(255,107,0,0.15); }
/* Document body — white, letterhead style */
.trai-doc-body {
  flex: 1 1 auto;
  overflow-y: auto;
  padding: 24px 22px;
  background: #fff;
  font-family: Arial, sans-serif;
}
.trai-doc-body::-webkit-scrollbar { width: 4px; }
.trai-doc-body::-webkit-scrollbar-thumb { background: rgba(255,107,0,0.2); border-radius: 2px; }
/* Letterhead top strip */
.trai-doc-letterhead {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding-bottom: 14px;
  margin-bottom: 16px;
  border-bottom: 2px solid #FF6B00;
}
.trai-doc-letterhead-logo {
  display: flex;
  align-items: center;
}
.trai-doc-letterhead-logo svg { height: 22px; width: auto; }
.trai-doc-letterhead-date {
  font-size: 11px;
  color: #888;
}
.trai-doc-doc-title {
  font-size: 16px;
  font-weight: bold;
  color: #1A1A2E;
  margin-bottom: 14px;
}
.trai-doc-content {
  font-size: 13px;
  color: #2A2A2A;
  line-height: 1.7;
  white-space: pre-wrap;
}
/* Signature block */
.trai-doc-sig {
  margin-top: 24px;
  padding-top: 14px;
  border-top: 1px solid #eee;
  font-size: 12px;
  color: #666;
}
/* Request changes bar at bottom of doc pane */
.trai-doc-feedback {
  padding: 10px 14px;
  border-top: 1px solid #eee;
  background: #FAFAFA;
  display: flex;
  gap: 8px;
  align-items: center;
  flex-shrink: 0;
}
.trai-doc-feedback-input {
  flex: 1;
  border: 1px solid #ddd;
  border-radius: 16px;
  padding: 7px 12px;
  font-size: 12px;
  font-family: Arial, sans-serif;
  outline: none;
  color: #1A1A2E;
}
.trai-doc-feedback-input:focus { border-color: #FF6B00; }
.trai-doc-feedback-btn {
  background: #FF6B00;
  color: #fff;
  border: none;
  border-radius: 16px;
  padding: 7px 14px;
  font-size: 12px;
  font-weight: bold;
  cursor: pointer;
  white-space: nowrap;
}
.trai-doc-feedback-btn:hover { background: #e05e00; }

/* ---- Global search (ribbon) ---- */
.trai-search { position: relative; display: none; }
.trai-authorized .trai-search { display: inline-block; }
.trai-search-input {
  border: 1px solid #ccc;
  border-radius: 16px;
  padding: 6px 12px;
  font-size: 13px;
  min-width: 220px;
  font-family: Arial, sans-serif;
}
.trai-search-input:focus { outline: none; border-color: #FF6B00; }
.trai-search-dropdown {
  position: absolute;
  top: 100%;
  left: 0;
  right: 0;
  background: #1A1A2E;
  border: 1px solid rgba(255,107,0,0.2);
  border-radius: 8px;
  margin-top: 4px;
  max-height: 360px;
  overflow-y: auto;
  z-index: 9050;
  box-shadow: 0 8px 24px rgba(0,0,0,0.4);
}
.trai-search-group-label {
  font-size: 11px;
  text-transform: uppercase;
  color: rgba(255,255,255,0.3);
  padding: 8px 12px 4px;
  letter-spacing: 0.6px;
}
.trai-search-result {
  padding: 8px 12px;
  font-size: 13px;
  cursor: pointer;
  color: rgba(255,255,255,0.8);
}
.trai-search-result:hover,
.trai-search-result.active { background: rgba(255,107,0,0.12); color: #FF6B00; }

/* Hide old Lily FAB */
#lilyFab { display: none !important; }
/* Hide Customize Home button */
.hal-toggle { display: none !important; }

/* PR388: hide TRai surface for field roles (foreman, field_tech) */
body.trai-hidden #trai-float-btn,
body.trai-hidden .trai-float-btn,
body.trai-hidden .trai-panel,
body.trai-hidden .trai-backdrop {
  display: none !important;
}
