
* {
  box-sizing: border-box;
}

body {
  margin: 0;
  font-family: Arial, sans-serif;
  background: linear-gradient(
    135deg, #dbeafe, #e9d5ff, #f3e8ff
  );
  color: #1e293b;
}

.screen[hidden] {
  display: none !important;
}

/* HOME PAGE */

#home {
  min-height: 100vh;
  text-align: center;
  padding: 45px 20px;
}

.robot-logo {
  font-size: 75px;
  margin-bottom: 15px;
}

h1 {
  color: #1e3a8a;
  font-size: 32px;
}

p {
  color: #475569;
}

.mode {
  display: block;
  width: 90%;
  max-width: 350px;
  padding: 22px;
  margin: 18px auto;
  border: none;
  border-radius: 18px;
  color: white;
  font-size: 20px;
  font-weight: bold;
  cursor: pointer;
  box-shadow: 0 5px 12px #00000018;
}

.study {
  background: #2563eb;
}

.general {
  background: #16a34a;
}

.quiz {
  background: #9333ea;
}

/* PROFILE PAGE */

#profilePage {
  min-height: 100vh;
  text-align: center;
  padding: 30px 20px;
}

h2 {
  color: #1e3a8a;
}

.avatar-options {
  display: flex;
  justify-content: center;
  gap: 15px;
  margin: 30px 0;
}

.avatar-options button {
  font-size: 45px;
  padding: 15px;
  border: 2px solid #c7d2fe;
  border-radius: 20px;
  background: white;
  cursor: pointer;
}

.avatar-options button:focus {
  border-color: #2563eb;
  background: #dbeafe;
}

.small-note {
  font-size: 12px;
}

.continue {
  display: block;
  margin: 35px auto;
  padding: 16px 40px;
  background: #2563eb;
  color: white;
  border: none;
  border-radius: 14px;
  font-size: 18px;
}

.back {
  padding: 10px 16px;
  border: none;
  border-radius: 12px;
  background: #e0e7ff;
  cursor: pointer;
}

/* CHAT PAGE */

#chatPage {
  position: fixed;
  inset: 0;
  display: flex;
  flex-direction: column;
  background: #eef2ff;
}

.chat-header {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 15px;
  background: white;
}

.robot-small {
  font-size: 32px;
}

.chat-header small {
  display: block;
  color: #64748b;
}

#chat {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  padding: 15px;
}

.message-row {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  margin: 12px 0;
}

.user-row {
  justify-content: flex-end;
}

.message {
  max-width: 78%;
  padding: 13px;
  border-radius: 16px;
  overflow-wrap: anywhere;
}

.user {
  background: #2563eb;
  color: white;
}

.bot {
  background: white;
  color: #1e293b;
}

.avatar {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  object-fit: cover;
  flex-shrink: 0;
}

.chat-input {
  display: flex;
  gap: 8px;
  padding: 12px;
  padding-bottom: max(12px, env(safe-area-inset-bottom));
  background: white;
}

#question {
  flex: 1;
  min-width: 0;
  padding: 14px;
  border: 1px solid #cbd5e1;
  border-radius: 24px;
}

.chat-input button {
  width: 46px;
  height: 46px;
  border: none;
  border-radius: 50%;
  background: #2563eb;
  color: white;
  font-size: 20px;
}

.avatar-options button {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  flex: 1;
  min-width: 0;
  padding: 12px 6px;
}

.avatar-options button span {
  font-size: 40px;
}

.avatar-options button small {
  font-size: 12px;
  font-weight: bold;
  color: #1e3a8a;
}

.avatar-options {
  max-width: 400px;
  margin-left: auto;
  margin-right: auto;
}

/* FIX HOMEPAGE LAYOUT */

#home {
  display: block;
  width: 100%;
  min-height: 100vh;
  text-align: center;
  padding: 35px 20px;
}

#home .robot-logo {
  width: 110px;
  height: 110px;
  margin: 10px auto 15px;
}

