:root {
  --g: #5b7a55;
  --g-soft: #e6eedf;
  --on-g: #fff;
  --bg: #f6f4ef;
  --card: #fff;
  --field: #fcfbf8;
  --ink: #222;
  --mut: #6b6b64;
  --line: #e3dfd6;
  --warn: #c07a1f;
  --danger: #b3453a;
  --shadow: 0 4px 12px rgba(0, 0, 0, 0.22);
  --tabbar-h: 56px;
  /* top bar + tab bar, for sizing sticky side panels to the visible scroll area */
  --chrome-h: calc(52px + env(safe-area-inset-top) + var(--tabbar-h) + env(safe-area-inset-bottom));
  color-scheme: light dark;
}

@media (prefers-color-scheme: dark) {
  :root {
    --g: #8fb286;
    --g-soft: #2a3628;
    --on-g: #10150f;
    --bg: #171a16;
    --card: #21251f;
    --field: #1b1e1a;
    --ink: #ecebe4;
    --mut: #a3a69c;
    --line: #343a32;
    --warn: #e0a24a;
    --danger: #e8776b;
    --shadow: 0 4px 12px rgba(0, 0, 0, 0.5);
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

html, body { height: 100%; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font: 16px/1.5 -apple-system, BlinkMacSystemFont, "Apple SD Gothic Neo", "Noto Sans KR", "Malgun Gothic", sans-serif;
  -webkit-text-size-adjust: 100%;
  overscroll-behavior: none;
}

#shell {
  position: relative;
  display: flex;
  flex-direction: column;
  height: 100vh;
  height: 100dvh;
  max-width: 760px;
  margin: 0 auto;
  overflow: hidden;
}

/* ---------- top bar ---------- */
.topbar {
  display: flex;
  align-items: center;
  gap: 4px;
  padding: calc(env(safe-area-inset-top) + 6px) calc(8px + env(safe-area-inset-right)) 4px calc(16px + env(safe-area-inset-left));
  min-height: 52px;
}
.topbar h1 { flex: 1; margin: 0; font-size: 17px; font-weight: 700; }

.icon-btn, .sync {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 44px;
  height: 44px;
  border: 0;
  border-radius: 12px;
  background: transparent;
  color: var(--mut);
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}
.sync .dot {
  width: 12px;
  height: 12px;
  border-radius: 50%;
  background: var(--mut);
  box-shadow: 0 0 0 3px transparent;
}
.sync[data-state="ok"] .dot { background: var(--g); }
.sync[data-state="offline"] .dot { background: transparent; border: 2px solid var(--mut); }
.sync[data-state="needs-login"] .dot { background: var(--warn); }
.sync[data-state="error"] .dot { background: var(--warn); }
.sync[data-state="syncing"] .dot { background: var(--g); animation: pulse 1s ease-in-out infinite; }
@keyframes pulse { 50% { box-shadow: 0 0 0 5px var(--g-soft); } }
@media (prefers-reduced-motion: reduce) { .sync[data-state="syncing"] .dot { animation: none; } }

/* ---------- main ---------- */
#view {
  flex: 1;
  min-height: 0;
  overflow-x: hidden;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding: 4px calc(16px + env(safe-area-inset-right)) calc(var(--tabbar-h) + 88px) calc(16px + env(safe-area-inset-left));
  outline: none;
  /* A focused field scrolls clear of the sticky stage header (about 130px) and the floating button. */
  scroll-padding: 150px 0 96px;
}
body.is-login #view { padding-bottom: calc(24px + env(safe-area-inset-bottom)); }

.view-title { display: flex; align-items: center; gap: 8px; margin: 8px 0 12px; font-size: 20px; }
.hint { color: var(--mut); font-size: 13px; margin: 4px 0 8px; }
.hint.spaced { margin-top: 20px; }
.muted { color: var(--mut); font-size: 14px; }
.grow { flex: 1; }

.chip {
  display: inline-block;
  font-size: 12px;
  font-weight: 600;
  padding: 2px 9px;
  border-radius: 20px;
  background: var(--g-soft);
  color: var(--g);
}

/* ---------- cards ---------- */
.card {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 14px;
  padding: 14px;
  margin-bottom: 10px;
  overflow-wrap: anywhere;
}
.card > b { display: block; font-size: 12px; color: var(--g); margin-bottom: 4px; }
.card-link { display: block; color: inherit; text-decoration: none; -webkit-tap-highlight-color: transparent; }
.card-link:active { background: var(--field); }
.card-title { font-size: 16px; font-weight: 600; }
.card.empty { color: var(--mut); font-size: 14px; }
.inline-link { display: block; margin-top: 8px; padding: 10px 0; color: var(--g); font-weight: 600; text-decoration: none; }

