#wptc-chat-widget {
  position: fixed;
  bottom: 90px;
  right: 20px;
  width: 520px;
  max-width: calc(100vw - 40px);
  max-height: 620px;
  background: #ffffff;
  border: 1px solid #e5e7eb;
  border-radius: 14px;
  display: none;
  flex-direction: column;
  z-index: 10000;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Arial, sans-serif;
  font-size: 14px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.16);
  overflow: hidden;
  transition: transform 0.15s ease-out;
}

/* Lifted above the device's own on-screen keyboard while typing a plain
   (non-math) message on a phone/tablet — see chat.js. */
#wptc-chat-widget.wptc-kb-lifted {
  transition: transform 0.15s ease-out;
}

#wptc-chat-widget.wptc-open {
  display: flex;
}

#wptc-chat-widget.minimized {
  height: 40px;
  overflow: hidden;
}

#wptc-chat-widget header {
  background: #0073aa;
  color: white;
  font-weight: bold;
  text-align: left;
  padding: 10px 12px;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

#wptc-chat-widget header .controls {
  display: flex;
  gap: 6px;
}

#wptc-chat-widget header button {
  background: transparent;
  border: none;
  color: white;
  font-size: 16px;
  cursor: pointer;
  padding: 0 4px;
}

#wptc-chat-log {
  flex: 1;
  padding: 12px;
  overflow-x: hidden;
  overflow-y: auto;
  max-height: 520px;
  line-height: 1.35;
  display: flex;
  flex-direction: column;
  gap: 10px;
  scroll-behavior: smooth;
  -webkit-overflow-scrolling: touch;
  background: #ffffff;
}

#wptc-chat-log .user,
#wptc-chat-log .bot {
  display: block;
  box-sizing: border-box;
  max-width: min(92%, 360px);
  width: fit-content;
  min-width: 72px;
  min-height: 40px;
  padding: 10px 12px;
  border-radius: 16px;
  color: #111827;
  white-space: normal;
  overflow-wrap: break-word;
  word-break: normal;
  overflow-x: auto;
  overflow-y: visible;
  height: auto;
  flex-shrink: 0;
  line-height: 1.3;
}

#wptc-chat-log .user {
  align-self: flex-end;
  background: #0073aa;
  color: #ffffff;
  border-bottom-right-radius: 5px;
}

#wptc-chat-log .bot {
  align-self: flex-start;
  background: #f3f4f6;
  color: #111827;
  border-bottom-left-radius: 5px;
}

#wptc-chat-log .bot.loading {
  font-style: italic;
  color: #6b7280;
}

#wptc-chat-widget footer {
  padding: 10px;
  border-top: 1px solid #e5e7eb;
  display: flex;
  gap: 8px;
  background: #ffffff;
}

#wptc-chat-input {
  flex: 1;
  min-width: 0;
  padding: 8px 12px;
  font-size: 14px;
  border: 1px solid #d1d5db;
  border-radius: 999px;
  outline: none;
}

#wptc-chat-input:focus {
  border-color: #0073aa;
}

#wptc-chat-send {
  background: #0073aa;
  color: white;
  border: none;
  border-radius: 50%;
  width: 40px;
  height: 40px;
  cursor: pointer;
  font-size: 18px;
  line-height: 40px;
  flex-shrink: 0;
}

#wptc-reopen-button {
  position: fixed;
  bottom: 20px;
  right: 20px;
  background: #0073aa;
  color: white;
  border-radius: 50%;
  width: 60px;
  height: 60px;
  text-align: center;
  line-height: 60px;
  font-size: 26px;
  cursor: pointer;
  z-index: 9999;
  box-shadow: 0 4px 10px rgba(0, 0, 0, 0.25);
  display: block;
}

#wptc-reopen-button.wptc-hidden {
  display: none;
}

#wptc-chat-log .bot p,
#wptc-chat-log .user p {
  margin: 0 !important;
  line-height: 1.25 !important;
}

#wptc-chat-log .bot p:last-child,
#wptc-chat-log .user p:last-child {
  margin-bottom: 0 !important;
}

#wptc-chat-log .bot pre,
#wptc-chat-log .user pre {
  margin: 0.15em 0;
  padding: 6px;
  border-radius: 8px;
  background: rgba(0, 0, 0, 0.05);
  overflow-x: auto;
  max-width: 100%;
  white-space: pre-wrap;
  word-break: break-word;
}

#wptc-chat-log .user pre {
  background: rgba(255, 255, 255, 0.15);
}

@media (max-width: 480px) {
  #wptc-chat-widget {
    width: min(82vw, 360px);
    right: 12px;
    bottom: 10px;
    max-height: 70vh;
  }

  #wptc-chat-log {
    max-height: none;
  }

  #wptc-chat-log .user,
  #wptc-chat-log .bot {
    max-width: 92%;
  }

  #wptc-reopen-button {
    width: 50px;
    height: 50px;
    font-size: 22px;
    line-height: 50px;
  }
}

