/* Zatersio Marketing Manager — shared styles (chat, login, admin). */

/* ---------- Tokens ---------- */
:root {
  color-scheme: light dark;

  --bg: #f6f7f9;
  --surface: #ffffff;
  --surface-2: #f0f2f5;
  --surface-3: #e6e9ee;
  --border: #dde1e7;
  --border-strong: #c5ccd6;
  --text: #16191f;
  --text-muted: #5b6472;
  --text-faint: #8a93a1;

  --accent: #3b5bdb;
  --accent-hover: #3249c0;
  --accent-soft: #e8edff;
  --accent-contrast: #ffffff;

  --danger: #c92a2a;
  --danger-soft: #fff0f0;
  --danger-border: #f3c2c2;
  --warning: #9c5b00;
  --warning-soft: #fff6e5;
  --warning-border: #f1d49c;
  --success: #1f7a4d;
  --success-soft: #e7f6ee;
  --neutral-soft: #eef0f3;

  --user-bubble: #3b5bdb;
  --user-bubble-text: #ffffff;
  --code-bg: #f3f4f7;
  --backdrop: rgba(15, 18, 25, 0.45);

  --font-sans: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial,
    "Noto Sans", sans-serif, "Apple Color Emoji", "Segoe UI Emoji";
  --font-mono: ui-monospace, SFMono-Regular, "SF Mono", Menlo, Consolas, "Liberation Mono",
    monospace;

  --radius-sm: 6px;
  --radius: 10px;
  --radius-lg: 14px;
  --shadow-sm: 0 1px 2px rgba(16, 24, 40, 0.06);
  --shadow: 0 4px 16px rgba(16, 24, 40, 0.08);
  --focus-ring: 0 0 0 3px rgba(59, 91, 219, 0.35);
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1115;
    --surface: #171a20;
    --surface-2: #1e2229;
    --surface-3: #272c35;
    --border: #2a2f38;
    --border-strong: #3a414d;
    --text: #e7eaf0;
    --text-muted: #a1a9b6;
    --text-faint: #717a88;

    --accent: #7b93ff;
    --accent-hover: #93a7ff;
    --accent-soft: #1f2744;
    --accent-contrast: #0f1115;

    --danger: #ff8787;
    --danger-soft: #2c1618;
    --danger-border: #5c2a2d;
    --warning: #f5c26b;
    --warning-soft: #2b2212;
    --warning-border: #5a4520;
    --success: #69db9c;
    --success-soft: #13271d;
    --neutral-soft: #242932;

    --user-bubble: #3b5bdb;
    --user-bubble-text: #ffffff;
    --code-bg: #1b1f26;
    --backdrop: rgba(0, 0, 0, 0.6);

    --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.4);
    --shadow: 0 6px 20px rgba(0, 0, 0, 0.45);
    --focus-ring: 0 0 0 3px rgba(123, 147, 255, 0.4);
  }
}

/* ---------- Base ---------- */
*,
*::before,
*::after {
  box-sizing: border-box;
}

html,
body {
  height: 100%;
}

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font-sans);
  font-size: 15px;
  line-height: 1.55;
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
}

h1,
h2,
h3,
h4 {
  line-height: 1.25;
  margin: 0 0 0.5em;
  font-weight: 650;
}

h1 { font-size: 1.5rem; }
h2 { font-size: 1.2rem; }
h3 { font-size: 1.05rem; }

p { margin: 0 0 0.75em; }

a {
  color: var(--accent);
  text-decoration: none;
}
a:hover { text-decoration: underline; }

code,
pre,
kbd {
  font-family: var(--font-mono);
  font-size: 0.875em;
}

:focus-visible {
  outline: none;
  box-shadow: var(--focus-ring);
}

.muted { color: var(--text-muted); }
.small { font-size: 0.85rem; }

.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;
}

[hidden] { display: none !important; }

/* ---------- Buttons ---------- */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.45rem;
  min-height: 36px;
  padding: 0.45rem 0.9rem;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--surface);
  color: var(--text);
  font: inherit;
  font-size: 0.9rem;
  font-weight: 550;
  line-height: 1.2;
  cursor: pointer;
  text-decoration: none;
  white-space: nowrap;
  transition: background-color 0.12s, border-color 0.12s, color 0.12s;
}
.btn:hover { background: var(--surface-2); text-decoration: none; }
.btn:disabled,
.btn[aria-disabled="true"] { opacity: 0.55; cursor: not-allowed; }

