:root {
  --bg: #f0f2f5;
  --surface: #ffffff;
  --surface-2: #f5f6f6;
  --surface-3: #e9edef;
  --ink: #111b21;
  --ink-dim: #667781;
  --ink-faint: #8696a0;
  --line: #e9edef;
  --accent: #00a884;
  --accent-hover: #008f72;
  --accent-soft: rgba(0, 168, 132, 0.12);
  --accent-text: #ffffff;
  --bubble-in: #ffffff;
  --bubble-in-ink: #111b21;
  --bubble-out: #d9fdd3;
  --bubble-out-ink: #111b21;
  --conv-bg: #efeae2;
  --urgent: #e0602e;
  --urgent-dim: #fde8e0;
  --aging: #b8870a;
  --aging-dim: #f8efd4;
  --handled: #027d69;
  --handled-dim: #d9f5ef;
  --overlay: rgba(17, 27, 33, 0.55);
  --shadow: 0 1px 3px rgba(11, 20, 26, 0.08);
  --shadow-bubble: 0 1px 0.5px rgba(11, 20, 26, 0.13);
  --danger: #e0602e;
  --sans: -apple-system, BlinkMacSystemFont, "Segoe UI", Helvetica, Arial, sans-serif;
  --radius: 8px;
  --radius-lg: 12px;
  color-scheme: light;
}

html[data-theme="dark"] {
  --bg: #111b21;
  --surface: #202c33;
  --surface-2: #2a3942;
  --surface-3: #182229;
  --ink: #e9edef;
  --ink-dim: #8696a0;
  --ink-faint: #667781;
  --line: #2a3942;
  --accent: #00a884;
  --accent-hover: #06cf9c;
  --accent-soft: rgba(0, 168, 132, 0.18);
  --accent-text: #111b21;
  --bubble-in: #202c33;
  --bubble-in-ink: #e9edef;
  --bubble-out: #005c4b;
  --bubble-out-ink: #e9edef;
  --conv-bg: #0b141a;
  --urgent: #ef7b4c;
  --urgent-dim: #3d241c;
  --aging: #d4a017;
  --aging-dim: #3a3016;
  --handled: #06cf9c;
  --handled-dim: #12352e;
  --overlay: rgba(0, 0, 0, 0.72);
  --shadow: 0 1px 3px rgba(0, 0, 0, 0.35);
  --shadow-bubble: 0 1px 0.5px rgba(0, 0, 0, 0.4);
  --danger: #ef7b4c;
  color-scheme: dark;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
}

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

button, input, textarea {
  font-family: inherit;
}

button { cursor: pointer; }
button:disabled { opacity: 0.4; cursor: default; }

:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

.hidden, [hidden] { display: none !important; }

/* ---------- Shared controls ---------- */

.ghost-btn,
.icon-btn,
.dismiss-btn,
.qr-skip {
  background: transparent;
  border: 1px solid var(--line);
  color: var(--ink-dim);
  border-radius: 6px;
  font-size: 13px;
  font-weight: 500;
  padding: 8px 12px;
  min-height: 36px;
  white-space: nowrap;
}

.ghost-btn:hover,
.icon-btn:hover,
.dismiss-btn:hover,
.qr-skip:hover {
  background: var(--surface-2);
  color: var(--ink);
  border-color: var(--ink-faint);
}

.dismiss-btn:hover {
  border-color: var(--handled);
  color: var(--handled);
}

