:root {
  --primary: #2563eb;
  --primary-light: #eff6ff;
  --primary-dark: #1d4ed8;
  --success: #16a34a;
  --danger: #dc2626;
  --warning: #d97706;
  --gray-50: #f9fafb;
  --gray-100: #f3f4f6;
  --gray-200: #e5e7eb;
  --gray-400: #9ca3af;
  --gray-600: #4b5563;
  --gray-700: #374151;
  --gray-900: #111827;
  --radius: 8px;
  --shadow: 0 1px 3px rgba(0,0,0,.1), 0 1px 2px rgba(0,0,0,.06);
  --shadow-md: 0 4px 6px rgba(0,0,0,.07), 0 2px 4px rgba(0,0,0,.06);
}

* { box-sizing: border-box; margin: 0; padding: 0; }

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  font-size: 14px;
  color: var(--gray-700);
  background: var(--gray-50);
  min-height: 100vh;
}

a { color: var(--primary); text-decoration: none; }
a:hover { text-decoration: underline; }

/* ── Layout ────────────────────────────────────────────────────── */

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

.sidebar {
  width: 220px;
  background: #fff;
  border-right: 1px solid var(--gray-200);
  display: flex;
  flex-direction: column;
  flex-shrink: 0;
  padding: 0;
}

.sidebar-logo {
  padding: 20px 20px 16px;
  font-weight: 700;
  font-size: 16px;
  color: var(--primary);
  border-bottom: 1px solid var(--gray-100);
}

.sidebar-logo span {
  display: block;
  font-size: 11px;
  font-weight: 400;
  color: var(--gray-400);
  margin-top: 2px;
}

.sidebar nav {
  flex: 1;
  padding: 12px 0;
}

.sidebar nav a {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 9px 20px;
  color: var(--gray-600);
  font-weight: 500;
  border-left: 3px solid transparent;
  transition: background .15s, color .15s;
}

.sidebar nav a:hover {
  background: var(--gray-50);
  color: var(--gray-900);
  text-decoration: none;
}

.sidebar nav a.active {
  color: var(--primary);
  background: var(--primary-light);
  border-left-color: var(--primary);
}

.sidebar nav a.sys {
  font-size: 13px;
  padding: 7px 20px;
  color: var(--gray-500);
}

.sidebar-section-label {
  padding: 16px 20px 4px;
  font-size: 10px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: .08em;
  color: var(--gray-400);
}

.sidebar-footer {
  padding: 16px 20px;
  border-top: 1px solid var(--gray-100);
}

.main-content {
  flex: 1;
  padding: 32px;
  overflow-y: auto;
  animation: pageIn 0.18s ease-out;
}

.page-header {
  margin-bottom: 24px;
}

.page-header h1 {
  font-size: 22px;
  font-weight: 700;
  color: var(--gray-900);
}

.page-header p {
  color: var(--gray-400);
  margin-top: 4px;
}

/* ── Cards ─────────────────────────────────────────────────────── */

.cards {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(180px, 1fr));
  gap: 16px;
  margin-bottom: 28px;
}

.card {
  background: #fff;
  border: 1px solid var(--gray-200);
  border-radius: var(--radius);
  padding: 20px;
  box-shadow: var(--shadow);
}

.card-label {
  font-size: 12px;
  color: var(--gray-400);
  text-transform: uppercase;
  letter-spacing: .04em;
  font-weight: 600;
}

.card-value {
  font-size: 30px;
  font-weight: 700;
  color: var(--gray-900);
  margin-top: 6px;
}

/* ── Tables ─────────────────────────────────────────────────────── */

.table-wrap {
  background: #fff;
  border: 1px solid var(--gray-200);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  overflow: hidden;
}

.table-wrap table {
  width: 100%;
  border-collapse: collapse;
}

.table-wrap th {
  text-align: left;
  padding: 10px 16px;
  font-size: 12px;
  font-weight: 600;
  color: var(--gray-400);
  text-transform: uppercase;
  letter-spacing: .04em;
  background: var(--gray-50);
  border-bottom: 1px solid var(--gray-200);
}

.table-wrap td {
  padding: 12px 16px;
  border-bottom: 1px solid var(--gray-100);
  color: var(--gray-700);
}