.btn-primary {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--accent-contrast);
}
.btn-primary:hover { background: var(--accent-hover); border-color: var(--accent-hover); }

.btn-ghost {
  background: transparent;
  border-color: transparent;
  color: var(--text-muted);
}
.btn-ghost:hover { background: var(--surface-2); color: var(--text); }

.btn-danger {
  background: transparent;
  border-color: var(--danger-border);
  color: var(--danger);
}
.btn-danger:hover { background: var(--danger-soft); }

.btn-sm { min-height: 30px; padding: 0.3rem 0.65rem; font-size: 0.82rem; }
.btn-lg { min-height: 44px; padding: 0.65rem 1.2rem; font-size: 1rem; }
.btn-block { width: 100%; }

.icon-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 36px;
  height: 36px;
  padding: 0;
  border: 0;
  border-radius: var(--radius-sm);
  background: transparent;
  color: var(--text-muted);
  cursor: pointer;
}
.icon-btn:hover { background: var(--surface-2); color: var(--text); }
.icon-btn svg { width: 20px; height: 20px; }

/* ---------- Form controls ---------- */
.input,
.select,
.textarea,
input[type="text"],
input[type="email"],
input[type="search"],
select,
textarea {
  width: 100%;
  min-height: 36px;
  padding: 0.45rem 0.7rem;
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-sm);
  background: var(--surface);
  color: var(--text);
  font: inherit;
  font-size: 0.95rem;
}
select,
.select { width: auto; padding-right: 1.8rem; }
textarea,
.textarea { resize: vertical; line-height: 1.5; }
.input:focus,
.select:focus,
.textarea:focus,
input:focus,
select:focus,
textarea:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: var(--focus-ring);
}
input::placeholder,
textarea::placeholder { color: var(--text-faint); }

label,
.label { font-size: 0.85rem; font-weight: 550; color: var(--text-muted); }

.field { display: flex; flex-direction: column; gap: 0.3rem; }

/* ---------- Cards ---------- */
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-sm);
  padding: 1.25rem;
}

/* ---------- Badges ---------- */
.badge {
  display: inline-flex;
  align-items: center;
  gap: 0.3rem;
  padding: 0.12rem 0.5rem;
  border-radius: 999px;
  background: var(--neutral-soft);
  color: var(--text-muted);
  font-size: 0.75rem;
  font-weight: 600;
  line-height: 1.5;
  white-space: nowrap;
}
.badge-success { background: var(--success-soft); color: var(--success); }
.badge-warning { background: var(--warning-soft); color: var(--warning); }
.badge-danger { background: var(--danger-soft); color: var(--danger); }
.badge-accent { background: var(--accent-soft); color: var(--accent); }

/* ---------- Tables ---------- */
table,
.table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.9rem;
}
th,
td {
  padding: 0.55rem 0.75rem;
  border-bottom: 1px solid var(--border);
  text-align: left;
  vertical-align: middle;
}
th {
  color: var(--text-muted);
  font-size: 0.78rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.03em;
  background: var(--surface-2);
}
.table-wrap {
  overflow-x: auto;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
}
.table-wrap table tr:last-child td { border-bottom: 0; }

/* ---------- Banners / toasts ---------- */
.banner {
  display: flex;
  align-items: flex-start;
  gap: 0.6rem;
  padding: 0.65rem 0.85rem;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface-2);
  color: var(--text);
  font-size: 0.9rem;
}
.banner .banner-text { flex: 1; min-width: 0; }
.banner-error { background: var(--danger-soft); border-color: var(--danger-border); color: var(--danger); }
.banner-warning { background: var(--warning-soft); border-color: var(--warning-border); color: var(--warning); }
.banner-success { background: var(--success-soft); border-color: transparent; color: var(--success); }

