:root {
  --bg: #ecebe6;
  --surface: #fbfaf7;
  --surface-2: #f2f1ec;
  --text: #1f1f1c;
  --muted: #6b6a64;
  --line: #d9d7cf;
  --accent: #5f9a2b;
  --accent-ink: #ffffff;
  --accent-soft: #e5f0d8;
  --ok: #2e8b4a;
  --bad: #c8412f;
  --shadow: 0 6px 24px rgba(0, 0, 0, 0.12), 0 1px 3px rgba(0, 0, 0, 0.08);
  --page-shadow: 0 1px 4px rgba(0, 0, 0, 0.14);
  --radius: 12px;
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-t: env(safe-area-inset-top, 0px);
  --font: -apple-system, BlinkMacSystemFont, "Apple SD Gothic Neo", "Pretendard", "Noto Sans KR", sans-serif;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #161615;
    --surface: #23231f;
    --surface-2: #2c2c28;
    --text: #ecebe6;
    --muted: #a19f97;
    --line: #3a3a35;
    --accent: #8cc04f;
    --accent-ink: #13200a;
    --accent-soft: #2c3a20;
    --shadow: 0 6px 24px rgba(0, 0, 0, 0.5);
    --page-shadow: 0 1px 6px rgba(0, 0, 0, 0.6);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  -webkit-text-size-adjust: 100%;
  overscroll-behavior: none;
}
button, input, textarea, select { font: inherit; color: inherit; }
button { cursor: pointer; }
#app { height: 100%; }

.icon { width: 20px; height: 20px; flex: none; }
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  min-height: 36px; min-width: 36px; padding: 0 10px;
  border: 0; border-radius: 9px; background: transparent;
  white-space: nowrap;
}
.btn:hover { background: var(--surface-2); }
.btn.on { background: var(--accent); color: var(--accent-ink); }
.btn:disabled { opacity: 0.4; cursor: default; }
.btn.primary { background: var(--accent); color: var(--accent-ink); }
.btn.danger { color: var(--bad); }

/* ---------- 서재 ---------- */
.library { max-width: 1100px; margin: 0 auto; padding: calc(24px + var(--safe-t)) 16px 40px; }
.library h1 { font-size: 24px; margin: 8px 0 20px; }
.lib-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.lib-head .btn { color: var(--muted); font-size: 14px; }

.login { display: flex; flex-direction: column; align-items: center; gap: 12px; width: min(320px, 100%); color: var(--text); }
.login h1 { font-size: 22px; margin: 4px 0 8px; }
.login input {
  width: 100%; padding: 12px 14px; font-size: 17px; border-radius: 12px;
  border: 1px solid var(--line); background: var(--surface);
}
.login input:focus { outline: 2px solid var(--accent); outline-offset: 1px; }
.login .btn { width: 100%; min-height: 46px; font-size: 16px; font-weight: 600; }
.login-msg { min-height: 1.4em; margin: 0; font-size: 14px; color: var(--bad); text-align: center; }
.shelf { display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 20px; }
.book-card { display: block; text-decoration: none; color: inherit; }
.book-card img {
  width: 100%; aspect-ratio: 3 / 4; object-fit: contain;
  border-radius: 6px; box-shadow: var(--page-shadow); background: var(--surface);
}
.book-card .t { margin-top: 8px; font-size: 14px; font-weight: 600; }
.book-card .s { font-size: 12px; color: var(--muted); }
.empty { color: var(--muted); padding: 40px 0; }

/* ---------- 뷰어 ---------- */
.reader { height: 100%; display: flex; flex-direction: column; }

.topbar {
  flex: none; display: flex; align-items: center; gap: 4px;
  padding: calc(6px + var(--safe-t)) 8px 6px;
  background: var(--surface); border-bottom: 1px solid var(--line);
  z-index: 20;
}
.topbar .title { font-weight: 600; font-size: 15px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; min-width: 0; flex: 1; }
.body { flex: 1; display: flex; min-height: 0; position: relative; }

