:root {
  --color-bg: #f4f5f7;
  --color-surface: #ffffff;
  --color-border: #e3e5ea;
  --color-text: #1a1d23;
  --color-text-muted: #6b7280;

  /* Brand palette, sampled from the Egrove Systems logo (charcoal wordmark +
     orange mark). --color-primary is the logo's orange hue darkened until
     it clears WCAG AA (4.5:1) as both text-on-white and white-text-on-fill,
     so it's safe for links, buttons and focus rings alike. --color-accent
     keeps the true, vivid logo orange for decorative/graphic use (chart
     lines, stat dots) where text-contrast rules don't apply. --color-ink is
     the logo's near-black, used for the header/footer dark surfaces instead
     of a generic navy-black. */
  --color-primary: #a96009;
  --color-primary-hover: #824a07;
  --color-primary-soft: #fdf4ea;
  --color-accent: #f39629;
  --color-ink: #201d1c;
  --color-success: #15803d;
  --color-success-bg: #dcfce7;
  --color-warning: #a16207;
  --color-warning-bg: #fef9c3;
  --color-muted-bg: #eef0f3;
  --radius-sm: 6px;
  --radius-md: 10px;
  --radius-lg: 16px;
  --shadow-sm: 0 1px 2px rgba(16, 24, 40, 0.06);
  --shadow-md: 0 8px 24px rgba(16, 24, 40, 0.08);
  --font-sans: -apple-system, BlinkMacSystemFont, "Segoe UI", Inter, Roboto, Helvetica, Arial, sans-serif;

  /* Data-viz tokens (see dataviz skill reference palette). Stat-tile status
     dots reuse the --color-success/--color-warning/--color-muted-bg tokens
     above (already the badge palette) so status color stays consistent
     app-wide; the chart line/bars use --color-accent (brand orange) since
     they're a single-series magnitude comparison, not a status signal. */
  --chart-gridline: #e1e0d9;
  --chart-axis: #c3c2b7;
  --chart-muted-text: #898781;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: var(--font-sans);
  color: var(--color-text);
  background: var(--color-bg);
  -webkit-font-smoothing: antialiased;
}

a { color: var(--color-primary); }

/* ---- App shell ---- */

.app-header {
  background: var(--color-ink);
  color: #f4f5f7;
  padding: 0 1.5rem;
  height: 56px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  position: sticky;
  top: 0;
  z-index: 10;
  box-shadow: var(--shadow-sm);
}

.app-header .brand {
  display: flex;
  align-items: center;
  text-decoration: none;
  margin-right: 2rem;
}

.brand-logo-chip {
  display: inline-flex;
  align-items: center;
  background: #fff;
  border-radius: 6px;
  padding: 0.3rem 0.65rem;
}

.brand-logo { height: 22px; width: auto; display: block; }
.brand-logo-lg { height: 34px; }

.app-nav { display: flex; align-items: center; gap: 0.25rem; flex: 1; }

.app-nav a {
  color: #c7cad1;
  text-decoration: none;
  font-size: 0.9rem;
  font-weight: 500;
  padding: 0.4rem 0.75rem;
  border-radius: var(--radius-sm);
}