.icon-btn {
  width: 36px;
  min-width: 36px;
  padding: 0;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

.icon-btn svg {
  width: 18px;
  height: 18px;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.8;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.icon-btn .icon-moon {
  fill: currentColor;
  stroke: none;
}

html[data-theme="light"] .icon-sun { display: none; }
html[data-theme="dark"] .icon-moon { display: none; }

.send-btn {
  background: var(--accent);
  color: var(--accent-text);
  border: none;
  font-weight: 600;
  font-size: 14px;
  padding: 10px 18px;
  min-height: 44px;
  border-radius: var(--radius);
  white-space: nowrap;
}

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

.filter-chip {
  font-size: 13px;
  font-weight: 500;
  padding: 6px 12px;
  min-height: 32px;
  border-radius: 100px;
  border: 1px solid var(--line);
  background: transparent;
  color: var(--ink-dim);
  flex-shrink: 0;
}

.filter-chip:hover { background: var(--surface-2); color: var(--ink); }

.filter-chip.active {
  background: var(--accent-soft);
  color: var(--accent);
  border-color: transparent;
  font-weight: 600;
}

input, textarea {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  color: var(--ink);
  font-size: 15px;
  padding: 10px 12px;
}

input:focus, textarea:focus {
  outline: none;
  border-color: var(--accent);
}

::-webkit-scrollbar { width: 8px; height: 8px; }
::-webkit-scrollbar-thumb {
  background: var(--line);
  border-radius: 4px;
}

/* ---------- Login ---------- */

.login-page {
  min-height: 100vh;
  min-height: 100dvh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
  padding-top: max(24px, env(safe-area-inset-top));
  padding-bottom: max(24px, env(safe-area-inset-bottom));
  position: relative;
}

.login-theme {
  position: absolute;
  top: max(16px, env(safe-area-inset-top));
  right: max(16px, env(safe-area-inset-right));
}

.card {
  width: 100%;
  max-width: 380px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius-lg);
  padding: 28px 28px 24px;
  box-shadow: var(--shadow);
}

.card h1 {
  font-size: 22px;
  font-weight: 700;
  margin: 0 0 6px;
  letter-spacing: -0.02em;
}

.card p {
  font-size: 14px;
  color: var(--ink-dim);
  line-height: 1.5;
  margin: 0 0 22px;
}

.card label {
  display: block;
  font-size: 12px;
  font-weight: 600;
  letter-spacing: 0.02em;
  color: var(--ink-dim);
  margin-bottom: 6px;
}

.card input {
  width: 100%;
  margin-bottom: 14px;
}

.card .send-btn { width: 100%; margin-top: 4px; }

.btn-row {
  display: flex;
  gap: 8px;
  margin-top: 8px;
}
.btn-row .send-btn,
.btn-row .ghost-btn {
  flex: 1;
  width: auto;
  margin-top: 0;
}
.card form > .ghost-btn { width: 100%; }

.err {
  font-size: 13px;
  color: var(--danger);
  margin: 0 0 12px;
  display: none;
}

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

.app {
  display: flex;
  height: 100vh;
  height: 100dvh;
  overflow: hidden;
  background: var(--bg);
  padding-left: env(safe-area-inset-left);
  padding-right: env(safe-area-inset-right);
}

.list-pane {
  width: 380px;
  min-width: 320px;
  max-width: 420px;
  flex-shrink: 0;
  border-right: 1px solid var(--line);
  display: flex;
  flex-direction: column;
  background: var(--surface);
}

.list-header {
  padding: 12px 16px 10px;
  padding-top: max(12px, env(safe-area-inset-top));
  border-bottom: 1px solid var(--line);
}

.toolbar {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 10px;
}

.toolbar-identity { min-width: 0; flex: 1; }

.user-name {
  font-size: 17px;
  font-weight: 700;
  letter-spacing: -0.02em;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.session-pill {
  display: inline-block;
  margin-top: 4px;
  font-size: 11px;
  font-weight: 500;
  color: var(--ink-dim);
  background: var(--surface-2);
  border-radius: 100px;
  padding: 3px 8px;
  max-width: 100%;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.header-actions {
  display: flex;
  flex-wrap: wrap;
  justify-content: flex-end;
  gap: 6px;
  flex-shrink: 0;
}

.thread-count {
  font-size: 12px;
  color: var(--ink-faint);
  margin-top: 10px;
}

.search-wrap {
  padding: 8px 12px 4px;
}

.search-input {
  width: 100%;
  background: var(--surface-2);
  border: 1px solid transparent;
  border-radius: 22px;
  color: var(--ink);
  font-size: 15px;
  padding: 9px 14px;
}

.search-input:focus {
  outline: none;
  background: var(--surface);
  border-color: var(--accent);
}

.search-input::placeholder { color: var(--ink-faint); }

.filter-row {
  display: flex;
  gap: 6px;
  padding: 8px 12px;
  border-bottom: 1px solid var(--line);
  overflow-x: auto;
  flex-wrap: nowrap;
  align-items: center;
  -webkit-overflow-scrolling: touch;
}

.filter-row::-webkit-scrollbar { height: 0; }

.row-label {
  font-size: 11px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--ink-faint);
  flex-shrink: 0;
  margin-right: 2px;
}

.threads {
  flex: 1;
  overflow-y: auto;
}

.thread-row {
  display: flex;
  gap: 12px;
  padding: 12px 16px;
  border-bottom: 1px solid var(--line);
  cursor: pointer;
  align-items: center;
  min-height: 72px;
}

.thread-row:hover { background: var(--surface-2); }
.thread-row.selected { background: var(--surface-3); }

.avatar {
  width: 44px;
  height: 44px;
  border-radius: 50%;
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 15px;
  font-weight: 600;
  color: #fff;
  letter-spacing: 0.02em;
}

.thread-body { flex: 1; min-width: 0; }

.thread-top {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  gap: 8px;
}

.thread-name {
  font-size: 16px;
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.thread-wait {
  font-size: 12px;
  color: var(--ink-faint);
  flex-shrink: 0;
}

.thread-preview {
  font-size: 13.5px;
  color: var(--ink-dim);
  margin-top: 2px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.thread-meta {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-top: 4px;
}

.badge {
  display: inline-block;
  font-size: 10.5px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  padding: 2px 7px;
  border-radius: 4px;
}

.badge.urgent { background: var(--urgent-dim); color: var(--urgent); }
.badge.aging { background: var(--aging-dim); color: var(--aging); }
.badge.handled { background: var(--handled-dim); color: var(--handled); }
.badge.low { background: var(--surface-2); color: var(--ink-faint); }

.empty-list {
  padding: 48px 24px;
  text-align: center;
  color: var(--ink-faint);
  font-size: 14px;
  line-height: 1.6;
}

/* ---------- Conversation ---------- */

.conv-pane {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-width: 0;
  background: var(--conv-bg);
}

.conv-header {
  padding: 10px 16px;
  padding-top: max(10px, env(safe-area-inset-top));
  border-bottom: 1px solid var(--line);
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
  background: var(--surface);
  min-height: 64px;
}

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

.conv-header-left > div { min-width: 0; }

.back-btn {
  display: none;
  width: 40px;
  min-width: 40px;
  height: 40px;
  padding: 0;
  border: none;
  background: transparent;
  color: var(--ink);
  border-radius: 50%;
  align-items: center;
  justify-content: center;
}

.back-btn:hover { background: var(--surface-2); }

.back-btn svg {
  width: 22px;
  height: 22px;
  fill: none;
  stroke: currentColor;
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.conv-header h2 {
  font-size: 16px;
  font-weight: 600;
  margin: 0;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.conv-header .num {
  font-size: 12px;
  color: var(--ink-faint);
  margin-top: 1px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.messages {
  flex: 1;
  overflow-y: auto;
  padding: 16px 20px 12px;
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.msg {
  max-width: min(65%, 520px);
  padding: 6px 9px 5px 9px;
  border-radius: 8px;
  font-size: 14.2px;
  line-height: 1.45;
  box-shadow: var(--shadow-bubble);
  word-wrap: break-word;
  overflow-wrap: anywhere;
}

.msg.in {
  background: var(--bubble-in);
  color: var(--bubble-in-ink);
  align-self: flex-start;
  border-top-left-radius: 0;
}

.msg.out {
  background: var(--bubble-out);
  color: var(--bubble-out-ink);
  align-self: flex-end;
  border-top-right-radius: 0;
}

.msg-time {
  font-size: 11px;
  color: var(--ink-faint);
  margin-top: 3px;
  text-align: right;
}

.composer {
  border-top: 1px solid var(--line);
  padding: 10px 16px;
  padding-bottom: max(10px, env(safe-area-inset-bottom));
  display: flex;
  gap: 10px;
  align-items: flex-end;
  background: var(--surface);
}

.composer textarea {
  flex: 1;
  resize: none;
  background: var(--surface-2);
  border: 1px solid transparent;
  border-radius: 22px;
  padding: 10px 16px;
  min-height: 44px;
  max-height: 140px;
  font-size: 15px;
  line-height: 1.4;
}

.composer textarea:focus {
  background: var(--surface);
  border-color: var(--accent);
}

.no-selection {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  color: var(--ink-faint);
  font-size: 15px;
  gap: 8px;
  padding: 24px;
  text-align: center;
}

.no-selection strong {
  color: var(--ink-dim);
  font-size: 18px;
  font-weight: 600;
}

.send-error {
  font-size: 12px;
  color: var(--danger);
  padding: 0 16px 8px;
  background: var(--surface);
}

/* ---------- Overlays ---------- */

.qr-overlay,
.admin-overlay {
  position: fixed;
  inset: 0;
  background: var(--overlay);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 20;
  padding: 24px;
  padding-top: max(24px, env(safe-area-inset-top));
  padding-bottom: max(24px, env(safe-area-inset-bottom));
}

.admin-overlay { z-index: 30; }

.qr-card,
.admin-card {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow);
  width: 100%;
}

.qr-card {
  max-width: 360px;
  padding: 28px 28px 24px;
  text-align: center;
}

.qr-card h2,
.admin-card h2 {
  font-size: 18px;
  font-weight: 700;
  margin: 0 0 8px;
  letter-spacing: -0.02em;
}

.qr-card p {
  font-size: 14px;
  color: var(--ink-dim);
  line-height: 1.5;
  margin: 0 0 20px;
}

.qr-card img {
  width: 240px;
  height: 240px;
  max-width: 100%;
  height: auto;
  aspect-ratio: 1;
  background: #fff;
  border-radius: var(--radius);
  padding: 8px;
}

.qr-wait {
  font-size: 13px;
  color: var(--ink-faint);
  margin-top: 16px;
}

.qr-skip {
  display: block;
  width: 100%;
  margin-top: 16px;
}

.admin-card {
  max-width: 440px;
  max-height: 90vh;
  overflow: auto;
  padding: 22px 24px 24px;
}

.admin-card .hint {
  font-size: 13.5px;
  color: var(--ink-dim);
  line-height: 1.5;
  margin: 0 0 16px;
}

.admin-card label {
  display: block;
  font-size: 12px;
  font-weight: 600;
  color: var(--ink-dim);
  margin-bottom: 5px;
}

.admin-card input {
  width: 100%;
  margin-bottom: 10px;
}

.admin-form-row { display: flex; gap: 8px; }
.admin-form-row > div { flex: 1; min-width: 0; }

.token-box {
  font-size: 12px;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  background: var(--surface-2);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 10px;
  word-break: break-all;
  white-space: pre-wrap;
  margin: 12px 0 14px;
  color: var(--aging);
}

.user-list { margin: 16px 0 0; }

.user-item {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 8px;
  padding: 12px 0;
  border-top: 1px solid var(--line);
  font-size: 14px;
  flex-wrap: wrap;
}

.user-item .meta {
  font-size: 12px;
  color: var(--ink-faint);
  text-transform: capitalize;
}

.user-item .meta.oauth-id {
  text-transform: none;
  word-break: break-all;
  margin-top: 4px;
}

.admin-err {
  font-size: 13px;
  color: var(--danger);
  margin: 0 0 10px;
  display: none;
}

/* ---------- Mobile ---------- */

@media (max-width: 768px) {
  .app { position: relative; }

  .list-pane {
    width: 100%;
    min-width: 0;
    max-width: none;
    border-right: none;
  }

  .conv-pane {
    position: absolute;
    inset: 0;
    display: none;
    z-index: 5;
  }

  .app--open .list-pane { display: none; }
  .app--open .conv-pane { display: flex; }

  .back-btn { display: inline-flex; }

  .ghost-btn, .dismiss-btn, .send-btn, .icon-btn, .filter-chip, .qr-skip {
    min-height: 44px;
  }

  .filter-chip { padding: 8px 14px; }

  .icon-btn {
    width: 44px;
    min-width: 44px;
  }

  input, textarea, .card input, .admin-card input, .composer textarea {
    font-size: 16px;
  }

  .msg { max-width: 82%; }

  .messages { padding: 12px; }

  .admin-form-row { flex-direction: column; gap: 0; }

  .qr-card, .admin-card { max-width: 100%; }

  .header-actions .ghost-btn { padding: 8px 10px; font-size: 13px; }

  .no-selection { display: none; }
}

@media (max-width: 420px) {
  .list-header { padding: 10px 12px 8px; }
  .thread-row { padding: 10px 12px; }
}
