:root {
  color-scheme: light;
  --paper: #f7f1e8;
  --paper-soft: #fbf6ee;
  --surface: #fffaf2;
  --surface-strong: #ffffff;
  --surface-muted: #f0e4d2;
  --ink: #241c15;
  --ink-soft: #46372a;
  --muted: #746250;
  --line: #dfd0bd;
  --line-strong: #c9b394;
  --primary: #7f1d1d;
  --primary-hover: #661616;
  --primary-soft: #f4dfd7;
  --accent: #0f766e;
  --accent-soft: #d9efea;
  --warning: #a16207;
  --warning-soft: #f5e8bf;
  --danger: #b91c1c;
  --danger-soft: #f5d8d3;
  --shadow: 0 18px 54px rgba(73, 47, 25, 0.12);
  --shadow-soft: 0 10px 28px rgba(73, 47, 25, 0.08);
  --radius: 8px;
  --font-ui: Inter, "Microsoft YaHei UI", "Microsoft YaHei", "PingFang SC", "Segoe UI", system-ui, sans-serif;
  --font-display: Georgia, "Times New Roman", "Microsoft YaHei", "PingFang SC", serif;
  color: var(--ink);
  background: var(--paper);
  font-family: var(--font-ui);
  font-size: 16px;
  line-height: 1.55;
  text-rendering: geometricPrecision;
}

* {
  box-sizing: border-box;
}

html {
  scroll-behavior: smooth;
}

body {
  margin: 0;
  min-width: 320px;
  background:
    linear-gradient(rgba(247, 241, 232, 0.95), rgba(247, 241, 232, 0.95)),
    url("https://images.unsplash.com/photo-1516321318423-f06f85e504b3?auto=format&fit=crop&w=1800&q=80") center/cover fixed;
}

button,
input,
textarea,
select {
  font: inherit;
}

button {
  cursor: pointer;
}

button:focus-visible,
input:focus-visible,
textarea:focus-visible,
select:focus-visible,
[data-route]:focus-visible {
  outline: 3px solid color-mix(in srgb, var(--accent), white 42%);
  outline-offset: 2px;
}

.login-page {
  min-height: 100dvh;
  display: grid;
  grid-template-columns: minmax(0, 1.08fr) minmax(390px, 0.92fr);
  background: var(--paper);
}

.login-visual {
  position: relative;
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  padding: clamp(32px, 5vw, 72px);
  color: #fffaf2;
  background:
    linear-gradient(rgba(39, 27, 18, 0.72), rgba(39, 27, 18, 0.8)),
    url("https://images.unsplash.com/photo-1497366811353-6870744d04b2?auto=format&fit=crop&w=1800&q=80") center/cover;
}

.admin-login-page .login-visual {
  background:
    linear-gradient(rgba(36, 28, 21, 0.76), rgba(36, 28, 21, 0.84)),
    url("https://images.unsplash.com/photo-1450101499163-c8848c66ca85?auto=format&fit=crop&w=1800&q=80") center/cover;
}

.login-copy {
  max-width: 720px;
}

.login-copy h1 {
  margin: 18px 0;
  max-width: 780px;
  font-family: var(--font-display);
  font-size: clamp(42px, 7vw, 82px);
  line-height: 1.02;
  letter-spacing: 0;
}

.login-copy p {
  max-width: 600px;
  margin: 0;
  color: #f6e9d8;
  font-size: clamp(17px, 2vw, 21px);
}

.metric-strip {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 12px;
}

.metric-strip div {
  min-height: 96px;
  padding: 18px;
  border: 1px solid rgba(255, 250, 242, 0.28);
  border-radius: var(--radius);
  background: rgba(255, 250, 242, 0.1);
  backdrop-filter: blur(10px);
}

.metric-strip strong {
  display: block;
  font-family: var(--font-display);
  font-size: 30px;
  line-height: 1;
}

.metric-strip span {
  display: block;
  margin-top: 8px;
  color: #ecdcc8;
  font-size: 13px;
  font-weight: 700;
}

.login-panel {
  display: grid;
  place-items: center;
  padding: clamp(22px, 4vw, 48px);
  background: var(--paper);
}

.auth-card,
.agent-card,
.insight-panel,
.thread-list,
.chat-panel,
.panel-form,
.table-panel,
.stat-card {
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface);
  box-shadow: var(--shadow-soft);
}

.auth-card {
  width: min(440px, 100%);
  padding: 32px;
}

.auth-card h2,
.panel-form h2,
.insight-panel h2,
.agent-intro h2,
.admin-lede h2 {
  margin: 0;
  color: var(--ink);
  font-family: var(--font-display);
  font-size: 28px;
  line-height: 1.12;
  letter-spacing: 0;
}