.memo-preview {
  color: var(--ink);
  white-space: pre-wrap;
  display: -webkit-box;
  -webkit-line-clamp: 3;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

/* 7-segment progress bar (mockup) */
.prog { display: flex; gap: 3px; padding-top: 10px; }
.prog i { flex: 1; height: 4px; border-radius: 2px; background: var(--line); }
.prog i.on { background: var(--g); }

/* ---------- memos ---------- */
.memo-text { white-space: pre-wrap; overflow-wrap: anywhere; font-size: 16px; }
.meta { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; margin-top: 8px; color: var(--mut); font-size: 12px; }
.badge { padding: 2px 8px; border-radius: 20px; background: var(--g-soft); color: var(--g); font-weight: 600; }
.row-actions { display: flex; align-items: center; flex-wrap: wrap; gap: 8px; margin-top: 12px; }

.picker { margin-top: 12px; padding-top: 10px; border-top: 1px dashed var(--line); display: flex; flex-direction: column; gap: 8px; align-items: stretch; }
.pick-row {
  min-height: 48px;
  padding: 10px 14px;
  text-align: left;
  font: inherit;
  color: var(--ink);
  background: var(--field);
  border: 1px solid var(--line);
  border-radius: 12px;
  cursor: pointer;
}

/* ---------- forms and buttons ---------- */
textarea, input[type="password"], input[type="text"], input[type="number"], input[type="search"] {
  width: 100%;
  font: inherit;
  font-size: 17px; /* >= 16px so iOS does not zoom */
  color: var(--ink);
  background: var(--field);
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 12px;
}
textarea { resize: none; min-height: 120px; line-height: 1.5; }
textarea:focus, input:focus { outline: 2px solid var(--g); outline-offset: 1px; }
.memo-edit { min-height: 110px; }

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: 48px;
  padding: 0 18px;
  font: inherit;
  font-size: 16px;
  font-weight: 600;
  color: var(--ink);
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 12px;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}
.btn.small { min-height: 44px; padding: 0 14px; font-size: 14px; }
.btn.primary { background: var(--g); border-color: var(--g); color: var(--on-g); }
.btn.quiet { background: transparent; color: var(--mut); }
.btn.danger { background: var(--danger); border-color: var(--danger); color: #fff; }
.btn.block { display: flex; width: 100%; }
.btn:disabled { opacity: 0.45; cursor: default; }
.btn:focus-visible, .icon-btn:focus-visible, .sync:focus-visible, .tabbar a:focus-visible, .fab:focus-visible, .pick-row:focus-visible { outline: 2px solid var(--g); outline-offset: 2px; }

/* ---------- login ---------- */
.login { max-width: 360px; margin: 8vh auto 0; text-align: center; }
.login-mark {
  width: 72px; height: 72px; margin: 0 auto 12px;
  display: flex; align-items: center; justify-content: center;
  background: var(--g); color: var(--on-g);
  border-radius: 18px; font-size: 38px; font-weight: 800;
}
.login h2 { margin: 0 0 4px; }
.login-form { margin: 18px 0 8px; text-align: left; display: flex; flex-direction: column; gap: 10px; }
.login-form label { font-size: 14px; font-weight: 600; }
.form-msg { min-height: 1.4em; margin: 0; color: var(--danger); font-size: 14px; }
.login .btn.quiet { margin-top: 4px; font-size: 14px; font-weight: 500; }
body.is-login .tabbar, body.is-login .fab { display: none; }

/* ---------- FAB and tab bar ---------- */
.fab {
  position: absolute;
  right: calc(16px + env(safe-area-inset-right));
  bottom: calc(var(--tabbar-h) + env(safe-area-inset-bottom) + 16px);
  width: 56px;
  height: 56px;
  border: 0;
  border-radius: 50%;
  background: var(--g);
  color: var(--on-g);
  font-size: 28px;
  line-height: 1;
  box-shadow: var(--shadow);
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}
.fab:active { transform: scale(0.95); }

.tabbar {
  display: flex;
  background: var(--card);
  border-top: 1px solid var(--line);
  padding-bottom: env(safe-area-inset-bottom);
  padding-left: env(safe-area-inset-left);
  padding-right: env(safe-area-inset-right);
}
.tabbar a {
  flex: 1;
  min-width: 0;
  min-height: var(--tabbar-h);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0 2px;
  font-size: 12px;
  text-align: center;
  color: var(--mut);
  text-decoration: none;
  white-space: nowrap;
  -webkit-tap-highlight-color: transparent;
}
.tabbar a[aria-current="page"] { color: var(--g); font-weight: 700; }

/* ---------- quick memo sheet ---------- */
.sheet-backdrop {
  position: fixed;
  inset: 0;
  z-index: 20;
  display: flex;
  align-items: flex-end;
  justify-content: center;
  background: rgba(0, 0, 0, 0.4);
  padding-bottom: var(--kb, 0px);
}
.sheet {
  width: 100%;
  max-width: 760px;
  background: var(--card);
  border-radius: 18px 18px 0 0;
  padding: 16px calc(16px + env(safe-area-inset-right)) calc(16px + env(safe-area-inset-bottom)) calc(16px + env(safe-area-inset-left));
  box-shadow: 0 -6px 24px rgba(0, 0, 0, 0.25);
}
.sheet-title { display: block; margin-bottom: 8px; font-weight: 700; }
.sheet textarea { min-height: 140px; }
.sheet .row-actions { justify-content: flex-end; }
.sheet .btn { min-width: 96px; }
body.sheet-open #view { overflow: hidden; }

/* ---------- toast ---------- */
#toast {
  position: fixed;
  left: 50%;
  bottom: calc(var(--tabbar-h) + env(safe-area-inset-bottom) + 84px);
  z-index: 30;
  max-width: calc(100vw - 32px);
  transform: translate(-50%, 8px);
  padding: 10px 16px;
  border-radius: 20px;
  background: #2b2f2a;
  color: #fff;
  font-size: 14px;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.15s, transform 0.15s;
}
#toast.show { opacity: 1; transform: translate(-50%, 0); }

