/* --bg values are mirrored by <meta name="theme-color"> in every generated HTML page — change them together */
:root {
  color-scheme: light dark;
  --bg: #ffffff;
  --fg: #211e1b;
  --muted: #6f6a63;
  --border: #e8e4dd;
  --border-strong: #d8d2c9;
  --border-stronger: #a9a299;
  --hover: #f7f5f0;
  --err: #c93f2e;
  --accent: #3f3dbb;
  --accent-contrast: #fff;
  --sheet-shadow: 0 1px 2px rgba(46, 38, 28, 0.07), 0 0 0 0.5px rgba(46, 38, 28, 0.02);
  --sheet-shadow-hover: 0 3px 10px rgba(46, 38, 28, 0.10), 0 0 0 0.5px rgba(46, 38, 28, 0.04);
  --mono: ui-monospace, "SF Mono", "Cascadia Mono", Menlo, Consolas, "Apple SD Gothic Neo", "Noto Sans KR", monospace;
  --radius: 3px;
}

/* Dark palette: same monochrome scale, inverted. An explicit choice
   (data-theme) always wins over the OS-level media query below — see the
   :root[data-theme="dark"] block, which applies regardless of OS setting. */
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --bg: #09090b;
    --fg: #ece9e3;
    --muted: #a49e94;
    --border: #3d3a35;
    --border-strong: #524e47;
    --border-stronger: #736d63;
    --hover: #211f1c;
    --err: #f08575;
    --accent: #a3a0f0;
    --accent-contrast: #131217;
    --sheet-shadow: none;
    --sheet-shadow-hover: none;
  }
}

:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #09090b;
  --fg: #ece9e3;
  --muted: #a49e94;
  --border: #3d3a35;
  --border-strong: #524e47;
  --border-stronger: #736d63;
  --hover: #211f1c;
  --err: #f08575;
  --accent: #a3a0f0;
  --accent-contrast: #131217;
  --sheet-shadow: none;
  --sheet-shadow-hover: none;
}

:root[data-theme="light"] {
  color-scheme: light;
}

*, *::before, *::after { box-sizing: border-box; }

html, body { margin: 0; }

body {
  color: var(--fg);
  background: var(--bg);
  line-height: 1.6;
  font-size: 16px;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, "Apple SD Gothic Neo", "Noto Sans KR", sans-serif;
  -webkit-font-smoothing: antialiased;
  word-break: keep-all;
  overflow-wrap: break-word;
}

/* typewriter layer */
.wordmark, .drop .filelist, .hint, #status, .back, .result, dl.info dt, .pagesizes, footer, .thumbcell .thumbnum { font-family: var(--mono); letter-spacing: 0; }

a { color: inherit; }
a:hover { text-decoration-color: var(--accent); }

/* header */
header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  max-width: 960px;
  margin: 0 auto;
  padding: 1rem 1.25rem;
  border-bottom: 1px solid var(--border);
}

.wordmark {
  font-weight: 600;
  color: var(--fg);
  text-decoration: none;
}

.langtoggle {
  display: flex;
  align-items: center;
  gap: .3rem;
}
.langsel {
  font: inherit;
  font-size: .85rem;
  color: var(--muted);
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: .15rem .4rem;
  cursor: pointer;
}

.themetoggle {
  font: inherit;
  font-size: .85rem;
  line-height: 1;
  color: var(--muted);
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: .2rem .45rem;
  cursor: pointer;
}
.themetoggle:hover {
  background: var(--hover);
  border-color: var(--border-strong);
}

/* main */
main {
  max-width: 720px;
  margin: 0 auto;
  padding: 2.5rem 1.25rem 4rem;
}
main.wide { max-width: 960px; }

/* hero (landing) */
.hero h1 {
  font-size: clamp(1.9rem, 4.5vw, 2.6rem);
  font-weight: 700;
  letter-spacing: -0.025em;
  line-height: 1.15;
  margin: 0 0 .5rem;
}
.hero p {
  color: var(--muted);
  font-size: 1.05rem;
  margin: 0;
}

