:root {
  --ink: #241c16;
  --muted: #74685c;
  --paper: #efe6da;
  --sheet: #fffaf4;
  --line: rgba(36, 28, 22, 0.12);
  --accent: #b84321;
  --accent-dark: #8c3016;
  --header: #fbf6f0;
  --danger: #8d3016;
  --shadow: 0 16px 40px rgba(48, 32, 18, 0.08);
  --header-h: 4.25rem;
  color-scheme: light;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--paper);
  color: var(--ink);
  font-family: "Segoe UI", "Apple SD Gothic Neo", "Noto Sans KR", "Malgun Gothic", sans-serif;
}

body {
  height: 100%;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}
body.is-busy { cursor: progress; }

button, input { font: inherit; color: inherit; }
button { cursor: pointer; }
button:disabled { cursor: not-allowed; opacity: 0.45; }

.topbar {
  position: sticky;
  top: 0;
  z-index: 5;
  display: flex;
  align-items: center;
  gap: 0.85rem;
  min-height: 3.35rem;
  padding: 0.45rem 0.9rem;
  background: var(--header);
  color: var(--ink);
  border-bottom: 1px solid var(--line);
  overflow: hidden;
}
.toolbar {
  display: flex;
  align-items: center;
  gap: 0.55rem;
  flex: 0 1 auto;
  margin-left: auto;
  min-width: 0;
  flex-wrap: nowrap;
  overflow-x: auto;
  scrollbar-width: none;
}
.toolbar::-webkit-scrollbar { display: none; }
.toolbar > * { flex: none; }

.brand { display: flex; align-items: center; gap: 0.55rem; flex: none; }
.brand h1 { margin: 0; font-size: 0.98rem; font-weight: 700; letter-spacing: -0.02em; white-space: nowrap; }
.count { color: var(--muted); font-size: 0.8rem; font-variant-numeric: tabular-nums; white-space: nowrap; }

