*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --bg: #f5f6f8;
  --surface: #ffffff;
  --border: #e2e5ea;
  --text: #1a1d23;
  --text-secondary: #5f6775;
  --primary: #2563eb;
  --primary-hover: #1d4ed8;
  --primary-light: #eff4ff;
  --success: #059669;
  --success-light: #ecfdf5;
  --danger: #dc2626;
  --danger-light: #fef2f2;
  --radius: 8px;
  --shadow: 0 1px 3px rgba(0,0,0,.08), 0 1px 2px rgba(0,0,0,.04);
  --shadow-lg: 0 4px 12px rgba(0,0,0,.1);
}

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
  background: var(--bg); color: var(--text); line-height: 1.5; min-height: 100vh;
}

/* ---- Login ---- */
.login-wrapper { display: flex; align-items: center; justify-content: center; min-height: 100vh; padding: 1rem; }
.login-card { background: var(--surface); border-radius: 12px; box-shadow: var(--shadow-lg); padding: 2.5rem 2rem; width: 100%; max-width: 380px; }
.login-card h1 { font-size: 1.35rem; font-weight: 600; text-align: center; margin-bottom: 1.5rem; }

/* ---- Forms ---- */
label { display: block; font-size: .82rem; font-weight: 500; color: var(--text-secondary); margin-bottom: .3rem; }
input[type="text"], input[type="password"], input[type="number"], input[type="date"], select {
  width: 100%; padding: .6rem .75rem; border: 1px solid var(--border); border-radius: var(--radius);
  font-size: .9rem; color: var(--text); background: var(--surface); transition: border-color .15s;
}
input:focus, select:focus { outline: none; border-color: var(--primary); box-shadow: 0 0 0 3px rgba(37,99,235,.12); }
.form-group { margin-bottom: 1rem; }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: .4rem;
  padding: .6rem 1.2rem; border: none; border-radius: var(--radius); font-size: .88rem;
  font-weight: 500; cursor: pointer; transition: background .15s, box-shadow .15s;
}
.btn-primary { background: var(--primary); color: #fff; }
.btn-primary:hover { background: var(--primary-hover); }
.btn-primary:disabled { opacity: .5; cursor: default; }
.btn-secondary { background: var(--bg); color: var(--text); border: 1px solid var(--border); }
.btn-secondary:hover { background: var(--border); }
.btn-danger { background: var(--danger); color: #fff; }
.btn-sm { padding: .4rem .8rem; font-size: .8rem; }
.btn-block { width: 100%; }
.error-msg { color: var(--danger); font-size: .82rem; text-align: center; margin-top: .75rem; min-height: 1.2rem; }
.required-star { color: var(--danger); }

/* ---- Top bar ---- */
.topbar {
  display: flex; align-items: center; justify-content: space-between; background: var(--surface);
  border-bottom: 1px solid var(--border); padding: 0 1.5rem; height: 56px; position: sticky; top: 0; z-index: 100;
}
.topbar h1 { font-size: 1.05rem; font-weight: 600; }
.topbar-right { display: flex; align-items: center; gap: .75rem; font-size: .88rem; color: var(--text-secondary); }

/* ---- Home menu ---- */
.home-menu {
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  min-height: calc(100vh - 56px); padding: 2rem;
}
.home-greeting { font-size: 1.4rem; font-weight: 600; margin-bottom: .5rem; }
.home-subtitle { color: var(--text-secondary); margin-bottom: 2rem; }
.home-cards { display: flex; gap: 1.5rem; margin-bottom: 2rem; }
.home-card {
  width: 200px; height: 160px; background: var(--surface); border: 2px solid var(--border);
  border-radius: 12px; display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: .75rem; cursor: pointer; transition: border-color .2s, box-shadow .2s, transform .15s;
  text-decoration: none; color: var(--text);
}
.home-card:hover { border-color: var(--primary); box-shadow: var(--shadow-lg); transform: translateY(-2px); }
.home-card-icon { font-size: 2.2rem; }
.home-card-label { font-size: 1rem; font-weight: 600; text-transform: uppercase; letter-spacing: .04em; }
.home-link { color: var(--primary); font-size: .9rem; font-weight: 500; cursor: pointer; text-decoration: none; }
.home-link:hover { text-decoration: underline; }

/* ---- Tabs ---- */
.tabs { display: flex; gap: 0; border-bottom: 1px solid var(--border); background: var(--surface); padding: 0 1.5rem; overflow-x: auto; }
.tab {
  padding: .75rem 1.2rem; font-size: .88rem; font-weight: 500; color: var(--text-secondary);
  border: none; background: none; cursor: pointer; border-bottom: 2px solid transparent; white-space: nowrap;
}
.tab:hover { color: var(--text); }
.tab.active { color: var(--primary); border-bottom-color: var(--primary); }
.tab-content { display: none; padding: 1.5rem; }
.tab-content.active { display: block; }

/* ---- Confirmation dialog ---- */
.confirm-dialog {
  max-width: 440px; margin: 3rem auto; background: var(--surface); border-radius: 12px;
  box-shadow: var(--shadow-lg); padding: 2rem;
}
.confirm-dialog h2 { font-size: 1.2rem; font-weight: 600; margin-bottom: 1.5rem; }
.confirm-actions { display: flex; gap: .75rem; justify-content: flex-end; margin-top: 1.5rem; }

/* ---- Document header bar ---- */
.doc-header {
  background: var(--surface); border-bottom: 1px solid var(--border); padding: 1rem 1.5rem;
  display: flex; align-items: center; justify-content: space-between;
}
.doc-header-info { font-size: .92rem; }
.doc-header-info strong { font-weight: 600; }
.doc-header-route { color: var(--text-secondary); font-size: .85rem; margin-top: .2rem; }

/* ---- Batch layout ---- */
.batch-layout { display: grid; grid-template-columns: 200px 1fr; min-height: calc(100vh - 56px - 60px); }

/* ---- Category sidebar ---- */
.cat-sidebar {
  background: var(--surface); border-right: 1px solid var(--border); padding: .75rem 0; overflow-y: auto;
}
.cat-sidebar-title {
  font-size: .72rem; text-transform: uppercase; letter-spacing: .04em; color: var(--text-secondary);
  font-weight: 600; padding: .5rem 1rem; margin-bottom: .25rem;
}
.cat-item {
  display: block; width: 100%; padding: .5rem 1rem; text-align: left; font-size: .84rem;
  border: none; background: none; cursor: pointer; color: var(--text); transition: background .1s;
  border-left: 3px solid transparent;
}
.cat-item:hover { background: var(--bg); }
.cat-item.active { background: var(--primary-light); color: var(--primary); border-left-color: var(--primary); font-weight: 500; }

/* ---- Batch main area ---- */
.batch-main { padding: 1rem 1.5rem; overflow-y: auto; }

/* ---- Search ---- */
.search-row { display: flex; gap: .75rem; flex-wrap: wrap; margin-bottom: 1rem; }
.search-row .search-wrapper { flex: 1; min-width: 200px; margin-bottom: 0; }
.search-wrapper { position: relative; max-width: 560px; margin-bottom: 1rem; }
.search-input {
  width: 100%; padding: .6rem 1rem .6rem 2.2rem; border: 1px solid var(--border);
  border-radius: var(--radius); font-size: .88rem;
}
.search-input-sku { padding-left: 2.8rem; }
.search-icon { position: absolute; left: .7rem; top: 50%; transform: translateY(-50%); color: var(--text-secondary); font-size: .85rem; pointer-events: none; }
.search-results {
  position: absolute; top: 100%; left: 0; right: 0; background: var(--surface);
  border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow-lg);
  max-height: 300px; overflow-y: auto; z-index: 50; display: none;
}
.search-results.open { display: block; }
.search-item { padding: .55rem 1rem; cursor: pointer; border-bottom: 1px solid var(--border); font-size: .84rem; }
.search-item:last-child { border-bottom: none; }
.search-item:hover { background: var(--primary-light); }
.search-item-main { font-weight: 500; }
.search-item-meta { color: var(--text-secondary); font-size: .76rem; margin-top: .1rem; }

/* ---- Item list (category browse) ---- */
.item-list { display: flex; flex-direction: column; gap: .25rem; }
.item-list-row {
  display: flex; justify-content: space-between; align-items: center; padding: .5rem .75rem;
  background: var(--surface); border: 1px solid var(--border); border-radius: 6px; font-size: .84rem;
  cursor: pointer; transition: border-color .15s;
}
.item-list-row:hover { border-color: var(--primary); }
.item-list-name { font-weight: 500; }
.item-list-meta { color: var(--text-secondary); font-size: .78rem; }

/* ---- Batch table ---- */
.batch-table-wrap { margin-top: 1.5rem; }
.batch-table-title { font-size: .95rem; font-weight: 600; margin-bottom: .75rem; }
.batch-table { width: 100%; border-collapse: collapse; font-size: .82rem; }
.batch-table th {
  text-align: left; padding: .5rem .5rem; background: var(--bg); font-weight: 600;
  font-size: .75rem; text-transform: uppercase; letter-spacing: .03em; color: var(--text-secondary);
  border-bottom: 1px solid var(--border); white-space: nowrap;
}
.batch-table td { padding: .35rem .35rem; border-bottom: 1px solid var(--border); vertical-align: middle; }
.batch-table input, .batch-table select {
  padding: .35rem .5rem; font-size: .82rem; border: 1px solid var(--border);
  border-radius: 4px; width: 100%; min-width: 80px;
}
.batch-table input:focus { border-color: var(--primary); outline: none; }
.batch-table .col-qty { width: 90px; }
.batch-table .col-remove { width: 36px; text-align: center; }
.batch-table .remove-btn {
  background: none; border: none; cursor: pointer; color: var(--text-secondary);
  font-size: 1rem; padding: .2rem .4rem; border-radius: 4px;
}
.batch-table .remove-btn:hover { background: var(--danger-light); color: var(--danger); }
.add-row-btn {
  margin-top: .5rem; padding: .4rem .8rem; font-size: .82rem; color: var(--primary);
  background: none; border: 1px dashed var(--primary); border-radius: var(--radius);
  cursor: pointer; width: 100%;
}
.add-row-btn:hover { background: var(--primary-light); }
.batch-actions { display: flex; gap: .75rem; justify-content: flex-end; margin-top: 1.5rem; padding-top: 1rem; border-top: 1px solid var(--border); }

/* ---- Combobox ---- */
.combo-wrapper { position: relative; }
.combo-dropdown {
  position: absolute; top: 100%; left: 0; right: 0; background: var(--surface);
  border: 1px solid var(--border); border-radius: 4px; box-shadow: var(--shadow-lg);
  max-height: 160px; overflow-y: auto; z-index: 40; display: none; font-size: .8rem;
}
.combo-dropdown.open { display: block; }
.combo-option { padding: .35rem .5rem; cursor: pointer; }
.combo-option:hover { background: var(--primary-light); }

/* ---- Cards ---- */
.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 1.25rem; box-shadow: var(--shadow); }
.card + .card { margin-top: 1rem; }
.card-title { font-size: .95rem; font-weight: 600; margin-bottom: .75rem; }
.item-detail-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(180px, 1fr)); gap: .6rem; margin-bottom: 1rem; }
.item-detail-grid dt { font-size: .75rem; color: var(--text-secondary); text-transform: uppercase; letter-spacing: .03em; }
.item-detail-grid dd { font-size: .9rem; font-weight: 500; margin-bottom: .4rem; }

