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

html, body {
  height: 100%;
}

body {
  font-family: Arial, "Noto Sans Khmer", "Khmer OS", sans-serif;
  background: #101426;
  color: #ffffff;
  -webkit-text-size-adjust: 100%;
}

.hidden {
  display: none !important;
}

button {
  font-family: inherit;
  cursor: pointer;
}

input, textarea, select {
  font-family: inherit;
  font-size: 16px;
}

.logo {
  font-size: 22px;
  font-weight: bold;
  color: #9b8cff;
}

/* ===== Auth ===== */
#auth-screen {
  min-height: 100dvh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
}

.auth-card {
  width: 100%;
  max-width: 400px;
  background: #1b2238;
  border: 1px solid #343d59;
  border-radius: 20px;
  padding: 24px 20px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.auth-card h2 {
  font-size: 20px;
}

.auth-card input,
.auth-card select,
.sidebar-bottom select {
  width: 100%;
  background: #29324b;
  color: #fff;
  border: 1px solid #48516d;
  border-radius: 10px;
  padding: 12px;
}

.msg-info,
.msg-error {
  font-size: 14px;
  line-height: 1.5;
  min-height: 0;
}

.msg-info { color: #55d6a0; }
.msg-error { color: #ff8a8a; }

.primary-btn {
  background: #8069ff;
  color: #fff;
  border: none;
  border-radius: 12px;
  padding: 13px 20px;
  font-size: 16px;
  font-weight: bold;
}

.primary-btn:disabled {
  opacity: .6;
}

.primary-btn:active {
  transform: scale(.98);
}

.link-btn {
  background: none;
  border: none;
  color: #a493ff;
  font-size: 14px;
  padding: 6px;
}

/* ===== App layout ===== */
#app-screen {
  height: 100dvh;
  display: flex;
  overflow: hidden;
}

#sidebar {
  position: fixed;
  top: 0;
  left: 0;
  bottom: 0;
  width: 280px;
  max-width: 85vw;
  background: #141a30;
  border-right: 1px solid #293047;
  display: flex;
  flex-direction: column;
  transform: translateX(-100%);
  transition: transform .2s ease;
  z-index: 30;
  padding-top: env(safe-area-inset-top);
}

#sidebar.open {
  transform: translateX(0);
}

#overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, .5);
  z-index: 20;
}

.sidebar-top {
  display: flex;
  gap: 8px;
  padding: 12px;
}

.sidebar-top .primary-btn {
  flex: 1;
  padding: 11px;
  font-size: 15px;
}

.icon-btn {
  background: #29324b;
  color: #fff;
  border: 1px solid #48516d;
  border-radius: 10px;
  width: 42px;
  height: 42px;
  font-size: 18px;
}

#conversation-list {
  flex: 1;
  overflow-y: auto;
  padding: 0 8px;
}

.conv-item {
  display: flex;
  align-items: center;
  border-radius: 10px;
  margin-bottom: 4px;
}

.conv-item.active {
  background: #29324b;
}

.conv-title {
  flex: 1;
  background: none;
  border: none;
  color: #fff;
  text-align: left;
  padding: 11px 10px;
  font-size: 14px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.conv-delete {
  background: none;
  border: none;
  color: #8992aa;
  padding: 10px;
  font-size: 14px;
}

.empty-note {
  color: #8992aa;
  font-size: 13px;
  padding: 12px;
  text-align: center;
}

.sidebar-bottom {
  padding: 12px;
  padding-bottom: calc(12px + env(safe-area-inset-bottom));
  border-top: 1px solid #293047;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

#user-label {
  color: #aeb5ca;
  font-size: 13px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.sidebar-bottom .link-btn {
  text-align: left;
  color: #ff8a8a;
}

/* ===== Chat ===== */
#chat-panel {
  flex: 1;
  min-width: 0;
  height: 100dvh;
  display: flex;
  flex-direction: column;
}

.chat-header {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 14px;
  padding-top: calc(12px + env(safe-area-inset-top));
  border-bottom: 1px solid #293047;
}

#messages {
  flex: 1;
  overflow-y: auto;
  padding: 16px 14px;
}

.welcome {
  color: #aeb5ca;
  text-align: center;
  margin-top: 25vh;
  line-height: 1.7;
  padding: 0 20px;
}

.msg {
  max-width: 88%;
  padding: 10px 14px;
  border-radius: 16px;
  margin-bottom: 12px;
  line-height: 1.65;
  word-wrap: break-word;
  overflow-wrap: anywhere;
}

.msg.user {
  background: #8069ff;
  margin-left: auto;
  border-bottom-right-radius: 4px;
}

.msg.assistant {
  background: #1b2238;
  border: 1px solid #343d59;
  border-bottom-left-radius: 4px;
}

.msg.thinking {
  background: #1b2238;
  border: 1px solid #343d59;
  color: #8992aa;
}

.msg.error {
  background: #3a1d28;
  border: 1px solid #7a3345;
  color: #ffb3b3;
}

.msg p {
  white-space: pre-wrap;
}

.msg p + p,
.msg pre + p,
.msg p + pre {
  margin-top: 8px;
}

.msg pre {
  background: #0b0f1e;
  border-radius: 10px;
  padding: 10px;
  overflow-x: auto;
  font-size: 13px;
  line-height: 1.5;
}

.composer {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  padding: 10px 12px;
  padding-bottom: calc(10px + env(safe-area-inset-bottom));
  border-top: 1px solid #293047;
}

.composer textarea {
  flex: 1;
  background: #1b2238;
  color: #fff;
  border: 1px solid #343d59;
  border-radius: 14px;
  padding: 11px 13px;
  resize: none;
  outline: none;
  max-height: 150px;
  line-height: 1.4;
}

.composer textarea::placeholder {
  color: #8992aa;
}

.composer .primary-btn {
  width: 46px;
  height: 46px;
  padding: 0;
  font-size: 20px;
}

@media (min-width: 800px) {
  #sidebar {
    position: static;
    transform: none;
    flex-shrink: 0;
  }

  #overlay,
  #menu-btn,
  #close-sidebar {
    display: none !important;
  }

  #messages {
    padding: 20px 8%;
  }
}