.mark { position: relative; width: 1.7rem; height: 1.7rem; flex: none; }
.mark span {
  position: absolute;
  inset: 0.28rem 0.15rem 0.05rem 0.28rem;
  border: 1.5px solid currentColor;
  border-radius: 2px;
  background: #fffaf4;
}
.mark span:nth-child(2) { transform: translate(-3px, -3px); }
.mark span:nth-child(3) { transform: translate(-6px, -6px); background: #f0d3c6; }
.mark.lg { width: 3.2rem; height: 3.2rem; color: var(--accent); margin-bottom: 0.4rem; }

.btn {
  border: 1px solid transparent;
  border-radius: 999px;
  padding: 0.45rem 0.9rem;
  background: transparent;
}
.btn.solid { background: var(--accent); color: #fffaf4; }
.btn.solid:hover { background: var(--accent-dark); }
.btn.ghost { border-color: var(--line); color: var(--ink); background: #fff; }
.btn.ghost:hover { border-color: rgba(36, 28, 22, 0.28); }
.btn.tiny {
  border: 1px solid var(--line);
  background: #fff;
  border-radius: 999px;
  padding: 0.28rem 0.6rem;
  font-size: 0.78rem;
}
.btn.tiny:hover { border-color: var(--ink); }
.btn.tiny.danger:hover { color: white; background: var(--danger); border-color: var(--danger); }

#stage {
  flex: 1;
  min-height: 0;
  width: 100%;
  margin: 0;
  display: flex;
  flex-direction: column;
}

.empty {
  flex: 1;
  width: 100%;
  display: grid;
  justify-items: center;
  align-content: center;
  gap: 1rem;
  padding: 1.5rem;
}
.drop {
  width: min(640px, 100%);
  border: 1.5px dashed rgba(36, 28, 22, 0.28);
  background: rgba(255, 250, 244, 0.72);
  border-radius: 18px;
  padding: 2.4rem 1.4rem;
  display: grid;
  justify-items: center;
  text-align: center;
  gap: 0.35rem;
}
.drop strong { font-size: 1.25rem; }
.drop span, .hints { color: var(--muted); }
.hints { display: flex; flex-wrap: wrap; gap: 0.5rem 0.75rem; justify-content: center; margin: 0; padding: 0; list-style: none; }
.hints li { background: rgba(255, 250, 244, 0.8); border-radius: 999px; padding: 0.3rem 0.7rem; font-size: 0.85rem; }

#workspace {
  flex: 1;
  min-height: 0;
  display: grid;
  grid-template-columns: 236px minmax(0, 1fr);
}
.rail {
  min-height: 0;
  overflow: auto;
  border-right: 1px solid var(--line);
  background: rgba(255, 250, 244, 0.55);
  padding: 0.85rem 0.75rem 1rem;
}
.rail-head {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 0.4rem;
  margin-bottom: 0.65rem;
  font-size: 0.78rem;
  font-weight: 700;
  color: var(--muted);
}
.rail-hint { font-weight: 600; color: var(--accent-dark); }
.thumbs { display: grid; gap: 0.45rem; }
.thumb {
  display: grid;
  gap: 0.28rem;
  width: 100%;
  padding: 0.35rem;
  border: 1px solid transparent;
  border-radius: 12px;
  background: transparent;
  text-align: left;
  cursor: grab;
}
.thumb:active { cursor: grabbing; }
.thumb img {
  width: 100%;
  height: auto;
  display: block;
  object-fit: contain;
  background: white;
  border: 1px solid var(--line);
  border-radius: 8px;
  pointer-events: none;
}
.thumb-top {
  display: flex;
  align-items: center;
  gap: 0.35rem;
  padding: 0 0.1rem;
  font-size: 0.72rem;
  color: var(--muted);
  font-variant-numeric: tabular-nums;
}
.thumb-top span:last-child { margin-left: auto; }
.thumb-open {
  display: grid;
  gap: 0.28rem;
  width: 100%;
  padding: 0;
  border: 0;
  background: transparent;
  text-align: left;
  cursor: pointer;
}
.tick { width: 0.95rem; height: 0.95rem; margin: 0; accent-color: var(--accent); }
.thumb-actions { display: flex; gap: 0.28rem; }
.thumb-actions .btn.tiny { flex: 1; padding: 0.22rem 0; text-align: center; }
.thumb-actions .btn.extract { color: var(--accent-dark); border-color: rgba(184, 67, 33, 0.35); }
.thumb.is-active {
  border-color: rgba(184, 67, 33, 0.45);
  background: #fff;
  box-shadow: 0 8px 18px rgba(90, 48, 24, 0.06);
}
.thumb.drop-before { box-shadow: inset 0 3px 0 var(--accent); }
.thumb.drop-after { box-shadow: inset 0 -3px 0 var(--accent); }
body.is-filedrag .rail { background: #fff8f4; }

.viewer { min-height: 0; overflow: auto; padding: 1.1rem 1.4rem 1.6rem; }
.pages { width: min(860px, 100%); margin: 0 auto; display: grid; gap: 1rem; }
.sheet {
  background: transparent;
  border: 0;
  border-radius: 0;
  box-shadow: none;
  overflow: visible;
  scroll-margin-top: 0.6rem;
}
.sheet.replace-target { outline: 2px solid var(--accent); }
.sheet.move-target { outline: 2px solid var(--ink); }

.sheet.is-active .sheet-page img { outline: 2px solid rgba(184, 67, 33, 0.55); outline-offset: 4px; }
.sheet.drop-before { box-shadow: inset 0 3px 0 var(--accent); }
.sheet.drop-after { box-shadow: inset 0 -3px 0 var(--accent); }
.sheet-bar {
  position: sticky;
  top: 0;
  z-index: 3;
  display: flex;
  align-items: center;
  gap: 0.4rem;
  flex-wrap: wrap;
  padding: 0.55rem 0.7rem;
  background: rgba(255, 250, 244, 0.94);
  border-bottom: 1px solid var(--line);
  backdrop-filter: blur(10px);
}
.num {
  font-variant-numeric: tabular-nums;
  font-weight: 700;
  min-width: 1.6rem;
}
.name { font-size: 0.86rem; color: var(--muted); min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.grow { flex: 1; }
.badge {
  font-size: 0.68rem;
  letter-spacing: 0.04em;
  border-radius: 999px;
  padding: 0.12rem 0.4rem;
  background: #f0e4d8;
}
.rot { font-size: 0.75rem; color: var(--accent-dark); font-weight: 700; }

.sheet-page { background: transparent; }
.sheet-page img {
  display: block;
  width: 100%;
  height: auto;
  background: white;
  border-radius: 10px;
  box-shadow: var(--shadow);
}

.dock-group { display: flex; align-items: center; gap: 0.4rem; flex-wrap: nowrap; }
.dock-label { font-size: 0.75rem; color: var(--muted); white-space: nowrap; }
.seg button, .btn { white-space: nowrap; }
.check-all { display: flex; align-items: center; gap: 0.3rem; font-size: 0.78rem; color: var(--muted); }

.seg { display: flex; padding: 0.15rem; background: #e7dccf; border-radius: 999px; }
.seg button {
  border: 0;
  background: transparent;
  border-radius: 999px;
  padding: 0.32rem 0.7rem;
  font-size: 0.82rem;
}
.seg button.is-on { background: white; box-shadow: 0 1px 2px rgba(36, 28, 22, 0.12); font-weight: 700; }
.rotate-seg button:hover:not(:disabled) { background: #fff; color: var(--accent-dark); }

#toast {
  position: fixed;
  left: 50%;
  bottom: 1.2rem;
  transform: translateX(-50%);
  z-index: 8;
  max-width: min(520px, calc(100% - 2rem));
  background: var(--ink);
  color: #fffaf4;
  border-radius: 999px;
  padding: 0.55rem 0.9rem;
  font-size: 0.88rem;
}
#toast.error { background: var(--danger); }

@media (max-width: 860px) {
  .topbar { align-items: center; }
  #workspace { grid-template-columns: 1fr; grid-template-rows: auto minmax(0, 1fr); }
  .rail {
    max-height: 148px;
    border-right: 0;
    border-bottom: 1px solid var(--line);
    padding-bottom: 0.75rem;
  }
  .thumbs { display: flex; gap: 0.45rem; }
  .thumb { width: 156px; flex: none; }
  .thumb.drop-before { box-shadow: inset 3px 0 0 var(--accent); }
  .thumb.drop-after { box-shadow: inset -3px 0 0 var(--accent); }
  .name { flex-basis: 100%; }
}
