:root {
  --bg: #f3f5f9;
  --side: #ffffff;
  --panel: #ffffff;
  --line: #e2e6ee;
  --text: #1c2333;
  --muted: #7a8396;
  --soft: #f6f8fb;
  --accent: #3563e9;
  --accent-soft: #e8eefe;
  --ok: #15924a;
  --ok-soft: #e7f6ed;
  --bad: #d83a3f;
  --bad-soft: #fdecec;
  --exam: #f2621b;
  --mono: 'JetBrains Mono', D2Coding, Consolas, monospace;
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  font-family: Pretendard, -apple-system, 'Malgun Gothic', sans-serif;
  font-size: 15px;
  color: var(--text);
  background: var(--bg);
}
button { font: inherit; color: inherit; }
[hidden] { display: none !important; }

/* ===== 레이아웃 ===== */
#app { display: flex; height: 100vh; overflow: hidden; }

#sidebar {
  width: 220px;
  flex: none;
  background: var(--side);
  border-right: 1px solid var(--line);
  display: flex;
  flex-direction: column;
  transition: margin-left .2s ease;
}
body.side-collapsed #sidebar { margin-left: -220px; }
.side-head {
  display: flex; align-items: center; justify-content: space-between;
  padding: 20px 14px 16px 22px;
}
.logo { font-size: 20px; font-weight: 800; letter-spacing: -.02em; }
#chapterList { display: flex; flex-direction: column; gap: 8px; padding: 4px 14px 20px; overflow: auto; }
.chapter {
  text-align: left;
  padding: 13px 16px;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: var(--panel);
  font-weight: 600;
  cursor: pointer;
  display: flex; justify-content: space-between; align-items: center;
}
.chapter:hover:not(:disabled) { border-color: var(--accent); }
.chapter.active { background: var(--accent); border-color: var(--accent); color: #fff; }
.chapter:disabled { color: var(--muted); cursor: default; background: var(--soft); }
.chapter small { font-size: 12px; font-weight: 500; }

.icon-btn {
  width: 30px; height: 30px;
  border: 1px solid var(--line); border-radius: 8px;
  background: var(--panel); cursor: pointer; font-size: 16px; line-height: 1;
}
.icon-btn:hover { border-color: var(--accent); color: var(--accent); }
#sideOpen { display: none; position: fixed; left: 8px; top: 20px; z-index: 5; }
body.side-collapsed #sideOpen { display: block; }

#main { flex: 1; min-width: 0; display: flex; flex-direction: column; gap: 14px; padding: 16px 20px; }
body.side-collapsed #main { padding-left: 52px; }

/* ===== 상단 ===== */
#topbar { display: flex; align-items: center; gap: 24px; min-height: 56px; }
#chapterTitle { margin: 0; font-size: 22px; font-weight: 800; white-space: nowrap; }

#steps { list-style: none; margin: 0 0 0 auto; padding: 0; display: flex; }
#steps li { position: relative; display: flex; }
#steps li + li::before {
  content: ''; position: absolute; top: 9px; right: 50%; width: 100%; height: 3px;
  background: var(--line);
}
.step {
  position: relative; z-index: 1;
  min-width: 92px;
  display: flex; flex-direction: column; align-items: center; gap: 6px;
  background: none; border: 0; padding: 0; cursor: pointer;
}
.step .dot {
  width: 21px; height: 21px; border-radius: 50%;
  border: 3px solid #b7bfcd; background: var(--panel);
  transition: transform .15s;
}
.step:hover .dot { transform: scale(1.15); }
.step .label { font-size: 12.5px; color: var(--muted); font-weight: 600; }
.step.active .dot { background: var(--accent); border-color: var(--accent); }
.step.active .label { color: var(--accent); }
.step.empty .dot { border-style: dashed; }
.step.empty .label { color: #b7bfcd; }
.step.exam .dot { border-color: var(--exam); }
.step.exam .label { color: var(--exam); }

#examBadge { display: none; align-items: center; gap: 18px; margin-left: auto; }
.exam-label { font-weight: 700; font-size: 17px; }
#timer {
  font-family: var(--mono); font-size: 34px; font-weight: 600;
  font-variant-numeric: tabular-nums;
  background: rgba(255,255,255,.18); padding: 2px 16px; border-radius: 10px;
}
#timer.hurry { background: #fff; color: var(--bad); animation: blink 1s steps(2) infinite; }

/* ===== 작업 영역 ===== */
#workspace {
  flex: 1; min-height: 0;
  display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); gap: 16px;
}
.panel { background: var(--panel); border: 1px solid var(--line); border-radius: 14px; min-height: 0; }