/* ---------- contents list ---------- */
.new-content { display: flex; gap: 8px; margin-bottom: 12px; }
.new-content .new-title { flex: 1; min-width: 0; }
.new-content .btn { flex: none; }
.filter { margin-bottom: 12px; }
.card-link-inner { display: block; color: inherit; text-decoration: none; -webkit-tap-highlight-color: transparent; }
.card-link-inner > b { display: block; font-size: 12px; color: var(--g); margin-bottom: 4px; }
.content-card.is-done .card-title { color: var(--mut); }

/* segmented toggle (filter, 내부/외부, 시작점) */
.seg { display: inline-flex; gap: 0; border: 1px solid var(--line); border-radius: 12px; overflow: hidden; background: var(--field); flex: none; }
.seg.wide { display: flex; }
.seg-btn {
  flex: 1;
  min-height: 44px;
  min-width: 44px;
  padding: 0 12px;
  font: inherit;
  font-size: 14px;
  font-weight: 600;
  color: var(--mut);
  background: transparent;
  border: 0;
  cursor: pointer;
  white-space: nowrap;
  -webkit-tap-highlight-color: transparent;
}
.seg-btn + .seg-btn { border-left: 1px solid var(--line); }
.seg-btn[aria-pressed="true"] { background: var(--g); color: var(--on-g); }
.seg-btn:focus-visible, .chip-btn:focus-visible, .pick-del:focus-visible, .stage-tab:focus-visible, .back-link:focus-visible { outline: 2px solid var(--g); outline-offset: -2px; }

/* ---------- content view ---------- */
/* The header stays visible while a long stage scrolls. From 768px (end of file) .stage-tabs becomes the left list. */
.content-top {
  position: sticky;
  top: 0;
  z-index: 5;
  margin: 0 calc(-16px - env(safe-area-inset-right)) 0 calc(-16px - env(safe-area-inset-left));
  padding: 4px calc(16px + env(safe-area-inset-right)) 8px calc(16px + env(safe-area-inset-left));
  background: var(--bg);
}
.content-head { display: flex; align-items: center; gap: 4px; }
.back-link {
  flex: none;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 44px;
  height: 44px;
  font-size: 28px;
  line-height: 1;
  color: var(--mut);
  text-decoration: none;
}
.content-title {
  flex: 1;
  min-width: 0;
  font: inherit;
  font-size: 19px;
  font-weight: 700;
  color: var(--ink);
  background: transparent;
  border: 1px solid transparent;
  border-radius: 10px;
  min-height: 44px;
  padding: 8px;
}
.content-title:focus { background: var(--field); border-color: var(--line); }
.content-top .prog { padding-top: 6px; }

