:root {
  --bg:        #16161a;
  --bg-2:      #1c1c21;
  --bg-3:      #232329;
  --line:      #2e2e36;
  --fg:        #e6e4e0;
  --fg-dim:    #9a9aa4;
  --fg-faint:  #66666f;
  --accent:    #c9a86a;
  --accent-2:  #7aa2c9;
  --ok:        #6fbf8b;
  --warn:      #d8a657;
  --bad:       #d97c7c;
  --radius:    8px;
  --editor-font: "본명조", "Noto Serif KR", "나눔명조", NanumMyeongjo, "바탕", Batang, serif;
  --ui-font: -apple-system, BlinkMacSystemFont, "Pretendard", "Apple SD Gothic Neo",
             "Noto Sans KR", "맑은 고딕", "Malgun Gothic", sans-serif;
  --ed-size: 17px;
  --ed-line: 1.9;
  --ed-width: 42rem;
}
:root[data-theme="light"] {
  --bg: #faf8f4; --bg-2: #f2efe9; --bg-3: #e9e5dd; --line: #ddd7cc;
  --fg: #22201c; --fg-dim: #6b675f; --fg-faint: #9b968c;
  --accent: #9a7b3a; --accent-2: #3d6c96;
}
:root[data-theme="sepia"] {
  --bg: #2a241d; --bg-2: #322b22; --bg-3: #3b3329; --line: #4a4034;
  --fg: #e8dcc8; --fg-dim: #a99a83; --fg-faint: #7a6e5c;
  --accent: #d0a85c; --accent-2: #8fae9b;
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
@supports (height: 100dvh) { html, body { height: 100dvh; } }  /* 모바일 주소창 대응 */
body {
  background: var(--bg); color: var(--fg);
  font-family: var(--ui-font); font-size: 14px;
  display: flex; flex-direction: column; overflow: hidden;
  -webkit-text-size-adjust: 100%;
  overscroll-behavior: none;
}
#scrim { display: none; }
button, input, select, textarea { font-family: inherit; font-size: inherit; color: inherit; }
::selection { background: color-mix(in srgb, var(--accent) 35%, transparent); }
.spacer { flex: 1; }
.dim { color: var(--fg-dim); }
.faint { color: var(--fg-faint); }

/* ---------------------------------------------------------------- 상단바 --- */
#topbar {
  display: flex; align-items: center; gap: 8px;
  padding: 0 10px; height: 46px; flex: 0 0 46px;
  background: var(--bg-2); border-bottom: 1px solid var(--line);
}
.icon-btn {
  background: none; border: 1px solid transparent; color: var(--fg-dim);
  border-radius: var(--radius); padding: 6px 9px; cursor: pointer; line-height: 1;
}
.icon-btn:hover { background: var(--bg-3); color: var(--fg); }
.icon-btn.sm { padding: 3px 6px; font-size: 12px; }
.project-picker { display: flex; align-items: center; gap: 4px; }
#project-select {
  background: var(--bg-3); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 6px 8px; max-width: 15rem; cursor: pointer;
}
#viewnav { display: flex; gap: 2px; margin-left: 6px; }
#viewnav button {
  background: none; border: none; color: var(--fg-dim); cursor: pointer;
  padding: 7px 11px; border-radius: var(--radius);
}
#viewnav button:hover { background: var(--bg-3); }
#viewnav button.on { background: var(--bg-3); color: var(--accent); }
#save-indicator { font-size: 12px; color: var(--fg-faint); min-width: 4.5rem; text-align: right; }
#save-indicator.dirty { color: var(--warn); }
#save-indicator.saving { color: var(--accent-2); }

/* ------------------------------------------------------------------ 레이아웃 --- */
#app { flex: 1; display: flex; min-height: 0; }
#sidebar {
  width: 250px; flex: 0 0 250px; background: var(--bg-2);
  border-right: 1px solid var(--line); display: flex; flex-direction: column; min-height: 0;
}
#inspector {
  width: 310px; flex: 0 0 310px; background: var(--bg-2);
  border-left: 1px solid var(--line); display: flex; flex-direction: column; min-height: 0;
}
#stage { flex: 1; min-width: 0; display: flex; position: relative; }
.view { display: none; flex: 1; min-width: 0; flex-direction: column; overflow: auto; }
body[data-view="write"]    #view-write,
body[data-view="board"]    #view-board,
body[data-view="codex"]    #view-codex,
body[data-view="timeline"] #view-timeline,
body[data-view="stats"]    #view-stats { display: flex; }

