/* ===== Savings Tracker Chat Widget =====
 * Strictly scoped under .chat-widget / .cw-* to avoid colliding with styles.css.
 * Reuses the app's CSS custom properties (--bg, --accent, etc.) when they are
 * defined on :root by styles.css, with safe fallback values for standalone use.
 */

.chat-widget {
  --cw-bg:        var(--bg-card,    #1A1A1A);
  --cw-bg-input:  var(--bg-input,   #262626);
  --cw-text:      var(--text,       #FFFFFF);
  --cw-muted:     var(--text-muted, #A0A0A0);
  --cw-accent:    var(--accent,     #7C3AED);
  --cw-accent-hv: var(--accent-hover,#6D28D9);
  --cw-danger:    var(--danger,     #EF4444);
  --cw-border:    var(--border,     #333333);
  --cw-radius:    var(--radius,     12px);
  --cw-radius-sm: var(--radius-sm,  8px);

  position: fixed;
  right: 1rem;
  bottom: 1rem;
  z-index: 1000;
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif;
  color: var(--cw-text);
  line-height: 1.4;
}

/* ===== Floating toggle button ===== */
.chat-widget .cw-toggle {
  width: 52px;
  height: 52px;
  border-radius: 50%;
  border: none;
  background: var(--cw-accent);
  color: #fff;
  font-size: 1.5rem;
  cursor: pointer;
  box-shadow: 0 4px 14px rgba(0, 0, 0, 0.35);
  transition: background 0.2s, transform 0.15s;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0;
}
.chat-widget .cw-toggle:hover { background: var(--cw-accent-hv); }
.chat-widget .cw-toggle:focus-visible {
  outline: 2px solid var(--cw-accent);
  outline-offset: 3px;
}
.chat-widget .cw-toggle:active { transform: scale(0.96); }

/* Hide the toggle while the panel is open so it does not overlap. */
.chat-widget:not(.cw-collapsed) .cw-toggle { display: none; }

/* ===== Panel ===== */
.chat-widget .cw-panel {
  position: absolute;
  right: 0;
  bottom: 0;
  width: 340px;
  max-width: calc(100vw - 2rem);
  height: 480px;
  max-height: calc(100vh - 2rem);
  background: var(--cw-bg);
  border: 1px solid var(--cw-border);
  border-radius: var(--cw-radius);
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.5);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}
.chat-widget .cw-panel.hidden { display: none !important; }

/* ===== Header ===== */
.chat-widget .cw-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0.75rem 1rem;
  background: var(--cw-bg-input);
  border-bottom: 1px solid var(--cw-border);
}
.chat-widget .cw-header h3 {
  margin: 0;
  font-size: 1rem;
  font-weight: 600;
}
.chat-widget .cw-close {
  background: transparent;
  border: none;
  color: var(--cw-muted);
  font-size: 1.4rem;
  line-height: 1;
  cursor: pointer;
  padding: 0 0.35rem;
  border-radius: var(--cw-radius-sm);
}
.chat-widget .cw-close:hover { color: var(--cw-text); }
.chat-widget .cw-close:focus-visible {
  outline: 2px solid var(--cw-accent);
  outline-offset: 1px;
}

/* ===== Message list ===== */
.chat-widget .cw-messages {
  flex: 1 1 auto;
  overflow-y: auto;
  padding: 0.75rem;
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
  font-size: 0.9rem;
  background: var(--cw-bg);
}

.chat-widget .cw-msg {
  padding: 0.55rem 0.75rem;
  border-radius: var(--cw-radius-sm);
  max-width: 85%;
  word-wrap: break-word;
  overflow-wrap: anywhere;
  white-space: pre-wrap;
}
.chat-widget .cw-msg-user {
  background: var(--cw-accent);
  color: #fff;
  align-self: flex-end;
  border-bottom-right-radius: 2px;
}
.chat-widget .cw-msg-assistant {
  background: var(--cw-bg-input);
  color: var(--cw-text);
  align-self: flex-start;
  border-bottom-left-radius: 2px;
}
.chat-widget .cw-msg-error {
  background: rgba(239, 68, 68, 0.12);
  color: var(--cw-danger);
  align-self: flex-start;
  border: 1px solid rgba(239, 68, 68, 0.4);
}
.chat-widget .cw-msg-pending {
  background: var(--cw-bg-input);
  color: var(--cw-muted);
  align-self: flex-start;
  font-style: italic;
}

/* ===== Form ===== */
.chat-widget .cw-form {
  display: flex;
  gap: 0.4rem;
  padding: 0.6rem 0.75rem;
  border-top: 1px solid var(--cw-border);
  background: var(--cw-bg);
}
.chat-widget .cw-input {
  flex: 1 1 auto;
  min-width: 0;
  background: var(--cw-bg-input);
  border: 1px solid var(--cw-border);
  border-radius: var(--cw-radius-sm);
  color: var(--cw-text);
  padding: 0.5rem 0.7rem;
  font-size: 0.9rem;
  font-family: inherit;
}
.chat-widget .cw-input:focus {
  outline: none;
  border-color: var(--cw-accent);
}
.chat-widget .cw-input:disabled {
  opacity: 0.6;
  cursor: not-allowed;
}
.chat-widget .cw-send {
  background: var(--cw-accent);
  color: #fff;
  border: none;
  border-radius: var(--cw-radius-sm);
  padding: 0.5rem 0.9rem;
  font-size: 0.9rem;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.2s;
}
.chat-widget .cw-send:hover:not(:disabled) { background: var(--cw-accent-hv); }
.chat-widget .cw-send:focus-visible {
  outline: 2px solid var(--cw-accent);
  outline-offset: 2px;
}
.chat-widget .cw-send:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

/* ===== Empty-state hint ===== */
.chat-widget .cw-empty {
  color: var(--cw-muted);
  font-size: 0.85rem;
  text-align: center;
  padding: 1rem 0.5rem;
}

/* ===== Responsive ===== */
@media (max-width: 480px) {
  .chat-widget {
    right: 0.75rem;
    bottom: 0.75rem;
  }
  .chat-widget .cw-panel {
    width: calc(100vw - 1.5rem);
    height: 70vh;
  }
}
