/* WiseCashAI v2 — chat stylesheet
   Plain CSS, no framework. Responsive (phone / tablet / desktop),
   light/dark, safe-area aware, 44px min tap targets, reduced-motion. */

/* ---------- Tokens ---------- */
:root {
  --primary: #4e73df;
  --primary-contrast: #fff;
  --secondary: #1cc88a;
  --danger: #e74a3b;
  --warning: #f6c23e;

  --bg: #f8f9fc;
  --bg-elevated: #fff;
  --bg-sunken: #eef1f8;
  --text: #1f2430;
  --text-muted: #5a5c69;
  --border: #e3e6ef;
  --ring: rgba(78, 115, 223, .35);

  --bubble-user-bg: #4e73df;
  --bubble-user-text: #fff;
  --bubble-assistant-bg: #fff;
  --bubble-assistant-text: #1f2430;
  --bubble-assistant-border: #e3e6ef;

  --card-bg: #fff;
  --card-border: #e3e6ef;
  --chip-bg: #edf1fb;
  --chip-text: #2c3659;
  --chip-border: #d9e0f2;

  --shadow-sm: 0 1px 2px rgba(16, 24, 40, .04);
  --shadow-md: 0 4px 16px rgba(16, 24, 40, .06);
  --shadow-lg: 0 10px 28px rgba(16, 24, 40, .12);

  --radius-sm: 0;
  --radius: 0;
  --radius-lg: 0;

  --font-ui: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
  --font-mono: ui-monospace, SFMono-Regular, 'SF Mono', Menlo, Consolas, monospace;

  --tap-min: 44px;
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-top: env(safe-area-inset-top, 0px);
}

[data-theme="dark"] {
  --bg: #141826;
  --bg-elevated: #1e2130;
  --bg-sunken: #0f1320;
  --text: #e9ecef;
  --text-muted: #a0a4b5;
  --border: #2a2e3f;
  --ring: rgba(120, 150, 240, .45);

  --bubble-user-bg: #4e73df;
  --bubble-user-text: #fff;
  --bubble-assistant-bg: #242839;
  --bubble-assistant-text: #e9ecef;
  --bubble-assistant-border: #2a2e3f;

  --card-bg: #242839;
  --card-border: #2a2e3f;
  --chip-bg: #2a2e3f;
  --chip-text: #d2d6e8;
  --chip-border: #394057;
}

@media (prefers-color-scheme: dark) {
  html:not([data-theme]) body { color-scheme: dark; }
}

/* ---------- Reset-ish ---------- */
*, *::before, *::after { box-sizing: border-box; border-radius: 0 !important; }
html, body { margin: 0; padding: 0; height: 100%; }
body {
  font-family: var(--font-ui);
  font-size: 16px;
  line-height: 1.5;
  color: var(--text);
  background: var(--bg);
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
  overflow: hidden; /* chat scroll is inner */
}
button, input, textarea, select {
  font: inherit;
  color: inherit;
}
button { cursor: pointer; background: none; border: 0; }
a { color: var(--primary); text-decoration: none; }
a:hover, a:focus-visible { text-decoration: underline; }

.visually-hidden {
  position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px;
  overflow: hidden; clip: rect(0,0,0,0); white-space: nowrap; border: 0;
}

:focus-visible {
  outline: 2px solid var(--ring);
  outline-offset: 2px;
  border-radius: 4px;
}

/* ---------- App bar ---------- */
.app-bar {
  display: flex; align-items: center; justify-content: space-between;
  padding: calc(10px + var(--safe-top)) 14px 10px;
  background: var(--bg-elevated);
  border-bottom: 1px solid var(--border);
  position: sticky; top: 0; z-index: 10;
}
.brand {
  display: inline-flex; align-items: center; gap: 10px;
  color: var(--text); font-weight: 700; font-size: 16px;
}
.brand em { font-style: normal; color: var(--primary); }
.brand img { border-radius: 6px; }
.badge-beta {
  display: inline-block; margin-left: 4px;
  background: var(--chip-bg); color: var(--chip-text);
  font-size: 11px; font-weight: 700; letter-spacing: .04em; text-transform: uppercase;
  padding: 2px 6px; border-radius: 4px; border: 1px solid var(--chip-border);
}
.app-bar-actions { display: inline-flex; gap: 6px; }