.table-wrap tr:last-child td { border-bottom: none; }

.table-wrap tbody tr:hover { background: var(--gray-50); }

.table-wrap tbody tr.clickable { cursor: pointer; }

/* ── Badges ─────────────────────────────────────────────────────── */

.badge {
  display: inline-block;
  padding: 2px 8px;
  border-radius: 99px;
  font-size: 11px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: .03em;
}

.badge-active  { background: #dcfce7; color: #15803d; }
.badge-link    { background: #dbeafe; color: #1d4ed8; }
.badge-init    { background: var(--gray-100); color: var(--gray-600); }

/* ── Forms ──────────────────────────────────────────────────────── */

.form-section {
  background: #fff;
  border: 1px solid var(--gray-200);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  margin-bottom: 24px;
  overflow: hidden;
}

.form-section-title {
  padding: 14px 20px;
  font-weight: 600;
  font-size: 13px;
  color: var(--gray-700);
  background: var(--gray-50);
  border-bottom: 1px solid var(--gray-200);
  text-transform: uppercase;
  letter-spacing: .04em;
}

.form-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
  gap: 16px;
  padding: 20px;
}

.form-group {
  display: flex;
  flex-direction: column;
  gap: 5px;
}

.form-group.full { grid-column: 1 / -1; }

.form-grid-1col {
  grid-template-columns: 1fr;
}

label {
  font-size: 12px;
  font-weight: 600;
  color: var(--gray-600);
}

input[type="text"],
input[type="password"],
input[type="number"],
input[type="url"],
input[type="email"],
input[type="tel"],
input[type="search"],
input[type="date"],
select,
textarea {
  border: 1px solid var(--gray-200);
  border-radius: 6px;
  padding: 8px 10px;
  font-size: 14px;
  font-family: inherit;
  color: var(--gray-900);
  background: #fff;
  transition: border-color .15s, box-shadow .15s;
  width: 100%;
}

select { cursor: pointer; }

input:focus, select:focus, textarea:focus {
  outline: none;
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(37,99,235,.1);
}

textarea { resize: vertical; min-height: 70px; }

/* ── Buttons ─────────────────────────────────────────────────────── */

.btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 8px 16px;
  border-radius: 6px;
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
  border: none;
  transition: background .15s, opacity .15s;
}

.btn-primary {
  background: var(--primary);
  color: #fff;
}
.btn-primary:hover { background: var(--primary-dark); }

.btn-danger {
  background: #fff;
  color: var(--danger);
  border: 1px solid #fecaca;
}
.btn-danger:hover { background: #fef2f2; }

.btn-ghost {
  background: transparent;
  color: var(--gray-600);
  border: 1px solid var(--gray-200);
}
.btn-ghost:hover { background: var(--gray-50); }

.btn-sm { padding: 5px 10px; font-size: 12px; }

.btn:disabled { opacity: .5; cursor: not-allowed; }

/* ── Toggle ─────────────────────────────────────────────────────── */

.toggle {
  position: relative;
  display: inline-block;
  width: 36px;
  height: 20px;
}

.toggle input { opacity: 0; width: 0; height: 0; }

.toggle-slider {
  position: absolute;
  inset: 0;
  background: var(--gray-200);
  border-radius: 99px;
  cursor: pointer;
  transition: background .2s;
}

.toggle-slider::before {
  content: "";
  position: absolute;
  width: 14px;
  height: 14px;
  border-radius: 50%;
  background: #fff;
  top: 3px;
  left: 3px;
  transition: transform .2s;
  box-shadow: 0 1px 2px rgba(0,0,0,.2);
}

.toggle input:checked + .toggle-slider { background: var(--primary); }
.toggle input:checked + .toggle-slider::before { transform: translateX(16px); }

/* ── Chat ────────────────────────────────────────────────────────── */

.chat-wrap {
  background: #fff;
  border: 1px solid var(--gray-200);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  overflow: hidden;
}

.chat-header {
  padding: 16px 20px;
  border-bottom: 1px solid var(--gray-100);
  display: flex;
  align-items: center;
  gap: 12px;
  background: var(--gray-50);
}

.chat-header-info h2 { font-size: 15px; font-weight: 600; color: var(--gray-900); }
.chat-header-info p  { font-size: 12px; color: var(--gray-400); }

.chat-messages {
  padding: 20px;
  display: flex;
  flex-direction: column;
  gap: 12px;
  min-height: 300px;
  max-height: 60vh;
  overflow-y: auto;
}

.msg { display: flex; gap: 8px; max-width: 75%; }
.msg.user       { align-self: flex-start; }
.msg.assistant  { align-self: flex-end; flex-direction: row-reverse; }
.msg.human_agent { align-self: flex-end; flex-direction: row-reverse; }

.msg-bubble {
  padding: 10px 14px;
  border-radius: 14px;
  font-size: 14px;
  line-height: 1.5;
  white-space: pre-wrap;
  word-break: break-word;
}

.msg.user .msg-bubble {
  background: var(--gray-100);
  color: var(--gray-900);
  border-bottom-left-radius: 4px;
}

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

.msg.human_agent .msg-bubble {
  background: #16a34a;
  color: #fff;
  border-bottom-right-radius: 4px;
}

.msg-time {
  font-size: 11px;
  color: var(--gray-400);
  margin-top: 5px;
  padding: 0 4px;
  white-space: nowrap;
  display: flex;
  align-items: center;
  gap: 4px;
}

.msg.assistant .msg-time,
.msg.human_agent .msg-time { justify-content: flex-end; }

/* ── Login ───────────────────────────────────────────────────────── */

.login-page {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--gray-50);
}

