/* ------------------------------------------------------------------ 기본 */

:root {
  --bg: #f7f7f8;
  --surface: #ffffff;
  --surface-2: #f1f1f4;
  --border: #e0e0e6;
  --text: #1c1c1e;
  --muted: #6b6b75;
  --primary: #4a5d7e;
  --primary-soft: #eaeff7;
  --ok: #2e7d5b;
  --err: #c62828;
  --err-bg: #fdecea;
  --pending: #8a8a95;
  --radius: 12px;
  --scribe-size: 18px;
  color-scheme: light dark;
}

/* 색은 여기서만 다시 정의한다. 값이 한 곳에만 있으면 두 모드가 갈라지지 않는다. */
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #17171a;
    --surface: #1f1f23;
    --surface-2: #26262c;
    --border: #34343c;
    --text: #ececf0;
    --muted: #9a9aa5;
    --primary: #8fa6cc;
    --primary-soft: #262d3a;
    --ok: #6fc79b;
    --err: #ff7b72;
    --err-bg: #3a2020;
    --pending: #7a7a86;
  }
}

* { box-sizing: border-box; }

/* 클래스로 만든 규칙이 기본 hidden을 이겨버리는 일을 막는다 */
[hidden] { display: none !important; }

body {
  margin: 0;
  padding: 0 16px 48px;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Apple SD Gothic Neo", "Malgun Gothic", "맑은 고딕", sans-serif;
  font-size: 15px;
  line-height: 1.6;
  -webkit-text-size-adjust: 100%;
}

main { max-width: 780px; margin: 0 auto; }

h1, h2 { line-height: 1.3; }
h2 { font-size: 1rem; margin: 0 0 8px; }
p { margin: 0 0 10px; }
.muted { color: var(--muted); font-size: 0.87rem; }
.grow { flex: 1; }

/* ------------------------------------------------------------------ 머리말 */

.top { max-width: 780px; margin: 0 auto; padding: 18px 0 10px; }
.top h1 { font-size: 1.25rem; margin: 0; letter-spacing: -0.01em; }
.top__sub { margin: 2px 0 0; color: var(--muted); font-size: 0.85rem; }

.mode-tabs {
  position: sticky; top: 0; z-index: 5;
  display: flex; gap: 4px;
  max-width: 780px; margin: 0 auto 14px;
  padding: 6px 0;
  background: var(--bg);
  overflow-x: auto;
}
.mode-tabs button {
  flex: 1 0 auto;
  padding: 8px 12px;
  border: 1px solid var(--border);
  border-radius: 999px;
  background: var(--surface);
  color: var(--muted);
  font: inherit; font-size: 0.9rem;
  cursor: pointer;
}
.mode-tabs button[aria-selected="true"] {
  background: var(--primary-soft);
  border-color: var(--primary);
  color: var(--primary);
  font-weight: 600;
}

/* ------------------------------------------------------------------ 카드 */

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px;
  margin-bottom: 12px;
}

.card--hero { text-align: center; }
.hero__row { display: flex; justify-content: space-around; margin-bottom: 12px; }
.hero__stat { min-width: 70px; }
.hero__num { font-size: 1.7rem; font-weight: 700; line-height: 1.1; }
.hero__label { color: var(--muted); font-size: 0.8rem; }

label { display: block; margin-bottom: 10px; font-size: 0.9rem; color: var(--muted); }
input[type="text"], input[type="number"], select, textarea {
  display: block; width: 100%; margin-top: 4px;
  padding: 9px 10px;
  border: 1px solid var(--border); border-radius: 10px;
  background: var(--surface); color: var(--text);
  font: inherit; font-size: 16px; /* iOS에서 입력 시 화면이 확대되지 않는 최소 크기 */
}
textarea { resize: vertical; line-height: 1.6; }
.check { display: flex; align-items: center; gap: 8px; color: var(--text); }
.check input { width: auto; margin: 0; }
.row { display: flex; gap: 8px; flex-wrap: wrap; align-items: flex-end; }
.grid2 { display: grid; grid-template-columns: 2fr 1fr; gap: 8px; }
@media (max-width: 420px) { .grid2 { grid-template-columns: 1fr; } }

/* ------------------------------------------------------------------ 버튼 */

