:root {
  --bg: #f4f2ee;
  --surface: #ffffff;
  --ink: #1d1b18;
  --muted: #706a61;
  --line: #ded8cf;
  --accent: #0f766e;
  --accent-dark: #0b5f59;
  --warm: #b7791f;
  --danger: #b42318;
  --ok: #287d3c;
  --shadow: 0 14px 34px rgba(34, 28, 20, 0.08);
  --label-width: 40mm;
  --label-height: 25mm;
  --label-preview-width: 200px;
  --label-preview-height: 125px;
  --label-font-scale: 1;
  --label-print-gap: 2mm;
  --label-print-break: auto;
}

* { box-sizing: border-box; }

html {
  overflow-x: hidden;
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
}

body {
  margin: 0;
  min-height: 100vh;
  width: 100%;
  overflow-x: hidden;
  display: grid;
  grid-template-columns: 248px 1fr;
  background: var(--bg);
  color: var(--ink);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC", "Microsoft YaHei", sans-serif;
}

button, input, select {
  font: inherit;
}

.sidebar {
  min-height: 100vh;
  padding: 20px 16px;
  background: #211e1a;
  color: #fff;
  display: flex;
  flex-direction: column;
  gap: 22px;
}

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

.brand-copy {
  min-width: 0;
  flex: 1;
}

.brand-title-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
}

.mark {
  width: 42px;
  height: 42px;
  display: grid;
  place-items: center;
  border: 1px solid rgba(255, 255, 255, 0.28);
  background: #0f766e;
  font-weight: 800;
  font-size: 22px;
}

.brand strong,
.brand span {
  display: block;
}

.brand span {
  margin-top: 3px;
  color: rgba(255, 255, 255, 0.62);
  font-size: 12px;
}

.nav {
  display: grid;
  gap: 8px;
}

.nav-item {
  width: 100%;
  border: 0;
  background: transparent;
  color: rgba(255, 255, 255, 0.74);
  text-align: left;
  padding: 12px 14px;
  border-radius: 8px;
  cursor: pointer;
}

.nav-item:hover,
.nav-item.active {
  background: rgba(255, 255, 255, 0.1);
  color: #fff;
}

.operator-panel {
  display: grid;
  gap: 10px;
  padding: 12px;
  border: 1px solid rgba(255, 255, 255, 0.13);
  border-radius: 8px;
  background: rgba(255, 255, 255, 0.06);
}

.operator-panel label {
  color: rgba(255, 255, 255, 0.62);
}

.operator-panel select {
  color: #fff;
  background: #2e2a24;
  border-color: rgba(255, 255, 255, 0.16);
}

.operator-panel select:disabled {
  opacity: 1;
}

.account-line {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  color: rgba(255, 255, 255, 0.78);
  font-size: 13px;
}

.logout-button {
  min-height: 30px;
  padding: 0 10px;
  color: #fff;
  background: rgba(255, 255, 255, 0.08);
  border-color: rgba(255, 255, 255, 0.16);
}

.sidebar-note {
  margin-top: auto;
  padding: 14px;
  border: 1px solid rgba(255, 255, 255, 0.13);
  border-radius: 8px;
  background: rgba(255, 255, 255, 0.06);
}

.sidebar-note span,
.sidebar-note strong {
  display: block;
}

.sidebar-note span {
  color: rgba(255, 255, 255, 0.58);
  font-size: 12px;
}

.sidebar-note strong {
  margin-top: 3px;
  font-size: 14px;
}

.app {
  min-width: 0;
  padding: 24px;
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  margin-bottom: 20px;
}

.topbar-heading {
  min-width: 0;
}

.topbar-right {
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  gap: 10px;
  min-width: 0;
}

h1, h2, p {
  margin: 0;
}

h1 {
  font-size: 26px;
  line-height: 1.2;
}

.topbar p {
  margin-top: 6px;
  color: var(--muted);
  font-size: 14px;
}

.top-actions,
.panel-actions,
.form-actions,
.report-toolbar {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
}

.attendance-status {
  min-height: 38px;
  display: inline-flex;
  align-items: center;
  padding: 0 10px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fbfaf7;
  color: var(--muted);
  font-size: 13px;
  white-space: nowrap;
}

.attendance-status.clock_in {
  border-color: #c9e5d0;
  background: #eef8f0;
  color: var(--ok);
}

.attendance-status.clock_out {
  border-color: #f0d2cb;
  background: #fff0ed;
  color: var(--danger);
}