.login-box {
  background: #fff;
  border: 1px solid var(--gray-200);
  border-radius: var(--radius);
  box-shadow: var(--shadow-md);
  padding: 36px;
  width: 100%;
  max-width: 360px;
}

.login-title {
  font-size: 20px;
  font-weight: 700;
  color: var(--gray-900);
  margin-bottom: 4px;
}

.login-sub {
  font-size: 13px;
  color: var(--gray-400);
  margin-bottom: 24px;
}

.login-box .form-group { margin-bottom: 16px; }

.login-error {
  color: var(--danger);
  font-size: 13px;
  margin-top: 10px;
  display: none;
}

/* ── FAQ list ────────────────────────────────────────────────────── */

.faq-list { display: flex; flex-direction: column; gap: 0; }

.faq-item {
  background: #fff;
  border: 1px solid var(--gray-200);
  border-bottom: none;
  padding: 14px 16px;
  display: flex;
  align-items: flex-start;
  gap: 12px;
}

.faq-item:first-child { border-radius: var(--radius) var(--radius) 0 0; }
.faq-item:last-child  { border-bottom: 1px solid var(--gray-200); border-radius: 0 0 var(--radius) var(--radius); }
.faq-item:only-child  { border-radius: var(--radius); border-bottom: 1px solid var(--gray-200); }

.faq-drag { cursor: grab; color: var(--gray-400); padding-top: 2px; font-size: 16px; }

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

.faq-view { display: flex; flex-direction: column; gap: 2px; }
.faq-edit { display: none; flex-direction: column; gap: 8px; }

.faq-q { font-weight: 600; color: var(--gray-900); }
.faq-a { color: var(--gray-600); font-size: 13px; }

.faq-actions { display: flex; gap: 6px; align-items: center; flex-shrink: 0; }

/* ── Add form ────────────────────────────────────────────────────── */

.add-form {
  background: #fff;
  border: 1px solid var(--gray-200);
  border-radius: var(--radius);
  padding: 20px;
  box-shadow: var(--shadow);
  margin-top: 16px;
}

.add-form h3 { font-size: 14px; font-weight: 600; margin-bottom: 14px; }

/* ── Alert ───────────────────────────────────────────────────────── */

.alert {
  padding: 10px 14px;
  border-radius: 6px;
  font-size: 13px;
  margin-bottom: 16px;
  display: none;
}

