/* 공통 배경 & 리셋은 /css/base.css 에서 처리 */

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  background-color: #ffffff;  /* 💡 완전한 흰색으로 설정 */
  font-family: var(--font, sans-serif);  /* 폰트 지정 */
}

#toggle-sidebar {
  all: unset; /* 👉 브라우저 및 글로벌 스타일 초기화 */
  position: fixed;
  top: 96px;
  left: 16px;
  z-index: 1100;
  background-color: white;
  color: #333;
  border: 1px solid #ccc;
  border-radius: 6px;
  padding: 0.5rem;
  cursor: pointer;
  transition: left 0.3s ease;
  font-size: 1.4rem;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 30px;
  height: 30px;
}

#toggle-icon {
  line-height: 36px;
}

/* 사이드바 전체 */
#sidebar {
  position: fixed;
  top: 0;
  left: 0;
  width: 240px;
  height: 100%;
  background-color: #f9f9f9;
  border-right: 1px solid #ddd;
  padding: 1rem;
  box-shadow: 2px 0 8px rgba(0,0,0,0.05);
  transform: translateX(-100%);
  transition: transform 0.3s ease;
  z-index: 1000;
  overflow-y: auto;

  scrollbar-width: thin; /* Firefox용 */
  scrollbar-color: rgba(0, 0, 0, 0.05) transparent;
}

.has-app-header #sidebar {
  top: 54px; /* 헤더 아래에서 10px 위로 */
  height: calc(100% - 54px);
}

/* Chrome, Edge, Safari용 */
#sidebar::-webkit-scrollbar {
  width: 6px;
}

#sidebar::-webkit-scrollbar-thumb {
  background-color: rgba(0, 0, 0, 0.2);
  border-radius: 4px;
}

#sidebar::-webkit-scrollbar-track {
  background: transparent;
}

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

#sidebar h2 {
  margin-top: 0;
  font-size: 1.2rem;
  color: #333;
}

.sidebar-header {
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
}

.sidebar-subject {
  margin: 0;
  color: #1e293b;
  font-size: 1rem;
  font-weight: 600;
  letter-spacing: 0.02em;
  text-transform: none;
}

#sidebar ul {
  list-style: none;
  padding: 0;
  margin-top: 1rem;
}

.sidebar-skeleton {
  height: 16px;
  margin: 0.5rem 0;
  border-radius: 6px;
}

#sidebar li {
  margin: 0.5rem 0;
  padding: 0.5rem;
  cursor: pointer;
  border-radius: 6px;
  transition: background 0.2s;
}

#sidebar li:hover {
  background-color: #eee;
}

.chapter-title {
  font-weight: bold;
  color: #444;
  background: #e0e0e0;
  padding: 0.4rem 0.6rem;
  margin-top: 1rem;
  border-radius: 6px;
  cursor: default;
  /* 긴 텍스트 처리 */
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  position: relative;
}

/* 마우스 호버 시 전체 텍스트 표시 */
.chapter-title:hover::after,
.subchapter:hover::after {
  content: attr(data-full-title);
  position: absolute;
  left: 0;
  top: 100%;
  margin-top: 4px;
  background: rgba(0, 0, 0, 0.9);
  color: white;
  padding: 6px 10px;
  border-radius: 4px;
  white-space: normal;
  word-wrap: break-word;
  max-width: 300px;
  z-index: 1000;
  font-size: 0.85rem;
  font-weight: normal;
  box-shadow: 0 2px 8px rgba(0,0,0,0.3);
  animation: fadeIn 0.2s;
}

@keyframes fadeIn {
  from { opacity: 0; transform: translateY(-4px); }
  to { opacity: 1; transform: translateY(0); }
}

.subchapter {
  padding: 0.4rem 0.6rem;
  margin-left: 1rem;
  cursor: pointer;
  border-radius: 6px;
  transition: background 0.2s;
  /* 긴 텍스트 처리 */
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  position: relative;
}

.subchapter:hover {
  background-color: #eaeaea;
}

.subchapter.active {
  background:#007aff;
  color:#fff;
}

/* ---- 채팅창 ---- */
#chat-container {
  flex: 1;
  display: flex;
  flex-direction: column;
  max-width: 800px;
  margin: 0 auto;
  width: 100%;
  padding: 2rem 1rem;
  margin-bottom: 100px; /* 입력창 높이만큼 여백 확보 */
}