button {
  min-height: 38px;
  border: 1px solid var(--line);
  background: var(--surface);
  color: var(--ink);
  border-radius: 8px;
  padding: 0 14px;
  cursor: pointer;
  white-space: nowrap;
}

button:hover {
  border-color: #bdb3a5;
}

.primary {
  background: var(--accent);
  color: #fff;
  border-color: var(--accent);
  font-weight: 700;
}

.primary:hover {
  background: var(--accent-dark);
  border-color: var(--accent-dark);
}

.ghost {
  background: #f8f6f2;
}

.language-toggle {
  min-width: 64px;
}

.brand-language-toggle {
  min-height: 30px;
  padding: 0 10px;
  border-color: rgba(255, 255, 255, 0.28);
  background: rgba(255, 255, 255, 0.08);
  color: #fff;
  font-size: 12px;
}

.icon-button {
  width: 40px;
  padding: 0;
  display: grid;
  place-items: center;
}

.icon-button svg {
  width: 20px;
  height: 20px;
  fill: none;
  stroke: currentColor;
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.view {
  display: none;
}

.view.active {
  display: block;
}

.metrics {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 14px;
  margin-bottom: 16px;
}

.metrics.compact {
  grid-template-columns: repeat(5, minmax(0, 1fr));
}

.metric {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 16px;
  box-shadow: var(--shadow);
}

.metric span {
  display: block;
  color: var(--muted);
  font-size: 13px;
}

.metric strong {
  display: block;
  margin-top: 8px;
  font-size: 26px;
  line-height: 1.1;
}

.metric small {
  display: block;
  margin-top: 8px;
  color: var(--muted);
}

.content-grid {
  display: grid;
  grid-template-columns: minmax(0, 1.2fr) minmax(360px, 0.8fr);
  gap: 16px;
}

.settings-grid {
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  gap: 16px;
}

.panel {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 8px;
  box-shadow: var(--shadow);
  margin-bottom: 16px;
}

.panel-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 14px 16px;
  border-bottom: 1px solid var(--line);
}

.panel-head h2 {
  font-size: 16px;
}

.panel-head span {
  color: var(--muted);
  font-size: 13px;
}

.panel-actions {
  justify-content: flex-end;
  flex-wrap: wrap;
}

.form-panel {
  padding-bottom: 2px;
}

.form-grid {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 12px;
  padding: 16px;
}

.compact-form {
  grid-template-columns: repeat(3, minmax(0, 1fr));
}

label {
  display: grid;
  gap: 6px;
  color: var(--muted);
  font-size: 13px;
}

input,
select {
  width: 100%;
  height: 38px;
  font-size: 16px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
  color: var(--ink);
  padding: 0 10px;
}

.filter-select,
.filter-date {
  width: auto;
  min-width: 138px;
}

.field-with-button {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 8px;
}

.compact-button {
  min-height: 38px;
  padding: 0 12px;
  white-space: nowrap;
}

input:focus,
select:focus {
  outline: 2px solid rgba(15, 118, 110, 0.18);
  border-color: var(--accent);
}

.wide {
  grid-column: span 2;
}

.form-actions {
  grid-column: 1 / -1;
  justify-content: flex-end;
}

.search {
  max-width: 260px;
}

.activity-table {
  min-width: 1100px;
}

.table-wrap {
  overflow: auto;
  max-width: 100%;
  overscroll-behavior-x: contain;
  -webkit-overflow-scrolling: touch;
}

table {
  width: 100%;
  border-collapse: collapse;
  min-width: 940px;
}

th,
td {
  padding: 12px 14px;
  border-bottom: 1px solid #eee9e2;
  text-align: left;
  vertical-align: top;
  font-size: 14px;
}

th {
  color: var(--muted);
  background: #fbfaf7;
  font-size: 12px;
  font-weight: 700;
  white-space: nowrap;
}

td {
  color: #2a2723;
}

.subline {
  display: block;
  margin-top: 4px;
  color: var(--muted);
  font-size: 12px;
  line-height: 1.25;
}

.product-thumb {
  width: 54px;
  height: 54px;
  border-radius: 8px;
  border: 1px solid var(--line);
  object-fit: cover;
  background: #f1eee8;
  display: grid;
  place-items: center;
  color: var(--muted);
  font-weight: 700;
}

.product-thumb.placeholder {
  font-size: 18px;
}

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

.row-actions button {
  min-height: 30px;
  padding: 0 10px;
  font-size: 12px;
}

.tag {
  display: inline-flex;
  align-items: center;
  min-height: 24px;
  padding: 0 8px;
  border-radius: 999px;
  background: #f1eee8;
  color: #4b453d;
  font-size: 12px;
  white-space: nowrap;
}

