/* 기본값 = 화이트(라이트) 테마 */
:root, :root[data-theme="light"] {
  --charcoal: #ffffff;
  --charcoal-2: #f4f5f6;
  --line: #e3e5e8;
  --text: #1c1f22;
  --text-dim: #6b7280;
  --safety-red: #e0332f;
  --safety-red-dim: #f6cfce;
  --ok: #2f8a52;
  --ok-hover: #1f6b3c;
  --warn: #a3720c;
  --gold: #a17e04;
  --btn-hover-bg: #eceef0;
}

/* 다크 테마 — 토글 버튼으로 전환 시 켜짐 */
:root[data-theme="dark"] {
  --charcoal: #1c1f22;
  --charcoal-2: #26292d;
  --line: #383c41;
  --text: #e9ebee;
  --text-dim: #9aa0a8;
  --safety-red: #e0332f;
  --safety-red-dim: #7a1f1d;
  --ok: #4caf6a;
  --ok-hover: #6ecb8a;
  --warn: #d9a441;
  --gold: #f5c518;
  --btn-hover-bg: #303439;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--charcoal);
  color: var(--text);
  font-family: "Pretendard", -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  transition: background 0.15s ease, color 0.15s ease;
}

.app {
  max-width: 1180px;
  margin: 0 auto;
  padding: 28px 32px 60px;
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding-bottom: 18px;
  border-bottom: 1px solid var(--line);
  margin-bottom: 16px;
}

.brand { display: flex; align-items: center; gap: 12px; }

.brand-mark {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 40px;
  height: 40px;
  border-radius: 6px;
  background: var(--safety-red);
  color: #fff;
  font-weight: 800;
  letter-spacing: -0.5px;
  font-size: 15px;
}

.brand-text h1 { font-size: 21px; margin: 0; letter-spacing: -0.3px; }
.brand-text .sub { margin: 3px 0 0; font-size: 13px; color: var(--text-dim); }

.btn {
  border: 1px solid var(--line);
  background: var(--charcoal-2);
  color: var(--text);
  padding: 9px 14px;
  border-radius: 6px;
  font-size: 13px;
  cursor: pointer;
  font-weight: 600;
  transition: background 0.15s, border-color 0.15s;
}
.btn:hover { background: var(--btn-hover-bg); }
a.btn { text-decoration: none; display: inline-block; }
.btn-outline { background: transparent; }
.btn-primary {
  background: var(--safety-red);
  border-color: var(--safety-red);
  color: #fff;
}
.btn-primary:hover { background: #c72c28; }

.status-bar {
  font-size: 13.5px;
  color: var(--text-dim);
  padding: 10px 14px;
  background: var(--charcoal-2);
  border-radius: 6px;
  margin-bottom: 16px;
}
.status-bar.error { color: var(--safety-red); }

.search-row { margin-bottom: 12px; }
.search-row input {
  width: 100%;
  padding: 13px 16px;
  border-radius: 8px;
  border: 1px solid var(--line);
  background: var(--charcoal-2);
  color: var(--text);
  font-size: 16px;
}
.search-row input::placeholder { color: var(--text-dim); }

.live-lookup {
  display: flex;
  gap: 8px;
  margin-bottom: 10px;
  padding: 14px;
  border: 1px dashed var(--line);
  border-radius: 8px;
}
.live-lookup .autocomplete { flex: 1; }
.live-lookup input {
  width: 100%;
  height: 46px;
  padding: 0 14px;
  border-radius: 8px;
  border: 1px solid var(--line);
  background: var(--charcoal);
  color: var(--text);
  font-size: 15px;
  box-sizing: border-box;
}
.live-lookup .btn-primary {
  height: 46px;
  padding: 0 20px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  box-sizing: border-box;
}

.live-result {
  margin-bottom: 18px;
  padding: 20px 22px;
  border-radius: 10px;
  background: var(--charcoal-2);
  border-left: 3px solid var(--ok);
}
.live-result.error { border-left-color: var(--safety-red); }
.hidden { display: none; }

.live-card-head {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 16px;
  margin-bottom: 18px;
}
.live-card-name { font-size: 19px; font-weight: 700; }
.live-card-code { margin-top: 4px; font-size: 13.5px; color: var(--text-dim); }
.live-card-right {
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  gap: 8px;
}
.live-tag.live { color: var(--ok); font-weight: 700; }
.live-tag.cached { color: var(--text-dim); }
.live-refresh-btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 7px 12px;
  font-size: 13px;
  white-space: nowrap;
}
.live-refresh-btn svg { flex-shrink: 0; }
.live-refresh-btn.spinning svg { animation: row-btn-spin 0.7s linear infinite; }
.live-refresh-btn:disabled { opacity: 0.6; cursor: default; }

