:root {
  color-scheme: light;
  --ink: #182026;
  --muted: #637078;
  --paper: #f4f1ea;
  --surface: #fffdf8;
  --surface-strong: #ffffff;
  --line: #d8d2c8;
  --line-strong: #aaa399;
  --focus: #176b65;
  --focus-soft: #d9ebe8;
  --danger: #a43a32;
  --shadow: 0 18px 48px rgb(24 32 38 / 10%);
  --header-height: 68px;
  --mobile-toolbar-height: 0px;
  font-family: Pretendard, "Apple SD Gothic Neo", "Noto Sans KR", system-ui, sans-serif;
}

* { box-sizing: border-box; }

html, body { min-width: 320px; min-height: 100%; }

body {
  margin: 0;
  color: var(--ink);
  background: var(--paper);
  font-size: 15px;
  line-height: 1.5;
}

button, input, select, textarea { font: inherit; color: inherit; }

button, .inline-button {
  min-height: 44px;
  padding: 9px 14px;
  border: 1px solid var(--line-strong);
  border-radius: 9px;
  background: var(--surface);
  cursor: pointer;
  font-weight: 650;
}

.icon-button { min-width: 44px; padding-inline: 8px; }

button:hover { border-color: var(--ink); }
button:disabled { cursor: not-allowed; opacity: .48; }

a { color: var(--focus); text-underline-offset: 3px; }

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

.skip-link {
  position: fixed;
  top: 8px;
  left: 8px;
  z-index: 100;
  min-height: 44px;
  transform: translateY(-150%);
  padding: 10px 14px;
  color: white;
  background: var(--focus);
  border-radius: 8px;
}

.skip-link:focus { transform: translateY(0); }

.site-header {
  min-height: var(--header-height);
  display: grid;
  grid-template-columns: max-content 1fr max-content;
  align-items: center;
  gap: 24px;
  padding: 10px 20px;
  border-bottom: 1px solid var(--line);
  background: rgb(255 253 248 / 88%);
  backdrop-filter: blur(18px) saturate(130%);
}

.site-header > p { margin: 0; color: var(--muted); }
.site-header nav { display: flex; align-items: center; gap: 14px; }

.brand {
  min-height: 44px;
  display: inline-flex;
  align-items: center;
  gap: 9px;
  color: var(--ink);
  text-decoration: none;
  font-size: 17px;
  font-weight: 760;
  letter-spacing: -.02em;
}

.brand svg { width: 30px; height: 30px; fill: none; stroke: var(--focus); stroke-width: 2.2; stroke-linecap: round; stroke-linejoin: round; }
.privacy-boundary { color: var(--muted); }
.network-status { min-width: 58px; color: var(--focus); font-weight: 700; }
.network-status[data-offline="true"] { color: var(--danger); }

.workspace {
  height: calc(100dvh - var(--header-height) - var(--mobile-toolbar-height));
  min-height: 620px;
  display: grid;
  grid-template-columns: 248px minmax(360px, 1fr) 320px;
}

.side-panel {
  min-width: 0;
  display: flex;
  flex-direction: column;
  padding: 20px 16px;
  background: var(--surface);
  overflow-y: auto;
}

.projects-panel { border-right: 1px solid var(--line); }
.rules-panel { border-left: 1px solid var(--line); }

