:root {
  --primary: #0b63e5;
  --primary-dark: #0849a6;
  --primary-soft: #eaf2ff;
  --canvas: #edf2f8;
  --surface: #ffffff;
  --surface-subtle: #f7f9fc;
  --text: #172033;
  --muted: #667085;
  --line: #dce3ee;
  --success: #177245;
  --warning: #9a6700;
  --danger: #a12727;
  font-family:
    Inter,
    system-ui,
    -apple-system,
    BlinkMacSystemFont,
    "Segoe UI",
    sans-serif;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  color: var(--text);
  background: var(--canvas);
}

button,
input,
textarea {
  font: inherit;
}

button {
  cursor: pointer;
}

button:focus-visible,
input:focus-visible,
textarea:focus-visible,
.conversation-card:focus-visible,
.messages:focus-visible {
  outline: 3px solid rgba(11, 99, 229, 0.24);
  outline-offset: 2px;
}

button:disabled,
textarea:disabled {
  cursor: not-allowed;
  opacity: 0.55;
}

[hidden] {
  display: none !important;
}

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

/* Login */
.login-page {
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: 24px;
  background:
    radial-gradient(circle at 15% 15%, rgba(11, 99, 229, 0.16), transparent 34%),
    linear-gradient(135deg, #f7faff 0%, #eaf0f8 100%);
}

.login-shell {
  width: min(100%, 440px);
}

.login-card {
  padding: 40px;
  border: 1px solid rgba(220, 227, 238, 0.9);
  border-radius: 18px;
  background: rgba(255, 255, 255, 0.97);
  box-shadow: 0 24px 60px rgba(31, 41, 55, 0.12);
}

.login-mark,
.topbar-mark {
  display: grid;
  place-items: center;
  color: #fff;
  background: var(--primary);
  font-weight: 800;
}

.login-mark {
  width: 44px;
  height: 44px;
  border-radius: 12px;
  font-size: 20px;
}

.login-eyebrow,
.topbar-eyebrow,
.section-eyebrow {
  margin: 0;
  font-size: 11px;
  font-weight: 800;
  letter-spacing: 0.11em;
  text-transform: uppercase;
}

.login-eyebrow {
  margin: 28px 0 8px;
  color: var(--primary-dark);
}

.login-title {
  margin: 0;
  font-size: clamp(30px, 7vw, 40px);
  line-height: 1.1;
}

.login-subtitle {
  margin: 12px 0 28px;
  color: var(--muted);
  line-height: 1.5;
}

.login-error {
  margin: 0 0 20px;
  padding: 12px 14px;
  border: 1px solid #f1b8b8;
  border-radius: 8px;
  color: #8a1c1c;
  background: #fff4f4;
}

.login-form,
.form-field {
  display: grid;
}

.login-form {
  gap: 18px;
}

.form-field {
  gap: 8px;
  color: #344054;
  font-size: 14px;
  font-weight: 700;
}

.form-field input,
#search,
#replyContent {
  width: 100%;
  border: 1px solid var(--line);
  color: var(--text);
  background: var(--surface);
}

.form-field input {
  padding: 12px 14px;
  border-radius: 8px;
}

.form-field input:focus,
#search:focus,
#replyContent:focus {
  border-color: var(--primary);
  outline: 3px solid rgba(11, 99, 229, 0.14);
}

.login-button,
.primary-button,
.secondary-button,
.pagination-button,
.back-button,
.filter-btn {
  min-height: 38px;
  padding: 8px 12px;
  border: 1px solid transparent;
  border-radius: 8px;
  font-weight: 700;
}

.login-button,
.primary-button {
  border-color: var(--primary);
  color: #fff;
  background: var(--primary);
}

.login-button {
  width: 100%;
  padding: 12px 16px;
}

.login-button:hover,
.primary-button:hover:not(:disabled) {
  background: var(--primary-dark);
}

/* Dashboard shell */
.admin-page {
  min-width: 320px;
  min-height: 100vh;
  overflow: hidden;
}

.admin-app {
  display: flex;
  height: 100vh;
  flex-direction: column;
}

