:root {
  --bg: #0c0e12;
  --surface: #141821;
  --surface-2: #1b212e;
  --border: #262e3f;
  --text: #eef1f7;
  --muted: #93a0b8;
  --accent: #f97316;
  --accent-hover: #fb923c;
  --accent-soft: rgba(249, 115, 22, 0.14);
  --danger: #f87171;
  --radius: 14px;
  --font: -apple-system, BlinkMacSystemFont, 'Inter', 'Segoe UI', Roboto, sans-serif;
  --mono: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace;
}

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  padding: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  -webkit-font-smoothing: antialiased;
}

.hidden {
  display: none !important;
}

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
}

/* ---------------- Auth Screen ---------------- */

.auth-screen {
  min-height: 100dvh;
  display: grid;
  place-items: center;
  padding: 20px;
  background: radial-gradient(circle at 50% 0%, rgba(249, 115, 22, 0.14), transparent 55%), var(--bg);
}

.auth-card {
  width: 100%;
  max-width: 380px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 18px;
  padding: 32px 26px;
  text-align: center;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.45);
}

.brand-badge {
  width: 54px;
  height: 54px;
  margin: 0 auto 14px;
  border-radius: 15px;
  background: var(--accent-soft);
  border: 1px solid rgba(249, 115, 22, 0.35);
  display: grid;
  place-items: center;
  font-size: 26px;
}

.auth-card h1 {
  margin: 0 0 8px;
  font-size: 22px;
  letter-spacing: -0.02em;
}

.auth-sub {
  margin: 0 0 22px;
  font-size: 14px;
  line-height: 1.5;
  color: var(--muted);
}

.auth-field {
  text-align: left;
  margin-bottom: 12px;
}

.auth-label {
  display: block;
  font-size: 12.5px;
  font-weight: 600;
  color: var(--muted);
  margin-bottom: 6px;
}

.auth-select {
  width: 100%;
  background: var(--bg);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 11px;
  padding: 11px 12px;
  font-size: 14.5px;
  outline: none;
  cursor: pointer;
}

.auth-select:focus {
  border-color: var(--accent);
}

.input-wrap {
  display: flex;
  align-items: center;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 11px;
  padding: 4px 6px 4px 12px;
  transition: border-color 0.15s;
}

.input-wrap:focus-within {
  border-color: var(--accent);
}

.input-wrap input {
  flex: 1;
  background: transparent;
  border: none;
  outline: none;
  color: var(--text);
  font-size: 15px;
  padding: 9px 0;
}

.icon-btn {
  background: transparent;
  border: none;
  color: var(--muted);
  font-size: 12px;
  font-weight: 600;
  padding: 6px 8px;
  border-radius: 7px;
  cursor: pointer;
}

.icon-btn:hover {
  color: var(--text);
  background: var(--surface-2);
}

.primary-btn {
  margin-top: 14px;
  width: 100%;
  padding: 12px 16px;
  border: none;
  border-radius: 11px;
  background: var(--accent);
  color: #0c0e12;
  font-size: 15px;
  font-weight: 700;
  cursor: pointer;
  transition: background 0.15s, transform 0.1s;
}

.primary-btn:hover:not(:disabled) {
  background: var(--accent-hover);
}

.primary-btn:disabled {
  opacity: 0.6;
  cursor: wait;
}

.error-text {
  margin: 10px 0 0;
  font-size: 13px;
  color: var(--danger);
  text-align: left;
}

/* ---------------- App Shell ---------------- */

.app-shell {
  height: 100dvh;
  display: flex;
  flex-direction: column;
}

.topbar {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 12px 18px;
  background: rgba(20, 24, 33, 0.9);
  backdrop-filter: blur(10px);
  border-bottom: 1px solid var(--border);
  z-index: 10;
}

.topbar-left {
  display: flex;
  align-items: center;
  gap: 10px;
  min-width: 0;
}

.logo-icon {
  font-size: 22px;
}

.logo-title {
  font-size: 15px;
  font-weight: 700;
  letter-spacing: -0.01em;
}

.logo-sub {
  font-size: 12px;
  color: var(--muted);
}

.topbar-controls {
  display: flex;
  align-items: center;
  gap: 8px;
}

#manager-select {
  background: var(--surface-2);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 9px;
  padding: 7px 10px;
  font-size: 13px;
  max-width: 210px;
  cursor: pointer;
}

#manager-select:focus {
  outline: none;
  border-color: var(--accent);
}