.alert-success { background: #dcfce7; color: #15803d; border: 1px solid #bbf7d0; }
.alert-error   { background: #fef2f2; color: var(--danger); border: 1px solid #fecaca; }

/* ── Misc ────────────────────────────────────────────────────────── */

.empty-state {
  text-align: center;
  padding: 48px 20px;
  color: var(--gray-400);
}

.empty-state p { font-size: 15px; margin-top: 8px; }

.back-link {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  color: var(--gray-600);
  font-size: 13px;
  margin-bottom: 20px;
}

.back-link:hover { color: var(--gray-900); text-decoration: none; }

.form-actions {
  padding: 16px 20px;
  background: var(--gray-50);
  border-top: 1px solid var(--gray-200);
  display: flex;
  align-items: center;
  gap: 12px;
}

/* ── Tabs ───────────────────────────────────────────────────────── */

.tab-bar {
  display: flex;
  border-bottom: 2px solid var(--gray-200);
  margin-bottom: 24px;
}

.tab-btn {
  padding: 10px 20px;
  font-size: 14px;
  font-weight: 500;
  color: var(--gray-600);
  background: none;
  border: none;
  border-bottom: 3px solid transparent;
  margin-bottom: -2px;
  cursor: pointer;
  transition: color .15s, border-color .15s;
}

.tab-btn:hover { color: var(--gray-900); }

.tab-btn.active {
  color: var(--primary);
  border-bottom-color: var(--primary);
  font-weight: 600;
}

.tab-panel { display: none; }
.tab-panel.active { display: block; }

/* ── Day separator (conversation view) ──────────────────────────── */

.day-separator {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 16px;
}

.day-separator::before,
.day-separator::after {
  content: "";
  flex: 1;
  height: 1px;
  background: var(--gray-200);
}

.day-separator span {
  font-size: 11px;
  font-weight: 600;
  color: var(--gray-400);
  text-transform: uppercase;
  letter-spacing: .05em;
  white-space: nowrap;
}

/* ── Email chips (patient record) ───────────────────────────────── */

.email-chips {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  min-height: 28px;
  margin-bottom: 8px;
}

.email-chip {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  background: var(--primary-light);
  color: var(--primary-dark);
  border-radius: 99px;
  padding: 3px 10px 3px 12px;
  font-size: 13px;
}

.email-chip button {
  background: none;
  border: none;
  cursor: pointer;
  color: var(--primary);
  padding: 0;
  line-height: 1;
  font-size: 16px;
  opacity: .6;
}
.email-chip button:hover { opacity: 1; }

.email-add-row { display: flex; gap: 8px; }
.email-add-row input { flex: 1; }

/* ── Notification dots (nav + tabs) ─────────────────────────────── */

.nav-dot {
  width: 7px;
  height: 7px;
  background: var(--danger);
  border-radius: 50%;
  flex-shrink: 0;
  margin-left: auto;
}

.tab-dot {
  display: inline-block;
  width: 7px;
  height: 7px;
  background: var(--danger);
  border-radius: 50%;
  margin-left: 5px;
  vertical-align: middle;
  flex-shrink: 0;
}

/* ── Transitions & loading ───────────────────────────────────────── */

@keyframes pageIn {
  from { opacity: 0; transform: translateY(6px); }
  to   { opacity: 1; transform: translateY(0); }
}

@keyframes rowIn {
  from { opacity: 0; }
  to   { opacity: 1; }
}

@keyframes shimmer {
  0%   { background-position: 200% 0; }
  100% { background-position: -200% 0; }
}

.skeleton {
  display: block;
  height: 13px;
  border-radius: 4px;
  background: linear-gradient(90deg, var(--gray-100) 25%, var(--gray-200) 50%, var(--gray-100) 75%);
  background-size: 200% 100%;
  animation: shimmer 1.5s ease-in-out infinite;
}

.table-wrap tbody tr { animation: rowIn 0.18s ease-out; }
.faq-item            { animation: rowIn 0.18s ease-out; }

/* ── Conversation page ─────────────────────────────────────────── */

.conv-layout {
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.patient-fields {
  display: flex;
  flex-direction: row;
  gap: 0;
}

.patient-field {
  display: flex;
  flex-direction: column;
  gap: 3px;
  flex: 1;
  min-width: 0;
  padding: 0 20px;
  border-right: 1px solid var(--gray-200);
}

.patient-field:first-child { padding-left: 0; }
.patient-field:last-child  { border-right: none; }

.patient-field label {
  font-size: 11px;
  font-weight: 600;
  color: var(--gray-400);
  text-transform: uppercase;
  letter-spacing: .04em;
  white-space: nowrap;
}

.info-value {
  font-size: 14px;
  color: var(--gray-900);
}

.name-edit-wrap { min-width: 0; }

.name-display {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  cursor: pointer;
  border-radius: 4px;
  padding: 2px 4px;
  margin: -2px -4px;
  transition: background .15s;
  max-width: 100%;
}

.name-display:hover { background: var(--gray-100); }

.name-display .edit-icon {
  opacity: 0;
  color: var(--gray-400);
  font-size: 11px;
  transition: opacity .15s;
  flex-shrink: 0;
}

.name-display:hover .edit-icon { opacity: 1; }

.name-input-wrap {
  display: none;
  align-items: center;
  gap: 6px;
}

.name-input-wrap input {
  width: 180px;
  padding: 3px 7px;
  font-size: 14px;
  border: 1px solid var(--primary);
  border-radius: 4px;
  box-shadow: 0 0 0 3px rgba(37,99,235,.1);
  outline: none;
  color: var(--gray-900);
  background: #fff;
  font-family: inherit;
}

.name-hint {
  font-size: 11px;
  color: var(--gray-400);
  margin-top: 3px;
}

.notes-block {
  background: #fff;
  border: 1px solid var(--gray-200);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  overflow: hidden;
}

.notes-block-title {
  padding: 12px 16px;
  font-size: 11px;
  font-weight: 700;
  color: var(--gray-400);
  text-transform: uppercase;
  letter-spacing: .06em;
  background: var(--gray-50);
  border-bottom: 1px solid var(--gray-200);
}

.notes-body {
  padding: 12px 16px 16px;
}

.notes-body textarea {
  width: 100%;
  font-size: 13px;
  line-height: 1.55;
  resize: vertical;
  min-height: 100px;
  border: 1px solid var(--gray-200);
  border-radius: 6px;
  padding: 8px 10px;
  color: var(--gray-900);
  font-family: inherit;
  background: var(--gray-50);
  transition: border-color .15s, background .15s, box-shadow .15s;
}

.notes-body textarea:focus {
  outline: none;
  background: #fff;
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(37,99,235,.1);
}

.notes-footer {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-top: 10px;
}

.notes-status {
  font-size: 12px;
  opacity: 0;
  transition: opacity .2s;
}

.notes-status.visible { opacity: 1; }
.notes-status.ok  { color: var(--success); }
.notes-status.err { color: var(--danger); }

/* ── Appointment list (ficha + conversation panel) ───────────────── */

.appt-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.appt-item {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 12px;
  border: 1px solid var(--gray-200);
  border-radius: 6px;
  background: #fff;
}

.appt-item.upcoming {
  border-color: #bfdbfe;
  background: var(--primary-light);
}

.appt-item.muted { opacity: .6; }

.appt-date-block {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  min-width: 44px;
  padding: 5px 6px;
  border-radius: 6px;
  background: var(--gray-100);
  color: var(--gray-600);
}

.appt-item.upcoming .appt-date-block {
  background: var(--primary);
  color: #fff;
}

.appt-day { font-size: 17px; font-weight: 700; line-height: 1.05; }

.appt-month {
  font-size: 10px;
  text-transform: uppercase;
  letter-spacing: .05em;
}

.appt-info { flex: 1; min-width: 0; }

.appt-time {
  font-size: 13px;
  font-weight: 600;
  color: var(--gray-900);
  text-transform: capitalize;
}

.appt-summary {
  font-size: 12px;
  color: var(--gray-400);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.appt-status {
  display: flex;
  align-items: center;
}

/* Pill badge inline with the date/time line. */
.appt-next {
  display: inline-block;
  padding: 1px 7px;
  border-radius: 99px;
  background: var(--primary);
  color: #fff;
  font-size: 9px;
  font-weight: 700;
  line-height: 14px;
  text-transform: uppercase;
  letter-spacing: .04em;
  white-space: nowrap;
  vertical-align: 1px;
  margin-left: 4px;
}

.appt-empty {
  font-size: 13px;
  color: var(--gray-400);
  padding: 4px 0;
}