.live-card-time { font-size: 12.5px; color: var(--text-dim); white-space: nowrap; }

.live-card-stats-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 16px;
  margin-bottom: 18px;
}
.live-card-stat-label { font-size: 13px; color: var(--text-dim); margin-bottom: 4px; }
.live-card-stat-value { font-size: 28px; font-weight: 800; color: var(--text); line-height: 1.1; }
.live-card-stat-unit { font-size: 14px; font-weight: 600; color: var(--text-dim); margin-left: 2px; }

.live-card-meta {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(140px, 1fr));
  gap: 14px;
  padding-top: 16px;
  border-top: 1px solid var(--line);
}
.live-card-meta-item .label { font-size: 12px; color: var(--text-dim); margin-bottom: 3px; }
.live-card-meta-item .value { font-size: 14.5px; font-weight: 600; }

.margin-calc {
  margin-top: 16px;
  padding: 14px 16px;
  background: var(--charcoal);
  border: 1px solid var(--line);
  border-radius: 8px;
}
.margin-calc-title { font-size: 13.5px; font-weight: 700; margin-bottom: 10px; }
.margin-calc-base { font-weight: 400; color: var(--text-dim); font-size: 12px; }
.margin-calc-empty { font-size: 12.5px; color: var(--text-dim); margin: 0; }

.margin-calc-row {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 8px;
}
.margin-calc-row:last-child { margin-bottom: 0; }
.margin-calc-row input {
  width: 110px;
  padding: 8px 10px;
  border-radius: 6px;
  border: 1px solid var(--line);
  background: var(--charcoal-2);
  color: var(--text);
  font-size: 14px;
  text-align: right;
}
.margin-unit { font-size: 13px; color: var(--text-dim); width: 14px; }
.margin-result {
  font-size: 14.5px;
  font-weight: 700;
  color: var(--ok);
  margin-left: auto; /* 입력창 옆 빈 공간을 채우며 오른쪽 끝으로 붙음 */
  text-align: right;
  white-space: nowrap;
}
.margin-result.copyable {
  cursor: pointer;
  text-decoration: underline dotted;
  text-underline-offset: 3px;
}
.margin-result.copyable:hover { color: var(--ok-hover); }
.margin-result.copyable::after {
  content: " 📋";
  font-size: 11px;
  opacity: 0.7;
}

.raw-details { margin-top: 16px; }
.raw-details summary {
  cursor: pointer;
  font-size: 12.5px;
  color: var(--text-dim);
}
.raw-details summary:hover { color: var(--text); }
.raw-json {
  margin-top: 10px;
  padding: 12px 14px;
  background: var(--charcoal);
  border: 1px solid var(--line);
  border-radius: 6px;
  font-size: 12px;
  line-height: 1.6;
  overflow-x: auto;
  white-space: pre-wrap;
  word-break: break-all;
  color: var(--text-dim);
}

.table-wrap {
  border: 1px solid var(--line);
  border-radius: 8px;
  overflow: hidden;
}

table { width: 100%; border-collapse: collapse; font-size: 15px; table-layout: fixed; }

/* 헤더 텍스트는 절대 줄바꿈되지 않도록 (좁은 칸에서 "매입/가" 처럼 쪼개지는 문제 방지) */
thead th {
  text-align: left;
  padding: 13px 12px;
  background: var(--charcoal-2);
  color: var(--text-dim);
  font-weight: 600;
  font-size: 13px;
  border-bottom: 1px solid var(--line);
  white-space: nowrap;
}
th.num, td.num { text-align: right; }
th.star-col, td.star-col { width: 36px; padding-left: 14px; padding-right: 4px; }

