/* Fully scoped styles under .fac-widget to avoid site conflicts */
.fac-widget {
  /* Use Peyda if loaded by the theme, otherwise inherit */
  font-family: 'PeydaWebFaNum', 'Peyda', var(--ast-global-font-family, inherit), inherit;
  color: var(--fac-color-text, #0a0a0a);
  /* Theme variables (Light defaults) */
  --fac-color-text: #0a0a0a;
  --fac-color-muted: #334155;
  --fac-color-note: #64748b;
  --fac-bg-surface: #ffffff;
  --fac-bg-chat: #f8fafc;
  --fac-border: #e5e7eb;
  --fac-fieldset-border: #eef2f7;
  --fac-assistant-bubble-bg: #ffffff;
  --fac-assistant-bubble-text: #0a0a0a;
  --fac-input-bg: #ffffff;
  --fac-input-text: #0a0a0a;
  --fac-focus: #86efac;
  --fac-user-bubble-bg: #059669;
  --fac-user-bubble-text: #ffffff;

  /* Modern accent and surfaces */
  --fac-accent-1: #34d399; /* emerald 400 */
  --fac-accent-2: #10b981; /* emerald 500 */
  --fac-ring-color: rgba(16,185,129,0.25);
  --fac-card-bg-from: rgba(255,255,255,0.78);
  --fac-card-bg-to: rgba(248,250,252,0.78);
  --fac-input-bg-start: #ffffff;
  --fac-input-bg-end: #f8fafc;
  --fac-placeholder: #94a3b8;

  /* Glass variables */
  --fac-glass-bg: rgba(255, 255, 255, 0);
  --fac-glass-bg-2: rgba(255, 255, 255, 0);
  --fac-glass-border: rgba(255, 255, 255, 0.429);
}

/* Mobile-only: Fix parent containers */
@media screen and (max-width: 768px) {
  .fac-widget-container,
  div:has(> .fac-widget),
  section:has(> .fac-widget),
  article:has(> .fac-widget),
  .entry-content:has(> .fac-widget),
  .post-content:has(> .fac-widget),
  .page-content:has(> .fac-widget) {
    max-width: 100% !important;
    overflow-x: hidden !important;
  }
  
  /* Critical viewport fix for mobile */
  body:has(.fac-widget) {
    overflow-x: hidden !important;
  }
}

.fac-widget * { 
  box-sizing: border-box;
}

/* Override theme styles that might cause overflow */
.fac-widget {
  width: 100%;
  max-width: 100%;
  overflow-x: hidden;
}
.fac-widget .fac-grid,
.fac-widget .fac-chat,
.fac-widget .fac-form {
  width: 100%;
  max-width: 100%;
}

/* Dark theme overrides via attribute */
.fac-widget[data-fac-theme="dark"] {
  --fac-color-text: #e5e7eb;
  --fac-color-muted: #d1d5db;
  --fac-color-note: #94a3b8;
  --fac-bg-surface: #0b0f14;
  --fac-bg-chat: #0b0f14;
  --fac-border: #1f2937;
  --fac-fieldset-border: #111827;
  --fac-assistant-bubble-bg: #0b0f14;
  --fac-assistant-bubble-text: #e5e7eb;
  --fac-input-bg: #0b0f14;
  --fac-input-text: #e5e7eb;

  --fac-accent-1: #22c55e; /* emerald 500 */
  --fac-accent-2: #16a34a; /* emerald 600 */
  --fac-ring-color: rgba(34,197,94,0.35);
  --fac-card-bg-from: rgba(11,15,20,0.6);
  --fac-card-bg-to: rgba(11,15,20,0.55);
  --fac-input-bg-start: #0e141b;
  --fac-input-bg-end: #0b0f14;
  --fac-placeholder: #9aa4b2;

  --fac-glass-bg: rgba(17,24,39,0.46);
  --fac-glass-bg-2: rgba(17,24,39,0.30);
  --fac-glass-border: rgba(255, 255, 255, 0.467);
}

/* Fallback: respect system dark if no explicit theme attribute set */
@media (prefers-color-scheme: dark) {
  .fac-widget:not([data-fac-theme]) {
    --fac-color-text: #e5e7eb;
    --fac-color-muted: #d1d5db;
    --fac-color-note: #94a3b8;
    --fac-bg-surface: #0b0f14;
    --fac-bg-chat: #0b0f14;
    --fac-border: #1f2937;
    --fac-fieldset-border: #111827;
    --fac-assistant-bubble-bg: #0b0f14;
    --fac-assistant-bubble-text: #e5e7eb;
    --fac-input-bg: #0b0f14;
    --fac-input-text: #e5e7eb;
    --fac-accent-1: #22c55e;
    --fac-accent-2: #16a34a;
    --fac-ring-color: rgba(34,197,94,0.35);
    --fac-card-bg-from: rgba(11,15,20,0.6);
    --fac-card-bg-to: rgba(11,15,20,0.55);
    --fac-input-bg-start: #0e141b;
    --fac-input-bg-end: #0b0f14;
    --fac-placeholder: #9aa4b2;

    --fac-glass-bg: rgba(17,24,39,0.46);
    --fac-glass-bg-2: rgba(17,24,39,0.30);
    --fac-glass-border: rgba(255,255,255,0.06);
  }
}

.fac-widget .fac-grid { display: grid; gap: 16px; grid-template-columns: 1fr; }

/* Top toolbar (reset) */
.fac-widget .fac-toolbar { display: flex; justify-content: flex-end; align-items: center; gap: 8px; }
.fac-widget .fac-reset {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 12px 24px;
  border-radius: 12px;
  border: none;
  cursor: pointer;
  font-size: 15px;
  font-weight: 600;
  letter-spacing: 0.5px;
  background: linear-gradient(135deg, #ef4444, #dc2626); /* red gradient */
  color: #fff;
  box-shadow: 0 4px 12px rgba(239, 68, 68, 0.28), 0 2px 4px rgba(0, 0, 0, 0.08);
  position: relative;
  overflow: hidden;
}
.fac-widget .fac-reset::after {
  content: '';
  position: absolute;
  top: 0; left: -100%;
  width: 100%; height: 100%;
  background: linear-gradient(90deg, transparent, rgba(255,255,255,0.22), transparent);
  transition: left .5s;
  pointer-events: none;
  border-radius: inherit;
}
.fac-widget .fac-reset:hover::after { left: 100%; }
.fac-widget .fac-reset:hover {
  background: linear-gradient(135deg, #dc2626, #b91c1c);
  transform: translateY(-2px);
  box-shadow: 0 6px 18px rgba(220,38,38,0.36), 0 4px 8px rgba(0,0,0,0.12);
}
.fac-widget .fac-reset:active { transform: translateY(0); box-shadow: 0 3px 10px rgba(220,38,38,0.28), 0 2px 4px rgba(0,0,0,0.10); }
.fac-widget[data-fac-theme="dark"] .fac-reset {
  background: linear-gradient(135deg, #dc2626, #b91c1c);
  box-shadow: 0 4px 12px rgba(220,38,38,0.4), 0 2px 4px rgba(0,0,0,0.26);
}
.fac-widget[data-fac-theme="dark"] .fac-reset:hover {
  background: linear-gradient(135deg, #b91c1c, #991b1b);
  box-shadow: 0 6px 20px rgba(220,38,38,0.5), 0 4px 8px rgba(0,0,0,0.3);
}
/* Icon for reset button: circular arrow */
.fac-widget .fac-reset::before {
  content: '';
  width: 18px;
  height: 18px;
  display: inline-block;
  background-repeat: no-repeat;
  background-position: center;
  background-size: 18px 18px;
  filter: drop-shadow(0 0 0 rgba(0,0,0,0));
  transition: transform .35s ease;
  transform-origin: 50% 50%;
  margin-inline-end: 8px;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='20' height='20' viewBox='0 0 24 24' fill='none' stroke='%23ffffff' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpolyline points='1 4 1 10 7 10'/%3E%3Cpath d='M3.51 15a9 9 0 1 0 2.13-9.36L1 10'/%3E%3C/svg%3E");
}
.fac-widget .fac-reset:hover::before { transform: rotate(-180deg); }

.fac-widget .fac-chat {
  background: var(--fac-bg-chat);
  border: 1px solid var(--fac-border);
  border-radius: 16px;
  padding: 12px;
  /* Subtle decorative wash */
  background-image:
    radial-gradient(700px 300px at 100% -10%, rgba(16,185,129,0.08), transparent),
    radial-gradient(500px 240px at 0% 110%, rgba(59,130,246,0.07), transparent);
  background-blend-mode: screen;
  /* leave room for sticky form at bottom */
  padding-bottom: calc(12px + var(--fac-form-h, 260px) + var(--fac-chatbar-h, 0px));
}

.fac-widget .fac-row { display: flex; margin: 8px 0; }
.fac-widget .fac-left { justify-content: flex-start; }
.fac-widget .fac-right { justify-content: flex-end; }
.fac-widget .fac-bubble {
  max-width: 85%;
  padding: 10px 14px;
  border-radius: 16px;
  line-height: 1.9;
  box-shadow: 0 1px 1px rgba(0,0,0,0.04);
  font-size: inherit;
  word-wrap: break-word;
  overflow-wrap: break-word;
  /* Desktop: contain tables within bubble */
  overflow: hidden;
  /* Ensure tables don't push bubble beyond max-width */
  min-width: 0;
}
.fac-widget .fac-bubble .fac-math,
.fac-widget .fac-bubble .fac-math-block { font-family: ui-monospace, Menlo, Monaco, Consolas, "Liberation Mono", "Courier New", monospace; direction: ltr; }
.fac-widget .fac-bubble .fac-math { display: inline-block; padding: 0 .15em; border-radius: 6px; background: rgba(148,163,184,0.08); max-width: 100%; overflow-wrap: break-word; word-break: break-all; }
.fac-widget .fac-bubble .fac-math-block { margin: .5em 0; padding: .5em .6em; border-radius: 10px; background: rgba(148,163,184,0.08); overflow-x: auto; -webkit-overflow-scrolling: touch; max-width: 100%; }
.fac-widget .fac-assistant {
  background: var(--fac-assistant-bubble-bg);
  color: var(--fac-assistant-bubble-text);
  border: 1px solid var(--fac-border);
  border-top-left-radius: 6px;
}
.fac-widget .fac-user { background: var(--fac-user-bubble-bg); color: var(--fac-user-bubble-text); border-top-right-radius: 6px; }

.fac-widget .fac-form {
  /* Gradient border card */
  border-radius: 16px;
  padding: 16px;
  position: sticky;
  bottom: 0;
  z-index: 10;
  width: min(100%, 1200px);
  margin-inline: auto;
  border: 1px solid transparent;
  background:
    linear-gradient(180deg, var(--fac-bg-surface), var(--fac-bg-surface)) padding-box,
    linear-gradient(135deg, var(--fac-accent-1), var(--fac-accent-2)) border-box;
  box-shadow: 0 10px 20px rgba(2,6,23,0.06), 0 2px 6px rgba(2,6,23,0.05);
}
@supports (backdrop-filter: blur(1px)) {
  .fac-widget .fac-form { backdrop-filter: saturate(140%) blur(6px); }
}
.fac-widget .fac-form fieldset {
  border: 1px solid var(--fac-fieldset-border);
  padding: 12px;
  margin-bottom: 12px;
  border-radius: 12px;
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
  align-items: end;
  gap: 10px 12px;
  position: relative;
  background: linear-gradient(180deg, var(--fac-card-bg-from), var(--fac-card-bg-to));
  box-shadow: inset 0 1px 0 rgba(255,255,255,0.06), 0 1px 1px rgba(2,6,23,0.04);
}
/* Area inline row (width x length in meters) */
.fac-widget .fac-form .fac-area-row {
  display: flex;
  gap: 8px;
  align-items: center;
}
.fac-widget .fac-form .fac-area-row input {
  flex: 1 1 0;
  min-width: 120px;
}
/* Try to keep controls in the same row when possible */
.fac-widget .fac-grid { grid-auto-flow: row; }

@supports (backdrop-filter: blur(1px)) {
  .fac-widget .fac-form fieldset { backdrop-filter: saturate(150%) blur(8px); }
}
.fac-widget .fac-form legend { padding: 0 6px; font-weight: 700; font-size: inherit; color: var(--fac-color-muted); grid-column: 1 / -1; }
.fac-widget .fac-form label { display: grid; gap: 6px; font-size: inherit; color: var(--fac-color-muted); min-width: 0; }
.fac-widget .fac-form input, .fac-widget .fac-form select {
  width: 100%;
  border: 1px solid var(--fac-border);
  padding: 10px 12px;
  border-radius: 12px;
  outline: none;
  font-size: 14px;
  color: var(--fac-input-text);
  min-height: 44px;
  background: linear-gradient(180deg, var(--fac-input-bg-start), var(--fac-input-bg-end));
  box-shadow: inset 0 1px 0 rgba(255,255,255,0.6), 0 1px 2px rgba(2,6,23,0.04);
  transition: border-color .2s ease, box-shadow .2s ease, background-color .2s ease, transform .06s ease;
}
/* Autosizing inputs by content length */
.fac-widget .fac-form input.fac-autosize {
  width: auto;
  max-width: 100%;
  min-width: 12ch;
}
/* Inline row for EC dual inputs */
.fac-widget .fac-form .fac-ec-row {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
}
.fac-widget .fac-form .fac-ec-row input { flex: 1 1 0; min-width: 100px; }
.fac-widget .fac-form .fac-ec-row input.fac-autosize { flex: 0 0 auto; min-width: 100px; }
.fac-widget .fac-form .fac-ec-row .fac-tilde { opacity: .8; color: var(--fac-color-note); }
.fac-widget .fac-form .fac-ec-factor { width: auto; min-width: 88px; }
/* EC unit selector and preview */
.fac-widget .fac-form .fac-ec-unit { width: auto; min-width: 90px; }
.fac-widget .fac-form .fac-ec-prev { color: var(--fac-color-note); white-space: nowrap; flex: 0 0 auto; }

/* Lightweight tooltip with icon */
.fac-widget .fac-tip { position: relative; display: inline-flex; align-items: center; margin-inline-start: 6px; }
.fac-widget .fac-tip-icon {
  border: 0;
  width: 22px; height: 22px;
  border-radius: 999px;
  background: linear-gradient(135deg, var(--fac-accent-1), var(--fac-accent-2));
  color: #fff;
  font-weight: 700;
  line-height: 1;
  display: inline-flex; align-items: center; justify-content: center;
  cursor: help;
}
.fac-widget .fac-tip-content {
  position: absolute;
  bottom: calc(100% + 8px);
  inset-inline-end: 0;
  z-index: 20;
  min-width: 220px;
  max-width: min(70vw, 320px);
  padding: 10px 12px;
  border-radius: 12px;
  border: 1px solid var(--fac-border);
  background: var(--fac-bg-surface);
  color: var(--fac-color-text);
  box-shadow: 0 10px 18px rgba(2,6,23,0.10), 0 2px 6px rgba(2,6,23,0.08);
  opacity: 0; pointer-events: none; transform: translateY(8px);
  transition: opacity .16s ease, transform .16s ease;
}
.fac-widget .fac-tip:hover .fac-tip-content,
.fac-widget .fac-tip:focus-within .fac-tip-content { opacity: 1; pointer-events: auto; transform: translateY(0); }
.fac-widget .fac-tip-content svg { width: 100%; height: auto; margin-top: 6px; }
.fac-widget .fac-form select {
  -webkit-appearance: none;
  -moz-appearance: none;
  appearance: none;
  /* custom chevron + keep gradient background */
  background-image:
    url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='20' height='20' viewBox='0 0 24 24' fill='none' stroke='%2364758b' stroke-width='2.2' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpolyline points='6 9 12 15 18 9'/%3E%3C/svg%3E"),
    linear-gradient(180deg, var(--fac-input-bg-start), var(--fac-input-bg-end));
  background-repeat: no-repeat, no-repeat;
  background-size: 18px 18px, auto;
  background-position: right 12px center, 0 0;
  padding-inline-end: 36px; /* room for arrow in LTR */
}
.fac-widget[dir='rtl'] .fac-form select {
  background-position: left 12px center, 0 0;
  padding-inline-start: 36px; /* room for arrow in RTL */
}
.fac-widget .fac-form select option { background: var(--fac-bg-surface); color: var(--fac-input-text); }
.fac-widget .fac-form select:focus {
  background-image:
    url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='20' height='20' viewBox='0 0 24 24' fill='none' stroke='%2310b981' stroke-width='2.2' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpolyline points='6 9 12 15 18 9'/%3E%3C/svg%3E"),
    linear-gradient(180deg, var(--fac-input-bg-start), var(--fac-input-bg-start));
}
/* Dual-unit rows for spacing */
.fac-widget .fac-form .fac-du-row {
  display: flex;
  align-items: center;
  gap: 6px;
  flex-wrap: nowrap;
}
.fac-widget .fac-form .fac-du-row input[type="text"] { flex: 1 1 auto; min-width: 0; width: auto; max-width: 140px; }
.fac-widget .fac-form .fac-du-row input[type="text"].fac-autosize { flex: 0 0 auto; min-width: 90px; }
/* Make unit selector compact inside dual-unit rows */
.fac-widget .fac-form .fac-du-row select { width: auto; min-width: 64px; flex: 0 0 auto; }
/* Hide conversion preview for spacing */
.fac-widget .fac-form .fac-conv { display: none; color: var(--fac-color-note); font-size: .95em; white-space: nowrap; flex: 0 0 auto; }

/* Slope slider row */
.fac-widget .fac-form .fac-slope-row { display: grid; gap: 6px; }
.fac-widget .fac-form .fac-slope-row input[type="range"] { width: 100%; }
.fac-widget .fac-form .fac-slope-hints { display: flex; justify-content: space-between; font-size: .9em; color: var(--fac-color-note); }

/* Friendly warnings */
.fac-widget .fac-warnings { margin-top: 8px; display: grid; gap: 8px; }
.fac-widget .fac-warn {
  display: flex; align-items: center; gap: 8px;
  padding: 10px 12px; border-radius: 12px;
  background: rgba(245, 158, 11, 0.10); border: 1px solid rgba(245, 158, 11, 0.35);
  color: #92400e;
}
.fac-widget[data-fac-theme="dark"] .fac-warn { background: rgba(245, 158, 11, 0.15); border-color: rgba(245, 158, 11, 0.45); color: #fbbf24; }
/* Hide default arrow in IE/Edge Legacy */
.fac-widget .fac-form select::-ms-expand { display: none; }
.fac-widget .fac-form input::placeholder, .fac-widget .fac-form select::placeholder { color: var(--fac-placeholder); opacity: .9; }
.fac-widget .fac-form label:has(> input:focus),
.fac-widget .fac-form label:has(> select:focus) { color: var(--fac-accent-2); }
.fac-widget .fac-form input:focus, .fac-widget .fac-form select:focus,
.fac-widget .fac-form input:focus-visible, .fac-widget .fac-form select:focus-visible { border-color: var(--fac-accent-2); box-shadow: 0 0 0 4px var(--fac-ring-color), 0 1px 2px rgba(2,6,23,0.06); background: linear-gradient(180deg, var(--fac-input-bg-start), var(--fac-input-bg-start)); }
.fac-widget .fac-form input:hover, .fac-widget .fac-form select:hover { border-color: color-mix(in srgb, var(--fac-border) 70%, var(--fac-accent-2)); }
.fac-widget .fac-form input:required:invalid, .fac-widget .fac-form select:required:invalid { border-color: #ef4444; }
.fac-widget .fac-form input:disabled, .fac-widget .fac-form select:disabled { opacity: .7; cursor: not-allowed; }
.fac-widget input, .fac-widget select, .fac-widget button, .fac-widget textarea { font: inherit; }
.fac-widget .fac-submit {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  width: 100%;
  padding: 14px 28px;
  background: linear-gradient(135deg, #4CAF50, #388E3C); /* Green gradient */
  color: white;
  border: none;
  border-radius: 12px;
  font-size: 15px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  box-shadow: 0 4px 12px rgba(76, 175, 80, 0.3), 0 2px 4px rgba(0, 0, 0, 0.1); /* Green shadow */
  font-family: 'Peyda', Tahoma, Arial, sans-serif;
  letter-spacing: 0.5px;
  position: relative;
  overflow: hidden;
}
.fac-widget .fac-submit::before {
  content: '';
  position: absolute;
  top: 0;
  left: -100%;
  width: 100%;
  height: 100%;
  background: linear-gradient(90deg, transparent, rgba(255, 255, 255, 0.2), transparent);
  transition: left 0.5s;
}
.fac-widget .fac-submit:hover::before {
  left: 100%;
}
.fac-widget .fac-submit:hover {
  background: linear-gradient(135deg, #388E3C, #2E7D32); /* Darker green hover */
  transform: translateY(-3px);
  box-shadow: 0 6px 20px rgba(76, 175, 80, 0.4), 0 4px 8px rgba(0, 0, 0, 0.15); /* Darker green shadow hover */
}
.fac-widget .fac-submit:active {
  transform: translateY(-1px);
  box-shadow: 0 4px 12px rgba(76, 175, 80, 0.3), 0 2px 4px rgba(0, 0, 0, 0.1); /* Green shadow active */
}
.fac-widget[data-fac-theme="dark"] .fac-submit {
  background: linear-gradient(135deg, #388E3C, #2E7D32); /* Dark green for dark theme */
  box-shadow: 0 4px 12px rgba(76, 175, 80, 0.4), 0 2px 4px rgba(0, 0, 0, 0.2); /* Dark green shadow */
}
.fac-widget[data-fac-theme="dark"] .fac-submit:hover {
  background: linear-gradient(135deg, #2E7D32, #1B5E20); /* Even darker green for dark theme hover */
  box-shadow: 0 6px 20px rgba(76, 175, 80, 0.5), 0 4px 8px rgba(0, 0, 0, 0.25); /* Even darker green shadow hover */
}
.fac-widget .fac-note { margin-top: 8px; color: var(--fac-color-note); font-size: inherit; }

/* Ensure code/pre inside widget also use site font (override theme's monospace rule) */
.fac-widget code, .fac-widget pre {
  font-family: inherit;
  background: transparent !important;
  color: inherit !important;
  padding: 0 !important;
  margin: 0 !important;
  border: 0 !important;
  border-radius: 0 !important;
  box-shadow: none !important;
}

/* If themes inject empty code/pre wrappers as siblings of the widget, hide them to prevent visual artifacts */
.fac-widget + code:empty, .fac-widget + pre:empty, .fac-widget + p:has(> code:empty), .fac-widget + p:has(> pre:empty) { display: none !important; }
code:empty + .fac-widget, pre:empty + .fac-widget, p:has(> code:empty) + .fac-widget, p:has(> pre:empty) + .fac-widget { display: block; }

/* Global neutralizer when widget is present: prevent theme code/pre styles from showing bars */
body:has(.fac-widget) code,
body:has(.fac-widget) pre {
  background: transparent !important;
  color: inherit !important;
  border: 0 !important;
  border-radius: 0 !important;
  padding: 0 !important;
  font-family: inherit !important;
  box-shadow: none !important;
}

/* Submit button loading state */
.fac-widget .fac-submit.is-loading {
  position: relative;
  color: transparent;
  pointer-events: none;
}
.fac-widget .fac-submit.is-loading::after {
  content: "";
  position: absolute;
  inset: 0;
  margin: auto;
  width: 18px;
  height: 18px;
  border-radius: 999px;
  border: 2px solid rgba(255,255,255,0.6);
  border-top-color: #ffffff;
  animation: fac-spin 0.8s linear infinite;
}
@keyframes fac-spin { to { transform: rotate(360deg); } }

/* Markdown heading styles inside bubbles */
.fac-widget .fac-bubble .fac-md-h1 { font-size: 1.25em; margin: .4em 0; }
.fac-widget .fac-bubble .fac-md-h2 { font-size: 1.2em;  margin: .4em 0; }
.fac-widget .fac-bubble .fac-md-h3 { font-size: 1.15em; margin: .4em 0; }
.fac-widget .fac-bubble .fac-md-h4 { font-size: 1.1em;  margin: .3em 0; }
.fac-widget .fac-bubble .fac-md-h5 { font-size: 1.05em; margin: .3em 0; }
.fac-widget .fac-bubble .fac-md-h6 { font-size: 1em;    margin: .2em 0; opacity: .9; }

/* Thinking status animated dots (Mexican wave) */
.fac-widget .fac-think { display: inline-flex; align-items: center; gap: 8px; }
.fac-widget .fac-think-label { opacity: .9; }
.fac-widget .fac-think-dots { display: inline-flex; gap: 5px; line-height: 1; }
.fac-widget .fac-think-dots i {
  width: 6px; height: 6px; background: currentColor; opacity: .85;
  display: inline-block; border-radius: 50%;
  animation: fac-think-wave 1.2s ease-in-out infinite;
}
.fac-widget .fac-think-dots i:nth-child(2) { animation-delay: .15s; }
.fac-widget .fac-think-dots i:nth-child(3) { animation-delay: .3s; }
@keyframes fac-think-wave {
  0%, 60%, 100% { transform: translateY(0); opacity: .8; }
  30% { transform: translateY(-6px); opacity: 1; }
}

/* Beautiful reveal animation for assistant messages */
.fac-widget .fac-bubble.fac-assistant.fac-reveal {
  animation: fac-bubble-reveal 0.6s cubic-bezier(0.34, 1.56, 0.64, 1) forwards;
  transform-origin: bottom left;
}
@keyframes fac-bubble-reveal {
  0% {
    opacity: 0;
    transform: scale(0.92) translateY(20px);
  }
  100% {
    opacity: 1;
    transform: scale(1) translateY(0);
  }
}

/* collapsing form after submit */
.fac-widget .fac-form.fac-form-collapsed {
  overflow: hidden;
  transition: height .28s ease;
  /* Hide card visuals when collapsed so only the toggle is visible */
  padding: 0;
  border: 0 !important;
  background: transparent !important;
  box-shadow: none !important;
}

/* Collapsed form toggle pill */
.fac-widget .fac-form-toggle {
  position: sticky;
  bottom: 10px;
  z-index: 11;
  width: min(100%, 480px);
  margin: 8px auto 0 auto;
  padding: 10px 14px;
  display: flex;
  align-items: center;
  gap: 10px;
  border-radius: 999px;
  border: 1px solid transparent;
  background:
    linear-gradient(180deg, var(--fac-bg-surface), var(--fac-bg-surface)) padding-box,
    linear-gradient(135deg, var(--fac-accent-1), var(--fac-accent-2)) border-box;
  color: var(--fac-color-text);
  cursor: pointer;
  box-shadow: 0 8px 18px rgba(2,6,23,0.10), 0 2px 6px rgba(2,6,23,0.08);
  text-align: center;
  justify-content: center;
  user-select: none;
}
.fac-widget .fac-form-toggle:hover { filter: brightness(1.02); }
.fac-widget .fac-form-toggle .fac-dot { width: 8px; height: 8px; border-radius: 999px; background: var(--fac-accent-2); box-shadow: 0 0 0 4px var(--fac-ring-color); }
.fac-widget .fac-form-toggle .fac-label { font-weight: 600; }

/* Sticky chat input bar */
.fac-widget .fac-chatbar {
  position: fixed;
  bottom: 12px;
  left: 50%;
  transform: translateX(-50%);
  z-index: 999;
  display: none; /* becomes flex after first submit */
  gap: 10px;
  align-items: center;
  width: min(900px, calc(100vw - 24px));
  margin: 0;
  padding: 8px 10px;
  border-radius: 16px;
  border: 1px solid var(--fac-glass-border);
  background: linear-gradient(180deg, var(--fac-glass-bg), var(--fac-glass-bg-2));
  box-shadow: 0 10px 18px rgba(2,6,23,0.10), 0 2px 6px rgba(2,6,23,0.08);
  backdrop-filter: saturate(160%) blur(12px);
}
.fac-widget .fac-chatbar input[type="text"] {
  flex: 1 1 auto;
  min-width: 0;
  border: 0px solid var(--fac-glass-border);
  border-radius: 12px;
  padding: 10px 12px;
  font-size: 14px;
  /* Match the chatbar glass background (no brighter inner area) */
  background: linear-gradient(180deg, var(--fac-glass-bg), var(--fac-glass-bg-2));
  color: var(--fac-input-text);
  outline: none;
  text-align: right;
  direction: rtl;
}
.fac-widget .fac-chatbar input[type="text"]:focus {
  border-color: var(--fac-accent-2);
  box-shadow: 0 0 0 3px var(--fac-ring-color);
}
.fac-widget .fac-chatbar input[type="text"]::placeholder { text-align: right; direction: rtl; }
.fac-widget .fac-chatbar button {
  flex: 0 0 auto;
  border: none;
  border-radius: 12px;
  padding: 14px 28px;
  background: linear-gradient(135deg, #2196F3, #1976D2);
  color: white;
  font-weight: 600;
  cursor: pointer;
  box-shadow: 0 4px 12px rgba(33, 150, 243, 0.3), 0 2px 4px rgba(0, 0, 0, 0.1);
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  font-size: 15px; /* Added font-size for consistency */
  font-family: 'Peyda', Tahoma, Arial, sans-serif; /* Added font-family for consistency */
  letter-spacing: 0.5px; /* Added letter-spacing for consistency */
  position: relative;
  overflow: hidden;
}
.fac-widget .fac-chatbar button::before {
  content: '';
  position: absolute;
  top: 0;
  left: -100%;
  width: 100%;
  height: 100%;
  background: linear-gradient(90deg, transparent, rgba(255, 255, 255, 0.2), transparent);
  transition: left 0.5s;
  pointer-events: none;
  border-radius: inherit;
}
.fac-widget .fac-chatbar button:hover::before {
  left: 100%;
}
.fac-widget .fac-chatbar button:hover {
  background: linear-gradient(135deg, #1976D2, #1565C0);
  transform: translateY(-3px);
  box-shadow: 0 6px 20px rgba(33, 150, 243, 0.4), 0 4px 8px rgba(0, 0, 0, 0.15);
}
.fac-widget .fac-chatbar button:active {
  transform: translateY(-1px);
  box-shadow: 0 4px 12px rgba(33, 150, 243, 0.3), 0 2px 4px rgba(0, 0, 0, 0.1);
}
.fac-widget[data-fac-theme="dark"] .fac-chatbar button {
  background: linear-gradient(135deg, #1E88E5, #1565C0);
  box-shadow: 0 4px 12px rgba(30, 136, 229, 0.4), 0 2px 4px rgba(0, 0, 0, 0.2);
}
.fac-widget[data-fac-theme="dark"] .fac-chatbar button:hover {
  background: linear-gradient(135deg, #1565C0, #0D47A1);
  box-shadow: 0 6px 20px rgba(30, 136, 229, 0.5), 0 4px 8px rgba(0, 0, 0, 0.25);
}
.fac-widget .fac-chatbar input[disabled],
.fac-widget .fac-chatbar button[disabled] { opacity: .6; cursor: default; }

/* Cursor hint tag near mouse when chat input is disabled */
.fac-widget .fac-cursor-hint {
  position: fixed;
  top: -9999px;
  left: -9999px;
  z-index: 1000;
  pointer-events: none;
  background: rgba(2,6,23,0.85);
  color: #fff;
  padding: 6px 8px;
  border-radius: 8px;
  font-size: 12px;
  box-shadow: 0 2px 6px rgba(2,6,23,0.25);
  white-space: nowrap;
  opacity: 0;
  transform: translate3d(0,0,0);
  transition: opacity .12s ease;
}

/* Tables inside bubbles */
.fac-widget .fac-bubble table.fac-table { width: 100%; max-width: 100%; border-collapse: collapse; margin: .4em 0; font-size: inherit; table-layout: auto; }
.fac-widget .fac-bubble table.fac-table thead th { background: rgba(148,163,184,0.10); color: var(--fac-color-text); font-weight: 700; }
.fac-widget .fac-bubble table.fac-table th, 
.fac-widget .fac-bubble table.fac-table td { 
  border: 1px solid var(--fac-border); 
  padding: 8px 10px; 
  vertical-align: top;
  /* Desktop: allow text wrapping for better fit */
  word-wrap: break-word;
  overflow-wrap: break-word;
}
/* Wrapper for horizontal scroll - simple for desktop */
.fac-widget .fac-bubble .fac-table-wrapper { 
  width: 100%; 
  max-width: 100%; 
  overflow-x: auto; 
  -webkit-overflow-scrolling: touch; 
  margin: .4em 0; 
  display: block;
  /* Prevent table from pushing bubble beyond max-width */
  min-width: 0;
}
.fac-widget .fac-bubble table.fac-table tbody tr:nth-child(even) { background: rgba(148,163,184,0.06); }

/* CSV block actions */
.fac-widget .fac-bubble .fac-csv { display: grid; gap: 8px; }
.fac-widget .fac-bubble .fac-csv-actions { display: flex; gap: 8px; justify-content: flex-start; }
.fac-widget .fac-bubble .fac-btn { border: 0; border-radius: 10px; padding: 6px 10px; background: linear-gradient(135deg, var(--fac-accent-1), var(--fac-accent-2)); color: #fff; cursor: pointer; font-weight: 700; }

/* Advanced settings panel */
.fac-widget .fac-advanced-wrapper { display: grid; gap: 8px; margin-bottom: 16px; }
.fac-widget .fac-advanced-toggle {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  width: max-content; /* Changed to max-content */
  padding: 14px 28px;
  background: linear-gradient(135deg, #2196F3, #1976D2);
  color: white;
  border: none;
  border-radius: 12px;
  font-size: 15px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  box-shadow: 0 4px 12px rgba(33, 150, 243, 0.3), 0 2px 4px rgba(0, 0, 0, 0.1);
  font-family: 'Peyda', Tahoma, Arial, sans-serif;
  letter-spacing: 0.5px;
  position: relative;
  overflow: hidden;
  margin-inline: auto; /* Centering the button */
}
.fac-widget .fac-advanced-toggle .fac-toggle-icon {
  transition: transform 0.3s ease;
  display: inline-block;
}
/* Pulse-down loop to draw attention when collapsed */
@media (prefers-reduced-motion: no-preference) {
  .fac-widget .fac-advanced-toggle[aria-expanded="false"] .fac-toggle-icon {
    animation: fac-arrow-bounce 1.1s ease-in-out infinite;
  }
  @keyframes fac-arrow-bounce {
    0% { transform: translateY(0); }
    40% { transform: translateY(6px); }
    60% { transform: translateY(4px); }
    100% { transform: translateY(0); }
  }
}
/* Stop animation and rotate when expanded */
.fac-widget .fac-advanced-toggle[aria-expanded="true"] .fac-toggle-icon {
  animation: none;
  transform: rotate(180deg);
}
/* Pressed state: quick move down */
.fac-widget .fac-advanced-toggle:active .fac-toggle-icon {
  transform: translateY(6px);
}
/* If expanded and pressed combine rotation + downward move */
.fac-widget .fac-advanced-toggle[aria-expanded="true"]:active .fac-toggle-icon {
  transform: rotate(180deg) translateY(6px);
}
.fac-widget .fac-advanced-toggle::before {
  content: '';
  position: absolute;
  top: 0;
  left: -100%;
  width: 100%;
  height: 100%;
  background: linear-gradient(90deg, transparent, rgba(255, 255, 255, 0.2), transparent);
  transition: left 0.5s;
}
.fac-widget .fac-advanced-toggle:hover::before {
  left: 100%;
}
.fac-widget .fac-advanced-toggle[aria-expanded="true"] { box-shadow: 0 6px 14px rgba(0,0,0,0.12); }
.fac-widget .fac-advanced-toggle:hover {
  background: linear-gradient(135deg, #1976D2, #1565C0);
  transform: translateY(-3px);
  box-shadow: 0 6px 20px rgba(33, 150, 243, 0.4), 0 4px 8px rgba(0, 0, 0, 0.15);
}
.fac-widget .fac-advanced-toggle:active {
  transform: translateY(-1px);
  box-shadow: 0 4px 12px rgba(33, 150, 243, 0.3), 0 2px 4px rgba(0, 0, 0, 0.1);
}
.fac-widget[data-fac-theme="dark"] .fac-advanced-toggle {
  background: linear-gradient(135deg, #1E88E5, #1565C0);
  box-shadow: 0 4px 12px rgba(30, 136, 229, 0.4), 0 2px 4px rgba(0, 0, 0, 0.2);
}
.fac-widget[data-fac-theme="dark"] .fac-advanced-toggle:hover {
  background: linear-gradient(135deg, #1565C0, #0D47A1);
  box-shadow: 0 6px 20px rgba(30, 136, 229, 0.5), 0 4px 8px rgba(0, 0, 0, 0.25);
}
.fac-widget .fac-advanced { display: block; transition: max-height .28s ease, opacity .2s ease; }
.fac-widget .fac-advanced[hidden] { display: none; opacity: 0; max-height: 0; overflow: hidden; }

/* Fix for images and media - always apply */
.fac-widget img,
.fac-widget video,
.fac-widget iframe {
  max-width: 100%;
  height: auto;
}

/* Allow horizontal scroll only where intended */
.fac-widget .fac-table-wrapper,
.fac-widget .fac-math-block {
  overflow-x: auto;
  max-width: 100%;
}

/* Desktop: ensure tables stay within bubble */
@media screen and (min-width: 769px) {
  .fac-widget .fac-bubble table.fac-table {
    /* Use fixed layout for predictable column widths */
    table-layout: fixed;
  }
  .fac-widget .fac-bubble table.fac-table th,
  .fac-widget .fac-bubble table.fac-table td {
    /* Allow wrapping in desktop for better fit */
    white-space: normal;
    word-wrap: break-word;
    overflow-wrap: break-word;
  }
}

/* Mobile responsive optimizations */
@media screen and (max-width: 768px) {
  /* Critical: Prevent all text overflow on mobile (but NOT in tables) */
  .fac-widget p,
  .fac-widget div:not(.fac-table-wrapper),
  .fac-widget span:not(table *),
  .fac-widget h1, .fac-widget h2, .fac-widget h3, .fac-widget h4, .fac-widget h5, .fac-widget h6,
  .fac-widget li {
    word-wrap: break-word !important;
    overflow-wrap: break-word !important;
    word-break: break-word !important;
  }
  
  /* Tables: keep text intact and use horizontal scroll instead */
  .fac-widget table.fac-table th,
  .fac-widget table.fac-table td {
    white-space: nowrap !important;
    word-break: keep-all !important;
    overflow-wrap: normal !important;
    word-wrap: normal !important;
  }
  
  /* Ensure table wrapper scrolls smoothly on mobile */
  .fac-widget .fac-bubble .fac-table-wrapper {
    overflow-x: auto !important;
    -webkit-overflow-scrolling: touch !important;
    width: 100% !important;
    position: relative;
    /* Add subtle shadow to indicate scrollability on mobile */
    background: 
      linear-gradient(90deg, var(--fac-bg-surface) 0%, transparent 20px) 0 0,
      linear-gradient(90deg, transparent calc(100% - 20px), var(--fac-bg-surface) 100%) 100% 0,
      linear-gradient(90deg, rgba(0,0,0,0.1) 0%, transparent 10px) 0 0,
      linear-gradient(-90deg, rgba(0,0,0,0.1) 0%, transparent 10px) 100% 0;
    background-repeat: no-repeat;
    background-size: 20px 100%, 20px 100%, 10px 100%, 10px 100%;
    background-attachment: local, local, scroll, scroll;
  }
  
  /* Prevent horizontal overflow on mobile */
  .fac-widget,
  .fac-widget .fac-row,
  .fac-widget .fac-bubble,
  .fac-widget .fac-chat,
  .fac-widget .fac-grid,
  .fac-widget .fac-form,
  .fac-widget fieldset {
    overflow-x: hidden !important;
    max-width: 100% !important;
  }
  
  /* Reduce bubble max-width on mobile for better readability */
  .fac-widget .fac-bubble { max-width: 95% !important; font-size: 14px; padding: 8px 12px; }
  
  /* Make form fields stack vertically on mobile */
  .fac-widget .fac-form fieldset { grid-template-columns: 1fr; }
  
  /* Reduce padding on mobile */
  .fac-widget .fac-form { padding: 12px; }
  .fac-widget .fac-form fieldset { padding: 10px; }
  
  /* Make chatbar full width on mobile */
  .fac-widget .fac-chatbar { width: calc(100vw - 16px); }
  
  /* Table styling on mobile - keep readable with scroll */
  .fac-widget .fac-bubble table.fac-table { font-size: 14px; }
  .fac-widget .fac-bubble table.fac-table th,
  .fac-widget .fac-bubble table.fac-table td { 
    padding: 8px 12px;
    min-width: 100px; /* Minimum width to keep readable */
  }
  
  /* Visual hint for scrollable tables on mobile */
  .fac-widget .fac-bubble .fac-table-wrapper::after {
    content: '◀ ▶';
    position: absolute;
    bottom: 4px;
    right: 4px;
    background: rgba(16, 185, 129, 0.85);
    color: white;
    padding: 2px 6px;
    border-radius: 4px;
    font-size: 10px;
    opacity: 0.7;
    pointer-events: none;
    letter-spacing: 2px;
  }
  .fac-widget[dir='rtl'] .fac-bubble .fac-table-wrapper::after {
    right: auto;
    left: 4px;
  }
  
  /* Smaller headings on mobile */
  .fac-widget .fac-bubble .fac-md-h1 { font-size: 1.15em; }
  .fac-widget .fac-bubble .fac-md-h2 { font-size: 1.1em; }
  
  /* Better math block scrolling indicator */
  .fac-widget .fac-bubble .fac-math-block {
    background: linear-gradient(to right, rgba(148,163,184,0.08), rgba(148,163,184,0.12));
  }
  
  /* Area input row should wrap on very small screens */
  .fac-widget .fac-form .fac-area-row { flex-wrap: wrap; }
  .fac-widget .fac-form .fac-area-row input { min-width: 100px; flex: 1 1 calc(50% - 4px); }
}

/* Extra small mobile devices */
@media screen and (max-width: 480px) {
  .fac-widget .fac-bubble { max-width: 98% !important; font-size: 13px; }
  
  /* Smaller buttons on very small screens */
  .fac-widget .fac-reset,
  .fac-widget .fac-submit,
  .fac-widget .fac-chatbar button { padding: 10px 20px; font-size: 14px; }
  
  /* Stack EC row items */
  .fac-widget .fac-form .fac-ec-row { flex-direction: column; align-items: stretch; }
  .fac-widget .fac-form .fac-ec-row input,
  .fac-widget .fac-form .fac-ec-row select { width: 100%; }
  
  /* Stack dual-unit rows */
  .fac-widget .fac-form .fac-du-row { flex-direction: column; align-items: stretch; }
  .fac-widget .fac-form .fac-du-row input,
  .fac-widget .fac-form .fac-du-row select { width: 100%; max-width: 100%; }
  
  /* Full width tooltips on small screens */
  .fac-widget .fac-tip-content { max-width: calc(100vw - 32px); right: auto; left: 50%; transform: translateX(-50%) translateY(8px); }
  .fac-widget .fac-tip:hover .fac-tip-content,
  .fac-widget .fac-tip:focus-within .fac-tip-content { transform: translateX(-50%) translateY(0); }
}

