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

:root {
  --bg: #eef1f6;
  --panel: #ffffff;
  --panel-2: #f8f9fc;
  --border: #e4e8ef;
  --text: #17202b;
  --muted: #6b7686;
  --accent: #1c3a5e;
  --accent-2: #2d6a4f;
  --accent-warm: #c9883a;
  --accent-soft: #e8eef6;
  --attention: #c0392b;
  --attention-soft: #fdeceb;
  --bubble-in: #ffffff;
  --bubble-out: #1c3a5e;
  --shadow-sm: 0 1px 2px rgba(23, 32, 43, 0.05);
  --shadow-md: 0 4px 16px rgba(23, 32, 43, 0.08);
  --radius: 14px;
}

html, body { height: 100%; }

body {
  font-family: -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  -webkit-font-smoothing: antialiased;
}

.muted { color: var(--muted); font-size: 0.85rem; }
.error { color: var(--attention); font-size: 0.9rem; margin-top: 8px; }

::-webkit-scrollbar { width: 10px; height: 10px; }
::-webkit-scrollbar-thumb { background: #c9d0da; border-radius: 8px; border: 2px solid transparent; background-clip: content-box; }
::-webkit-scrollbar-thumb:hover { background: #aeb7c4; border: 2px solid transparent; background-clip: content-box; }
::-webkit-scrollbar-track { background: transparent; }

/* ---------- Login ---------- */
.login-body {
  display: grid; place-items: center; height: 100vh;
  background: linear-gradient(150deg, #1c3a5e 0%, #2c5580 55%, #2d6a4f 130%);
}
.login-card {
  background: var(--panel); border: none; border-radius: 18px;
  padding: 36px; width: 340px; display: flex; flex-direction: column; gap: 14px;
  box-shadow: 0 20px 60px rgba(10, 20, 35, 0.35);
}
.login-card h1 { font-size: 1.35rem; letter-spacing: -0.01em; }
.login-card input {
  padding: 12px 14px; border: 1px solid var(--border); border-radius: 10px; font-size: 1rem;
  background: var(--panel-2);
}
.login-card input:focus { outline: 2px solid var(--accent); outline-offset: -1px; background: var(--panel); }
.login-card button {
  padding: 12px; border: none; border-radius: 10px; background: var(--accent);
  color: white; font-size: 1rem; font-weight: 600; cursor: pointer;
}
.login-card button:hover { background: #254a75; }

/* ---------- Layout ---------- */
#app { display: flex; height: 100vh; }

#sidebar {
  width: 340px; min-width: 280px; background: var(--panel);
  border-right: 1px solid var(--border); display: flex; flex-direction: column;
}
.sidebar-header {
  display: flex; justify-content: space-between; align-items: center;
  padding: 16px 16px 12px;
}
.sidebar-header h1 { font-size: 1.15rem; letter-spacing: -0.01em; }

.header-actions { display: flex; gap: 2px; }
.header-actions button, .header-actions a {
  background: none; border: none; font-size: 1.05rem; cursor: pointer;
  width: 34px; height: 34px; display: grid; place-items: center;
  border-radius: 10px; text-decoration: none; color: var(--text);
  transition: background 0.12s;
}
.header-actions button:hover, .header-actions a:hover { background: var(--accent-soft); }

.ai-global-toggle {
  display: flex; align-items: center; gap: 9px; margin: 0 16px 12px;
  padding: 9px 12px; border-radius: 10px; background: var(--panel-2);
  border: 1px solid var(--border);
  font-size: 0.86rem; font-weight: 500; cursor: pointer; user-select: none;
}
.ai-global-toggle input { accent-color: var(--accent-2); width: 15px; height: 15px; }

#conversation-list { list-style: none; overflow-y: auto; flex: 1; padding: 0 10px 12px; }
#conversation-list li {
  padding: 11px 12px; border-radius: 12px; cursor: pointer; margin-bottom: 2px;
  transition: background 0.12s;
}
#conversation-list li:hover { background: var(--panel-2); }
#conversation-list li.active { background: var(--accent-soft); }

.conv-row { display: flex; align-items: center; gap: 11px; }
.conv-avatar {
  width: 42px; height: 42px; border-radius: 50%; flex-shrink: 0;
  display: grid; place-items: center;
  color: white; font-weight: 700; font-size: 0.95rem; letter-spacing: 0.02em;
}
.conv-main { display: flex; flex-direction: column; gap: 3px; min-width: 0; flex: 1; }
.conv-top { display: flex; justify-content: space-between; align-items: baseline; gap: 8px; }
.conv-name {
  font-weight: 600; font-size: 0.93rem;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.conv-time { font-size: 0.72rem; color: var(--muted); flex-shrink: 0; }
.conv-bottom { display: flex; justify-content: space-between; align-items: center; gap: 8px; }
.conv-preview {
  color: var(--muted); font-size: 0.82rem; white-space: nowrap;
  overflow: hidden; text-overflow: ellipsis; flex: 1;
}
li.unread-conv .conv-preview { color: var(--text); font-weight: 500; }
li.unread-conv .conv-name { font-weight: 700; }

.conv-badges { display: flex; gap: 4px; align-items: center; flex-shrink: 0; }
.badge {
  font-size: 0.7rem; border-radius: 10px; padding: 2px 7px; font-weight: 700;
  line-height: 1.4;
}
.badge.unread { background: var(--accent); color: white; min-width: 20px; text-align: center; }
.badge.attention { background: var(--attention); color: white; }
.badge.ai-off { background: #eceff3; color: var(--muted); }
.badge.approved { background: #dcefe4; }

#main { flex: 1; display: flex; flex-direction: column; overflow: hidden; }
#main > section { flex: 1; display: flex; flex-direction: column; overflow: hidden; }
#empty-view, #appointments-view, #settings-view { padding: 28px 32px; overflow-y: auto; }
#empty-view { display: grid; place-items: center; }
.empty-state { text-align: center; color: var(--muted); }
.empty-state .empty-icon { font-size: 2.6rem; display: block; margin-bottom: 10px; }

#appointments-view h2, #settings-view h2 { font-size: 1.25rem; letter-spacing: -0.01em; }

/* ---------- Chat ---------- */
#chat-header {
  display: flex; justify-content: space-between; align-items: center; gap: 14px;
  padding: 12px 22px; background: var(--panel); border-bottom: 1px solid var(--border);
  box-shadow: var(--shadow-sm); z-index: 2;
}
.chat-id { display: flex; align-items: center; gap: 12px; min-width: 0; }
#chat-avatar {
  width: 40px; height: 40px; border-radius: 50%; flex-shrink: 0;
  display: grid; place-items: center; color: white; font-weight: 700; font-size: 0.9rem;
}
.chat-names { display: flex; flex-direction: column; min-width: 0; }
#chat-name { font-weight: 700; font-size: 1rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
#chat-phone { margin-left: 0; font-size: 0.78rem; }
.chat-header-actions { display: flex; align-items: center; gap: 14px; flex-shrink: 0; }
.toggle-label {
  display: flex; align-items: center; gap: 6px; font-size: 0.83rem; cursor: pointer;
  user-select: none; color: var(--muted); font-weight: 500;
}
.toggle-label input { accent-color: var(--accent-2); }

#messages {
  flex: 1; overflow-y: auto; padding: 24px 26px;
  display: flex; flex-direction: column; gap: 8px;
  background:
    radial-gradient(circle at 15% 20%, rgba(28, 58, 94, 0.03), transparent 40%),
    radial-gradient(circle at 85% 80%, rgba(45, 106, 79, 0.03), transparent 40%),
    var(--bg);
}
.day-sep {
  align-self: center; margin: 12px 0 6px;
  font-size: 0.72rem; font-weight: 600; color: var(--muted);
  background: var(--panel); border: 1px solid var(--border);
  padding: 3px 12px; border-radius: 20px; box-shadow: var(--shadow-sm);
}
.msg { max-width: 62%; display: flex; flex-direction: column; }
.msg-in { align-self: flex-start; }
.msg-out { align-self: flex-end; }
.msg-body {
  padding: 10px 15px; border-radius: 18px; white-space: pre-wrap; word-break: break-word;
  font-size: 0.94rem; line-height: 1.42;
}
.msg-in .msg-body {
  background: var(--bubble-in); border: 1px solid var(--border);
  border-bottom-left-radius: 5px; box-shadow: var(--shadow-sm);
}
.msg-out .msg-body {
  background: var(--bubble-out); color: #f3f6fa; border-bottom-right-radius: 5px;
  box-shadow: var(--shadow-sm);
}
.msg-out.msg-ai .msg-body { background: #2d6a4f; }
.msg-meta { font-size: 0.7rem; color: var(--muted); margin-top: 4px; padding: 0 6px; }
.msg-out .msg-meta { text-align: right; }

#send-form {
  display: flex; gap: 10px; padding: 14px 22px 18px; background: var(--panel);
  border-top: 1px solid var(--border); align-items: flex-end;
}
#send-input {
  flex: 1; resize: none; padding: 11px 16px; border: 1px solid var(--border);
  border-radius: 22px; font-size: 0.94rem; font-family: inherit;
  background: var(--panel-2); max-height: 140px; line-height: 1.4;
}
#send-input:focus { outline: 2px solid var(--accent); outline-offset: -1px; background: var(--panel); }
#send-form button {
  width: 42px; height: 42px; border: none; border-radius: 50%;
  background: var(--accent); color: white; font-size: 1.05rem; cursor: pointer;
  flex-shrink: 0; display: grid; place-items: center; transition: background 0.12s;
}
#send-form button:hover { background: #254a75; }

