:root {
  --bg: #f5f4ef;
  --surface: #ffffff;
  --surface-2: #f0efe9;
  --ink: #18211d;
  --muted: #5e6a64;
  --line: #e1dfd6;
  --brand: #0f4d3a;
  --brand-2: #0b3d2e;
  --brand-ink: #ffffff;
  --accent: #b8871b;
  --ok: #12784f;
  --ok-bg: #e5f3ec;
  --warn: #8a5a00;
  --warn-bg: #fbf0d9;
  --bad: #b3382c;
  --bad-bg: #fbe9e7;
  --info-bg: #e9f0f8;
  --focus: #2f7cf6;
  --radius: 12px;
  --shadow: 0 1px 2px rgb(0 0 0 / 6%), 0 4px 16px rgb(0 0 0 / 4%);
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0f1412; --surface: #171e1b; --surface-2: #1e2723; --ink: #e8ede9; --muted: #9aa8a1; --line: #2a3530;
    --brand: #13583f; --brand-2: #0e3f2e; --accent: #e3b341; --ok: #46d39a; --ok-bg: #143326; --warn: #f0c46b; --warn-bg: #3a2e14;
    --bad: #f2786b; --bad-bg: #3a1c18; --info-bg: #1a2735; --shadow: none; color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #0f1412; --surface: #171e1b; --surface-2: #1e2723; --ink: #e8ede9; --muted: #9aa8a1; --line: #2a3530;
  --brand: #13583f; --brand-2: #0e3f2e; --accent: #e3b341; --ok: #46d39a; --ok-bg: #143326; --warn: #f0c46b; --warn-bg: #3a2e14;
  --bad: #f2786b; --bad-bg: #3a1c18; --info-bg: #1a2735; --shadow: none; color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--ink);
  font: 15px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  font-variant-numeric: tabular-nums;
}
a { color: inherit; }
a:hover { color: var(--brand); }
:focus-visible { outline: 2px solid var(--focus); outline-offset: 2px; }
h1, h2, h3 { margin: 0; line-height: 1.2; }
h1 { font-size: 22px; }
h2 { font-size: 17px; }
h3 { font-size: 13px; color: var(--muted); font-weight: 600; text-transform: uppercase; letter-spacing: .04em; margin-bottom: 6px; }
p { margin: 6px 0; }
.muted { color: var(--muted); }
.small { font-size: 13px; }
.nowrap { white-space: nowrap; }
.pre { white-space: pre-wrap; }
.big { font-size: 26px; font-weight: 700; }
[hidden] { display: none !important; }
.visually-hidden { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }

