:root {
  --font: "Pretendard", "Apple SD Gothic Neo", "Noto Sans KR", "Malgun Gothic", system-ui, sans-serif;
  --mono: ui-monospace, "SFMono-Regular", Consolas, "D2Coding", "Noto Sans KR", monospace;
  --bg: #f4f6f3;
  --paper: #ffffff;
  --ink: #16201b;
  --ink-2: #4a5a52;
  --line: #d5ddd7;
  --line-strong: #9fb0a6;
  --accent: #127a52;       /* 시트 초록 계열 */
  --accent-ink: #ffffff;
  --accent-soft: #dff2e8;
  --fill: #fff1c7;          /* 채운 칸 */
  --fill-line: #b7791f;
  --fill-ink: #8a5a10;      /* 글자용(대비 4.5:1 이상). --fill-line 은 테두리 전용 */
  --merge: #cfe8db;
  --warn-bg: #fff4e5;
  --warn-line: #d9822b;
  --radius: 14px;
  color-scheme: light dark;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1512;
    --paper: #17201b;
    --ink: #e8efe9;
    --ink-2: #a7b8ad;
    --line: #2c3a32;
    --line-strong: #4c6457;
    --accent: #3fbf85;
    --accent-ink: #06140d;
    --accent-soft: #173327;
    --fill: #3d3212;
    --fill-line: #f0b44a;
    --fill-ink: #f0b44a;
    --merge: #1f3d2f;
    --warn-bg: #33240f;
    --warn-line: #f0a24a;
  }
}