.stage-tabs {
  display: flex;
  gap: 6px;
  margin-top: 10px;
  padding-bottom: 2px;
  overflow-x: auto;
  overscroll-behavior-x: contain;
  scrollbar-width: none;
  -webkit-overflow-scrolling: touch;
}
.stage-tabs::-webkit-scrollbar { display: none; }
.stage-tab {
  flex: none;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  min-height: 44px;
  padding: 0 14px;
  font-size: 13px;
  color: var(--ink);
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 20px;
  text-decoration: none;
  white-space: nowrap;
  -webkit-tap-highlight-color: transparent;
}
.stage-tab .num { color: var(--mut); font-size: 12px; }
.stage-tab.done::after { content: "✓"; color: var(--g); font-weight: 700; }
.stage-tab[aria-current="step"] { background: var(--g); border-color: var(--g); color: var(--on-g); font-weight: 700; }
.stage-tab[aria-current="step"] .num, .stage-tab[aria-current="step"].done::after { color: var(--on-g); }

.content-body { padding-top: 6px; }
.stage-title { margin: 8px 0 0; font-size: 18px; }
.content-body > .hint { margin: 2px 0 12px; }
.ref-card { background: var(--g-soft); border-color: transparent; }
.ref-text { white-space: pre-wrap; font-size: 15px; }
.stage-nav { display: flex; justify-content: space-between; gap: 10px; margin-top: 16px; }
.stage-nav .btn { flex: 1; text-decoration: none; min-width: 0; }
.stage-nav-gap { flex: 1; }

/* ---------- stage fields ---------- */
.card textarea, .card input[type="text"] { margin-top: 4px; }
.card textarea[data-grow] { min-height: 52px; overflow-y: auto; }
.generic-text { min-height: 220px; }
.spaced-s { margin-top: 12px; }

.pick-head { display: flex; align-items: center; gap: 8px; margin: 4px 0; }
.pick-head > b { font-size: 16px; }
.chip.count.ok { background: var(--g); color: var(--on-g); }
.pick-list { list-style: none; margin: 8px 0 12px; padding: 0; }
.pick-item { display: flex; align-items: flex-start; gap: 8px; margin-bottom: 8px; }
.pick-item .seg { margin-top: 2px; }
.pick-item .seg-btn { flex: none; min-height: 48px; padding: 0 10px; font-size: 13px; }
.pick-text { flex: 1; min-width: 0; min-height: 52px; padding: 12px; }
.pick-del {
  flex: none;
  min-width: 48px;
  height: 52px;
  padding: 0 8px;
  font: inherit;
  font-size: 14px;
  color: var(--mut);
  background: transparent;
  border: 0;
  border-radius: 12px;
  cursor: pointer;
}
.pick-del.armed { color: var(--danger); font-weight: 700; }
.add-btn { margin-top: 4px; }

.chips { display: flex; flex-wrap: wrap; gap: 8px; }
.chip-btn {
  min-height: 44px;
  padding: 0 16px;
  font: inherit;
  font-size: 14px;
  font-weight: 600;
  color: var(--mut);
  background: var(--field);
  border: 1px solid var(--line);
  border-radius: 22px;
  cursor: pointer;
}
.chip-btn[aria-pressed="true"] { background: var(--g-soft); color: var(--g); border-color: var(--g); }

.key-list { display: flex; flex-direction: column; gap: 8px; margin: 8px 0; }
.key-row { display: flex; align-items: center; gap: 4px; min-height: 48px; }
.key-radio-hit { flex: none; display: flex; align-items: center; justify-content: center; width: 44px; height: 48px; cursor: pointer; }
.key-row input[type="text"] { margin-top: 0; flex: 1; min-width: 0; }
.key-radio { width: 24px; height: 24px; margin: 0; accent-color: var(--g); }
.reminder { margin-top: 12px; padding: 10px 12px; border-radius: 10px; background: var(--g-soft); color: var(--g); font-size: 14px; font-weight: 600; }