.tag.danger {
  background: #fff0ed;
  color: var(--danger);
}

.tag.ok {
  background: #eef8f0;
  color: var(--ok);
}

.timeline {
  padding: 8px 16px 16px;
}

.event {
  display: grid;
  grid-template-columns: 72px 1fr;
  gap: 12px;
  padding: 12px 0;
  border-bottom: 1px solid #eee9e2;
}

.event:last-child {
  border-bottom: 0;
}

.event time {
  color: var(--muted);
  font-size: 12px;
}

.event strong {
  display: block;
  font-size: 14px;
}

.event span {
  display: block;
  margin-top: 4px;
  color: var(--muted);
  font-size: 13px;
}

.report-toolbar {
  justify-content: flex-end;
  margin-bottom: 14px;
}

.report-toolbar label {
  width: 170px;
}

.toast {
  position: fixed;
  right: 22px;
  bottom: 22px;
  min-width: 220px;
  max-width: 320px;
  padding: 12px 14px;
  background: #211e1a;
  color: #fff;
  border-radius: 8px;
  box-shadow: 0 12px 30px rgba(0, 0, 0, 0.24);
  opacity: 0;
  transform: translateY(10px);
  pointer-events: none;
  transition: 0.18s ease;
}

.toast.show {
  opacity: 1;
  transform: translateY(0);
}

.login-screen {
  position: fixed;
  inset: 0;
  z-index: 40;
  display: none;
  align-items: center;
  justify-content: center;
  padding: 20px;
  background: #f4f2ee;
}

.login-language-toggle {
  position: fixed;
  top: 20px;
  right: 20px;
  z-index: 41;
  background: var(--surface);
}

.login-screen.show {
  display: flex;
}

.login-card {
  width: min(420px, 100%);
  padding: 28px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 8px;
  box-shadow: var(--shadow);
  display: grid;
  gap: 14px;
}

.login-card .mark {
  color: #fff;
}

.login-card h2 {
  font-size: 22px;
}

.login-card p,
.login-hint {
  color: var(--muted);
  font-size: 13px;
  line-height: 1.45;
}

.modal {
  position: fixed;
  inset: 0;
  z-index: 20;
  display: none;
  align-items: center;
  justify-content: center;
  padding: 20px;
  background: rgba(24, 21, 18, 0.46);
  overflow: auto;
  overscroll-behavior: contain;
}

.modal.show {
  display: flex;
}

.modal-card {
  width: min(980px, 100%);
  max-height: calc(100vh - 40px);
  max-height: calc(100dvh - 40px);
  overflow: auto;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 8px;
  box-shadow: 0 24px 80px rgba(0, 0, 0, 0.26);
}

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

.modal-head h2 {
  font-size: 18px;
}

.modal-head span {
  display: block;
  margin-top: 4px;
  color: var(--muted);
  font-size: 13px;
}

.modal-head .icon-button {
  font-size: 24px;
  line-height: 1;
}

.product-form {
  display: grid;
  grid-template-columns: 220px 1fr;
  gap: 0;
}

.photo-box {
  padding: 16px;
  border-right: 1px solid var(--line);
  background: #fbfaf7;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.photo-preview {
  width: 100%;
  aspect-ratio: 1;
  border: 1px dashed #c9c0b3;
  border-radius: 8px;
  background: #fff;
  display: grid;
  place-items: center;
  color: var(--muted);
  overflow: hidden;
}

.photo-preview img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.upload-button {
  height: 38px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
  color: var(--ink);
  display: grid;
  place-items: center;
  cursor: pointer;
}

.upload-button input {
  position: absolute;
  width: 1px;
  height: 1px;
  opacity: 0;
  pointer-events: none;
}

.editor-grid {
  padding: 16px;
}

.scanner-card {
  width: min(560px, 100%);
}

.scanner-body {
  padding: 16px;
  display: grid;
  gap: 14px;
}

#scannerVideo {
  width: 100%;
  aspect-ratio: 4 / 3;
  background: #161412;
  border-radius: 8px;
  object-fit: cover;
}

.html5-qr-reader {
  display: none;
  overflow: hidden;
  border-radius: 8px;
  background: #161412;
}

.use-html5-scanner #scannerVideo {
  display: none;
}

.use-html5-scanner .html5-qr-reader {
  display: block;
}

.html5-qr-reader video {
  width: 100% !important;
  border-radius: 8px;
}

.html5-qr-reader img,
.html5-qr-reader button,
.html5-qr-reader select {
  display: none !important;
}