th.sortable { cursor: pointer; user-select: none; }
th.sortable:hover { color: var(--text); }
.sort-arrow { font-size: 10px; color: var(--safety-red); display: inline-block; width: 8px; }

tbody td {
  padding: 13px 12px;
  border-bottom: 1px solid var(--line);
  overflow: hidden;
  text-overflow: ellipsis;
}
tbody tr:last-child td { border-bottom: none; }
tbody tr:hover { background: rgba(255,255,255,0.02); }

.category-cell { color: var(--text-dim); font-size: 13.5px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

.qty-low { color: var(--warn); font-weight: 700; }
.qty-zero { color: var(--safety-red); font-weight: 700; }

.empty { text-align: center; color: var(--text-dim); padding: 30px 0; }

.star-btn {
  background: transparent;
  border: none;
  cursor: pointer;
  font-size: 18px;
  line-height: 1;
  padding: 4px;
  color: var(--text-dim);
}
.star-btn.starred { color: var(--gold); }
.star-btn:hover { color: var(--gold); }
.live-star-btn { font-size: 20px; vertical-align: -3px; margin-right: 2px; }

.row-live-btn {
  width: 34px;
  height: 34px;
  padding: 0;
  border-radius: 6px;
  border: 1px solid var(--line);
  background: transparent;
  color: var(--text-dim);
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}
.row-live-btn:hover { border-color: var(--safety-red); color: var(--text); }
.row-live-btn.spinning svg { animation: row-btn-spin 0.7s linear infinite; }
.row-live-btn:disabled { opacity: 0.6; cursor: default; }
@keyframes row-btn-spin { to { transform: rotate(360deg); } }

.header-actions { display: flex; gap: 8px; flex-wrap: wrap; }

.muted { color: var(--text-dim); }

.badge-unverified {
  display: inline-block;
  font-size: 12.5px;
  font-weight: 700;
  color: var(--warn);
  background: rgba(217, 164, 65, 0.12);
  border: 1px solid var(--warn);
  padding: 4px 9px;
  border-radius: 5px;
}

.badge-priority {
  display: inline-block;
  font-size: 11px;
  font-weight: 700;
  color: var(--safety-red);
  background: rgba(224, 51, 47, 0.12);
  border: 1px solid var(--safety-red);
  padding: 1px 7px;
  border-radius: 4px;
  margin-left: 5px;
  vertical-align: middle;
}

.form-card {
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 16px;
  margin-bottom: 14px;
  background: var(--charcoal-2);
}
.form-title { font-size: 14px; margin: 0 0 10px; font-weight: 700; }
.hint { font-size: 12.5px; color: var(--text-dim); margin: 0 0 12px; line-height: 1.5; }

.form-row { margin-bottom: 10px; }
.form-row label { display: block; font-size: 12.5px; color: var(--text-dim); margin-bottom: 5px; }
.form-row input {
  width: 100%;
  padding: 9px 12px;
  border-radius: 6px;
  border: 1px solid var(--line);
  background: var(--charcoal);
  color: var(--text);
  font-size: 13.5px;
}

#codeInput { width: 100%; padding: 10px 12px; border-radius: 6px; border: 1px solid var(--line); background: var(--charcoal); color: var(--text); font-size: 14px; }

.autocomplete { position: relative; }

.autocomplete-dropdown {
  position: absolute;
  top: calc(100% + 4px);
  left: 0;
  right: 0;
  max-height: 280px;
  overflow-y: auto;
  background: var(--charcoal);
  border: 1px solid var(--line);
  border-radius: 6px;
  z-index: 20;
  box-shadow: 0 8px 20px rgba(0,0,0,0.35);
}

.autocomplete-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
  padding: 9px 12px;
  cursor: pointer;
  font-size: 13.5px;
  border-bottom: 1px solid var(--line);
}
.autocomplete-item:last-child { border-bottom: none; }
.autocomplete-item:hover { background: var(--charcoal-2); }
.autocomplete-item .ac-name { color: var(--text); }
.autocomplete-item .ac-code { color: var(--text-dim); font-size: 12px; white-space: nowrap; }

.form-row select {
  width: 100%;
  padding: 9px 12px;
  border-radius: 6px;
  border: 1px solid var(--line);
  background: var(--charcoal);
  color: var(--text);
  font-size: 13.5px;
}