#left { overflow: auto; padding: 20px 26px 28px; }
.left-head { display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding-bottom: 14px; border-bottom: 2px solid var(--text); }
#typeTitle { margin: 0; font-size: 19px; font-weight: 800; }

#theoryWrap { padding: 20px 0 6px; border-bottom: 2px solid var(--text); }
#video { width: 100%; max-width: 640px; aspect-ratio: 16 / 9; border-radius: 12px; overflow: hidden; background: #11151f; }
#video iframe { width: 100%; height: 100%; border: 0; display: block; }
.video-ph { height: 100%; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 8px; color: #8a93a6; }
.video-ph .play { width: 56px; height: 56px; border-radius: 50%; border: 2px solid #8a93a6;
  display: flex; align-items: center; justify-content: center; font-size: 20px; padding-left: 4px; }
body.theory-closed #theoryWrap { display: none; }

#problemWrap { padding-top: 18px; }
.problem-head { display: flex; align-items: center; justify-content: space-between; }
.problem-head h3 { margin: 0; font-size: 17px; }

#placeholder { text-align: center; color: var(--muted); padding: 80px 0; }
.ph-icon { font-size: 38px; }

/* 본문 */
.prose { line-height: 1.7; }
.prose code { font-family: var(--mono); font-size: .92em; background: var(--soft); padding: 1px 5px; border-radius: 5px; border: 1px solid var(--line); }
.prose pre, .sample pre {
  font-family: var(--mono); font-size: 14.5px; line-height: 1.55;
  margin: 8px 0 14px; padding: 12px 16px; border-radius: 10px; white-space: pre-wrap; word-break: break-all;
}
.prose pre.ex { background: #1b2130; color: #e7ecf5; }
.prose pre.out, .sample pre { background: var(--soft); border: 1px solid var(--line); }
.out-label, .sample-label { font-size: 13px; font-weight: 700; color: var(--muted); }
.sample { margin-top: 10px; }
.loading { color: var(--muted); padding: 10px 0; }
.error-box { color: var(--bad); }

/* ===== 오른쪽: 에디터 / 입출력 ===== */
#right { display: flex; flex-direction: column; overflow: hidden; }
.right-head {
  display: flex; justify-content: space-between; align-items: center;
  padding: 12px 18px; border-bottom: 1px solid var(--line); font-weight: 700;
}
.py-status { font-size: 12.5px; font-weight: 600; color: var(--muted); }
.py-status.ready { color: var(--ok); }
.py-status.error { color: var(--bad); }

#editor { flex: 1; min-height: 0; }
.CodeMirror { height: 100%; font-family: var(--mono); font-size: 16px; line-height: 1.6; }
.CodeMirror-gutters { background: var(--soft); border-right: 1px solid var(--line); }
.CodeMirror-linenumber { color: #a3abbb; padding: 0 10px 0 6px; }

.io { height: 34%; min-height: 180px; display: grid; grid-template-columns: 2fr 3fr; border-top: 1px solid var(--line); }
.io-box { display: flex; flex-direction: column; min-height: 0; min-width: 0; }
.io-box + .io-box { border-left: 1px solid var(--line); }
.io-box label { font-size: 12.5px; font-weight: 700; color: var(--muted); padding: 8px 14px 4px; }
#stdin {
  flex: 1; resize: none; border: 0; outline: 0; padding: 4px 14px 10px;
  font-family: var(--mono); font-size: 14.5px; line-height: 1.55; background: transparent;
}
#result { flex: 1; overflow: auto; padding: 4px 14px 12px; font-size: 14px; }
#result pre { font-family: var(--mono); font-size: 14.5px; line-height: 1.55; margin: 0; white-space: pre-wrap; word-break: break-all; }
#result .muted { color: var(--muted); }
#result pre.err { color: var(--bad); margin-top: 6px; }
.verdict { font-weight: 800; font-size: 16px; padding: 8px 12px; border-radius: 8px; margin-bottom: 8px; }
.verdict.ok { background: var(--ok-soft); color: var(--ok); }
.verdict.bad { background: var(--bad-soft); color: var(--bad); }
.hint { margin-top: 8px; padding: 8px 12px; border-radius: 8px; background: #fff7e6; color: #8a5a00; font-size: 13.5px; }

table.diff { width: 100%; border-collapse: collapse; font-family: var(--mono); font-size: 13.5px; }
table.diff th { font-family: Pretendard, sans-serif; font-size: 12px; color: var(--muted); text-align: left; padding: 2px 8px; }
table.diff td { padding: 2px 8px; border-top: 1px solid var(--line); vertical-align: top; white-space: pre-wrap; word-break: break-all; }
table.diff td.n { color: #a3abbb; width: 1%; text-align: right; }
table.diff tr.hit td { background: var(--bad-soft); }
table.diff .sp { color: #c0c6d2; }
.diff-note { font-size: 12px; color: var(--muted); margin-top: 6px; }

.actions { display: flex; gap: 8px; padding: 10px 14px; border-top: 1px solid var(--line); }
.spacer { flex: 1; }

/* ===== 버튼 ===== */
.btn {
  border: 1px solid var(--line); background: var(--panel);
  padding: 9px 18px; border-radius: 9px; font-weight: 700; cursor: pointer;
}
.btn:hover:not(:disabled) { border-color: var(--accent); color: var(--accent); }
.btn:disabled { opacity: .45; cursor: default; }
.btn.small { padding: 6px 12px; font-size: 13.5px; }
.btn.ghost { background: transparent; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.success { background: var(--ok); border-color: var(--ok); color: #fff; }
.btn.exam { background: var(--exam); border-color: var(--exam); color: #fff; }
.btn.primary:hover:not(:disabled), .btn.success:hover:not(:disabled), .btn.exam:hover:not(:disabled) { color: #fff; filter: brightness(1.08); }
.btn.armed { background: var(--bad); border-color: var(--bad); color: #fff; }

/* ===== 시험 모드 ===== */
body.exam #sidebar, body.exam #sideOpen, body.exam #steps,
body.exam #theoryWrap, body.exam #theoryToggle, body.exam #newProblem { display: none !important; }
body.exam #main { padding: 0 26px 26px; }
body.exam #topbar {
  margin: 0 -26px; padding: 14px 40px;
  background: var(--exam); color: #fff;
}
body.exam #examBadge { display: flex; }
body.exam::after {
  content: ''; position: fixed; inset: 0; pointer-events: none; z-index: 40;
  border: 10px solid var(--exam);
  animation: examPulse 2.4s ease-in-out infinite;
}
@keyframes examPulse { 50% { border-color: #ff9a5c; } }
@keyframes blink { 50% { opacity: .55; } }

/* ===== 모달 ===== */
#modalLayer {
  position: fixed; inset: 0; z-index: 60;
  background: rgba(17, 21, 31, .55);
  display: flex; align-items: center; justify-content: center; padding: 24px;
}
.modal {
  background: var(--panel); border-radius: 18px; padding: 30px 34px;
  width: min(560px, 100%); box-shadow: 0 20px 60px rgba(0,0,0,.25);
}
.modal h2 { margin: 0 0 4px; font-size: 22px; }
.modal .sub { color: var(--muted); margin: 0 0 14px; }
.rules { padding-left: 20px; line-height: 1.9; margin: 0 0 6px; }
.modal-actions { display: flex; justify-content: flex-end; gap: 10px; margin-top: 22px; }
.modal .big { font-size: 44px; font-weight: 900; letter-spacing: -.02em; }
.modal.out { text-align: center; border-top: 10px solid var(--bad); }
.modal.out .big { color: var(--bad); }
.modal.out #outReason { font-size: 17px; font-weight: 600; }
.modal.out .modal-actions { justify-content: center; }

.modal.pass { width: min(1100px, 100%); border: 6px solid var(--ok); }
.modal.pass .big { color: var(--ok); }
.pass-top { display: flex; justify-content: space-between; align-items: flex-start; gap: 20px; }
.clock-box { text-align: right; }
.clock-label { font-size: 13px; color: var(--muted); font-weight: 700; }
#liveClock { font-family: var(--mono); font-size: 46px; font-weight: 600; font-variant-numeric: tabular-nums; }
.live-bar { height: 6px; border-radius: 3px; background: var(--ok-soft); overflow: hidden; }
.live-bar span { display: block; height: 100%; width: 30%; background: var(--ok); border-radius: 3px; animation: slide 1.6s linear infinite; }
@keyframes slide { from { transform: translateX(-100%); } to { transform: translateX(340%); } }
.pass-info { display: grid; grid-template-columns: auto 1fr; gap: 4px 18px; margin: 14px 0 18px; font-size: 16px; }
.pass-info dt { color: var(--muted); font-weight: 700; }
.pass-info dd { margin: 0; font-weight: 700; }
.pass-cols { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; }
.col-label { font-size: 13px; font-weight: 700; color: var(--muted); margin-bottom: 6px; }
.pass-cols pre {
  margin: 0; max-height: 40vh; overflow: auto;
  font-family: var(--mono); font-size: 15px; line-height: 1.55;
  background: var(--soft); border: 1px solid var(--line); border-radius: 10px; padding: 12px 16px; white-space: pre-wrap;
}

#toast {
  position: fixed; left: 50%; bottom: 40px; transform: translateX(-50%); z-index: 80;
  background: #1b2130; color: #fff; padding: 10px 20px; border-radius: 10px; font-weight: 600;
}
