:root {
  --bg: #f4f6f9;
  --surface: #ffffff;
  --text: #17202b;
  --muted: #5d6b7c;
  --border: #dde3ea;
  --primary: #2563eb;
  --primary-hover: #1d4ed8;
  --danger: #dc2626;
  --mine: #2563eb;
  --theirs: #e8edf3;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f141a;
    --surface: #171d25;
    --text: #e8edf3;
    --muted: #9aa8b8;
    --border: #2a3441;
    --theirs: #232c37;
  }
}

* { box-sizing: border-box; }

html, body { height: 100%; }

body {
  margin: 0;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.45;
}

a { color: var(--primary); }
.muted { color: var(--muted); }
.small { font-size: 0.85rem; }
.pad { padding: 1rem; }
.error { color: var(--danger); font-size: 0.9rem; margin: 0.25rem 0 0; }

/* Buttons */
.btn {
  border: 0;
  border-radius: 10px;
  padding: 0.65rem 1.1rem;
  font: inherit;
  font-weight: 600;
  cursor: pointer;
  color: #fff;
}
.btn:disabled { opacity: 0.5; cursor: not-allowed; }
.btn-primary { background: var(--primary); }
.btn-primary:hover:not(:disabled) { background: var(--primary-hover); }
.btn-danger { background: var(--danger); }

/* Auth pages */
.auth-page { display: grid; place-items: center; padding: 1rem; }
.auth-card {
  width: 100%;
  max-width: 380px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 2rem;
}
.auth-card h1 { margin: 0 0 0.25rem; font-size: 1.5rem; }
.auth-card form { display: grid; gap: 0.35rem; margin: 1.25rem 0; }
.auth-card label { font-weight: 600; font-size: 0.9rem; margin-top: 0.5rem; }
.auth-card input[type="text"],
.auth-card input[type="password"],
.auth-card input[type="email"] {
  width: 100%;
  padding: 0.65rem 0.8rem;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--bg);
  color: var(--text);
  font: inherit;
}
.auth-card .btn { margin-top: 1rem; }

/* Chat layout */
.chat-page { display: flex; flex-direction: column; height: 100vh; height: 100dvh; }

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  padding: 0.75rem 1.25rem;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}
.topbar strong { display: block; font-size: 1.1rem; }
.topbar form { margin: 0; }

.thread {
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
  width: 100%;
  max-width: 860px;
  margin: 0 auto;
}

.messages {
  flex: 1;
  overflow-y: auto;
  padding: 1.25rem;
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
}

.empty { margin: auto; text-align: center; color: var(--muted); }
.empty h2 { color: var(--text); margin: 0 0 0.25rem; }

.msg { display: flex; flex-direction: column; max-width: 75%; }
.msg-mine { align-self: flex-end; align-items: flex-end; }
.msg-theirs { align-self: flex-start; align-items: flex-start; }

.bubble {
  padding: 0.6rem 0.9rem;
  border-radius: 16px;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}
.msg-mine .bubble { background: var(--mine); color: #fff; border-bottom-right-radius: 4px; }
.msg-theirs .bubble { background: var(--theirs); border-bottom-left-radius: 4px; }

.meta { font-size: 0.75rem; color: var(--muted); margin-top: 0.2rem; }

.status { margin: 0; padding: 0.4rem 1.25rem; font-size: 0.85rem; color: var(--muted); }

.composer {
  display: flex;
  gap: 0.6rem;
  padding: 0.9rem 1.25rem 1.1rem;
  border-top: 1px solid var(--border);
  background: var(--surface);
}
.composer #message-input {
  flex: 1;
  padding: 0.7rem 0.9rem;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--bg);
  color: var(--text);
  font: inherit;
}

/* Agent inbox */
.inbox-layout { flex: 1; min-height: 0; display: grid; grid-template-columns: 280px 1fr; }
.conversation-list {
  overflow-y: auto;
  background: var(--surface);
  border-right: 1px solid var(--border);
}
.conversation-item .name { font-weight: 600; }
.inbox-thread { display: flex; flex-direction: column; min-height: 0; }
.thread-title { margin: 0; padding: 0.75rem 1.25rem; border-bottom: 1px solid var(--border); font-size: 1.05rem; }
.inbox-thread .thread { max-width: none; margin: 0; flex: 1; }

