:root {
  color-scheme: light;
  --bg: #f6f7f9;
  --surface: #ffffff;
  --surface-2: #f0f3f6;
  --ink: #1f2933;
  --muted: #667382;
  --line: #d9e0e7;
  --accent: #bb1e2d;
  --accent-dark: #8f1622;
  --ok: #0f8a5f;
  --warn: #b76a00;
  --danger: #b42318;
  --shadow: 0 18px 42px rgba(25, 34, 45, .12);
  --radius: 8px;
  font-family: "Segoe UI", Roboto, Arial, sans-serif;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  min-height: 100vh;
  background: var(--bg);
  color: var(--ink);
  letter-spacing: 0;
}

button, input, textarea {
  font: inherit;
}

button {
  cursor: pointer;
}

.hidden { display: none !important; }

.app-header {
  position: sticky;
  top: 0;
  z-index: 10;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px clamp(16px, 4vw, 32px);
  background: rgba(255, 255, 255, .95);
  border-bottom: 1px solid var(--line);
  backdrop-filter: blur(12px);
}

.brand-lockup {
  display: flex;
  align-items: center;
  gap: 12px;
  min-width: 0;
}

.brand-logo, .auth-logo {
  width: 44px;
  height: 44px;
  object-fit: cover;
  border-radius: 6px;
  border: 1px solid var(--line);
}

.brand, .doc-label {
  margin: 0;
  color: var(--muted);
  font-size: 12px;
  font-weight: 700;
  text-transform: uppercase;
}

h1, h2, h3, p {
  margin-top: 0;
}

.app-header h1 {
  margin: 0;
  font-size: 21px;
  line-height: 1.1;
}

main {
  width: min(1080px, 100%);
  margin: 0 auto;
  padding: 18px clamp(14px, 4vw, 32px) 42px;
}

.auth-view {
  min-height: calc(100vh - 92px);
  display: grid;
  place-items: center;
}

.auth-panel {
  width: min(420px, 100%);
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 28px;
}

.auth-logo {
  width: 62px;
  height: 62px;
  margin-bottom: 18px;
}

.auth-panel h2 {
  margin-bottom: 8px;
  font-size: 24px;
}

.stock-form h2, .open-panel h2 {
  margin-bottom: 18px;
  font-size: 24px;
}

.auth-copy {
  margin: 0 0 22px;
  color: var(--muted);
  line-height: 1.5;
}

form label, .field-grid label, .scan-panel label {
  display: grid;
  gap: 7px;
  color: var(--muted);
  font-size: 13px;
  font-weight: 700;
}

input, textarea {
  width: 100%;
  min-height: 46px;
  padding: 10px 12px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: #fff;
  color: var(--ink);
  outline: none;
}

input:focus, textarea:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(187, 30, 45, .12);
}

.auth-panel form, .stock-form, .open-panel, .scan-panel, .line-panel, .menu-panel, .balance-panel {
  display: grid;
  gap: 16px;
}

.primary-button, .secondary-button, .danger-button, .icon-button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 9px;
  min-height: 44px;
  border: 1px solid transparent;
  border-radius: 6px;
  font-size: 14px;
  font-weight: 800;
}

.primary-button {
  padding: 0 18px;
  background: var(--accent);
  color: #fff;
}

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

.secondary-button {
  padding: 0 16px;
  background: var(--surface);
  color: var(--ink);
  border-color: var(--line);
}

.danger-button {
  width: 100%;
  padding: 0 16px;
  background: #fff4f2;
  border-color: #ffd5cf;
  color: var(--danger);
}

.icon-button {
  width: 44px;
  padding: 0;
  background: var(--surface-2);
  color: var(--ink);
  border-color: var(--line);
  flex: 0 0 auto;
}

.icon-button.dark {
  background: var(--ink);
  border-color: var(--ink);
  color: #fff;
}

button svg { width: 19px; height: 19px; }

.user-bar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 10px;
  margin-bottom: 16px;
  color: var(--muted);
  font-size: 13px;
  font-weight: 700;
}

.connection-state {
  display: inline-flex;
  align-items: center;
  gap: 7px;
}

.state-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--ok);
}

.menu-panel {
  grid-template-columns: repeat(2, minmax(0, 1fr));
  align-items: stretch;
}

.module-button {
  width: 100%;
  min-height: 150px;
  display: grid;
  grid-template-columns: 76px minmax(0, 1fr) 24px;
  align-items: center;
  gap: 14px;
  padding: 18px;
  text-align: left;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--ink);
  box-shadow: 0 10px 24px rgba(25, 34, 45, .08);
}

.module-button:hover {
  border-color: rgba(187, 30, 45, .42);
  box-shadow: 0 14px 30px rgba(25, 34, 45, .12);
}

.module-button img {
  width: 76px;
  height: 76px;
  object-fit: contain;
  border-radius: 8px;
}

.module-button strong {
  display: block;
  margin-bottom: 7px;
  font-size: 20px;
}

