:root {
  --ad-color-bg: #fafbfc;
  --ad-color-surface: #ffffff;
  --ad-color-border: #e2e5ea;
  --ad-color-text: #1b1f24;
  --ad-color-text-muted: #5b6470;
  --ad-color-primary: #2f6feb;
  --ad-color-primary-hover: #2558c4;
  --ad-radius: 10px;
  --ad-font: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  font-family: var(--ad-font);
  background: var(--ad-color-bg);
  color: var(--ad-color-text);
}

button, input, select {
  font-family: inherit;
  font-size: 14px;
}

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

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

.ad-login-card {
  width: 100%;
  max-width: 360px;
  background: var(--ad-color-surface);
  border: 1px solid var(--ad-color-border);
  border-radius: var(--ad-radius);
  padding: 32px;
}

.ad-login-card h1 {
  font-size: 20px;
  margin: 0 0 6px;
}

.ad-login-card p {
  color: var(--ad-color-text-muted);
  margin: 0 0 24px;
  font-size: 14px;
}

.ad-field {
  margin-bottom: 16px;
}

.ad-field label {
  display: block;
  font-size: 13px;
  font-weight: 500;
  margin-bottom: 6px;
}

.ad-field input, .ad-field select {
  width: 100%;
  padding: 9px 11px;
  border: 1px solid var(--ad-color-border);
  border-radius: 8px;
  background: #fff;
  color: var(--ad-color-text);
}

.ad-field input:focus-visible, .ad-field select:focus-visible {
  outline: 2px solid var(--ad-color-primary);
  outline-offset: 1px;
}

.ad-btn-primary {
  width: 100%;
  padding: 10px 14px;
  border: none;
  border-radius: 8px;
  background: var(--ad-color-primary);
  color: #fff;
  font-weight: 600;
  cursor: pointer;
}

.ad-btn-primary:hover {
  background: var(--ad-color-primary-hover);
}

.ad-btn-primary:disabled {
  opacity: 0.6;
  cursor: not-allowed;
}

.ad-error-text {
  color: #c0392b;
  font-size: 13px;
  margin-top: 10px;
  min-height: 16px;
}

.ad-login-card[hidden] {
  display: none;
}

.ad-forgot-row {
  display: flex;
  justify-content: flex-end;
  margin: -6px 0 16px;
}

.ad-link-btn {
  background: none;
  border: none;
  padding: 0;
  color: var(--ad-color-primary);
  font-size: 13px;
  cursor: pointer;
}

.ad-link-btn:hover {
  color: var(--ad-color-primary-hover);
  text-decoration: underline;
}

.ad-link-btn:disabled {
  color: var(--ad-color-text-muted);
  cursor: not-allowed;
  text-decoration: none;
}

.ad-recovery-footer {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  margin-top: 4px;
}

.ad-recovery-footer .ad-link-btn:only-child {
  margin: 0 auto;
}

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

.ad-app[hidden], .ad-login-screen[hidden] {
  display: none;
}

.ad-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 18px 28px;
  background: var(--ad-color-surface);
  border-bottom: 1px solid var(--ad-color-border);
}

.ad-header h1 {
  font-size: 18px;
  margin: 0;
}

.ad-header-actions {
  display: flex;
  align-items: center;
  gap: 12px;
}

.ad-username {
  font-size: 13px;
  color: var(--ad-color-text-muted);
}

.ad-btn-secondary {
  padding: 7px 14px;
  border-radius: 8px;
  border: 1px solid var(--ad-color-border);
  background: #fff;
  color: var(--ad-color-text);
  cursor: pointer;
  font-size: 13.5px;
}

.ad-btn-secondary:hover {
  background: var(--ad-color-bg);
}

.ad-main {
  padding: 24px 28px 48px;
  max-width: 1180px;
  margin: 0 auto;
}

.ad-toolbar {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  margin-bottom: 18px;
}

.ad-toolbar input[type="search"],
.ad-toolbar select {
  padding: 9px 12px;
  border-radius: 8px;
  border: 1px solid var(--ad-color-border);
  background: #fff;
}

.ad-toolbar input[type="search"] {
  flex: 1;
  min-width: 200px;
}