.toast {
  position: fixed;
  left: 50%;
  bottom: 1.25rem;
  transform: translateX(-50%);
  max-width: min(520px, calc(100vw - 2rem));
  padding: 0.7rem 1rem;
  border-radius: var(--radius);
  background: var(--text);
  color: var(--bg);
  box-shadow: var(--shadow);
  font-size: 0.9rem;
  z-index: 100;
}

/* ---------- Avatar ---------- */
.avatar {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  flex: none;
  width: 32px;
  height: 32px;
  border-radius: 50%;
  background: var(--accent-soft);
  color: var(--accent);
  font-size: 0.8rem;
  font-weight: 700;
  object-fit: cover;
  overflow: hidden;
}

/* ---------- Brand ---------- */
.brand {
  display: flex;
  align-items: center;
  gap: 0.55rem;
  font-weight: 700;
  letter-spacing: -0.01em;
  color: var(--text);
}
.brand-mark {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 28px;
  height: 28px;
  border-radius: 8px;
  background: var(--accent);
  color: var(--accent-contrast);
  font-size: 0.9rem;
  font-weight: 800;
}

/* ---------- Page header (shared by chat/admin) ---------- */
.topbar {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  min-height: 56px;
  padding: 0.5rem 1rem;
  border-bottom: 1px solid var(--border);
  background: var(--surface);
}
.topbar .spacer { flex: 1; }
.user-chip {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  min-width: 0;
}
.user-chip .user-name {
  max-width: 180px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-size: 0.9rem;
  font-weight: 550;
}

/* ================================================================
   Chat page
   ================================================================ */
.chat-shell {
  display: grid;
  grid-template-columns: 272px minmax(0, 1fr);
  height: 100vh;
  height: 100dvh;
}

.sidebar {
  display: flex;
  flex-direction: column;
  min-height: 0;
  border-right: 1px solid var(--border);
  background: var(--surface);
}
.sidebar-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.5rem;
  min-height: 56px;
  padding: 0.5rem 0.75rem 0.5rem 1rem;
  border-bottom: 1px solid var(--border);
}
.sidebar-actions { padding: 0.75rem; }
.thread-list {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  margin: 0;
  padding: 0 0.5rem 0.75rem;
  list-style: none;
}
.thread-list .list-label {
  padding: 0.5rem 0.5rem 0.35rem;
  color: var(--text-faint);
  font-size: 0.72rem;
  font-weight: 650;
  text-transform: uppercase;
  letter-spacing: 0.05em;
}
.thread-item {
  display: block;
  width: 100%;
  padding: 0.5rem 0.65rem;
  border: 0;
  border-radius: var(--radius-sm);
  background: transparent;
  color: var(--text);
  font: inherit;
  font-size: 0.9rem;
  text-align: left;
  cursor: pointer;
}
.thread-item:hover { background: var(--surface-2); }
.thread-item[aria-current="true"] { background: var(--accent-soft); color: var(--accent); font-weight: 600; }
.thread-item .thread-title {
  display: block;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.thread-item .thread-time { display: block; color: var(--text-faint); font-size: 0.75rem; font-weight: 400; }
.thread-empty { padding: 0.75rem; color: var(--text-faint); font-size: 0.85rem; }

.main {
  display: flex;
  flex-direction: column;
  min-width: 0;
  min-height: 0;
}
.main .topbar .menu-btn { display: none; }
.chat-title {
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-size: 0.95rem;
  font-weight: 600;
}

.banner-slot { padding: 0.75rem 1rem 0; }
.banner-slot:empty { display: none; }

.messages {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  scroll-behavior: smooth;
}
.messages-inner {
  display: flex;
  flex-direction: column;
  gap: 1.1rem;
  max-width: 820px;
  margin: 0 auto;
  padding: 1.5rem 1rem 2rem;
}

.msg { display: flex; min-width: 0; }
.msg-user { justify-content: flex-end; }
.msg-user .bubble {
  max-width: min(80%, 620px);
  padding: 0.6rem 0.9rem;
  border-radius: var(--radius-lg) var(--radius-lg) 4px var(--radius-lg);
  background: var(--user-bubble);
  color: var(--user-bubble-text);
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}
.msg-assistant { justify-content: flex-start; gap: 0.7rem; }
.msg-assistant .assistant-avatar { margin-top: 0.1rem; width: 28px; height: 28px; font-size: 0.8rem; }
.msg-assistant .assistant-body {
  display: flex;
  flex-direction: column;
  gap: 0.6rem;
  flex: 1;
  min-width: 0;
}

/* Markdown content */
.md { overflow-wrap: anywhere; }
.md > :first-child { margin-top: 0; }
.md > :last-child { margin-bottom: 0; }
.md p,
.md ul,
.md ol,
.md pre,
.md table,
.md blockquote { margin: 0 0 0.75em; }
.md ul,
.md ol { padding-left: 1.4em; }
.md li + li { margin-top: 0.2em; }
.md h1,
.md h2,
.md h3,
.md h4 { margin: 1em 0 0.45em; }
.md h1 { font-size: 1.3rem; }
.md h2 { font-size: 1.15rem; }
.md h3,
.md h4 { font-size: 1rem; }
.md code {
  padding: 0.1em 0.35em;
  border-radius: 4px;
  background: var(--code-bg);
}
.md pre {
  padding: 0.75rem 0.9rem;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--code-bg);
  overflow-x: auto;
}
.md pre code { padding: 0; background: none; }
.md blockquote {
  padding-left: 0.85em;
  border-left: 3px solid var(--border-strong);
  color: var(--text-muted);
}
.md table {
  display: block;
  width: max-content;
  max-width: 100%;
  overflow-x: auto;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  font-size: 0.87rem;
  font-variant-numeric: tabular-nums;
}
.md th { text-transform: none; letter-spacing: 0; font-size: 0.82rem; }
.md th,
.md td { padding: 0.4rem 0.65rem; white-space: nowrap; }
.md tr:last-child td { border-bottom: 0; }
.md hr { border: 0; border-top: 1px solid var(--border); margin: 1em 0; }
.md img { max-width: 100%; }

