:root {
  --bg: #f4f6f9;
  --card: #fff;
  --line: #e5e8ee;
  --line2: #f0f2f5;
  --text: #1f2937;
  --muted: #6b7280;
  --brand: #2563eb;
  --brand-soft: #eaf1ff;
  --ok: #059669;   --ok-soft: #e7f8f0;
  --warn: #d97706; --warn-soft: #fff4e0;
  --short: #dc2626; --short-soft: #fdecec;
  --over: #7c3aed; --over-soft: #f2ebff;
  --none: #6b7280; --none-soft: #f1f3f5;
  --radius: 14px;
}
* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  font-family: Pretendard, -apple-system, "Malgun Gothic", sans-serif;
  background: var(--bg); color: var(--text); font-size: 14px; line-height: 1.5;
}
[hidden] { display: none !important; }
h1, h2, h3 { margin: 0; letter-spacing: -0.02em; }
h1 { font-size: 22px; }
h2 { font-size: 17px; }
h3 { font-size: 16px; }
.sub, .hint { color: var(--muted); font-size: 12.5px; margin: 4px 0 0; }
b { font-weight: 700; }

#app { max-width: 1320px; margin: 0 auto; padding: 16px; display: flex; flex-direction: column; gap: 16px; }
.card { background: var(--card); border-radius: var(--radius); padding: 20px; box-shadow: 0 1px 3px rgba(15, 23, 42, .06); }