/* Empty state, math fallback, tools, sources, toolbar */
.wptc-empty-state {
  display: flex;
  flex-direction: column;
  gap: 4px;
  padding: 12px 4px 4px;
  color: #6b7280;
  text-align: center;
}
.wptc-empty-state strong {
  color: #111827;
  font-size: 15px;
}
.wptc-empty-state span {
  font-size: 13px;
}
.wptc-math-fallback {
  font-family: 'Cambria Math', Georgia, 'Times New Roman', serif;
}
.wptc-math-fallback-display {
  display: block;
  text-align: left;
  margin: 0.25em 0 !important;
  line-height: normal !important;
}
.wptc-toolbar {
  display: flex;
  gap: 6px;
  padding: 6px 10px 0;
  background: #fff;
  flex-wrap: wrap;
}
.wptc-mode-btn {
  border: 1px solid #e5e7eb;
  background: #f9fafb;
  color: #374151;
  border-radius: 999px;
  padding: 4px 10px;
  font-size: 12px;
  cursor: pointer;
}
.wptc-mode-btn:hover,
.wptc-mode-btn[aria-pressed="true"] {
  border-color: #0073aa;
  color: #0073aa;
}
.wptc-stop-btn {
  border-color: #fecaca;
  color: #b91c1c;
  background: #fef2f2;
}
.wptc-bot-tools {
  display: flex;
  gap: 6px;
  margin-top: 10px;
  flex-wrap: wrap;
}
.wptc-tool-btn {
  border: 1px solid #e5e7eb;
  background: #fff;
  color: #374151;
  border-radius: 8px;
  padding: 4px 8px;
  font-size: 12px;
  cursor: pointer;
}
.wptc-tool-btn:hover,
.wptc-tool-btn.active {
  border-color: #0073aa;
  color: #0073aa;
}
.wptc-sources {
  margin-top: 8px;
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  align-items: center;
}
.wptc-sources-label {
  font-size: 11px;
  color: #6b7280;
}
.wptc-source-chip {
  display: inline-block;
  font-size: 11px;
  padding: 3px 8px;
  border-radius: 999px;
  background: #e5e7eb;
  color: #1f2937;
  text-decoration: none;
  max-width: 100%;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
a.wptc-source-chip:hover {
  background: #dbeafe;
  color: #1d4ed8;
}
#wptc-chat-send:disabled {
  opacity: 0.6;
  cursor: not-allowed;
}

#wptc-chat-log .bot div,
#wtb-shortcode-log .bot div {
  max-width: 100%;
  overflow-x: auto;
  overflow-y: visible;
}

#wptc-chat-log .bot img,
#wptc-chat-log .user img,
#wtb-shortcode-log .bot img,
#wtb-shortcode-log .user img {
  max-width: 100%;
  height: auto;
}

#wptc-chat-log .wptc-bot-tools,
#wptc-chat-log .wptc-sources,
#wtb-shortcode-log .wptc-bot-tools,
#wtb-shortcode-log .wptc-sources {
  max-width: 100%;
}

/* Keep the widget from spanning the full screen on phones - a
   narrower card anchored to the right, like it's sitting on top of
   the page rather than replacing it, instead of stretching
   edge-to-edge. Message-content spacing (the worked-solution line
   height/margins further below) is untouched by this. */
@media (max-width: 768px) {
  #wptc-chat-widget {
    left: auto;
    right: 12px;
    width: min(82vw, 360px);
    max-width: none;
    bottom: 10px;
    border-radius: 14px;
  }
  #wptc-chat-widget footer {
    padding: 10px 12px 14px;
  }
  #wptc-chat-input {
    min-height: 48px;
    font-size: 16px;
    border-radius: 12px;
    padding: 12px 14px;
  }
  #wptc-chat-send {
    width: 48px;
    height: 48px;
    line-height: 48px;
  }
}

/* v1.6.47: wider normal message field without changing the MathLive panel */
#wptc-chat-widget .wptc-input-row {
  width: 100%;
}
#wptc-chat-widget #wptc-chat-input {
  flex: 1 1 auto;
}
@media (max-width: 768px) {
  #wptc-chat-widget {
    width: min(82vw, 360px);
  }
}

/* Tight row height for solution workings */
#wptc-chat-log .bot,
#wptc-chat-log .bot p {
  line-height: 1.4;
}
#wptc-chat-log .bot li {
  line-height: 1.45;
  margin: 0.15em 0;
}
#wptc-chat-log .bot p + p {
  margin-top: 0 !important;
}
#wptc-chat-log .bot li + li {
  margin-top: 0.2em !important;
}
#wptc-chat-log .bot ul,
#wptc-chat-log .bot ol {
  margin: 0 !important;
  padding-top: 0 !important;
  padding-bottom: 0 !important;
  padding-left: 1.2em;
}
#wptc-chat-log .bot br {
  display: block;
  content: "";
  margin: 0;
  line-height: 0.9 !important;
  height: 0.55em;
}

/* Bubble: grow with content; horizontal scroll if a line is wider than
   the bubble, never a vertical scrollbar inside it (same overflow
   quirk as above). */
