/* themes.css
 * Extracted from the original inline <style> block.
 * Load order matters: see the <link> order in index.html.
 */
    /* ================= CHAT THEME ENGINE (CSS VARIABLES) ================= */
    :root {
      --chat-bg: rgba(255, 255, 255, 0.045);
      --chat-border: rgba(255, 255, 255, 0.10);
      --msg-user-bg: rgba(99, 102, 241, 0.22);
      --msg-user-border: rgba(129, 140, 248, 0.35);
      --msg-char-bg: rgba(255, 255, 255, 0.055);
      --msg-char-border: rgba(255, 255, 255, 0.10);
      --text-color: #e5e7eb;
      --chat-label: #94a3b8;
      --chat-input-bg: rgba(10, 12, 20, 0.55);
      --chat-input-color: #e5e7eb;
      --chat-accent: #818cf8;
    }
    body[data-theme="amoled"] {
      --chat-bg: #000000; --chat-border: #1a1a1a;
      --msg-user-bg: #0f1014; --msg-user-border: #262a35;
      --msg-char-bg: #040404; --msg-char-border: #141414;
      --text-color: #f5f5f5; --chat-label: #8b8b8b;
      --chat-input-bg: #070707; --chat-input-color: #f5f5f5; --chat-accent: #a3e635;
    }
    body[data-theme="light"] {
      --chat-bg: #ffffff; --chat-border: #e5e7eb;
      --msg-user-bg: #e0e7ff; --msg-user-border: #c7d2fe;
      --msg-char-bg: #f3f4f6; --msg-char-border: #e5e7eb;
      --text-color: #111827; --chat-label: #6b7280;
      --chat-input-bg: #ffffff; --chat-input-color: #111827; --chat-accent: #4f46e5;
    }
    body[data-theme="vn"] {
      --chat-bg: rgba(18, 16, 32, 0.55); --chat-border: rgba(244, 114, 182, 0.28);
      --msg-user-bg: rgba(236, 72, 153, 0.18); --msg-user-border: rgba(244, 114, 182, 0.45);
      --msg-char-bg: rgba(56, 189, 248, 0.14); --msg-char-border: rgba(56, 189, 248, 0.38);
      --text-color: #f8fafc; --chat-label: #cbd5e1;
      --chat-input-bg: rgba(15, 12, 28, 0.7); --chat-input-color: #f8fafc; --chat-accent: #f472b6;
    }
    body[data-theme="vn"] #chatPanel {
      backdrop-filter: blur(26px) saturate(160%);
      -webkit-backdrop-filter: blur(26px) saturate(160%);
    }

    /* ---------- Chat surface theming ---------- */
    #chatPanel { background: var(--chat-bg) !important; border-color: var(--chat-border) !important; }
    #chatName { color: var(--text-color); }
    #chatTitle, .chat-label { color: var(--chat-label); }
    #chatMessages { background: transparent; }
    #chatInput { background: var(--chat-input-bg); color: var(--chat-input-color); border-color: var(--chat-border); }
    .msg-bubble { color: var(--text-color); border: 1px solid var(--chat-border); }
    .msg-user { background: var(--msg-user-bg); border-color: var(--msg-user-border); }
    .msg-char { background: var(--msg-char-bg); border-color: var(--msg-char-border); }
    .chat-row { animation: fadeIn 0.25s ease both; }
    /* Circular avatar frame: no outer box, no square backdrop. */
    .chat-avatar {
      width: 40px; height: 40px;
      min-width: 40px; min-height: 40px;
      flex: 0 0 40px;
      border-radius: 9999px;
      overflow: hidden;
      object-fit: cover;
      border: 1px solid var(--chat-border);
      background: transparent;
      display: block;
      box-sizing: border-box;
    }
    body[data-theme="light"] .chat-avatar { background: transparent; }
    @media (max-width: 768px) {
      .chat-avatar { width: 32px; height: 32px; }
      #chatMessages { padding-left: 0.6rem; padding-right: 0.6rem; }
      #chatName { font-size: 0.8rem; }
    }

    /* ================= UNIFIED SINGLE-SIDE CHAT STREAM ================= */
    /* Every message (user and character) is laid out identically: avatar first,
       then a column of [name label] + [message] + [action buttons]. The document
       direction decides the side, so LTR reads left and RTL reads right with no
       per-message mirroring. */
    .chat-row {
      display: flex;
      flex-direction: row;
      align-items: flex-start;
      gap: 0.75rem;
      width: 100%;
      min-width: 0;
    }
    .chat-column {
      display: flex;
      flex-direction: column;
      flex: 1 1 auto;
      min-width: 0;
      text-align: start;
      align-items: flex-start;
    }
    .chat-label { text-align: start; }
    /* The bubble row is a full-width block; the bubble inside it shrink-wraps. */
    .bubble-row {
      display: flex;
      flex-direction: column;
      align-items: flex-start;
      width: 100%;
      max-width: 100%;
      min-width: 0;
    }
    .msg-bubble { text-align: start; max-width: 100%; }
    /* Action buttons sit under the message, aligned to the reading edge. */
    .rp-controls { align-items: center; justify-content: flex-start; }
    /* RTL: avatar sits immediately to the RIGHT of the text block.
       `row` (not `row-reverse`) is already correct because the document
       direction is RTL, so the first child (avatar) lands on the right. */
    [dir="rtl"] .chat-row { flex-direction: row; }
    [dir="rtl"] .chat-column { align-items: flex-start; text-align: right; }
    [dir="rtl"] .chat-label { text-align: right; }
    [dir="rtl"] .bubble-row { align-items: flex-start; }
    [dir="rtl"] .msg-bubble { text-align: right; }
    [dir="rtl"] .rp-controls { justify-content: flex-start; }
    /* Reveal action buttons for the row under the pointer or keyboard focus. */
    .chat-row:hover .rp-controls,
    .chat-row:focus-within .rp-controls { opacity: 1; }
    @media (hover: none) { .rp-controls { opacity: 0.85; } }

    /* ================= FLAT / NOVEL CHAT LAYOUT (SillyTavern Document Mode — DEFAULT) ================= */
    /* Novel mode: single-column, no boxes, no bubbles, continuous story reading */
    body[data-chat-layout="novel"] #chatMessages {
      padding: 24px 32px;
      max-width: 860px;
      margin: 0 auto;
      width: 100%;
    }
    @media (max-width: 768px) {
      body[data-chat-layout="novel"] #chatMessages {
        padding: 16px 18px;
      }
    }
    body[data-chat-layout="novel"] .chat-row {
      display: flex !important;
      /* Both roles share ONE direction. RTL mirroring is applied below via
         [dir="rtl"] — never per-role, so avatars stay attached to messages. */
      flex-direction: row !important;
      align-items: flex-start !important;
      gap: 16px !important;
      width: 100% !important;
      padding: 18px 0 !important;
      border-bottom: 1px solid rgba(255, 255, 255, 0.06) !important;
      background: transparent !important;
      box-shadow: none !important;
    }
    body[data-chat-layout="novel"] .chat-row { flex-direction: row !important; }
    body[data-chat-layout="novel"] .chat-row:last-child {
      border-bottom: none !important;
    }
    body[data-chat-layout="novel"] .chat-avatar {
      width: 44px !important;
      height: 44px !important;
      min-width: 44px !important;
      min-height: 44px !important;
      flex: 0 0 44px !important;
      border-radius: 9999px !important;
      overflow: hidden !important;
      display: block !important;
      object-fit: cover !important;
      border: 1px solid rgba(255, 255, 255, 0.14) !important;
      background: transparent !important;
      box-sizing: border-box !important;
    }
    @media (max-width: 768px) {
      body[data-chat-layout="novel"] .chat-avatar {
        width: 36px !important;
        height: 36px !important;
        min-width: 36px !important;
        min-height: 36px !important;
        flex: 0 0 36px !important;
      }
    }
    body[data-chat-layout="novel"] .chat-column {
      flex: 1 !important;
      min-width: 0 !important;
      display: flex;
      flex-direction: column;
      align-items: flex-start !important;
      gap: 0;
      text-align: start !important;
    }
    body[data-chat-layout="novel"] .chat-label {
      margin: 0 !important;
      margin-bottom: 6px !important;
      font-size: 0.75rem !important;
      font-weight: 700 !important;
      text-transform: none !important;
      letter-spacing: normal !important;
      opacity: 1 !important;
      color: var(--chat-label, #cbd5e1) !important;
      padding: 0 !important;
      background: transparent !important;
      border: none !important;
      display: block !important;
      text-align: start !important;
    }
    [dir="rtl"] body[data-chat-layout="novel"] .chat-label { text-align: right !important; }
    body[data-chat-layout="novel"] .bubble-row {
      display: flex !important;
      flex-direction: column !important;
      align-items: flex-start !important;
      gap: 0 !important;
      width: 100% !important;
      max-width: 100% !important;
      padding: 0 !important;
    }
    body[data-chat-layout="novel"] .msg-bubble,
    body[data-chat-layout="novel"] .msg-user,
    body[data-chat-layout="novel"] .msg-char {
      background: transparent !important;
      border: none !important;
      box-shadow: none !important;
      border-radius: 0 !important;
      padding: 0 !important;
      margin: 0 !important;
      max-width: none !important;
      width: 100% !important;
      line-height: 1.75 !important;
      font-size: 15.5px !important;
      color: var(--text-color, #e5e7eb) !important;
      text-align: start !important;
    }
    [dir="rtl"] body[data-chat-layout="novel"] .msg-bubble { text-align: right !important; }
    body[data-chat-layout="novel"] .rp-controls {
      display: flex;
      align-items: center;
      justify-content: flex-start;
      gap: 8px;
      margin-top: 6px;
      opacity: 0;
      transition: opacity 0.15s ease;
      flex-wrap: wrap;
    }
    body[data-chat-layout="novel"] .chat-row:hover .rp-controls,
    body[data-chat-layout="novel"] .chat-row:focus-within .rp-controls {
      opacity: 1;
    }
    @media (hover: none) {
      body[data-chat-layout="novel"] .rp-controls { opacity: 0.85; }
    }
    body[data-chat-layout="novel"] .rp-controls button {
      opacity: 0.8;
      font-size: 0.7rem;
      padding: 4px 8px;
    }

    /* Syntax highlighting classes for roleplay parser */
    .rp-action { font-style: italic; color: var(--rp-action-color, #e2b97f); }
    .rp-dialogue { color: #ffffff !important; font-weight: 500; }
    .rp-emphasis { font-weight: 700; color: var(--rp-emphasis-color, var(--chat-accent, #f472b6)); }
    .rp-whisper { font-style: italic; color: #7dd3fc !important; opacity: 0.85; }
    .rp-ooc {
      display: inline-block;
      font-size: 0.75em;
      padding: 0.1em 0.5em;
      border-radius: 0.35rem;
      background: var(--rp-ooc-bg, rgba(148, 163, 184, 0.15));
      border: 1px solid var(--rp-ooc-border, rgba(148, 163, 184, 0.3));
      color: var(--rp-ooc-color, #94a3b8);
      font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
    }
    /* Inline emphasis chips must follow the message's reading direction. */
    [dir="rtl"] .msg-bubble .rp-ooc,
    [dir="rtl"] .msg-bubble .rp-whisper { unicode-bidi: isolate; }
    /* Theme-specific syntax colors */
    body[data-theme="light"] {
      --rp-action-color: #6b5599;
      --rp-dialogue-color: #166534;
      --rp-emphasis-color: #be185d;
      --rp-whisper-color: #0d9488;
      --rp-ooc-bg: rgba(100, 116, 139, 0.1);
      --rp-ooc-border: rgba(100, 116, 139, 0.2);
      --rp-ooc-color: #475569;
    }
    body[data-theme="amoled"] {
      --rp-action-color: #cbb6a0;
      --rp-dialogue-color: #fcd34d;
      --rp-emphasis-color: #f472b6;
      --rp-whisper-color: #7dd3fc;
      --rp-ooc-bg: rgba(148, 163, 184, 0.1);
      --rp-ooc-border: rgba(148, 163, 184, 0.2);
      --rp-ooc-color: #94a3b8;
    }
    body[data-theme="vn"] {
      --rp-action-color: #cba6b4;
      --rp-dialogue-color: #fde68a;
      --rp-emphasis-color: #f9a8d4;
      --rp-whisper-color: #7dd3fc;
      --rp-ooc-bg: rgba(148, 163, 184, 0.12);
      --rp-ooc-border: rgba(148, 163, 184, 0.25);
      --rp-ooc-color: #94a3b8;
    }