/* ---------- stage 5: 가지치기 ---------- */
.ref-line { display: flex; flex-direction: column; gap: 2px; margin: 4px 0 8px; padding: 8px 10px; border-radius: 10px; background: var(--g-soft); color: var(--ink); font-size: 14px; }
.ref-line b { display: inline; margin: 0; font-size: 12px; color: var(--g); }
.branch-list { list-style: none; margin: 8px 0; padding: 0; display: flex; flex-direction: column; gap: 10px; }
.branch { padding: 10px; border: 1px solid var(--line); border-radius: 12px; background: var(--card); transition: opacity 0.15s; }
.branch-text { min-height: 52px; margin-top: 0; }
.scores { display: flex; flex-direction: column; margin-top: 6px; }
.score-row { display: flex; align-items: center; gap: 10px; min-height: 44px; }
.score-label { flex: none; width: 36px; font-size: 13px; font-weight: 600; color: var(--mut); }
.score-range { flex: 1; min-width: 0; height: 44px; margin: 0; accent-color: var(--g); }
.score-range.unset { opacity: 0.45; }
.card .score-num { flex: none; width: 72px; min-height: 44px; margin-top: 0; padding: 6px 8px; text-align: center; }
.branch-bar { display: flex; align-items: center; gap: 8px; margin-top: 6px; }
.branch-total { flex: none; }
.branch-del.armed, .line-del.armed { color: var(--danger); font-weight: 700; }
.reason-box { margin-top: 6px; }
.reason-box .hint { margin: 2px 0; }
.branch.dim { opacity: 0.55; border-style: dashed; }
.branch.dim:focus-within { opacity: 1; }
.limit-hint { padding: 8px 10px; border-radius: 10px; background: var(--g-soft); color: var(--warn); font-weight: 600; }

/* ---------- stage 6: 대본·줄콘티 ---------- */
.ver-tabs { align-items: center; }
.ver-tabs .new-ver { margin-left: auto; }
.line-summary { margin: 4px 0 8px; }
.line-list { list-style: none; margin: 0 0 10px; padding: 0; display: flex; flex-direction: column; gap: 10px; }
.line { padding: 10px; border: 1px solid var(--line); border-left-width: 4px; border-radius: 12px; background: var(--card); }
.line.flagged { border-color: var(--warn); background: var(--card); background: color-mix(in srgb, var(--warn) 14%, var(--card)); }
.line-top { display: flex; align-items: center; gap: 4px; }
.line-num { flex: none; min-width: 28px; font-size: 13px; font-weight: 700; color: var(--g); }
.line-top .btn.small { min-width: 44px; padding: 0 10px; }
.line-cols { display: grid; grid-template-columns: minmax(0, 1fr); gap: 8px; margin-top: 4px; }
.line-col { min-width: 0; }
.col-label { display: block; font-size: 12px; font-weight: 600; color: var(--mut); }
.line-col textarea { margin-top: 2px; min-height: 52px; }
.line-flags { display: flex; flex-wrap: wrap; align-items: center; gap: 8px 12px; margin-top: 6px; }
.line-checks { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; margin-top: 6px; }
.line-checks .col-label { margin-right: 2px; }
.flag-btn[aria-pressed="true"] { background: color-mix(in srgb, var(--warn) 20%, var(--card)); border-color: var(--warn); color: var(--warn); }
.check-label { display: flex; align-items: center; gap: 10px; min-height: 44px; cursor: pointer; font-size: 15px; }
.check-box { flex: none; width: 24px; height: 24px; margin: 0; accent-color: var(--g); }
@media (min-width: 900px) {
  /* 900, not 768: on a portrait iPad the stage list takes the left 190px, so the body is narrower than the screen. */
  .line-cols { grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); gap: 12px; }
}

/* ---------- stage 7: 후킹·검수 ---------- */
.first-rows { display: flex; flex-direction: column; gap: 8px; margin: 8px 0; }
.first-row { display: flex; align-items: flex-start; gap: 4px; }
.first-row textarea { flex: 1; min-width: 0; margin-top: 0; min-height: 52px; }
.check-list { list-style: none; margin: 4px 0 0; padding: 0; }
.check-list li + li { border-top: 1px solid var(--line); }
.ref-pick { margin-bottom: 10px; }

/* ---------- 레퍼런스 ---------- */
.view-title .add-ref { margin-left: auto; }
.ref-filters { display: flex; flex-direction: column; gap: 10px; margin: 0 0 12px; }
.ref-search { margin: 0; }
.ref-filters .seg { align-self: stretch; }
.ref-tagchips { max-height: 104px; overflow-y: auto; -webkit-overflow-scrolling: touch; }
.ref-tagchips[hidden], .ref-chips[hidden], .f[hidden], .ref-group[hidden] { display: none; }
.ref-list { display: flex; flex-direction: column; }
.ref-card-item .row-actions { margin-top: 10px; }
.ref-body { display: flex; flex-direction: column; align-items: flex-start; gap: 4px; min-width: 0; max-width: 100%; text-align: left; }
.ref-head { display: flex; flex-wrap: wrap; align-items: center; gap: 6px; }
.badge.ref-at { background: transparent; border: 1px solid var(--line); color: var(--mut); }
.ref-title { font-size: 16px; font-weight: 600; line-height: 1.4; overflow-wrap: anywhere; }
.ref-quote { display: -webkit-box; -webkit-box-orient: vertical; -webkit-line-clamp: 4; line-clamp: 4; overflow: hidden; white-space: pre-wrap; overflow-wrap: anywhere; font-size: 15px; line-height: 1.5; color: var(--ink); }
.ref-quote.full { display: block; -webkit-line-clamp: unset; line-clamp: unset; max-height: 40vh; overflow-y: auto; }
.ref-source, .ref-url { font-size: 13px; color: var(--mut); overflow-wrap: anywhere; }
.ref-url.plain { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 12px; }
.ref-note { font-size: 14px; color: var(--ink); overflow-wrap: anywhere; }
.ref-tags { display: flex; flex-wrap: wrap; gap: 4px 8px; }
.ref-tag { font-size: 13px; color: var(--g); }
.ref-open { text-decoration: none; }
.ref-viewer .ref-open { text-decoration: none; }
.ref-gone { margin-top: 6px; font-size: 13px; overflow-wrap: anywhere; }