#chat-window {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
  padding-bottom: 120px; /* 입력창 기본 여유 - JavaScript로 동적 업데이트됨 */
  /* max-height 제거하여 페이지 전체 스크롤 사용 */
}

#chat-window img {
  max-width: 100%;
  height: auto;
  border-radius: 10px;
  display: block;
  margin: 0.5rem 0;
  box-shadow: 0 8px 24px rgba(15, 23, 42, 0.12);
}

.loading-placeholder {
  text-align: center;
  color: #60748a;
  padding: 1.5rem 0;
}

.chat-skeleton {
  height: 90px;
  border-radius: 16px;
  margin-right: auto;
  width: 90%;
}

.chat-skeleton:nth-child(2) {
  height: 70px;
  margin-left: auto;
  margin-right: 0;
  width: 85%;
}

.chat-skeleton:nth-child(3) {
  height: 60px;
  width: 70%;
}

form {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  max-width: 800px;
  margin: 0 auto;
  padding: 1rem;
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  border-top: 1px solid #eee;
  background: white;
  z-index: 100;
  align-items: flex-end;
}

.input-area textarea {
  flex: 1;
  padding: 0.4rem 1rem 0.3rem 1rem;
  font-size: 1rem;
  border-radius: 20px;
  border: 1px solid #ccc;
  outline: none;
  resize: none;
  line-height: 1.5;
  font-family: inherit;
  min-width: 0;

  height: 30px;
  overflow-y: hidden;
}

.input-area textarea:focus {
  border-color: #007aff;
}

form button[type="submit"] {
  border-radius: 20px;
  background-color: #007aff;
  color: white;
  font-weight: bold;
  border: none;
  cursor: pointer;
  padding: 0.5rem 1rem;
  min-height: 50px;
  flex: 0 0 auto;
  align-self: flex-end;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
}

form button[type="submit"] .btn-text {
  font-size: 1rem;
  line-height: 1;
}

form button[type="submit"] .btn-shortcut {
  font-size: 0.65rem;
  font-weight: normal;
  opacity: 0.85;
  line-height: 1;
}

.btn-reset {
  background-color: #e3eaf3;
  color: #1e2a38;
  border: none;
  border-radius: 999px;
  font-weight: 600;
  padding: 0.45rem 1.1rem;
  cursor: pointer;
  transition: background-color 0.2s ease, transform 0.12s ease;
  height: 50px;
  display: inline-flex;
  align-items: center;
  gap: 0.35rem;
  flex: 0 0 auto;
  align-self: flex-end;
  white-space: nowrap;
}

.input-area {
  flex: 1 1 240px;
  display: flex;
  gap: 0.5rem;
  align-items: flex-end;
  min-width: 0;
}

form button[type="submit"]:hover {
  background-color: #0060dd;
}

.btn-reset:hover {
  background-color: #cfd9ea;
  transform: translateY(-1px);
}

.btn-reset:disabled {
  opacity: 0.6;
  cursor: default;
}

.reset-label-compact {
  display: none;
}



/* ---- 반응형: 작은 화면에서 사이드바 오버레이 ---- */
@media (max-width: 768px) {
  #chat-container { margin-left: 0; }
  #sidebar.open { transform: translateX(0); }
  /* 채팅창 좁은 화면 대응 */
  #chat-container {
    padding: 1rem 0.5rem;
    font-size: 0.95rem;
  }

  #chat-window {
    padding-bottom: 110px;  /* 모바일 입력창 높이 고려 - JavaScript로 동적 업데이트됨 */
  }

  form {
    padding: 0.5rem;
    gap: 0.35rem 0.5rem;
    align-items: stretch;
  }

  .input-area {
    gap: 0.35rem;
  }

  textarea {
    font-size: 0.9rem;
    padding: 0.3rem 0.8rem;
  }

  form button[type="submit"] {
    padding: 0.4rem 0.8rem;
    min-height: 44px;
  }

  .btn-reset {
    padding: 0.35rem 0.9rem;
    font-size: 0.95rem;
    height: 44px;
  }

  .lesson-editor-btn {
    top: 96px; /* 모바일에서도 헤더를 가리지 않게 */
    right: 12px;
  }

  form button[type="submit"] .btn-text {
    font-size: 0.9rem;
  }

  form button[type="submit"] .btn-shortcut {
    font-size: 0.6rem;
  }

  .assistant, .user {
    font-size: 0.95rem;
    max-width: 90%;
    padding: 0.5rem 0.8rem;
  }
}

