:root {
  --bg: #f4f6f3;
  --surface: #ffffff;
  --surface-2: #eef2ec;
  --text: #1d2420;
  --muted: #5f6b64;
  --line: #dde3dc;
  --brand: #1f7a4d;
  --brand-ink: #ffffff;
  --brand-soft: #dcefe3;
  --danger: #c62828;
  --danger-soft: #fde4e4;
  --warn: #b26a00;
  --warn-soft: #fff1d6;
  --info: #1d5fa8;
  --info-soft: #e1ecfa;
  --best: #0d7a3a;
  --radius: 12px;
  --shadow: 0 1px 2px rgba(0, 0, 0, .06), 0 2px 8px rgba(0, 0, 0, .04);
  --tabs-h: 62px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #121614; --surface: #1b211e; --surface-2: #232b27; --text: #e7ece8; --muted: #9aa69f; --line: #2f3833;
    --brand: #3fb07a; --brand-ink: #08160f; --brand-soft: #1d3a2b; --danger: #ff6b6b; --danger-soft: #3a1f1f;
    --warn: #f0b24a; --warn-soft: #3a2e17; --info: #7fb2f0; --info-soft: #1b2a3d; --best: #59d08f;
    --shadow: none; color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #121614; --surface: #1b211e; --surface-2: #232b27; --text: #e7ece8; --muted: #9aa69f; --line: #2f3833;
  --brand: #3fb07a; --brand-ink: #08160f; --brand-soft: #1d3a2b; --danger: #ff6b6b; --danger-soft: #3a1f1f;
  --warn: #f0b24a; --warn-soft: #3a2e17; --info: #7fb2f0; --info-soft: #1b2a3d; --best: #59d08f;
  --shadow: none; color-scheme: dark;
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0; background: var(--bg); color: var(--text);
  font: 15px/1.5 -apple-system, BlinkMacSystemFont, "Apple SD Gothic Neo", "Malgun Gothic", "Noto Sans KR", system-ui, sans-serif;
  padding-bottom: calc(var(--tabs-h) + env(safe-area-inset-bottom) + 12px);
}
a { color: var(--info); }
h1, h2, h3 { line-height: 1.3; margin: 0 0 8px; }
h2 { font-size: 18px; }
h3 { font-size: 16px; }
small, .muted { color: var(--muted); }
.num { font-variant-numeric: tabular-nums; }

/* 상단바 */
.topbar {
  position: sticky; top: 0; z-index: 20; display: flex; align-items: center; justify-content: space-between;
  gap: 8px; padding: 10px 16px; padding-top: calc(10px + env(safe-area-inset-top));
  background: var(--brand); color: var(--brand-ink);
}
.brand { color: inherit; text-decoration: none; font-weight: 700; font-size: 17px; }
.top-actions { display: flex; gap: 6px; }
.topbar .chip { background: rgba(255, 255, 255, .18); color: inherit; border: 0; }

/* 탭: 모바일은 하단 고정 */
.tabs {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 20; display: grid; grid-template-columns: repeat(6, 1fr);
  background: var(--surface); border-top: 1px solid var(--line); padding-bottom: env(safe-area-inset-bottom);
}
.tabs a {
  position: relative; display: flex; flex-direction: column; align-items: center; justify-content: center;
  height: var(--tabs-h); font-size: 11px; color: var(--muted); text-decoration: none; gap: 2px;
}
.tabs a span { font-size: 20px; line-height: 1; }
.tabs a.on { color: var(--brand); font-weight: 700; }
.tabs .badge { position: absolute; top: 6px; left: calc(50% + 6px); }

@media (min-width: 900px) {
  body { padding-bottom: 24px; }
  .tabs {
    position: sticky; top: 52px; bottom: auto; grid-template-columns: repeat(6, auto); justify-content: start;
    border-top: 0; border-bottom: 1px solid var(--line); padding: 0 12px;
  }
  .tabs a { flex-direction: row; height: 46px; padding: 0 16px; font-size: 14px; gap: 6px; }
  .tabs a span { font-size: 16px; }
  .tabs a.on { box-shadow: inset 0 -3px 0 var(--brand); }
  .tabs .badge { position: static; }
}

.view { max-width: 1100px; margin: 0 auto; padding: 14px 16px; }