/* chips on the hook / script stage */
.ref-chips { display: flex; flex-wrap: wrap; gap: 8px; margin: 0 0 10px; }
.ref-chip { display: inline-flex; align-items: center; max-width: 100%; min-height: 44px; border: 1px solid var(--line); border-radius: 22px; background: var(--g-soft); color: var(--g); overflow: hidden; }
.ref-chip-open { flex: 1 1 auto; min-width: 0; max-width: 240px; min-height: 44px; padding: 0 6px 0 14px; font: inherit; font-size: 13px; font-weight: 600; color: inherit; background: transparent; border: 0; text-align: left; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; cursor: pointer; }
.ref-chip-x { flex: none; min-width: 44px; min-height: 44px; padding: 0 12px 0 6px; font: inherit; font-size: 18px; color: var(--mut); background: transparent; border: 0; cursor: pointer; }
.ref-chip-open:focus-visible, .ref-chip-x:focus-visible, .ref-pick-item:focus-visible { outline: 2px solid var(--g); outline-offset: -2px; }

/* add / edit / pick sheets */
.sheet.tall { max-height: calc(100vh - var(--kb, 0px) - 12px); max-height: calc(100dvh - var(--kb, 0px) - 12px); overflow-y: auto; overscroll-behavior: contain; -webkit-overflow-scrolling: touch; }
.sheet-head { display: flex; align-items: center; justify-content: space-between; gap: 10px; margin-bottom: 10px; }
.sheet-head .sheet-title { margin: 0; font-size: 17px; }
.sheet-actions { position: sticky; bottom: calc(-16px - env(safe-area-inset-bottom)); margin: 12px -16px calc(-16px - env(safe-area-inset-bottom)); padding: 10px 16px calc(12px + env(safe-area-inset-bottom)); background: var(--card); border-top: 1px solid var(--line); }
.sheet .btn.armed { color: var(--danger); font-weight: 700; }
.ref-form .f { display: flex; flex-direction: column; gap: 4px; margin-bottom: 12px; }
.ref-form .f input, .ref-form .f textarea { margin: 0; }
.ref-form .col-label { margin-left: 2px; }
.ref-text-input { min-height: 120px; }
.ref-detect { margin: 0; min-height: 1.3em; }
.ref-clip { display: flex; }
.ref-clip:empty { display: none; }
.paste-chip { max-width: 100%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; color: var(--g); border-color: var(--g); background: var(--g-soft); }
.ref-tagpreview { display: flex; flex-wrap: wrap; gap: 6px; }
.ref-tagpreview:empty { display: none; }
.ref-suggest:empty { display: none; }
.ref-suggest .chip-btn { font-weight: 500; }
.ref-pick-list { display: flex; flex-direction: column; gap: 8px; }
.ref-pick-item { display: block; width: 100%; padding: 12px; text-align: left; font: inherit; color: var(--ink); background: var(--card); border: 1px solid var(--line); border-radius: 12px; cursor: pointer; -webkit-tap-highlight-color: transparent; }
.ref-pick-item:active { background: var(--field); }
.ref-view-card { padding: 12px; border: 1px solid var(--line); border-radius: 12px; background: var(--field); }
.ref-picker .ref-filters { margin-bottom: 10px; }
.ref-picker .card { margin-bottom: 0; }