#home .robot-image {
  display: block;
  width: 100%;
  height: 100%;
  object-fit: contain;
  border-radius: 20px;
}

#home h1 {
  font-size: 30px;
  margin: 15px 0;
}

#home > .mode {
  display: block;
  width: 90%;
  max-width: 350px;
  min-height: 65px;
  margin: 18px auto;
  padding: 18px;
  border: none;
  border-radius: 16px;
  color: white;
  font-size: 20px;
  font-weight: bold;
}

#home > .study {
  background: #2563eb;
}

#home > .general {
  background: #16a34a;
}

#home > .quiz {
  background: #9333ea;
}

/* Robot logo in chat header */
.robot-small-image {
  width: 42px;
  height: 42px;
  object-fit: contain;
  border-radius: 50%;
  background: white;
}

/* Robot logo beside AI messages */
.message-row .avatar {
  width: 36px;
  height: 36px;
  object-fit: contain;
  border-radius: 50%;
  background: white;
}


/* Study options: shown only in Study Mode */
.study-options { background: #fff; padding: 12px 16px; display: grid; gap: 7px; border-bottom: 1px solid #cbd5e1; max-height: 42vh; overflow-y: auto; }
.study-options[hidden] { display: none !important; }
.study-options label { font-weight: bold; font-size: 14px; color: #1e3a8a; }
.study-options select { width: 100%; padding: 9px; border: 1px solid #c7d2fe; border-radius: 10px; background: #f8faff; font-size: 15px; }

/* Coordinated mode colors: homepage and chat */
#home > .study { background: #2563eb; }
#home > .general { background: #f97316; }
#home > .quiz { background: #16a34a; }

body[data-mode="study"] #chatPage { --theme: #2563eb; --pale: #eff6ff; }
body[data-mode="general"] #chatPage { --theme: #f97316; --pale: #fff7ed; }
body[data-mode="quiz"] #chatPage { --theme: #16a34a; --pale: #f0fdf4; }
#chatPage { background: var(--pale, #eff6ff); }
#chatPage .chat-header { border-bottom: 3px solid var(--theme, #2563eb); }
#chatPage #modeTitle { color: var(--theme, #2563eb); }
#chatPage .message.user { background: var(--theme, #2563eb); }
#chatPage .chat-input button { background: var(--theme, #2563eb); }
#chatPage .back { color: var(--theme, #2563eb); }



/* CLEAN CHAT LAYOUT - replaces conflicting older history rules */
#chatPage:not([hidden]) {
  position: fixed;
  inset: 0;
  height: 100vh;
  height: 100dvh;
  width: 100%;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  isolation: isolate;
  background: var(--pale, #eff6ff);
}
#chatPage::before {
  content: "";
  position: absolute;
  inset: 0;
  z-index: 0;
  pointer-events: none;
  background-repeat: repeat;
  background-size: 235px 410px;
  background-position: 12px 18px;
  opacity: .13;
}
body[data-mode="study"] #chatPage::before { background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='390' height='680' viewBox='0 0 390 680'%3E%3Cg fill='none' stroke='%236b7280' stroke-width='2.5' stroke-linecap='round' stroke-linejoin='round'%3E%3Cg transform='translate(48 82) rotate(-18)'%3E%3Cellipse rx='31' ry='11' transform='rotate(45)'/%3E%3Cellipse rx='31' ry='11' transform='rotate(-45)'/%3E%3Ccircle r='5'/%3E%3C/g%3E%3Cg transform='translate(305 205) rotate(14)'%3E%3Cpath d='M-16-30h32m-24 0v23l-15 26q-4 10 7 10h32q11 0 7-10L8-7v-23M-19 13h38'/%3E%3C/g%3E%3Cg transform='translate(85 365) rotate(-13)'%3E%3Cpath d='M-28-24q14-9 28 0v50q-14-9-28 0zM0-24q14-9 28 0v50q-14-9-28 0z'/%3E%3C/g%3E%3Cg transform='translate(296 465) rotate(17)'%3E%3Ccircle r='27'/%3E%3Cpath d='M0-16v18l14 8'/%3E%3C/g%3E%3Cg transform='translate(95 580) rotate(-12)'%3E%3Cpath d='M-24-18h48M-18-29v58M-26 18l52-36'/%3E%3C/g%3E%3Cg transform='translate(340 625) rotate(20)'%3E%3Cpath d='M-19-23h38v46h-38zM-10-8h20M-10 3h20M-10 14h13'/%3E%3C/g%3E%3C/g%3E%3C/svg%3E"); }
body[data-mode="general"] #chatPage::before { background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='390' height='680' viewBox='0 0 390 680'%3E%3Cg fill='none' stroke='%236b7280' stroke-width='2.5' stroke-linecap='round' stroke-linejoin='round'%3E%3Cg transform='translate(315 80) rotate(-20)'%3E%3Ccircle r='23'/%3E%3Cellipse rx='38' ry='11'/%3E%3C/g%3E%3Cg transform='translate(67 215) rotate(12)'%3E%3Ccircle r='28'/%3E%3Cellipse rx='12' ry='28'/%3E%3Cpath d='M-28 0h56M-23-14h46M-23 14h46'/%3E%3C/g%3E%3Cg transform='translate(305 315) rotate(18)'%3E%3Cpath d='M0-27l7 19 20 8-20 7-7 20-7-20-20-7 20-8z'/%3E%3C/g%3E%3Cg transform='translate(92 445) rotate(-15)'%3E%3Cpath d='M-21-8a21 21 0 1 1 42 0q0 14-11 23v9h-20v-9q-11-9-11-23M-10 31h20M-6 37h12'/%3E%3C/g%3E%3Cg transform='translate(305 580) rotate(25)'%3E%3Cpath d='M-25-13l18 7 7-18 7 18 18-7-12 17 12 16-20-5-5 20-5-20-20 5 12-16z'/%3E%3C/g%3E%3C/g%3E%3C/svg%3E"); }
body[data-mode="quiz"] #chatPage::before { background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='390' height='680' viewBox='0 0 390 680'%3E%3Cg fill='none' stroke='%236b7280' stroke-width='2.5' stroke-linecap='round' stroke-linejoin='round'%3E%3Cg transform='translate(65 100) rotate(-17)'%3E%3Cpath d='M-15-15q0-20 17-20t17 17q0 13-17 22v12M2 29h1'/%3E%3C/g%3E%3Cg transform='translate(300 205) rotate(13)'%3E%3Cpath d='M-23-25h46v30q0 23-23 23T-23 5zM-23-15h-12v13q0 16 17 16M23-15h12v13q0 16-17 16M0 28v14M-15 42h30'/%3E%3C/g%3E%3Cg transform='translate(85 385) rotate(-12)'%3E%3Cpath d='M-27-34h54v68h-54zM-17-18l7 7 12-14M5-14h12M-17 7l7 7 12-14M5 11h12'/%3E%3C/g%3E%3Cg transform='translate(305 475) rotate(18)'%3E%3Cpath d='M-14-18q0-17 15-17t15 15q0 11-15 20v11M1 25h1'/%3E%3C/g%3E%3Cg transform='translate(110 605) rotate(-14)'%3E%3Ccircle r='27'/%3E%3Cpath d='M-12 0l8 9 17-20'/%3E%3C/g%3E%3C/g%3E%3C/svg%3E"); }

#chatPage > .chat-header,
#chatPage > .study-options,
#chatPage > #chat,
#chatPage > .chat-input { position: relative; z-index: 1; flex-shrink: 0; }
#chatPage .chat-header {
  display: flex;
  gap: 10px;
  align-items: center;
  min-height: 76px;
  padding: 12px 14px;
}
#chatPage .chat-header > div { min-width: 0; flex: 1; }
#chatPage #modeTitle { font-size: clamp(17px, 4.6vw, 22px); }
#chatPage #chat {
  flex: 1 1 auto;
  min-height: 0;
  overflow-y: auto;
  padding: 16px;
  overscroll-behavior: contain;
}
#chatPage .chat-input { flex-shrink: 0; }
#chatPage .message { white-space: pre-wrap; line-height: 1.45; }
#chatPage .history-button {
  margin-left: auto;
  flex: 0 0 44px;
  width: 44px;
  height: 44px;
  border: 0;
  border-radius: 50%;
  display: grid;
  place-items: center;
  color: white;
  background: var(--theme, #2563eb);
  font-size: 23px;
  box-shadow: 0 3px 12px #0002;
  cursor: pointer;
}
#chatPage input:focus, #chatPage select:focus {
  outline: 2px solid #222;
  outline-offset: 0;
  border-color: #222;
  box-shadow: none;
}
#chatPage #historyBackdrop[hidden],
#chatPage #historyPanel[hidden] { display: none !important; }
#chatPage #historyBackdrop {
  position: fixed;
  inset: 0;
  z-index: 100;
  background: rgba(15,23,42,.36);
}
#chatPage #historyPanel {
  position: fixed;
  top: 0;
  right: 0;
  bottom: 0;
  left: auto;
  width: min(78vw, 360px);
  height: 100vh;
  height: 100dvh;
  z-index: 101;
  display: flex;
  flex-direction: column;
  gap: 12px;
  padding: max(22px, env(safe-area-inset-top)) 18px 22px;
  overflow-y: auto;
  background: #fff;
  box-shadow: -8px 0 25px #0002;
  animation: historyEnter .22s ease-out;
}
@keyframes historyEnter { from { transform: translateX(100%); } to { transform: translateX(0); } }
#historyPanel .history-panel-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 8px;
}
#historyPanel h3 { margin: 0; font-size: 21px; }
#historyPanel button {
  cursor: pointer;
  border: 0;
  border-radius: 12px;
  padding: 12px;
  font-size: 15px;
  background: #f1f5f9;
  color: #1e293b;
}
#historyPanel .history-panel-header button { font-size: 20px; padding: 7px 12px; }
#historyPanel .new-chat-button { text-align: left; font-weight: 700; }
#historyList { display: flex; flex-direction: column; gap: 5px; }
#historyList button {
  text-align: left;
  background: transparent;
  border-radius: 10px;
  padding: 11px;
  width: 100%;
  overflow-wrap: anywhere;
}
#historyList button:hover, #historyList button:focus { background: #eef2ff; }
#historyList button.active-history { background: #e8efff; font-weight: 700; }
.history-muted { color: #64748b; font-size: 14px; margin: 8px 4px; }
@media (min-width: 800px) {
  #chatPage .chat-header, #chatPage .study-options,
  #chatPage #chat, #chatPage .chat-input { padding-left: max(20px, calc((100vw - 900px)/2)); padding-right: max(20px, calc((100vw - 900px)/2)); }
}

/* Chat history conversation rows */
.history-chat-row {
  display: flex;
  align-items: center;
  gap: 8px;
  margin: 8px 0;
}

.history-chat-name {
  flex: 1;
  min-width: 0;
  text-align: left;
  border: none;
  border-radius: 12px;
  padding: 13px;
  background: transparent;
  color: #243047;
  font-size: 15px;
  overflow-wrap: anywhere;
}

.history-chat-name.active {
  background: #e9efff;
}

.history-chat-menu {
  flex-shrink: 0;
  width: 40px;
  height: 40px;
  border: none;
  border-radius: 50%;
  background: #e9efff;
  color: #2563eb;
  font-size: 23px;
  cursor: pointer;
}

/* FIX: Horizontal chat history rows */

#historyList .history-chat-row {
  display: flex !important;
  flex-direction: row !important;
  align-items: center !important;
  width: 100% !important;
  gap: 8px !important;
  margin: 6px 0 !important;
}

