* {
  box-sizing: border-box;
}
html, body {
  margin: 0;
  padding: 0;
  width: 100%;
  max-width: 100vw;
  overflow-x: hidden;
  height: 100vh;
}
body {
  font-family: "Segoe UI", -apple-system, BlinkMacSystemFont, sans-serif;
  background: #fff;
}
.container {
  width: 100%;
  height: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 20px;
}

/* --- Auth Section --- */
.auth-section {
  background: #f2f2f2;
  padding: 40px 32px;
  border-radius: 12px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.08);
  text-align: center;
  width: 100%;
  max-width: 420px;
}
.auth-section h1 {
  font-size: 28px;
  margin-bottom: 40px;
  color: #333;
}
.auth-section input[type="password"] {
  width: 100%;
  padding: 16px;
  margin-bottom: 20px;
  border: 1px solid #ccc;
  border-radius: 8px;
  font-size: 18px;
  outline: none;
}
.auth-section input[type="password"]:focus {
  border-color: #10a37f;
}
.auth-section button {
  width: 100%;
  padding: 16px;
  background: #10a37f;
  color: white;
  border: none;
  border-radius: 8px;
  font-size: 18px;
  cursor: pointer;
}
.auth-section button:hover {
  background: #0d8a6a;
}
#authError {
  color: #dc3545;
  margin-top: 16px;
  font-size: 16px;
  min-height: 24px;
}

/* --- Main Layout --- */
.main-app {
  width: 100%;
  height: 100%;
  display: none;
  flex-direction: row;
}
.main-app:not(.hidden) {
  display: flex !important;
}
.hidden {
  display: none !important;
}

#sidebar {
  width: 300px;
  background: #f7f7f8;
  border-right: 1px solid #e5e5e5;
  display: flex;
  flex-direction: column;
  flex-shrink: 0;
  transition: margin-left 0.3s ease;
  margin-left: 0;
}
#sidebar.closed {
  margin-left: -300px;
}
.sidebar-header {
  padding: 16px;
  font-weight: 600;
  font-size: 16px;
  color: #333;
  border-bottom: 1px solid #e5e5e5;
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.sidebar-header button {
  background: none;
  border: none;
  font-size: 18px;
  cursor: pointer;
  color: #666;
  padding: 4px 8px;
  border-radius: 4px;
}
.sidebar-header button:hover {
  background: #e5e5e5;
}
.logout-btn {
  background: #dc3545 !important;
  color: white !important;
  padding: 6px 12px !important;
  border-radius: 6px !important;
  font-size: 14px !important;
}
.logout-btn:hover {
  background: #c82333 !important;
}

/* --- Chat List --- */
#list {
  flex: 1;
  overflow-y: auto;
  padding: 8px;
}
.chat-item {
  padding: 12px 14px;
  cursor: pointer;
  border-radius: 8px;
  margin-bottom: 4px;
  background: #fff;
  transition: all 0.15s ease;
}
.chat-item:hover {
  background: #ececec;
}
.chat-item.active {
  background: #10a37f;
}
.chat-item.active .chat-title,
.chat-item.active .chat-time {
  color: #fff;
}
.chat-title {
  font-weight: 500;
  color: #333;
  margin-bottom: 4px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  font-size: 14px;
}
.chat-time {
  font-size: 11px;
  color: #888;
}