@media (max-width: 720px) {
  .inbox-layout { grid-template-columns: 1fr; grid-template-rows: auto 1fr; }
  .conversation-list { max-height: 30vh; border-right: 0; border-bottom: 1px solid var(--border); }
  .msg { max-width: 88%; }
}


/* Attachments, drag and drop */
.thread { position: relative; }

.btn-icon {
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text);
  border-radius: 12px;
  width: 2.8rem;
  font-size: 1.15rem;
  cursor: pointer;
}
.btn-icon:hover { border-color: var(--primary); }

.hint { margin: 0; padding: 0 1.25rem 0.75rem; font-size: 0.78rem; color: var(--muted); background: var(--surface); }

.drop-overlay {
  position: absolute;
  inset: 0;
  z-index: 10;
  display: grid;
  place-items: center;
  background: rgba(37, 99, 235, 0.14);
  border: 3px dashed var(--primary);
  border-radius: 12px;
  pointer-events: none;
}
.drop-overlay[hidden] { display: none; }
.drop-overlay div {
  background: var(--surface);
  color: var(--text);
  padding: 0.9rem 1.5rem;
  border-radius: 12px;
  font-weight: 600;
  border: 1px solid var(--border);
}

.attachment { padding: 0.35rem; max-width: 100%; }
.msg-mine .attachment { background: var(--mine); color: #fff; }
.msg-theirs .attachment { background: var(--theirs); }
.attachment img, .attachment video {
  display: block;
  max-width: min(320px, 100%);
  max-height: 320px;
  border-radius: 12px;
}
.attachment audio { display: block; max-width: 100%; }
.attachment-caption { font-size: 0.75rem; opacity: 0.8; padding: 0.3rem 0.4rem 0.1rem; overflow-wrap: anywhere; }
.file-link {
  display: flex;
  align-items: center;
  gap: 0.6rem;
  padding: 0.4rem 0.6rem;
  color: inherit;
  text-decoration: none;
}
.file-link:hover .file-name { text-decoration: underline; }
.file-icon { font-size: 1.4rem; }
.file-name { font-weight: 600; overflow-wrap: anywhere; }
.file-size { font-size: 0.78rem; opacity: 0.8; white-space: nowrap; }

/* Inbox: tabs, pins, ticket controls */
.tabs { display: flex; border-bottom: 1px solid var(--border); }
.tab {
  flex: 1;
  text-align: center;
  padding: 0.7rem;
  text-decoration: none;
  color: var(--muted);
  font-weight: 600;
}
.tab.active { color: var(--primary); box-shadow: inset 0 -3px 0 var(--primary); }

.conversation-item {
  display: flex;
  align-items: stretch;
  border-bottom: 1px solid var(--border);
}
.conversation-item .conversation-link {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  padding: 0.8rem 1rem;
  text-decoration: none;
  color: inherit;
}
.conversation-item { border-bottom: 1px solid var(--border); }
.conversation-item:hover, .conversation-item.active { background: var(--bg); }
.conversation-item.active { box-shadow: inset 3px 0 0 var(--primary); }
.pin-form { margin: 0; display: flex; }
.pin-btn {
  border: 0;
  background: transparent;
  cursor: pointer;
  padding: 0 0.9rem;
  font-size: 1rem;
  opacity: 0.25;
  filter: grayscale(1);
}
.pin-btn:hover { opacity: 0.7; }
.pin-btn.on { opacity: 1; filter: none; }
.pin-mark { margin-right: 0.25rem; }

.badge {
  display: inline-block;
  font-size: 0.7rem;
  font-weight: 600;
  padding: 0.1rem 0.5rem;
  border-radius: 999px;
  background: var(--theirs);
  color: var(--muted);
  vertical-align: middle;
  margin-left: 0.3rem;
}
.badge-danger { background: rgba(220, 38, 38, 0.15); color: var(--danger); }

.thread-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 0.5rem;
  padding: 0.6rem 1.25rem;
  border-bottom: 1px solid var(--border);
}
.thread-header .thread-title { padding: 0; border: 0; }
.thread-actions { display: flex; flex-wrap: wrap; gap: 0.5rem; align-items: center; }
.thread-actions form { margin: 0; display: flex; gap: 0.4rem; align-items: center; }
.btn-secondary { background: var(--theirs); color: var(--text); padding: 0.45rem 0.85rem; }
.btn-secondary:hover { filter: brightness(0.95); }
.thread-actions .btn-danger { padding: 0.45rem 0.85rem; }
.reason-input {
  padding: 0.45rem 0.6rem;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--bg);
  color: var(--text);
  font: inherit;
  font-size: 0.85rem;
  width: 11rem;
}
.ban-reason { margin: 0; padding: 0.4rem 1.25rem; background: rgba(220, 38, 38, 0.08); color: var(--danger); }

