:root {
  --navy: #0b2f3a;
  --navy-2: #0F766E;
  --bg: #f4f6f8;
  --surface: #ffffff;
  --surface-2: #f8fafb;
  --text: #13212c;
  --muted: #536371;
  --border: #d8e0e6;
  --border-strong: #b5c2cc;
  --accent: #0F766E; /* main accent (QA addition 1) */
  --accent-ink: #ffffff;
  --accent-text: #0F766E;
  --accent-soft: #e6f4f2;
  --danger: #b42318;
  --danger-bg: #fef3f2;
  --warn: #8a5300;
  --warn-bg: #fff7e6;
  --ok: #067647;
  --focus: #1f6feb;
  --radius: 12px;
  --shadow: 0 1px 2px rgb(16 24 40 / 6%), 0 1px 3px rgb(16 24 40 / 8%);
  --font: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --mono: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0d1419;
    --surface: #141e25;
    --surface-2: #19252e;
    --text: #e6edf2;
    --muted: #9fb0bd;
    --border: #2a3943;
    --border-strong: #3a4d59;
    --accent: #0F766E;
    --accent-ink: #ffffff;
    --accent-text: #5eead4;
    --accent-soft: #10302d;
    --danger: #ff8a80;
    --danger-bg: #2c1513;
    --warn: #ffc56b;
    --warn-bg: #2b2210;
    --ok: #57d39b;
    --focus: #79b8ff;
    --shadow: 0 1px 2px rgb(0 0 0 / 40%);
    color-scheme: dark;
  }
  :root mark { background: #ffd54f; color: #13212c; }
}

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

[hidden] { display: none !important; }

::placeholder { color: var(--muted); opacity: 1; }

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

body {
  margin: 0;
  font: 15px/1.5 var(--font);
  color: var(--text);
  background: var(--bg);
  min-height: 100vh;
}

svg { width: 18px; height: 18px; flex: none; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }

.wrap { width: 100%; max-width: 1120px; margin: 0 auto; padding: 0 16px; }

.visually-hidden {
  position: absolute !important;
  width: 1px; height: 1px; padding: 0; margin: -1px;
  overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; border: 0;
}

:focus-visible { outline: 3px solid var(--focus); outline-offset: 2px; border-radius: 6px; }

.skip-link {
  position: absolute; left: 12px; top: -48px; z-index: 50;
  background: var(--surface); color: var(--text); padding: 8px 14px; border-radius: 8px;
  box-shadow: var(--shadow); text-decoration: none; font-weight: 600;
}
.skip-link:focus { top: 12px; }