/* landing groups */
.groups { max-width: 960px; margin: 0 auto; }
.group { margin-top: 2.5rem; }
.group h2 {
  display: flex;
  align-items: center;
  gap: .75rem;
  font-family: var(--mono);
  font-size: .72rem;
  text-transform: uppercase;
  letter-spacing: .14em;
  color: var(--muted);
  font-weight: 600;
  margin: 0 0 .75rem;
}
.group h2::after {
  content: "";
  flex: 1;
  height: 1px;
  background: var(--border);
}

.grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(210px, 1fr));
  gap: .75rem;
}

.card-link {
  position: relative;
  display: flex;
  flex-direction: column;
  border: 1px solid var(--border-strong);
  border-radius: var(--radius);
  padding: .9rem 1rem;
  text-decoration: none;
  color: var(--fg);
  box-shadow: var(--sheet-shadow);
  transition: background .12s, border-color .12s, box-shadow .12s;
}
.card-link:hover {
  background: var(--hover);
  border-color: var(--border-stronger);
  box-shadow: var(--sheet-shadow-hover);
}
.card-link .name {
  font-weight: 550;
  display: block;
}
.card-link .desc {
  color: var(--muted);
  font-size: .875rem;
  display: block;
  margin-top: .15rem;
}

/* dog-ear: ::before masks the corner with the PAGE background (relies on cards
   keeping the same background as the page — a tinted card would break the
   illusion), ::after is the fold flap, kept exactly 1px smaller so the
   diagonal reads as a crease. Sizes only; colors/geometry are shared here. */
.card-link::before,
.card-link::after,
.drop::before,
.drop::after {
  content: "";
  position: absolute;
  top: -1px;
  right: -1px;
  width: 0;
  height: 0;
  border-style: solid;
  transition: border-width .15s ease;
  pointer-events: none;
}
.card-link::before {
  border-width: 0 16px 16px 0;
  border-color: transparent var(--bg) transparent transparent;
}
.card-link::after {
  border-width: 0 15px 15px 0;
  border-color: transparent transparent var(--border-strong) transparent;
}
.card-link:hover::before { border-width: 0 21px 21px 0; }
.card-link:hover::after { border-width: 0 20px 20px 0; }

/* tool page */
main h1 {
  font-size: 1.5rem;
  font-weight: 600;
  letter-spacing: -0.01em;
  margin: 0 0 .35rem;
}
main .desc {
  color: var(--muted);
  margin: 0 0 1.5rem;
}

/* dropzone */
.drop {
  position: relative;
  border: 1.5px dashed var(--border-strong);
  border-radius: var(--radius);
  padding: 2.25rem 1.25rem;
  text-align: center;
  color: var(--muted);
  cursor: pointer;
  transition: .12s;
}
.drop:hover,
.drop.over {
  border-color: var(--accent);
  background: var(--hover);
}
.drop:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}
.drop .filelist {
  list-style: none;
  padding: 0;
  margin: .75rem 0 0;
  font-size: .8rem;
  color: var(--fg);
}
.drop .filelist li { margin-top: .15rem; }
.drop input[type="file"] {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip: rect(0 0 0 0);
  white-space: nowrap;
}

/* same dog-ear treatment as .card-link, sized down slightly since the
   dropzone border is dashed rather than solid */
.drop::before {
  border-width: 0 14px 14px 0;
  border-color: transparent var(--bg) transparent transparent;
}
.drop::after {
  border-width: 0 13px 13px 0;
  border-color: transparent transparent var(--border-strong) transparent;
}
.drop:hover::before,
.drop.over::before { border-width: 0 18px 18px 0; }
.drop:hover::after,
.drop.over::after { border-width: 0 17px 17px 0; }