.selected-info {
  margin-top: 10px;
  padding: 10px 12px;
  border-radius: 6px;
  background: var(--charcoal);
  font-size: 13px;
  line-height: 1.6;
}

.offset-preview {
  margin: 10px 0;
  padding: 10px 12px;
  border-radius: 6px;
  background: var(--charcoal);
  border-left: 3px solid var(--ok);
  font-size: 13.5px;
  line-height: 1.6;
}
.offset-preview.negative { border-left-color: var(--safety-red); }

.toast {
  position: fixed;
  left: 50%;
  bottom: 24px;
  transform: translateX(-50%);
  padding: 12px 20px;
  background: var(--ok);
  color: #0d2b18;
  font-weight: 700;
  border-radius: 8px;
  text-align: center;
  box-shadow: 0 8px 24px rgba(0,0,0,0.35);
  z-index: 100;
  white-space: nowrap;
}
.toast.error { background: var(--safety-red); color: #fff; }

.admin-lock-btn {
  border: 1px solid var(--line);
  background: var(--charcoal-2);
  color: var(--text-dim);
  width: 38px;
  height: 38px;
  border-radius: 6px;
  cursor: pointer;
  font-size: 15px;
  flex-shrink: 0;
}
.admin-lock-btn:hover { border-color: var(--safety-red); color: var(--text); }

.lock-screen {
  max-width: 380px;
  margin: 80px auto;
}
.lock-card {
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 28px 24px;
  background: var(--charcoal-2);
  text-align: center;
}
.lock-title { font-size: 20px; font-weight: 700; margin-bottom: 10px; }
.lock-card .hint { text-align: left; }
.lock-card input {
  width: 100%;
  padding: 12px 14px;
  border-radius: 8px;
  border: 1px solid var(--line);
  background: var(--charcoal);
  color: var(--text);
  font-size: 16px;
  text-align: center;
  letter-spacing: 4px;
}
.lock-error { color: var(--safety-red); font-size: 13px; margin-top: 10px; }

.layout {
  display: grid;
  grid-template-columns: 1fr 220px;
  grid-template-areas:
    "search   sidebar"
    "lookup   sidebar"
    "result   sidebar"
    "table    sidebar";
  column-gap: 24px;
  row-gap: 0;
  align-items: start;
}
.layout > .search-row { grid-area: search; }
.layout > .live-lookup { grid-area: lookup; }
.layout > #liveResult { grid-area: result; }
.layout > .table-wrap { grid-area: table; }

.category-col {
  grid-area: sidebar;
  width: 220px;
  flex-shrink: 0;
  position: sticky;
  top: 20px;
}
.category-title {
  font-size: 13px;
  font-weight: 700;
  color: var(--text-dim);
  margin-bottom: 10px;
  padding-left: 2px;
}
.category-list {
  display: flex;
  flex-direction: column;
  gap: 4px;
  max-height: calc(100vh - 80px);
  overflow-y: auto;
}
.category-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 8px;
  padding: 9px 12px;
  border-radius: 6px;
  font-size: 13.5px;
  color: var(--text-dim);
  cursor: pointer;
  border: 1px solid transparent;
}
.category-item:hover { background: var(--charcoal-2); color: var(--text); }
.category-item.active {
  background: var(--charcoal-2);
  border-color: var(--safety-red);
  color: var(--text);
  font-weight: 700;
}
.category-item .cat-count { font-size: 11.5px; color: var(--text-dim); }
.category-item.active .cat-count { color: var(--safety-red); }

.category-item.favorite-cat { color: var(--gold); }
.category-item.favorite-cat:hover { color: var(--gold); background: rgba(161, 126, 4, 0.08); }
.category-item.favorite-cat.active { border-color: var(--gold); background: rgba(161, 126, 4, 0.1); }
.category-item.favorite-cat.active .cat-count { color: var(--gold); }
.category-item.favorite-cat .cat-count { color: var(--gold); }