/* --- Chat Panel --- */
#chatPanel {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  background: #fff;
  min-width: 0;
}
#chatHeader {
  padding: 12px 16px;
  border-bottom: 1px solid #e5e5e5;
  display: flex;
  flex-direction: column;
  gap: 8px;
  background: #fff;
}
.header-row {
  display: flex;
  align-items: center;
  gap: 10px;
}
#backBtn {
  background: none;
  border: 1px solid #e5e5e5;
  font-size: 13px;
  cursor: pointer;
  color: #333;
  padding: 6px 10px;
  border-radius: 6px;
  flex-shrink: 0;
  display: none;
}
#backBtn:hover {
  background: #f0f0f0;
}
#backBtn.show {
  display: inline-block;
}
#chatTitle {
  font-weight: 600;
  font-size: 16px;
  color: #333;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  flex-shrink: 1;
  min-width: 0;
}
#searchBox {
  display: flex;
  align-items: center;
  gap: 6px;
  flex: 1;
}
#searchInput {
  padding: 8px 12px;
  border: 1px solid #e5e5e5;
  border-radius: 6px;
  font-size: 14px;
  width: 350px;
  outline: none;
}
#searchInput:focus {
  border-color: #10a37f;
}
#clearBtn {
  background: #dc3545;
  border: none;
  color: #fff;
  padding: 8px 10px;
  border-radius: 6px;
  cursor: pointer;
  font-size: 14px;
  flex-shrink: 0;
  display: none;
}
#clearBtn:hover {
  background: #c82333;
}
#clearBtn.show {
  display: inline-block;
}
#loading {
  padding: 12px 20px;
  color: #888;
  font-size: 13px;
}

/* --- Chat container --- */
#chat {
  flex: 1;
  padding: 20px;
  overflow-y: auto;
  overflow-x: hidden;
  display: flex;
  flex-direction: column;
  gap: 32px;
}

/* --- Messages --- */
.message {
  max-width: 80%;
  width: fit-content;
  padding: 16px 20px;
  border-radius: 20px;
  line-height: 1.7;
  word-wrap: break-word;
  font-size: 15px;
  box-shadow: 0 1px 3px rgba(0,0,0,0.1);
}

/* User - bên trái */
.message.user {
  align-self: flex-start;
  background: #f5f5f5;
  color: #333;
  border-bottom-left-radius: 4px;
  margin-right: 10%;
}

/* Assistant - lệch phải “vừa phải” */
.message.assistant {
  align-self: flex-end;
  background: #e0f7fa;
  color: #333;
  border-bottom-right-radius: 4px;
  margin-left: 5%;
}

/* Sources box - cùng phía với assistant */
.sources-section {
  align-self: flex-end;
  max-width: 80%;
  margin-left: 5%;
  margin-top: 12px;
  padding: 14px 18px;
  background: #f0f8ff;
  border-radius: 12px;
  border: 1px solid #b0e0e6;
  font-size: 14px;
  line-height: 1.6;
}

.message-role {
  font-size: 10px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  margin-bottom: 8px;
  color: #10a37f;
}
.message.user .message-role {
  color: #666;
}
.message-role.perplexity {
  color: #00695c;
}

.sources-section h3 {
  font-size: 13px !important;
  font-weight: 700 !important;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  color: #00695c !important;
  margin: 0 0 10px 0 !important;
}
.reference-item {
  margin-bottom: 6px;
}
.reference-item a {
  color: #00695c;
  word-break: break-all;
}

/* Inline citations */
.citation {
  color: #00695c;
  text-decoration: none;
  font-size: 11px;
  font-weight: 600;
  vertical-align: super;
  margin: 0 2px;
  background: #b0e0e6;
  padding: 2px 5px;
  border-radius: 4px;
}
.citation:hover {
  background: #00695c;
  color: white;
}

/* Table */
.chat-table {
  width: 100%;
  border-collapse: collapse;
  margin: 16px 0;
  font-size: 14px;
}
.chat-table th,
.chat-table td {
  border: 1px solid #b0e0e6;
  padding: 10px 12px;
  text-align: left;
}
.chat-table th {
  background-color: #e0f7fa;
  font-weight: 600;
}

/* Typography */
.message h2, .message h3, .message h4, .message h5 {
  margin: 16px 0 8px 0;
  font-weight: 600;
  color: #222;
}
.message code {
  background: #f0f0f0;
  padding: 2px 6px;
  border-radius: 4px;
  font-family: Consolas, monospace;
  font-size: 13px;
}
.message pre {
  background: #f0f0f0;
  padding: 12px;
  border-radius: 6px;
  overflow-x: auto;
  margin: 10px 0;
}

/* Mobile */
@media (max-width: 768px) {
  #chat {
    gap: 24px;
    padding: 16px;
  }
  .message, .sources-section {
    max-width: 90%;
  }
  .message.user {
    margin-right: 5%;
  }
  .message.assistant, .sources-section {
    margin-left: 3%;
  }
}
