:root {
  --bg: #f8fafc;
  --surface: #ffffff;
  --surface-2: #eef2f7;
  --text: #020617;
  --muted: #64748b;
  --line: #dbe3ef;
  --primary: #1e3a8a;
  --primary-strong: #0f172a;
  --accent: #ca8a04;
  --danger: #b91c1c;
  --radius: 8px;
  --shadow: 0 10px 30px rgba(15, 23, 42, 0.08);
  --sidebar-width: clamp(220px, 18vw, 248px);
  --page-padding: clamp(16px, 2.2vw, 28px);
  font-family: "Fira Sans", "Microsoft YaHei", Arial, sans-serif;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-width: 320px;
  background: var(--bg);
  color: var(--text);
  line-height: 1.5;
}

a {
  color: inherit;
  text-decoration: none;
}

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

input,
select,
textarea {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 9px 10px;
  background: #fff;
  color: var(--text);
}

textarea {
  min-height: 74px;
  resize: vertical;
}

input:focus,
select:focus,
textarea:focus,
button:focus-visible,
a:focus-visible {
  outline: 3px solid rgba(30, 58, 138, 0.25);
  outline-offset: 2px;
}

.app-shell {
  min-height: 100vh;
  display: grid;
  grid-template-columns: var(--sidebar-width) minmax(0, 1fr);
}

.sidebar {
  background: var(--primary-strong);
  color: #fff;
  padding: 22px 16px;
  min-height: 100vh;
  position: sticky;
  top: 0;
}

.brand,
.login-brand {
  display: flex;
  align-items: center;
  gap: 12px;
}

.brand-mark {
  display: inline-grid;
  place-items: center;
  width: 38px;
  height: 38px;
  border-radius: 8px;
  background: var(--accent);
  color: #111827;
  font-weight: 800;
}

.brand strong {
  display: block;
  font-size: 17px;
}

.brand small,
.muted,
small {
  color: var(--muted);
}

.sidebar .brand small {
  color: #aebbd0;
}

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

.nav a {
  padding: 10px 12px;
  border-radius: 6px;
  color: #d9e2f1;
  transition: background 0.2s ease, color 0.2s ease;
}

.nav a:hover {
  background: rgba(255, 255, 255, 0.1);
  color: #fff;
}

.main {
  min-width: 0;
  max-width: 1800px;
  padding: var(--page-padding);
}

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

.topbar h1 {
  margin: 0;
  font-size: clamp(22px, 2vw, 28px);
  letter-spacing: 0;
}

.userbar {
  display: flex;
  align-items: center;
  gap: 12px;
  flex-wrap: wrap;
}

.panel {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 18px;
  margin-bottom: 18px;
  overflow-x: auto;
}

.panel-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 12px;
  margin-bottom: 14px;
}

.panel-header h2 {
  margin: 0;
  font-size: 17px;
}

.stats-grid {
  display: grid;
  grid-template-columns: repeat(4, minmax(180px, 1fr));
  gap: 14px;
  margin-bottom: 18px;
}

.stat-card {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 18px;
  box-shadow: var(--shadow);
}

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

.stat-card strong {
  display: block;
  margin-top: 8px;
  font-size: 28px;
}

.stat-card small {
  display: block;
  margin-top: 6px;
  color: var(--muted);
  font-size: 12px;
}

.dashboard-grid {
  display: grid;
  grid-template-columns: minmax(0, 1.6fr) minmax(300px, 1fr);
  gap: 18px;
  margin-bottom: 18px;
}

.compact-list {
  display: grid;
  gap: 10px;
}

.compact-row {
  display: grid;
  grid-template-columns: auto minmax(0, 1fr) auto;
  gap: 12px;
  align-items: center;
  padding: 12px 14px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fbfdff;
}

.compact-row strong,
.compact-row span,
.compact-row small {
  min-width: 0;
}

.compact-row strong {
  font-size: 13px;
}

.compact-row span {
  color: var(--muted);
  font-size: 13px;
}

.compact-row small {
  text-align: right;
  color: #334155;
}

.trend-table {
  min-width: 100%;
}

.mini-bar {
  position: relative;
  height: 8px;
  border-radius: 999px;
  background: #e2e8f0;
  overflow: hidden;
}

