:root {
  --green: #25D366;
  --green-dark: #128C7E;
  --bg: #f0f2f5;
  --panel: #ffffff;
  --border: #e5e7eb;
  --text: #111827;
  --muted: #6b7280;
  --bubble-in: #ffffff;
  --bubble-out: #d9fdd3;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
}

.hidden { display: none !important; }

/* Login */
.login-screen {
  height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, var(--green-dark), var(--green));
}
.login-card {
  background: var(--panel);
  padding: 32px;
  border-radius: 12px;
  width: 320px;
  display: flex;
  flex-direction: column;
  gap: 12px;
  box-shadow: 0 10px 30px rgba(0,0,0,0.2);
}
.login-card h1 { font-size: 18px; margin: 0 0 8px; text-align: center; }
.login-card input {
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: 8px;
  font-size: 14px;
}
.login-card button {
  background: var(--green-dark);
  color: white;
  border: none;
  padding: 10px;
  border-radius: 8px;
  cursor: pointer;
  font-size: 14px;
}
.login-card button:hover { background: #0f6f61; }
.error { color: #dc2626; font-size: 13px; margin: 0; min-height: 16px; }

/* App layout */
.app {
  display: grid;
  grid-template-columns: 320px 1fr auto;
  height: 100vh;
}

.sidebar {
  border-right: 1px solid var(--border);
  background: var(--panel);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}
.sidebar-header {
  padding: 12px;
  display: flex;
  gap: 8px;
  border-bottom: 1px solid var(--border);
}
.sidebar-header select {
  flex: 1;
  padding: 8px;
  border-radius: 8px;
  border: 1px solid var(--border);
}
.sidebar-header button {
  border: none;
  background: var(--bg);
  border-radius: 8px;
  padding: 8px 10px;
  cursor: pointer;
}

.sidebar-tabs { display: flex; border-bottom: 1px solid var(--border); }
.sidebar-tab {
  flex: 1;
  border: none;
  background: var(--panel);
  padding: 10px;
  font-size: 13px;
  cursor: pointer;
  color: var(--muted);
  border-bottom: 2px solid transparent;
}
.sidebar-tab.active { color: var(--green-dark); font-weight: 600; border-bottom-color: var(--green-dark); }

.chat-list { overflow-y: auto; flex: 1; }
.chat-item {
  padding: 12px 16px;
  border-bottom: 1px solid #f3f4f6;
  cursor: pointer;
  display: flex;
  flex-direction: column;
  gap: 2px;
}
.chat-item:hover { background: #f9fafb; }
.chat-item.active { background: #e7f8ef; }
.chat-item-top { display: flex; justify-content: space-between; align-items: baseline; }
.chat-item-name { font-weight: 600; font-size: 14px; }
.chat-item-time { font-size: 11px; color: var(--muted); }
.chat-item-preview { font-size: 13px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.chat-item-unread {
  display: inline-block;
  background: var(--green);
  color: white;
  border-radius: 999px;
  font-size: 11px;
  padding: 1px 7px;
  margin-left: 6px;
}

.chat-main { display: flex; flex-direction: column; background: #e5ddd5; min-width: 0; }
.chat-empty { flex: 1; display: flex; align-items: center; justify-content: center; color: var(--muted); }
.chat-view { display: flex; flex-direction: column; height: 100%; }

.chat-header {
  background: var(--panel);
  padding: 12px 16px;
  border-bottom: 1px solid var(--border);
  display: flex;
  justify-content: space-between;
  align-items: center;
}
#chat-title { font-weight: 600; }
.chat-header-actions { display: flex; gap: 8px; }
#kb-toggle {
  border: 1px solid var(--border);
  background: white;
  border-radius: 8px;
  padding: 6px 10px;
  cursor: pointer;
  font-size: 13px;
}
#ticket-btn {
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 6px 10px;
  cursor: pointer;
  font-size: 13px;
}
#ticket-btn.open-ticket { background: #fff4e5; border-color: #f0b84f; color: #92620a; }
#ticket-btn.no-ticket { background: #f3f4f6; }

/* Dashboard */
.dashboard-view { flex: 1; overflow-y: auto; padding: 24px; background: var(--bg); }
.dashboard-view.hidden { display: none; }
.dash-summary { display: flex; gap: 16px; margin-bottom: 20px; flex-wrap: wrap; }
.dash-card { background: var(--panel); border-radius: 10px; padding: 16px 20px; min-width: 160px; box-shadow: 0 1px 2px rgba(0,0,0,0.06); }
.dash-card .dash-num { font-size: 24px; font-weight: 700; color: var(--green-dark); }
.dash-card .dash-label { font-size: 12px; color: var(--muted); margin-top: 4px; }
.dash-table { width: 100%; border-collapse: collapse; background: var(--panel); border-radius: 10px; overflow: hidden; }
.dash-table th, .dash-table td { text-align: left; padding: 10px 14px; border-bottom: 1px solid #f3f4f6; font-size: 13px; }
.dash-table th { background: #f9fafb; color: var(--muted); font-weight: 600; }
.dash-badge { display: inline-block; padding: 2px 8px; border-radius: 999px; font-size: 11px; font-weight: 600; }
.dash-badge.warn { background: #fff4e5; color: #92620a; }
.dash-badge.ok { background: #e7f8ef; color: var(--green-dark); }

/* Settings modal */
.modal { position: fixed; inset: 0; background: rgba(0,0,0,0.4); display: flex; align-items: center; justify-content: center; z-index: 50; }
.modal.hidden { display: none; }
.modal-card { background: var(--panel); border-radius: 12px; padding: 24px; width: 420px; max-height: 80vh; overflow-y: auto; }
.modal-card h3 { margin-top: 0; }
.settings-row { display: flex; justify-content: space-between; align-items: center; padding: 10px 0; border-bottom: 1px solid #f3f4f6; }
.settings-row select { padding: 6px; border-radius: 6px; border: 1px solid var(--border); }
#settings-close { margin-top: 12px; width: 100%; }

.messages {
  flex: 1;
  overflow-y: auto;
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 6px;
}
.bubble {
  max-width: 60%;
  padding: 8px 10px;
  border-radius: 10px;
  font-size: 14px;
  line-height: 1.4;
  box-shadow: 0 1px 1px rgba(0,0,0,0.08);
}
.bubble.in { align-self: flex-start; background: var(--bubble-in); }
.bubble.out { align-self: flex-end; background: var(--bubble-out); }
.bubble-meta { font-size: 10px; color: var(--muted); margin-top: 4px; text-align: right; }
.bubble-sender { font-size: 11px; font-weight: 600; color: var(--green-dark); margin-bottom: 2px; }

.composer {
  background: var(--panel);
  border-top: 1px solid var(--border);
  padding: 10px 16px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.composer textarea {
  width: 100%;
  min-height: 60px;
  resize: vertical;
  padding: 10px;
  border-radius: 8px;
  border: 1px solid var(--border);
  font-family: inherit;
  font-size: 14px;
}
.composer-actions { display: flex; justify-content: flex-end; gap: 8px; }
.btn-primary, .btn-secondary {
  border: none;
  border-radius: 8px;
  padding: 9px 16px;
  font-size: 13px;
  cursor: pointer;
}
.btn-primary { background: var(--green-dark); color: white; }
.btn-primary:hover { background: #0f6f61; }
.btn-secondary { background: #f3f4f6; color: var(--text); }
.btn-secondary:hover { background: #e5e7eb; }
.btn-primary:disabled, .btn-secondary:disabled { opacity: 0.6; cursor: default; }

.kb-panel {
  width: 300px;
  background: var(--panel);
  border-left: 1px solid var(--border);
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}
.kb-panel h3 { margin: 0; font-size: 15px; }
.kb-hint { font-size: 12px; color: var(--muted); margin: 0; }
.kb-panel textarea { flex: 1; min-height: 300px; padding: 10px; border-radius: 8px; border: 1px solid var(--border); font-family: inherit; font-size: 13px; }
.kb-status { font-size: 12px; color: var(--green-dark); min-height: 16px; margin: 0; }
