* { box-sizing: border-box; }

html, body { height: 100%; margin: 0; }

body {
  font-family: "Malgun Gothic", "NanumGothic", sans-serif;
  color: var(--text);
  background: var(--bg);
}

.app {
  display: grid;
  grid-template-columns: 300px 1fr;
  min-height: 100vh;
  align-items: start;
  transition: grid-template-columns 0.2s ease;
}

body.sidebar-collapsed .app {
  grid-template-columns: 0 1fr;
}

.sidebar-wrap {
  position: relative;
  min-width: 0;
  width: 300px;
}

/* 접힌 뒤에도 wrap이 300px로 본문 위를 덮어 클릭을 가로채지 않게 */
body.sidebar-collapsed .sidebar-wrap {
  width: 0;
  overflow: hidden;
  pointer-events: none;
}

/* 사이드바: 본문보다 길면 내부 스크롤 (JS가 max-height 동기화) */
.sidebar {
  position: static;
  height: auto;
  overflow-y: auto;
  overflow-x: hidden;
  padding: 16px;
  margin-bottom: 20px;
  border-right: 1px solid var(--line);
  background: #f9fbff;
  width: 300px;
}

body.sidebar-collapsed .sidebar {
  transform: translateX(-100%);
  opacity: 0;
  pointer-events: none;
}

.sidebar-toggle {
  position: fixed;
  left: 0;
  top: 50%;
  transform: translateY(-50%);
  z-index: 100;
  width: 28px;
  height: 56px;
  border-radius: 0 10px 10px 0;
  border: 1px solid var(--line);
  border-left: 0;
  background: #fff;
  cursor: pointer;
  font-size: 12px;
  box-shadow: var(--shadow);
  transition: left 0.2s ease;
}

body:not(.sidebar-collapsed) .sidebar-toggle {
  left: 300px;
}

/* ── 상단 바 (모바일·좁은 화면) ── */
.topbar {
  display: none;
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  z-index: 300;
  height: 52px;
  padding: 0 12px;
  align-items: center;
  gap: 10px;
  background: #fff;
  border-bottom: 1px solid var(--line);
  box-shadow: 0 2px 12px rgba(15, 23, 42, 0.06);
}

.hamburger-btn {
  width: 40px;
  height: 40px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: #fff;
  font-size: 20px;
  line-height: 1;
  cursor: pointer;
  flex-shrink: 0;
}

.topbar-title {
  font-size: 16px;
  font-weight: 800;
  flex: 0 1 auto;
  max-width: 42%;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.topbar-nav {
  display: flex;
  gap: 4px;
  flex-shrink: 0;
  margin-left: 6px;
}

.topbar-nav .nav-btn {
  padding: 6px 8px;
  font-size: 11px;
}

/* 좁은 화면 우상단 저장 — 카드제작일 때만 */
.topbar-save {
  margin-left: auto;
  flex-shrink: 0;
  display: none;
}

body.section-create .topbar-save {
  display: inline-flex;
}

.mobile-overlay {
  display: none;
  position: fixed;
  inset: 0;
  z-index: 250;
  background: rgba(15, 23, 42, 0.4);
}

body.mobile-menu-open .mobile-overlay {
  display: block;
}

.main {
  padding: 16px;
  min-width: 0;
}

.title { font-size: 22px; font-weight: 800; margin-bottom: 4px; }

/* 앱 타이틀 = 홈(카드관리) 버튼 — 전역 button 스타일을 텍스트로 되돌린다 */
.app-home-btn {
  display: block;
  padding: 0;
  border-radius: 0;
  background: none;
  color: inherit;
  font-family: inherit;
  text-align: left;
}

.app-home-btn:hover { color: var(--blue); }

.app-home-btn:focus-visible {
  outline: 2px solid var(--blue);
  outline-offset: 2px;
  border-radius: 6px;
}

.sub { font-size: 12px; color: var(--muted); line-height: 1.5; margin-bottom: 12px; }

.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; }

.stats {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 8px;
  margin-bottom: 12px;
}

.study-workspace {
  display: flex;
  flex-direction: column;
  gap: 12px;
  margin-top: 12px;
}

/* 뷰포트 기준 높이 — 내용만큼 페이지 늘어나지 않고, 칸 안에서만 스크롤 */
.study-layout {
  display: grid;
  grid-template-columns: minmax(0, 4.5fr) minmax(0, 5.5fr);
  gap: 14px;
  align-items: stretch;
  flex: none;
  height: calc(100vh - 350px);
  min-height: 510px;
  transition: grid-template-columns 0.2s ease;
}

/* 문제 가리기 — 문제칸을 세로 탭으로 접어 해설에 폭을 몰아준다 */
body.study-prompt-collapsed .study-layout {
  grid-template-columns: 44px minmax(0, 1fr);
}

.study-panel-tab {
  display: none;
}

body.study-prompt-collapsed .study-prompt-panel .study-panel-head,
body.study-prompt-collapsed .study-prompt-panel .study-panel-body {
  display: none;
}

body.study-prompt-collapsed .study-prompt-panel .study-panel-tab {
  display: block;
  width: 100%;
  height: 100%;
  padding: 14px 0;
  border: 1px solid #93c5fd;
  border-radius: 16px;
  background: #eff6ff;
  color: var(--blue);
  font: inherit;
  font-size: 13px;
  font-weight: 700;
  letter-spacing: 2px;
  white-space: nowrap;
  writing-mode: vertical-rl;
  cursor: pointer;
}

body.study-prompt-collapsed .study-prompt-panel .study-panel-tab:hover {
  background: #dbeafe;
  border-color: var(--blue);
}