.topbar {
  display: flex;
  min-height: 68px;
  flex: 0 0 auto;
  align-items: center;
  justify-content: space-between;
  gap: 20px;
  padding: 10px 20px;
  color: #fff;
  background: #0a397d;
  box-shadow: 0 1px 0 rgba(255, 255, 255, 0.08);
}

.topbar-brand,
.header-actions,
.conversation-title-row,
.conversation-details,
.conversation-actions,
.composer-footer,
.composer-actions,
.panel-heading,
.conversation-card-top,
.conversation-card-footer {
  display: flex;
  align-items: center;
}

.topbar-brand {
  gap: 12px;
}

.topbar-mark {
  width: 38px;
  height: 38px;
  border-radius: 10px;
}

.topbar-eyebrow {
  color: rgba(255, 255, 255, 0.66);
}

.topbar h1 {
  margin: 2px 0 0;
  font-size: 18px;
}

.header-actions {
  gap: 12px;
}

.realtime-connection {
  display: inline-flex;
  align-items: center;
  gap: 8px;
}

.realtime-status {
  color: rgba(255, 255, 255, 0.76);
  font-size: 12px;
}

.realtime-status.state-offline,
.realtime-status.state-error,
.realtime-status.state-rate-limit {
  color: #ffe1e1;
  background: transparent;
}

.realtime-reconnect-button {
  border: 1px solid rgba(255, 255, 255, 0.4);
  border-radius: 7px;
  padding: 5px 8px;
  color: #fff;
  background: rgba(255, 255, 255, 0.1);
  font: inherit;
  font-size: 11px;
  font-weight: 700;
  cursor: pointer;
}

.admin-name {
  color: rgba(255, 255, 255, 0.82);
  font-size: 13px;
}

.logout-form {
  display: inline-flex;
}

.topbar .secondary-button {
  border-color: rgba(255, 255, 255, 0.32);
  color: #fff;
  background: rgba(255, 255, 255, 0.08);
}

.dashboard {
  display: grid;
  min-height: 0;
  flex: 1;
  grid-template-columns: minmax(280px, 338px) minmax(0, 1fr);
  gap: 12px;
  padding: 12px;
}

.conversation-panel,
.chat-panel {
  min-height: 0;
  overflow: hidden;
  border: 1px solid var(--line);
  border-radius: 14px;
  background: var(--surface);
  box-shadow: 0 8px 28px rgba(23, 32, 51, 0.06);
}

.conversation-panel {
  display: flex;
  flex-direction: column;
}

.panel-heading {
  justify-content: space-between;
  gap: 12px;
  padding: 18px 18px 12px;
}

.panel-heading h2,
.conversation-header h2,
.empty-selection h2 {
  margin: 3px 0 0;
}

.panel-heading h2 {
  font-size: 20px;
}

.section-eyebrow {
  color: var(--primary-dark);
}

.count-label {
  min-width: 30px;
  padding: 5px 9px;
  border-radius: 999px;
  color: var(--primary-dark);
  background: var(--primary-soft);
  font-size: 12px;
  font-weight: 800;
  text-align: center;
}

.controls {
  padding: 0 14px 12px;
}

#search {
  min-height: 42px;
  padding: 9px 11px;
  border-radius: 9px;
}

.filters {
  display: flex;
  gap: 6px;
  margin-top: 10px;
  overflow-x: auto;
  padding-bottom: 2px;
}

.filter-btn,
.pagination-button,
.secondary-button,
.back-button {
  border-color: var(--line);
  color: #344054;
  background: var(--surface);
}

.filter-btn {
  min-height: 34px;
  flex: 0 0 auto;
  padding: 6px 9px;
  font-size: 12px;
}

.filter-btn.active {
  border-color: #8db8f5;
  color: var(--primary-dark);
  background: var(--primary-soft);
}

.panel-status,
.chat-status {
  min-height: 28px;
  color: var(--muted);
  font-size: 12px;
}

.panel-status {
  padding: 7px 16px;
  border-top: 1px solid #eef1f5;
  border-bottom: 1px solid #eef1f5;
  background: var(--surface-subtle);
}

.panel-status:empty,
.chat-status:empty {
  min-height: 0;
  padding-top: 0;
  padding-bottom: 0;
  border: 0;
}