body.hide-sidebar #sidebar { display: none; }
body.focus-mode #sidebar, body.focus-mode #inspector, body.focus-mode #topbar { display: none; }
body.focus-mode .ed-head { opacity: .25; }
body.focus-mode .ed-head:hover { opacity: 1; }
body:not([data-view="write"]) #inspector { display: none; }

/* -------------------------------------------------------------- 회차 목록 --- */
.side-head {
  display: flex; align-items: center; justify-content: space-between;
  padding: 10px 12px 6px; font-size: 12px; color: var(--fg-dim);
}
.side-actions { display: flex; gap: 4px; }
.filter {
  margin: 0 10px 8px; padding: 6px 9px; background: var(--bg-3);
  border: 1px solid var(--line); border-radius: var(--radius);
}
.ep-list { flex: 1; overflow: auto; padding: 0 6px 10px; }
.vol-group { margin-bottom: 6px; }
.vol-head {
  display: flex; align-items: center; gap: 6px; padding: 6px 8px;
  font-size: 11px; color: var(--fg-faint); text-transform: uppercase; letter-spacing: .04em;
}
.vol-head .vol-title { cursor: text; }
.ep-item {
  display: flex; align-items: baseline; gap: 7px; padding: 7px 9px;
  border-radius: var(--radius); cursor: pointer; border: 1px solid transparent;
}
.ep-item:hover { background: var(--bg-3); }
.ep-item.on { background: var(--bg-3); border-color: color-mix(in srgb, var(--accent) 45%, transparent); }
.ep-item.drag-over { border-top: 2px solid var(--accent); }
.ep-item .no { color: var(--fg-faint); font-variant-numeric: tabular-nums; font-size: 12px; min-width: 2.2em; }
.ep-item .nm { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.ep-item .ct { font-size: 11px; color: var(--fg-faint); font-variant-numeric: tabular-nums; }
.ep-item .dot { width: 6px; height: 6px; border-radius: 50%; flex: 0 0 6px; }
.ep-more {
  background: none; border: none; color: var(--fg-faint); cursor: pointer;
  padding: 2px 4px; line-height: 1; opacity: 0; transition: opacity .15s; flex: 0 0 auto;
}
.ep-item:hover .ep-more, .ep-item.on .ep-more { opacity: 1; }
.ep-more:hover { color: var(--fg); }
.dot.draft { background: var(--fg-faint); }
.dot.done { background: var(--ok); }
.dot.published { background: var(--accent-2); }
.side-foot { padding: 8px 12px; border-top: 1px solid var(--line); font-size: 11px; color: var(--fg-faint); }

/* ---------------------------------------------------------------- 에디터 --- */
.ed-head {
  display: flex; gap: 8px; align-items: center; padding: 10px 16px;
  border-bottom: 1px solid var(--line); transition: opacity .2s;
}
.ed-head input, .ed-head select {
  background: transparent; border: 1px solid transparent; border-radius: var(--radius); padding: 5px 7px;
}
.ed-head input:hover, .ed-head select:hover { border-color: var(--line); }
.ed-head input:focus, .ed-head select:focus { border-color: var(--accent); outline: none; background: var(--bg-2); }
.ep-number { width: 4.5rem; color: var(--fg-dim); font-variant-numeric: tabular-nums; }
.ep-title { flex: 1; font-size: 15px; font-weight: 600; }
#ep-due { color: var(--fg-dim); font-size: 12px; }

.ed-wrap { flex: 1; overflow: auto; display: flex; justify-content: center; }
#editor {
  width: 100%; max-width: var(--ed-width); background: transparent; border: none;
  outline: none; resize: none; padding: 40px 24px 45vh;
  font-family: var(--editor-font); font-size: var(--ed-size); line-height: var(--ed-line);
  color: var(--fg); letter-spacing: .01em;
}
#editor::placeholder { color: var(--fg-faint); }

.ed-foot {
  display: flex; align-items: center; gap: 12px; padding: 7px 16px;
  border-top: 1px solid var(--line); font-size: 12px; color: var(--fg-dim);
  font-variant-numeric: tabular-nums;
}
.gauge { width: 110px; height: 4px; background: var(--bg-3); border-radius: 2px; overflow: hidden; }
.gauge-fill { height: 100%; width: 0; background: var(--accent); transition: width .25s, background .25s; }
.gauge-fill.over { background: var(--ok); }
.gauge-fill.way-over { background: var(--warn); }