.flash { list-style: none; margin: 0; padding: 0.6rem 1.25rem; background: var(--surface); border-bottom: 1px solid var(--border); }
.flash li { font-size: 0.9rem; }
.flash-success { color: #15803d; }
.flash-error { color: var(--danger); }

/* Online / offline presence */
.presence {
  display: inline-flex;
  align-items: center;
  gap: 0.4rem;
  margin-left: 0.6rem;
  font-size: 0.85rem;
  color: var(--muted);
}
.presence-dot { width: 0.6rem; height: 0.6rem; border-radius: 50%; background: #9aa8b8; }
.presence.online { color: #15803d; }
.presence.online .presence-dot { background: #22c55e; box-shadow: 0 0 0 3px rgba(34, 197, 94, 0.2); }
@media (prefers-color-scheme: dark) { .presence.online { color: #4ade80; } }

/* Top bar layout */
.topbar-left { display: flex; align-items: center; gap: 1.5rem; flex-wrap: wrap; }
.topbar-right { display: flex; align-items: center; gap: 0.6rem; }
.topbar-right form { margin: 0; }

.btn-icon-sm {
  border: 1px solid var(--border);
  background: var(--bg);
  border-radius: 10px;
  width: 2.3rem;
  height: 2.3rem;
  font-size: 1.05rem;
  cursor: pointer;
}
.btn-icon-sm:hover { border-color: var(--primary); }

/* Agent availability switch */
.availability { display: flex; align-items: center; gap: 0.75rem; flex-wrap: wrap; }
.switch { display: inline-flex; align-items: center; gap: 0.5rem; cursor: pointer; user-select: none; }
.switch input { position: absolute; opacity: 0; width: 0; height: 0; }
.switch-track {
  position: relative;
  width: 2.6rem;
  height: 1.45rem;
  border-radius: 999px;
  background: #9aa8b8;
  transition: background 0.15s;
}
.switch-thumb {
  position: absolute;
  top: 0.15rem;
  left: 0.15rem;
  width: 1.15rem;
  height: 1.15rem;
  border-radius: 50%;
  background: #fff;
  transition: transform 0.15s;
}
.switch input:checked + .switch-track { background: #22c55e; }
.switch input:checked + .switch-track .switch-thumb { transform: translateX(1.15rem); }
.switch input:focus-visible + .switch-track { outline: 2px solid var(--primary); outline-offset: 2px; }
.switch input:disabled + .switch-track { opacity: 0.6; }
.availability-label { font-weight: 600; min-width: 3.4rem; }

/* Unread tickets and new-ticket banner */
.unread-dot {
  display: none;
  width: 0.55rem;
  height: 0.55rem;
  border-radius: 50%;
  background: var(--primary);
  margin-right: 0.4rem;
  vertical-align: middle;
}
.conversation-item.unread .unread-dot { display: inline-block; }
.conversation-item.unread .name { font-weight: 800; }

.new-banner { padding: 0.55rem 1.25rem; background: rgba(37, 99, 235, 0.12); border-bottom: 1px solid var(--border); }
.new-banner[hidden] { display: none; }
.new-banner a { font-weight: 600; text-decoration: none; }