.app-nav a:hover { color: #fff; background: rgba(255, 255, 255, 0.08); }
.app-nav a.active { color: #fff; background: rgba(255, 255, 255, 0.12); }

.app-user {
  display: flex;
  align-items: center;
  gap: 0.6rem;
  font-size: 0.85rem;
  color: #c7cad1;
}

.app-user .avatar {
  width: 28px;
  height: 28px;
  border-radius: 50%;
  background: var(--color-primary);
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 600;
  font-size: 0.8rem;
}

.app-user a { color: #c7cad1; }
.app-user a:hover { color: #fff; }

.status-toggle {
  display: inline-flex;
  align-items: center;
  gap: 0.7rem;
  background: none;
  border: none;
  padding: 0;
  cursor: pointer;
}

.status-toggle-track {
  position: relative;
  width: 46px;
  height: 26px;
  border-radius: 999px;
  background: var(--color-muted-bg);
  transition: background 0.15s ease;
  flex-shrink: 0;
}

.status-toggle-thumb {
  position: absolute;
  top: 3px;
  left: 3px;
  width: 20px;
  height: 20px;
  border-radius: 50%;
  background: #fff;
  box-shadow: var(--shadow-sm);
  transition: transform 0.15s ease;
}

.status-toggle.is-active .status-toggle-track { background: var(--color-success); }
.status-toggle.is-active .status-toggle-thumb { transform: translateX(20px); }

.status-toggle-label { font-size: 0.95rem; font-weight: 600; color: var(--color-text); }
.status-toggle.is-active .status-toggle-label { color: var(--color-success); }

.app-main {
  max-width: 1040px;
  margin: 0 auto;
  padding: 2rem 1.5rem 3rem;
}

.page-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 1.5rem;
}

.page-header h1 { margin: 0; font-size: 1.4rem; letter-spacing: -0.01em; }
.page-subtitle { color: var(--color-text-muted); font-size: 0.9rem; margin-top: 0.25rem; }

/* ---- Cards / surfaces ---- */

.card {
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-sm);
}

.card-padded { padding: 1.5rem; }

.center-screen {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
}

.auth-card { width: 340px; }
.auth-card h1 { font-size: 1.2rem; margin: 0 0 1.25rem; }

.login-brand {
  display: flex;
  align-items: center;
  margin-bottom: 1.5rem;
}

/* ---- Buttons ---- */

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.4rem;
  background: var(--color-primary);
  color: #fff;
  padding: 0.55rem 1rem;
  border-radius: var(--radius-sm);
  text-decoration: none;
  border: none;
  font-size: 0.88rem;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.15s ease;
}

.btn:hover { background: var(--color-primary-hover); color: #fff; }
.btn:disabled { opacity: 0.5; cursor: not-allowed; }

.btn-secondary {
  background: var(--color-surface);
  color: var(--color-text);
  border: 1px solid var(--color-border);
}

.btn-secondary:hover { background: var(--color-muted-bg); color: var(--color-text); }
.btn-block { width: 100%; }

/* ---- Forms ---- */

form p { display: flex; flex-direction: column; margin: 0 0 1rem; }

label { font-size: 0.82rem; font-weight: 600; color: var(--color-text-muted); margin-bottom: 0.35rem; }

input[type="text"], input[type="password"], input[type="email"], input[type="url"],
textarea, select {
  font-family: inherit;
  font-size: 0.9rem;
  padding: 0.55rem 0.7rem;
  border: 1px solid var(--color-border);
  border-radius: var(--radius-sm);
  background: var(--color-surface);
  color: var(--color-text);
}

input:focus, textarea:focus, select:focus {
  outline: none;
  border-color: var(--color-primary);
  box-shadow: 0 0 0 3px var(--color-primary-soft);
}

.helptext { font-size: 0.78rem; color: var(--color-text-muted); margin-top: 0.25rem; }
.errorlist { color: #b91c1c; font-size: 0.82rem; padding: 0; margin: 0.25rem 0; list-style: none; }

.inline-form { display: flex; gap: 0.6rem; align-items: center; margin-top: 1.25rem; }
.inline-form input { flex: 1; }

/* ---- Table ---- */

.table-card { overflow: hidden; }
.table-scroll { overflow-x: auto; -webkit-overflow-scrolling: touch; }

table { border-collapse: collapse; width: 100%; font-size: 0.88rem; }

th {
  text-align: left;
  padding: 0.7rem 1rem;
  background: var(--color-muted-bg);
  color: var(--color-text-muted);
  font-weight: 600;
  font-size: 0.76rem;
  text-transform: uppercase;
  letter-spacing: 0.03em;
}

td { padding: 0.75rem 1rem; border-top: 1px solid var(--color-border); vertical-align: middle; }
tr:hover td { background: #fafafb; }

/* ---- Badges ---- */

.badge {
  display: inline-flex;
  align-items: center;
  gap: 0.3rem;
  padding: 0.2rem 0.6rem;
  border-radius: 999px;
  font-size: 0.72rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.02em;
}

.badge-open { background: var(--color-success-bg); color: var(--color-success); }
.badge-pending { background: var(--color-warning-bg); color: var(--color-warning); }
.badge-closed { background: var(--color-muted-bg); color: var(--color-text-muted); }

/* ---- Tabs ---- */

.tab-bar {
  display: flex;
  gap: 0.25rem;
  border-bottom: 1px solid var(--color-border);
  margin-bottom: 1rem;
  overflow-x: auto;
}

.tab-bar a {
  display: inline-flex;
  align-items: center;
  gap: 0.45rem;
  padding: 0.65rem 0.9rem;
  font-size: 0.88rem;
  font-weight: 600;
  color: var(--color-text-muted);
  text-decoration: none;
  border-bottom: 2px solid transparent;
  white-space: nowrap;
}

.tab-bar a:hover { color: var(--color-text); }
.tab-bar a.active { color: var(--color-primary); border-bottom-color: var(--color-primary); }

.tab-count {
  background: var(--color-muted-bg);
  color: var(--color-text-muted);
  border-radius: 999px;
  padding: 0.05rem 0.5rem;
  font-size: 0.75rem;
  font-weight: 700;
}

.tab-bar a.active .tab-count { background: var(--color-primary-soft); color: var(--color-primary); }

/* ---- Modal ---- */

.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(15, 17, 23, 0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 2000;
  padding: 1rem;
}

.modal-overlay.hidden { display: none; }

.modal-card {
  background: var(--color-surface);
  border-radius: var(--radius-lg);
  padding: 1.5rem;
  width: 380px;
  max-width: 100%;
  box-shadow: var(--shadow-md);
}

.modal-card h2 { margin: 0 0 0.4rem; font-size: 1.1rem; }
.modal-actions { display: flex; justify-content: flex-end; gap: 0.6rem; margin-top: 1.25rem; }

/* ---- Inbox ---- */

.inbox-list { display: flex; flex-direction: column; }

.conversation-link {
  display: flex;
  align-items: center;
  gap: 0.9rem;
  flex: 1;
  min-width: 0;
  color: inherit;
  text-decoration: none;
}

.btn-sm { padding: 0.3rem 0.7rem; font-size: 0.78rem; flex-shrink: 0; }

.conversation-row {
  display: flex;
  align-items: center;
  gap: 0.9rem;
  padding: 0.9rem 1.25rem;
  border-bottom: 1px solid var(--color-border);
  text-decoration: none;
  color: inherit;
}

.conversation-row:last-child { border-bottom: none; }
.conversation-row:hover { background: #fafafb; }

.conversation-avatar {
  width: 38px;
  height: 38px;
  border-radius: 50%;
  background: var(--color-primary-soft);
  color: var(--color-primary);
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 700;
  font-size: 0.85rem;
  flex-shrink: 0;
}

.conversation-main { flex: 1; min-width: 0; }
.conversation-title { font-weight: 600; font-size: 0.92rem; }
.conversation-meta { font-size: 0.8rem; color: var(--color-text-muted); margin-top: 0.15rem; }
.conversation-time {
  font-size: 0.78rem;
  color: var(--color-text-muted);
  white-space: nowrap;
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  gap: 0.35rem;
}

.unread-badge {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 20px;
  height: 20px;
  padding: 0 0.4rem;
  border-radius: 999px;
  background: var(--color-primary);
  color: #fff;
  font-size: 0.72rem;
  font-weight: 700;
  line-height: 1;
}

.empty-state { padding: 3rem 1.5rem; text-align: center; color: var(--color-text-muted); }

/* ---- Thread ---- */

.thread-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 1rem 1.25rem;
  border-bottom: 1px solid var(--color-border);
}

.thread-header h1 { font-size: 1.05rem; margin: 0; }
.back-link { display: inline-flex; align-items: center; gap: 0.3rem; font-size: 0.85rem; color: var(--color-text-muted); text-decoration: none; margin-bottom: 1rem; }
.back-link:hover { color: var(--color-text); }

.thread-messages {
  padding: 1.25rem;
  height: 420px;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 0.6rem;
  background: #fafafb;
}

.bubble-row { display: flex; }
.bubble-row.from-agent { justify-content: flex-end; }

.bubble {
  max-width: 70%;
  padding: 0.55rem 0.85rem;
  border-radius: var(--radius-md);
  font-size: 0.88rem;
  line-height: 1.4;
}

.bubble .sender { display: block; font-size: 0.72rem; font-weight: 700; opacity: 0.65; margin-bottom: 0.15rem; }
.bubble .time { display: block; font-size: 0.68rem; opacity: 0.55; margin-top: 0.2rem; }

.from-visitor .bubble { background: #fff; border: 1px solid var(--color-border); border-bottom-left-radius: 2px; }
.from-agent .bubble { background: var(--color-primary); color: #fff; border-bottom-right-radius: 2px; }

.bubble-image { display: block; max-width: 100%; max-height: 220px; border-radius: var(--radius-sm); margin-top: 0.1rem; }

.composer {
  display: flex;
  align-items: flex-end;
  gap: 0.6rem;
  padding: 1rem 1.25rem;
  border-top: 1px solid var(--color-border);
}

.composer textarea {
  flex: 1;
  resize: none;
  min-height: 40px;
}

.composer .btn-secondary { flex-shrink: 0; padding: 0.55rem; }

/* ---- Utility ---- */

.text-muted { color: var(--color-text-muted); }
.mt-lg { margin-top: 1.5rem; }
.code-block {
  background: var(--color-ink);
  color: #e5e7eb;
  padding: 0.9rem 1rem;
  border-radius: var(--radius-sm);
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 0.82rem;
  overflow-x: auto;
}

/* ---- Dashboard ---- */

.stat-tile-row {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
  gap: 0.9rem;
  margin-bottom: 1.5rem;
}

.stat-tile {
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-md);
  padding: 1rem 1.1rem;
  box-shadow: var(--shadow-sm);
}

.stat-tile .stat-label {
  display: flex;
  align-items: center;
  gap: 0.4rem;
  font-size: 0.78rem;
  color: var(--color-text-muted);
  font-weight: 600;
  margin-bottom: 0.4rem;
}

.stat-tile .stat-dot { width: 8px; height: 8px; border-radius: 50%; background: var(--color-text-muted); flex-shrink: 0; }
.stat-tile .stat-dot.good { background: var(--color-success); }
.stat-tile .stat-dot.warning { background: var(--color-warning); }

.stat-tile .stat-value { font-size: 1.6rem; font-weight: 700; letter-spacing: -0.01em; }

.dashboard-grid {
  display: grid;
  grid-template-columns: 3fr 2fr;
  gap: 1.25rem;
  align-items: start;
  margin-bottom: 1.5rem;
}

@media (max-width: 800px) {
  .dashboard-grid { grid-template-columns: 1fr; }
}

.chart-card h2, .table-card-title {
  font-size: 0.95rem;
  margin: 0 0 0.9rem;
}

.trend-chart .chart-line { fill: none; stroke: var(--color-accent); stroke-width: 2; stroke-linejoin: round; stroke-linecap: round; }
.trend-chart .chart-gridline { stroke: var(--chart-gridline); stroke-width: 1; }
.trend-chart .chart-dot { fill: var(--color-accent); stroke: var(--color-surface); stroke-width: 2; }
.trend-chart .chart-axis-label { fill: var(--chart-muted-text); font-size: 9px; font-family: var(--font-sans); }
.trend-chart .chart-value-label { fill: var(--color-text); font-size: 11px; font-weight: 600; font-family: var(--font-sans); }

.bar-chart { display: flex; flex-direction: column; gap: 0.7rem; }
.bar-chart-row { display: grid; grid-template-columns: 100px 1fr 34px; align-items: center; gap: 0.6rem; font-size: 0.82rem; }
.bar-chart-row .bar-name { color: var(--color-text); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.bar-chart-track { background: var(--color-muted-bg); border-radius: 999px; height: 10px; overflow: hidden; }
.bar-chart-fill { background: var(--color-accent); height: 100%; border-radius: 999px; }
.bar-chart-row .bar-count { text-align: right; font-variant-numeric: tabular-nums; color: var(--color-text-muted); }

.leaderboard-table td, .leaderboard-table th { font-variant-numeric: tabular-nums; }
.leaderboard-table td:first-child, .leaderboard-table th:first-child { font-variant-numeric: normal; }

/* ---- Responsive ---- */

@media (max-width: 880px) {
  .app-header { flex-wrap: wrap; height: auto; padding: 0.6rem 1rem; row-gap: 0.5rem; }
  .app-header .brand { margin-right: auto; }
  .app-nav {
    order: 3;
    flex-basis: 100%;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    gap: 0.15rem;
  }
  .app-nav a { white-space: nowrap; padding: 0.4rem 0.6rem; }
  .app-user span:not(.avatar) { display: none; }
}

@media (max-width: 720px) {
  .app-main { padding: 1.25rem 1rem 2.5rem; }

  .page-header { flex-direction: column; align-items: flex-start; gap: 0.75rem; }
  .page-header > div:last-child { width: 100%; }

  .thread-header { flex-direction: column; align-items: flex-start; gap: 0.75rem; }
  .thread-header > div:last-child { width: 100%; justify-content: space-between; }

  .conversation-row { flex-wrap: wrap; row-gap: 0.5rem; }
  .conversation-link { flex-wrap: wrap; row-gap: 0.4rem; }
  .conversation-time { flex-direction: row; align-items: center; margin-left: auto; }
  .pick-btn { margin-left: auto; }

  .modal-card { width: 100%; }
}

@media (max-width: 480px) {
  .center-screen { padding: 1.5rem 1rem; align-items: flex-start; }
  .auth-card { width: 100%; }

  .thread-messages { height: 60vh; }
  .composer { flex-direction: column; align-items: stretch; }
  .composer .btn { width: 100%; }

  .inline-form { flex-direction: column; align-items: stretch; }

  .bar-chart-row { grid-template-columns: 72px 1fr 28px; font-size: 0.78rem; }
}