/* Top bar */
.topbar {
  position: sticky; top: 0; z-index: 10;
  display: grid; grid-template-columns: auto 1fr auto; align-items: center; gap: 8px 20px;
  padding: 10px 24px 0; background: var(--brand); color: var(--brand-ink);
}
.brand { display: flex; align-items: center; gap: 10px; text-decoration: none; color: inherit; }
.brand:hover { color: inherit; }
.brand > span { display: flex; flex-direction: column; }
.brand img { border-radius: 8px; box-shadow: 0 0 0 1px rgb(255 255 255 / 25%); }
.brand-name { font-weight: 700; font-size: 14px; }
.brand-sub { font-size: 12px; opacity: .8; }
.scan { display: flex; gap: 6px; max-width: 520px; width: 100%; justify-self: center; }
.scan input {
  flex: 1; min-width: 0; font: 600 16px/1 ui-monospace, "Cascadia Mono", Consolas, monospace; letter-spacing: .04em;
  padding: 9px 12px; border-radius: 8px; border: 2px solid transparent; background: #fff; color: #111;
}
.scan input::placeholder { font-family: system-ui, sans-serif; font-weight: 400; letter-spacing: 0; color: #77807b; }
.scan input:focus { border-color: var(--accent); outline: none; }
.scan input.bad { border-color: #f2786b; }
.btn-scan { background: var(--accent); color: #1b1300; border: 0; font-weight: 700; }
.nav { grid-column: 1 / -1; display: flex; gap: 2px; overflow-x: auto; margin: 0 -8px; }
.nav a {
  padding: 9px 12px; text-decoration: none; white-space: nowrap; color: rgb(255 255 255 / 80%); font-weight: 500;
  border-bottom: 3px solid transparent;
}
.nav a:hover { color: #fff; }
.nav a.active { color: #fff; border-bottom-color: var(--accent); }
.user { position: relative; }
.menu {
  position: absolute; right: 0; top: calc(100% + 6px); min-width: 190px; padding: 6px;
  background: var(--surface); color: var(--ink); border: 1px solid var(--line); border-radius: 10px; box-shadow: var(--shadow);
}
.menu button { display: block; width: 100%; text-align: left; padding: 8px 10px; border: 0; background: none; color: inherit; border-radius: 6px; font: inherit; cursor: pointer; }
.menu button:hover { background: var(--surface-2); }

/* Layout */
.view { max-width: 1200px; margin: 0 auto; padding: 20px 24px 60px; display: flex; flex-direction: column; gap: 16px; }
.card { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 16px 18px; box-shadow: var(--shadow); }
.card > h2 { margin-bottom: 10px; }
.grid-2 { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 16px; }
.grid-3 { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 16px; }
.labels-layout { grid-template-columns: minmax(0, 5fr) minmax(0, 6fr); align-items: start; }
.page-head { display: flex; flex-direction: column; gap: 6px; }
.page-title-row { display: flex; justify-content: space-between; align-items: flex-end; gap: 12px; flex-wrap: wrap; }
.page-sub { color: var(--muted); margin-top: 4px; }
.page-actions, .row-actions, .quick { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }
.row-actions.inline { display: inline-flex; margin-left: 8px; gap: 4px; }
.crumbs { font-size: 13px; color: var(--muted); display: flex; gap: 6px; flex-wrap: wrap; }
.crumbs a { color: var(--muted); }
.hero h1 { margin-bottom: 4px; }
.search-row { display: flex; gap: 8px; margin-top: 10px; }
.search-row input { flex: 1; min-width: 0; }
.tiles { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 12px; }
.tile { text-decoration: none; }
.tile-value { font-size: 28px; font-weight: 700; }
.tile-label { color: var(--muted); font-size: 13px; }

/* Controls */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  padding: 8px 14px; border-radius: 8px; border: 1px solid var(--line); background: var(--surface); color: var(--ink);
  font: 600 14px/1.2 inherit; font-family: inherit; text-decoration: none; cursor: pointer; white-space: nowrap;
}
.btn:hover { border-color: var(--muted); color: var(--ink); }
.btn:disabled { opacity: .6; cursor: wait; }
.btn-primary { background: var(--brand); color: #fff; border-color: var(--brand); }
.btn-primary:hover { background: var(--brand-2); color: #fff; }
.btn-danger { background: var(--bad); color: #fff; border-color: var(--bad); }
.btn-ghost { background: transparent; }
.btn-ghost-inverse { background: transparent; color: #fff; border-color: rgb(255 255 255 / 30%); }
.btn-ghost-inverse:hover { color: #fff; border-color: #fff; }
.btn-link { background: none; border: 0; padding: 2px 4px; color: var(--brand); font-weight: 500; text-decoration: underline; }
.danger-text { color: var(--bad); }
.small-btn { padding: 6px 10px; font-size: 13px; align-self: end; }
.btn-block { width: 100%; }
input, select, textarea {
  font: inherit; color: var(--ink); background: var(--surface); border: 1px solid var(--line); border-radius: 8px; padding: 8px 10px; width: 100%;
}
textarea { resize: vertical; }
input:focus, select:focus, textarea:focus { border-color: var(--focus); outline: none; }
input.bad { border-color: var(--bad); }
input:disabled, select:disabled { background: var(--surface-2); color: var(--muted); }
.code-input, input.shelf-scan { font-family: ui-monospace, "Cascadia Mono", Consolas, monospace; text-transform: uppercase; }
input.shelf-scan::placeholder, .code-input::placeholder { text-transform: none; font-family: system-ui, sans-serif; }
.field { display: flex; flex-direction: column; gap: 4px; min-width: 0; }
.field-label { font-weight: 600; font-size: 13px; }
.hint { font-size: 12.5px; color: var(--muted); }
.fields { display: flex; flex-direction: column; gap: 12px; }
.fields.cols-2 { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); }
.form { display: flex; flex-direction: column; gap: 16px; }
.form h3 { margin-top: 6px; }
.form-actions { display: flex; justify-content: flex-end; }
.form-error { color: var(--bad); background: var(--bad-bg); padding: 8px 10px; border-radius: 8px; margin: 0; }
.rows { display: flex; flex-direction: column; gap: 10px; margin-bottom: 8px; }
.row-line { display: grid; grid-template-columns: repeat(auto-fit, minmax(160px, 1fr)); gap: 10px; align-items: start; padding: 10px; background: var(--surface-2); border-radius: 10px; }
.shelf-picker { display: grid; grid-template-columns: 1fr 150px; gap: 6px; }
.kind-picker { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 10px; }
.kind-option { display: flex; gap: 10px; padding: 12px; border: 2px solid var(--line); border-radius: 10px; cursor: pointer; }
.kind-option:has(input:checked) { border-color: var(--brand); background: var(--ok-bg); }
.kind-option input { width: auto; margin-top: 3px; }
.kind-option > span { display: flex; flex-direction: column; gap: 2px; }
.copies { width: 80px; text-align: right; }

/* Data */
.table-wrap { overflow-x: auto; }
.table { width: 100%; border-collapse: collapse; }
.table th, .table td { text-align: left; padding: 8px 10px; border-bottom: 1px solid var(--line); vertical-align: top; }
.table th { font-size: 12px; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); font-weight: 600; }
.table .num { text-align: right; }
.table .note { color: var(--muted); max-width: 320px; }
.table tr.inactive td { color: var(--muted); }
.empty { margin: 4px 0; }
.code-chip {
  font: 600 13px/1 ui-monospace, "Cascadia Mono", Consolas, monospace; letter-spacing: .03em;
  background: var(--surface-2); border: 1px solid var(--line); padding: 3px 6px; border-radius: 6px; white-space: nowrap;
}
.badge { display: inline-block; font-size: 12px; font-weight: 600; padding: 2px 8px; border-radius: 99px; background: var(--surface-2); white-space: nowrap; }
.badge-available { background: var(--ok-bg); color: var(--ok); }
.badge-damaged, .badge-repair { background: var(--bad-bg); color: var(--bad); }
.badge-inspection { background: var(--warn-bg); color: var(--warn); }
.badge-out { background: var(--info-bg); }
.badge-retired { color: var(--muted); }
.status-pair { display: inline-flex; align-items: center; gap: 4px; margin-right: 10px; }
.totals { display: flex; gap: 18px; flex-wrap: wrap; margin: 6px 0 10px; }
.stat strong { font-size: 18px; }
.stat-ok strong { color: var(--ok); }
.stat-held strong { color: var(--bad); }
.result-head { display: flex; justify-content: space-between; gap: 12px; align-items: flex-start; }
.rate { font-weight: 700; font-size: 18px; white-space: nowrap; }
.results { display: flex; flex-direction: column; gap: 12px; }
.shelf-head, .rack-head, .room-head { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; margin-bottom: 8px; }
.room-head h2 { margin-right: auto; }
.rack-head .row-actions { margin-left: auto; }
.racks { display: flex; flex-direction: column; gap: 12px; }
.rack { border: 1px solid var(--line); border-radius: 10px; padding: 10px 12px; }
.shelf-list { margin: 0; padding-left: 18px; display: flex; flex-direction: column; gap: 6px; }

/* Feedback */
.notice { padding: 10px 14px; border-radius: 10px; background: var(--surface-2); }
.notice.error { background: var(--bad-bg); color: var(--bad); }
.notice.ok { background: var(--ok-bg); color: var(--ok); }
.notice.info { background: var(--info-bg); }
.skeleton { height: 90px; border-radius: var(--radius); background: linear-gradient(90deg, var(--surface-2), var(--surface), var(--surface-2)); background-size: 200% 100%; animation: shimmer 1.2s infinite; }
.skeleton.short { height: 200px; }
@keyframes shimmer { to { background-position: -200% 0; } }
.toast {
  position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%); z-index: 50; max-width: min(560px, calc(100% - 32px));
  padding: 12px 16px; border-radius: 10px; color: #fff; background: #1f2a25; box-shadow: 0 6px 24px rgb(0 0 0 / 25%);
}
.toast-error { background: #8f2a20; }

/* Dialogs */
dialog { border: 0; border-radius: 14px; padding: 0; width: min(520px, calc(100% - 32px)); background: var(--surface); color: var(--ink); box-shadow: 0 20px 60px rgb(0 0 0 / 30%); }
dialog.wide { width: min(980px, calc(100% - 32px)); }
dialog::backdrop { background: rgb(0 0 0 / 40%); }
dialog form { padding: 20px; display: flex; flex-direction: column; gap: 14px; }
.dialog-actions { display: flex; justify-content: flex-end; gap: 8px; }

/* Login */
.login-page { display: grid; place-items: center; min-height: 100vh; padding: 16px; }
.login-card { width: min(380px, 100%); background: var(--surface); border: 1px solid var(--line); border-radius: 16px; padding: 28px; box-shadow: var(--shadow); display: flex; flex-direction: column; gap: 16px; }
.login-card form { display: flex; flex-direction: column; gap: 12px; }
.brand-lg { color: var(--ink); }
.brand-lg .brand-sub { color: var(--muted); opacity: 1; }

/* Labels: on-screen preview of the printed sheets */
.label-preview { display: flex; flex-direction: column; gap: 12px; align-items: flex-start; overflow: auto; max-height: 75vh; }
.label-preview .sheet { zoom: .55; box-shadow: 0 0 0 1px var(--line), var(--shadow); }
.sheet { background: #fff; color: #000; position: relative; }
.sheet-a4 { width: 210mm; height: 297mm; overflow: hidden; }
.sheet-single { display: inline-block; }
.label {
  display: flex; flex-direction: column; align-items: center; justify-content: center; text-align: center; overflow: hidden;
  background: #fff; color: #000; outline: 1px dashed #c8c8c8; outline-offset: -1px;
}
.label-title { font-weight: 700; line-height: 1.1; max-width: 100%; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; font-family: Arial, sans-serif; }
.label-sub { line-height: 1.1; max-width: 100%; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; font-family: Arial, sans-serif; }
.label svg { display: block; margin: .6mm 0 .4mm; }
.label-code { font-family: "Consolas", "DejaVu Sans Mono", "Courier New", monospace; font-weight: 700; letter-spacing: .06em; line-height: 1; }
#print-area { display: none; }

@media print {
  body.printing-labels > *:not(#print-area) { display: none !important; }
  body.printing-labels { background: #fff; }
  body.printing-labels #print-area { display: block; }
  #print-area .sheet { break-after: page; page-break-after: always; box-shadow: none; }
  #print-area .sheet:last-child { break-after: auto; page-break-after: auto; }
  #print-area .label { outline: none; }
}

/* Phones and small tablets */
@media (max-width: 760px) {
  .topbar { grid-template-columns: 1fr auto; padding: 8px 16px 0; }
  .scan { grid-column: 1 / -1; grid-row: 2; max-width: none; }
  .brand-sub { display: none; }
  .view { padding: 16px 16px 48px; }
  .grid-2, .grid-3, .labels-layout, .fields.cols-2, .kind-picker { grid-template-columns: minmax(0, 1fr); }
  .tiles { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .shelf-picker { grid-template-columns: minmax(0, 1fr); }
}