/* 카드·레이아웃 */
.card {
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 14px; margin-bottom: 12px; box-shadow: var(--shadow);
}
.card.tight { padding: 10px 12px; }
.row { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.row.between { justify-content: space-between; }
.grow { flex: 1 1 auto; min-width: 0; }
.stack > * + * { margin-top: 8px; }
.grid { display: grid; gap: 12px; }
@media (min-width: 640px) { .grid.two { grid-template-columns: 1fr 1fr; } .grid.three { grid-template-columns: repeat(3, 1fr); } }
.section-title { display: flex; align-items: center; justify-content: space-between; gap: 8px; margin: 18px 0 8px; flex-wrap: wrap; }
.section-title h2 { margin: 0; }
.hint { font-size: 13px; color: var(--muted); }
.empty { text-align: center; color: var(--muted); padding: 28px 12px; }

/* 버튼 */
button, .btn {
  font: inherit; border-radius: 10px; border: 1px solid var(--line); background: var(--surface); color: var(--text);
  padding: 9px 14px; min-height: 40px; cursor: pointer; text-decoration: none; display: inline-flex; align-items: center;
  justify-content: center; gap: 6px; white-space: nowrap;
}
button:hover, .btn:hover { border-color: var(--muted); }
button.primary, .btn.primary { background: var(--brand); color: var(--brand-ink); border-color: var(--brand); font-weight: 600; }
button.danger, .btn.danger { color: var(--danger); border-color: var(--danger); }
button.ghost { background: transparent; border-color: transparent; }
button.sm, .btn.sm { padding: 4px 10px; min-height: 32px; font-size: 13px; }
button:disabled { opacity: .5; cursor: not-allowed; }
.chip {
  display: inline-flex; align-items: center; gap: 4px; padding: 4px 10px; min-height: 32px; border-radius: 999px;
  border: 1px solid var(--line); background: var(--surface); color: var(--text); font-size: 13px; text-decoration: none; cursor: pointer;
}
.chip.on { background: var(--brand); color: var(--brand-ink); border-color: var(--brand); }
.chips { display: flex; gap: 6px; overflow-x: auto; padding-bottom: 4px; -webkit-overflow-scrolling: touch; }
.chips::-webkit-scrollbar { display: none; }

/* 배지 */
.badge {
  display: inline-block; min-width: 20px; padding: 1px 7px; border-radius: 999px; font-size: 12px; font-weight: 700;
  background: var(--surface-2); color: var(--muted); text-align: center; line-height: 18px;
}
.badge.red { background: var(--danger); color: #fff; }
.badge.warn { background: var(--warn-soft); color: var(--warn); }
.badge.info { background: var(--info-soft); color: var(--info); }
.badge.ok { background: var(--brand-soft); color: var(--best); }
.badge.best { background: var(--best); color: #fff; }
.st-new { background: var(--warn-soft); color: var(--warn); }
.st-ordered { background: var(--info-soft); color: var(--info); }
.st-invoiced { background: var(--brand-soft); color: var(--best); }
.st-done { background: var(--surface-2); color: var(--muted); }
.st-canceled { background: var(--danger-soft); color: var(--danger); }

/* 폼 */
label.f { display: block; font-size: 13px; color: var(--muted); }
label.f > input, label.f > select, label.f > textarea { margin-top: 4px; }
input, select, textarea {
  font: inherit; font-size: 16px; /* iOS 확대 방지 */ width: 100%; padding: 9px 10px; border-radius: 10px;
  border: 1px solid var(--line); background: var(--surface); color: var(--text); min-height: 42px;
}
input[type="checkbox"], input[type="radio"] { width: 20px; height: 20px; min-height: 0; padding: 0; accent-color: var(--brand); }
textarea { min-height: 70px; resize: vertical; }
.check { display: inline-flex; align-items: center; gap: 6px; font-size: 14px; color: var(--text); }
fieldset { border: 1px solid var(--line); border-radius: var(--radius); padding: 10px 12px; margin: 0; }
legend { font-size: 13px; color: var(--muted); padding: 0 4px; }
.search { position: relative; }

/* 마감 보드 */
.cutoff { display: flex; gap: 12px; align-items: center; border-left: 5px solid var(--line); }
.cutoff.urgent { border-left-color: var(--danger); background: var(--danger-soft); }
.cutoff.soon { border-left-color: var(--warn); background: var(--warn-soft); }
.cutoff.ok { border-left-color: var(--brand); }
.cutoff .left { font-size: 22px; font-weight: 800; font-variant-numeric: tabular-nums; }
.cutoff.urgent .left { color: var(--danger); }
.cutoff.soon .left { color: var(--warn); }
.cutoff .pending { text-align: center; min-width: 64px; }
.cutoff .pending b { display: block; font-size: 24px; line-height: 1.1; }

.stats { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; }
.stat { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 10px; text-align: center; text-decoration: none; color: var(--text); }
.stat b { display: block; font-size: 22px; font-variant-numeric: tabular-nums; }
.stat small { font-size: 12px; }

/* 가격비교 */
.pgroup h3 { display: flex; justify-content: space-between; gap: 8px; align-items: baseline; flex-wrap: wrap; }
.offer {
  display: grid; grid-template-columns: 1fr auto; gap: 2px 10px; padding: 10px 0; border-top: 1px solid var(--line);
}
.offer:first-of-type { border-top: 0; }
.offer.best .total { color: var(--best); }
.offer.sold { opacity: .55; }
.offer .total { font-size: 17px; font-weight: 800; text-align: right; font-variant-numeric: tabular-nums; }
.offer .meta { grid-column: 1 / -1; font-size: 12.5px; color: var(--muted); display: flex; gap: 10px; flex-wrap: wrap; }
.up { color: var(--danger); }
.down { color: var(--best); }
.margin-neg { color: var(--danger); font-weight: 700; }

/* 주문 목록 */
.order { display: grid; grid-template-columns: auto 1fr; gap: 4px 10px; align-items: start; }
.order .body { min-width: 0; }
.order .line1 { display: flex; gap: 6px; align-items: center; flex-wrap: wrap; }
.order .addr { font-size: 13px; color: var(--muted); overflow-wrap: anywhere; }
.order .tools { grid-column: 1 / -1; display: flex; gap: 6px; flex-wrap: wrap; }
.copyline { display: flex; gap: 6px; align-items: center; padding: 6px 0; border-top: 1px dashed var(--line); }
.copyline:first-child { border-top: 0; }
.copyline .v { flex: 1; min-width: 0; overflow-wrap: anywhere; }

/* 표 */
.table-wrap { overflow-x: auto; -webkit-overflow-scrolling: touch; border: 1px solid var(--line); border-radius: 10px; }
table { border-collapse: collapse; width: 100%; font-size: 13px; }
th, td { padding: 6px 8px; border-bottom: 1px solid var(--line); text-align: left; white-space: nowrap; }
th { background: var(--surface-2); font-weight: 600; }

/* 템플릿 편집기 */
.tpl-row { display: grid; grid-template-columns: 1fr 1fr auto; gap: 6px; align-items: center; margin-bottom: 6px; }
.tpl-row .tools { display: flex; gap: 2px; }
.tpl-row .fixed { grid-column: 1 / -1; }

/* 다이얼로그 */
dialog {
  border: 0; border-radius: 16px; padding: 0; width: min(640px, calc(100vw - 16px)); max-height: calc(100dvh - 24px);
  background: var(--surface); color: var(--text); box-shadow: 0 10px 40px rgba(0, 0, 0, .3);
}
dialog::backdrop { background: rgba(0, 0, 0, .45); }
dialog form { display: flex; flex-direction: column; max-height: calc(100dvh - 24px); margin: 0; }
.dlg-head { padding: 14px 16px; border-bottom: 1px solid var(--line); display: flex; justify-content: space-between; align-items: center; gap: 8px; }
.dlg-head h2 { margin: 0; }
.dlg-body { padding: 14px 16px; overflow-y: auto; }
.dlg-foot { padding: 12px 16px; border-top: 1px solid var(--line); display: flex; gap: 8px; justify-content: flex-end; flex-wrap: wrap; }
@media (max-width: 640px) {
  dialog { width: 100vw; max-width: 100vw; margin: auto 0 0; border-radius: 16px 16px 0 0; }
  dialog form { max-height: 92dvh; }
  .dlg-foot { padding-bottom: calc(12px + env(safe-area-inset-bottom)); }
  .dlg-foot button { flex: 1; }
}

.toast {
  position: fixed; left: 50%; bottom: calc(var(--tabs-h) + 20px + env(safe-area-inset-bottom)); transform: translateX(-50%);
  background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: 10px; font-size: 14px; z-index: 100;
  opacity: 0; pointer-events: none; transition: opacity .2s; max-width: calc(100vw - 32px);
}
.toast.show { opacity: 1; }

.notice { border-radius: 10px; padding: 10px 12px; font-size: 13.5px; background: var(--info-soft); color: var(--text); }
.notice.warn { background: var(--warn-soft); }
.notice.danger { background: var(--danger-soft); }
.kbd { font-family: ui-monospace, Menlo, Consolas, monospace; background: var(--surface-2); padding: 1px 5px; border-radius: 5px; }
details > summary { cursor: pointer; color: var(--muted); font-size: 13px; }