.module-button small {
  display: block;
  color: var(--muted);
  font-size: 13px;
  font-weight: 700;
  line-height: 1.35;
}

.home-panel {
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(320px, .82fr);
  gap: 16px;
  align-items: start;
}

.stock-form, .open-panel, .scan-panel, .line-panel, .stocktake-panel, .balance-result, .search-results {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 18px;
}

.stocktake-toolbar, .screen-heading {
  grid-column: 1 / -1;
  display: grid;
  grid-template-columns: 44px minmax(0, 1fr);
  gap: 12px;
  align-items: center;
}

.stocktake-toolbar h2, .screen-heading h2 {
  margin: 2px 0 0;
  font-size: 25px;
}

.form-heading, .panel-heading, .items-heading {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.form-heading h2, .panel-heading h2, .items-heading h3 {
  margin: 0;
}

#nextRefPreview, .items-heading span {
  margin: 5px 0 0;
  color: var(--muted);
  font-size: 13px;
  font-weight: 700;
}

.field-grid {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 12px;
}

.field-grid.two {
  grid-template-columns: minmax(120px, .35fr) minmax(0, 1fr);
}

.manual-row {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 44px;
  gap: 10px;
}

.manual-row.search-row {
  grid-template-columns: minmax(0, 1fr) 44px 44px;
}

.recent-list {
  display: grid;
  gap: 8px;
  max-height: 52vh;
  overflow: auto;
  padding-right: 2px;
}

.recent-button {
  width: 100%;
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 8px;
  padding: 12px;
  text-align: left;
  border: 1px solid var(--line);
  background: #fff;
  border-radius: 6px;
}

.recent-button strong {
  display: block;
  font-size: 15px;
}

.recent-button span {
  color: var(--muted);
  font-size: 12px;
  font-weight: 700;
}

.recent-qty {
  align-self: center;
  color: var(--accent);
  font-weight: 900;
}

.stocktake-panel {
  display: grid;
  gap: 16px;
}

.doc-heading {
  display: grid;
  grid-template-columns: 44px minmax(0, 1fr) auto;
  gap: 12px;
  align-items: center;
}

.doc-heading h2 {
  margin: 2px 0 0;
  font-size: 25px;
}

.status-badge {
  display: inline-flex;
  align-items: center;
  min-height: 30px;
  padding: 0 10px;
  border-radius: 999px;
  font-size: 12px;
  font-weight: 900;
  background: #e8f7ef;
  color: var(--ok);
}

.status-badge.locked {
  background: #fff2d8;
  color: var(--warn);
}

.status-badge.transfer {
  background: #ffe8e4;
  color: var(--danger);
}

.doc-facts {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 10px;
  margin: 0;
}

.doc-facts div {
  padding: 12px;
  background: var(--surface-2);
  border-radius: 6px;
}

.doc-facts dt {
  margin-bottom: 5px;
  color: var(--muted);
  font-size: 12px;
  font-weight: 800;
}

.doc-facts dd {
  margin: 0;
  font-size: 16px;
  font-weight: 900;
}

.remark-block {
  border-left: 4px solid var(--accent);
  padding: 10px 12px;
  background: #fff8f8;
  border-radius: 0 6px 6px 0;
}

.remark-block span {
  color: var(--muted);
  font-size: 12px;
  font-weight: 900;
}

.remark-block p {
  margin: 4px 0 0;
  font-weight: 700;
}

.scan-actions {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 156px;
  gap: 10px;
}

.scan-actions .primary-button {
  min-height: 52px;
  font-size: 15px;
}

.balance-panel .scan-actions {
  grid-template-columns: 1fr;
}

.balance-result {
  display: grid;
  gap: 14px;
}

.search-results {
  display: grid;
  gap: 12px;
}

.scan-status {
  display: grid;
  gap: 12px;
}

.search-loading {
  display: grid;
  grid-template-columns: 22px minmax(0, 1fr);
  gap: 6px 10px;
  align-items: center;
  width: 100%;
  padding: 12px 14px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: #fff;
  color: var(--ink);
}

.search-loading span {
  width: 18px;
  height: 18px;
  border: 3px solid #dbe4ee;
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin .8s linear infinite;
}

.search-loading strong {
  font-size: 14px;
}

.search-loading p {
  grid-column: 2;
  margin: 0;
  color: var(--muted);
  font-size: 12px;
  font-weight: 700;
}

.search-result-list {
  display: grid;
  gap: 8px;
  max-height: 46vh;
  overflow: auto;
  padding-right: 2px;
}

.search-result-button {
  width: 100%;
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 12px;
  align-items: center;
  padding: 12px;
  text-align: left;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: #fff;
  color: var(--ink);
}

.search-result-button:hover {
  border-color: rgba(187, 30, 45, .42);
}

.search-result-button strong {
  color: var(--accent);
  font-size: 18px;
}

.particular-heading {
  display: grid;
  grid-template-columns: 44px minmax(0, 1fr) auto;
  gap: 12px;
  align-items: center;
}

.particular-heading.no-back {
  grid-template-columns: minmax(0, 1fr) auto;
}