/* Header */
.app-header { background: var(--navy); color: #fff; }
.header-inner { display: flex; align-items: center; justify-content: space-between; gap: 12px; min-height: 64px; flex-wrap: wrap; padding-top: 10px; padding-bottom: 10px; }
.brand { display: flex; align-items: center; gap: 10px; }
.brand-mark { width: 36px; height: 36px; }
.brand-mark rect { fill: #fff; stroke: none; }
.brand-mark path { stroke: var(--navy); stroke-width: 2.4; }
.brand-name { margin: 0; font-weight: 750; font-size: 18px; letter-spacing: -0.01em; }
.brand-tag { margin: 0; font-size: 12.5px; opacity: .8; }
.save-state { margin: 0; display: inline-flex; align-items: center; gap: 8px; font-size: 13px; background: rgb(255 255 255 / 10%); padding: 5px 12px; border-radius: 999px; }
.save-state .dot { width: 8px; height: 8px; border-radius: 50%; background: #57d39b; }
.save-state[data-state="error"] .dot { background: #ff8a80; }
.save-state[data-state="warn"] .dot { background: #ffc56b; }

main.wrap { padding-top: 20px; padding-bottom: 8px; }

/* Summary */
.stats { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 12px; margin: 0; }
.stat { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 14px 16px; box-shadow: var(--shadow); min-width: 0; }
.stat dt { font-size: 13px; color: var(--muted); font-weight: 550; }
.stat dd { margin: 4px 0 0; font-size: 24px; font-weight: 700; font-variant-numeric: tabular-nums; letter-spacing: -0.01em; overflow-wrap: anywhere; }
.stat-primary { background: linear-gradient(135deg, var(--navy), var(--navy-2)); border-color: transparent; color: #fff; }
.stat-primary dt { color: #fff; }
.stats-note { margin: 8px 2px 0; font-size: 12.5px; color: var(--muted); }

/* Banners */
.banners { display: grid; gap: 10px; margin-top: 16px; }
.banners:empty { display: none; }
.banner { display: flex; gap: 12px; align-items: flex-start; border-radius: var(--radius); padding: 12px 14px; border: 1px solid; }
.banner-error { background: var(--danger-bg); border-color: color-mix(in srgb, var(--danger) 35%, transparent); color: var(--text); }
.banner-warn { background: var(--warn-bg); border-color: color-mix(in srgb, var(--warn) 35%, transparent); }
.banner .banner-body { flex: 1; min-width: 0; }
.banner h3 { margin: 0 0 4px; font-size: 15px; }
.banner-error h3 { color: var(--danger); }
.banner-warn h3 { color: var(--warn); }
.banner p, .banner ul { margin: 0; }
.banner ul { padding-left: 18px; margin-top: 6px; font-size: 14px; max-height: 220px; overflow: auto; }
.banner li + li { margin-top: 2px; }
.banner-actions { display: flex; gap: 8px; margin-top: 10px; flex-wrap: wrap; }

/* Panel & toolbar */
.panel { margin-top: 20px; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow); }
.panel-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 16px 16px 0; flex-wrap: wrap; }
.panel-head h2 { margin: 0; font-size: 18px; }
.panel-head h2:focus { outline: none; }
.result-count { margin: 6px 16px 0; color: var(--muted); font-size: 13.5px; min-height: 1.5em; }
.toolbar { display: flex; gap: 12px; padding: 12px 16px 16px; flex-wrap: wrap; align-items: center; border-bottom: 1px solid var(--border); }
.search { position: relative; flex: 1 1 260px; min-width: 0; }
.search > svg { position: absolute; left: 11px; top: 50%; transform: translateY(-50%); color: var(--muted); pointer-events: none; }
.search input { width: 100%; padding-left: 36px; padding-right: 36px; }
.kbd-hint { position: absolute; right: 10px; top: 50%; transform: translateY(-50%); }
.search input:focus + .kbd-hint, .search input:not(:placeholder-shown) + .kbd-hint { display: none; }
.actions { display: flex; gap: 8px; flex-wrap: wrap; }

kbd { font: 12px var(--mono); border: 1px solid var(--border-strong); border-bottom-width: 2px; border-radius: 5px; padding: 0 5px; color: var(--muted); background: var(--surface-2); }

input {
  font: inherit; color: inherit; background: var(--surface);
  border: 1px solid var(--border-strong); border-radius: 9px; padding: 9px 12px; min-height: 44px; width: 100%;
}
input:focus-visible { outline: 3px solid var(--focus); outline-offset: 0; border-color: var(--focus); border-radius: 9px; }
input[aria-invalid="true"] { border-color: var(--danger); }
input[type="search"]::-webkit-search-cancel-button { cursor: pointer; }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  font: inherit; font-weight: 600; font-size: 14px; color: var(--text);
  background: var(--surface); border: 1px solid var(--border-strong); border-radius: 9px;
  min-height: 44px; padding: 8px 14px; cursor: pointer; white-space: nowrap;
}
.btn:hover:not(:disabled) { background: var(--surface-2); border-color: var(--muted); }
.btn:disabled { opacity: .5; cursor: not-allowed; }
.btn-primary { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }
.btn-primary:hover:not(:disabled) { background: color-mix(in srgb, var(--accent) 88%, #000); border-color: transparent; }
.btn-danger { color: var(--danger); }
.btn-small { min-height: 36px; padding: 4px 12px; font-size: 13.5px; }
.btn-inverse { background: transparent; color: #fff; border-color: rgb(255 255 255 / 55%); }
.btn-inverse:hover:not(:disabled) { background: rgb(255 255 255 / 12%); }
.link-btn { font: inherit; background: none; border: 0; padding: 4px; color: var(--accent-text); text-decoration: underline; cursor: pointer; }
#undo-label { max-width: 220px; overflow: hidden; text-overflow: ellipsis; }

.icon-btn {
  display: inline-flex; align-items: center; justify-content: center;
  width: 40px; height: 40px; border-radius: 9px; border: 1px solid var(--border-strong);
  background: var(--surface); color: var(--text); cursor: pointer; padding: 0;
}
.icon-btn:hover:not(:disabled) { background: var(--surface-2); }
.icon-btn:disabled { opacity: .4; cursor: not-allowed; }

/* Table */
.list-region { min-height: 160px; }
.table-scroll { overflow-x: auto; position: relative; }
table { width: 100%; border-collapse: collapse; font-variant-numeric: tabular-nums; }
caption { text-align: left; }
th, td { padding: 10px 16px; text-align: left; border-bottom: 1px solid var(--border); vertical-align: middle; }
thead th { font-size: 12.5px; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); font-weight: 650; background: var(--surface-2); }
tbody tr:last-child td, tbody tr:last-child th { border-bottom: 0; }
tbody tr:hover { background: var(--surface-2); }
.num { text-align: right; }
td.sku, th.sku { font-family: var(--mono); font-size: 13.5px; font-weight: 600; }
td.sku { overflow-wrap: anywhere; min-width: 7ch; }
tbody th { font-weight: 600; }
.item-name { white-space: pre-line; overflow-wrap: anywhere; }
.inventory-table th.item-name { min-width: 12ch; }
.qty-cell { display: inline-flex; align-items: center; gap: 6px; justify-content: flex-end; }
.qty-value { min-width: 3ch; text-align: center; font-weight: 650; }
.badge { display: inline-block; white-space: nowrap; font-size: 11.5px; font-weight: 650; padding: 1px 8px; border-radius: 999px; background: var(--danger-bg); color: var(--danger); border: 1px solid color-mix(in srgb, var(--danger) 30%, transparent); margin-left: 8px; vertical-align: 1px; }
tr.is-out td:not(.row-actions), tr.is-out th { color: var(--muted); }
.badge-reorder { background: var(--warn-bg); color: var(--warn); border-color: color-mix(in srgb, var(--warn) 35%, transparent); }
.badge-archived { background: var(--surface-2); color: var(--muted); border-color: var(--border-strong); }
.stat-sub { margin: 2px 0 0 !important; font-size: 12.5px !important; font-weight: 500 !important; color: var(--muted); }
.stat-reorder dd:first-of-type { color: var(--warn); }
.view-switch { display: inline-flex; border: 1px solid var(--border-strong); border-radius: 10px; padding: 3px; gap: 3px; background: var(--surface-2); }
.seg { font: inherit; font-size: 14px; font-weight: 600; border: 0; background: transparent; color: var(--muted); border-radius: 7px; min-height: 40px; padding: 4px 12px; cursor: pointer; }
.seg[aria-pressed="true"] { background: var(--accent); color: var(--accent-ink); }
.seg-count { font-variant-numeric: tabular-nums; }
.footer-notes { display: grid; gap: 2px; }
#reserved-note { color: var(--text); font-weight: 600; }
.row-actions { white-space: nowrap; text-align: right; }
.row-actions .btn + .btn { margin-left: 6px; }
mark { background: color-mix(in srgb, #ffd54f 55%, transparent); color: inherit; border-radius: 3px; padding: 0 1px; }

/* States */
.state { padding: 44px 20px; text-align: center; color: var(--muted); }
.state h3 { color: var(--text); margin: 10px 0 4px; font-size: 17px; }
.state p { margin: 0 auto; max-width: 42ch; }
.state .state-actions { margin-top: 16px; display: flex; gap: 8px; justify-content: center; flex-wrap: wrap; }
.state-icon { width: 44px; height: 44px; color: var(--border-strong); }
.state-loading { display: flex; flex-direction: column; align-items: center; gap: 10px; }
.spinner { width: 28px; height: 28px; border-radius: 50%; border: 3px solid var(--border); border-top-color: var(--accent); animation: spin .8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
.is-busy { position: relative; }
.busy-overlay { position: absolute; inset: 0; display: flex; align-items: center; justify-content: center; gap: 10px; background: color-mix(in srgb, var(--surface) 80%, transparent); font-weight: 600; z-index: 2; border-radius: var(--radius); }

/* Stock activity */
.activity .panel-head { align-items: baseline; }
.activity-count { margin: 0; color: var(--muted); font-size: 13.5px; }
.activity-intro { margin: 4px 16px 0; color: var(--muted); font-size: 13.5px; }
.activity-notice { margin: 10px 16px 0; padding: 8px 12px; border-radius: 8px; background: var(--warn-bg); color: var(--text); font-size: 14px; }
.activity-filter { display: flex; gap: 8px; padding: 12px 16px 0; flex-wrap: wrap; }
.activity-filter .search { flex: 1 1 240px; }
.activity-filter .search input { padding-right: 12px; }
.activity-filter-status, .activity-empty { overflow-wrap: anywhere; }
.activity-filter-status { margin: 6px 16px 0; color: var(--muted); font-size: 13.5px; min-height: 1.5em; }
.activity-empty { margin: 0; padding: 24px 16px; color: var(--muted); text-align: center; }
.activity-list { list-style: none; margin: 10px 0 0; padding: 0; border-top: 1px solid var(--border); }
.activity-item { display: grid; grid-template-columns: 7.5rem minmax(0, 1fr) auto; gap: 4px 12px; align-items: baseline; padding: 10px 16px; border-bottom: 1px solid var(--border); }
.activity-item:last-child { border-bottom: 0; }
.activity-item:focus-visible { outline-offset: -3px; }
.activity-type { justify-self: start; font-size: 12px; font-weight: 650; padding: 1px 8px; border-radius: 999px; background: var(--accent-soft); color: var(--accent-text); border: 1px solid color-mix(in srgb, var(--accent) 30%, transparent); white-space: nowrap; }
.type-archive .activity-type { background: var(--surface-2); color: var(--muted); border-color: var(--border-strong); }
.type-undo .activity-type, .type-reset .activity-type { background: var(--warn-bg); color: var(--warn); border-color: color-mix(in srgb, var(--warn) 35%, transparent); }
.activity-text { margin: 0; overflow-wrap: anywhere; white-space: pre-line; }
.activity-item time { color: var(--muted); font-size: 13px; white-space: nowrap; font-variant-numeric: tabular-nums; }
.activity-foot { padding: 0 16px; }
.activity-foot:has(#activity-more:not([hidden])) { padding: 10px 16px 14px; border-top: 1px solid var(--border); }

/* Toast */
.toast {
  position: fixed; left: 16px; right: 16px; bottom: 20px; margin-inline: auto; width: fit-content; z-index: 40;
  display: flex; align-items: center; gap: 10px; max-width: min(560px, calc(100vw - 32px));
  background: #13212c; color: #fff; border-radius: 12px; padding: 8px 8px 8px 16px; box-shadow: 0 8px 28px rgb(0 0 0 / 25%);
}
.toast[hidden] { display: none; }
.toast p { margin: 0; flex: 1; min-width: 0; font-size: 14px; }
.toast .icon-btn { background: transparent; border-color: transparent; color: #fff; }
.toast .icon-btn:hover { background: rgb(255 255 255 / 12%); }

/* Dialog */
.dialog { border: 0; padding: 0; border-radius: 16px; width: min(520px, calc(100vw - 24px)); max-height: calc(100dvh - 24px); background: var(--surface); color: var(--text); box-shadow: 0 24px 60px rgb(0 0 0 / 30%); }
.dialog::backdrop { background: rgb(8 20 30 / 55%); }
.dialog form { display: flex; flex-direction: column; max-height: calc(100dvh - 24px); }
.dialog-head { padding: 18px 20px 0; }
.dialog-head h2 { margin: 0; font-size: 19px; }
.dialog-body { padding: 14px 20px; overflow: auto; }
.dialog-foot { display: flex; justify-content: flex-end; gap: 8px; padding: 12px 20px 18px; border-top: 1px solid var(--border); }
.field { margin-bottom: 12px; min-width: 0; }
.field label { display: block; font-weight: 600; font-size: 14px; margin-bottom: 5px; }
.field-row { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.hint { margin: 4px 0 0; font-size: 12.5px; color: var(--muted); }
.field-error { margin: 4px 0 0; font-size: 13px; color: var(--danger); font-weight: 550; }
.field-error:empty { display: none; }
.form-error { background: var(--danger-bg); color: var(--danger); padding: 8px 12px; border-radius: 8px; font-weight: 550; margin: 0 0 12px; }
.prefix-input { position: relative; }
.prefix-input span { position: absolute; left: 12px; top: 50%; transform: translateY(-50%); color: var(--muted); font-weight: 600; pointer-events: none; }
.prefix-input input { padding-left: 38px; }
.line-preview { margin: 4px 0 0; font-size: 14px; color: var(--muted); }
.small-preview { max-height: 240px; overflow: auto; border: 1px solid var(--border); border-radius: 10px; margin-top: 10px; }
.small-preview th, .small-preview td { padding: 6px 10px; font-size: 13.5px; }

.app-footer.wrap { margin-top: 10px; padding-bottom: 96px; display: flex; justify-content: space-between; align-items: flex-end; gap: 12px; flex-wrap: wrap; color: var(--muted); font-size: 13px; }
.app-footer p { margin: 0; }

/* Tablet */
@media (max-width: 860px) {
  .stats { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  /* Tablet and phone: the table becomes stacked cards so long SKUs/names never force sideways scrolling. */
  .inventory-table thead { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); }
  .inventory-table, .inventory-table tbody, .inventory-table tr, .inventory-table td, .inventory-table th { display: block; }
  .inventory-table tr { display: grid; grid-template-columns: minmax(0, 1fr); gap: 2px 12px; padding: 14px 16px; border-bottom: 1px solid var(--border); }
  .inventory-table tbody tr:last-child { border-bottom: 0; }
  .inventory-table td, .inventory-table th { border: 0; padding: 0; text-align: left; }
  .inventory-table td.sku { grid-column: 1 / -1; overflow-wrap: anywhere; }
  .inventory-table th.item-name { grid-column: 1 / -1; font-size: 16px; }
  .inventory-table td[data-label] { grid-column: 1 / -1; display: flex; flex-wrap: wrap; justify-content: space-between; align-items: center; gap: 4px 12px; padding: 3px 0; overflow-wrap: anywhere; }
  .inventory-table td[data-label]::before { content: attr(data-label); color: var(--muted); font-size: 13.5px; font-weight: 550; }
  .inventory-table td.row-actions { grid-column: 1 / -1; display: grid; grid-template-columns: 1fr 1fr; gap: 8px; margin-top: 8px; }
  .row-actions .btn + .btn { margin-left: 0; }
  .btn-small { min-height: 44px; }
  .actions { width: 100%; }
  .actions .btn { flex: 1 1 auto; }
}

/* Phone: table becomes stacked cards */
@media (max-width: 640px) {
  body { font-size: 15px; }
  main.wrap { padding-top: 14px; }
  .stat dd { font-size: 20px; }
  .stat { padding: 12px; }
  .kbd-hint { display: none; }
  .actions { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .actions .btn { min-width: 0; padding-inline: 10px; }
  #add-btn { grid-column: 1 / -1; }
  #undo-btn { grid-column: 1 / -1; }
  .field-row { grid-template-columns: 1fr; gap: 0; }
  .app-footer.wrap { flex-direction: column; align-items: flex-start; }
  .activity-item { grid-template-columns: minmax(0, 1fr); }
  .activity-foot .btn, .activity-filter .btn { width: 100%; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: .01ms !important; animation-iteration-count: 1 !important; transition: none !important; }
}

@media (forced-colors: active) {
  .btn, .icon-btn, input { border: 1px solid ButtonText; }
  .stat-primary { border: 1px solid CanvasText; }
}