/* ---------- 강의 노트 ---------- */
.note-search { margin: 0 0 12px; min-height: 48px; }
.note-group { margin-bottom: 10px; background: var(--card); border: 1px solid var(--line); border-radius: 14px; overflow: hidden; }
.note-group > summary {
  display: flex;
  align-items: center;
  gap: 8px;
  min-height: 48px;
  padding: 8px 14px;
  font-weight: 700;
  cursor: pointer;
  list-style: none;
  -webkit-tap-highlight-color: transparent;
}
.note-group > summary::-webkit-details-marker { display: none; }
.note-group > summary::after { content: "›"; margin-left: auto; font-size: 22px; line-height: 1; color: var(--mut); transform: rotate(90deg); transition: transform 0.15s; }
.note-group[open] > summary::after { transform: rotate(-90deg); }
.note-group > summary:focus-visible { outline: 2px solid var(--g); outline-offset: -2px; }
.note-group-title { min-width: 0; overflow-wrap: anywhere; }
.note-items { padding: 0 14px 6px; }
.note-item { padding: 10px 0; border-top: 1px solid var(--line); overflow-wrap: anywhere; }
.note-term { display: block; font-size: 15px; color: var(--g); }
.note-text { margin: 4px 0 0; font-size: 15px; line-height: 1.6; }

/* ---------- 설정 ---------- */
.set-sec { padding: 14px; }
.set-title { margin: 0 0 6px; font-size: 17px; }
.set-note { margin: 0 0 10px; color: var(--mut); font-size: 14px; }
.set-note.strong { color: var(--ink); font-weight: 600; }
.set-buttons { display: flex; flex-direction: column; gap: 10px; }
.card.warn { border-color: var(--warn); background: var(--card); background: color-mix(in srgb, var(--warn) 14%, var(--card)); }
.card.warn > b { font-size: 15px; color: var(--warn); }
.stage-list { list-style: none; margin: 0 0 14px; padding: 0; }
.stage-row { border-top: 1px solid var(--line); }
.stage-row:first-child { border-top: 0; }
.stage-row-head { display: flex; align-items: center; gap: 10px; min-height: 52px; padding: 4px 0; }
.stage-row-head .num { flex: none; width: 22px; color: var(--mut); font-size: 13px; text-align: center; }
.stage-row-main { flex: 1; min-width: 0; overflow-wrap: anywhere; }
.stage-row-main .hint { margin: 0; }
.stage-confirm { margin: 0 0 8px; padding: 10px 12px; border-radius: 12px; background: var(--field); border: 1px solid var(--line); }
.stage-confirm .set-note { margin-bottom: 4px; }
.stage-confirm .row-actions { margin-top: 8px; }
.stage-form { display: flex; flex-direction: column; gap: 6px; padding-top: 12px; border-top: 1px dashed var(--line); }
.stage-form .col-label { margin-top: 4px; }
.stage-form input { margin: 0; }
.stage-form .form-msg { min-height: 1.4em; }
.info-list { display: grid; grid-template-columns: minmax(0, 7.5em) minmax(0, 1fr); gap: 6px 12px; margin: 0 0 14px; font-size: 14px; }
.info-list dt { color: var(--mut); }
.info-list dd { margin: 0; overflow-wrap: anywhere; }
.export-note { margin: 10px 0 0; color: var(--warn); font-weight: 600; }

/* ---------- card grids (1 column on phones; 2 columns from 900px, see the iPad block) ---------- */
.card-grid { display: block; }