.mini-bar-fill {
  position: absolute;
  inset: 0 auto 0 0;
  border-radius: inherit;
  background: linear-gradient(90deg, #1e3a8a, #3b82f6);
}

.mini-bar-fill.warn {
  background: linear-gradient(90deg, #b45309, #f59e0b);
}

.pill {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border-radius: 999px;
  padding: 4px 10px;
  font-size: 12px;
  font-weight: 700;
}

.pill-positive {
  background: #dcfce7;
  color: #166534;
}

.pill-negative {
  background: #fee2e2;
  color: #991b1b;
}

.muted-block {
  color: var(--muted);
  font-size: 13px;
}

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

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

.data-table th {
  color: #334155;
  font-size: 13px;
  background: #f8fafc;
}

.data-table tr:hover td {
  background: #fbfdff;
}

.btn {
  border: 0;
  border-radius: 6px;
  padding: 9px 13px;
  min-height: 44px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: transform 0.15s ease, opacity 0.15s ease, background 0.15s ease;
  white-space: nowrap;
}

.btn:hover {
  transform: translateY(-1px);
}

.btn:disabled {
  cursor: not-allowed;
  opacity: 0.55;
  transform: none;
}

.btn-primary {
  background: var(--primary);
  color: #fff;
}

.btn-secondary {
  background: #e2e8f0;
  color: #0f172a;
}

.btn-danger {
  background: var(--danger);
  color: #fff;
}

.form-stack {
  display: grid;
  gap: 13px;
}

label span {
  display: block;
  margin-bottom: 6px;
  color: #334155;
  font-size: 13px;
  font-weight: 600;
}

.grid-form {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 14px;
  align-items: end;
}

.grid-form > .btn,
.grid-form > a.btn {
  width: 100%;
}

.span-2 {
  grid-column: span 2;
}

.two-column {
  display: grid;
  grid-template-columns: 320px minmax(0, 1fr);
  gap: 18px;
}

.inline-form {
  display: flex;
  align-items: flex-start;
  flex-wrap: wrap;
  gap: 8px;
}

.inline-form.wide {
  display: grid;
  min-width: 0;
}

.actions {
  display: flex;
  gap: 8px;
  align-items: flex-start;
  flex-wrap: wrap;
}

.copy-input {
  min-width: 0;
  width: min(100%, 420px);
  cursor: pointer;
}

.login-page {
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: 24px;
  background: radial-gradient(circle at top left, #dbeafe, transparent 28%), var(--bg);
}

.login-panel {
  width: min(420px, 100%);
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 28px;
}

.login-brand {
  margin-bottom: 22px;
}

.auth-switch {
  margin: 12px 0 0;
  text-align: center;
}

.auth-switch a {
  color: var(--primary);
  font-weight: 700;
}

.login-brand h1 {
  margin: 0;
  font-size: 22px;
}

.login-brand p {
  margin: 2px 0 0;
  color: var(--muted);
}

.alert {
  border-radius: 6px;
  padding: 10px 12px;
  margin-bottom: 14px;
}

.alert-error {
  background: #fee2e2;
  color: #991b1b;
}

.alert-success {
  background: #dcfce7;
  color: #166534;
}

.install-panel {
  width: min(560px, 100%);
}

.status-ok,
.status-warn {
  display: inline-block;
  border-radius: 999px;
  padding: 3px 9px;
  font-size: 12px;
  font-weight: 700;
}

.status-ok {
  background: #dcfce7;
  color: #166534;
}

.status-warn {
  background: #fef3c7;
  color: #92400e;
}

.chat-page {
  min-height: 100vh;
  background: #e8eef7;
}

.chat-shell {
  width: min(920px, 100%);
  min-height: 100vh;
  min-height: 100dvh;
  margin: 0 auto;
  background: #fff;
  display: grid;
  grid-template-rows: auto minmax(0, 1fr) auto;
  border-left: 1px solid var(--line);
  border-right: 1px solid var(--line);
}

.chat-header {
  min-height: 68px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 18px;
  background: var(--primary-strong);
  color: #fff;
}

.chat-header strong {
  display: block;
}

.chat-header span,
.chat-header small {
  color: #cbd5e1;
}

.chat-messages {
  overflow-y: auto;
  padding: 18px;
  background: #f8fafc;
}

.message {
  display: grid;
  gap: 4px;
  margin-bottom: 14px;
}

.message.user {
  justify-items: end;
}

.message.assistant {
  justify-items: start;
}

.bubble {
  max-width: min(74%, 680px);
  padding: 11px 13px;
  border-radius: 8px;
  white-space: pre-wrap;
  word-break: break-word;
  box-shadow: 0 1px 2px rgba(15, 23, 42, 0.08);
}

.message.assistant .bubble {
  background: #fff;
  border: 1px solid var(--line);
}

.message.user .bubble {
  background: var(--primary);
  color: #fff;
}

.bubble p {
  margin: 0;
  white-space: pre-wrap;
}

.bubble p + p,
.bubble p + .bubble-list,
.bubble .bubble-list + p,
.bubble .bubble-list + .bubble-list {
  margin-top: 10px;
}

.bubble-list {
  margin: 0;
  padding-left: 20px;
}

.bubble-list li + li {
  margin-top: 6px;
}

.message time {
  color: var(--muted);
  font-size: 12px;
}

.feedback-panel {
  display: grid;
  gap: 8px;
  margin-top: 2px;
}

.feedback-actions {
  display: inline-flex;
  gap: 8px;
}

.feedback-btn {
  min-width: 40px;
  min-height: 32px;
  padding: 6px 10px;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: #fff;
  color: #334155;
  cursor: pointer;
}

.feedback-btn:hover {
  background: #f8fafc;
}

.feedback-form {
  display: grid;
  gap: 8px;
  width: min(100%, 420px);
}

.feedback-form textarea {
  min-height: 68px;
}

.feedback-form-actions {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}

.feedback-state {
  color: #334155;
  font-size: 12px;
  font-weight: 700;
}

.feedback-note {
  margin: 0;
  color: var(--muted);
  font-size: 13px;
  max-width: min(100%, 420px);
}

.chat-input {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 10px;
  padding: 12px;
  border-top: 1px solid var(--line);
  background: #fff;
}

.chat-input textarea {
  min-height: 42px;
  max-height: 140px;
  resize: none;
}

.invalid-link {
  display: grid;
  place-content: center;
  text-align: center;
  padding: 32px;
}

.invalid-link h1 {
  margin: 0 0 8px;
  font-size: 24px;
}

.invalid-link p {
  margin: 0;
  color: var(--muted);
}

.modal-open {
  overflow: hidden;
}

.qr-modal[hidden] {
  display: none !important;
}

.qr-modal {
  position: fixed;
  inset: 0;
  background: rgba(15, 23, 42, 0.58);
  display: grid;
  place-items: center;
  padding: 24px;
  z-index: 1000;
}

.qr-dialog {
  width: min(520px, 100%);
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 14px;
  box-shadow: 0 20px 60px rgba(15, 23, 42, 0.28);
  padding: 18px;
}

.qr-preview-wrap {
  display: grid;
  gap: 14px;
  justify-items: center;
}

.qr-code-box {
  width: 252px;
  min-height: 252px;
  display: grid;
  place-items: center;
  border-radius: 12px;
  background: linear-gradient(180deg, #f8fafc, #eef2f7);
  border: 1px solid var(--line);
  padding: 16px;
}

.qr-code-box img,
.qr-code-box canvas {
  display: block;
  max-width: 100%;
  height: auto;
}

code {
  font-family: "Fira Code", Consolas, monospace;
  font-size: 12px;
}

@media (max-width: 960px) {
  .app-shell {
    grid-template-columns: 1fr;
  }

  .sidebar {
    position: static;
    min-height: auto;
  }

  .nav {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .panel-header,
  .topbar,
  .userbar {
    align-items: flex-start;
    flex-direction: column;
  }

  .stats-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .grid-form,
  .two-column,
  .dashboard-grid {
    grid-template-columns: 1fr;
  }

  .span-2 {
    grid-column: auto;
  }

  .data-table {
    min-width: 640px;
  }

  .copy-input {
    width: 100%;
  }
}

@media (max-width: 560px) {
  .main {
    padding: 16px;
  }

  .panel {
    padding: 14px;
  }

  .stats-grid {
    gap: 10px;
    grid-template-columns: 1fr;
  }

  .chat-shell {
    border: 0;
  }

  .chat-header {
    flex-direction: column;
    align-items: flex-start;
    gap: 6px;
  }

  .bubble {
    max-width: 88%;
  }

  .chat-input {
    grid-template-columns: 1fr;
  }

  .feedback-form {
    width: 100%;
  }

  .inline-form,
  .actions {
    width: 100%;
  }

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

  .actions > .btn,
  .actions > a.btn,
  .actions > form {
    width: 100%;
  }

  .data-table {
    min-width: 560px;
  }

  .qr-dialog {
    padding: 14px;
  }

  .qr-code-box {
    width: 100%;
    min-height: 220px;
  }
}

@media (min-width: 1600px) {
  .stats-grid {
    grid-template-columns: repeat(4, minmax(220px, 1fr));
  }

  .data-table {
    min-width: 100%;
  }
}

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