:root {
  --bg-start: #eef2f8;
  --bg-end: #dde4ef;
  --panel: #ffffff;
  --border: #d8dce4;
  --border-soft: #eceff4;
  --bdc-bubble: #0a84ff;
  --bdc-bubble-2: #0070e0;
  --bdc-text: #ffffff;
  --customer-bubble: #eef0f3;
  --customer-text: #1c1c1e;
  --muted: #6e6e73;
  --muted-2: #8e8e93;
  --danger: #ff3b30;
  --success: #30b46b;
  --header-h: 60px;
  --shadow-soft: 0 1px 2px rgba(16, 24, 40, 0.04), 0 1px 3px rgba(16, 24, 40, 0.06);
  --shadow-pop: 0 10px 30px rgba(16, 24, 40, 0.08), 0 4px 10px rgba(16, 24, 40, 0.05);
}

* { box-sizing: border-box; }

html, body {
  height: 100%;
}

body {
  margin: 0;
  padding: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen, Ubuntu, "Helvetica Neue", sans-serif;
  background: linear-gradient(180deg, var(--bg-start), var(--bg-end));
  color: #1c1c1e;
  overflow: hidden;
  -webkit-font-smoothing: antialiased;
}

/* ================= Chat shell (main app) ================= */

.chat-shell {
  max-width: 680px;
  margin: 0 auto;
  height: 100vh;
  display: flex;
  flex-direction: column;
  background: var(--panel);
  box-shadow: var(--shadow-pop);
}

@media (min-width: 720px) {
  .chat-shell {
    height: calc(100vh - 40px);
    margin-top: 20px;
    margin-bottom: 20px;
    border-radius: 18px;
    overflow: hidden;
  }
  body { padding: 0; }
}

/* ================= Header (sticky via flex) ================= */

.chat-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 16px;
  background: rgba(255, 255, 255, 0.85);
  backdrop-filter: saturate(180%) blur(12px);
  -webkit-backdrop-filter: saturate(180%) blur(12px);
  border-bottom: 1px solid var(--border-soft);
  min-height: var(--header-h);
  gap: 12px;
  flex-wrap: wrap;
  flex-shrink: 0;
  z-index: 2;
}

.chat-title {
  display: flex;
  flex-direction: column;
  line-height: 1.2;
}

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

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

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

.chat-controls select,
.chat-controls .btn-ghost {
  font-size: 13px;
  padding: 7px 12px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: #fff;
  color: #1c1c1e;
  cursor: pointer;
  font-family: inherit;
  text-decoration: none;
  transition: background 0.15s ease, border-color 0.15s ease, transform 0.1s ease;
}

.chat-controls .btn-ghost:hover {
  background: #f3f5f8;
}
.chat-controls .btn-ghost:active { transform: translateY(1px); }

.btn-logout {
  color: #1c1c1e;
  display: inline-flex;
  align-items: center;
}