/* ── 버튼 ── */
.btn {
  font: inherit; font-size: 13px; font-weight: 600; border: 1px solid var(--line); background: #fff; color: var(--text);
  border-radius: 9px; padding: 7px 12px; cursor: pointer; white-space: nowrap; display: inline-flex; align-items: center; gap: 6px;
}
.btn:hover { border-color: #cbd2dc; background: #fafbfc; }
.btn.primary { background: var(--brand); border-color: var(--brand); color: #fff; }
.btn.primary:hover { background: #1d4ed8; }
.btn.danger { color: var(--short); }
.btn.ghost { background: transparent; }
.btn.sm { padding: 4px 9px; font-size: 12px; }
.btn.toggle.on { background: var(--brand-soft); border-color: var(--brand); color: var(--brand); }
.btn:disabled { opacity: .5; cursor: not-allowed; }
.btn.google { width: 100%; justify-content: center; padding: 11px; font-size: 15px; }
.count { background: var(--short); color: #fff; border-radius: 999px; font-size: 11px; padding: 0 7px; margin-left: 2px; }

/* ── 헤더 ── */
.top-row { display: flex; justify-content: space-between; gap: 16px; flex-wrap: wrap; align-items: flex-start; }
.top-actions { display: flex; gap: 8px; flex-wrap: wrap; margin-top: 14px; justify-content: flex-end; }
.live { display: inline-flex; align-items: center; gap: 5px; color: var(--ok); font-weight: 600; }
.live i { width: 7px; height: 7px; border-radius: 50%; background: var(--ok); box-shadow: 0 0 0 3px var(--ok-soft); }
.live.off { color: var(--short); } .live.off i { background: var(--short); box-shadow: 0 0 0 3px var(--short-soft); }
.who { display: flex; align-items: center; gap: 10px; }
.who img { width: 34px; height: 34px; border-radius: 50%; background: var(--line); object-fit: cover; }
.roles { display: flex; gap: 4px; flex-wrap: wrap; margin-top: 2px; }
.badge { display: inline-block; font-size: 11px; font-weight: 600; padding: 1px 7px; border-radius: 999px; background: var(--none-soft); color: var(--none); }
.badge.admin { background: #111827; color: #fff; }
.badge.sales { background: var(--ok-soft); color: var(--ok); }
.badge.purchase { background: var(--brand-soft); color: var(--brand); }
.badge.demo { background: var(--warn-soft); color: var(--warn); }

/* ── 상태 색 ── */
.st-ok { --c: var(--ok); --cs: var(--ok-soft); }
.st-warn { --c: var(--warn); --cs: var(--warn-soft); }
.st-short { --c: var(--short); --cs: var(--short-soft); }
.st-over { --c: var(--over); --cs: var(--over-soft); }
.st-none { --c: var(--none); --cs: var(--none-soft); }
.chip { display: inline-block; font-size: 11.5px; font-weight: 700; padding: 1px 8px; border-radius: 999px; background: var(--cs); color: var(--c); }

/* ── 요약 카드 ── */
.cards { display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: 14px; }
.icard { background: #fff; border-radius: var(--radius); padding: 16px 18px; border-top: 4px solid var(--c); box-shadow: 0 1px 3px rgba(15, 23, 42, .06); }
.icard .name { display: flex; align-items: center; gap: 6px; font-size: 16px; font-weight: 700; margin-top: 4px; }
.icard .emoji { font-size: 22px; }
.icard .big { font-size: 26px; font-weight: 800; color: var(--c); letter-spacing: -0.03em; margin: 6px 0 2px; }
.icard .stock { font-size: 13px; }
.icard dl { display: grid; grid-template-columns: auto 1fr; gap: 2px 10px; margin: 10px 0 0; padding-top: 10px; border-top: 1px dashed var(--line); font-size: 12.5px; }
.icard dt { color: var(--muted); } .icard dd { margin: 0; text-align: right; font-weight: 600; }

/* ── 섹션 ── */
.sec-head { display: flex; justify-content: space-between; align-items: flex-end; gap: 12px; flex-wrap: wrap; margin-bottom: 14px; }
.tools { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.tools label { font-size: 12.5px; color: var(--muted); display: flex; gap: 4px; align-items: center; }
input, select, textarea { font: inherit; font-size: 13px; border: 1px solid var(--line); border-radius: 8px; padding: 5px 8px; background: #fff; color: var(--text); }
input:focus, select:focus { outline: 2px solid var(--brand-soft); border-color: var(--brand); }
.notice { background: var(--brand-soft); color: #1e40af; border-radius: 10px; padding: 8px 12px; font-size: 12.5px; margin-bottom: 10px; }

/* ── 원장 테이블 ── */
.table-wrap { overflow-x: auto; border: 1px solid var(--line); border-radius: 12px; }
table.ledger { border-collapse: collapse; width: 100%; font-size: 13px; font-variant-numeric: tabular-nums; }
.ledger th, .ledger td { padding: 6px 10px; border-bottom: 1px solid var(--line2); white-space: nowrap; }
.ledger thead th { position: sticky; top: 0; background: #f8f9fb; font-size: 12px; color: var(--muted); font-weight: 600; text-align: right; border-bottom: 1px solid var(--line); }
.ledger thead th:first-child, .ledger thead th:nth-child(2) { text-align: left; }
.ledger td.n { text-align: right; min-width: 78px; }
.ledger td.item { vertical-align: top; border-right: 1px solid var(--line); background: #fff; position: sticky; left: 0; z-index: 1; width: 132px; min-width: 132px; max-width: 132px; white-space: normal; }
.ledger td.lbl { position: sticky; left: 132px; z-index: 1; background: #fff; min-width: 116px; box-shadow: 1px 0 0 var(--line); }
.ledger thead th:first-child { position: sticky; left: 0; z-index: 2; }
.ledger thead th:nth-child(2) { position: sticky; left: 132px; z-index: 2; }
.ledger td.item .nm { font-weight: 700; font-size: 14px; display: flex; gap: 6px; align-items: center; }
.ledger td.item .unit { color: var(--muted); font-size: 11.5px; }
.ledger td.lbl { color: var(--muted); font-size: 12.5px; }
.ledger tr.grp-end td { border-bottom: 2px solid var(--line); }
.ledger tr.r-end td.n { font-weight: 800; }
.ledger tr.r-end td.lbl { color: var(--text); font-weight: 700; }
.ledger tr.r-sub td { background: #fbfcfe; }
.ledger tr.r-sub td.lbl { padding-left: 22px; font-size: 12px; }
.ledger tr.r-sub.mine td { background: #f1fbf6; }
.ledger tr.r-usage td.n { color: #b45309; }
.ledger tr.r-purchase td.n { color: var(--brand); }
.ledger td.cur, .ledger th.cur { background: #f2f6ff; }
.ledger td.neg { color: var(--short) !important; }
.ledger td.na { color: #cbd2dc; }
.ledger .cov { font-size: 11.5px; }
.ledger input.cell { width: 86px; text-align: right; padding: 3px 6px; font-variant-numeric: tabular-nums; }
.ledger input.cell.saved { background: var(--ok-soft); transition: background .6s; }
.ledger .exp { cursor: pointer; color: var(--brand); font-size: 11px; margin-left: 4px; }

/* ── 차트 ── */
.charts { display: grid; grid-template-columns: repeat(auto-fill, minmax(380px, 1fr)); gap: 14px; }
.chart-card { border: 1px solid var(--line); border-radius: 12px; padding: 12px 14px; }
.chart-card h4 { margin: 0 0 6px; font-size: 14px; display: flex; gap: 6px; align-items: center; }
.chart-box { position: relative; height: 230px; }

/* ── 이력 ── */
.logs { display: flex; flex-direction: column; font-size: 12.5px; max-height: 320px; overflow: auto; }
.logs .lg { display: grid; grid-template-columns: 120px 110px 1fr; gap: 10px; padding: 6px 4px; border-bottom: 1px solid var(--line2); }
.logs .lg time { color: var(--muted); }
.empty { color: var(--muted); padding: 16px; text-align: center; font-size: 13px; }

/* ── 게이트 ── */
.gate { min-height: 100vh; display: grid; place-items: center; padding: 16px; background: linear-gradient(135deg, #1e3a5f, #2c6aa0); }
.gate-card { background: #fff; border-radius: 18px; padding: 36px 32px; width: 100%; max-width: 400px; text-align: center; box-shadow: 0 20px 50px rgba(0, 0, 0, .25); }
.gate-logo { font-size: 44px; }
.gate-card h1 { font-size: 20px; margin: 6px 0 4px; }
.gate-card p { color: var(--muted); font-size: 13px; }
.gate-card .btn { margin-top: 8px; }

/* ── 모달 ── */
.modal { position: fixed; inset: 0; background: rgba(15, 23, 42, .45); display: grid; place-items: center; padding: 16px; z-index: 50; }
.modal-box { background: #fff; border-radius: 16px; width: 100%; max-width: 1080px; max-height: calc(100vh - 32px); display: flex; flex-direction: column; overflow: hidden; }
.modal-head { display: flex; justify-content: space-between; align-items: center; padding: 14px 20px; border-bottom: 1px solid var(--line); }
.modal-body { padding: 16px 20px 22px; overflow: auto; }
.tabs { display: flex; gap: 4px; border-bottom: 1px solid var(--line); margin-bottom: 14px; }
.tabs button { font: inherit; font-weight: 600; background: none; border: 0; padding: 8px 14px; cursor: pointer; color: var(--muted); border-bottom: 2px solid transparent; }
.tabs button.on { color: var(--brand); border-color: var(--brand); }
table.grid { border-collapse: collapse; width: 100%; font-size: 13px; }
.grid th, .grid td { border-bottom: 1px solid var(--line2); padding: 6px 6px; text-align: left; vertical-align: middle; }
.grid th { font-size: 12px; color: var(--muted); font-weight: 600; background: #f8f9fb; }
.grid td.r, .grid th.r { text-align: right; white-space: nowrap; }
.grid label { white-space: nowrap; }
.picker summary { cursor: pointer; border: 1px solid var(--line); border-radius: 8px; padding: 5px 8px; font-size: 13px; list-style: none; }
.picker summary::after { content: " ▾"; color: var(--muted); }
.picker-list { display: flex; flex-direction: column; gap: 2px; margin-top: 4px; padding: 6px 8px; border: 1px solid var(--line); border-radius: 8px; max-height: 200px; overflow: auto; background: #fff; }
.picker-list label { font-size: 12.5px; color: var(--text); }
.grid input[type=text], .grid input[type=number], .grid input[type=email], .grid input[type=month] { width: 100%; min-width: 60px; }
.grid tr.off td { opacity: .5; }
.grid tr.bad td { background: var(--short-soft); }
.grid tr.skip td { opacity: .45; }
.form-row { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; margin: 10px 0; }
.form-row label { font-size: 12.5px; color: var(--muted); display: flex; align-items: center; gap: 4px; }
.step { border: 1px solid var(--line); border-radius: 12px; padding: 12px 14px; margin-bottom: 12px; }
.step h4 { margin: 0 0 8px; font-size: 13.5px; }
details.step summary { cursor: pointer; font-size: 13.5px; }
.radio-set { display: flex; gap: 6px; flex-wrap: wrap; }
.radio-set label { border: 1px solid var(--line); border-radius: 8px; padding: 6px 10px; cursor: pointer; font-size: 13px; color: var(--text); }
.radio-set label:has(input:checked) { border-color: var(--brand); background: var(--brand-soft); color: var(--brand); font-weight: 600; }
.radio-set input { display: none; }
.raw-preview { max-height: 220px; overflow: auto; border: 1px solid var(--line); border-radius: 8px; }
.raw-preview table { border-collapse: collapse; font-size: 12px; }
.raw-preview td, .raw-preview th { border: 1px solid var(--line2); padding: 3px 6px; white-space: nowrap; max-width: 220px; overflow: hidden; text-overflow: ellipsis; }
.raw-preview th { background: #f8f9fb; position: sticky; top: 0; }
.raw-preview tr.hdr td { background: var(--brand-soft); font-weight: 700; }
.diff-up { color: var(--brand); font-weight: 700; }
.diff-down { color: var(--short); font-weight: 700; }

.toast { position: fixed; bottom: 22px; left: 50%; transform: translateX(-50%); background: #111827; color: #fff; padding: 10px 16px; border-radius: 10px; font-size: 13px; z-index: 60; box-shadow: 0 8px 20px rgba(0, 0, 0, .2); }
.toast.err { background: var(--short); }
.foot { text-align: center; color: var(--muted); font-size: 12px; padding: 8px 0 24px; }

@media (max-width: 640px) {
  #app { padding: 10px 16px; }
  .card { padding: 14px; }
  .charts { grid-template-columns: 1fr; }
  .logs .lg { grid-template-columns: 1fr; gap: 0; }
  .top-actions { justify-content: stretch; }
  .top-actions .btn { flex: 1; justify-content: center; }
}