/* ------------------------------------------------------------- 인스펙터 --- */
.sheet-close { display: none; }
.tabs { display: flex; border-bottom: 1px solid var(--line); }
.tabs button {
  flex: 1; background: none; border: none; color: var(--fg-dim); cursor: pointer;
  padding: 10px 4px; border-bottom: 2px solid transparent; font-size: 13px;
}
.tabs button:hover { color: var(--fg); }
.tabs button.on { color: var(--accent); border-bottom-color: var(--accent); }
.insp-body { flex: 1; overflow: auto; padding: 10px; }

.card {
  background: var(--bg-3); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 9px 11px; margin-bottom: 7px;
}
.card.click { cursor: pointer; }
.card.click:hover { border-color: color-mix(in srgb, var(--accent) 50%, transparent); }
.card h4 { margin: 0 0 3px; font-size: 13px; display: flex; align-items: center; gap: 6px; }
.card p { margin: 0; font-size: 12px; color: var(--fg-dim); line-height: 1.5; }
.tag {
  font-size: 10px; padding: 1px 6px; border-radius: 20px;
  background: color-mix(in srgb, var(--accent-2) 22%, transparent); color: var(--accent-2);
}
.tag.warn { background: color-mix(in srgb, var(--warn) 22%, transparent); color: var(--warn); }
.tag.bad  { background: color-mix(in srgb, var(--bad) 22%, transparent); color: var(--bad); }
.tag.ok   { background: color-mix(in srgb, var(--ok) 22%, transparent); color: var(--ok); }

.row { display: flex; gap: 6px; align-items: center; }
.row.wrap { flex-wrap: wrap; }
.btn {
  background: var(--bg-3); border: 1px solid var(--line); color: var(--fg);
  border-radius: var(--radius); padding: 7px 12px; cursor: pointer;
}
.btn:hover { border-color: var(--accent); }
.btn.primary { background: var(--accent); color: #17150f; border-color: var(--accent); font-weight: 600; }
.btn.ghost { background: none; }
.btn.danger { color: var(--bad); }
.btn:disabled { opacity: .45; cursor: default; }
.btn.sm { padding: 4px 8px; font-size: 12px; }

.field { display: flex; flex-direction: column; gap: 4px; margin-bottom: 10px; }
.field label { font-size: 11px; color: var(--fg-dim); }
.field input, .field select, .field textarea {
  background: var(--bg-3); border: 1px solid var(--line);
  border-radius: var(--radius); padding: 7px 9px; width: 100%;
}
.field textarea { min-height: 80px; resize: vertical; line-height: 1.6; }
.field input:focus, .field textarea:focus, .field select:focus { outline: none; border-color: var(--accent); }
.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }

.empty { color: var(--fg-faint); font-size: 12px; text-align: center; padding: 26px 10px; line-height: 1.7; }
.sec-title {
  font-size: 11px; color: var(--fg-faint); text-transform: uppercase;
  letter-spacing: .05em; margin: 14px 0 7px;
}
.sec-title:first-child { margin-top: 0; }

/* --------------------------------------------------------------- 큰 뷰 --- */
.page { padding: 22px 26px; max-width: 1100px; width: 100%; margin: 0 auto; }
.page h2 { margin: 0 0 4px; font-size: 19px; }
.page .sub { color: var(--fg-dim); font-size: 12px; margin-bottom: 18px; }

.board { display: flex; gap: 14px; align-items: flex-start; overflow-x: auto; padding-bottom: 20px; }
.board-col { flex: 0 0 260px; }
.board-col > h3 { font-size: 12px; color: var(--fg-dim); margin: 0 0 8px; }
.board-card {
  background: var(--bg-2); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 10px; margin-bottom: 8px; cursor: pointer;
}
.board-card:hover { border-color: var(--accent); }
.board-card .bc-no { font-size: 11px; color: var(--fg-faint); }
.board-card .bc-tt { font-size: 13px; font-weight: 600; margin: 2px 0 5px; }
.board-card .bc-sy { font-size: 12px; color: var(--fg-dim); line-height: 1.5;
  display: -webkit-box; -webkit-line-clamp: 4; -webkit-box-orient: vertical; overflow: hidden; }