.cursor::after {
  content: "";
  display: inline-block;
  width: 0.5em;
  height: 1em;
  margin-left: 2px;
  vertical-align: -0.12em;
  background: var(--text-muted);
  animation: blink 1s steps(2, start) infinite;
}
@keyframes blink { to { visibility: hidden; } }

.thinking {
  display: inline-flex;
  align-items: center;
  gap: 0.3rem;
  color: var(--text-faint);
  font-size: 0.85rem;
}
.thinking span {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: currentColor;
  animation: pulse 1.2s infinite ease-in-out;
}
.thinking span:nth-child(2) { animation-delay: 0.15s; }
.thinking span:nth-child(3) { animation-delay: 0.3s; }
@keyframes pulse {
  0%, 80%, 100% { opacity: 0.25; transform: scale(0.85); }
  40% { opacity: 1; transform: scale(1); }
}

/* Tool call cards */
.tool-card {
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  font-size: 0.85rem;
}
.tool-card > summary {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.45rem 0.7rem;
  cursor: pointer;
  list-style: none;
  color: var(--text-muted);
  user-select: none;
}
.tool-card > summary::-webkit-details-marker { display: none; }
.tool-card > summary::before {
  content: "";
  width: 6px;
  height: 6px;
  flex: none;
  border-right: 1.5px solid currentColor;
  border-bottom: 1.5px solid currentColor;
  transform: rotate(-45deg);
  transition: transform 0.15s;
}
.tool-card[open] > summary::before { transform: rotate(45deg); }
.tool-card .tool-name {
  font-family: var(--font-mono);
  font-size: 0.82rem;
  font-weight: 600;
  color: var(--text);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.tool-card .tool-status { margin-left: auto; }
.tool-card .tool-detail {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
  padding: 0 0.7rem 0.7rem;
  border-top: 1px solid var(--border);
}
.tool-card .tool-section-label {
  margin-top: 0.6rem;
  color: var(--text-faint);
  font-size: 0.72rem;
  font-weight: 650;
  text-transform: uppercase;
  letter-spacing: 0.05em;
}
.tool-card pre {
  margin: 0;
  padding: 0.55rem 0.7rem;
  max-height: 320px;
  overflow: auto;
  border-radius: var(--radius-sm);
  background: var(--code-bg);
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}
.tool-card .tool-output { max-height: 360px; overflow: auto; }

.spinner {
  width: 12px;
  height: 12px;
  border: 2px solid var(--border-strong);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

.status-line {
  display: flex;
  align-items: center;
  gap: 0.45rem;
  color: var(--text-faint);
  font-size: 0.8rem;
}
.status-line::before {
  content: "";
  width: 5px;
  height: 5px;
  border-radius: 50%;
  background: currentColor;
}

.inline-error {
  padding: 0.5rem 0.7rem;
  border: 1px solid var(--danger-border);
  border-radius: var(--radius-sm);
  background: var(--danger-soft);
  color: var(--danger);
  font-size: 0.87rem;
}
.inline-error.recoverable {
  border-color: var(--warning-border);
  background: var(--warning-soft);
  color: var(--warning);
}

/* Empty state */
.empty-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 0.35rem;
  min-height: 55vh;
  padding: 1rem;
  text-align: center;
}
.empty-state h1 { font-size: 1.4rem; margin-bottom: 0.15rem; }
.suggestions {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 0.6rem;
  width: 100%;
  max-width: 640px;
  margin-top: 1.25rem;
}
.suggestion {
  padding: 0.7rem 0.85rem;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--text);
  font: inherit;
  font-size: 0.88rem;
  text-align: left;
  cursor: pointer;
}
.suggestion:hover { border-color: var(--border-strong); background: var(--surface-2); }