/* ---------- iPad and wider (>= 768px) ---------- */
/* Touch first: :hover is only used for decoration, inside @media (hover: hover). */
@media (min-width: 768px) {
  #shell { max-width: none; }

  /* Screens read as a centered column of at most 840px; the top bar and tab bar keep the full width. */
  #view {
    padding-left: max(calc(16px + env(safe-area-inset-left)), calc((100% - 840px) / 2));
    padding-right: max(calc(16px + env(safe-area-inset-right)), calc((100% - 840px) / 2));
    scroll-padding: 24px 0 96px;
  }
  .view-title { margin-top: 12px; }

  /* ----- content screen: stage list on the left, body on the right ----- */
  #view:has(> .content-view) {
    padding-left: calc(16px + env(safe-area-inset-left));
    padding-right: calc(16px + env(safe-area-inset-right));
  }
  .content-view {
    display: grid;
    grid-template-columns: 190px minmax(0, 1fr);
    column-gap: 20px;
    align-items: start;
    max-width: 1240px;
    margin: 0 auto;
  }
  /* The header's children (head, progress bar, stage list) become cells of the grid. */
  .content-top { display: contents; }
  .content-head { grid-column: 1 / -1; grid-row: 1; margin-bottom: 8px; }
  .content-top > .prog { grid-column: 2; grid-row: 2; max-width: 720px; padding-top: 4px; }

  .stage-tabs {
    grid-column: 1;
    grid-row: 2 / 4;
    align-self: start;
    position: sticky;
    top: 8px;
    flex-direction: column;
    gap: 2px;
    margin-top: 0;
    padding: 8px;
    /* The list starts below the title row (about 72px); sized so its bottom is on screen before the page has scrolled. */
    max-height: calc(100vh - var(--chrome-h) - 16px - 72px);
    max-height: calc(100dvh - var(--chrome-h) - 16px - 72px);
    overflow-x: hidden;
    overflow-y: auto; /* 7+ stages scroll inside the column */
    overscroll-behavior: contain;
    background: var(--card);
    border: 1px solid var(--line);
    border-radius: 14px;
  }
  .stage-tab {
    width: 100%;
    min-width: 0;
    overflow-wrap: anywhere;
    padding: 8px 12px;
    font-size: 14px;
    white-space: normal;
    background: transparent;
    border-color: transparent;
    border-radius: 10px;
  }
  .stage-tab .num { flex: none; width: 16px; }
  .stage-tab.done::after { margin-left: auto; }
  .stage-tab[aria-current="step"] .num { color: var(--on-g); }

  .content-body { grid-column: 2; grid-row: 3; width: 100%; max-width: 720px; padding-top: 4px; }
  /* The previous stage's message stays in view while the stage scrolls. */
  .ref-card {
    position: sticky;
    top: 8px;
    z-index: 4;
    max-height: min(24vh, 160px);
    overflow-y: auto; /* a long message scrolls inside the card */
    -webkit-overflow-scrolling: touch;
    box-shadow: 0 2px 10px rgba(0, 0, 0, 0.14);
  }

  /* ----- card lists ----- */
  .note-search { max-width: 480px; }

  /* ----- sheets become centered dialogs ----- */
  .sheet-backdrop {
    align-items: center;
    padding: max(24px, env(safe-area-inset-top)) max(24px, env(safe-area-inset-right)) calc(max(24px, env(safe-area-inset-bottom)) + var(--kb, 0px)) max(24px, env(safe-area-inset-left));
  }
  .sheet {
    max-width: 560px;
    padding: 20px;
    border-radius: 18px;
    box-shadow: 0 12px 40px rgba(0, 0, 0, 0.35);
  }
  .sheet.tall {
    max-height: calc(100vh - var(--kb, 0px) - 48px);
    max-height: calc(100dvh - var(--kb, 0px) - 48px);
  }
  .sheet-actions {
    bottom: -20px;
    margin: 12px -20px -20px;
    padding: 10px 20px 14px;
    border-radius: 0 0 18px 18px;
  }
}

/* A little wider rail on larger iPads. */
@media (min-width: 1024px) {
  .content-view { grid-template-columns: 210px minmax(0, 1fr); }
}

/* While typing, the message card stops sticking: with the on-screen keyboard up it would cover the field.
   Nothing else is pinned over the body, so the default scroll-padding (24px) is enough. */
@media (min-width: 768px) and (max-width: 1099.98px) {
  #view:has(.content-body :focus-within) .ref-card { position: static; }
}

/* Landscape / large: the message card moves out to a right-hand column of its own (only when there is a card). */
@media (min-width: 1100px) {
  .content-body:has(> .ref-card) {
    max-width: none;
    display: grid;
    grid-template-columns: minmax(0, 720px) 260px;
    column-gap: 20px;
    align-items: start;
  }
  .content-body:has(> .ref-card) > * { grid-column: 1; min-width: 0; }
  /* 10 rows: the card spans every row of the body, so it can stick while the left column scrolls. */
  .content-body > .ref-card {
    grid-column: 2;
    grid-row: 1 / span 10;
    margin-bottom: 0;
    /* starts about 75px below the top and stays clear of the floating button (72px) in the bottom right corner */
    max-height: calc(100vh - var(--chrome-h) - 160px);
    max-height: calc(100dvh - var(--chrome-h) - 160px);
  }
}

@media (min-width: 900px) {
  .card-grid, .ref-list { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 0 12px; align-items: stretch; }
  .card-grid > .card.empty, .ref-list > .card.empty { grid-column: 1 / -1; }
}

@media (min-width: 768px) and (hover: hover) {
  .stage-tab:not([aria-current="step"]):hover { background: var(--field); }
  .card-link:hover, .pick-row:hover, .ref-pick-item:hover { background: var(--field); }
}

/* settings: records that could not be uploaded */
.held { margin: 10px 0; padding: 10px 12px; border-radius: 10px; border: 1px solid var(--warn); }
.held-list { margin: 6px 0; padding-left: 18px; }
.held-list li { margin: 2px 0; overflow-wrap: anywhere; }