.icon-btn {
  min-width: var(--tap-min); min-height: var(--tap-min);
  display: inline-flex; align-items: center; justify-content: center;
  color: var(--text-muted);
  border-radius: 10px;
  transition: background-color .15s ease, color .15s ease;
}
.icon-btn:hover, .icon-btn:focus-visible { color: var(--text); background: var(--bg-sunken); }
.icon-btn:active { background: var(--border); }

/* ---------- Layout ---------- */
.layout {
  display: grid;
  grid-template-columns: 1fr;
  height: calc(100dvh - 57px - var(--safe-top));
  min-height: 0;
}

.chat {
  display: grid;
  grid-template-rows: auto 1fr auto auto auto auto;
  min-height: 0;
  max-width: 880px;
  width: 100%;
  margin: 0 auto;
  padding: 0 14px;
}

.ai-status-bar {
  background: #fff7e6; color: #7a5200;
  border: 1px solid #f1d69c;
  border-radius: var(--radius);
  padding: 8px 12px;
  margin: 10px 0 0;
  font-size: 13px;
}
[data-theme="dark"] .ai-status-bar { background: #3a2d0f; color: #f1d69c; border-color: #5c4516; }

.message-stream {
  overflow-y: auto;
  overscroll-behavior: contain;
  padding: 14px 2px 6px;
  display: flex; flex-direction: column; gap: 14px;
  scroll-behavior: smooth;
}
.message-stream::-webkit-scrollbar { width: 8px; }
.message-stream::-webkit-scrollbar-thumb { background: var(--border); border-radius: 4px; }

/* ---------- Bubbles ---------- */
.bubble {
  max-width: 86%;
  padding: 10px 14px;
  border-radius: var(--radius-lg);
  line-height: 1.55;
  word-wrap: break-word;
  position: relative;
}
.bubble.user {
  background: var(--bubble-user-bg); color: var(--bubble-user-text);
  align-self: flex-end;
  border-bottom-right-radius: 6px;
}
.bubble.assistant {
  background: var(--bubble-assistant-bg); color: var(--bubble-assistant-text);
  border: 1px solid var(--bubble-assistant-border);
  align-self: flex-start;
  border-bottom-left-radius: 6px;
  box-shadow: var(--shadow-sm);
}
.bubble .msg-actions {
  margin-top: 6px; display: flex; gap: 4px;
  font-size: 12px; color: var(--text-muted);
  opacity: 0; transition: opacity .15s ease;
}
.bubble.assistant:hover .msg-actions,
.bubble.assistant:focus-within .msg-actions { opacity: 1; }
.msg-actions button {
  padding: 4px 8px; border-radius: 6px; color: var(--text-muted);
}
.msg-actions button:hover { background: var(--bg-sunken); color: var(--text); }

.bubble .number-highlight {
  font-family: var(--font-mono); font-weight: 600;
  padding: 0 2px; border-radius: 3px;
  background: rgba(78,115,223,.08);
}
.bubble .grounded-warning {
  display: inline-block; margin-left: 4px;
  font-size: 11px; padding: 1px 6px; border-radius: 10px;
  background: var(--warning); color: #5a4500;
}

.typing-dots {
  display: inline-flex; gap: 4px; padding: 6px 4px;
}
.typing-dots span {
  width: 6px; height: 6px; border-radius: 50%;
  background: var(--text-muted);
  animation: bounce 1.2s infinite;
}
.typing-dots span:nth-child(2) { animation-delay: .2s; }
.typing-dots span:nth-child(3) { animation-delay: .4s; }
@keyframes bounce { 0%,80%,100% { transform: translateY(0); opacity: .4; } 40% { transform: translateY(-4px); opacity: 1; } }

/* ---------- Cards (inline in chat) ---------- */
.card {
  margin-top: 10px;
  background: var(--card-bg);
  border: 1px solid var(--card-border);
  border-radius: var(--radius);
  padding: 12px 14px;
  box-shadow: var(--shadow-sm);
}
.card + .card { margin-top: 8px; }
.card-title { font-weight: 600; margin-bottom: 6px; display: flex; justify-content: space-between; align-items: center; gap: 8px; }
.card-title .muted { font-weight: 400; color: var(--text-muted); font-size: 13px; }
.card-row {
  display: grid; grid-template-columns: 1fr auto; gap: 4px 12px;
  padding: 6px 0; border-top: 1px solid var(--border); font-size: 14px;
}
.card-row:first-of-type { border-top: 0; }
.card-actions { margin-top: 10px; display: flex; flex-wrap: wrap; gap: 6px; }

.progress {
  width: 100%; height: 8px; background: var(--bg-sunken); border-radius: 999px; overflow: hidden; margin: 6px 0;
}
.progress > span { display: block; height: 100%; background: var(--primary); border-radius: 999px; }
.progress.over > span { background: var(--danger); }

.chart-wrap { width: 100%; height: 220px; margin-top: 6px; }

/* ---------- Suggestion chips ---------- */
.suggestion-chips {
  display: flex; gap: 8px; overflow-x: auto; padding: 6px 2px;
  scrollbar-width: none;
}
.suggestion-chips::-webkit-scrollbar { display: none; }
.chip {
  flex: 0 0 auto;
  min-height: 36px;
  padding: 8px 14px;
  border-radius: 999px;
  background: var(--chip-bg); color: var(--chip-text);
  border: 1px solid var(--chip-border);
  font-size: 13px;
  white-space: nowrap;
}
.chip:hover { background: var(--bg-sunken); }

/* ---------- Composer ---------- */
.composer {
  display: grid;
  grid-template-columns: auto auto 1fr auto;
  gap: 4px; align-items: end;
  padding: 8px; border-top: 1px solid var(--border);
  background: var(--bg-elevated);
  border-radius: var(--radius) var(--radius) 0 0;
  margin-top: 4px;
}
.composer-input {
  border: 0; outline: none; background: transparent;
  resize: none; max-height: 200px; min-height: 40px;
  padding: 10px 6px;
  color: var(--text);
}
.composer-input::placeholder { color: var(--text-muted); }
.composer-btn { color: var(--text-muted); }
.composer-send { color: var(--primary); }
.composer-send[aria-disabled="true"] { color: var(--text-muted); pointer-events: none; }

/* ---------- Buttons ---------- */
.btn {
  min-height: var(--tap-min);
  padding: 10px 14px;
  border-radius: 10px;
  background: var(--bg-sunken);
  color: var(--text);
  border: 1px solid var(--border);
  font-weight: 500;
}
.btn:hover { background: var(--border); }
.btn-primary { background: var(--primary); color: var(--primary-contrast); border-color: var(--primary); }
.btn-primary:hover { filter: brightness(1.05); }
.btn-secondary { background: var(--secondary); color: #0e3b29; border-color: var(--secondary); }
.btn-danger { background: var(--danger); color: #fff; border-color: var(--danger); }
.btn-sm { min-height: 32px; padding: 6px 10px; font-size: 13px; border-radius: 8px; }

/* ---------- Ad slot ---------- */
.ad-slot {
  margin: 6px auto 0;
  max-width: 728px;
  min-height: 60px;
  text-align: center;
  padding-bottom: var(--safe-bottom);
}

/* ---------- Footer ---------- */
.chat-footer {
  display: flex; gap: 8px; align-items: center; flex-wrap: wrap;
  padding: 6px 4px 10px;
  font-size: 12px; color: var(--text-muted);
  padding-bottom: calc(10px + var(--safe-bottom));
}
.link-btn { color: var(--text-muted); padding: 4px 6px; border-radius: 6px; }
.link-btn:hover { color: var(--text); background: var(--bg-sunken); text-decoration: none; }

/* ---------- Drawer ---------- */
.drawer {
  position: fixed;
  background: var(--bg-elevated);
  border-left: 1px solid var(--border);
  z-index: 30;
  box-shadow: var(--shadow-lg);
  display: flex; flex-direction: column;
  min-height: 0;
}
.drawer[hidden] { display: none; }   /* explicit: hidden attribute beats .drawer's display:flex */

/* Mobile: bottom sheet. Hidden attribute truly hides (display:none) so a11y tools
   see it as absent. Slide-in animates when opened. */
.drawer {
  right: 0; left: 0; bottom: 0; top: auto;
  max-height: 85dvh;
  border-radius: var(--radius-lg) var(--radius-lg) 0 0;
  border-left: 0; border-top: 1px solid var(--border);
  padding-bottom: var(--safe-bottom);
}
.drawer[data-open="true"] { animation: wc-slide-up .22s ease both; }
.drawer-backdrop {
  position: fixed; inset: 0; background: rgba(0,0,0,.35);
  z-index: 20;
}
.drawer-backdrop[data-open="true"] { animation: wc-fade-in .22s ease both; }
@keyframes wc-slide-up { from { transform: translateY(100%); } to { transform: translateY(0); } }
@keyframes wc-fade-in  { from { opacity: 0; } to { opacity: 1; } }

.drawer-head {
  display: flex; align-items: center; justify-content: space-between;
  padding: 14px 14px 8px;
  border-bottom: 1px solid var(--border);
}
.drawer-head h2 { margin: 0; font-size: 16px; }
.drawer-body { overflow-y: auto; padding: 10px 14px 18px; }
.drawer-section { padding: 10px 0; border-bottom: 1px solid var(--border); }
.drawer-section:last-child { border-bottom: 0; }
.drawer-meta { color: var(--text-muted); font-size: 13px; }

.field-label { display: block; font-size: 13px; color: var(--text-muted); margin-bottom: 6px; }
.field-input {
  width: 100%;
  min-height: var(--tap-min);
  padding: 10px 12px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text);
}
.field-input:focus { border-color: var(--primary); outline: 2px solid var(--ring); outline-offset: -1px; }
.field-row { display: flex; gap: 8px; align-items: center; }
.field-row .field-input { flex: 1; }
.field-hint { color: var(--text-muted); font-size: 12px; margin: 6px 0 0; }

/* ---------- Breakpoints ---------- */
/* Small tablet / landscape phone */
@media (min-width: 601px) {
  .chat { padding: 0 18px; }
  .bubble { max-width: 80%; }
}

/* Tablet */
@media (min-width: 901px) {
  .chat { padding: 0 24px; }
  .drawer {
    right: 0; left: auto; top: calc(57px + var(--safe-top)); bottom: 0;
    width: 360px; max-height: none;
    border-radius: 0;
    border-left: 1px solid var(--border); border-top: 0;
    padding-bottom: var(--safe-bottom);
  }
  .drawer[data-open="true"] { animation: wc-slide-in .22s ease both; }
  @keyframes wc-slide-in { from { transform: translateX(100%); } to { transform: translateX(0); } }
  .drawer-backdrop { display: none; }
}

/* Desktop — persistent drawer, ignore hidden attribute */
@media (min-width: 1201px) {
  .layout { grid-template-columns: 1fr 360px; }
  .drawer[hidden] { display: flex !important; }
  .drawer { position: static; animation: none !important; box-shadow: none; border-radius: 0; }
  #toggleDrawerBtn, #closeDrawerBtn { display: none; }
}

/* ---------- Motion ---------- */
@media (prefers-reduced-motion: reduce) {
  .drawer, .drawer-backdrop, .typing-dots span { transition: none !important; animation: none !important; }
}

/* ---------- Markdown content inside bubbles ---------- */
.bubble p { margin: 0 0 8px; }
.bubble p:last-child { margin-bottom: 0; }
.bubble ul, .bubble ol { padding-left: 18px; margin: 4px 0 8px; }
.bubble code {
  font-family: var(--font-mono); font-size: 13px;
  background: var(--bg-sunken); padding: 1px 5px; border-radius: 4px;
}
.bubble pre {
  background: var(--bg-sunken); padding: 10px; border-radius: 8px;
  overflow-x: auto; font-size: 13px;
}
.bubble table { width: 100%; border-collapse: collapse; margin: 6px 0; font-size: 14px; }
.bubble th, .bubble td { border: 1px solid var(--border); padding: 4px 8px; text-align: left; }