.codex-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); gap: 10px; }
.tl { border-left: 2px solid var(--line); margin-left: 10px; padding-left: 18px; }
.tl-item { position: relative; margin-bottom: 16px; }
.tl-item::before {
  content: ''; position: absolute; left: -25px; top: 5px; width: 9px; height: 9px;
  border-radius: 50%; background: var(--accent); border: 2px solid var(--bg);
}

.stat-row { display: grid; grid-template-columns: repeat(auto-fit, minmax(130px, 1fr)); gap: 10px; margin-bottom: 20px; }
.stat {
  background: var(--bg-2); border: 1px solid var(--line);
  border-radius: var(--radius); padding: 12px 14px;
}
.stat .v { font-size: 22px; font-weight: 600; font-variant-numeric: tabular-nums; }
.stat .k { font-size: 11px; color: var(--fg-dim); margin-top: 2px; }
.heat {
  display: grid; grid-auto-flow: column;
  grid-template-rows: repeat(7, 12px); grid-auto-columns: 12px;
  gap: 3px; justify-content: start; overflow-x: auto; padding-bottom: 6px;
}
.heat i { width: 12px; height: 12px; border-radius: 2px; background: var(--bg-3); display: block; }

table.tbl { width: 100%; border-collapse: collapse; font-size: 13px; }
table.tbl th, table.tbl td { text-align: left; padding: 7px 9px; border-bottom: 1px solid var(--line); }
table.tbl th { color: var(--fg-dim); font-weight: 500; font-size: 11px; }
table.tbl tr.click { cursor: pointer; }
table.tbl tr.click:hover td { background: var(--bg-2); }
.num { text-align: right !important; font-variant-numeric: tabular-nums; }

/* ---------------------------------------------------------------- 모달 --- */
.overlay {
  position: fixed; inset: 0; background: rgba(0,0,0,.55); backdrop-filter: blur(2px);
  display: flex; align-items: center; justify-content: center; z-index: 50; padding: 20px;
}
.modal {
  background: var(--bg-2); border: 1px solid var(--line); border-radius: 12px;
  width: 100%; max-width: 560px; max-height: 86vh; display: flex; flex-direction: column;
  box-shadow: 0 20px 60px rgba(0,0,0,.5);
}
.modal.wide { max-width: 860px; }
.modal-head { padding: 14px 18px; border-bottom: 1px solid var(--line); display: flex; align-items: center; }
.modal-head h3 { margin: 0; font-size: 15px; }
.modal-body { padding: 16px 18px; overflow: auto; }
.modal-foot { padding: 12px 18px; border-top: 1px solid var(--line); display: flex; gap: 8px; justify-content: flex-end; }

#toast-root { position: fixed; bottom: 18px; left: 50%; transform: translateX(-50%); z-index: 90;
  display: flex; flex-direction: column; gap: 6px; align-items: center; pointer-events: none; }
.toast {
  background: var(--bg-3); border: 1px solid var(--line); color: var(--fg);
  padding: 9px 16px; border-radius: 20px; font-size: 13px; box-shadow: 0 6px 20px rgba(0,0,0,.4);
  animation: pop .18s ease-out;
}
.toast.bad { border-color: var(--bad); color: var(--bad); }
.toast.ok { border-color: var(--ok); }
@keyframes pop { from { opacity: 0; transform: translateY(8px); } }

.hit { padding: 6px 8px; border-radius: 6px; cursor: pointer; font-size: 12px; line-height: 1.6; }
.hit:hover { background: var(--bg-3); }
.hit mark { background: color-mix(in srgb, var(--accent) 45%, transparent); color: var(--fg); border-radius: 2px; }

/* ----------------------------------------------------------------- 모바일 --- */
#btn-inspector { display: none; }