.small-btn {
  padding: 5px 12px; border: 1px solid var(--border); border-radius: 8px;
  background: var(--panel); cursor: pointer; font-size: 0.8rem; font-weight: 500;
}
.small-btn:hover { background: var(--panel-2); }
#resolve-btn { border-color: var(--accent-2); color: var(--accent-2); font-weight: 600; }

/* ---------- Appointments ---------- */
#appts-table {
  border-collapse: separate; border-spacing: 0; width: 100%; max-width: 920px; margin-top: 18px;
  background: var(--panel); border: 1px solid var(--border); border-radius: var(--radius);
  overflow: hidden; box-shadow: var(--shadow-sm);
}
#appts-table th, #appts-table td {
  text-align: left; padding: 12px 16px; border-bottom: 1px solid var(--border); font-size: 0.9rem;
}
#appts-table tr:last-child td { border-bottom: none; }
#appts-table th {
  color: var(--muted); font-weight: 600; font-size: 0.74rem; text-transform: uppercase;
  letter-spacing: 0.05em; background: var(--panel-2);
}
#appts-table tbody tr:hover td { background: var(--panel-2); }
.appt-pet { font-size: 0.8rem; }

/* ---------- Settings ---------- */
#settings-form {
  max-width: 860px; display: flex; flex-direction: column; gap: 18px; margin-top: 18px;
  background: var(--panel); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 26px; box-shadow: var(--shadow-sm);
}
#settings-form label { display: flex; flex-direction: column; gap: 6px; font-size: 0.88rem; font-weight: 600; }
#settings-form input, #settings-form textarea {
  padding: 10px 13px; border: 1px solid var(--border); border-radius: 10px;
  font-size: 0.94rem; font-family: inherit; font-weight: 400; background: var(--panel-2);
}
#settings-form input:focus, #settings-form textarea:focus {
  outline: 2px solid var(--accent); outline-offset: -1px; background: var(--panel);
}
#settings-form .grow-textarea { min-height: 320px; resize: vertical; line-height: 1.55; }
#settings-form fieldset { border: 1px solid var(--border); border-radius: 10px; padding: 16px; }
#settings-form legend { font-size: 0.85rem; font-weight: 600; padding: 0 8px; }
.hours-row { display: flex; align-items: center; gap: 10px; margin-bottom: 8px; }
.day-label { width: 40px; font-size: 0.9rem; font-weight: 600; }
.hours-row input { padding: 6px 9px; }
#settings-form > button {
  align-self: flex-start; padding: 11px 26px; border: none; border-radius: 10px;
  background: var(--accent); color: white; font-size: 0.95rem; font-weight: 600; cursor: pointer;
}
#settings-form > button:hover { background: #254a75; }

