/* 컴포넌트: 상단바, 사이드 메뉴, 하단 탭, 버튼, 배지, 배너, 토스트, 빈 상태, 표 */

/* 상단바 */
.topbar {
  display: flex; flex-wrap: wrap; align-items: center; gap: var(--sp-2) var(--sp-3);
  min-height: var(--topbar-h); padding: var(--sp-2) var(--sp-4);
  background: var(--surface); border-bottom: 1px solid var(--border);
}
.brand { display: flex; align-items: baseline; gap: var(--sp-2); font-weight: 700; font-size: var(--fs-lg); }
.brand small { font-weight: 400; color: var(--text-muted); font-size: var(--fs-sm); }
.topbar-spacer { flex: 1 1 0; }
.chip {
  display: inline-flex; align-items: center; gap: var(--sp-1);
  padding: 2px var(--sp-2); border-radius: 999px; font-size: var(--fs-sm);
  background: var(--surface-2); color: var(--text);
}
.chip-label { color: var(--text-muted); }

.seg { display: inline-flex; border: 1px solid var(--border); border-radius: var(--radius); overflow: hidden; }
.seg button {
  min-height: var(--touch); min-width: var(--touch); padding: 0 var(--sp-3);
  border: 0; border-right: 1px solid var(--border); background: var(--surface); cursor: pointer;
}
.seg button:last-child { border-right: 0; }
.seg button[aria-pressed="true"] { background: var(--accent); color: var(--accent-contrast); font-weight: 600; }

/* 버튼 */
.btn {
  min-height: var(--touch); padding: 0 var(--sp-4); border-radius: var(--radius);
  border: 1px solid var(--border); background: var(--surface); cursor: pointer;
}
.btn:hover { background: var(--surface-2); }
.btn:disabled { opacity: 0.5; cursor: not-allowed; }
.btn.primary { background: var(--accent); color: var(--accent-contrast); border-color: var(--accent); }

/* 사이드 메뉴 */
.sidebar { background: var(--surface); border-right: 1px solid var(--border); padding: var(--sp-3) 0 var(--sp-5); }
.nav-group { margin-bottom: var(--sp-3); }
.nav-group h2 {
  padding: var(--sp-2) var(--sp-4) var(--sp-1); font-size: var(--fs-sm);
  font-weight: 600; color: var(--text-muted);
}
.nav-link {
  display: flex; align-items: center; min-height: var(--touch); padding: 0 var(--sp-4);
  color: var(--text); text-decoration: none; border-left: 4px solid transparent;
}
.nav-link:hover { background: var(--surface-2); }
.nav-link[aria-current="page"] { background: var(--accent-soft); border-left-color: var(--accent); font-weight: 600; }

/* 하단 탭바(390px) */
.tabbar { background: var(--surface); border-top: 1px solid var(--border); }
.tabbar a, .tabbar button {
  flex: 1 1 0; min-width: 0; display: flex; align-items: center; justify-content: center;
  border: 0; background: transparent; color: var(--text); text-decoration: none; cursor: pointer;
  border-top: 3px solid transparent;
}
.tabbar [aria-current="page"], .tabbar [aria-expanded="true"] { border-top-color: var(--accent); color: var(--accent); font-weight: 600; }

/* 알림 영역, 배너 */
.notice:empty { display: none; }
.notice { padding: var(--sp-3) var(--sp-5) 0; }
@media (max-width: 767px) { .notice { padding: var(--sp-3) var(--sp-4) 0; } }

.banner {
  display: flex; gap: var(--sp-3); padding: var(--sp-3) var(--sp-4);
  border-radius: var(--radius); border: 1px solid currentColor;
}
.banner-icon { font-weight: 700; }
.banner-body { min-width: 0; }
.error-detail ul { padding-left: var(--sp-5); list-style: disc; }
.error-hint { margin-top: var(--sp-1); }

/* tone: 배지·배너·토스트 공통. 색 + 아이콘 문자 + 라벨 */
.tone-neutral { background: var(--tone-neutral-bg); color: var(--tone-neutral-fg); }
.tone-info    { background: var(--tone-info-bg);    color: var(--tone-info-fg); }
.tone-success { background: var(--tone-success-bg); color: var(--tone-success-fg); }
.tone-warn    { background: var(--tone-warn-bg);    color: var(--tone-warn-fg); }
.tone-danger  { background: var(--tone-danger-bg);  color: var(--tone-danger-fg); }

.badge {
  display: inline-flex; align-items: center; gap: var(--sp-1);
  padding: 1px var(--sp-2); border-radius: 999px; font-size: var(--fs-sm); white-space: nowrap;
}
.badge-icon { font-size: 0.75em; }