/* signature pad */
.signpad { display: inline-block; max-width: 100%; }
.signcanvas {
  display: block;
  width: 400px;
  max-width: 100%;
  height: 160px;
  background: #ffffff;
  border: 1.5px dashed var(--border-strong);
  border-radius: var(--radius);
  touch-action: none;
  cursor: crosshair;
}
.signcanvas:hover { border-color: var(--border-stronger); }
#drawFields button.secondary { margin-top: .75rem; }

/* controls */
label {
  display: block;
  font-weight: 600;
  font-size: .88rem;
  margin: 1rem 0 .35rem;
}
label:first-of-type { margin-top: 0; }

input[type="text"],
input[type="number"],
select,
textarea {
  font: inherit;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: .5rem .6rem;
  background: var(--bg);
  color: var(--fg);
  width: auto;
}
textarea { width: 100%; resize: vertical; }

input[type="text"]:focus-visible,
input[type="number"]:focus-visible,
select:focus-visible,
textarea:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
}

input[type="checkbox"],
input[type="radio"],
input[type="range"] {
  accent-color: var(--accent);
}

.checkline {
  display: flex;
  gap: .5rem;
  align-items: center;
  margin: 1rem 0;
}
.checkline label { margin: 0; font-weight: 400; }

.hint {
  color: var(--muted);
  font-size: .78rem;
  margin: .3rem 0 0;
}

/* buttons */
button.primary {
  margin-top: 1.5rem;
  background: var(--fg);
  color: var(--bg);
  border: none;
  border-radius: var(--radius);
  padding: .6rem 1.25rem;
  font: inherit;
  font-weight: 600;
  cursor: pointer;
  transition: background .12s, color .12s;
}
button.primary:hover:not(:disabled) { background: var(--accent); color: var(--accent-contrast); }
button.primary:disabled { opacity: .45; cursor: default; }

button.secondary {
  background: none;
  border: 1px solid var(--border);
  color: var(--fg);
  border-radius: var(--radius);
  padding: .4rem .8rem;
  font: inherit;
  font-size: .85rem;
  cursor: pointer;
}
button.secondary:hover { background: var(--hover); }
button.secondary:disabled { opacity: .45; cursor: default; }

:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

/* status / errors / links */
#status {
  color: var(--muted);
  font-size: .82rem;
}
.err { color: var(--err); margin-top: 1rem; min-height: 1.2em; }
.back {
  color: var(--muted);
  text-decoration: none;
  font-size: .8rem;
}
.back:hover { color: var(--fg); }

.result {
  font-size: .8rem;
  color: var(--muted);
  margin-top: 1rem;
}

dl.info { margin: 0; }
dl.info dt {
  font-size: .74rem;
  color: var(--muted);
  margin-top: .75rem;
}
dl.info dd { margin: .1rem 0 0; font-size: .98rem; }
.pagesizes {
  font-size: .76rem;
  color: var(--muted);
  white-space: pre-wrap;
  margin: .75rem 0 0;
}

/* footer */
footer {
  max-width: 720px;
  margin: 0 auto;
  padding: 2rem 1.25rem;
  border-top: 1px solid var(--border);
  color: var(--muted);
  font-size: .78rem;
  text-align: center;
}

@media (max-width: 480px) {
  main { padding: 1.75rem 1rem 3rem; }
}

@media (prefers-reduced-motion: reduce) {
  * { transition: none !important; }
}

.page-info { margin-top: 3rem; border-top: 1px solid var(--border); padding-top: 1.5rem; }
.page-info h2 { font-size: 1.05rem; font-weight: 600; margin: 1.5rem 0 .5rem; }
.page-info ol { padding-left: 1.25rem; margin: 0; } .page-info li { margin: .35rem 0; }
.page-info h3 { font-size: .95rem; font-weight: 600; margin: 1rem 0 .25rem; }
.page-info p { margin: 0; color: var(--muted); }