.ghost-btn {
  background: transparent;
  color: var(--muted);
  border: 1px solid var(--border);
  border-radius: 9px;
  padding: 7px 11px;
  font-size: 13px;
  font-weight: 500;
  cursor: pointer;
  white-space: nowrap;
  transition: color 0.15s, border-color 0.15s, background 0.15s;
}

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

.status-btn {
  display: inline-flex;
  align-items: center;
  gap: 7px;
}

.status-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  flex: 0 0 auto;
  background: #eab308;
}

.status-dot.ok {
  background: #22c55e;
  box-shadow: 0 0 0 3px rgba(34, 197, 94, 0.18);
}

.status-dot.expired {
  background: #ef4444;
  box-shadow: 0 0 0 3px rgba(239, 68, 68, 0.22);
}

.status-dot.checking {
  background: #eab308;
}

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

.modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(5, 7, 10, 0.72);
  backdrop-filter: blur(4px);
  display: grid;
  place-items: center;
  padding: 16px;
  z-index: 50;
}

.modal-card {
  width: 100%;
  max-width: 500px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 20px;
  box-shadow: 0 24px 70px rgba(0, 0, 0, 0.55);
}

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

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

.session-banner {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 12px 14px;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 12px;
  margin-bottom: 16px;
}

.session-banner-left {
  display: flex;
  align-items: center;
  gap: 11px;
  min-width: 0;
}

.session-state {
  font-size: 14px;
  font-weight: 600;
}

.session-meta {
  font-size: 12px;
  color: var(--muted);
  margin-top: 2px;
}

.modal-section {
  display: flex;
  flex-direction: column;
}

.field-label {
  font-size: 13px;
  font-weight: 600;
  margin-bottom: 4px;
}

.field-hint {
  margin: 0 0 10px;
  font-size: 12.5px;
  line-height: 1.5;
  color: var(--muted);
}

.field-hint code,
.modal-footer-tip code {
  font-family: var(--mono);
  font-size: 11.5px;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 4px;
  padding: 1px 4px;
  color: var(--text);
}

.cookie-textarea {
  width: 100%;
  background: var(--bg);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 10px 12px;
  font-family: var(--mono);
  font-size: 12px;
  line-height: 1.45;
  resize: vertical;
  outline: none;
}

.cookie-textarea:focus {
  border-color: var(--accent);
}

.form-msg {
  margin: 10px 0 0;
  font-size: 13px;
}

.form-msg.error {
  color: var(--danger);
}

.form-msg.success {
  color: #4ade80;
}

.modal-footer-tip {
  margin-top: 16px;
  padding-top: 14px;
  border-top: 1px solid var(--border);
  font-size: 12.5px;
  line-height: 1.5;
  color: var(--muted);
}

.inline-action-btn {
  margin-top: 10px;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 7px 12px;
  border-radius: 9px;
  border: 1px solid rgba(249, 115, 22, 0.5);
  background: var(--accent-soft);
  color: var(--text);
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
}

.inline-action-btn:hover {
  background: rgba(249, 115, 22, 0.24);
}

/* ---------------- Chat Feed ---------------- */

.chat-feed {
  flex: 1 1 auto;
  overflow-y: auto;
  padding: 20px 16px 28px;
  scroll-behavior: smooth;
}

.empty-state {
  max-width: 740px;
  margin: 3.5vh auto 0;
  text-align: center;
}

.empty-hero {
  font-size: 40px;
  margin-bottom: 10px;
}

.empty-state h2 {
  margin: 0 0 8px;
  font-size: 24px;
  letter-spacing: -0.02em;
}

.empty-state p {
  margin: 0 auto 24px;
  max-width: 480px;
  color: var(--muted);
  font-size: 14px;
  line-height: 1.55;
}

.prompt-chips {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 8px;
}

.chip {
  background: var(--surface);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 9px 14px;
  font-size: 13px;
  cursor: pointer;
  transition: border-color 0.15s, background 0.15s, transform 0.1s;
}

.chip:hover {
  background: var(--surface-2);
  border-color: var(--accent);
}