#historyList .history-chat-name {
  display: block !important;
  flex: 1 1 0 !important;
  width: auto !important;
  min-width: 0 !important;
  max-width: none !important;
  text-align: left !important;
  white-space: nowrap !important;
  overflow: hidden !important;
  text-overflow: ellipsis !important;
  overflow-wrap: normal !important;
  word-break: normal !important;
  writing-mode: horizontal-tb !important;
  padding: 12px !important;
  font-size: 15px !important;
  border-radius: 12px !important;
}

#historyList .history-chat-menu {
  display: flex !important;
  flex: 0 0 42px !important;
  width: 42px !important;
  height: 42px !important;
  align-items: center !important;
  justify-content: center !important;
  border: none !important;
  border-radius: 50% !important;
  background: #e9efff !important;
  color: #2563eb !important;
  font-size: 24px !important;
  cursor: pointer !important;
}

/* Chat history Rename / Delete popup */

.chat-options-menu {
    z-index: 99999;
    width: 170px;
    padding: 7px;
    background: white;
    border: 1px solid #e5e7eb;
    border-radius: 15px;
    box-shadow: 0 8px 25px rgba(0,0,0,0.15);
    display: flex;
    flex-direction: column;
    gap: 4px;
}

.chat-options-menu button {
    display: block;
    width: 100%;
    padding: 12px;
    text-align: left;
    font-size: 15px;
    color: #1e293b;
    background: transparent;
    border: none;
    border-radius: 9px;
    cursor: pointer;
}