.particular-heading h3 {
  margin: 2px 0 0;
  font-size: 22px;
}

.particular-heading strong {
  color: var(--accent);
  font-size: 28px;
  justify-self: end;
}

.balance-facts {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 10px;
  margin: 0;
}

.balance-facts div {
  padding: 12px;
  background: var(--surface-2);
  border-radius: 6px;
}

.balance-facts dt {
  margin-bottom: 5px;
  color: var(--muted);
  font-size: 12px;
  font-weight: 800;
}

.balance-facts dd {
  margin: 0;
  font-size: 16px;
  font-weight: 900;
  overflow-wrap: anywhere;
}

.qty-stepper {
  display: grid;
  grid-template-columns: 40px minmax(0, 1fr) 40px;
  gap: 6px;
}

.qty-stepper .icon-button {
  width: 40px;
  min-height: 52px;
}

.qty-stepper input {
  min-height: 52px;
  text-align: center;
  font-weight: 900;
}

.lines-list {
  display: grid;
  gap: 9px;
}

.line-row {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 112px;
  gap: 12px;
  align-items: center;
  padding: 12px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: #fff;
}

.line-code {
  margin: 0 0 4px;
  font-size: 14px;
  font-weight: 900;
}

.line-desc {
  margin: 0;
  color: var(--muted);
  font-size: 13px;
  line-height: 1.35;
}

.line-meta {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-top: 8px;
  color: var(--muted);
  font-size: 12px;
  font-weight: 800;
}

.balance-meta {
  display: inline-flex;
  gap: 4px;
}

.balance-positive {
  color: #0b66c3;
}

.balance-negative {
  color: var(--danger);
}

.balance-zero {
  color: var(--muted);
}

.line-qty {
  display: grid;
  gap: 4px;
  justify-items: end;
}

.line-qty strong {
  font-size: 24px;
}

.line-qty span {
  color: var(--muted);
  font-size: 12px;
  font-weight: 800;
}

dialog {
  width: min(720px, calc(100% - 24px));
  border: 0;
  border-radius: var(--radius);
  padding: 0;
  box-shadow: var(--shadow);
}

dialog::backdrop {
  background: rgba(18, 27, 38, .62);
}

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

.dialog-header h2 {
  margin: 0;
  font-size: 21px;
}

.camera-frame {
  position: relative;
  background: #111923;
  aspect-ratio: 4 / 3;
  overflow: hidden;
}

.camera-frame video {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.scan-guide {
  position: absolute;
  left: 50%;
  top: 50%;
  width: min(68%, 380px);
  aspect-ratio: 1;
  border: 3px solid rgba(255, 255, 255, .92);
  border-radius: 8px;
  transform: translate(-50%, -50%);
  box-shadow: 0 0 0 999px rgba(0, 0, 0, .18);
}

#cameraHelp {
  margin: 0;
  padding: 14px 16px 18px;
  color: var(--muted);
  font-size: 13px;
  font-weight: 700;
}

.edit-dialog form {
  display: grid;
  gap: 14px;
  padding: 0 16px 16px;
}

.edit-dialog .dialog-header {
  margin: 0 -16px;
}

.toast {
  position: fixed;
  left: 50%;
  bottom: 20px;
  transform: translateX(-50%) translateY(80px);
  max-width: min(520px, calc(100% - 24px));
  padding: 12px 14px;
  border-radius: 6px;
  background: var(--ink);
  color: #fff;
  font-size: 14px;
  font-weight: 700;
  opacity: 0;
  transition: .22s ease;
  z-index: 30;
}

.toast.show {
  opacity: 1;
  transform: translateX(-50%) translateY(0);
}

.toast.error {
  background: var(--danger);
}

@keyframes spin {
  to {
    transform: rotate(360deg);
  }
}

@media (max-width: 760px) {
  main {
    padding-inline: 12px;
  }

  .menu-panel, .home-panel {
    grid-template-columns: 1fr;
  }

  .field-grid, .field-grid.two, .doc-facts, .scan-actions, .balance-facts {
    grid-template-columns: 1fr;
  }

  .module-button {
    min-height: 118px;
    grid-template-columns: 68px minmax(0, 1fr) 24px;
  }

  .module-button img {
    width: 68px;
    height: 68px;
  }

  .doc-heading {
    grid-template-columns: 44px minmax(0, 1fr);
  }

  .particular-heading {
    grid-template-columns: 44px minmax(0, 1fr);
  }

  .particular-heading strong {
    grid-column: 2;
    justify-self: start;
  }

  .particular-heading.no-back {
    grid-template-columns: 1fr;
  }

  .particular-heading.no-back strong {
    grid-column: auto;
  }

  .status-badge {
    grid-column: 2;
    width: max-content;
  }

  .line-row {
    grid-template-columns: 1fr;
  }

  .search-result-button {
    grid-template-columns: 1fr;
  }

  .line-qty {
    justify-items: start;
    grid-template-columns: auto auto;
    align-items: baseline;
  }
}
