:root {
  --bg: #fafaf8; --panel: #f1f0ec; --paper: #ffffff; --text: #1f1f1d; --mut: #77756f;
  --line: #e0ded8; --acc: #3b6fd9; --acc-soft: #e4ecfb; --danger: #c8402f;
  --ins: #d6f2d8; --del: #fbdcd8;
  --fs: 17px; --lh: 1.9; --w: 760px; --ref: 380px; --refm: 42%;
  --ff: "Pretendard", "Apple SD Gothic Neo", "Noto Sans KR", "Malgun Gothic", sans-serif;
  --ui: "Pretendard", "Apple SD Gothic Neo", "Noto Sans KR", "Malgun Gothic", system-ui, sans-serif;
}
[data-theme="sepia"] {
  --bg: #f6efe0; --panel: #ede3cf; --paper: #fbf5e8; --text: #3a3226; --mut: #85775f;
  --line: #ddd0b6; --acc: #a2602a; --acc-soft: #f0dfc6; --ins: #dcecc4; --del: #f3d3c2;
}
[data-theme="dark"] {
  --bg: #1b1c1f; --panel: #232529; --paper: #1f2023; --text: #d9d9d6; --mut: #8b8d93;
  --line: #34363c; --acc: #7aa2f7; --acc-soft: #2a3348; --danger: #f07a6a; --ins: #1f4428; --del: #55262a;
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; overflow: hidden; overflow: clip; background: var(--bg); color: var(--text); font-family: var(--ui); font-size: 14px; }
button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; background: none; border: 0; padding: 0; }
input, select, textarea { background: var(--paper); border: 1px solid var(--line); border-radius: 8px; padding: 8px 10px; outline: none; }
input:focus, select:focus, textarea:focus { border-color: var(--acc); }
.sp { flex: 1; }

#app { display: flex; height: var(--vh, 100dvh); overflow: hidden; overflow: clip; }