@media (max-width: 900px) {
  .app { padding: 16px 14px 40px; }

  /* 1. 상단 버튼 — 가로로 넘치지 않고 줄바꿈되도록 */
  .topbar { flex-direction: column; align-items: flex-start; gap: 12px; }
  .header-actions { width: 100%; flex-wrap: wrap; gap: 8px; }
  .header-actions .btn { flex: 1 1 auto; text-align: center; min-height: 44px; }

  /* 2. 카테고리 — 검색창 바로 아래로, 가로 한 줄, 손가락으로 옆으로 드래그해서 넘기기
     (PC는 오른쪽 사이드바지만, 모바일은 한 열로 바뀌면서 "검색 → 카테고리 → 실시간조회 → 결과 → 표" 순서가 됨) */
  .layout { display: flex; flex-direction: column; align-items: stretch; }
  .layout > * { margin-bottom: 12px; }
  .layout > .search-row { order: 1; }
  .layout > .category-col { order: 2; }
  .layout > .live-lookup { order: 3; }
  .layout > #liveResult { order: 4; }
  .layout > .table-wrap { order: 5; margin-bottom: 0; }

  .category-col { width: 100%; position: static; }
  .category-list {
    flex-direction: row;
    flex-wrap: nowrap;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    max-height: none;
    padding-bottom: 6px;
    scrollbar-width: none;
  }
  .category-list::-webkit-scrollbar { display: none; }
  .category-item { flex-shrink: 0; white-space: nowrap; min-height: 40px; }

  /* 입력창 글씨는 16px 이상 유지 — 이보다 작으면 아이폰에서 탭할 때 화면이 확 확대되는 문제가 있음 */
  .search-row input,
  .live-lookup input,
  .margin-calc-row input,
  .form-row input,
  .form-row select {
    font-size: 16px !important;
  }

  /* 실시간 조회 버튼 등 터치 영역을 손가락 크기에 맞게 키움 */
  .btn, .row-live-btn { min-height: 44px; }

  /* 좌우로 넘치기 쉬운 블록들은 줄바꿈되도록 */
  .live-card-head { flex-wrap: wrap; }
  .live-card-meta { grid-template-columns: 1fr; }
  .margin-calc-row input { width: 90px; } /* 좁은 화면에서 결과 표시할 공간을 더 확보 */

  /* 3·4. 표를 좌우로 넓게 펼치는 대신, 품목 하나 = 카드 하나로 세로로 쌓아서 보여줌
     (칸이 좁아져서 값끼리 겹치거나 헷갈리는 문제를, 구분선이 아니라 아예 "카드" 단위로 나눠서 해결) */
  .table-wrap { border: none; overflow: visible; }
  table, thead, tbody, tr { display: block; width: 100%; }
  thead { display: none; } /* 각 칸에 라벨이 직접 붙으니 표 머리글은 필요 없음 */

  tbody tr {
    margin-bottom: 12px;
    border: 1px solid var(--line);
    border-radius: 10px;
    padding: 4px 14px;
    background: var(--charcoal-2);
  }
  tbody tr:last-child { margin-bottom: 0; }

  tbody td {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 12px;
    padding: 11px 0;
    border-bottom: 1px solid var(--line);
    text-align: right;
    font-size: 15px;
  }
  tbody tr td:not(:last-child) { border-bottom: 1px solid var(--line); }
  tbody tr td:last-child { border-bottom: none; padding-top: 12px; }

  /* 라벨(품목코드/품목명 등)을 값 왼쪽에 붙여서, 어떤 값인지 절대 안 헷갈리게 함 */
  tbody td[data-label]::before {
    content: attr(data-label);
    font-size: 12px;
    font-weight: 700;
    color: var(--text-dim);
    text-align: left;
    flex-shrink: 0;
  }
  tbody td[data-label=""]::before { content: none; }

  tbody td[data-label] { text-align: right; }
  tbody td[data-label="품목명"] { text-align: right; line-height: 1.4; }

  /* 별표 칸은 카드 맨 위, 왼쪽에 크게 보이도록 */
  tbody td.star-col { justify-content: flex-start; border-bottom: 1px solid var(--line); padding: 10px 0; }
  tbody td.star-col .star-btn { font-size: 22px; padding: 2px; }

  /* "지금 확인" 버튼은 카드 맨 아래, 손가락으로 누르기 쉽게 꽉 채움 */
  tbody tr td:last-child { justify-content: center; }
  tbody tr td:last-child .row-live-btn { width: 100%; padding: 12px; font-size: 14px; }
}