.ad-toolbar .ad-btn-secondary {
  margin-left: auto;
}

.ad-table-wrap {
  background: var(--ad-color-surface);
  border: 1px solid var(--ad-color-border);
  border-radius: var(--ad-radius);
  overflow-x: auto;
}

table.ad-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 13.5px;
}

.ad-table th, .ad-table td {
  text-align: left;
  padding: 10px 14px;
  border-bottom: 1px solid var(--ad-color-border);
  white-space: nowrap;
}

.ad-table td.ad-td-wrap {
  white-space: normal;
  max-width: 320px;
}

.ad-table th {
  color: var(--ad-color-text-muted);
  font-weight: 600;
  background: var(--ad-color-bg);
  position: sticky;
  top: 0;
}

.ad-table tbody tr:hover {
  background: #f7f9fc;
}

.ad-status-select {
  padding: 5px 8px;
  border-radius: 6px;
  border: 1px solid var(--ad-color-border);
  background: #fff;
}

.ad-badge {
  display: inline-block;
  padding: 3px 9px;
  border-radius: 999px;
  font-size: 12px;
  font-weight: 600;
}

.ad-badge--NEW { background: #e8f0fe; color: #2f6feb; }
.ad-badge--IN_PROGRESS { background: #fff4e0; color: #b7791f; }
.ad-badge--RESOLVED { background: #e6f6ee; color: #1e8e5a; }
.ad-badge--CLOSED { background: #eceef1; color: #5b6470; }

.ad-empty-state {
  padding: 48px 24px;
  text-align: center;
  color: var(--ad-color-text-muted);
}

.ad-loading-row td {
  text-align: center;
  color: var(--ad-color-text-muted);
  padding: 24px;
}

/* ============================================================
   Multi-tenant admin shell additions
   ============================================================ */

.ad-shell {
  display: flex;
  min-height: 100vh;
}

.ad-shell[hidden] {
  display: none;
}

.ad-sidebar {
  width: 220px;
  flex-shrink: 0;
  background: var(--ad-color-surface);
  border-right: 1px solid var(--ad-color-border);
  display: flex;
  flex-direction: column;
  padding: 20px 0;
}

.ad-sidebar-brand {
  font-weight: 700;
  font-size: 15px;
  padding: 0 20px 18px;
  border-bottom: 1px solid var(--ad-color-border);
  margin-bottom: 12px;
}

.ad-nav {
  display: flex;
  flex-direction: column;
  gap: 2px;
  padding: 0 10px;
}

.ad-nav-link {
  display: block;
  padding: 9px 12px;
  border-radius: 8px;
  color: var(--ad-color-text);
  text-decoration: none;
  font-size: 14px;
}

.ad-nav-link:hover {
  background: var(--ad-color-bg);
}

.ad-nav-link--active {
  background: #e8f0fe;
  color: var(--ad-color-primary);
  font-weight: 600;
}

.ad-shell-main {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
}

.ad-topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px 28px;
  background: var(--ad-color-surface);
  border-bottom: 1px solid var(--ad-color-border);
}

.ad-topbar-title {
  font-size: 17px;
  font-weight: 600;
}

.ad-main {
  padding: 24px 28px 48px;
  overflow-y: auto;
}

.ad-loading-text {
  color: var(--ad-color-text-muted);
  padding: 24px 0;
}

/* Stat cards */

.ad-stat-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
  gap: 14px;
  margin-bottom: 28px;
}

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

.ad-stat-value {
  font-size: 26px;
  font-weight: 700;
}

.ad-stat-label {
  font-size: 13px;
  color: var(--ad-color-text-muted);
  margin-top: 4px;
}

.ad-section-title {
  font-size: 16px;
  margin: 0 0 12px;
}

/* Extra status badges for conversations / tickets */
.ad-badge--AI_HANDLING { background: #e8f0fe; color: #2f6feb; }
.ad-badge--HUMAN_REQUIRED { background: #fdecea; color: #c0392b; }
.ad-badge--ASSIGNED { background: #fff4e0; color: #b7791f; }
.ad-badge--OPEN { background: #e8f0fe; color: #2f6feb; }
.ad-badge--WAITING_FOR_CLIENT { background: #fff4e0; color: #b7791f; }

.ad-clickable-row { cursor: pointer; }
.ad-clickable-row:hover { background: #f7f9fc; }

/* Forms (used inside modals) */

.ad-form {
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.ad-form .ad-field label {
  display: block;
  font-size: 13px;
  font-weight: 500;
  margin-bottom: 6px;
}

.ad-form input,
.ad-form select,
.ad-form textarea {
  width: 100%;
  padding: 9px 11px;
  border: 1px solid var(--ad-color-border);
  border-radius: 8px;
  font-family: inherit;
  font-size: 14px;
}

.ad-form textarea {
  resize: vertical;
}

/* Modal */

.ad-modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(20, 24, 30, 0.4);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
  padding: 20px;
}

.ad-modal {
  background: #fff;
  border-radius: var(--ad-radius);
  width: 100%;
  max-width: 480px;
  max-height: 90vh;
  overflow-y: auto;
  padding: 22px;
}

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

.ad-modal-title {
  font-size: 17px;
  margin: 0;
}

.ad-modal-close {
  background: none;
  border: none;
  font-size: 22px;
  line-height: 1;
  cursor: pointer;
  color: var(--ad-color-text-muted);
}

/* Knowledge base grid */

.ad-knowledge-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(260px, 1fr));
  gap: 14px;
}

.ad-knowledge-card {
  background: var(--ad-color-surface);
  border: 1px solid var(--ad-color-border);
  border-radius: var(--ad-radius);
  padding: 16px;
}

.ad-knowledge-card h3 {
  margin: 0 0 8px;
  font-size: 15px;
}

.ad-knowledge-card p {
  margin: 0 0 12px;
  font-size: 13.5px;
  color: var(--ad-color-text-muted);
  white-space: pre-wrap;
}

.ad-knowledge-card-actions {
  display: flex;
  gap: 8px;
}

/* Conversation / ticket detail */

.ad-back-link {
  display: inline-block;
  margin-bottom: 16px;
  color: var(--ad-color-primary);
  text-decoration: none;
  font-size: 13.5px;
}

.ad-detail-header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  margin-bottom: 16px;
  gap: 12px;
}

.ad-messages-panel {
  background: var(--ad-color-surface);
  border: 1px solid var(--ad-color-border);
  border-radius: var(--ad-radius);
  padding: 16px;
  max-height: 420px;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 10px;
  margin-bottom: 14px;
}

.ad-msg {
  max-width: 75%;
  padding: 8px 12px;
  border-radius: 10px;
  background: #fff;
  border: 1px solid var(--ad-color-border);
}

.ad-msg--customer { align-self: flex-start; }
.ad-msg--ai { align-self: flex-start; background: #eef4ff; border-color: #d7e4fb; }
.ad-msg--client_admin, .ad-msg--super_admin { align-self: flex-end; background: var(--ad-color-primary); color: #fff; border-color: var(--ad-color-primary); }
.ad-msg--client_admin .ad-msg-meta, .ad-msg--super_admin .ad-msg-meta { color: rgba(255,255,255,0.8); }

.ad-msg-meta {
  font-size: 11.5px;
  color: var(--ad-color-text-muted);
  margin-bottom: 3px;
}

.ad-msg-body {
  font-size: 14px;
  white-space: pre-wrap;
}

.ad-reply-form {
  display: flex;
  gap: 8px;
}

.ad-reply-form textarea {
  flex: 1;
  padding: 9px 11px;
  border: 1px solid var(--ad-color-border);
  border-radius: 8px;
  font-family: inherit;
  resize: none;
}

/* Settings page */

.ad-settings-section {
  background: var(--ad-color-surface);
  border: 1px solid var(--ad-color-border);
  border-radius: var(--ad-radius);
  padding: 18px 20px;
  margin-bottom: 16px;
}

.ad-code-block {
  background: #1b1f24;
  color: #e8eaed;
  padding: 14px 16px;
  border-radius: 8px;
  font-size: 12.5px;
  overflow-x: auto;
  white-space: pre;
}