*, *::before, *::after { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  font-family: var(--font);
  font-size: 16px;
  line-height: 1.65;
  color: var(--ink);
  background:
    linear-gradient(var(--bg), var(--bg)) padding-box,
    var(--bg);
  word-break: keep-all;
  overflow-wrap: anywhere;
}
h1, h2, h3 { line-height: 1.25; letter-spacing: -0.01em; }
code { font-family: var(--mono); }
.sr { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
.skip { position: absolute; left: -9999px; }
.skip:focus { left: 12px; top: 12px; z-index: 10; background: var(--paper); padding: 10px 14px; border-radius: 8px; }
:focus-visible { outline: 3px solid var(--accent); outline-offset: 2px; }

/* 히어로 */
.hero {
  display: grid;
  grid-template-columns: 1.1fr 1fr;
  gap: 40px;
  align-items: center;
  max-width: 1120px;
  margin: 0 auto;
  padding: 56px 24px 32px;
  position: relative;
}
.hero::before {
  content: "";
  position: absolute;
  inset: 0 0 auto 0;
  height: 100%;
  z-index: -1;
  background:
    repeating-linear-gradient(0deg, transparent 0 39px, var(--line) 39px 40px),
    repeating-linear-gradient(90deg, transparent 0 119px, var(--line) 119px 120px);
  opacity: .45;
  mask-image: linear-gradient(to bottom, #000 30%, transparent);
  -webkit-mask-image: linear-gradient(to bottom, #000 30%, transparent);
}
.eyebrow { margin: 0 0 8px; color: var(--accent); font-weight: 700; font-size: 14px; letter-spacing: .04em; }
.hero h1 { font-size: clamp(32px, 5vw, 52px); margin: 0 0 16px; font-weight: 800; }
.lede { margin: 0; color: var(--ink-2); max-width: 34em; }
.hero-art { margin: 0; }
.hero-art svg { width: 100%; height: auto; display: block; }
.hero-art figcaption { display: flex; justify-content: space-around; color: var(--ink-2); font-size: 14px; font-weight: 600; }

.frame { fill: var(--paper); stroke: var(--line-strong); stroke-width: 2; }
.rule { stroke: var(--line); stroke-width: 1.5; }
.merged { fill: var(--merge); stroke: var(--accent); stroke-width: 2; }
.val { fill: var(--accent); }
.ink { fill: var(--line-strong); opacity: .7; }
.head { fill: var(--ink-2); opacity: .55; }
.fillcell { fill: var(--fill); stroke: var(--fill-line); stroke-width: 2; stroke-dasharray: 5 4; }
.arrow { fill: none; stroke: var(--accent); stroke-width: 3; stroke-dasharray: 8 6; stroke-linecap: round; }
.ah { fill: var(--accent); }
.flow { fill: none; stroke: var(--fill-line); stroke-width: 2.5; stroke-dasharray: 4 4; stroke-linecap: round; }

@media (prefers-reduced-motion: no-preference) {
  .arrow { animation: march 1.2s linear infinite; }
  .flow { animation: march .9s linear infinite; }
  .merged { animation: breathe 3.6s ease-in-out infinite; transform-origin: 50px 130px; }
  .c1, .v1 { animation: pop 3.6s ease-in-out infinite; animation-delay: .3s; }
  .c2, .v2 { animation: pop 3.6s ease-in-out infinite; animation-delay: .8s; }
  .f1 { animation-delay: .2s; }
  .f2 { animation-delay: .6s; }
}
@keyframes march { to { stroke-dashoffset: -28; } }
@keyframes breathe { 0%, 100% { opacity: 1; } 50% { opacity: .55; } }
@keyframes pop { 0%, 15% { opacity: .15; } 35%, 85% { opacity: 1; } 100% { opacity: .15; } }

/* 도구 */
main {
  max-width: 1120px;
  margin: 0 auto;
  padding: 8px 24px 24px;
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
  gap: 20px;
}
.panel {
  background: var(--paper);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 20px;
  min-width: 0;
  box-shadow: 0 1px 0 var(--line), 0 12px 32px -24px rgba(0, 40, 20, .35);
}
.panel.result, .ad-slot { grid-column: 1 / -1; }
.panel h2 { font-size: 19px; margin: 0 0 12px; display: flex; align-items: center; gap: 10px; }
.panel-head { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: 8px 12px; margin-bottom: 12px; }
.panel-head h2 { margin: 0; }
.step {
  display: inline-grid; place-items: center;
  width: 28px; height: 28px; border-radius: 50%;
  background: var(--accent); color: var(--accent-ink); font-size: 15px; font-weight: 800;
}
.btns { display: flex; gap: 8px; flex-wrap: wrap; }
.btn {
  font: inherit; font-weight: 700; font-size: 15px;
  min-height: 44px; min-width: 44px; padding: 0 18px;
  border-radius: 10px; border: 1.5px solid var(--accent); cursor: pointer;
  touch-action: manipulation;
}
.btn.primary { background: var(--accent); color: var(--accent-ink); }
.btn.primary:disabled { opacity: .45; cursor: not-allowed; }
.btn.ghost { background: transparent; color: var(--accent); }
.btn:hover:not(:disabled) { filter: brightness(1.06); }
.btn.small { font-size: 14px; }

textarea {
  width: 100%;
  font: 16px/1.5 var(--mono);
  color: var(--ink);
  background: var(--bg);
  border: 1.5px solid var(--line-strong);
  border-radius: 10px;
  padding: 12px;
  resize: vertical;
  tab-size: 12;
}
#src { min-height: 200px; }
.out { margin-top: 12px; min-height: 120px; }
.hint { color: var(--ink-2); font-size: 14px; margin: 8px 0 0; }

fieldset { border: 0; margin: 0; padding: 0; min-width: 0; }
legend { font-weight: 700; font-size: 15px; padding: 0; margin-bottom: 8px; }
legend small { font-weight: 400; color: var(--ink-2); }
.opts { display: grid; gap: 16px; margin-bottom: 16px; }
.seg { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.seg legend { grid-column: 1 / -1; }
.radio, .check {
  display: flex; align-items: center; gap: 10px;
  min-height: 44px; padding: 8px 12px;
  border: 1.5px solid var(--line); border-radius: 10px; cursor: pointer;
  background: var(--paper);
}
.radio span, .check span { line-height: 1.3; }
.radio small { display: block; color: var(--ink-2); font-size: 13px; }
.radio:has(input:checked), .check:has(input:checked) { border-color: var(--accent); background: var(--accent-soft); }
input[type="radio"], input[type="checkbox"] { width: 22px; height: 22px; accent-color: var(--accent); flex: none; margin: 0; }
.hdr { display: grid; grid-template-columns: auto 88px; gap: 8px 12px; align-items: center; }
.hdr label[for] { font-weight: 700; font-size: 15px; }
.hdr .check { grid-column: 1 / -1; }
input[type="number"] {
  font: inherit; font-size: 16px; min-height: 44px; width: 100%;
  padding: 0 10px; border-radius: 10px; border: 1.5px solid var(--line-strong);
  background: var(--bg); color: var(--ink);
}
.col-list { display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 8px; }
.check.col b { font-family: var(--mono); color: var(--accent); margin-right: 2px; }
.check.col small { margin-left: 6px; font-size: 12px; font-weight: 700; color: var(--fill-ink); border: 1px dashed var(--fill-line); border-radius: 6px; padding: 0 5px; }

/* 결과 */
.count { margin: 0; display: flex; align-items: baseline; gap: 6px; }
.count strong { font-size: 32px; font-weight: 800; color: var(--accent); font-variant-numeric: tabular-nums; }
.warn { background: var(--warn-bg); border-left: 4px solid var(--warn-line); padding: 10px 14px; border-radius: 8px; margin-bottom: 12px; font-weight: 600; }
.legend { font-size: 14px; color: var(--ink-2); display: flex; align-items: center; gap: 8px; margin-bottom: 8px; }
.sw { width: 26px; height: 16px; background: var(--fill); border: 2px dashed var(--fill-line); border-radius: 4px; }
.table-wrap {
  max-width: 100%; overflow: auto; max-height: 60vh;
  border: 1px solid var(--line); border-radius: 10px;
  -webkit-overflow-scrolling: touch; overscroll-behavior-x: contain;
}
table { border-collapse: separate; border-spacing: 0; font-size: 14px; min-width: 100%; }
th, td { padding: 8px 12px; border-right: 1px solid var(--line); border-bottom: 1px solid var(--line); text-align: left; vertical-align: top; white-space: pre-wrap; max-width: 280px; min-width: 5.5em; overflow-wrap: normal; word-break: keep-all; }
thead th { position: sticky; top: 0; background: var(--accent-soft); font-family: var(--mono); font-size: 12px; color: var(--ink-2); z-index: 1; }
th.rn { position: sticky; left: 0; background: var(--bg); color: var(--ink-2); font-family: var(--mono); font-size: 12px; min-width: 40px; text-align: right; z-index: 1; }
thead th.rn { z-index: 2; }
tr.hdr-row td { font-weight: 700; background: color-mix(in srgb, var(--accent-soft) 55%, transparent); }
tr.blank-row td { background: repeating-linear-gradient(45deg, transparent 0 6px, var(--warn-bg) 6px 12px); }
td.filled { background: var(--fill); outline: 2px dashed var(--fill-line); outline-offset: -3px; }
.empty-note { color: var(--ink-2); border: 0; }
.copy-row { display: flex; flex-wrap: wrap; align-items: center; gap: 8px 16px; margin-top: 16px; }
.copy-row .hint { margin: 0; }

.ad-slot {
  height: 90px; display: grid; place-items: center;
  border: 1px dashed var(--line-strong); border-radius: var(--radius);
  color: var(--ink-2); font-size: 13px; background: var(--paper);
}

/* 설명 */
.guide { max-width: 760px; margin: 0 auto; padding: 24px 24px 8px; }
.guide h2 { font-size: 24px; margin: 40px 0 12px; padding-left: 12px; border-left: 5px solid var(--accent); }
.guide h3 { font-size: 18px; margin: 0 0 8px; }
.ways { list-style: none; padding: 0; margin: 0; display: grid; gap: 16px; }
.ways > li { background: var(--paper); border: 1px solid var(--line); border-radius: var(--radius); padding: 18px; min-width: 0; }
.limit { background: var(--bg); border-radius: 8px; padding: 10px 12px; font-size: 15px; }
.limit strong { color: var(--fill-ink); margin-right: 6px; }
.formula code { font-size: 18px; background: var(--accent-soft); padding: 6px 12px; border-radius: 8px; display: inline-block; }
.code { position: relative; }
.code .btn { position: absolute; top: 8px; right: 8px; background: var(--paper); }
.code pre { margin: 0; overflow: auto; max-width: 100%; background: var(--bg); border: 1px solid var(--line); border-radius: 10px; padding: 60px 14px 14px; font-size: 13px; line-height: 1.55; }
.faq details { border-bottom: 1px solid var(--line); }
.faq summary { cursor: pointer; font-weight: 700; min-height: 44px; display: flex; align-items: center; padding: 8px 0; }
.faq details p { margin: 0 0 14px; color: var(--ink-2); }
.foot { text-align: center; color: var(--ink-2); font-size: 14px; padding: 32px 24px 48px; }

/* 좁은 화면: 오너 폰(갤럭시 Z 폴드5 펼침 약 1,100px)까지 포함 */
@media (max-width: 1279px) {
  .hero { gap: 24px; padding: 40px 20px 24px; }
  main { padding: 8px 20px 20px; }
}
@media (max-width: 860px) {
  .hero { grid-template-columns: 1fr; }
  .hero-art { max-width: 460px; }
  main { grid-template-columns: minmax(0, 1fr); }
}
@media (max-width: 480px) {
  .hero { padding: 28px 16px 16px; }
  main { padding: 4px 12px 16px; gap: 14px; }
  .panel { padding: 16px 14px; }
  .seg { grid-template-columns: 1fr; }
  .guide { padding: 16px 16px 8px; }
  .col-list { grid-template-columns: 1fr 1fr; }
  th, td { max-width: 200px; }
}