.scroller {
  flex: 1; overflow: auto; position: relative;
  -webkit-overflow-scrolling: touch;
  touch-action: pan-x pan-y;
}
.pages {
  display: grid; justify-content: center; gap: 16px;
  padding: 16px 16px calc(150px + var(--safe-b));
  grid-template-columns: var(--pw);
  width: max-content; min-width: 100%;
}
.pages.spread { grid-template-columns: repeat(2, var(--pw)); column-gap: 2px; }
.pages.spread .page[data-n="1"] { grid-column-start: 2; }

.page {
  width: var(--pw); aspect-ratio: var(--aspect);
  position: relative; background: #fff; box-shadow: var(--page-shadow);
  container-type: size; overflow: hidden;
  -webkit-user-select: none; user-select: none;
}
.page > img { position: absolute; inset: 0; width: 100%; height: 100%; display: block; pointer-events: none; -webkit-user-drag: none; }
.page .ph { position: absolute; inset: 0; display: grid; place-items: center; color: #bbb; font-size: 5cqw; }

/* OCR 글자층: 보이지 않지만 선택·검색된다 */
/* 선택 색은 불투명하게 칠하고 층 전체를 반투명하게 해서, 조각이 맞닿거나 겹친 곳도 같은 색으로 보이게 한다 */
.text-layer { position: absolute; inset: 0; line-height: 1; -webkit-user-select: text; user-select: text; opacity: 0.3; }
.text-layer span {
  position: absolute; white-space: pre; color: transparent; transform-origin: 0 0;
  font-family: var(--font); cursor: text;
}
.text-layer ::selection { background: rgb(20, 100, 235); color: transparent; }
.text-layer .eol { font-size: 0; line-height: 0; }
.overlay, .overlay > div { position: absolute; inset: 0; pointer-events: none; }
.hit { position: absolute; background: rgba(255, 150, 0, 0.35); outline: 2px solid rgba(255, 130, 0, 0.9); border-radius: 2px; }
.hit.flash { animation: flash 1.6s ease-out 2; }
@keyframes flash { 0%, 100% { opacity: 1; } 50% { opacity: 0.25; } }
.spot {
  position: absolute; pointer-events: auto; border: 0; padding: 0;
  border-radius: 14%; background: var(--accent); color: #fff;
  display: grid; place-items: center; box-shadow: 0 2px 8px rgba(0,0,0,.25);
}
.spot svg { width: 46%; height: 46%; }
.spot.none { background: #9a9a92; }
.spot.playing { background: #e2862b; animation: pulse 1.4s ease-in-out infinite; }
@keyframes pulse { 50% { box-shadow: 0 0 0 6px rgba(226, 134, 43, 0.3); } }
.spot-extra {
  position: absolute; transform: translateX(-50%); display: flex; flex-direction: column; align-items: center; gap: 2px;
  pointer-events: none;
}
.spot-label {
  white-space: nowrap; font-size: max(10px, 1.4cqw); font-weight: 600; color: #4b7d1f;
  background: rgba(255,255,255,.92); padding: 0 4px; border-radius: 4px;
}
.spot-alt {
  pointer-events: auto; border: 1px solid #7cb342; background: #fff; color: #4b7d1f; border-radius: 99px;
  font-size: max(10px, 1.3cqw); font-weight: 600; padding: 1px 7px; white-space: nowrap;
}
.spot-alt.playing { background: #e2862b; border-color: #e2862b; color: #fff; }

/* ---------- 아래 도구 막대 ---------- */
.dock {
  position: absolute; left: 50%; bottom: calc(12px + var(--safe-b)); transform: translateX(-50%);
  display: flex; align-items: center; gap: 2px; padding: 4px;
  background: var(--surface); border: 1px solid var(--line); border-radius: 14px; box-shadow: var(--shadow);
  z-index: 15; max-width: calc(100% - 16px); overflow-x: auto; scrollbar-width: none;
}
.dock .sep { width: 1px; height: 22px; background: var(--line); margin: 0 4px; flex: none; }
.dock .pageno { display: flex; align-items: center; gap: 4px; color: var(--muted); font-size: 14px; padding: 0 2px; }
.dock .pageno input {
  width: 3.2em; text-align: center; padding: 5px 4px; border: 1px solid var(--line);
  border-radius: 8px; background: var(--surface-2); font-variant-numeric: tabular-nums; font-size: 15px;
}
.dock .pageno span { white-space: nowrap; }
.dock .zoom { font-size: 13px; color: var(--muted); min-width: 3.2em; text-align: center; font-variant-numeric: tabular-nums; }

/* 넘겨보기: 지금 묶음의 쪽만 보이고, 화면 가운데에 놓인다 */
.pages.flip { min-height: 100%; align-content: center; padding-bottom: calc(88px + var(--safe-b)); }
.pages.flip .page { display: none; }
.pages.flip .page.shown { display: block; }
.flip-nav {
  position: absolute; top: 50%; transform: translateY(-50%); z-index: 12; display: none;
  width: 40px; height: 72px; border: 0; border-radius: 12px; padding: 0;
  background: color-mix(in srgb, var(--surface) 70%, transparent); color: var(--text); box-shadow: var(--page-shadow);
  place-items: center; opacity: .55;
}
.flip-nav:hover { opacity: 1; }
.flip-nav.prev { left: 8px; }
.flip-nav.next { right: 8px; }
.view-flip .flip-nav { display: grid; }
.view-flip .page, .view-flip .text-layer span { cursor: pointer; }
@media (hover: none) { .view-flip .flip-nav { display: none; } }

/* ---------- 워크북 문제 ---------- */
.ex-field { position: absolute; pointer-events: auto; }
.ex-input {
  width: 100%; height: 100%; margin: 0; padding: 0 0.25em; border: 0; border-radius: 4px;
  background: rgba(70, 130, 255, 0.07); color: #1d3fae; font-family: var(--font); font-weight: 600;
  -webkit-user-select: text; user-select: text; outline: none;
}
.ex-input.multi { resize: none; padding: 0.3em 0.4em; line-height: 1.55; font-weight: 500; }
.ex-input:hover { background: rgba(70, 130, 255, 0.12); }
.ex-input:focus { background: rgba(70, 130, 255, 0.16); box-shadow: 0 0 0 2px rgba(70, 130, 255, 0.6); }
.checking .ex-input.ok { background: rgba(46, 139, 74, 0.14); color: var(--ok); }
.checking .ex-input.bad { background: rgba(200, 65, 47, 0.10); color: var(--bad); }
.ex-tip {
  position: absolute; transform: translateY(1px); padding: 2px 7px; border-radius: 6px; z-index: 2;
  background: var(--ok); color: #fff; font-size: max(11px, 1.5cqw); font-weight: 600; line-height: 1.35;
  white-space: normal; overflow-wrap: anywhere; width: max-content; pointer-events: none;
  box-shadow: 0 1px 4px rgba(0,0,0,.2);
}
.ex-tip.empty { background: rgba(46, 139, 74, 0.82); }
.ex-tip.sample { background: #6b4fd8; }
.ex-tip b { display: inline-block; margin-right: 6px; padding: 0 5px; border-radius: 4px; background: rgba(255,255,255,.25); font-size: 0.9em; }
.ex-opt {
  position: absolute; pointer-events: auto; padding: 0; border: 0; background: transparent; border-radius: 50%;
  cursor: pointer;
}
.ex-opt.wide { border-radius: 10px; }
.ex-opt:hover { background: rgba(70, 130, 255, 0.10); }
.ex-opt.sel { box-shadow: inset 0 0 0 2.5px #2f6bff; }
.checking .ex-opt.answer { box-shadow: inset 0 0 0 2.5px var(--ok); }
.checking .ex-opt.sel.bad { box-shadow: inset 0 0 0 2.5px var(--bad); background: rgba(200, 65, 47, 0.10); }
.dock .check span { font-variant-numeric: tabular-nums; }

/* 보기 방식 전환 버튼 묶음 */
.seg { display: flex; gap: 2px; background: var(--surface-2); border-radius: 10px; padding: 2px; flex: none; }
.seg .btn { min-height: 32px; }
.seg + .seg { margin-left: 4px; }

/* ---------- 음원 막대 ---------- */
.player {
  position: absolute; left: 50%; transform: translateX(-50%);
  bottom: calc(68px + var(--safe-b)); width: min(560px, calc(100% - 16px));
  background: var(--surface); border: 1px solid var(--line); border-radius: 14px; box-shadow: var(--shadow);
  padding: 8px 10px; z-index: 16; display: none;
}
.player.show { display: block; }
.player .row { display: flex; align-items: center; gap: 4px; }
.player .name { flex: 1; min-width: 0; font-size: 14px; font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.player .time { font-size: 12px; color: var(--muted); font-variant-numeric: tabular-nums; min-width: 6.5em; text-align: right; }
.player input[type=range] { width: 100%; accent-color: var(--accent); margin: 6px 0 2px; }
.player .play { width: 44px; height: 44px; border-radius: 50%; background: var(--accent); color: var(--accent-ink); }
.player .play:hover { background: var(--accent); filter: brightness(1.05); }
.player .skip { font-size: 12px; font-weight: 700; gap: 2px; }
.player .ctrls { justify-content: center; gap: 10px; }

/* ---------- 옆 패널 ---------- */
.panel {
  width: 340px; flex: none; background: var(--surface); border-right: 1px solid var(--line);
  display: none; flex-direction: column; min-height: 0; order: -1;
}
.panel.open { display: flex; }
.tabs { display: flex; gap: 2px; padding: 8px; border-bottom: 1px solid var(--line); }
.tabs .btn { flex: 1; font-size: 14px; }
.panel-body { flex: 1; overflow: auto; padding: 8px; padding-bottom: calc(80px + var(--safe-b)); }
.panel input[type=search] {
  width: 100%; padding: 9px 12px; border: 1px solid var(--line); border-radius: 10px; background: var(--surface-2);
  font-size: 16px; margin-bottom: 8px;
}
.item {
  display: block; width: 100%; text-align: left; border: 0; background: transparent;
  padding: 9px 10px; border-radius: 9px; line-height: 1.4;
}
.item:hover { background: var(--surface-2); }
.item .p { float: right; color: var(--muted); font-size: 12px; margin-left: 8px; font-variant-numeric: tabular-nums; }
.item.lv1 { font-weight: 600; }
.item.lv2 { padding-left: 24px; font-size: 14px; }
.item .snip { font-size: 13px; color: var(--muted); display: block; }
.item mark { background: rgba(255, 170, 0, 0.35); color: inherit; border-radius: 2px; }
.item.current { background: var(--accent-soft); }
.hint { color: var(--muted); font-size: 13px; padding: 8px 10px; }

@media (max-width: 760px) {
  .panel { position: absolute; inset: 0 auto 0 0; width: min(88vw, 360px); z-index: 30; box-shadow: var(--shadow); }
  .pages { padding-left: 6px; padding-right: 6px; gap: 10px; }
  .hide-sm { display: none !important; }
  .dock { gap: 0; }
  .dock .sep { margin: 0 2px; }
  .dock .pageno input { width: 2.8em; }
  .dock .btn { min-width: 32px; padding: 0 6px; }
  .dock .check span { font-size: 12px; }
}

.pop {
  position: fixed; z-index: 45; width: 320px; max-width: calc(100vw - 16px);
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow);
  padding: 12px; display: none; flex-direction: column; gap: 10px;
}
.pop.show { display: flex; }
.pop select {
  width: 100%; border: 1px solid var(--line); border-radius: 9px; padding: 8px 10px; background: var(--surface-2); font-size: 16px;
}
.pop .row { display: flex; align-items: center; gap: 6px; }
.pop .row .grow { flex: 1; }
.pop label { font-size: 13px; color: var(--muted); }
@media (max-width: 600px) {
  .pop { transform: none !important; left: 0 !important; right: 0; top: auto !important; bottom: 0; width: 100%; max-width: none; border-radius: 16px 16px 0 0; padding-bottom: calc(16px + var(--safe-b)); }
}

.toast {
  position: fixed; left: 50%; top: calc(64px + var(--safe-t)); transform: translateX(-50%); z-index: 60;
  background: var(--text); color: var(--bg); padding: 8px 14px; border-radius: 10px; font-size: 14px;
  opacity: 0; transition: opacity .2s; pointer-events: none;
}
.toast.show { opacity: 1; }

.center-msg { height: 100%; display: grid; place-items: center; text-align: center; padding: 24px; color: var(--muted); }

@media (min-width: 761px) { .body:has(.panel.open) .flip-nav.prev { left: 348px; } }