/* ---------- Dialog ---------- */
dialog {
  border: none; border-radius: 16px; padding: 26px; width: 380px;
  box-shadow: 0 16px 60px rgba(10, 20, 35, 0.25);
}
dialog::backdrop { background: rgba(15, 25, 40, 0.4); backdrop-filter: blur(2px); }
dialog h3 { margin-bottom: 14px; letter-spacing: -0.01em; }
dialog form { display: flex; flex-direction: column; gap: 12px; }
dialog label { display: flex; flex-direction: column; gap: 5px; font-size: 0.85rem; font-weight: 600; }
dialog input, dialog textarea {
  padding: 9px 12px; border: 1px solid var(--border); border-radius: 9px;
  font-family: inherit; font-size: 0.95rem; font-weight: 400; background: var(--panel-2);
}
dialog input:focus, dialog textarea:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
.dialog-actions { display: flex; justify-content: flex-end; gap: 10px; margin-top: 4px; }
.dialog-actions button {
  padding: 9px 18px; border-radius: 9px; border: 1px solid var(--border);
  background: var(--panel); cursor: pointer; font-weight: 500;
}
.dialog-actions button[type="submit"] { background: var(--accent); color: white; border: none; font-weight: 600; }

@media (max-width: 720px) {
  #sidebar { width: 100%; position: absolute; z-index: 5; height: 100%; }
  #sidebar:has(~ #main #chat-view:not([hidden])) { display: none; }
  .msg { max-width: 82%; }
  #messages { padding: 16px 12px; }
}