.panel-heading { display: flex; align-items: flex-start; justify-content: space-between; gap: 12px; margin-bottom: 16px; }
.panel-heading h2 { margin: 0; font-size: 20px; line-height: 1.2; letter-spacing: -.025em; }
.utility-label { margin: 0 0 3px; color: var(--muted); font-size: 11px; font-weight: 760; letter-spacing: .08em; text-transform: uppercase; }
.panel-close { display: none; }
.panel-intro, .panel-footer p, .empty-copy { color: var(--muted); font-size: 13px; }
.panel-intro { margin: -8px 0 14px; }
.full-width { width: 100%; }
.primary-button { color: white; border-color: var(--focus); background: var(--focus); }
.primary-button:hover { border-color: #0f4e49; background: #0f5c57; }
.danger-button { color: var(--danger); }

.field { display: grid; gap: 5px; }
.field > span, .project-name-field > span { color: var(--muted); font-size: 12px; font-weight: 680; }
.compact-field { margin: 16px 0 10px; }
input, select {
  min-height: 44px;
  width: 100%;
  border: 1px solid var(--line-strong);
  border-radius: 8px;
  background: var(--surface-strong);
  padding: 9px 11px;
}

.project-list { display: grid; gap: 4px; }
.project-item { display: grid; grid-template-columns: 1fr 44px; align-items: stretch; }
.project-open {
  min-width: 0;
  display: grid;
  justify-items: start;
  text-align: left;
  border-color: transparent;
  background: transparent;
}
.project-open strong, .project-open small { max-width: 100%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.project-open small { color: var(--muted); font-weight: 500; }
.project-item[data-current="true"] .project-open { border-color: var(--focus); background: var(--focus-soft); }
.project-item[data-current="true"] .project-open small { color: #4f5d64; }
.project-duplicate { padding: 4px; border-color: transparent; background: transparent; font-size: 12px; }
.panel-footer { margin-top: auto; padding-top: 18px; border-top: 1px solid var(--line); }
.panel-footer button { width: 100%; }

.editor-column { min-width: 0; display: flex; flex-direction: column; padding: 16px 18px; overflow: hidden; }
.project-bar { display: flex; align-items: end; justify-content: space-between; gap: 16px; }
.project-name-field { min-width: 0; flex: 1; display: grid; gap: 2px; }
.project-name-field input { min-height: 44px; padding: 3px 0; border: 0; border-bottom: 1px solid transparent; border-radius: 0; background: transparent; font-size: clamp(20px, 2vw, 29px); font-weight: 720; letter-spacing: -.035em; }
.project-name-field input:hover, .project-name-field input:focus { border-bottom-color: var(--line-strong); }
.save-state { display: flex; align-items: center; gap: 7px; min-width: max-content; color: var(--muted); font-size: 13px; }
.save-indicator { width: 8px; height: 8px; border-radius: 50%; background: #8a958e; }
.save-indicator[data-state="saving"] { background: #d59b2b; }
.save-indicator[data-state="saved"] { background: var(--focus); }
.save-indicator[data-state="error"] { background: var(--danger); }

.action-bar { display: flex; flex-wrap: wrap; gap: 8px; margin: 14px 0 10px; }
.export-actions { display: flex; }
.export-actions button { border-radius: 0; border-right-width: 0; }
.export-actions button:first-child { border-radius: 9px 0 0 9px; }
.export-actions button:last-child { border-radius: 0 9px 9px 0; border-right-width: 1px; }

.editor-meta { min-height: 44px; display: flex; align-items: center; gap: 9px; color: var(--muted); font-size: 13px; }
.editor-meta select { width: auto; min-height: 44px; padding-block: 6px; }
.editor-meta span { margin-left: auto; }

.editor-stack {
  position: relative;
  min-height: 280px;
  flex: 1;
  border: 1px solid var(--line-strong);
  border-radius: 12px;
  background: var(--surface-strong);
  overflow: hidden;
  box-shadow: inset 0 1px 0 rgb(255 255 255 / 80%);
}

.highlight-mirror, #sourceText {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  margin: 0;
  padding: 18px 20px 72px;
  border: 0;
  overflow: auto;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
  word-break: break-word;
  tab-size: 2;
  font: 15px/1.72 ui-monospace, SFMono-Regular, Menlo, Consolas, "Apple SD Gothic Neo", monospace;
  letter-spacing: -.01em;
}

.highlight-mirror { color: var(--ink); pointer-events: none; scrollbar-width: none; }
.highlight-mirror::-webkit-scrollbar { display: none; }
#sourceText { resize: none; color: transparent; caret-color: var(--ink); background: transparent; -webkit-text-fill-color: transparent; }
#sourceText::selection { background: rgb(23 107 101 / 28%); -webkit-text-fill-color: transparent; }
#sourceText::placeholder { color: transparent; }
.highlight-span { border-radius: 3px; box-decoration-break: clone; -webkit-box-decoration-break: clone; }
.highlight-span[data-overlap="true"] { box-shadow: inset 0 -3px 0 rgb(24 32 38 / 38%); }
.highlight-span[data-current="true"] { outline: 2px solid var(--focus); outline-offset: 1px; }
.zero-marker { display: inline-block; width: 2px; height: 1.35em; margin: 0 -1px; vertical-align: -.25em; background: var(--danger); }
.zero-marker[data-current="true"] { width: 4px; }

.match-progress {
  position: absolute;
  right: 14px;
  bottom: 14px;
  z-index: 3;
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 9px 8px 13px;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: rgb(255 253 248 / 94%);
  box-shadow: var(--shadow);
}
.match-progress[hidden] { display: none; }
.match-progress button { min-height: 36px; }
.editor-help { margin: 7px 2px 0; color: var(--muted); font-size: 12px; }

.match-navigator { display: grid; grid-template-columns: max-content 1fr max-content max-content; align-items: center; gap: 14px; min-height: 68px; padding-top: 10px; }
.match-navigator > div:first-child { display: grid; }
.match-navigator strong { font-size: 16px; }
.match-navigator span, .match-navigator p { color: var(--muted); font-size: 13px; }
.match-navigator p { margin: 0; overflow-wrap: anywhere; }
.match-navigator > div:last-child { display: flex; gap: 6px; }

.rules-list { display: grid; gap: 12px; margin-bottom: 12px; }
.rule-card { padding: 13px 0 14px; border-bottom: 1px solid var(--line); }
.rule-card:first-child { border-top: 1px solid var(--line); }
.rule-head { display: grid; grid-template-columns: 44px minmax(0, 1fr) 44px; align-items: center; gap: 7px; }
.rule-toggle { min-width: 44px; min-height: 44px; display: grid; place-items: center; }
.rule-toggle input { width: 20px; min-height: 20px; accent-color: var(--focus); }
.rule-name { min-width: 0; font-weight: 700; }
.rule-color { width: 44px; height: 44px; padding: 5px; }
.rule-grid { display: grid; grid-template-columns: 1fr 112px; gap: 7px; margin-top: 8px; }
.rule-options { display: flex; flex-wrap: wrap; gap: 4px 10px; margin-top: 7px; }
.rule-options label { min-height: 44px; display: inline-flex; align-items: center; gap: 5px; color: var(--muted); font-size: 12px; }
.rule-options input { width: 18px; min-height: 18px; }
.rule-actions { display: flex; gap: 4px; margin-top: 7px; }
.rule-actions button { min-width: 44px; min-height: 44px; padding: 5px; }
.rule-error { margin: 7px 0 0; color: var(--danger); font-size: 12px; }
.rule-count { margin-left: auto; color: var(--muted); font-size: 12px; align-self: center; }

.mobile-toolbar { display: none; }
.panel-backdrop { display: none; }

dialog { max-width: min(480px, calc(100vw - 32px)); border: 1px solid var(--line); border-radius: 16px; padding: 0; color: var(--ink); background: var(--surface); box-shadow: var(--shadow); }
dialog::backdrop { background: rgb(24 32 38 / 46%); backdrop-filter: blur(3px); }
dialog form { padding: 24px; }
dialog h2 { margin: 0 0 8px; font-size: 23px; }
dialog p { color: var(--muted); }
.dialog-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 22px; }

.toast { position: fixed; left: 50%; bottom: 22px; z-index: 120; transform: translateX(-50%); max-width: min(560px, calc(100vw - 32px)); padding: 12px 16px; border-radius: 10px; color: white; background: var(--ink); box-shadow: var(--shadow); text-align: center; }

.sr-only { position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px; overflow: hidden; clip: rect(0, 0, 0, 0); white-space: nowrap; border: 0; }

.document-page { min-height: 100dvh; }
.document-header { display: flex; justify-content: space-between; align-items: center; padding: 20px clamp(20px, 5vw, 72px); border-bottom: 1px solid var(--line); background: var(--surface); }
.document-page main { width: min(760px, calc(100% - 40px)); margin: clamp(48px, 8vw, 104px) auto; }
.document-page h1 { max-width: 16ch; margin: 8px 0 20px; font-size: clamp(38px, 7vw, 68px); line-height: 1.02; letter-spacing: -.055em; }
.document-page .lead { font-size: clamp(18px, 2.4vw, 23px); line-height: 1.6; color: #39454c; }
.document-page section { margin-top: 48px; padding-top: 24px; border-top: 1px solid var(--line); }
.document-page h2 { font-size: 22px; }
.inline-button { display: inline-flex; align-items: center; text-decoration: none; }

@media (max-width: 1080px) {
  .workspace { grid-template-columns: 220px minmax(360px, 1fr) 288px; }
  .site-header > p, .privacy-boundary { display: none; }
}

@media (max-width: 820px) {
  :root { --mobile-toolbar-height: 56px; }
  .site-header { grid-template-columns: 1fr max-content; padding-inline: 14px; }
  .site-header nav a { display: none; }
  .mobile-toolbar { min-height: var(--mobile-toolbar-height); display: flex; gap: 8px; padding: 6px 12px; border-bottom: 1px solid var(--line); background: var(--surface); }
  .mobile-toolbar button { flex: 1; }
  .workspace { grid-template-columns: minmax(0, 1fr); min-height: 540px; }
  .side-panel { position: fixed; top: 0; bottom: 0; z-index: 90; width: min(340px, calc(100vw - 28px)); box-shadow: var(--shadow); transform: translateX(-110%); transition: transform 180ms ease-out; }
  .projects-panel { left: 0; }
  .rules-panel { right: 0; left: auto; transform: translateX(110%); }
  .side-panel[data-open="true"] { transform: translateX(0); }
  .panel-close { display: block; }
  .panel-backdrop { position: fixed; inset: 0; z-index: 80; background: rgb(24 32 38 / 42%); }
  .panel-backdrop:not([hidden]) { display: block; }
  .editor-column { padding: 13px 12px; }
  .match-navigator { grid-template-columns: 1fr max-content; gap: 7px; }
  .match-navigator > p { grid-column: 1 / -1; grid-row: 2; }
  .match-navigator > button { grid-column: 1 / -1; }
  .match-navigator > div:last-child { grid-column: 2; grid-row: 1; }
}

@media (max-width: 480px) {
  .brand { font-size: 15px; }
  .brand svg { width: 26px; height: 26px; }
  .network-status { min-width: 0; font-size: 12px; }
  .project-bar { align-items: start; }
  .save-state { margin-top: 22px; font-size: 11px; }
  .action-bar { display: grid; grid-template-columns: 1fr 1fr; }
  .action-bar > button, .export-actions { width: 100%; }
  .export-actions { grid-column: 1 / -1; }
  .export-actions button { flex: 1; }
  .editor-meta { flex-wrap: wrap; }
  .editor-meta span { width: 100%; margin-left: 0; }
  .highlight-mirror, #sourceText { padding: 14px 14px 76px; font-size: 14px; }
  .match-navigator { padding-bottom: env(safe-area-inset-bottom); }
  .match-navigator strong { font-size: 14px; }
  .match-navigator > div:last-child button { padding-inline: 10px; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { scroll-behavior: auto !important; transition-duration: .01ms !important; animation-duration: .01ms !important; animation-iteration-count: 1 !important; }
}

@media (prefers-contrast: more) {
  :root { --line: #837c73; --line-strong: #514b45; --muted: #465157; }
  .highlight-span { outline: 1px solid currentColor; }
}
