:root {
  --primary: #1f6feb;
  --primary-dark: #1a5fd0;
  --danger: #d1242f;
  --danger-dark: #b21e28;
  --ok: #1a7f37;
  --bg: #f6f8fa;
  --card: #ffffff;
  --border: #d0d7de;
  --text: #1f2328;
  --muted: #656d76;
  --radius: 12px;
  --tap: 52px;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Apple SD Gothic Neo",
    "Malgun Gothic", Roboto, sans-serif;
  font-size: 16px;
  -webkit-text-size-adjust: 100%;
}

#app {
  max-width: 560px;
  margin: 0 auto;
  min-height: 100vh;
  background: var(--bg);
  display: flex;
  flex-direction: column;
}

/* ---- 상단바 ---- */
#topbar {
  position: sticky;
  top: 0;
  z-index: 20;
  background: var(--primary);
  color: #fff;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: max(env(safe-area-inset-top), 10px) 14px 10px;
  min-height: 56px;
}
#topbar .title { font-size: 18px; font-weight: 700; flex: 1; }
#topbar button {
  background: rgba(255, 255, 255, 0.18);
  color: #fff;
  border: none;
  border-radius: 8px;
  padding: 8px 12px;
  font-size: 14px;
}
#topbar .back { font-size: 20px; padding: 6px 10px; line-height: 1; }

#view {
  flex: 1;
  padding: 16px 14px 40px;
}

/* ---- 카드/섹션 ---- */
.card {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px;
  margin-bottom: 14px;
}
.card h2 { margin: 0 0 12px; font-size: 17px; }
.card h3 { margin: 0 0 8px; font-size: 15px; color: var(--muted); }

.section-label {
  font-size: 13px;
  font-weight: 700;
  color: var(--muted);
  margin: 0 0 6px;
  text-transform: none;
}