.user-badge {
  font-size: 12px;
  color: var(--muted);
  padding: 6px 10px;
  background: #f3f5f8;
  border-radius: 10px;
  max-width: 200px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

/* ================= System prompt drawer (offcanvas, right) ================= */

.prompt-drawer {
  position: fixed;
  top: 0;
  right: 0;
  height: 100vh;
  width: min(480px, 92vw);
  background: #fff;
  box-shadow: -14px 0 40px rgba(16, 24, 40, 0.18);
  transform: translateX(100%);
  visibility: hidden;
  transition: transform 0.28s ease-out, visibility 0s linear 0.28s;
  z-index: 100;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.prompt-drawer.open {
  transform: translateX(0);
  visibility: visible;
  transition: transform 0.28s ease-out, visibility 0s linear 0s;
}

.prompt-drawer-header {
  flex-shrink: 0;
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  padding: 14px 18px;
  border-bottom: 1px solid #eceff4;
  background: #fff9e6;
  gap: 12px;
}

.prompt-drawer-header strong {
  font-size: 14px;
  font-weight: 700;
  display: block;
}

.prompt-drawer-sub {
  font-size: 12px;
  color: var(--muted);
  margin-top: 2px;
  font-variant-numeric: tabular-nums;
}

.prompt-drawer pre {
  flex: 1;
  overflow-y: auto;
  margin: 0;
  padding: 14px 18px;
  white-space: pre-wrap;
  word-break: break-word;
  font-family: "SF Mono", Menlo, Consolas, monospace;
  font-size: 12px;
  line-height: 1.5;
  color: #3a3a3c;
  background: transparent;
}

.prompt-drawer pre::-webkit-scrollbar { width: 8px; }
.prompt-drawer pre::-webkit-scrollbar-thumb {
  background: rgba(0,0,0,0.16);
  border-radius: 8px;
}

.btn-close {
  background: transparent;
  border: none;
  font-size: 28px;
  line-height: 1;
  color: var(--muted);
  cursor: pointer;
  padding: 0 6px;
  border-radius: 8px;
  font-family: inherit;
}
.btn-close:hover { color: #1c1c1e; background: #f3f5f8; }

.prompt-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(16, 24, 40, 0.35);
  z-index: 99;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.28s ease-out;
  backdrop-filter: blur(2px);
  -webkit-backdrop-filter: blur(2px);
}

.prompt-backdrop.open {
  opacity: 1;
  pointer-events: auto;
}

.system-prompt-meta {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 8px;
  font-size: 12px;
}

.token-stats {
  color: var(--muted);
  font-variant-numeric: tabular-nums;
}

.system-prompt-panel pre {
  margin: 0;
  white-space: pre-wrap;
  word-break: break-word;
  font-family: "SF Mono", Menlo, Consolas, monospace;
  font-size: 12px;
  color: #3a3a3c;
  background: transparent;
}

/* ================= Thread (the only scrollable area) ================= */

.chat-thread {
  flex: 1;
  overflow-y: auto;
  padding: 16px 14px 8px;
  display: flex;
  flex-direction: column;
  gap: 2px;
  background: linear-gradient(180deg, #fcfcfd, #f6f8fb);
  scroll-behavior: smooth;
  min-height: 0;
}

.chat-thread::-webkit-scrollbar { width: 10px; }
.chat-thread::-webkit-scrollbar-track { background: transparent; }
.chat-thread::-webkit-scrollbar-thumb {
  background: rgba(0,0,0,0.14);
  border-radius: 10px;
  border: 2px solid transparent;
  background-clip: content-box;
}
.chat-thread::-webkit-scrollbar-thumb:hover { background: rgba(0,0,0,0.22); background-clip: content-box; }

/* ================= Bubbles ================= */

.bubble-row {
  display: flex;
  margin-top: 2px;
  animation: bubble-in 0.28s cubic-bezier(0.22, 0.72, 0.32, 1);
}

@keyframes bubble-in {
  from { opacity: 0; transform: translateY(6px) scale(0.98); }
  to   { opacity: 1; transform: translateY(0) scale(1); }
}

.bubble-row.customer { justify-content: flex-start; }
.bubble-row.bdc      { justify-content: flex-end; }
.bubble-row.automated { justify-content: center; margin: 14px 0 6px; }

.bubble {
  max-width: 72%;
  padding: 9px 14px;
  border-radius: 20px;
  font-size: 15.5px;
  line-height: 1.4;
  word-wrap: break-word;
  white-space: pre-wrap;
  box-shadow: var(--shadow-soft);
}

.bubble-row.customer .bubble {
  background: var(--customer-bubble);
  color: var(--customer-text);
  border-bottom-left-radius: 6px;
}

.bubble-row.bdc .bubble {
  background: linear-gradient(180deg, var(--bdc-bubble), var(--bdc-bubble-2));
  color: var(--bdc-text);
  border-bottom-right-radius: 6px;
}

.bubble-row.automated .bubble {
  background: #fff8e1;
  color: #5c4f1a;
  border: 1px dashed #e5ce81;
  box-shadow: none;
  max-width: 88%;
  font-size: 13.5px;
  line-height: 1.45;
}

.bubble-label {
  font-size: 10px;
  font-weight: 800;
  letter-spacing: 0.1em;
  text-transform: uppercase;
  color: #9e8a3d;
  margin-bottom: 4px;
}

.bubble.typing {
  background: var(--customer-bubble);
  padding: 11px 16px;
  min-width: 52px;
  box-shadow: var(--shadow-soft);
}

.typing-dots {
  display: inline-flex;
  gap: 4px;
  align-items: center;
  height: 10px;
}

.typing-dots span {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: #8e8e93;
  opacity: 0.4;
  animation: typing-pulse 1.2s infinite ease-in-out;
}

.typing-dots span:nth-child(2) { animation-delay: 0.15s; }
.typing-dots span:nth-child(3) { animation-delay: 0.30s; }

@keyframes typing-pulse {
  0%, 60%, 100% { opacity: 0.35; transform: translateY(0); }
  30%           { opacity: 1;    transform: translateY(-2px); }
}

.bubble-meta {
  font-size: 11px;
  color: var(--muted-2);
  padding: 2px 12px 4px;
  display: block;
}
.bubble-meta.customer  { text-align: left; }
.bubble-meta.bdc       { text-align: right; }
.bubble-meta.automated { text-align: center; font-style: italic; }

/* Attachment bubbles */
.bubble-attachment { padding: 10px !important; }

.attachment-card {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 2px 4px;
  min-width: 200px;
}

.attachment-icon {
  font-size: 26px;
  width: 44px;
  height: 44px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: #fff;
  border-radius: 10px;
  border: 1px solid rgba(0, 0, 0, 0.08);
  flex-shrink: 0;
}

.bubble-row.bdc .attachment-icon {
  border-color: rgba(255, 255, 255, 0.3);
}

.attachment-meta {
  display: flex;
  flex-direction: column;
  line-height: 1.25;
}

.attachment-name { font-weight: 600; font-size: 14px; }
.attachment-label { font-size: 11.5px; opacity: 0.85; }

/* ================= Composer (sticky via flex) ================= */

.composer-wrap {
  border-top: 1px solid var(--border-soft);
  background: rgba(255, 255, 255, 0.92);
  backdrop-filter: saturate(180%) blur(12px);
  -webkit-backdrop-filter: saturate(180%) blur(12px);
  flex-shrink: 0;
  z-index: 2;
}

.composer-meta {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 10px 16px 2px;
  font-size: 12px;
  color: var(--muted);
}

.sms-label {
  display: inline-flex;
  align-items: center;
  gap: 8px;
}

.sms-tag {
  display: inline-block;
  background: var(--success);
  color: #fff;
  font-size: 10px;
  font-weight: 800;
  padding: 3px 7px;
  border-radius: 5px;
  letter-spacing: 0.06em;
}

.attach-bar {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  flex-wrap: wrap;
}

.btn-attach {
  font-size: 12px;
  padding: 5px 11px;
  border-radius: 16px;
  border: 1px solid var(--border);
  background: #fff;
  color: #1c1c1e;
  cursor: pointer;
  font-family: inherit;
  transition: background 0.15s, border-color 0.15s;
}

.btn-attach:hover:not(:disabled) { background: #f3f5f8; border-color: #c7ccd4; }
.btn-attach:disabled { opacity: 0.5; cursor: not-allowed; background: #f2f2f7; }

.attach-hint {
  font-size: 11px;
  color: var(--muted);
  font-style: italic;
  margin-left: 2px;
}
.attach-hint.unlocked {
  color: var(--success);
  font-style: normal;
  font-weight: 600;
}

.chat-composer {
  display: flex;
  gap: 8px;
  padding: 10px 14px 12px;
}

.chat-composer input {
  flex: 1;
  padding: 11px 16px;
  border: 1px solid var(--border);
  border-radius: 22px;
  font-size: 15.5px;
  outline: none;
  background: #fff;
  font-family: inherit;
  transition: border-color 0.15s, box-shadow 0.15s;
}

.chat-composer input:focus {
  border-color: var(--bdc-bubble);
  box-shadow: 0 0 0 4px rgba(10, 132, 255, 0.12);
}

.chat-composer input:disabled {
  background: #f5f6f8;
  color: var(--muted);
}

.chat-composer button {
  padding: 9px 18px;
  background: linear-gradient(180deg, var(--bdc-bubble), var(--bdc-bubble-2));
  color: #fff;
  border: none;
  border-radius: 22px;
  font-weight: 600;
  font-size: 14px;
  cursor: pointer;
  font-family: inherit;
  box-shadow: 0 1px 2px rgba(10, 132, 255, 0.25);
  transition: transform 0.1s;
}

.chat-composer button:active:not(:disabled) { transform: translateY(1px); }
.chat-composer button:disabled {
  background: #a9c9ff;
  cursor: not-allowed;
  box-shadow: none;
}

.error-banner {
  background: var(--danger);
  color: #fff;
  padding: 8px 14px;
  font-size: 13px;
  text-align: center;
}

/* ================= Login page ================= */

.login-body {
  background: linear-gradient(180deg, var(--bg-start), var(--bg-end));
  min-height: 100vh;
  overflow: auto;
}

.login-wrap {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
}

.login-card {
  width: 100%;
  max-width: 400px;
  background: #fff;
  padding: 30px 28px 24px;
  border-radius: 18px;
  box-shadow: var(--shadow-pop);
  display: flex;
  flex-direction: column;
  gap: 14px;
}

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

.login-logo {
  height: 44px;
  padding: 0 14px;
  border-radius: 12px;
  background: linear-gradient(135deg, var(--bdc-bubble), #5b2ee8);
  color: #fff;
  font-weight: 800;
  font-size: 13px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  letter-spacing: 0.04em;
  white-space: nowrap;
  box-shadow: 0 4px 14px rgba(91, 46, 232, 0.3);
}

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

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

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

.login-field span {
  font-size: 12px;
  font-weight: 600;
  color: var(--muted);
  letter-spacing: 0.01em;
}

.login-field input {
  padding: 11px 14px;
  font-size: 15px;
  border-radius: 12px;
  border: 1px solid var(--border);
  font-family: inherit;
  outline: none;
  transition: border-color 0.15s, box-shadow 0.15s;
  background: #fff;
}

.login-field input:focus {
  border-color: var(--bdc-bubble);
  box-shadow: 0 0 0 4px rgba(10, 132, 255, 0.14);
}

.login-submit {
  margin-top: 6px;
  padding: 12px;
  background: linear-gradient(180deg, var(--bdc-bubble), var(--bdc-bubble-2));
  color: #fff;
  border: none;
  border-radius: 12px;
  font-weight: 700;
  font-size: 15px;
  font-family: inherit;
  cursor: pointer;
  box-shadow: 0 2px 8px rgba(10, 132, 255, 0.25);
  transition: transform 0.1s, box-shadow 0.15s;
}

.login-submit:hover { box-shadow: 0 4px 14px rgba(10, 132, 255, 0.3); }
.login-submit:active { transform: translateY(1px); }

.login-error {
  background: #ffecea;
  color: #b02020;
  border: 1px solid #ffc8c3;
  padding: 9px 12px;
  border-radius: 10px;
  font-size: 13px;
}

.login-hint {
  font-size: 11.5px;
  color: var(--muted);
  text-align: center;
  margin-top: 6px;
  line-height: 1.4;
}

.login-hint code {
  background: #f3f5f8;
  padding: 1px 6px;
  border-radius: 4px;
  font-size: 11.5px;
  color: #1c1c1e;
}