#wptc-chat-log .bot,
#wptc-chat-log .user,
#wtb-shortcode-log .bot,
#wtb-shortcode-log .user {
  height: auto !important;
  max-height: none !important;
  overflow-x: auto !important;
  overflow-y: hidden !important;
}
/* Fallback when :has() is unavailable — slightly roomier list lines overall */
@supports not (selector(:has(*))) {
  #wptc-chat-log .bot li,
  #wtb-shortcode-log .bot li {
    line-height: 1.7 !important;
    margin-top: 0.25em !important;
    margin-bottom: 0.25em !important;
  }
}

/* ========== KaTeX (v1.6.2 proven style — do not tighten further) ========== */
#wptc-chat-log .katex,
#wtb-shortcode-log .katex {
  font-size: 1.1em;
}
#wptc-chat-log .katex-display,
#wtb-shortcode-log .katex-display {
  display: block;
  text-align: left;
  margin: 12px 0;
  padding: 6px 0;
  background: transparent;
  border-radius: 6px;
  border: none;
  /* horizontal scroll only when formula is wider than bubble;
     height:auto so tall math is fully visible (no vertical scrollbar) */
  overflow-x: auto;
  overflow-y: hidden;
  height: auto;
  max-height: none;
  max-width: 100%;
}
#wptc-chat-log .katex-inline,
#wtb-shortcode-log .katex-inline {
  padding: 0 2px;
  background: transparent;
  border-radius: 3px;
}
/* Room for tall inline glyphs (∫, limits) inside list steps */
#wptc-chat-log .bot li:has(.katex),
#wtb-shortcode-log .bot li:has(.katex),
#wptc-chat-log .bot p:has(.katex),
#wtb-shortcode-log .bot p:has(.katex) {
  line-height: 1.85;
  margin-top: 0.35em;
  margin-bottom: 0.35em;
  padding-top: 0.1em;
  padding-bottom: 0.1em;
}
#wptc-chat-log .bot li .katex,
#wtb-shortcode-log .bot li .katex,
#wptc-chat-log .bot p .katex,
#wtb-shortcode-log .bot p .katex {
  vertical-align: middle;
}
/* Tables */
#wptc-chat-log .wptc-table-wrap,
#wtb-shortcode-log .wptc-table-wrap {
  display: block;
  width: 100%;
  max-width: 100%;
  overflow-x: auto;
  margin: 0.35em 0;
}
#wptc-chat-log .wptc-md-table,
#wtb-shortcode-log .wptc-md-table {
  border-collapse: collapse;
  font-size: 0.92em;
  line-height: 1.4;
}
#wptc-chat-log .wptc-md-table th,
#wptc-chat-log .wptc-md-table td,
#wtb-shortcode-log .wptc-md-table th,
#wtb-shortcode-log .wptc-md-table td {
  border: 1px solid #d1d5db;
  padding: 4px 8px;
  text-align: left;
  vertical-align: top;
}
#wptc-chat-log .wptc-md-table th,
#wtb-shortcode-log .wptc-md-table th {
  background: #e5e7eb;
  font-weight: 600;
}
#wptc-chat-log .bot .wptc-md-h,
#wtb-shortcode-log .bot .wptc-md-h {
  margin: 0.4em 0 0.2em;
  font-weight: 700;
  line-height: 1.3;
}

/* In-page confirm / notice dialog (replaces window.confirm / window.alert, which app WebViews block). */
.wtb-dialog-overlay{position:fixed;top:0;right:0;bottom:0;left:0;z-index:2147483647;background:rgba(15,23,42,.45);display:flex;align-items:center;justify-content:center;padding:16px;box-sizing:border-box}
.wtb-dialog{background:#fff;color:#111827;border-radius:14px;max-width:360px;width:100%;padding:18px;box-shadow:0 12px 40px rgba(0,0,0,.3);font-family:-apple-system,BlinkMacSystemFont,"Segoe UI",Arial,sans-serif;font-size:14px;line-height:1.45;box-sizing:border-box}
.wtb-dialog p{margin:0 0 14px;white-space:pre-line}
.wtb-dialog-actions{display:flex;justify-content:flex-end;gap:8px}
.wtb-dialog-actions button{appearance:none;border:1px solid #d1d5db;background:#f3f4f6;color:#111827;border-radius:8px;padding:9px 16px;font-size:14px;font-weight:600;cursor:pointer}
.wtb-dialog-actions .wtb-dialog-ok{background:#0073aa;border-color:#0073aa;color:#fff}

/* When the WaokMath app's address bar is showing, keep the chat window (and its Clear buttons)
   below it instead of letting the bar cover the header. */
html.waokmath-pwa-has-addressbar:not(.waokmath-pwa-bar-hidden) #wptc-chat-widget{
  max-height:calc(100vh - 90px - 64px);
  max-height:calc(100dvh - 90px - 64px);
}
html.waokmath-pwa-has-addressbar:not(.waokmath-pwa-bar-hidden) #wptc-chat-widget #wptc-chat-log{min-height:0}