/* Composer */
.composer {
  padding: 0.5rem 1rem calc(0.9rem + env(safe-area-inset-bottom));
  background: linear-gradient(to bottom, transparent, var(--bg) 30%);
}
.composer-box {
  display: flex;
  align-items: flex-end;
  gap: 0.5rem;
  max-width: 820px;
  margin: 0 auto;
  padding: 0.45rem 0.45rem 0.45rem 0.85rem;
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-lg);
  background: var(--surface);
  box-shadow: var(--shadow-sm);
}
.composer-box:focus-within { border-color: var(--accent); box-shadow: var(--focus-ring); }
.composer textarea {
  flex: 1;
  min-height: 24px;
  max-height: 220px;
  padding: 0.35rem 0;
  border: 0;
  background: transparent;
  resize: none;
  box-shadow: none;
  font-size: 1rem; /* >=16px avoids iOS zoom on focus */
}
.composer textarea:focus { box-shadow: none; }
.composer .send-btn { width: 38px; height: 38px; min-height: 38px; padding: 0; border-radius: var(--radius); }
.composer .send-btn svg { width: 18px; height: 18px; }
.composer-hint {
  max-width: 820px;
  margin: 0.35rem auto 0;
  color: var(--text-faint);
  font-size: 0.75rem;
  text-align: center;
}

.backdrop { display: none; }

/* ---------- Mobile ---------- */
@media (max-width: 860px) {
  .chat-shell { grid-template-columns: minmax(0, 1fr); }
  .sidebar {
    position: fixed;
    inset: 0 auto 0 0;
    z-index: 50;
    width: min(300px, 86vw);
    transform: translateX(-100%);
    transition: transform 0.2s ease;
    box-shadow: var(--shadow);
  }
  .chat-shell.nav-open .sidebar { transform: none; }
  .chat-shell.nav-open .backdrop {
    display: block;
    position: fixed;
    inset: 0;
    z-index: 40;
    background: var(--backdrop);
  }
  .main .topbar .menu-btn { display: inline-flex; }
}

@media (max-width: 560px) {
  body { font-size: 15px; }
  .topbar { padding: 0.5rem 0.6rem; gap: 0.4rem; }
  .user-chip .user-name { display: none; }
  .messages-inner { padding: 1rem 0.75rem 1.5rem; }
  .msg-user .bubble { max-width: 88%; }
  .msg-assistant .assistant-avatar { display: none; }
  .suggestions { grid-template-columns: minmax(0, 1fr); }
  .composer { padding: 0.4rem 0.6rem calc(0.6rem + env(safe-area-inset-bottom)); }
  .composer-hint { display: none; }
}

@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after { animation: none !important; transition: none !important; scroll-behavior: auto !important; }
}