.eyebrow {
  margin: 0 0 8px;
  color: var(--primary);
  font-size: 12px;
  font-weight: 900;
  letter-spacing: 0;
}

.pill,
.agent-kicker {
  display: inline-flex;
  align-items: center;
  min-height: 30px;
  padding: 0 10px;
  border-radius: var(--radius);
  color: var(--primary);
  background: var(--primary-soft);
  font-size: 12px;
  font-weight: 900;
}

label {
  display: grid;
  gap: 8px;
  margin-top: 16px;
  color: var(--ink-soft);
  font-size: 13px;
  font-weight: 800;
}

input,
textarea,
select {
  width: 100%;
  min-width: 0;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  color: var(--ink);
  background: #fffdf8;
  outline: none;
}

input,
select {
  min-height: 44px;
  padding: 0 12px;
}

textarea {
  min-height: 104px;
  resize: vertical;
  padding: 12px;
}

input:focus,
textarea:focus,
select:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent), transparent 82%);
}

.primary,
.ghost,
.tiny {
  border-radius: var(--radius);
  font-weight: 900;
  transition:
    transform 170ms ease,
    box-shadow 170ms ease,
    background 170ms ease,
    border-color 170ms ease;
}

.primary {
  min-height: 44px;
  padding: 0 16px;
  border: 1px solid var(--primary);
  color: #fffaf2;
  background: var(--primary);
  box-shadow: 0 12px 26px rgba(127, 29, 29, 0.2);
}

.primary:hover {
  background: var(--primary-hover);
  transform: translateY(-1px);
}

.primary:disabled {
  cursor: not-allowed;
  opacity: 0.55;
  transform: none;
  box-shadow: none;
}

.ghost {
  min-height: 44px;
  padding: 0 14px;
  border: 1px solid var(--line);
  color: var(--ink);
  background: var(--surface);
}

.ghost:hover {
  border-color: var(--line-strong);
  background: var(--paper-soft);
  transform: translateY(-1px);
}

.tiny {
  min-height: 32px;
  padding: 0 10px;
  border: 1px solid var(--line);
  color: var(--primary);
  background: var(--primary-soft);
}

.wide {
  width: 100%;
}

.form-error {
  min-height: 22px;
  color: var(--danger);
  font-size: 13px;
}

.app-shell {
  min-height: 100dvh;
  display: grid;
  grid-template-columns: 286px minmax(0, 1fr);
}

.sidebar {
  position: sticky;
  top: 0;
  height: 100dvh;
  display: flex;
  flex-direction: column;
  padding: 24px;
  border-right: 1px solid var(--line);
  background: rgba(255, 250, 242, 0.94);
  backdrop-filter: blur(16px);
}

.brand {
  display: flex;
  align-items: center;
  gap: 12px;
  min-height: 56px;
  cursor: pointer;
}

.brand-mark,
.avatar {
  display: grid;
  place-items: center;
  flex: 0 0 auto;
  width: 42px;
  height: 42px;
  border-radius: var(--radius);
  color: #fffaf2;
  background: var(--primary);
  font-family: var(--font-display);
  font-weight: 900;
}

.brand strong,
.profile strong {
  color: var(--ink);
}

.brand span,
.profile span,
.thread span,
table small,
.thread-help,
.panel-copy,
.topbar-description {
  display: block;
  color: var(--muted);
  font-size: 13px;
}

.side-nav {
  display: grid;
  gap: 8px;
  margin-top: 28px;
}

.side-nav button {
  display: flex;
  align-items: center;
  min-height: 44px;
  border: 1px solid transparent;
  border-radius: var(--radius);
  padding: 0 12px;
  color: var(--ink-soft);
  background: transparent;
  font-weight: 900;
  text-align: left;
}

.side-nav button.active,
.side-nav button:hover {
  color: var(--primary);
  border-color: var(--line);
  background: var(--primary-soft);
}

.sidebar-note {
  margin-top: 22px;
  padding: 16px 0 0;
  border-top: 1px solid var(--line);
}

.sidebar-note span {
  display: block;
  color: var(--primary);
  font-size: 12px;
  font-weight: 900;
}

.sidebar-note strong {
  display: block;
  margin-top: 6px;
  color: var(--ink-soft);
  font-size: 14px;
  line-height: 1.45;
}

.profile {
  display: flex;
  align-items: center;
  gap: 12px;
  min-width: 0;
  margin-top: auto;
  padding-top: 18px;
  border-top: 1px solid var(--line);
}

.profile > div:last-child {
  min-width: 0;
}