@media (max-width: 640px) {
  form {
    align-items: stretch;
  }

  .input-area {
    width: 100%;
  }

  .btn-reset {
    order: 3;
    width: 100%;
    justify-content: center;
    align-self: stretch;
    height: 40px;
  }

  .reset-label-full {
    display: none;
  }

  .reset-label-compact {
    display: inline;
  }
}

@media (min-width: 769px) and (max-width: 1024px) {
  #chat-container {
    padding: 1.5rem 1rem;
    font-size: 1rem;
  }

  .assistant, .user {
    font-size: 1rem;
    max-width: 85%;
  }
}

/* ---- 코드 블록 스타일 ---- */
pre code {
  background: #2d2d2d;
  color: #f8f8f2;
  display: block;
  padding: 12px;
  border-radius: 6px;
  overflow-x: auto;
}
.assistant pre {
  margin: 0; /* remove default top/bottom gaps around code blocks */
}

/* assistant 메시지 스타일 */
.assistant {
  align-self: flex-start;
  background-color: #f1f1f1;
  color: #000000;
  max-width: 80%;
  padding: 1.3rem 1rem 0rem; /* further increase top, trim bottom */
  border-radius: 18px;
  font-size: 1rem;
  line-height: 1.45;
  word-break: break-word;
  white-space: pre-wrap; /* keep indentation/newlines for problem text */
}
.assistant h1,
.assistant h2,
.assistant h3,
.assistant h4,
.assistant h5,
.assistant h6 {
  margin: 0.25em 0 0.2em;
  font-size: 1.05em;
  font-weight: 700;
}
.assistant p {
  margin: 0.2em 0;
}
.assistant p:first-child { margin-top: 0; }
.assistant p:last-child { margin-bottom: 0; }
.assistant ul,
.assistant ol {
  margin: 0.15em 0 0.25em 1.1em;
  padding-left: 1em;
}
.assistant li {
  margin: 0.08em 0;
}

/* 복사 방지 클래스 (학생만 적용) */
.assistant.no-copy {
  user-select: none;
  -webkit-user-select: none;
  -moz-user-select: none;
  -ms-user-select: none;
}

/* user 메시지 스타일도 추가해두면 안정적 */
.user {
  align-self: flex-end;
  background-color: #007aff;
  color: #ffffff;
  max-width: 80%;
  padding: 0.5rem 1rem;
  border-radius: 18px;
  font-size: 1rem;
  line-height: 1.6;
  word-break: break-word;
  white-space: pre-wrap; /* 공백과 들여쓰기 보존 (코드 입력용) */
}

/* ===== 모달 ===== */
#modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.3);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 2000;
}

#modal-box {
  background: white;
  padding: 1.5rem;
  border-radius: 8px;
  box-shadow: 0 2px 10px rgba(0,0,0,0.2);
  max-width: 90%;
  width: 360px;
  text-align: center;
  font-size: 1rem;
}

.modal-btns {
  margin-top: 1rem;
  display: flex;
  justify-content: center;
  gap: 1rem;
}

.modal-btns button {
  padding: 0.5rem 1rem;
  border: none;
  border-radius: 6px;
  cursor: pointer;
  font-size: 1rem;
}

#modal-ok {
  background: #007aff;
  color: white;
}

#modal-cancel {
  background: #ccc;
  color: black;
}

/* ===== 레슨 편집 패널 ===== */
.lesson-editor-btn {
  position: fixed;
  top: 76px; /* 헤더를 가리지 않도록 아래로 */
  right: 16px;
  z-index: 950; /* 헤더보다 낮게 */
  padding: 0.5rem 0.75rem;
  border: 1px solid #d0d5dd;
  border-radius: 6px;
  background: #fff;
  color: #111;
  font-size: 0.95rem;
  cursor: pointer;
  box-shadow: 0 1px 4px rgba(0,0,0,0.08);
}