/* page-thumbnail grid (progressive enhancement — Reorder/Remove/Split) */
.thumbgrid { margin: 1.25rem 0; }
.thumbgrid .thumbhint,
.thumbgrid .thumbnotice { margin: 0 0 .5rem; }
.thumbcells {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(140px, 1fr));
  gap: .6rem;
}
.thumbcell {
  position: relative;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: .35rem;
  background: var(--bg);
  cursor: pointer;
  user-select: none;
  transition: border-color .12s, opacity .12s, box-shadow .12s;
}
.thumbcell:hover { border-color: var(--border-stronger); }
.thumbcell canvas { display: block; width: 100%; height: auto; border-radius: 2px; }
.thumbcell .thumbnum {
  display: block;
  text-align: center;
  font-size: .72rem;
  color: var(--muted);
  margin-top: .3rem;
}
.thumbcell[draggable="true"] { cursor: grab; }
.thumbcell.dragging { opacity: .4; }
.thumbcell.dragover-before { box-shadow: -3px 0 0 var(--accent); }
.thumbcell.dragover-after { box-shadow: 3px 0 0 var(--accent); }
.thumbcell.removed { opacity: .35; }
.thumbcell.removed::after {
  content: "\2715";
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1.75rem;
  color: var(--err);
  pointer-events: none;
}
.thumbcell.selected { border-color: var(--accent); box-shadow: 0 0 0 2px var(--accent) inset; }

/* mdedit: split editor/preview layout */
.mdsplit {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 14px;
  margin: 1.25rem 0;
}
@media (max-width: 900px) {
  .mdsplit { grid-template-columns: 1fr; }
}
.mdpane label,
.mdpane .hint {
  display: block;
  margin: 0 0 .35rem;
  font-family: var(--mono);
  font-size: .78rem;
  font-weight: 400;
  color: var(--muted);
}

#mdsrc {
  font-family: var(--mono);
  min-height: 380px;
}

.mdpreview {
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--sheet-shadow);
  padding: 20px 22px;
  min-height: 380px;
  max-height: 380px;
  overflow-y: auto;
  overflow-wrap: anywhere;
}

.mdpreview h1, .mdpreview h2, .mdpreview h3, .mdpreview h4 {
  line-height: 1.25;
  margin: .9em 0 .35em;
}
.mdpreview h1:first-child, .mdpreview h2:first-child,
.mdpreview h3:first-child, .mdpreview h4:first-child { margin-top: 0; }
.mdpreview h1, .mdpreview h2 {
  border-bottom: 1px solid var(--border);
  padding-bottom: .3em;
}
.mdpreview p, .mdpreview ul, .mdpreview ol { margin: .6em 0; }
.mdpreview li { margin: .2em 0; }
.mdpreview blockquote {
  border-left: 3px solid var(--border-strong);
  margin: .6em 0;
  padding: .1em 1em;
  color: var(--muted);
}
.mdpreview code {
  font-family: var(--mono);
  background: var(--hover);
  padding: 2px 5px;
  border-radius: var(--radius);
}
.mdpreview pre {
  font-family: var(--mono);
  background: var(--hover);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: .75em 1em;
  overflow-x: auto;
}
.mdpreview pre code { background: none; padding: 0; border-radius: 0; }
.mdpreview a { color: var(--accent); text-decoration: underline; }
.mdpreview hr { border: none; border-top: 1px solid var(--border); margin: 1em 0; }
.mdpreview em { font-style: italic; }
.mdpreview strong { font-weight: 700; }

.mdbuttons { display: flex; flex-wrap: wrap; align-items: center; gap: .75rem; margin-top: 1.5rem; }
.mdbuttons button.primary,
.mdbuttons button.secondary { margin-top: 0; }

/* PWA "Open with which tool?" chooser (file-handler launch on the index page) */
.launch-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, .4);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 1.25rem;
  z-index: 100;
}
.launch-box {
  background: var(--bg);
  color: var(--fg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--sheet-shadow-hover);
  padding: 1.5rem;
  max-width: 420px;
  width: 100%;
}
.launch-box h2 {
  font-size: 1.1rem;
  font-weight: 600;
  margin: 0 0 1rem;
}
.launch-list {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
  gap: .6rem;
}
.launch-cancel { margin-top: 1.25rem; }