body.study-playing .stats {
  display: none;
}

/* 세트 구성(setup)과 학습 중(play)은 서로 배타적으로 표시 */
#studyPlayBar { display: none; }
body.study-playing #studyPlayBar { display: flex; margin-bottom: 10px; }
body.study-playing #studySetup { display: none; }

/* setup 화면(학습 중이 아닐 때)에선 작업영역·수정 패널 숨김 — :not()으로 특이성 충돌 방지 */
body:not(.study-playing) #studySection .study-workspace,
body:not(.study-playing) #studySection #studyEditPanel { display: none; }

body.study-playing .study-layout {
  height: calc(100vh - 200px);
  min-height: 590px;
}

#studySection {
  display: flex;
  flex-direction: column;
  gap: 0;
}

.study-panel {
  display: flex;
  flex-direction: column;
  min-width: 0;
  min-height: 0;
  height: 100%;
  overflow: hidden;
  background: #fff;
  border: 1px solid var(--line);
  border-radius: 16px;
  box-shadow: var(--shadow);
}

.study-panel-head {
  padding: 12px 16px;
  border-bottom: 1px solid var(--line);
  flex-shrink: 0;
  min-width: 0;
}

.study-panel-head.between {
  flex-wrap: nowrap;
  align-items: center;
  gap: 10px;
}

.study-panel-head h3 {
  margin: 0;
  flex-shrink: 0;
}

/* 해설 헤더 — 빈칸 1~N 한 줄, 가로 스크롤 */
.study-panel-head .study-navigator.toolbar {
  display: flex;
  flex: 1;
  min-width: 0;
  flex-wrap: nowrap;
  overflow-x: auto;
  overflow-y: hidden;
  justify-content: flex-start;
  gap: 6px;
  padding-bottom: 4px;
  -webkit-overflow-scrolling: touch;
  scrollbar-width: thin;
}

.study-panel-head .study-navigator .small {
  flex: 0 0 auto;
  white-space: nowrap;
}

.study-panel-body {
  padding: 16px;
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.study-panel-body-scroll,
.study-explanation-scroll {
  flex: 1;
  min-height: 0;
  overflow-x: auto;
  overflow-y: auto;
  padding: 0 12px 24px 0;
  scroll-padding-bottom: 24px;
}

/* 해설은 정답 노출 시에도 가로 스크롤 없이 줄바꿈되도록 */
.study-explanation-scroll {
  overflow-x: hidden;
}

.study-dock {
  flex-shrink: 0;
  border: 1px solid var(--line);
  border-radius: 16px;
  padding: 12px 16px;
  background: #fff;
  box-shadow: var(--shadow);
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.study-dock-row {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 12px;
  align-items: end;
}

.study-dock-memo {
  min-width: 0;
}

.study-dock-memo label {
  margin-bottom: 4px;
}

.study-dock-memo .sm {
  min-height: 72px;
  max-height: 120px;
}

.study-dock-actions {
  flex-shrink: 0;
  align-self: end;
}

@media (max-width: 900px) {
  .topbar { display: flex; }

  .sidebar-toggle { display: none !important; }

  .app {
    grid-template-columns: 1fr !important;
    padding-top: 52px;
    min-height: calc(100vh - 52px);
  }

  body.sidebar-collapsed .app {
    grid-template-columns: 1fr !important;
  }

  .sidebar-wrap {
    display: none;
    position: fixed;
    top: 52px;
    left: 0;
    right: 0;
    bottom: 0;
    z-index: 260;
    overflow: auto;
    background: #f9fbff;
    border-bottom: 1px solid var(--line);
    box-shadow: var(--shadow);
  }

  body.mobile-menu-open .sidebar-wrap {
    display: block;
    width: auto;
    overflow: auto;
    pointer-events: auto;
  }

  .sidebar {
    position: static;
    max-height: none;
    width: 100%;
    height: auto;
    min-height: 100%;
    transform: none !important;
    opacity: 1 !important;
    pointer-events: auto !important;
  }

  body.sidebar-collapsed .sidebar {
    transform: none !important;
    opacity: 1 !important;
  }

  .main {
    min-height: calc(100vh - 52px);
  }

  body.study-playing .study-layout {
    height: calc(100vh - 270px);
    min-height: 530px;
  }

  .study-dock-row {
    grid-template-columns: 1fr;
  }

  .study-dock-actions {
    align-self: stretch;
  }
}

@media (max-width: 1100px) {
  .grid2, .study-layout, .create-split, .manage-split { grid-template-columns: 1fr; }
  .stats { grid-template-columns: repeat(2, 1fr); }

  /* 한 줄로 쌓이는 폭에서는 세로 탭이 화면을 다 먹으므로 가로 막대로 접는다 */
  body.study-prompt-collapsed .study-layout { grid-template-columns: 1fr; }

  body.study-prompt-collapsed .study-prompt-panel {
    height: 44px;
    flex: none;
  }

  body.study-prompt-collapsed .study-prompt-panel .study-panel-tab {
    padding: 0 14px;
    writing-mode: horizontal-tb;
  }
}

/* 넓은 화면(탑바 없음) — 카드제작 헤더가 스크롤을 따라감 */
@media (min-width: 901px) {
  #createSection.panel {
    overflow: visible;
  }

  #createSection .create-head {
    position: sticky;
    top: 0;
    z-index: 40;
    margin: -4px -14px 8px;
    padding: 10px 14px;
    background: var(--panel, #fff);
    border-bottom: 1px solid var(--line);
    box-shadow: 0 4px 12px rgba(15, 23, 42, 0.04);
  }
}