.messages {
  max-width: 820px;
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.msg {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.msg.user {
  align-items: flex-end;
}

.msg.assistant {
  align-items: flex-start;
}

.bubble {
  max-width: 100%;
  border-radius: var(--radius);
  padding: 12px 16px;
  font-size: 14.5px;
  line-height: 1.6;
  word-break: break-word;
}

.msg.user .bubble {
  max-width: min(82%, 580px);
  background: var(--accent-soft);
  border: 1px solid rgba(249, 115, 22, 0.35);
  color: var(--text);
  white-space: pre-wrap;
}

.msg.assistant .bubble {
  width: 100%;
  background: var(--surface);
  border: 1px solid var(--border);
}

.msg.assistant.error .bubble {
  border-color: rgba(248, 113, 113, 0.45);
  color: #fecaca;
}

.tools-meta {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  padding-left: 2px;
}

.tool-pill {
  font-size: 11px;
  color: var(--muted);
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 2px 9px;
}

/* ---------------- Markdown Content ---------------- */

.md > *:first-child {
  margin-top: 0;
}

.md > *:last-child {
  margin-bottom: 0;
}

.md p {
  margin: 0 0 10px;
}

.md h1,
.md h2,
.md h3,
.md h4 {
  margin: 14px 0 8px;
  line-height: 1.3;
  letter-spacing: -0.01em;
}

.md h1 { font-size: 18px; }
.md h2 { font-size: 16.5px; }
.md h3 { font-size: 15px; color: var(--accent-hover); }
.md h4 { font-size: 14px; }

.md ul,
.md ol {
  margin: 6px 0 10px;
  padding-left: 22px;
}

.md li {
  margin: 4px 0;
}

.md blockquote {
  margin: 10px 0;
  padding: 6px 12px;
  border-left: 3px solid var(--accent);
  color: var(--muted);
  background: rgba(255, 255, 255, 0.02);
}

.md code {
  font-family: var(--mono);
  font-size: 13px;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 5px;
  padding: 1px 5px;
}

.md pre {
  margin: 10px 0;
  padding: 12px;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 10px;
  overflow-x: auto;
}

.md pre code {
  background: none;
  border: none;
  padding: 0;
  font-size: 12.5px;
  line-height: 1.5;
}

.md .table-wrap {
  width: 100%;
  overflow-x: auto;
  margin: 12px 0;
  border: 1px solid var(--border);
  border-radius: 10px;
}

.md table {
  width: 100%;
  border-collapse: collapse;
  font-size: 13px;
  font-variant-numeric: tabular-nums;
}

.md th,
.md td {
  padding: 8px 11px;
  text-align: left;
  border-bottom: 1px solid var(--border);
  white-space: nowrap;
}

.md th {
  background: var(--surface-2);
  color: #cbd5e1;
  font-weight: 600;
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: 0.03em;
}

.md tbody tr:nth-child(even) {
  background: rgba(255, 255, 255, 0.02);
}

.md tbody tr:last-child td {
  border-bottom: none;
}

/* ---------------- Thinking Indicator ---------------- */

.thinking {
  display: inline-flex;
  align-items: center;
  gap: 10px;
  color: var(--muted);
  font-size: 13.5px;
}

.dots {
  display: inline-flex;
  gap: 4px;
}

.dots span {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--accent);
  animation: pulse 1.2s infinite ease-in-out;
}

.dots span:nth-child(2) { animation-delay: 0.2s; }
.dots span:nth-child(3) { animation-delay: 0.4s; }

@keyframes pulse {
  0%, 80%, 100% { opacity: 0.25; transform: scale(0.8); }
  40% { opacity: 1; transform: scale(1.1); }
}

/* ---------------- Composer ---------------- */

.composer-bar {
  flex: 0 0 auto;
  padding: 12px 16px calc(12px + env(safe-area-inset-bottom));
  background: rgba(12, 14, 18, 0.94);
  border-top: 1px solid var(--border);
}

.composer-form {
  max-width: 820px;
  margin: 0 auto;
  display: flex;
  align-items: flex-end;
  gap: 10px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 8px 8px 8px 14px;
  transition: border-color 0.15s;
}

.composer-form:focus-within {
  border-color: var(--accent);
}

#chat-input {
  flex: 1;
  background: transparent;
  border: none;
  outline: none;
  resize: none;
  color: var(--text);
  font-family: var(--font);
  font-size: 15px;
  line-height: 1.45;
  max-height: 160px;
  padding: 6px 0;
}

.send-btn {
  flex: 0 0 auto;
  width: 38px;
  height: 38px;
  border: none;
  border-radius: 10px;
  background: var(--accent);
  color: #0c0e12;
  display: grid;
  place-items: center;
  cursor: pointer;
  transition: background 0.15s, opacity 0.15s;
}

