/*
 * Caden — RevenueCat Documentation Chatbot Styles
 *
 * Brand colors from revenuecat.com:
 *   Primary: #F4511E (coral/orange-red)
 *   Dark:    #1A1A2E
 *   Light bg:#F8F9FA
 */

/* ===== CSS Custom Properties ===== */
:root {
  --color-primary: #F4511E;
  --color-primary-dark: #D63E0D;
  --color-primary-light: #FF7043;
  --color-bg: #F8F9FA;
  --color-surface: #FFFFFF;
  --color-surface-alt: #F3F4F6;
  --color-border: #E5E7EB;
  --color-text: #111827;
  --color-text-secondary: #6B7280;
  --color-text-inverse: #FFFFFF;
  --color-error: #DC2626;
  --color-success: #16A34A;
  --color-caden-bubble-bg: #F3F4F6;
  --color-user-bubble-bg: #F4511E;
  --color-user-bubble-text: #FFFFFF;

  --border-radius-sm: 6px;
  --border-radius-md: 12px;
  --border-radius-lg: 18px;
  --border-radius-avatar: 50%;

  --shadow-sm: 0 1px 3px rgba(0, 0, 0, 0.08);
  --shadow-md: 0 4px 12px rgba(0, 0, 0, 0.1);

  --font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --font-size-base: 15px;
  --font-size-sm: 13px;
  --font-size-lg: 17px;

  --max-width: 768px;
  --header-height: 64px;
  --input-bar-height: 72px;
}

/* ===== Reset & Base ===== */
*, *::before, *::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

html, body {
  height: 100%;
}

body {
  font-family: var(--font-family);
  font-size: var(--font-size-base);
  color: var(--color-text);
  background-color: var(--color-bg);
  line-height: 1.6;
  -webkit-font-smoothing: antialiased;
}

/* Screen-reader-only utility */
.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

/* ===== Header ===== */
.app-header {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  z-index: 100;
  background-color: var(--color-primary);
  height: var(--header-height);
  box-shadow: var(--shadow-md);
}

.header-content {
  max-width: var(--max-width);
  margin: 0 auto;
  padding: 0 16px;
  height: 100%;
  display: flex;
  align-items: center;
  gap: 12px;
}

.caden-avatar {
  width: 40px;
  height: 40px;
  border-radius: var(--border-radius-avatar);
  background-color: rgba(255, 255, 255, 0.25);
  color: var(--color-text-inverse);
  font-size: 18px;
  font-weight: 700;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  border: 2px solid rgba(255, 255, 255, 0.4);
}

.header-title {
  color: var(--color-text-inverse);
  font-size: var(--font-size-lg);
  font-weight: 700;
  line-height: 1.2;
}

.header-subtitle {
  color: rgba(255, 255, 255, 0.85);
  font-size: var(--font-size-sm);
  line-height: 1.2;
}