.scanner-fallback {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 10px;
  align-items: end;
}

#scannerStatus {
  color: var(--muted);
  font-size: 13px;
}

.empty {
  padding: 18px 14px;
  color: var(--muted);
}

.compact-field {
  min-height: 40px;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 0 10px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface);
  color: var(--muted);
  white-space: nowrap;
}

.compact-field input {
  width: 54px;
  min-height: 28px;
  padding: 4px 6px;
  border: 0;
  border-radius: 4px;
  background: #f6f2eb;
  text-align: center;
}

.compact-field input:disabled {
  color: var(--muted);
  opacity: 0.78;
}

.label-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(var(--label-preview-width), 1fr));
  gap: 12px;
  padding: 16px;
  align-items: start;
}

.label-card {
  width: var(--label-preview-width);
  min-height: var(--label-preview-height);
  border: 1px dashed #b9afa1;
  border-radius: 8px;
  padding: 12px;
  display: grid;
  align-content: start;
  gap: 6px;
  background: #fff;
}

.label-card strong {
  font-size: 18px;
}

.label-card span,
.label-card small {
  color: var(--muted);
}

.barcode-text {
  min-height: 34px;
  display: grid;
  place-items: center;
  border: 1px solid var(--line);
  background: repeating-linear-gradient(90deg, #1d1b18 0 2px, #fff 2px 5px, #1d1b18 5px 6px, #fff 6px 10px);
  color: transparent;
  font-size: 1px;
}

@media print {
  body {
    display: block;
    background: #fff;
  }

  @page {
    margin: 6mm;
  }

  .sidebar,
  .topbar,
  .view:not(#products),
  #products > .panel:not(#productLabelsPanel),
  #productLabelsPanel .panel-head,
  .toast,
  .login-screen,
  .modal {
    display: none !important;
  }

  .app {
    padding: 0;
  }

  #products,
  #products.view,
  #productLabelsPanel {
    display: block !important;
  }

  .panel {
    border: 0;
    box-shadow: none;
  }

  .label-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, var(--label-width));
    gap: var(--label-print-gap);
    padding: 0;
  }

  .label-card {
    width: var(--label-width);
    height: var(--label-height);
    min-height: 0;
    padding: 2mm;
    gap: 1mm;
    border-radius: 0;
    box-shadow: none;
    overflow: hidden;
    page-break-inside: avoid;
    page-break-after: var(--label-print-break);
    break-inside: avoid;
    break-after: var(--label-print-break);
  }

  .label-card strong {
    font-size: calc(8pt * var(--label-font-scale));
    line-height: 1.1;
  }

  .label-card span,
  .label-card small {
    font-size: calc(6pt * var(--label-font-scale));
    line-height: 1.15;
  }

  .barcode-text {
    min-height: 7mm;
  }

  body.print-attendance .view,
  body.print-attendance #settings,
  body.print-attendance #attendanceReportPanel {
    display: block !important;
  }

  body.print-attendance #products,
  body.print-attendance #productLabelsPanel {
    display: none !important;
  }

  body.print-attendance #settings > .settings-grid > .panel:not(#attendanceReportPanel),
  body.print-attendance #attendanceReportPanel .panel-actions {
    display: none !important;
  }

  body.print-attendance .table-wrap {
    overflow: visible;
  }

  body.print-attendance table {
    min-width: 0;
    width: 100%;
  }

  body.print-attendance th,
  body.print-attendance td {
    padding: 6px 8px;
    font-size: 10pt;
  }
}