.state-offline,
.state-error,
.state-rate-limit {
  color: var(--danger);
  background: #fff5f5;
}

.conversation-list {
  min-height: 0;
  flex: 1;
  overflow-y: auto;
  padding: 10px;
  overscroll-behavior: contain;
}

.conversation-card {
  display: block;
  width: 100%;
  margin: 0 0 8px;
  padding: 12px;
  border: 1px solid #e7ebf1;
  border-radius: 10px;
  color: inherit;
  background: var(--surface);
  text-align: left;
  transition: border-color 120ms ease, background 120ms ease;
}

.conversation-card:hover {
  border-color: #a9c8f6;
  background: #fbfdff;
}

.conversation-card.selected {
  border-color: var(--primary);
  background: #f2f7ff;
  box-shadow: inset 3px 0 0 var(--primary);
}

.conversation-card.unread .conversation-card-name {
  font-weight: 800;
}

.conversation-card-top,
.conversation-card-footer {
  justify-content: space-between;
  gap: 10px;
}

.conversation-card-name {
  min-width: 0;
  overflow: hidden;
  font-size: 14px;
  font-weight: 700;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.conversation-preview {
  margin: 8px 0;
  overflow: hidden;
  color: var(--muted);
  font-size: 13px;
  line-height: 1.35;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.conversation-activity {
  min-width: 0;
  overflow: hidden;
  color: #7a8496;
  font-size: 11px;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.status-pill,
.status-badge {
  flex: 0 0 auto;
  border-radius: 999px;
  font-weight: 800;
  text-transform: capitalize;
}

.status-pill {
  padding: 3px 7px;
  font-size: 10px;
}

.status-badge {
  padding: 5px 9px;
  font-size: 11px;
}

.status-open {
  color: var(--success);
  background: #eaf8f0;
}

.status-waiting {
  color: var(--warning);
  background: #fff7df;
}

.status-resolved {
  color: #5f6878;
  background: #eef1f5;
}

.unread-badge {
  min-width: 24px;
  padding: 3px 7px;
  border-radius: 999px;
  color: #fff;
  background: var(--primary);
  font-size: 11px;
  font-weight: 800;
  text-align: center;
}

.empty-list-state,
.loading-state {
  display: grid;
  min-height: 180px;
  place-items: center;
  padding: 24px;
  color: var(--muted);
  text-align: center;
}

.pagination {
  display: flex;
  min-height: 54px;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 8px 12px;
  border-top: 1px solid var(--line);
  color: var(--muted);
  font-size: 12px;
}

.pagination-button {
  min-height: 34px;
  padding: 6px 9px;
  font-size: 12px;
}

/* Conversation */
.chat-panel {
  display: flex;
  flex-direction: column;
}

.empty-selection {
  display: grid;
  min-height: 100%;
  place-content: center;
  justify-items: center;
  padding: 36px;
  color: var(--muted);
  text-align: center;
}

.empty-selection-mark {
  display: grid;
  width: 48px;
  height: 48px;
  place-items: center;
  border-radius: 14px;
  color: var(--primary);
  background: var(--primary-soft);
  font-size: 24px;
}

.empty-selection h2 {
  color: var(--text);
}

.empty-selection p {
  max-width: 390px;
  margin: 9px 0 0;
  line-height: 1.5;
}

.chat-workspace {
  display: flex;
  min-height: 0;
  flex: 1;
  flex-direction: column;
}

.conversation-header {
  display: grid;
  flex: 0 0 auto;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 16px;
  align-items: center;
  padding: 16px 18px;
  border-bottom: 1px solid var(--line);
}

.back-button {
  display: none;
}

.conversation-title-row {
  min-width: 0;
  gap: 10px;
}

.conversation-header h2 {
  min-width: 0;
  overflow: hidden;
  font-size: 19px;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.conversation-details {
  gap: 7px;
  margin: 6px 0 0;
  color: var(--muted);
  font-size: 12px;
}

.conversation-actions {
  gap: 8px;
}

.chat-status {
  padding: 7px 18px;
  border-bottom: 1px solid #edf0f5;
  background: var(--surface-subtle);
}

.messages {
  display: flex;
  min-height: 0;
  flex: 1;
  flex-direction: column;
  gap: 12px;
  overflow-y: auto;
  padding: 20px;
  background:
    radial-gradient(circle at 100% 0%, rgba(11, 99, 229, 0.06), transparent 28%),
    var(--surface-subtle);
  overscroll-behavior: contain;
}

.msg-row {
  display: flex;
  max-width: min(76%, 720px);
  flex-direction: column;
  gap: 5px;
}

.msg-row.customer,
.msg-row.system {
  align-self: flex-start;
  align-items: flex-start;
}

.msg-row.admin {
  align-self: flex-end;
  align-items: flex-end;
}

.bubble {
  padding: 10px 13px;
  border-radius: 14px;
  overflow-wrap: anywhere;
  white-space: pre-wrap;
  line-height: 1.45;
}

.bubble.customer,
.bubble.system {
  border: 1px solid var(--line);
  color: var(--text);
  background: var(--surface);
  border-bottom-left-radius: 4px;
}

.bubble.admin {
  border: 1px solid var(--primary);
  color: #fff;
  background: var(--primary);
  border-bottom-right-radius: 4px;
}

.message-body {
  display: block;
}

/* Reserved for authenticated image-message rendering in a future phase. */
.message-attachment {
  display: block;
  max-width: 100%;
  margin-top: 8px;
  border-radius: 10px;
}

.message-meta {
  padding: 0 4px;
  color: var(--muted);
  font-size: 11px;
}

.message-empty {
  margin: auto;
  color: var(--muted);
  text-align: center;
}

.composer {
  position: sticky;
  bottom: 0;
  display: grid;
  flex: 0 0 auto;
  gap: 9px;
  padding: 12px 16px;
  border-top: 1px solid var(--line);
  background: var(--surface);
  box-shadow: 0 -8px 24px rgba(23, 32, 51, 0.04);
}

#replyContent {
  min-height: 76px;
  max-height: 180px;
  resize: vertical;
  padding: 11px 12px;
  border-radius: 9px;
  line-height: 1.45;
}

.composer-footer {
  justify-content: space-between;
  gap: 12px;
}

.character-count {
  color: var(--muted);
  font-size: 11px;
}

.composer-actions {
  justify-content: flex-end;
  gap: 8px;
}

@media (max-width: 800px) {
  .admin-page {
    overflow: auto;
  }

  .admin-app {
    min-height: 100vh;
    height: auto;
  }

  .topbar {
    min-height: 62px;
    padding: 9px 14px;
  }

  .topbar-mark,
  .admin-name,
  .realtime-status {
    display: none;
  }

  .dashboard {
    display: block;
    min-height: calc(100vh - 62px);
    padding: 0;
  }

  .conversation-panel,
  .chat-panel {
    min-height: calc(100vh - 62px);
    border: 0;
    border-radius: 0;
    box-shadow: none;
  }

  .chat-panel {
    display: none;
  }

  .dashboard.mobile-chat-open .conversation-panel {
    display: none;
  }

  .dashboard.mobile-chat-open .chat-panel {
    display: flex;
  }

  .back-button {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    grid-column: 1 / -1;
    justify-self: start;
  }

  .conversation-header {
    grid-template-columns: minmax(0, 1fr) auto;
    gap: 10px;
    padding: 12px 14px;
  }

  .messages {
    min-height: 52vh;
    padding: 16px 12px;
  }

  .msg-row {
    max-width: 88%;
  }

  .composer {
    padding: 10px 12px;
  }
}

@media (max-width: 480px) {
  .login-page {
    padding: 16px;
  }

  .login-card {
    padding: 28px 22px;
  }

  .topbar-eyebrow {
    display: none;
  }

  .filters {
    margin-right: -4px;
  }

  .conversation-title-row {
    align-items: flex-start;
    flex-direction: column;
    gap: 6px;
  }

  .conversation-details {
    align-items: flex-start;
    flex-direction: column;
    gap: 2px;
  }

  .conversation-details [aria-hidden="true"] {
    display: none;
  }
}