.chat-options-menu button:hover {
    background: #f1f5f9;
}

.chat-options-menu button:last-child {
    color: #dc2626;
}

/* Custom Rename and Delete dialogs */

.custom-chat-overlay {
    position: fixed;
    inset: 0;
    z-index: 999999;
    background: rgba(15, 23, 42, 0.45);
    display: flex;
    justify-content: center;
    align-items: center;
    padding: 20px;
}

.custom-chat-dialog {
    width: 100%;
    max-width: 380px;
    background: #ffffff;
    color: #172033;
    padding: 24px;
    border-radius: 20px;
    box-shadow: 0 15px 45px rgba(0,0,0,0.2);
    box-sizing: border-box;
}

.custom-chat-dialog h3 {
    margin: 0 0 16px;
    font-size: 20px;
    color: #172033;
}

.custom-chat-dialog p {
    font-size: 15px;
    color: #334155;
    line-height: 1.5;
}

.rename-chat-input {
    display: block;
    width: 100%;
    box-sizing: border-box;
    padding: 13px;
    font-size: 16px;
    color: #111827;
    background: white;
    border: 2px solid #2563eb;
    border-radius: 11px;
    outline: none;
}

.custom-chat-actions {
    display: flex;
    justify-content: flex-end;
    gap: 10px;
    margin-top: 22px;
}

.custom-chat-actions button {
    padding: 11px 20px;
    border-radius: 10px;
    border: none;
    font-size: 15px;
    font-weight: bold;
    cursor: pointer;
}

.chat-cancel-btn {
    background: #e5e7eb;
    color: #111827;
}

.chat-save-btn {
    background: #2563eb;
    color: #ffffff;
}

.chat-delete-btn {
    background: #dc2626;
    color: #ffffff;
}



/* Align chat avatars with the first line */
.message-row {
  display: flex;
  align-items: flex-start;
  gap: 10px;
}

.message-row .avatar {
  flex-shrink: 0;
  align-self: flex-start;
}

/* Keep mathematical equations inside chat bubbles */

.message {
  min-width: 0;
  max-width: 100%;
  overflow-wrap: break-word;
}

.message .katex-display {
  max-width: 100%;
  overflow-x: auto;
  overflow-y: hidden;
  padding: 8px 0;
  font-size: clamp(0.65rem, 2.5vw, 1rem);
}

.message .katex {
  max-width: 100%;
}

