:root {
  --bg: #f4f6f9;
  --panel: #ffffff;
  --line: #e1e5eb;
  --line-strong: #c9d0da;
  --text: #1d2733;
  --muted: #6b7684;
  --brand: #1f4e8c;
  --brand-dark: #173c6c;
  --brand-soft: #e8eff9;
  --ok: #1d7a46;
  --ok-soft: #e4f4ea;
  --warn: #a35d00;
  --warn-soft: #fff3df;
  --danger: #b3261e;
  --danger-soft: #fdeceb;
  --internal: #fbf7ec;
  --radius: 8px;
  font-family: 'Pretendard', 'Malgun Gothic', '맑은 고딕', -apple-system, 'Segoe UI', sans-serif;
  font-size: 14px;
  color: var(--text);
}
* { box-sizing: border-box; }
body { margin: 0; background: var(--bg); }
a { color: var(--brand); text-decoration: none; }
a:hover { text-decoration: underline; }
h1 { font-size: 20px; margin: 0; }
h2 { font-size: 16px; margin: 0 0 12px; }
h3 { font-size: 14px; margin: 0 0 8px; }
.muted { color: var(--muted); }
.small { font-size: 12px; }
.num { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
.center { text-align: center; }
.hidden { display: none !important; }

/* 레이아웃 */
.app { display: grid; grid-template-columns: 210px 1fr; grid-template-rows: 54px 1fr; min-height: 100vh; }
.topbar { grid-column: 1 / -1; display: flex; align-items: center; gap: 16px; padding: 0 20px; background: var(--brand-dark); color: #fff; }
.topbar .logo { font-weight: 700; font-size: 16px; letter-spacing: .3px; }
.topbar .logo span { opacity: .7; font-weight: 400; margin-left: 6px; font-size: 13px; }
.topbar .rates { display: flex; gap: 8px; margin-left: 16px; }
.rate-chip { background: rgba(255,255,255,.12); border-radius: 999px; padding: 3px 10px; font-size: 12px; font-variant-numeric: tabular-nums; }
.topbar .spacer { flex: 1; }
.topbar .who { font-size: 13px; opacity: .9; }
.topbar button { background: transparent; border: 1px solid rgba(255,255,255,.4); color: #fff; }
.topbar button:hover { background: rgba(255,255,255,.1); }
.sidebar { background: var(--panel); border-right: 1px solid var(--line); padding: 14px 10px; }
.sidebar a { display: block; padding: 9px 12px; border-radius: 6px; color: var(--text); margin-bottom: 2px; }
.sidebar a:hover { background: var(--bg); text-decoration: none; }
.sidebar a.active { background: var(--brand-soft); color: var(--brand); font-weight: 600; }
.sidebar .sep { height: 1px; background: var(--line); margin: 10px 4px; }
.main { padding: 22px 26px 60px; overflow-x: auto; }
.page-head { display: flex; align-items: center; gap: 12px; margin-bottom: 18px; flex-wrap: wrap; }
.page-head .spacer { flex: 1; }

/* 카드·표 */
.card { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 18px; margin-bottom: 16px; }
.card.internal { background: var(--internal); border-color: #eadfbf; }
.card .card-head { display: flex; align-items: center; gap: 10px; margin-bottom: 12px; }
.card .card-head h2 { margin: 0; }
.card .card-head .spacer { flex: 1; }
table.list { width: 100%; border-collapse: collapse; background: var(--panel); }
table.list th, table.list td { padding: 9px 10px; border-bottom: 1px solid var(--line); text-align: left; vertical-align: middle; }
table.list th { font-size: 12px; color: var(--muted); font-weight: 600; background: #fafbfc; white-space: nowrap; }
table.list tr.click { cursor: pointer; }
table.list tr.click:hover td { background: #f7f9fc; }
.table-wrap { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); overflow: auto; }

/* 폼 */
.grid { display: grid; gap: 12px 16px; }
.grid.c2 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.grid.c3 { grid-template-columns: repeat(3, minmax(0, 1fr)); }
.grid.c4 { grid-template-columns: repeat(4, minmax(0, 1fr)); }
.span2 { grid-column: span 2; }
.span3 { grid-column: span 3; }
.span-all { grid-column: 1 / -1; }
label.f { display: flex; flex-direction: column; gap: 4px; font-size: 12px; color: var(--muted); font-weight: 600; }
label.f .hint { font-weight: 400; }
input, select, textarea {
  font: inherit; color: var(--text); border: 1px solid var(--line-strong); border-radius: 6px; padding: 7px 9px; background: #fff; width: 100%;
}
textarea { resize: vertical; min-height: 36px; line-height: 1.4; }
input:focus, select:focus, textarea:focus { outline: 2px solid #9bb8e3; outline-offset: -1px; border-color: var(--brand); }
input:disabled, select:disabled, textarea:disabled { background: #f3f4f6; color: #555; }
input.num { text-align: right; }
.inline { display: flex; gap: 8px; align-items: center; }
.inline > * { flex: 0 0 auto; }
.check { display: inline-flex; align-items: center; gap: 6px; font-size: 13px; }
.check input { width: auto; }

button, .btn {
  font: inherit; cursor: pointer; border-radius: 6px; padding: 7px 14px; border: 1px solid var(--line-strong); background: #fff; color: var(--text);
  display: inline-flex; align-items: center; gap: 6px; white-space: nowrap; text-decoration: none;
}
button:hover, .btn:hover { background: #f3f5f8; text-decoration: none; }
button.primary { background: var(--brand); border-color: var(--brand); color: #fff; }
button.primary:hover { background: var(--brand-dark); }
button.danger { color: var(--danger); border-color: #e9b4b0; }
button.danger:hover { background: var(--danger-soft); }
button.sm { padding: 3px 8px; font-size: 12px; }
button.icon { padding: 3px 7px; font-size: 12px; color: var(--muted); }
button:disabled { opacity: .5; cursor: not-allowed; }

/* 배지 */
.badge { display: inline-block; padding: 2px 8px; border-radius: 999px; font-size: 12px; font-weight: 600; }
.st-draft { background: #eef0f3; color: #4b5563; }
.st-sent { background: var(--brand-soft); color: var(--brand); }
.st-won { background: var(--ok-soft); color: var(--ok); }
.st-lost { background: var(--danger-soft); color: var(--danger); }
.role-admin { background: #fde8e8; color: #9b1c1c; }
.role-manager { background: #efe7fb; color: #5b2a9a; }
.role-sales { background: var(--brand-soft); color: var(--brand); }
.role-viewer { background: #eef0f3; color: #4b5563; }
.tag-internal { background: #f3e6c1; color: #7a5a00; font-size: 11px; padding: 2px 7px; border-radius: 4px; font-weight: 600; }

/* 견적 편집 - 품목 표 */
table.items { width: 100%; border-collapse: collapse; }
table.items th { font-size: 12px; color: var(--muted); font-weight: 600; text-align: left; padding: 6px 6px; border-bottom: 2px solid var(--line); white-space: nowrap; }
table.items th.cost, table.items td.cost { background: var(--internal); }
table.items td { padding: 5px 6px; border-bottom: 1px solid var(--line); vertical-align: top; }
table.items tr.hdr td { background: #f2f6fb; }
table.items tr.hdr textarea { font-weight: 700; }
table.items td.calc { padding-top: 12px; }
table.items input, table.items select, table.items textarea { padding: 5px 7px; }
table.items .w-no { width: 34px; }
table.items .w-qty { width: 64px; }
table.items .w-cur { width: 76px; }
table.items .w-cost { width: 104px; }
table.items .w-mg { width: 72px; }
table.items .w-price { width: 116px; }
table.items .w-act { width: 92px; white-space: nowrap; }
.items-actions { display: flex; gap: 8px; margin-top: 10px; }

.summary { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 10px; }
.summary .s { background: var(--panel); border: 1px solid var(--line); border-radius: 6px; padding: 10px 12px; }
.summary .s .k { font-size: 12px; color: var(--muted); }
.summary .s .v { font-size: 17px; font-weight: 700; font-variant-numeric: tabular-nums; margin-top: 2px; }
.summary .s.big .v { color: var(--brand); font-size: 20px; }
.summary .s.good .v { color: var(--ok); }
.summary .s.bad .v { color: var(--danger); }

.cond-row { display: grid; grid-template-columns: 28px 1fr 1fr 30px; gap: 8px; align-items: start; margin-bottom: 8px; }
.cond-row .letter { padding-top: 8px; color: var(--muted); font-weight: 600; }

.actionbar { position: sticky; bottom: 0; background: rgba(244,246,249,.95); backdrop-filter: blur(4px); padding: 12px 0; display: flex; gap: 8px; flex-wrap: wrap; border-top: 1px solid var(--line); z-index: 5; }
.actionbar .spacer { flex: 1; }

.alert { padding: 10px 14px; border-radius: 6px; margin-bottom: 12px; font-size: 13px; }
.alert.warn { background: var(--warn-soft); color: var(--warn); }
.alert.info { background: var(--brand-soft); color: var(--brand); }
.alert.danger { background: var(--danger-soft); color: var(--danger); }

.fx-cards { display: grid; grid-template-columns: repeat(auto-fill, minmax(190px, 1fr)); gap: 10px; }
.fx-card { border: 1px solid var(--line); border-radius: 8px; padding: 12px 14px; background: #fbfcfe; }
.fx-cur { font-weight: 700; font-size: 13px; }
.fx-rate { font-size: 22px; font-weight: 700; font-variant-numeric: tabular-nums; margin: 2px 0; }
.fx-diff { font-size: 12px; font-variant-numeric: tabular-nums; }
.fx-diff.up { color: #c0392b; }
.fx-diff.down { color: #1f5fbf; }
.fx-sub { width: 100%; font-size: 12px; color: var(--muted); margin-top: 6px; border-collapse: collapse; }
.fx-sub td { padding: 1px 0; }
.fx-sub td:last-child { text-align: right; font-variant-numeric: tabular-nums; color: var(--text); }
.fx-warn { color: var(--warn); font-weight: 600; }

/* 로그인 */
.login-wrap { min-height: 100vh; display: flex; align-items: center; justify-content: center; background: linear-gradient(135deg, #173c6c, #2c6bb5); }
.login-box { background: #fff; border-radius: 12px; padding: 34px 34px 28px; width: 360px; box-shadow: 0 20px 50px rgba(0,0,0,.25); }
.login-box h1 { margin-bottom: 4px; }
.login-box p { margin: 0 0 22px; }
.login-box label.f { margin-bottom: 12px; }
.login-box button { width: 100%; justify-content: center; padding: 10px; margin-top: 6px; }
.login-err { color: var(--danger); font-size: 13px; min-height: 18px; margin-top: 10px; }

/* 토스트·모달 */
#toast { position: fixed; right: 20px; bottom: 20px; display: flex; flex-direction: column; gap: 8px; z-index: 100; }
.toast { background: #1d2733; color: #fff; padding: 10px 16px; border-radius: 6px; font-size: 13px; box-shadow: 0 6px 20px rgba(0,0,0,.2); }
.toast.err { background: var(--danger); }
.modal-bg { position: fixed; inset: 0; background: rgba(15,23,35,.45); display: flex; align-items: center; justify-content: center; z-index: 90; }
.modal { background: #fff; border-radius: 10px; padding: 22px; width: min(640px, 94vw); max-height: 90vh; overflow: auto; }
.modal .foot { display: flex; justify-content: flex-end; gap: 8px; margin-top: 18px; }

/* 권한표 */
table.perm td, table.perm th { text-align: center; }
table.perm td:first-child, table.perm th:first-child { text-align: left; }
.yes { color: var(--ok); font-weight: 700; }
.no { color: #c0c6cf; }
.own { color: var(--warn); font-weight: 600; }

/* 인쇄용 견적서 */
.print-page { background: #fff; max-width: 820px; margin: 0 auto; padding: 36px 40px; font-family: Arial, 'Malgun Gothic', sans-serif; font-size: 12.5px; color: #000; }
.print-page h1 { text-align: center; font-size: 26px; letter-spacing: 2px; margin: 0 0 26px; }
.print-page .meta { display: grid; grid-template-columns: 1.35fr 1fr; gap: 3px 20px; margin-bottom: 14px; }
.print-page .meta div { display: grid; grid-template-columns: 92px 1fr; }
.print-page .meta b { font-weight: 400; }
.print-page .lead { font-weight: 700; margin: 18px 0 8px; }
.print-page table { width: 100%; border-collapse: collapse; }
.print-page th, .print-page td { border-left: 1px solid #000; border-right: 1px solid #000; padding: 4px 6px; vertical-align: top; }
.print-page thead th { border: 1px solid #000; background: #efefef; }
.print-page tfoot td { border: 1px solid #000; font-weight: 700; padding: 7px 6px; }
.print-page tbody tr:last-child td { border-bottom: 1px solid #000; }
.print-page .spec { white-space: pre-wrap; }
.print-page .conds { margin-top: 8px; }
.print-page .conds div { display: grid; grid-template-columns: 24px 1fr; margin-bottom: 8px; }
.print-tools { max-width: 820px; margin: 16px auto; display: flex; gap: 8px; }
@media print {
  body { background: #fff; }
  .print-tools, .topbar, .sidebar, #toast { display: none !important; }
  .app { display: block; }
  .main { padding: 0; }
  .print-page { padding: 0; max-width: none; }
  @page { size: A4; margin: 14mm 12mm; }
}

@media (max-width: 900px) {
  .app { grid-template-columns: 1fr; grid-template-rows: auto auto 1fr; }
  .sidebar { display: flex; overflow-x: auto; border-right: 0; border-bottom: 1px solid var(--line); padding: 6px; }
  .sidebar a { white-space: nowrap; }
  .sidebar .sep { display: none; }
  .topbar .rates { display: none; }
  .grid.c3, .grid.c4 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
}

/* 제품 연결 표시 (견적 품목) */
.prod-tag { font-size: 11.5px; color: var(--muted); margin-top: 3px; display: flex; gap: 6px; align-items: center; flex-wrap: wrap; }
.prod-tag a { font-weight: 600; }
.prod-tag button { padding: 1px 6px; font-size: 11px; }