@media (max-width: 900px) {
  /* --- 상단바: 작품 + 도구만. 뷰 전환은 아래 탭바로 --- */
  #topbar { gap: 2px; padding: 0 6px; overflow: hidden;
            padding-top: env(safe-area-inset-top); height: calc(46px + env(safe-area-inset-top));
            flex-basis: calc(46px + env(safe-area-inset-top)); }
  #save-indicator, #btn-focus, #btn-prefs { display: none; }
  #btn-inspector { display: block; }
  #project-select { flex: 1 1 auto; max-width: none; min-width: 0; }
  .icon-btn { padding: 8px 7px; }

  /* --- 하단 탭바 --- */
  body { padding-bottom: calc(50px + env(safe-area-inset-bottom)); }
  #viewnav {
    position: fixed; left: 0; right: 0; bottom: 0; z-index: 30;
    margin: 0; height: calc(50px + env(safe-area-inset-bottom)); background: var(--bg-2);
    border-top: 1px solid var(--line); gap: 0;
    padding-bottom: env(safe-area-inset-bottom);
  }
  #viewnav button { flex: 1; border-radius: 0; font-size: 12px; padding: 0; }
  #viewnav button.on { background: none; }
  #toast-root { bottom: calc(78px + env(safe-area-inset-bottom)); }

  /* 타이핑 중에는 탭바를 숨겨 화면을 넓게 쓴다 */
  body.typing { padding-bottom: 0; }
  body.typing #viewnav { display: none; }

  /* --- 회차 목록: 왼쪽에서 나오는 서랍 --- */
  #sidebar {
    position: fixed; top: 0; bottom: 0; left: 0; z-index: 45;
    width: 82vw; max-width: 320px; flex: none; height: 100%;
    padding-top: env(safe-area-inset-top);
    transform: translateX(-100%); transition: transform .22s ease;
    box-shadow: 0 0 40px rgba(0,0,0,.55); display: flex;
  }
  body.show-sidebar #sidebar { transform: none; }

  /* --- 인스펙터: 아래에서 올라오는 시트 --- */
  #inspector {
    display: flex !important;
    position: fixed; left: 0; right: 0; bottom: 0; top: auto; z-index: 45;
    width: auto; flex: none; height: 78dvh;
    border-left: none; border-top: 1px solid var(--line);
    border-radius: 16px 16px 0 0; overflow: hidden;
    transform: translateY(101%); transition: transform .24s ease;
    box-shadow: 0 -10px 40px rgba(0,0,0,.55);
    padding-bottom: env(safe-area-inset-bottom);
  }
  body.show-inspector #inspector { transform: none; }
  body:not([data-view="write"]) #inspector { transform: translateY(101%); }
  .sheet-close {
    display: block; position: absolute; right: 6px; top: 4px; z-index: 2;
    background: none; border: none; color: var(--fg-dim); font-size: 16px;
    padding: 10px 12px; cursor: pointer;
  }
  #insp-tabs { padding-right: 42px; }

  /* --- 서랍/시트용 배경막 --- */
  body.show-sidebar #scrim, body.show-inspector #scrim {
    display: block; position: fixed; inset: 0; z-index: 40;
    background: rgba(0,0,0,.5); backdrop-filter: blur(1px);
  }

  /* --- 에디터 --- */
  .ed-head { flex-wrap: wrap; padding: 8px 12px; gap: 6px; }
  .ep-title { flex: 1 1 100%; order: -1; font-size: 16px; }
  #editor { padding: 20px 16px 40vh; }
  .ed-foot { gap: 8px; font-size: 11px; padding: 6px 12px; }
  .ed-foot .gauge { width: 54px; }
  #count-extra { display: none; }

  /* iOS: 16px 미만 입력란은 포커스 시 화면이 확대된다 */
  input, select, textarea { font-size: 16px; }
  .filter, .field input, .field select, .field textarea,
  .modal input, .modal select, #ep-due, #ep-number { font-size: 16px; }

  .page { padding: 16px 14px; }
  .modal { max-width: none; max-height: 88dvh; border-radius: 14px; }
  .overlay { padding: 10px; align-items: flex-end; }
  .modal-foot { position: sticky; bottom: 0; background: var(--bg-2); }
  .grid-2 { grid-template-columns: 1fr; }
  .board { display: block; }
  .board-col { width: 100%; }
  .codex-grid { grid-template-columns: 1fr; }
  table.tbl { font-size: 12px; }
  table.tbl th, table.tbl td { padding: 6px 5px; }

  /* 터치 대상 최소 크기 */
  .ep-item { padding: 11px 9px; }
  .hit { padding: 10px 8px; }
}

/* 터치 기기: 마우스 호버가 없으므로 회차 메뉴 버튼을 항상 보여준다 */
@media (hover: none) {
  .ep-more { opacity: 1 !important; }
}