.profile strong {
  display: block;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.main {
  min-width: 0;
  padding: clamp(18px, 3vw, 34px);
}

.topbar {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 18px;
  margin-bottom: 22px;
}

.topbar h1 {
  margin: 0;
  color: var(--ink);
  font-family: var(--font-display);
  font-size: clamp(32px, 4vw, 54px);
  line-height: 1.04;
  letter-spacing: 0;
}

.topbar-description {
  max-width: 720px;
  margin: 10px 0 0;
}

.top-actions {
  display: flex;
  gap: 10px;
  flex-wrap: wrap;
}

.content-grid {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 336px;
  gap: 22px;
  align-items: start;
}

.agent-grid {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 18px;
}

.agent-card {
  display: flex;
  flex-direction: column;
  min-height: 272px;
  padding: 22px;
}

.agent-card-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  color: var(--muted);
  font-size: 12px;
  font-weight: 900;
}

.agent-card h2 {
  margin: 22px 0 10px;
  color: var(--ink);
  font-family: var(--font-display);
  font-size: 29px;
  line-height: 1.12;
  letter-spacing: 0;
}

.agent-card p {
  flex: 1;
  margin: 0 0 22px;
  color: var(--muted);
}

.agent-card:hover,
.stat-card:hover {
  border-color: var(--line-strong);
  box-shadow: var(--shadow);
}

.insight-panel {
  position: sticky;
  top: 24px;
  padding: 22px;
}

.panel-copy {
  margin: 10px 0 0;
}

.insight-list {
  display: grid;
  gap: 12px;
  margin-top: 20px;
}

.insight-list div {
  display: flex;
  justify-content: space-between;
  gap: 16px;
  padding: 14px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--paper-soft);
}

.insight-list strong {
  color: var(--ink);
  font-variant-numeric: tabular-nums;
}

.chat-layout {
  display: grid;
  grid-template-columns: 306px minmax(0, 1fr);
  gap: 18px;
  height: calc(100dvh - 148px);
}

.thread-list {
  min-height: 0;
  padding: 14px;
  overflow: auto;
}

.thread-help {
  margin: 12px 0 0;
}

.thread-stack {
  display: grid;
  gap: 8px;
  margin-top: 14px;
}

.thread {
  display: grid;
  gap: 4px;
  width: 100%;
  min-height: 58px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 12px;
  color: var(--ink-soft);
  background: var(--paper-soft);
  text-align: left;
}

.thread.active,
.thread:hover {
  border-color: var(--primary);
  background: var(--primary-soft);
}

.chat-panel {
  min-width: 0;
  min-height: 0;
  display: grid;
  grid-template-rows: auto minmax(0, 1fr) auto;
}

.agent-intro {
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 20px 22px;
  border-bottom: 1px solid var(--line);
  background: var(--surface);
}

.agent-intro p {
  margin: 8px 0 0;
  color: var(--muted);
}

.messages {
  min-height: 0;
  overflow: auto;
  padding: 24px;
  background: var(--paper-soft);
}

.message {
  width: min(820px, 94%);
  margin-bottom: 16px;
}

.message.user {
  margin-left: auto;
}

.message-meta {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 6px;
  color: var(--muted);
  font-size: 12px;
}

.message.user .message-meta {
  justify-content: flex-end;
}

.message pre {
  margin: 0;
  white-space: pre-wrap;
  word-break: break-word;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 16px 18px;
  color: var(--ink);
  background: var(--surface);
  font-family: var(--font-ui);
  line-height: 1.66;
}

.message.user pre {
  color: #fffaf2;
  border-color: var(--primary);
  background: var(--primary);
}

.composer {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 116px;
  gap: 12px;
  padding: 16px;
  border-top: 1px solid var(--line);
  background: var(--surface);
}

.composer textarea {
  min-height: 76px;
  margin: 0;
  background: #fffdf8;
}

.empty-state {
  display: grid;
  place-items: center;
  align-content: center;
  min-height: 100%;
  padding: 34px;
  color: var(--muted);
  text-align: center;
}

.empty-state strong {
  color: var(--ink);
  font-family: var(--font-display);
  font-size: 30px;
}

.admin-layout {
  display: grid;
  gap: 16px;
  min-width: 0;
}

.admin-lede {
  max-width: 940px;
  padding: 20px 0 4px;
  border-top: 1px solid var(--line);
}

.admin-content {
  min-width: 0;
}

.tabs {
  display: flex;
  gap: 8px;
  overflow-x: auto;
  padding-bottom: 2px;
}

.tabs button {
  min-width: max-content;
  min-height: 44px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 0 14px;
  color: var(--ink-soft);
  background: var(--surface);
  font-weight: 900;
}

.tabs button.active,
.tabs button:hover {
  color: #fffaf2;
  border-color: var(--primary);
  background: var(--primary);
}

.stat-grid {
  display: grid;
  grid-template-columns: repeat(5, minmax(0, 1fr));
  gap: 14px;
}