.btn {
  padding: 9px 14px;
  border: 1px solid var(--border); border-radius: 10px;
  background: var(--surface); color: var(--text);
  font: inherit; font-size: 0.92rem; cursor: pointer;
}
.btn:hover { background: var(--surface-2); }
.btn--primary { background: var(--primary); border-color: var(--primary); color: #fff; font-weight: 600; }
.btn--primary:hover { filter: brightness(1.08); background: var(--primary); }
.btn--wide { width: 100%; padding: 12px; font-size: 1rem; }
.btn--quiet { color: var(--muted); }
.btn--icon { padding: 6px 12px; font-size: 1.1rem; line-height: 1; }
.btn:disabled { opacity: 0.5; cursor: default; }

/* ------------------------------------------------------------------ 진도 막대 */

.bar { height: 8px; background: var(--surface-2); border-radius: 999px; overflow: hidden; margin: 6px 0; }
.bar--thin { height: 4px; margin: 0 0 12px; }
.bar__fill { height: 100%; width: 0; background: var(--primary); transition: width 0.25s; }

/* ------------------------------------------------------------------ 필사 화면 */

.write__head { display: flex; align-items: flex-start; gap: 10px; margin-bottom: 6px; }
.write__where { font-size: 1.05rem; font-weight: 600; }
.write__nav { display: flex; gap: 6px; }
.write__foot { display: flex; align-items: center; gap: 8px; margin-top: 10px; }
.hint { margin-top: 10px; }

/* 원문 — 이미 쓴 부분은 흐려지고, 지금 칠 자리부터 진해진다 */
.source {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px;
  margin-bottom: 10px;
  font-size: var(--scribe-size);
  line-height: 1.75;
  white-space: pre-wrap;
  overflow-wrap: break-word;
  word-break: break-word;
}
.source .ch--todo { color: var(--text); }
.source .ch--ok { color: var(--muted); opacity: 0.55; }
.source .ch--pending { color: var(--text); }
.source .ch--err { color: var(--err); background: var(--err-bg); border-radius: 3px; }

/* 입력 — textarea와 레이어는 반드시 한 규칙에서 같은 값을 받는다.
   따로 적으면 언젠가 한쪽만 바뀌고, 그 순간 글자가 어긋나 보인다. */
.scribe { position: relative; }
.scribe__layer,
.scribe__input {
  width: 100%;
  min-height: 108px;
  margin: 0;
  padding: 14px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  font-family: inherit;
  font-size: var(--scribe-size);
  line-height: 1.75;
  letter-spacing: normal;
  white-space: pre-wrap;
  overflow-wrap: break-word;
  word-break: break-word;
}
.scribe__layer {
  position: absolute; inset: 0;
  background: var(--surface);
  overflow: hidden;
  pointer-events: none;
}
.scribe__input {
  position: relative;
  display: block;
  background: transparent;
  color: transparent;
  -webkit-text-fill-color: transparent; /* iOS는 color만으로는 글자가 남는다 */
  caret-color: var(--text);
  resize: none;
}
.scribe__input:focus { outline: 2px solid var(--primary); outline-offset: 1px; }

.ch--ok { color: var(--text); }
.ch--pending { color: var(--pending); border-bottom: 1px dotted var(--pending); }
.ch--err, .ch--extra { color: var(--err); background: var(--err-bg); border-radius: 3px; }

/* ------------------------------------------------------------------ 목차 */

.seg { display: flex; gap: 4px; margin-bottom: 12px; }
.seg__btn {
  flex: 1; padding: 7px 6px;
  border: 1px solid var(--border); border-radius: 8px;
  background: var(--surface); color: var(--muted);
  font: inherit; font-size: 0.85rem; cursor: pointer;
}
.seg__btn.is-on { background: var(--primary-soft); border-color: var(--primary); color: var(--primary); font-weight: 600; }

.booklist { display: flex; flex-direction: column; gap: 6px; }
.book {
  border: 1px solid var(--border); border-radius: 10px;
  background: var(--surface); overflow: hidden;
}
.book__head {
  display: flex; align-items: center; gap: 8px;
  width: 100%; padding: 10px 12px;
  border: 0; background: none; color: var(--text);
  font: inherit; text-align: left; cursor: pointer;
}
.book__name { font-weight: 600; }
.book__meta { margin-left: auto; color: var(--muted); font-size: 0.82rem; }
.book__body { padding: 0 12px 12px; }
.chapters { display: flex; flex-wrap: wrap; gap: 4px; }
.chip {
  min-width: 34px; padding: 5px 6px;
  border: 1px solid var(--border); border-radius: 7px;
  background: var(--surface-2); color: var(--muted);
  font: inherit; font-size: 0.8rem; text-align: center; cursor: pointer;
}
.chip.has { color: var(--text); background: var(--surface); }
.chip.part { background: var(--primary-soft); color: var(--primary); border-color: var(--primary); }
.chip.full { background: var(--primary); border-color: var(--primary); color: #fff; }
.chip:disabled { cursor: default; opacity: 0.45; }

/* ------------------------------------------------------------------ 가져오기 */

.badge {
  display: inline-block; padding: 2px 8px; border-radius: 999px;
  background: var(--surface-2); color: var(--muted); font-size: 0.78rem; font-weight: 400;
}
.badge.good { background: var(--primary-soft); color: var(--primary); }
.badge.warn { background: var(--err-bg); color: var(--err); }

.preview { max-height: 340px; overflow-y: auto; border: 1px solid var(--border); border-radius: 10px; }
.pv {
  display: flex; gap: 6px; align-items: flex-start;
  padding: 6px 8px; border-bottom: 1px solid var(--border);
}
.pv:last-child { border-bottom: 0; }
.pv.bad { background: var(--err-bg); }
.pv__n { min-width: 28px; color: var(--muted); font-size: 0.8rem; padding-top: 3px; }
.pv__t {
  flex: 1; min-width: 0;
  border: 0; background: none; color: var(--text);
  font: inherit; font-size: 0.9rem; line-height: 1.5;
  resize: none; overflow: hidden; padding: 2px 0;
}
.pv__t:focus { outline: 1px solid var(--primary); border-radius: 4px; }
.pv__btns { display: flex; gap: 2px; }
.pv__btn {
  border: 1px solid var(--border); border-radius: 6px;
  background: var(--surface); color: var(--muted);
  font: inherit; font-size: 0.75rem; padding: 2px 6px; cursor: pointer;
}

.dropped { margin: 6px 0; font-size: 0.8rem; color: var(--muted); }
.dropped span { display: inline-block; margin: 2px 4px 2px 0; padding: 2px 6px; background: var(--surface-2); border-radius: 6px; text-decoration: line-through; }

.warn-list { margin: 0 0 8px; padding: 8px 10px; background: var(--err-bg); color: var(--err); border-radius: 8px; font-size: 0.85rem; }
.warn-list div { margin: 2px 0; }

.sourcelist { display: flex; flex-direction: column; gap: 6px; margin-bottom: 10px; }
.src-row { display: flex; align-items: center; gap: 8px; padding: 8px 10px; border: 1px solid var(--border); border-radius: 10px; }
.src-row .muted { margin-left: auto; }

/* ------------------------------------------------------------------ 기타 */

.notice {
  padding: 10px 12px; margin-bottom: 12px;
  background: var(--err-bg); color: var(--err);
  border-radius: 10px; font-size: 0.87rem;
}

/* 기록 — 숫자 넷을 한 줄에, 좁은 화면에서는 둘씩 */
.statgrid { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; margin: 10px 0 14px; }
@media (max-width: 460px) { .statgrid { grid-template-columns: repeat(2, 1fr); } }
.statcell { text-align: center; padding: 8px 4px; background: var(--surface-2); border-radius: 10px; }
.statcell__num { font-size: 1.25rem; font-weight: 700; line-height: 1.2; }
.statcell__unit { font-size: 0.72rem; font-weight: 400; color: var(--muted); margin-left: 2px; }
.statcell__label { font-size: 0.75rem; color: var(--muted); margin-top: 2px; }

/* 날짜별 막대 — 라이브러리 없이 div 높이로 그린다 */
.days { display: flex; align-items: flex-end; gap: 3px; height: 72px; margin-bottom: 6px; }
.days__col { flex: 1; display: flex; flex-direction: column; justify-content: flex-end; height: 100%; min-width: 0; }
.days__bar { background: var(--primary); border-radius: 3px 3px 0 0; min-height: 2px; }
.days__bar.is-empty { background: var(--border); }
.days__bar.is-today { background: var(--ok); }

/* 날짜별 기록 — 하루 한 줄 */
.daylist { margin-top: 12px; border-top: 1px solid var(--border); }
.dayrow {
  display: flex; align-items: baseline; gap: 8px;
  padding: 7px 2px; border-bottom: 1px solid var(--border);
  font-size: 0.85rem;
}
.dayrow--today { background: var(--primary-soft); border-radius: 8px; padding-left: 8px; padding-right: 8px; }
.dayrow__date { min-width: 74px; color: var(--muted); }
.dayrow--today .dayrow__date { color: var(--primary); font-weight: 600; }
.dayrow__main { font-weight: 600; min-width: 52px; }
.dayrow__rest { margin-left: auto; color: var(--muted); text-align: right; }
.daylist__more { padding: 8px 2px; color: var(--muted); font-size: 0.82rem; text-align: center; }

.recent { display: flex; flex-direction: column; gap: 4px; margin-top: 8px; }
.recent button {
  display: flex; gap: 8px; width: 100%; padding: 8px 10px;
  border: 1px solid var(--border); border-radius: 10px;
  background: var(--surface); color: var(--text);
  font: inherit; font-size: 0.88rem; text-align: left; cursor: pointer;
}

.toast {
  position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%);
  max-width: calc(100% - 32px);
  padding: 10px 16px; border-radius: 999px;
  background: var(--text); color: var(--bg);
  font-size: 0.88rem; z-index: 20;
}

code { background: var(--surface-2); padding: 1px 5px; border-radius: 5px; font-family: ui-monospace, SFMono-Regular, Consolas, monospace; font-size: 0.85em; }