.send-btn:hover:not(:disabled) {
  background: var(--accent-hover);
}

.send-btn:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}

.composer-hint {
  max-width: 820px;
  margin: 6px auto 0;
  font-size: 11.5px;
  color: var(--muted);
  text-align: center;
}

@media (max-width: 640px) {
  .topbar {
    flex-wrap: wrap;
    padding: 10px 12px;
  }
  .logo-sub {
    display: none;
  }
  .topbar-controls {
    width: 100%;
    justify-content: space-between;
  }
  #manager-select {
    flex: 1;
    max-width: none;
  }
  .composer-hint {
    display: none;
  }
}

/* ---------------- Admin View ---------------- */

.admin-back-link {
  margin: 16px 0 0;
  font-size: 13px;
}

.admin-back-link a {
  color: var(--muted);
  text-decoration: none;
}

.admin-back-link a:hover {
  color: var(--text);
}

.link-btn {
  text-decoration: none;
  display: inline-flex;
  align-items: center;
}

.admin-status-pill {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  font-size: 12.5px;
  color: var(--muted);
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 5px 11px;
}

.admin-main {
  flex: 1 1 auto;
  overflow-y: auto;
  width: 100%;
  max-width: 980px;
  margin: 0 auto;
  padding: 20px 16px 40px;
}

.stats-grid {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 12px;
  margin-bottom: 16px;
}

.stat-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 14px 16px;
}

.stat-label {
  font-size: 12px;
  color: var(--muted);
  margin-bottom: 6px;
}

.stat-value {
  font-size: 22px;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
}

.stat-value-sm {
  font-size: 16px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.filters-bar {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-bottom: 16px;
}

.filter-input {
  flex: 1 1 220px;
  background: var(--surface);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 9px 12px;
  font-size: 14px;
  outline: none;
}

.filter-input:focus,
.filter-select:focus {
  border-color: var(--accent);
}

.filter-select {
  background: var(--surface);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 9px 10px;
  font-size: 13.5px;
  cursor: pointer;
}

.logs-list {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.empty-logs {
  text-align: center;
  color: var(--muted);
  background: var(--surface);
  border: 1px dashed var(--border);
  border-radius: 12px;
  padding: 36px 20px;
  font-size: 14px;
}

.log-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 14px 16px;
  cursor: pointer;
  transition: border-color 0.15s, background 0.15s;
}

.log-card:hover {
  border-color: #384359;
  background: #171c27;
}

.log-card.log-error {
  border-color: rgba(248, 113, 113, 0.38);
}

.log-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  flex-wrap: wrap;
  margin-bottom: 8px;
}

.log-meta-left,
.log-meta-right {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
}

.manager-tag {
  font-size: 12px;
  font-weight: 700;
  color: var(--accent-hover);
  background: var(--accent-soft);
  border: 1px solid rgba(249, 115, 22, 0.3);
  border-radius: 999px;
  padding: 2px 9px;
}

.err-tag {
  font-size: 11px;
  font-weight: 700;
  color: #fecaca;
  background: rgba(239, 68, 68, 0.18);
  border: 1px solid rgba(239, 68, 68, 0.35);
  border-radius: 999px;
  padding: 2px 8px;
}

.subtle-tag {
  font-size: 11.5px;
  color: var(--muted);
  font-family: var(--mono);
}

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

.log-question {
  font-size: 15px;
  font-weight: 600;
  line-height: 1.45;
  margin-bottom: 8px;
}

.log-preview {
  margin-top: 8px;
  font-size: 13px;
  line-height: 1.5;
  color: var(--muted);
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.log-detail {
  margin-top: 12px;
  padding-top: 12px;
  border-top: 1px solid var(--border);
  cursor: default;
}

.detail-block + .detail-block {
  margin-top: 12px;
}

.detail-label {
  font-size: 11.5px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--muted);
  margin-bottom: 5px;
}

.detail-pre,
.detail-md {
  margin: 0;
  padding: 10px 12px;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 9px;
  font-family: var(--font);
  font-size: 13.5px;
  line-height: 1.55;
  word-break: break-word;
  max-height: 420px;
  overflow-y: auto;
}

.detail-pre {
  white-space: pre-wrap;
}

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

.detail-error {
  font-size: 13px;
  color: var(--danger);
}

.load-more-wrap {
  margin-top: 16px;
  text-align: center;
}

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