@media (max-width: 980px) {
  body {
    grid-template-columns: 1fr;
  }

  .sidebar {
    min-height: auto;
    position: static;
    z-index: 4;
    padding: 10px 12px;
    gap: 10px;
  }

  .sidebar-note {
    display: none;
  }

  .brand {
    padding: 0 2px;
  }

  .brand-title-row {
    justify-content: flex-start;
  }

  .brand-language-toggle {
    margin-left: auto;
    min-height: 32px;
  }

  .brand .mark {
    width: 34px;
    height: 34px;
    font-size: 18px;
  }

  .nav {
    display: grid;
    grid-template-columns: repeat(4, minmax(0, 1fr));
    overflow: visible;
    gap: 6px;
    padding-bottom: 2px;
  }

  .nav-item {
    width: 100%;
    min-height: 36px;
    padding: 8px 6px;
    text-align: center;
    white-space: nowrap;
    font-size: 13px;
  }

  .operator-panel {
    grid-template-columns: 1fr 1fr;
    padding: 10px;
  }

  .app {
    padding: 16px;
    overflow-x: hidden;
  }

  .topbar {
    align-items: flex-start;
    flex-direction: column;
    min-width: 0;
  }

  .topbar-heading,
  .topbar-right {
    width: 100%;
  }

  .topbar-right {
    align-items: stretch;
  }

  .top-actions,
  .panel-actions,
  .report-toolbar {
    min-width: 0;
  }

  h1 {
    font-size: 22px;
  }

  .metrics,
  .metrics.compact,
  .content-grid,
  .form-grid,
  .compact-form,
  .product-form {
    grid-template-columns: 1fr;
  }

  .photo-box {
    border-right: 0;
    border-bottom: 1px solid var(--line);
  }

  .photo-preview {
    aspect-ratio: 16 / 9;
    max-height: 220px;
  }

  .wide,
  .form-actions {
    grid-column: auto;
  }

  .form-actions,
  .panel-actions,
  .report-toolbar {
    justify-content: stretch;
    flex-wrap: wrap;
  }

  .form-actions button,
  .panel-actions button,
  .report-toolbar button,
  .top-actions button {
    flex: 0 0 auto;
  }

  .search {
    max-width: none;
    flex: 1 1 100%;
  }

  .scanner-fallback {
    grid-template-columns: 1fr;
  }

  .modal {
    align-items: flex-start;
    justify-content: flex-start;
    padding: max(12px, env(safe-area-inset-top)) 12px max(12px, env(safe-area-inset-bottom));
  }

  .modal-card {
    width: 100%;
    max-height: calc(100vh - 24px);
    max-height: calc(100dvh - 24px);
  }

  .report-toolbar label {
    width: calc(50% - 5px);
  }

  .filter-select,
  .filter-date {
    flex: 1 1 calc(50% - 5px);
    min-width: 0;
  }

  .table-wrap {
    overflow: visible;
  }

  table,
  .activity-table {
    min-width: 0;
    width: 100%;
  }

  thead {
    display: none;
  }

  tbody {
    display: grid;
    gap: 10px;
  }

  tr {
    display: grid;
    gap: 0;
    padding: 10px;
    border: 1px solid var(--line);
    border-radius: 8px;
    background: #fff;
  }

  td {
    min-width: 0;
    display: flex;
    justify-content: space-between;
    gap: 12px;
    padding: 8px 0;
    border-bottom: 1px solid #f0ece5;
    font-size: 14px;
    text-align: right;
    overflow-wrap: anywhere;
  }

  td:last-child {
    border-bottom: 0;
  }

  td::before {
    content: attr(data-label);
    flex: 0 0 96px;
    color: var(--muted);
    font-size: 12px;
    font-weight: 700;
    text-align: left;
  }

  td.empty {
    display: block;
    text-align: center;
  }

  td.empty::before {
    content: "";
    display: none;
  }

  td .subline {
    text-align: right;
  }

  .row-actions {
    justify-content: flex-end;
  }

  .row-actions button {
    flex: 1 1 auto;
  }
}

@media (max-width: 640px) {
  .app {
    padding: 12px;
  }

  .sidebar {
    padding: 10px;
  }

  .brand {
    gap: 8px;
  }

  .brand strong {
    font-size: 14px;
  }

  .brand span {
    display: none;
  }

  .nav {
    grid-template-columns: repeat(3, minmax(0, 1fr));
  }

  .operator-panel {
    grid-template-columns: 1fr;
  }

  .account-line {
    align-items: flex-start;
    flex-direction: column;
  }

  .top-actions,
  .panel-actions,
  .report-toolbar {
    width: 100%;
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .top-actions button,
  .panel-actions button,
  .report-toolbar button {
    width: 100%;
    min-width: 0;
    flex: initial;
    padding: 0 10px;
  }

  .topbar-right > .language-toggle {
    width: auto;
  }

  .top-actions .icon-button {
    width: 100%;
  }

  .attendance-status,
  .search,
  .panel-actions select,
  .panel-actions .compact-field,
  .report-toolbar label {
    grid-column: 1 / -1;
    width: 100%;
    max-width: none;
  }

  .filter-select,
  .filter-date,
  .report-toolbar label {
    flex: 1 1 100%;
    width: 100%;
  }

  .panel-head {
    align-items: flex-start;
    flex-direction: column;
  }

  .form-grid,
  .compact-form,
  .editor-grid {
    padding: 12px;
  }

  .metrics,
  .metrics.compact {
    gap: 10px;
  }

  .metric {
    padding: 12px;
  }

  .metric strong {
    font-size: 22px;
  }
}