/* ---- 버튼 ---- */
button, .btn {
  font-family: inherit;
  font-size: 16px;
  min-height: var(--tap);
  border-radius: 10px;
  border: 1px solid transparent;
  background: var(--primary);
  color: #fff;
  padding: 0 16px;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
}
button:disabled { opacity: 0.5; }
.btn-block { width: 100%; }
.btn-secondary { background: #eaeef2; color: var(--text); border-color: var(--border); }
.btn-danger { background: var(--danger); }
.btn-danger:active { background: var(--danger-dark); }
.btn-ghost { background: transparent; color: var(--primary); border-color: var(--border); }
.btn-sm { min-height: 40px; font-size: 14px; padding: 0 12px; }

/* ---- 홈 그리드 ---- */
.home-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
}
.home-tile {
  min-height: 96px;
  border-radius: var(--radius);
  flex-direction: column;
  gap: 6px;
  font-size: 18px;
  font-weight: 700;
}
.home-tile .emoji { font-size: 30px; }
.home-tile.primary { background: var(--primary); }
.home-tile.wide { grid-column: span 2; }
.home-tile.sub { background: #eaeef2; color: var(--text); font-size: 16px; font-weight: 600; }

/* ---- 폼 ---- */
label.field { display: block; margin-bottom: 12px; }
label.field .lbl { display: block; font-size: 13px; color: var(--muted); margin-bottom: 4px; }
input, select, textarea {
  width: 100%;
  max-width: 100%;
  min-width: 0; /* 좁은 화면에서 date/number 입력이 폭을 넘기지 않도록 */
  box-sizing: border-box;
  font-family: inherit;
  font-size: 16px;
  min-height: var(--tap);
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: #fff;
  color: var(--text);
}
textarea { min-height: 72px; resize: vertical; }
.row { display: flex; gap: 8px; flex-wrap: wrap; }
.row > * { flex: 1 1 0; min-width: 0; }

/* ---- 리스트 ---- */
.list { list-style: none; margin: 0; padding: 0; }
.list-item {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 12px 4px;
  border-bottom: 1px solid var(--border);
}
.list-item:last-child { border-bottom: none; }
.list-item.col { flex-direction: column; align-items: stretch; }
.list-item.col .row { margin-top: 6px; }
.list-item .grow { flex: 1; min-width: 0; }
.list-item .name { font-weight: 600; }
.list-item .sub { font-size: 13px; color: var(--muted); margin-top: 2px; }
.list-item .qty { font-weight: 700; font-size: 18px; }

/* ---- 뱃지 ---- */
.badge {
  display: inline-block;
  font-size: 12px;
  font-weight: 700;
  padding: 3px 8px;
  border-radius: 999px;
  color: #fff;
}
.badge.in { background: var(--ok); }
.badge.out { background: var(--danger); }
.badge.gray { background: var(--muted); }
.badge.move { background: #8957e5; }
.badge.stock { background: #1f6feb; }
.badge.act { background: #57606a; }

/* 선택된 항목 요약 카드 */
.picked {
  display: flex;
  align-items: center;
  gap: 10px;
  background: #eef6ff;
  border: 1px solid #b6d5ff;
  border-radius: 10px;
  padding: 10px 12px;
}
.picked .grow { flex: 1; }
.picked .name { font-weight: 700; }
.picked .sub { font-size: 13px; color: var(--muted); }

.muted { color: var(--muted); }
.small { font-size: 13px; }
.center { text-align: center; }
.mt { margin-top: 12px; }
.mb { margin-bottom: 12px; }
.hidden { display: none !important; }
.spacer { height: 8px; }

.tabs { display: flex; gap: 6px; margin-bottom: 12px; }
.tabs button { flex: 1; min-height: 44px; }
.tabs button.active { background: var(--primary); color: #fff; }
.tabs button:not(.active) { background: #eaeef2; color: var(--text); }

/* 스캔↔검색 토글 */
.toggle { display: flex; border: 1px solid var(--border); border-radius: 10px; overflow: hidden; margin-bottom: 10px; }
.toggle button { flex: 1; border: none; border-radius: 0; background: #fff; color: var(--text); min-height: 46px; }
.toggle button.active { background: var(--primary); color: #fff; }

/* ---- 스캐너 오버레이 ---- */
#scanner-overlay {
  position: fixed;
  inset: 0;
  z-index: 100;
  background: #000;
  display: flex;
  flex-direction: column;
}
#scanner-overlay .scan-head {
  color: #fff;
  padding: max(env(safe-area-inset-top), 12px) 14px 12px;
  display: flex;
  align-items: center;
  gap: 10px;
}
#scanner-overlay .scan-head .title { flex: 1; font-weight: 700; }
#reader { flex: 1; width: 100%; }
#scanner-overlay .scan-foot { padding: 14px; color: #fff; }
#scanner-overlay .manual { display: flex; gap: 8px; }
#scanner-overlay .manual input { flex: 1; }

/* ---- 토스트 ---- */
#toast-root {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 24px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  z-index: 200;
  pointer-events: none;
}
.toast {
  background: #1f2328;
  color: #fff;
  padding: 12px 18px;
  border-radius: 999px;
  font-size: 14px;
  max-width: 88%;
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.3);
}
.toast.ok { background: var(--ok); }
.toast.err { background: var(--danger); }

/* 트리(로케이션) */
.tree-node { border: 1px solid var(--border); border-radius: 10px; margin-bottom: 8px; background: #fff; }
.tree-head { display: flex; flex-direction: column; gap: 8px; padding: 12px; }
.tree-head .grow { flex: 1; min-width: 0; }
.tree-line1 { display: flex; align-items: center; gap: 8px; }
.tree-line1 .name { word-break: keep-all; }
.tree-badges { display: flex; gap: 6px; flex-wrap: wrap; }
.tree-actions { display: flex; gap: 6px; flex-wrap: wrap; }
.tree-actions .btn-sm { flex: 1 1 auto; }
.tree-children { padding: 0 10px 10px 22px; }
.tree-toggle { width: 30px; min-height: 30px; padding: 0; background: #eaeef2; color: var(--text); border-radius: 8px; }
.tree-node.sub { border-style: dashed; border-color: #d0d7de; }
.tree-head .name .code { font-weight: 700; }
.tree-head .badge { flex: none; }

/* 분할/병합 패널 */
.op-panel { padding: 10px 12px; border-top: 1px dashed var(--border); background: #f6f8fa; }
.op-panel .section-label { margin-top: 8px; }
.op-row { display: flex; gap: 8px; align-items: center; margin-bottom: 8px; flex-wrap: wrap; }
.op-row input, .op-row select { flex: 1; min-width: 90px; }
.op-table { width: 100%; border-collapse: collapse; font-size: 13px; }
.op-table th, .op-table td { border: 1px solid var(--border); padding: 6px; text-align: center; }
.op-table th { background: #eaeef2; }
.op-table td.left { text-align: left; }
.op-table input { width: 64px; text-align: right; }
.merge-check { display: flex; align-items: center; gap: 8px; padding: 6px 0; }
.merge-check input[type=checkbox] { width: 20px; height: 20px; }

/* 로케이션 선택기 브레드크럼 */
.crumbs { margin: 6px 0; color: var(--muted); }
.crumbs a { color: #1f6feb; text-decoration: none; }

/* 대시보드 통계 카드 */
.scan-bar { display: flex; gap: 8px; align-items: center; }
.scan-bar input { flex: 1 1 auto; min-width: 0; }
.scan-bar .btn-sm { flex: 0 0 auto; }

.stat-grid { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; }
.stat-grid3 { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
.stat { display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px;
  border: 1px solid var(--border); border-radius: 10px; padding: 10px 4px; background: #fff; text-align: center; cursor: pointer; }
.stat.static { cursor: default; }
.stat .stat-num { font-size: 20px; font-weight: 800; line-height: 1.05; max-width: 100%; overflow: hidden; text-overflow: ellipsis; }
.stat .stat-label { font-size: 11px; color: var(--muted); white-space: nowrap; }
.stat.blue .stat-num { color: #1f6feb; }
.stat.warn .stat-num { color: #b8860b; }
.stat.danger .stat-num { color: var(--danger); }
.stat.move .stat-num { color: #8250df; }
@media (max-width: 380px) { .stat-grid { grid-template-columns: repeat(2, 1fr); } }

/* 오프라인 상태 배지 */
#offline-badge {
  display: none;
  position: fixed;
  left: 50%;
  bottom: 12px;
  transform: translateX(-50%);
  z-index: 60;
  background: #57606a;
  color: #fff;
  font-size: 13px;
  font-weight: 700;
  padding: 6px 14px;
  border-radius: 999px;
  box-shadow: 0 2px 8px rgba(0,0,0,.2);
}

/* QR 썸네일 */
.qr-thumb { width: 44px; height: 44px; border: 1px solid var(--border); border-radius: 6px; }

/* ---- 라벨 인쇄 ---- */
.label-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
  gap: 10px;
}
.label-card {
  border: 1px dashed var(--border);
  border-radius: 8px;
  padding: 10px;
  text-align: center;
  background: #fff;
}
.label-card img { width: 100%; max-width: 160px; height: auto; }
.label-grid.qr-only .label-card { padding: 6px; }
.label-card .cap { font-size: 12px; margin-top: 6px; word-break: break-all; }
.label-card .cap b { display: block; font-size: 13px; }

@media print {
  #topbar, #toast-root, .no-print { display: none !important; }
  body, #app, #view { background: #fff; }
  #app { max-width: none; }
  #view { padding: 0; }
  .label-grid { grid-template-columns: repeat(3, 1fr); gap: 6mm; }
  .label-card { break-inside: avoid; border: 1px solid #000; }
}