/* ── 사이드바 ── */
#side { width: 264px; flex: none; background: var(--panel); border-right: 1px solid var(--line); display: flex; flex-direction: column; min-height: 0; }
#workBtn { text-align: left; padding: 14px 16px 10px; display: flex; flex-direction: column; gap: 2px; }
#workName { font-size: 17px; font-weight: 700; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
#workBtn small { color: var(--mut); font-size: 12px; }
#tabs { display: flex; padding: 0 10px; gap: 2px; border-bottom: 1px solid var(--line); }
#tabs button { flex: 1; padding: 9px 0; font-size: 13px; color: var(--mut); border-bottom: 2px solid transparent; }
#tabs button.on { color: var(--text); font-weight: 700; border-color: var(--acc); }
#list { flex: 1; overflow-y: auto; padding: 6px; min-height: 0; }
.li { display: flex; align-items: center; gap: 4px; padding: 0 4px 0 10px; border-radius: 8px; min-height: 38px; cursor: pointer; }
.li:hover { background: color-mix(in srgb, var(--text) 6%, transparent); }
.li.on { background: var(--acc-soft); font-weight: 600; }
.li-t { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.li-c { color: var(--mut); font-size: 12px; font-weight: 400; font-variant-numeric: tabular-nums; }
.li .star { color: var(--acc); font-size: 12px; }
.empty { color: var(--mut); padding: 18px 12px; line-height: 1.6; }
.ib { width: 32px; height: 32px; border-radius: 8px; color: var(--mut); flex: none; font-size: 16px; }
.ib:hover { background: color-mix(in srgb, var(--text) 9%, transparent); color: var(--text); }
.add { margin: 6px 10px; padding: 9px; border: 1px dashed var(--line); border-radius: 8px; color: var(--mut); }
.add:hover { color: var(--acc); border-color: var(--acc); }
#total { padding: 4px 16px 8px; color: var(--mut); font-size: 12px; }
.side-foot { display: flex; border-top: 1px solid var(--line); }
.side-foot button { flex: 1; padding: 12px 0; color: var(--mut); }
.side-foot button:hover { color: var(--text); background: color-mix(in srgb, var(--text) 5%, transparent); }
#scrim { display: none; }
.side-closed #side { display: none; }

/* ── 본문 영역 ── */
#main { flex: 1; min-width: 0; display: flex; flex-direction: column; }
#top { display: flex; align-items: center; gap: 6px; padding: 6px 10px; border-bottom: 1px solid var(--line); }
#crumb { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; color: var(--mut); min-width: 0; }
#crumb b { color: var(--text); }
.tb { padding: 6px 11px; border-radius: 8px; border: 1px solid var(--line); background: var(--paper); white-space: nowrap; flex: none; }
.tb:hover { border-color: var(--acc); color: var(--acc); }
.tb.on { background: var(--acc-soft); border-color: var(--acc); color: var(--acc); }
.tb.pri, .pri { background: var(--acc); color: #fff; border: 1px solid var(--acc); border-radius: 8px; padding: 7px 14px; }
[data-theme="dark"] .pri { color: #14151a; }
.tb.danger { color: var(--danger); }

#marks { display: flex; gap: 6px; padding: 6px 10px; overflow-x: auto; border-bottom: 1px solid var(--line); align-items: center; scrollbar-width: none; }
#marks:empty { display: none; }
#marks .lbl { color: var(--mut); font-size: 12px; flex: none; }
.chip { display: flex; align-items: center; background: var(--panel); border: 1px solid var(--line); border-radius: 999px; flex: none; font-size: 13px; }
.chip .go { padding: 4px 4px 4px 11px; max-width: 180px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chip .mx { padding: 4px 9px 4px 4px; color: var(--mut); }
.chip:hover { border-color: var(--acc); }

#body { flex: 1; display: flex; min-height: 0; }
#view { flex: 1; min-width: 0; min-height: 0; display: flex; flex-direction: column; }
#split, #ref { display: none; }
.ref-open #split { display: block; width: 6px; cursor: col-resize; background: var(--line); flex: none; touch-action: none; }
.ref-open #split:hover { background: var(--acc); }
.ref-open #ref { display: flex; flex-direction: column; width: var(--ref); flex: none; background: var(--panel); min-height: 0; }
.ref-head { display: flex; gap: 6px; padding: 8px; border-bottom: 1px solid var(--line); align-items: center; }
.ref-head select { flex: 1; min-width: 0; }
#refBody { flex: 1; overflow-y: auto; padding: 14px 16px 40px; font-family: var(--ff); font-size: calc(var(--fs) - 2px); line-height: 1.75; white-space: pre-wrap; word-break: break-word; }
#refBody h3 { margin: 0 0 8px; font-size: 1.1em; }
#refBody h3:not(:first-child) { margin-top: 26px; padding-top: 18px; border-top: 1px solid var(--line); }
#refBody dt { color: var(--mut); font-size: .82em; margin-top: 10px; }
#refBody dd { margin: 0; }
#refBody dl { margin: 0; }

.head { display: flex; align-items: center; gap: 6px; padding: 10px 14px 4px; }
.head .no { font-weight: 700; font-size: 18px; white-space: nowrap; }
.head input { flex: 1; min-width: 0; border-color: transparent; background: transparent; font-size: 18px; font-weight: 700; padding: 6px; }
.head input:focus { border-color: var(--line); background: var(--paper); }
.tools { display: flex; gap: 6px; padding: 4px 14px 8px; overflow-x: auto; scrollbar-width: none; align-items: center; }
.tools .sep { width: 1px; height: 20px; background: var(--line); flex: none; }
.goal { height: 3px; background: var(--line); }
.goal i { display: block; height: 100%; width: 0; background: var(--acc); transition: width .3s; }
.goal i.done { background: #3aa765; }
.edwrap { flex: 1; position: relative; min-height: 0; }
.ed { position: absolute; inset: 0; width: 100%; height: 100%; resize: none; border: 0; border-radius: 0; background: var(--paper);
  font-family: var(--ff); font-size: var(--fs); line-height: var(--lh); word-break: keep-all; overflow-wrap: anywhere;
  padding: 28px max(18px, calc((100% - var(--w)) / 2)) 40vh; }
#measure { height: 0; visibility: hidden; pointer-events: none; overflow: hidden; padding-top: 28px; padding-bottom: 0; }

.sheet { flex: 1; overflow-y: auto; padding: 8px 18px 80px; }
.sheet-in { max-width: var(--w); margin: 0 auto; display: grid; grid-template-columns: repeat(3, 1fr); gap: 12px; }
.sheet label { display: flex; flex-direction: column; gap: 4px; color: var(--mut); font-size: 12px; grid-column: 1 / -1; }
.sheet label.s { grid-column: auto; }
.sheet textarea, .sheet input { color: var(--text); font-family: var(--ff); font-size: calc(var(--fs) - 1px); line-height: 1.7; resize: none; overflow: hidden; }

#status { display: flex; gap: 10px; padding: 5px 14px; border-top: 1px solid var(--line); color: var(--mut); font-size: 12.5px; font-variant-numeric: tabular-nums; white-space: nowrap; overflow-x: auto; scrollbar-width: none; }
#status b { color: var(--text); font-weight: 600; }

.welcome { margin: auto; max-width: 420px; padding: 24px; text-align: center; line-height: 1.7; }
.welcome h1 { font-size: 22px; margin: 0 0 6px; }
.welcome p { color: var(--mut); margin: 0 0 18px; }
.row { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.row > input, .row > select { flex: 1; min-width: 0; }

/* 집중 모드 */
.focus #side, .focus #marks, .focus .tools, .focus #scrim { display: none !important; }

/* ── 모달 · 메뉴 · 토스트 ── */
.modal-bg { position: fixed; inset: 0; background: rgba(0,0,0,.42); display: flex; align-items: center; justify-content: center; z-index: 50; padding: 16px; }
.modal { background: var(--bg); border-radius: 14px; width: min(560px, 100%); max-height: calc(var(--vh, 100dvh) - 32px); display: flex; flex-direction: column; box-shadow: 0 16px 50px rgba(0,0,0,.3); }
.modal.wide { width: min(920px, 100%); }
.mh { display: flex; align-items: center; padding: 10px 10px 10px 18px; border-bottom: 1px solid var(--line); }
.mh b { flex: 1; font-size: 16px; }
.mb { padding: 16px 18px 20px; overflow-y: auto; display: flex; flex-direction: column; gap: 12px; min-height: 0; }
.mb h4 { margin: 10px 0 0; font-size: 13px; color: var(--mut); border-bottom: 1px solid var(--line); padding-bottom: 6px; }
.mb .f { display: flex; flex-direction: column; gap: 4px; font-size: 12.5px; color: var(--mut); }
.mb .f > * { color: var(--text); font-size: 14px; }
.mb .chk { display: flex; gap: 8px; align-items: center; }
.mb .chk input { width: 17px; height: 17px; }
.mb textarea { resize: vertical; min-height: 90px; font-family: var(--ff); line-height: 1.7; }
.hint { color: var(--mut); font-size: 12.5px; line-height: 1.6; }
.err { color: var(--danger); white-space: pre-wrap; word-break: break-word; }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.wl { display: flex; align-items: center; gap: 6px; padding: 6px 6px 6px 12px; border: 1px solid var(--line); border-radius: 10px; background: var(--paper); }
.wl.on { border-color: var(--acc); }
.wl .t { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; cursor: pointer; font-weight: 600; }
.wl small { color: var(--mut); font-weight: 400; }

.hist { display: grid; grid-template-columns: 250px 1fr; gap: 12px; min-height: 0; flex: 1; }
.hist-l { overflow-y: auto; display: flex; flex-direction: column; gap: 4px; max-height: 60vh; }
.hist-l button { text-align: left; padding: 8px 10px; border-radius: 8px; border: 1px solid var(--line); background: var(--paper); }
.hist-l button.on { border-color: var(--acc); background: var(--acc-soft); }
.hist-l small { color: var(--mut); display: block; }
.hist-r { display: flex; flex-direction: column; gap: 8px; min-height: 0; }
.pre { flex: 1; overflow-y: auto; white-space: pre-wrap; word-break: break-word; background: var(--paper); border: 1px solid var(--line); border-radius: 8px; padding: 12px 14px; font-family: var(--ff); line-height: 1.75; max-height: 52vh; min-height: 120px; }
.pre ins { background: var(--ins); text-decoration: none; border-radius: 3px; }
.pre del { background: var(--del); border-radius: 3px; }
.pre h3, .pre h4 { margin: 14px 0 4px; }
.pre h3:first-child, .pre h4:first-child { margin-top: 0; }
.pre.md { white-space: normal; }
.pre.md p { margin: 6px 0; }
.pre.md ul { margin: 6px 0; padding-left: 20px; }
.seg { display: inline-flex; border: 1px solid var(--line); border-radius: 8px; overflow: hidden; }
.seg button { padding: 6px 12px; background: var(--paper); }
.seg button.on { background: var(--acc-soft); color: var(--acc); font-weight: 600; }

.menu { position: fixed; z-index: 60; background: var(--bg); border: 1px solid var(--line); border-radius: 10px; box-shadow: 0 8px 28px rgba(0,0,0,.2); padding: 5px; min-width: 170px; }
.menu button { display: block; width: 100%; text-align: left; padding: 9px 12px; border-radius: 7px; }
.menu button:hover { background: var(--acc-soft); }
.menu button.danger { color: var(--danger); }
#toast { position: fixed; left: 50%; bottom: 46px; transform: translateX(-50%); background: var(--text); color: var(--bg); padding: 9px 16px; border-radius: 999px; z-index: 70; opacity: 0; pointer-events: none; transition: opacity .2s; max-width: 90vw; text-align: center; }
#toast.on { opacity: .95; }
.spin { display: inline-block; width: 14px; height: 14px; border: 2px solid var(--line); border-top-color: var(--acc); border-radius: 50%; animation: sp .8s linear infinite; vertical-align: -2px; }
@keyframes sp { to { transform: rotate(360deg); } }

/* ── 휴대폰 ── */
@media (max-width: 800px) {
  #side { position: fixed; inset: 0 auto 0 0; width: min(86vw, 320px); z-index: 30; transform: translateX(-102%); visibility: hidden; transition: transform .2s, visibility .2s; display: flex !important; padding-bottom: env(safe-area-inset-bottom); }
  .side-open #side { transform: none; visibility: visible; box-shadow: 8px 0 30px rgba(0,0,0,.25); }
  .side-open #scrim { display: block; position: fixed; inset: 0; background: rgba(0,0,0,.4); z-index: 20; }
  #body { flex-direction: column; }
  .ref-open #split { width: auto; height: 10px; cursor: row-resize; }
  .ref-open #ref { width: auto; height: var(--refm); }
  .ed { padding: 18px 18px 30vh; }
  #measure { padding-top: 18px; }
  .head { padding: 6px 8px 2px; }
  .tools { padding: 4px 8px 6px; }
  .sheet-in { grid-template-columns: 1fr 1fr; }
  .hist { grid-template-columns: 1fr; }
  .hist-l { max-height: 26vh; }
  .grid2 { grid-template-columns: 1fr; }
  .modal-bg { padding: 8px; align-items: flex-end; }
  .modal { max-height: calc(var(--vh, 100dvh) - 16px); }
  input, select, textarea { font-size: 16px; } /* iOS 자동 확대 방지 */
  .li { min-height: 44px; }
}

/* ── 연결(참조) ── */
.links { display: flex; gap: 6px; padding: 2px 14px 8px; overflow-x: auto; align-items: center; scrollbar-width: none; flex-wrap: nowrap; }
.links .lbl { color: var(--mut); font-size: 12px; flex: none; }
.links .hint { white-space: nowrap; }
.chip.ghost, .chip.sug { padding: 4px 11px; color: var(--mut); border-style: dashed; }
.chip.sug { color: var(--acc); border-color: var(--acc); background: var(--acc-soft); border-style: solid; }
.tag { color: var(--acc); background: var(--acc-soft); border-radius: 4px; padding: 0 5px; font-size: 11px; }
.cklist { display: flex; flex-direction: column; gap: 2px; max-height: 46vh; overflow-y: auto; }
.cklist .chk { padding: 5px 2px; }
.cklist h4 { margin: 8px 0 2px; }
#refBody a.rl { color: var(--acc); cursor: pointer; text-decoration: underline; text-decoration-color: color-mix(in srgb, var(--acc) 35%, transparent); text-underline-offset: 3px; }
#refBody .relbox { margin-top: 14px; padding-top: 10px; border-top: 1px dashed var(--line); font-size: .9em; color: var(--mut); line-height: 2; }
#refBody .relbox a.rl { margin-right: 8px; white-space: nowrap; }
.ib.sm { width: 26px; height: 26px; font-size: 13px; vertical-align: middle; }
.tb.sm { padding: 3px 9px; font-size: 12.5px; }

/* ── 프롬프트 만들기 ── */
.modal.xl { width: min(1180px, 100%); }
.pb { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); gap: 16px; min-height: 0; }
.pb-l, .pb-r { display: flex; flex-direction: column; gap: 10px; min-width: 0; }
.pb-r #pbOut { min-height: 42vh; font-size: 13px; line-height: 1.6; resize: vertical; font-family: var(--ff); }
.pb .pre { max-height: 40vh; }
.grid3 { display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 10px; }
input.num { width: 84px; padding: 4px 8px; }
.mb textarea#pb_req, .mb textarea#pbGuide { min-height: 70px; }

/* ── 캐릭터 구체화 ── */
.ca { display: flex; flex-direction: column; gap: 10px; }
.ca-row { border: 1px solid var(--line); border-radius: 10px; padding: 8px 10px; background: var(--paper); display: flex; flex-direction: column; gap: 6px; }
.ca-old { color: var(--mut); font-size: 12.5px; white-space: pre-wrap; }
.ca-row textarea { min-height: 60px; }

/* ── 업데이트 알림 ── */
#updBar { position: fixed; left: 50%; transform: translateX(-50%); bottom: calc(14px + env(safe-area-inset-bottom)); z-index: 80; display: flex; gap: 8px; align-items: center; background: var(--bg); border: 1px solid var(--acc); border-radius: 12px; padding: 8px 8px 8px 14px; box-shadow: 0 8px 28px rgba(0,0,0,.2); max-width: calc(100vw - 24px); }
#updBar[hidden] { display: none; }
ul.cl { margin: 0; padding-left: 20px; line-height: 1.8; }

@media (max-width: 800px) {
  .pb { grid-template-columns: 1fr; }
  .pb-r #pbOut { min-height: 30vh; }
  .grid3 { grid-template-columns: 1fr 1fr; }
  .links { padding: 2px 8px 6px; }
}
.mb .chk input.num { width: 84px; height: auto; }
.grid3 > * { min-width: 0; }
.grid3 input, .grid3 select { width: 100%; min-width: 0; }