.lesson-editor-btn:hover {
  background: #f6f8fb;
}

.lesson-editor-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.35);
  backdrop-filter: blur(2px);
  z-index: 900; /* 헤더보다 낮게 */
}

.lesson-editor-panel {
  position: fixed;
  top: 54px; /* 10px 위로 조정 */
  right: 0;
  width: min(520px, 90vw);
  height: calc(100vh - 54px);
  max-height: calc(100vh - 54px);
  background: #fff;
  box-shadow: -4px 0 16px rgba(0,0,0,0.15);
  z-index: 950; /* 헤더보다 낮게 */
  display: flex;
  flex-direction: column;
  overflow: hidden; /* 내부 스크롤로만 처리 */
  box-sizing: border-box; /* padding 포함하여 높이 계산 */
  padding: 1.5rem 1.25rem 0.85rem; /* 상단 추가 여백 */
  gap: 1rem;
}

.lesson-editor-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
}

.lesson-editor-label {
  font-size: 0.85rem;
  color: #6b7280;
  margin: 0;
}

.lesson-editor-header h3 {
  margin: 0;
  font-size: 1.15rem;
  color: #111827;
}

.lesson-editor-header button {
  border: none;
  background: transparent;
  font-size: 1.25rem;
  cursor: pointer;
  padding: 0.25rem;
}

.lesson-editor-body {
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
  flex: 1;
  overflow: auto;
  padding-bottom: 0.5rem; /* footer 여유 */
}

.lesson-editor-body label {
  font-weight: 600;
  color: #1f2937;
}

.lesson-editor-body textarea {
  width: 100%;
  max-width: 100%;
  box-sizing: border-box;
  border: 1px solid #d1d5db;
  border-radius: 8px;
  padding: 0.75rem;
  font-size: 0.95rem;
  line-height: 1.5;
  resize: vertical;
  min-height: 64px;  /* 더 짧게 시작 */
  max-height: 180px; /* 과도한 확장 방지 (footer 침범 방지) */
  height: auto;
}

/* 프롬프트 영역은 여유 있게 확장 */
#lesson-prompt-input {
  flex: 1 1 auto;
  min-height: 200px;
  max-height: none;
}

/* 웰컴은 짧게 유지 */
#lesson-welcome-input {
  min-height: 80px;
  max-height: 180px;
}

.lesson-editor-body textarea:focus {
  outline: 2px solid #2563eb;
  border-color: #2563eb;
}

.lesson-editor-footer {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  padding-bottom: 0.25rem; /* 버튼 살짝 위로 */
  position: sticky;
  bottom: 0;
  background: #fff;
  padding-top: 0.25rem;
}

.lesson-editor-status {
  font-size: 0.9rem;
  color: #6b7280;
  min-height: 1.25rem;
}

.lesson-editor-actions {
  display: flex;
  gap: 0.5rem;
}

.lesson-editor-actions button {
  border-radius: 8px;
  padding: 0.55rem 1rem;
  font-size: 0.95rem;
  cursor: pointer;
  border: 1px solid #d1d5db;
}

.lesson-editor-actions button.primary {
  background: #2563eb;
  color: #fff;
  border-color: #2563eb;
}

.lesson-editor-actions button.primary:disabled {
  opacity: 0.6;
  cursor: not-allowed;
}

.lesson-editor-actions button.ghost {
  background: #f9fafb;
}

/* 숨김 처리 */
.hidden { display: none !important; }

/* assistant 로딩 상태 깜빡임 */
.assistant.loading {
  animation: blink 1s infinite ease-in-out;
  opacity: 0.6;
}

/* 깜빡이는 효과 정의 */
@keyframes blink {
  0%   { opacity: 0.2; }
  50%  { opacity: 1; }
  100% { opacity: 0.2; }
}

/* 데스크톱(>768 px)에서, 사이드바가 열렸을 때 메인 영역 ½ 너비만큼 이동 */
@media (min-width: 769px) {
  #sidebar.open ~ #chat-container,
  #sidebar.open ~ form {                 /* 입력창(form)까지 같이 이동 */
    transform: translateX(120px);        /* 240 px 사이드바의 절반 = 120 px */
    transition: transform 0.3s ease;
  }
}