/* 토스트 */
.toast-region {
  position: fixed; z-index: 100; right: var(--sp-4); bottom: var(--sp-4);
  display: flex; flex-direction: column; gap: var(--sp-2); max-width: min(420px, calc(100vw - 32px));
}
@media (max-width: 767px) { .toast-region { bottom: calc(var(--tabbar-h) + var(--sp-3)); } }
.toast {
  display: flex; align-items: center; gap: var(--sp-3); padding: var(--sp-3) var(--sp-4);
  border-radius: var(--radius); box-shadow: var(--shadow); border: 1px solid currentColor;
}
.toast-text { flex: 1 1 auto; min-width: 0; }
.toast-close {
  min-width: var(--touch); min-height: var(--touch); border: 0; background: transparent;
  color: inherit; font-size: 1.25rem; cursor: pointer;
}

/* 페이지, 빈 상태 */
.page { display: grid; gap: var(--sp-4); max-width: 1200px; }
.page h1 { font-size: var(--fs-xl); line-height: 1.3; }
.page h2 { font-size: var(--fs-lg); }
.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: var(--sp-4); }
.stack { display: grid; gap: var(--sp-3); }
.row { display: flex; flex-wrap: wrap; gap: var(--sp-2); align-items: center; }
.muted { color: var(--text-muted); }
.empty-state {
  padding: var(--sp-5); text-align: center; background: var(--surface);
  border: 1px dashed var(--border); border-radius: var(--radius);
}
.empty-title { font-size: var(--fs-lg); font-weight: 600; }
.empty-desc { margin-top: var(--sp-2); color: var(--text-muted); }

.kv { display: grid; grid-template-columns: max-content minmax(0, 1fr); gap: var(--sp-1) var(--sp-4); }
.kv dt { color: var(--text-muted); }

/* 표 */
.table-wrap { overflow-x: auto; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); }
.data-table { width: 100%; border-collapse: collapse; }
.data-table caption { padding: var(--sp-2) var(--sp-3); text-align: left; font-weight: 600; }
.data-table th, .data-table td { padding: var(--sp-2) var(--sp-3); text-align: left; border-bottom: 1px solid var(--border); }
.data-table th { background: var(--surface-2); font-weight: 600; white-space: nowrap; }
.data-table .num { text-align: right; font-variant-numeric: tabular-nums; }
.data-table tr:last-child td { border-bottom: 0; }
.data-table tr.clickable { cursor: pointer; }
.data-table tr.clickable:hover { background: var(--surface-2); }
.pager { display: flex; align-items: center; justify-content: flex-end; gap: var(--sp-2); padding-top: var(--sp-2); }

/* M1: 페이지 머리, 하위 메뉴, 링크, 목록 */
.page-head { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: var(--sp-3); }
.subnav { display: flex; flex-wrap: wrap; gap: var(--sp-1); }
.subnav a {
  display: inline-flex; align-items: center; min-height: var(--touch); padding: 0 var(--sp-4);
  border: 1px solid var(--border); border-radius: var(--radius); background: var(--surface); color: var(--text); text-decoration: none;
}
.subnav a[aria-current="page"] { background: var(--accent); color: var(--accent-contrast); border-color: var(--accent); font-weight: 600; }
.back-link { display: inline-flex; align-items: center; min-height: var(--touch); }
.bullets { padding-left: var(--sp-5); list-style: disc; }
.btn.danger { background: var(--tone-danger-bg); color: var(--tone-danger-fg); border-color: var(--tone-danger-fg); }
a.btn { display: inline-flex; align-items: center; color: var(--text); text-decoration: none; }
.sort-btn { min-height: var(--touch); padding: 0; border: 0; background: transparent; font-weight: 600; cursor: pointer; white-space: nowrap; }

/* 폼 */
.form { display: grid; gap: var(--sp-4); }
.form-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(min(100%, 240px), 1fr)); gap: var(--sp-3) var(--sp-4); }
.field { display: grid; gap: var(--sp-1); align-content: start; min-width: 0; }
.field label { font-weight: 600; }
.field .req { color: var(--tone-danger-fg); }
.field input:not([type="checkbox"]), .field select, .field textarea {
  width: 100%; min-height: var(--touch); padding: var(--sp-2) var(--sp-3);
  border: 1px solid var(--border); border-radius: var(--radius); background: var(--surface); color: var(--text);
}
.field textarea { font-family: var(--font-mono); }
.field input[readonly] { background: var(--surface-2); }
.field-check label { display: flex; align-items: center; gap: var(--sp-2); min-height: var(--touch); font-weight: 400; }
.field-check input { width: 20px; height: 20px; }
.field [aria-invalid="true"] { border-color: var(--tone-danger-fg); border-width: 2px; }
.field-hint { color: var(--text-muted); font-size: var(--fs-sm); }
.field-error { color: var(--tone-danger-fg); font-size: var(--fs-sm); font-weight: 600; }
.field-error:empty, .form-banner:empty { display: none; }
.form-actions { display: flex; flex-wrap: wrap; gap: var(--sp-2); align-items: center; }
.filter-bar { display: flex; flex-wrap: wrap; align-items: end; gap: var(--sp-3); }
.filter-bar .field { min-width: 160px; }
.rows-editor { display: grid; gap: var(--sp-3); }
.row-card { display: grid; gap: var(--sp-3); margin: 0; padding: var(--sp-3) var(--sp-4); border: 1px solid var(--border); border-radius: var(--radius); background: var(--surface); }
.row-card legend { padding: 0 var(--sp-2); font-weight: 600; }