.stat-card {
  min-height: 128px;
  padding: 20px;
}

.stat-card span {
  color: var(--muted);
  font-size: 13px;
  font-weight: 800;
}

.stat-card strong {
  display: block;
  margin-top: 12px;
  color: var(--ink);
  font-family: var(--font-display);
  font-size: clamp(28px, 4vw, 44px);
  line-height: 1;
  font-variant-numeric: tabular-nums;
}

.split {
  display: grid;
  grid-template-columns: 380px minmax(0, 1fr);
  gap: 16px;
  align-items: start;
}

.panel-form,
.table-panel {
  padding: 18px;
}

.panel-form.compact {
  position: sticky;
  top: 22px;
}

.form-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
}

.advanced-config {
  margin-top: 16px;
  padding: 12px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--paper-soft);
}

.advanced-config summary {
  color: var(--ink);
  font-size: 13px;
  font-weight: 900;
  cursor: pointer;
}

.button-row {
  display: flex;
  gap: 10px;
  flex-wrap: wrap;
  margin-top: 18px;
}

.table-panel {
  overflow: auto;
  min-width: 0;
  max-width: 100%;
}

table {
  width: 100%;
  min-width: 760px;
  border-collapse: collapse;
}

th,
td {
  border-bottom: 1px solid var(--line);
  padding: 13px 10px;
  text-align: left;
  vertical-align: top;
}

th {
  color: var(--primary);
  font-size: 12px;
  font-weight: 900;
  text-transform: uppercase;
}

td {
  color: var(--ink-soft);
}

td strong {
  color: var(--ink);
}

td input,
td select {
  min-width: 160px;
}

td .tiny {
  margin-left: 8px;
}

code {
  padding: 3px 6px;
  border-radius: 6px;
  color: var(--accent);
  background: var(--accent-soft);
}

.status {
  display: inline-flex;
  align-items: center;
  min-height: 26px;
  padding: 0 9px;
  border-radius: var(--radius);
  font-size: 12px;
  font-weight: 900;
}

.status.active,
.status.success {
  color: var(--accent);
  background: var(--accent-soft);
}

.status.draft {
  color: var(--warning);
  background: var(--warning-soft);
}

.status.disabled,
.status.error {
  color: var(--danger);
  background: var(--danger-soft);
}

.toast {
  position: fixed;
  right: 24px;
  bottom: 24px;
  z-index: 10;
  max-width: min(420px, calc(100vw - 48px));
  padding: 12px 14px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  color: var(--ink);
  background: var(--surface);
  box-shadow: var(--shadow);
}

.toast.success {
  border-color: color-mix(in srgb, var(--accent), white 35%);
}

.toast.error {
  border-color: color-mix(in srgb, var(--danger), white 35%);
}

.fatal {
  min-height: 100dvh;
  display: grid;
  place-items: center;
  align-content: center;
  gap: 16px;
  padding: 24px;
  text-align: center;
}

@media (max-width: 1240px) {
  .agent-grid,
  .stat-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .content-grid,
  .split {
    grid-template-columns: 1fr;
  }

  .insight-panel,
  .panel-form.compact {
    position: static;
  }
}

@media (max-width: 860px) {
  .login-page,
  .app-shell,
  .chat-layout {
    grid-template-columns: 1fr;
  }

  .login-visual {
    min-height: 430px;
    padding: 28px;
  }

  .metric-strip {
    grid-template-columns: 1fr;
  }

  .sidebar {
    position: static;
    height: auto;
    gap: 14px;
  }

  .side-nav {
    grid-template-columns: repeat(2, minmax(0, 1fr));
    margin-top: 8px;
  }

  .sidebar-note {
    margin-top: 4px;
  }

  .profile {
    margin-top: 0;
  }

  .main {
    padding: 18px;
  }

  .topbar {
    flex-direction: column;
  }

  .agent-grid,
  .stat-grid {
    grid-template-columns: 1fr;
  }

  .chat-layout {
    height: auto;
  }

  .thread-list {
    max-height: 240px;
  }

  .chat-panel {
    min-height: 72dvh;
  }

  .composer {
    grid-template-columns: 1fr;
  }

  .form-row {
    grid-template-columns: 1fr;
  }
}

@media (max-width: 520px) {
  .auth-card,
  .panel-form,
  .table-panel,
  .agent-card,
  .insight-panel {
    padding: 16px;
  }

  .login-panel {
    padding: 18px;
  }

  .side-nav {
    grid-template-columns: 1fr;
  }

  .messages {
    padding: 16px;
  }

  .message {
    width: 100%;
  }
}

@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    scroll-behavior: auto !important;
    transition-duration: 0.01ms !important;
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
  }
}