/* ===== Chat Container ===== */
.chat-container {
  max-width: var(--max-width);
  margin: 0 auto;
  padding-top: var(--header-height);
  padding-bottom: 0;
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

/* ===== Chat Messages ===== */
.chat-messages {
  flex: 1;
  overflow-y: auto;
  padding: 20px 16px;
  padding-bottom: calc(var(--input-bar-height) + 100px);
  display: flex;
  flex-direction: column;
  gap: 16px;
}

/* ===== Message Bubbles ===== */
.message-row {
  display: flex;
  align-items: flex-start;
  gap: 10px;
}

.message-row.user {
  flex-direction: row-reverse;
}

.message-avatar {
  width: 32px;
  height: 32px;
  border-radius: var(--border-radius-avatar);
  background-color: var(--color-primary);
  color: var(--color-text-inverse);
  font-size: 13px;
  font-weight: 700;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}

.message-row.user .message-avatar {
  background-color: var(--color-text-secondary);
}

.message-bubble {
  max-width: 78%;
  padding: 12px 16px;
  border-radius: var(--border-radius-lg);
  line-height: 1.6;
  word-break: break-word;
}

.message-bubble.caden {
  background-color: var(--color-caden-bubble-bg);
  border-top-left-radius: var(--border-radius-sm);
}

.message-bubble.user {
  background-color: var(--color-user-bubble-bg);
  color: var(--color-user-bubble-text);
  border-top-right-radius: var(--border-radius-sm);
}

/* ===== Opening Message ===== */
.opening-message .message-bubble {
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.opening-text {
  font-size: var(--font-size-base);
}

/* ===== Example Chips ===== */
.example-chips {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.chip {
  display: inline-block;
  padding: 6px 12px;
  background-color: var(--color-surface);
  border: 1.5px solid var(--color-primary);
  color: var(--color-primary);
  border-radius: 20px;
  font-size: var(--font-size-sm);
  font-family: var(--font-family);
  font-weight: 500;
  cursor: pointer;
  transition: background-color 0.15s, color 0.15s;
  text-align: left;
}

.chip:hover,
.chip:focus {
  background-color: var(--color-primary);
  color: var(--color-text-inverse);
  outline: 2px solid var(--color-primary-dark);
  outline-offset: 2px;
}

/* ===== Typing Indicator ===== */
.typing-indicator {
  display: flex;
  align-items: center;
  gap: 5px;
  padding: 4px 0;
}

.typing-dot {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background-color: var(--color-text-secondary);
  animation: typing-bounce 1.2s infinite;
}

.typing-dot:nth-child(2) { animation-delay: 0.2s; }
.typing-dot:nth-child(3) { animation-delay: 0.4s; }

@keyframes typing-bounce {
  0%, 60%, 100% { transform: translateY(0); opacity: 0.4; }
  30% { transform: translateY(-5px); opacity: 1; }
}

/* ===== Citations / Sources ===== */
.sources-block {
  margin-top: 10px;
  padding-top: 10px;
  border-top: 1px solid var(--color-border);
  font-size: var(--font-size-sm);
}

.sources-label {
  font-weight: 600;
  color: var(--color-text-secondary);
  margin-bottom: 4px;
}

.sources-list {
  list-style: none;
  display: flex;
  flex-direction: column;
  gap: 3px;
}

.sources-list li a {
  color: var(--color-primary);
  text-decoration: none;
  word-break: break-all;
}

.sources-list li a:hover {
  text-decoration: underline;
}

/* ===== Error in bubble ===== */
.bubble-error {
  color: var(--color-error);
  font-size: var(--font-size-sm);
  font-style: italic;
}

/* ===== Input Bar ===== */
.input-bar {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  background-color: var(--color-surface);
  border-top: 1px solid var(--color-border);
  padding: 12px 16px;
  display: flex;
  align-items: flex-end;
  gap: 10px;
  box-shadow: 0 -2px 8px rgba(0, 0, 0, 0.06);
  z-index: 50;
}

/* Center within max-width */
.input-bar::before {
  content: '';
  position: absolute;
  inset: 0;
  pointer-events: none;
}

.chat-input {
  flex: 1;
  min-height: 44px;
  max-height: 140px;
  padding: 10px 14px;
  border: 1.5px solid var(--color-border);
  border-radius: var(--border-radius-md);
  font-family: var(--font-family);
  font-size: var(--font-size-base);
  color: var(--color-text);
  background-color: var(--color-surface);
  resize: none;
  line-height: 1.5;
  transition: border-color 0.15s;
  overflow-y: auto;
}

.chat-input:focus {
  outline: none;
  border-color: var(--color-primary);
  box-shadow: 0 0 0 3px rgba(244, 81, 30, 0.15);
}

.chat-input:disabled {
  background-color: var(--color-surface-alt);
  color: var(--color-text-secondary);
  cursor: not-allowed;
}

.send-btn {
  padding: 10px 20px;
  background-color: var(--color-primary);
  color: var(--color-text-inverse);
  border: none;
  border-radius: var(--border-radius-md);
  font-family: var(--font-family);
  font-size: var(--font-size-base);
  font-weight: 600;
  cursor: pointer;
  white-space: nowrap;
  transition: background-color 0.15s;
  min-height: 44px;
}

.send-btn:hover:not(:disabled) {
  background-color: var(--color-primary-dark);
}

.send-btn:focus {
  outline: 2px solid var(--color-primary-dark);
  outline-offset: 2px;
}

.send-btn:disabled {
  background-color: var(--color-text-secondary);
  cursor: not-allowed;
}

/* ===== Ticket Section ===== */
/*
 * C1 fix: The submit button sits inside .chat-container (flex column) below
 * .chat-messages. Since .input-bar is position:fixed, the ticket-section is
 * not obscured by the bar itself — but it was scrolled off-screen because
 * .chat-messages consumed all remaining flex space. We remove the large
 * bottom padding from ticket-section and instead rely on the padding-bottom
 * already applied to .chat-messages to keep content clear of the fixed bar.
 * The button now appears in the natural scroll flow, visible just above the
 * chat input when the user scrolls to the bottom.
 */
/*
 * The [hidden] attribute sets display:none in the UA stylesheet, but author CSS
 * with display:flex overrides it. Add explicit [hidden] guard to ensure the
 * hidden attribute works correctly when ticket section is not yet revealed.
 */
.ticket-section[hidden] {
  display: none;
}

.ticket-section {
  max-width: var(--max-width);
  margin: 0 auto;
  padding: 8px 16px 120px;
  display: flex;
  justify-content: center;
  width: 100%;
}

/* Small secondary button — triggers chat flow instead of showing a form */
.submit-chat-btn {
  padding: 8px 18px;
  background-color: transparent;
  border: 1.5px solid var(--color-border);
  color: var(--color-text-secondary);
  border-radius: var(--border-radius-md);
  font-family: var(--font-family);
  font-size: var(--font-size-sm);
  font-weight: 500;
  cursor: pointer;
  text-align: center;
  transition: border-color 0.15s, color 0.15s;
}

.submit-chat-btn:hover {
  border-color: var(--color-primary);
  color: var(--color-primary);
}

.submit-chat-btn:focus {
  outline: 2px solid var(--color-primary-dark);
  outline-offset: 2px;
}

/* ===== Rendered Markdown inside Caden bubbles ===== */

/* Paragraphs — top margin only after the first to avoid leading gap */
.message-bubble.caden .bubble-text p {
  margin-top: 0.5em;
  margin-bottom: 0;
}

.message-bubble.caden .bubble-text p:first-child {
  margin-top: 0;
}

/* Unordered and ordered lists */
.message-bubble.caden .bubble-text ul,
.message-bubble.caden .bubble-text ol {
  padding-left: 1.4em;
  margin-top: 0.4em;
  margin-bottom: 0.4em;
}

.message-bubble.caden .bubble-text ul {
  list-style-type: disc;
}

.message-bubble.caden .bubble-text ol {
  list-style-type: decimal;
}

.message-bubble.caden .bubble-text li {
  margin-bottom: 0.2em;
  line-height: 1.6;
}

/* Inline code */
.message-bubble.caden .bubble-text code {
  background-color: var(--color-surface-alt);
  color: var(--color-text);
  font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, "Courier New", monospace;
  font-size: var(--font-size-sm);
  padding: 0.15em 0.35em;
  border-radius: 4px;
  border: 1px solid var(--color-border);
  word-break: break-word;
}

/* Fenced code blocks */
.message-bubble.caden .bubble-text pre {
  background-color: var(--color-surface-alt);
  border: 1px solid var(--color-border);
  border-radius: var(--border-radius-sm);
  padding: 10px 14px;
  margin-top: 0.5em;
  margin-bottom: 0.5em;
  overflow-x: auto;
}

.message-bubble.caden .bubble-text pre code {
  background-color: transparent;
  border: none;
  padding: 0;
  font-size: var(--font-size-sm);
  color: var(--color-text);
  word-break: normal;
  white-space: pre;
}

/* Headings — smaller than page headers, appropriate for chat context */
.message-bubble.caden .bubble-text h2 {
  font-size: 1em;
  font-weight: 700;
  margin-top: 0.75em;
  margin-bottom: 0.25em;
  color: var(--color-text);
  line-height: 1.3;
}

.message-bubble.caden .bubble-text h2:first-child,
.message-bubble.caden .bubble-text h3:first-child {
  margin-top: 0;
}

.message-bubble.caden .bubble-text h3 {
  font-size: 0.95em;
  font-weight: 700;
  margin-top: 0.6em;
  margin-bottom: 0.2em;
  color: var(--color-text);
  line-height: 1.3;
}

/* Strong and emphasis */
.message-bubble.caden .bubble-text strong {
  font-weight: 700;
}

.message-bubble.caden .bubble-text em {
  font-style: italic;
}

/* ===== Responsive ===== */
@media (max-width: 600px) {
  .message-bubble {
    max-width: 90%;
  }

  .chip {
    font-size: 12px;
    padding: 5px 10px;
  }

  .ticket-form-wrapper {
    padding: 16px;
  }
}

/* ===== Header Buttons (About, Tickets) ===== */
.header-btns {
  margin-left: auto;
  flex-shrink: 0;
  display: flex;
  align-items: center;
  gap: 8px;
}

.header-btn {
  padding: 6px 14px;
  /* C3: WCAG 2.5.5 minimum touch target 44x44px */
  min-height: 44px;
  background: transparent;
  border: 1.5px solid rgba(255, 255, 255, 0.6);
  border-radius: var(--border-radius-sm);
  color: var(--color-text-inverse);
  font-family: var(--font-family);
  font-size: var(--font-size-sm);
  font-weight: 500;
  cursor: pointer;
  transition: background-color 0.15s, border-color 0.15s;
  white-space: nowrap;
}

.header-btn:hover {
  background-color: rgba(255, 255, 255, 0.15);
  border-color: rgba(255, 255, 255, 0.9);
}

.header-btn:focus {
  outline: 2px solid rgba(255, 255, 255, 0.9);
  outline-offset: 2px;
}

/* ===== Modal Backdrop ===== */
.modal-backdrop {
  position: fixed;
  inset: 0;
  z-index: 200;
  background-color: rgba(0, 0, 0, 0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
  /* Smooth fade-in handled by toggling hidden attribute */
}

.modal-backdrop[hidden] {
  display: none;
}

/* ===== Modal Panel ===== */
.modal-panel {
  background-color: var(--color-surface);
  border-radius: var(--border-radius-md);
  box-shadow: var(--shadow-md);
  width: 100%;
  max-width: 560px;
  max-height: calc(100vh - 32px);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 20px 24px 16px;
  border-bottom: 1px solid var(--color-border);
  flex-shrink: 0;
}

.modal-title {
  font-size: var(--font-size-lg);
  font-weight: 700;
  color: var(--color-text);
}

.modal-close-btn {
  width: 32px;
  height: 32px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: transparent;
  border: none;
  border-radius: var(--border-radius-sm);
  color: var(--color-text-secondary);
  font-size: 16px;
  cursor: pointer;
  transition: background-color 0.15s, color 0.15s;
  flex-shrink: 0;
  line-height: 1;
}

.modal-close-btn:hover {
  background-color: var(--color-surface-alt);
  color: var(--color-text);
}

.modal-close-btn:focus {
  outline: 2px solid var(--color-primary);
  outline-offset: 2px;
}

/* ===== Modal Body ===== */
.modal-body {
  overflow-y: auto;
  padding: 20px 24px 24px;
  display: flex;
  flex-direction: column;
  gap: 20px;
}

.modal-section {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.modal-section-title {
  font-size: var(--font-size-base);
  font-weight: 700;
  color: var(--color-primary);
}

.modal-section p {
  color: var(--color-text);
  font-size: var(--font-size-base);
  line-height: 1.6;
}

.modal-list {
  list-style: none;
  display: flex;
  flex-direction: column;
  gap: 8px;
  padding: 0;
}

.modal-list li {
  padding-left: 16px;
  position: relative;
  color: var(--color-text);
  font-size: var(--font-size-base);
  line-height: 1.6;
}

.modal-list li::before {
  content: '\2022';
  position: absolute;
  left: 0;
  color: var(--color-primary);
  font-weight: 700;
}

/* ===== Modal Responsive ===== */
@media (max-width: 600px) {
  .modal-panel {
    max-height: calc(100vh - 24px);
  }

  .modal-header {
    padding: 16px 16px 12px;
  }

  .modal-body {
    padding: 16px 16px 20px;
  }
}

/* ===== Ticket List (inside tickets modal) ===== */
.ticket-list-container {
  display: flex;
  flex-direction: column;
  gap: 0;
}

.ticket-list {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.ticket-card {
  background-color: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--border-radius-md);
  padding: 14px 16px;
  box-shadow: var(--shadow-sm);
}

.ticket-card-title {
  font-size: var(--font-size-base);
  font-weight: 600;
  color: var(--color-text);
  margin-bottom: 6px;
  line-height: 1.4;
}

.ticket-card-meta {
  display: flex;
  align-items: center;
  gap: 10px;
  font-size: var(--font-size-sm);
  color: var(--color-text-secondary);
}

.ticket-status-badge {
  display: inline-block;
  padding: 2px 8px;
  border-radius: 12px;
  background-color: var(--color-surface-alt);
  border: 1px solid var(--color-border);
  font-size: 12px;
  font-weight: 500;
  color: var(--color-text-secondary);
  text-transform: capitalize;
}

.ticket-pagination {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 16px;
  margin-top: 16px;
}

.ticket-pagination button {
  padding: 8px 18px;
  background-color: var(--color-surface);
  border: 1.5px solid var(--color-primary);
  color: var(--color-primary);
  border-radius: var(--border-radius-sm);
  font-family: var(--font-family);
  font-size: var(--font-size-sm);
  font-weight: 600;
  cursor: pointer;
  transition: background-color 0.15s, color 0.15s;
}

.ticket-pagination button:hover:not(:disabled) {
  background-color: var(--color-primary);
  color: var(--color-text-inverse);
}

.ticket-pagination button:focus {
  outline: 2px solid var(--color-primary-dark);
  outline-offset: 2px;
}

.ticket-pagination button:disabled {
  border-color: var(--color-border);
  color: var(--color-text-secondary);
  cursor: not-allowed;
}

.ticket-pagination-info {
  font-size: var(--font-size-sm);
  color: var(--color-text-secondary);
}

.ticket-list-loading,
.ticket-list-empty,
.ticket-list-error {
  font-size: var(--font-size-sm);
  padding: 16px 0;
  text-align: center;
}

.ticket-list-loading,
.ticket-list-empty {
  color: var(--color-text-secondary);
}

.ticket-list-error {
  color: var(--color-error);
}

/* ===== Modal Letter Link ===== */
.modal-letter-link-section {
  padding: 4px 0;
}

.modal-letter-link {
  display: inline-block;
  padding: 10px 20px;
  background-color: var(--color-primary);
  color: var(--color-text-inverse);
  border-radius: var(--border-radius-md);
  font-size: var(--font-size-base);
  font-weight: 600;
  text-decoration: none;
  transition: background-color 0.15s;
  width: 100%;
  text-align: center;
  box-sizing: border-box;
}

.modal-letter-link:hover {
  background-color: var(--color-primary-dark);
}

.modal-letter-link:focus {
  outline: 2px solid var(--color-primary-dark);
  outline-offset: 2px;
}

/* ===== Ticket submit status (inline confirmation/error after form submission) ===== */
.ticket-submit-success {
  margin-top: 10px;
  padding: 8px 12px;
  background-color: #ECFDF5;
  border: 1px solid #6EE7B7;
  border-radius: var(--border-radius-sm);
  color: var(--color-success);
  font-size: var(--font-size-sm);
  font-weight: 500;
}

.ticket-submit-error {
  margin-top: 10px;
  padding: 8px 12px;
  background-color: #FEF2F2;
  border: 1px solid #FECACA;
  border-radius: var(--border-radius-sm);
  color: var(--color-error);
  font-size: var(--font-size-sm);
  font-weight: 500;
}

/* ===== Inline ticket form (rendered inside a Caden bubble) ===== */
.ticket-form-intro {
  margin-bottom: 12px;
  line-height: 1.5;
}

.ticket-inline-form {
  display: flex;
  flex-direction: column;
  gap: 10px;
  margin-top: 4px;
}

.ticket-field-label {
  display: block;
  font-size: var(--font-size-sm);
  font-weight: 600;
  color: var(--color-text);
  margin-bottom: 3px;
}

.ticket-field-input,
.ticket-field-textarea {
  width: 100%;
  padding: 8px 10px;
  border: 1.5px solid var(--color-border);
  border-radius: var(--border-radius-sm);
  font-family: var(--font-family);
  font-size: var(--font-size-sm);
  color: var(--color-text);
  background-color: var(--color-surface);
  line-height: 1.5;
  transition: border-color 0.15s;
  box-sizing: border-box;
}

.ticket-field-input:focus,
.ticket-field-textarea:focus {
  outline: none;
  border-color: var(--color-primary);
  box-shadow: 0 0 0 2px rgba(244, 81, 30, 0.12);
}

.ticket-field-textarea {
  resize: vertical;
  min-height: 56px;
}

.ticket-field-input.ticket-field-error,
.ticket-field-textarea.ticket-field-error {
  border-color: var(--color-error);
}

.ticket-field-readonly {
  background-color: var(--color-bg-secondary, #f8f9fa);
  color: var(--color-text-secondary, #6c757d);
  cursor: default;
}

.ticket-form-btn-row {
  display: flex;
  gap: 8px;
  margin-top: 4px;
}

.ticket-form-submit-btn {
  padding: 8px 18px;
  background-color: var(--color-primary);
  color: var(--color-text-inverse);
  border: none;
  border-radius: var(--border-radius-sm);
  font-family: var(--font-family);
  font-size: var(--font-size-sm);
  font-weight: 600;
  cursor: pointer;
  transition: background-color 0.15s;
  min-height: 36px;
}

.ticket-form-submit-btn:hover:not(:disabled) {
  background-color: var(--color-primary-dark);
}

.ticket-form-submit-btn:disabled {
  background-color: var(--color-text-secondary);
  cursor: not-allowed;
}

.ticket-form-submit-btn:focus {
  outline: 2px solid var(--color-primary-dark);
  outline-offset: 2px;
}

.ticket-form-cancel-btn {
  padding: 8px 14px;
  background-color: transparent;
  border: 1.5px solid var(--color-border);
  color: var(--color-text-secondary);
  border-radius: var(--border-radius-sm);
  font-family: var(--font-family);
  font-size: var(--font-size-sm);
  font-weight: 500;
  cursor: pointer;
  transition: border-color 0.15s, color 0.15s;
  min-height: 36px;
}

.ticket-form-cancel-btn:hover {
  border-color: var(--color-text-secondary);
  color: var(--color-text);
}

.ticket-form-cancel-btn:focus {
  outline: 2px solid var(--color-primary-dark);
  outline-offset: 2px;
}

.ticket-form-cancel-msg {
  margin-top: 8px;
  color: var(--color-text-secondary);
  font-size: var(--font-size-sm);
}

/* ===== Focus visible (accessibility) ===== */
:focus-visible {
  outline: 2px solid var(--color-primary);
  outline-offset: 2px;
}