/* 탭 */
.tablist { display: flex; gap: var(--sp-1); border-bottom: 1px solid var(--border); overflow-x: auto; }
.tablist [role="tab"] {
  min-height: var(--touch); padding: 0 var(--sp-4); border: 0; border-bottom: 3px solid transparent;
  background: transparent; cursor: pointer; white-space: nowrap;
}
.tablist [role="tab"][aria-selected="true"] { border-bottom-color: var(--accent); color: var(--accent); font-weight: 600; }
.tablist [role="tab"]:disabled { opacity: 0.5; cursor: not-allowed; }
.tabpanel { padding-top: var(--sp-4); }

/* 대화상자 */
.dialog {
  width: min(560px, calc(100vw - 32px)); max-height: calc(100vh - 32px); overflow-y: auto; padding: var(--sp-4) var(--sp-5);
  border: 1px solid var(--border); border-radius: var(--radius); background: var(--surface); color: var(--text); box-shadow: var(--shadow);
}
.dialog::backdrop { background: rgba(10, 14, 22, 0.6); }
.dialog h2 { margin-bottom: var(--sp-3); font-size: var(--fs-lg); }

@media (max-width: 767px) {
  /* 표 → 카드: 열 이름은 data-label 로 표시 */
  .table-wrap { overflow-x: visible; border: 0; background: transparent; }
  .data-table, .data-table tbody, .data-table tr, .data-table td { display: block; width: 100%; }
  .data-table caption { display: block; }
  .data-table thead { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); }
  .data-table tr { margin-bottom: var(--sp-3); background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); }
  .data-table td { display: flex; justify-content: space-between; gap: var(--sp-3); text-align: right; border-bottom: 1px solid var(--border); }
  .data-table td::before { content: attr(data-label); color: var(--text-muted); text-align: left; flex: 0 0 38%; }
  .data-table td.num { text-align: right; }
  .data-table .empty-row td::before { content: none; }
  .kv { grid-template-columns: minmax(0, 1fr); }
}

/* 자동 학습 투어 */
.tour-hl { outline: 4px solid var(--focus); outline-offset: 3px; }
.dialog.tour { margin: auto auto var(--sp-3); max-width: min(32rem, calc(100vw - 2rem)); }
.dialog.tour::backdrop { background: transparent; }
.dialog.tour, .dialog.tour-end { background: var(--tour-bg); color: var(--tour-fg); border-color: var(--tour-fg); }
.dialog.tour .btn, .dialog.tour-end .btn { background: var(--tour-btn); color: var(--tour-fg); border-color: var(--tour-fg); }
.dialog.tour .btn:hover, .dialog.tour-end .btn:hover { filter: brightness(0.93); }
.dialog.tour :focus-visible, .dialog.tour-end :focus-visible { outline-color: var(--tour-fg); }
.dialog.tour .muted { color: var(--tour-fg); }
/* 투어 중에는 대화상자 높이만큼 화면 아래 여백을 둬 대상이 가려지지 않게 한다 */
html.tour-on .view { padding-bottom: calc(var(--tour-pad, 0px) + var(--tabbar-h) + var(--sp-5)); }

/* LOT 라벨(인쇄용 텍스트) */
.lot-label { margin: 0; padding: var(--sp-3); border: 2px dashed var(--border); border-radius: var(--radius); white-space: pre-wrap; font-family: ui-monospace, Consolas, monospace; font-size: var(--fs-lg); background: var(--surface); color: var(--text); overflow-wrap: anywhere; }
@media print {
  body.printing-label > *:not(dialog) { display: none !important; }
  body.printing-label dialog { position: static; box-shadow: none; border: 0; width: auto; max-height: none; }
  body.printing-label dialog button { display: none; }
  body.printing-label .lot-label { color: #000; background: #fff; border-color: #000; }
}

/* CUM 표가 넓어도 페이지가 아니라 .table-wrap 안에서만 가로 스크롤한다 */
.cum-host, .cum-host > * { min-width: 0; }