/* ---- Tables ---- */
.table-wrapper { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; font-size: .84rem; }
th { text-align: left; padding: .6rem .75rem; background: var(--bg); font-weight: 600; font-size: .78rem; text-transform: uppercase; letter-spacing: .03em; color: var(--text-secondary); border-bottom: 1px solid var(--border); white-space: nowrap; }
td { padding: .55rem .75rem; border-bottom: 1px solid var(--border); }
tr:hover td { background: #f9fafb; }
.badge { display: inline-block; padding: .15rem .5rem; border-radius: 4px; font-size: .75rem; font-weight: 500; }
.badge-revision { background: #ede9fe; color: #6d28d9; }
.badge-transfer { background: #e0f2fe; color: #0369a1; }
.badge-positive { color: var(--success); }
.badge-negative { color: var(--danger); }

/* ---- Pagination ---- */
.pagination { display: flex; align-items: center; justify-content: center; gap: .5rem; padding: 1rem; font-size: .85rem; }
.pagination button { padding: .35rem .7rem; border: 1px solid var(--border); border-radius: var(--radius); background: var(--surface); cursor: pointer; font-size: .82rem; }
.pagination button:disabled { opacity: .4; cursor: default; }

/* ---- Toast ---- */
.toast-container { position: fixed; top: 1rem; right: 1rem; z-index: 1000; display: flex; flex-direction: column; gap: .5rem; }
.toast { padding: .7rem 1.2rem; border-radius: var(--radius); font-size: .85rem; font-weight: 500; box-shadow: var(--shadow-lg); animation: slideIn .25s ease; max-width: 400px; }
.toast-success { background: var(--success); color: #fff; }
.toast-error { background: var(--danger); color: #fff; }
@keyframes slideIn { from { transform: translateX(100%); opacity: 0; } to { transform: translateX(0); opacity: 1; } }

/* ---- Admin layout ---- */
.admin-layout { display: grid; grid-template-columns: 280px 1fr; min-height: calc(100vh - 56px); }
.sidebar { background: var(--surface); border-right: 1px solid var(--border); padding: 1rem; overflow-y: auto; }
.sidebar-title { font-size: .78rem; text-transform: uppercase; letter-spacing: .04em; color: var(--text-secondary); font-weight: 600; margin-bottom: .75rem; }
.user-card { padding: .85rem; border: 1px solid var(--border); border-radius: var(--radius); cursor: pointer; margin-bottom: .5rem; transition: border-color .15s; }
.user-card:hover { border-color: var(--primary); }
.user-card.active { border-color: var(--primary); background: var(--primary-light); }
.user-card-name { font-weight: 600; font-size: .9rem; margin-bottom: .35rem; }
.user-card-meta { font-size: .78rem; color: var(--text-secondary); line-height: 1.5; }
.main-panel { padding: 0; overflow-y: auto; }

/* ---- Filters ---- */
.filters { display: flex; flex-wrap: wrap; align-items: flex-end; gap: .75rem; padding: 1rem 1.5rem; background: var(--surface); border-bottom: 1px solid var(--border); }
.filters .form-group { margin-bottom: 0; min-width: 140px; }
.filters input, .filters select { padding: .45rem .6rem; font-size: .82rem; }

.history-header { display: flex; justify-content: space-between; align-items: center; margin-bottom: 1rem; }
.history-header h2 { font-size: 1.1rem; font-weight: 600; }

/* ---- Responsive ---- */
@media (max-width: 768px) {
  .admin-layout { grid-template-columns: 1fr; }
  .sidebar { border-right: none; border-bottom: 1px solid var(--border); display: flex; gap: .5rem; overflow-x: auto; padding: .75rem; }
  .user-card { min-width: 200px; margin-bottom: 0; }
  .batch-layout { grid-template-columns: 1fr; }
  .cat-sidebar { border-right: none; border-bottom: 1px solid var(--border); display: flex; gap: 0; overflow-x: auto; padding: .5rem 0; }
  .cat-item { white-space: nowrap; border-left: none; border-bottom: 2px solid transparent; }
  .cat-item.active { border-bottom-color: var(--primary); border-left-color: transparent; }
  .home-cards { flex-direction: column; }
  .home-card { width: 100%; max-width: 300px; height: 120px; }
  .filters { flex-direction: column; }
  .topbar h1 { font-size: .92rem; }
}

/* ---- Warehouse management ---- */
.wh-add-row { display: flex; gap: .5rem; align-items: center; margin-top: 1rem; }
.wh-add-input { max-width: 300px; }
.wh-name-input {
  border: 1px solid transparent; background: transparent; padding: .3rem .5rem;
  font-size: .84rem; border-radius: 4px; width: 100%;
}
.wh-name-input:focus { border-color: var(--primary); background: var(--surface); outline: none; }

.mt-1 { margin-top: .5rem; }
.mt-2 { margin-top: 1rem; }
.mt-3 { margin-top: 1.5rem; }
.mb-1 { margin-bottom: .5rem; }
.text-muted { color: var(--text-secondary); }
.text-sm { font-size: .82rem; }
.hidden { display: none; }

/* ---- Modal overlay ---- */
.modal-overlay {
  position: fixed; inset: 0; background: rgba(0,0,0,.45); z-index: 200;
  display: flex; align-items: center; justify-content: center; padding: 1rem;
}
.modal-overlay.hidden { display: none; }
.modal {
  background: var(--surface); border-radius: 12px; box-shadow: var(--shadow-lg);
  padding: 1.5rem 2rem; width: 100%; max-width: 500px; max-height: 90vh; overflow-y: auto;
}
.modal-wide { max-width: 750px; }
.modal-title { font-size: 1.1rem; font-weight: 600; margin-bottom: 1rem; }
.modal-body { margin-bottom: 1.25rem; font-size: .88rem; line-height: 1.6; }
.modal-body ul { margin: .5rem 0 .5rem 1.25rem; }
.modal-actions { display: flex; gap: .75rem; justify-content: flex-end; padding-top: .75rem; border-top: 1px solid var(--border); }

/* ---- Sortable headers ---- */
.sortable { cursor: pointer; user-select: none; position: relative; padding-right: 1.2rem !important; }
.sortable::after { content: ''; position: absolute; right: .3rem; top: 50%; transform: translateY(-50%); font-size: .65rem; color: var(--text-secondary); }
.sortable:hover { color: var(--primary); }
.sortable.sort-asc::after { content: '\25B2'; }
.sortable.sort-desc::after { content: '\25BC'; }

/* ---- DB toolbar ---- */
.db-toolbar {
  display: flex; justify-content: space-between; align-items: center; flex-wrap: wrap;
  padding: .75rem 1.5rem; gap: .75rem; border-bottom: 1px solid var(--border);
}
.db-toolbar-left { display: flex; gap: .5rem; }
.db-toolbar-right { display: flex; gap: .5rem; align-items: center; }

/* ---- Archive toggle ---- */
.archive-toggle {
  display: inline-flex; align-items: center; gap: .35rem; font-size: .82rem;
  color: var(--text-secondary); cursor: pointer; white-space: nowrap;
}
.archive-toggle input { margin: 0; }

/* ---- Archived row ---- */
.archived-row td { opacity: .5; }
.archived-row:hover td { opacity: .7; }

/* ---- Flash green animation ---- */
.flash-green { animation: flashGreen .8s ease; }
@keyframes flashGreen { 0% { background: #d1fae5; } 100% { background: transparent; } }

/* ---- Inline edit ---- */
.edit-input {
  padding: .3rem .4rem; font-size: .82rem; border: 1px solid var(--primary);
  border-radius: 4px; background: var(--surface); width: 100%; min-width: 60px;
}
.edit-input:focus { outline: none; box-shadow: 0 0 0 2px rgba(37,99,235,.15); }
.edit-qty { width: 60px; min-width: 60px; }
.row-actions { white-space: nowrap; }
.row-actions .btn { padding: .25rem .5rem; font-size: .78rem; }

/* ---- Category rename ---- */
.cat-rename-row { display: flex; align-items: center; gap: .5rem; }
.cat-rename-input { max-width: 160px; padding: .4rem .6rem; font-size: .82rem; border: 1px solid var(--border); border-radius: 4px; }
.cat-rename-input:focus { border-color: var(--primary); outline: none; }

/* ---- Warehouse delete options ---- */
.wh-delete-options { margin: 1rem 0; display: flex; flex-direction: column; gap: .5rem; }
.radio-option { display: flex; align-items: flex-start; gap: .5rem; font-size: .85rem; cursor: pointer; padding: .5rem; border: 1px solid var(--border); border-radius: var(--radius); }
.radio-option:hover { border-color: var(--primary); }
.radio-option input { margin-top: .2rem; }
.wh-confirm-name { margin-top: .75rem; }
.wh-confirm-name label { font-size: .82rem; display: block; margin-bottom: .3rem; }
.wh-confirm-name input { max-width: 300px; }

/* ---- DB add form ---- */
.db-add-form { padding: 1.5rem; border-bottom: 1px solid var(--border); background: #fafbfc; }

/* ---- Audit badge ---- */
.badge-audit { background: #fef3c7; color: #92400e; }

/* ---- Import preview ---- */
#importPreviewWrap .table-wrapper { max-height: 300px; overflow-y: auto; }

/* ---- Category tree (numbered) ---- */
.cat-node {
  display: flex; align-items: center; gap: 6px;
  padding: 4px 8px 4px calc(8px + var(--depth, 0) * 14px);
  cursor: pointer; user-select: none; line-height: 1.4;
  font-size: 13px; border-radius: 4px;
}
.cat-node:hover { background: #F0F0F0; }
.cat-node.selected { background: #EEF2FF; font-weight: 600; }
.cat-path { font-family: ui-monospace, monospace; color: #6B7280; font-size: 12px; min-width: 42px; }
.cat-name { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.cat-count { color: #9CA3AF; font-size: 11px; flex-shrink: 0; }
.cat-chevron { width: 12px; color: #6B7280; font-size: 11px; text-align: center; flex-shrink: 0; }
.cat-children { }

/* ---- Breadcrumb ---- */
.cat-breadcrumb {
  padding: .5rem .75rem; font-size: .8rem; color: var(--text-secondary);
  border-top: 1px solid var(--border); margin-top: .5rem;
}
.bc-seg { color: var(--primary); cursor: pointer; font-family: ui-monospace, monospace; font-size: .78rem; }
.bc-seg:hover { text-decoration: underline; }
.bc-sep { color: var(--text-secondary); margin: 0 .15rem; }

/* ---- Mobile sidebar drawer ---- */
.cat-drawer-toggle {
  display: none; padding: .5rem 1rem; font-size: .88rem; font-weight: 500;
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  cursor: pointer; margin-bottom: .75rem;
}
.cat-drawer-overlay {
  display: none; position: fixed; inset: 0; background: rgba(0,0,0,.4); z-index: 150;
}
.cat-drawer-overlay.open { display: block; }

@media (max-width: 768px) {
  .cat-sidebar { display: none; }
  .cat-sidebar.drawer-open {
    display: block; position: fixed; left: 0; top: 56px; bottom: 0; width: 280px;
    z-index: 160; box-shadow: var(--shadow-lg); overflow-y: auto;
  }
  .cat-drawer-toggle { display: block; }
}

/* ---- Import report ---- */
.import-summary h3 { font-size: .95rem; font-weight: 600; margin-bottom: .5rem; }
.import-summary td { padding: .3rem .75rem; font-size: .85rem; }
.import-summary td:first-child { color: var(--text-secondary); }
.import-details { margin-top: .5rem; font-size: .85rem; }
.import-details summary { cursor: pointer; font-weight: 500; padding: .3rem 0; color: var(--text-secondary); }
.import-details summary:hover { color: var(--text); }
.import-list { margin: .25rem 0 0 1.25rem; font-size: .82rem; line-height: 1.6; }
.import-errors summary { color: var(--danger); }
.text-success { color: var(--success); }
.text-warning { color: #d97706; }
