:root {
  color-scheme: light;
  --bg: #f3f6f7;
  --paper: #fff;
  --ink: #192730;
  --muted: #526676;
  --line: #dce5e8;
  --dark: #132b37;
  --dark-hover: #20414f;
  --accent: #096b60;
  --accent-hover: #07574f;
  --accent-soft: #dff4ee;
  --warning: #81540b;
  --warning-soft: #fff3d7;
  --danger: #a52534;
  --danger-soft: #fdf0f1;
  --radius: 12px;
  font-family: "Instrument Sans", "Segoe UI", Arial, sans-serif;
  font-size: 100%;
  font-synthesis: none;
}

* { box-sizing: border-box; }
body { margin: 0; color: var(--ink); background: var(--bg); line-height: 1.5; }
[hidden] { display: none !important; }
button, input, select, textarea { font: inherit; }
button:disabled { cursor: not-allowed; opacity: .56; }
button, input, select, a { -webkit-tap-highlight-color: transparent; }
button, a, select, input:not([type=checkbox]) { min-height: 42px; }
button, a { touch-action: manipulation; }
:focus-visible { outline: 3px solid #1a9383; outline-offset: 3px; }
a { color: var(--accent); }
a.button { text-decoration: none; }
h1, h2, h3, p { margin: 0; }
h1 { font-size: clamp(1.9rem, 5vw, 2.65rem); letter-spacing: -.055em; line-height: 1.12; }
h2 { font-size: 1.25rem; letter-spacing: -.023em; line-height: 1.25; }
h3 { font-size: 1.05rem; line-height: 1.3; }
small, .small { font-size: .875rem; }
.muted { color: var(--muted); }
.number { font-variant-numeric: tabular-nums; text-align: right; white-space: nowrap; }
.nowrap { white-space: nowrap; }
.mono { font-family: Consolas, monospace; font-size: .9em; }

.shell { display: grid; grid-template-columns: 232px minmax(0, 1fr); min-height: 100dvh; }
.side { position: sticky; top: 0; height: 100dvh; display: flex; flex-direction: column; gap: 2rem; background: var(--dark); color: #edf7f8; padding: 1.5rem 1rem; }
.brand { display: flex; align-items: center; gap: .75rem; padding: .2rem .35rem; font-size: 1.13rem; font-weight: 720; letter-spacing: -.026em; }
.brand-mark { display: grid; place-items: center; flex: none; width: 37px; height: 37px; border-radius: 10px; background: #9ee4d1; color: #153a39; font-size: 1.25rem; font-weight: 800; }
.navigation { display: grid; gap: .32rem; }
.nav-item { display: flex; align-items: center; gap: .75rem; border: 0; border-radius: 8px; width: 100%; padding: .7rem .8rem; text-align: left; cursor: pointer; background: transparent; color: #dcebed; font-size: .93rem; font-weight: 630; }
.nav-item:hover, .nav-item[aria-current=page] { background: var(--dark-hover); }
.nav-item[aria-current=page] { color: #c1f3e4; }
.nav-symbol { display: inline-grid; place-items: center; width: 1.2rem; flex: none; font-size: 1.12rem; line-height: 1; }
.side-foot { margin-top: auto; display: grid; align-items: center; column-gap: .55rem; grid-template-columns: auto minmax(0, 1fr); padding: .65rem .35rem 0; border-top: 1px solid #36515a; font-size: .88rem; }
.user-badge { display: grid; place-items: center; width: 31px; height: 31px; border-radius: 50%; background: var(--dark-hover); color: #c1f3e4; font-weight: 750; }
.side-foot #user-name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.logout { color: #bcd0d5; grid-column: 2; justify-self: start; }
.topbar { display: flex; justify-content: flex-end; align-items: center; gap: 1rem; min-height: 69px; padding: .65rem clamp(1rem, 4vw, 3rem); border-bottom: 1px solid var(--line); background: var(--paper); }
.branch-control { display: flex; align-items: center; gap: .7rem; }
.branch-control label { color: var(--muted); font-size: .875rem; font-weight: 680; }
.branch-control select { min-width: 155px; }
.mobile-brand { display: none; }
.view { max-width: 1400px; margin-inline: auto; padding: clamp(1rem, 4vw, 3rem); outline: none; }

.page-head { display: flex; justify-content: space-between; align-items: flex-end; flex-wrap: wrap; gap: 1rem; margin-bottom: 1.5rem; }
.page-head p { margin-top: .4rem; }
.page-actions, .actions, .toolbar { display: flex; align-items: center; gap: .6rem; flex-wrap: wrap; }
.page-body, .stack { display: grid; gap: 1rem; }
.panel { min-width: 0; background: var(--paper); border: 1px solid var(--line); border-radius: var(--radius); padding: 1.2rem; }
.panel-head { display: flex; justify-content: space-between; align-items: center; gap: .75rem; flex-wrap: wrap; margin-bottom: .85rem; }
.panel-head h2 { margin: 0; }
.panel .panel { padding: .9rem; }
.empty { color: var(--muted); padding: .75rem 0; }
.tiles { display: grid; grid-template-columns: repeat(auto-fit, minmax(min(220px, 100%), 1fr)); gap: .85rem; }
.tile { display: grid; align-content: start; gap: .4rem; }
.tile .value { font-size: 1.55rem; font-weight: 760; letter-spacing: -.03em; }
.table-wrap { overflow: auto; }
table { border-collapse: collapse; width: 100%; }
th, td { padding: .78rem .72rem; text-align: left; border-bottom: 1px solid var(--line); vertical-align: middle; }
th { background: #f4f8f8; font-size: .85rem; color: var(--muted); font-weight: 750; }
td { font-size: .94rem; }
tbody tr:last-child td { border-bottom: 0; }
tbody tr:hover { background: #fafcfc; }
table .actions { flex-wrap: nowrap; }
.badge { display: inline-block; border-radius: 999px; padding: .2rem .56rem; font-size: .83rem; line-height: 1.3; font-weight: 700; white-space: nowrap; background: #eaf0f2; color: #3b5664; }
.badge.success { background: var(--accent-soft); color: var(--accent-hover); }
.badge.warning { background: var(--warning-soft); color: var(--warning); }
.badge.danger { background: var(--danger-soft); color: var(--danger); }
.callout { padding: .8rem 1rem; border-radius: 8px; background: var(--accent-soft); color: var(--accent-hover); }
.callout.error, .form-error { background: var(--danger-soft); color: var(--danger); }
.callout.warning { background: var(--warning-soft); color: var(--warning); }
.form-error { padding: .65rem .8rem; border-radius: 8px; }

.button, .quiet { border-radius: 8px; border: 1px solid var(--line); background: var(--paper); color: var(--ink); cursor: pointer; font-size: .92rem; font-weight: 680; line-height: 1.2; padding: .6rem .85rem; display: inline-flex; justify-content: center; align-items: center; text-align: center; }
.button:hover { background: #edf4f4; }
.button.primary { color: white; background: var(--accent); border-color: var(--accent); }
.button.primary:hover { background: var(--accent-hover); }
.button.compact { min-height: 35px; padding: .35rem .65rem; }
.quiet { border: 0; background: transparent; padding-inline: .15rem; }
.quiet:hover { text-decoration: underline; }
label { display: grid; gap: .3rem; font-size: .9rem; font-weight: 660; }
label.check { display: flex; flex-direction: row; align-items: center; gap: .55rem; min-height: 42px; }
input[type=checkbox] { accent-color: var(--accent); width: 1.12rem; height: 1.12rem; margin: 0; flex: none; }
input:not([type=checkbox]), select, textarea { display: block; width: 100%; min-width: 0; border: 1px solid #b9cbd0; border-radius: 8px; background-color: var(--paper); color: var(--ink); padding: .55rem .7rem; }
input:disabled, select:disabled { color: var(--muted); background-color: #eef3f3; }
input::placeholder { color: #627582; }
select { appearance: none; padding-right: 1.95rem; background-image: linear-gradient(45deg, transparent 50%, #4b6571 50%),linear-gradient(135deg, #4b6571 50%, transparent 50%); background-position: calc(100% - 15px) 50%,calc(100% - 10px) 50%; background-size: 5px 5px; background-repeat: no-repeat; }
select[multiple] { min-height: 96px; appearance: auto; background-image: none; padding-right: .7rem; }
input[type=file] { padding: .45rem; }
input[type=file]::file-selector-button { margin-right: .7rem; border: 0; border-radius: 5px; padding: .35rem .55rem; color: var(--accent-hover); background: var(--accent-soft); font: inherit; cursor: pointer; }
.fields, .filters { display: grid; grid-template-columns: repeat(auto-fit, minmax(min(160px, 100%), 1fr)); align-items: end; gap: .8rem; }
.fields .wide, .filters .wide { grid-column: 1 / -1; }
.form-section { border-top: 1px solid var(--line); padding-top: 1rem; }
.form-section h3 { margin-bottom: .65rem; }
fieldset { min-width: 0; margin: 0; padding: .65rem .8rem; border: 1px solid var(--line); border-radius: 8px; }
legend { padding-inline: .3rem; color: var(--muted); font-size: .88rem; font-weight: 700; }

.initial-load, .login-page { min-height: 100dvh; display: grid; align-items: center; justify-items: center; padding: 1rem; }
.login-page { background: linear-gradient(140deg, #0e2630 0%, #173b46 60%, #276e64 100%); }
.login-card { width: min(100%, 410px); background: var(--paper); border: 1px solid var(--line); border-radius: 18px; padding: clamp(1.3rem, 6vw, 2rem); display: grid; gap: 1.3rem; box-shadow: 0 18px 65px #0f25336e; }
.login-card .brand { padding: 0; }
.login-card h1 { font-size: 2rem; }
.notice { position: fixed; z-index: 5; right: 1rem; bottom: 1rem; max-width: min(480px, calc(100vw - 2rem)); background: var(--dark); color: white; border-radius: 9px; padding: .75rem 1rem; box-shadow: 0 3px 18px #21394431; }
.notice.error { background: var(--danger); }
.dialog { border: 1px solid var(--line); border-radius: 15px; padding: 0; width: min(690px, calc(100% - 2rem)); max-height: calc(100dvh - 2rem); color: var(--ink); }
.dialog::backdrop { background: #162d38a8; }
.dialog-content { display: grid; gap: 1rem; padding: clamp(1rem, 4vw, 1.6rem); }
.dialog-head { display: flex; align-items: center; justify-content: space-between; gap: .75rem; }
.dialog-actions { display: flex; flex-wrap: wrap; justify-content: flex-end; gap: .55rem; border-top: 1px solid var(--line); padding-top: .85rem; }
.location-removal-preview { min-width: 0; overflow-wrap: anywhere; }
.location-removal-list { max-height: min(20rem, 42dvh); overflow-y: auto; }

.count-list { display: grid; gap: .6rem; }
.count-card { display: grid; grid-template-columns: minmax(150px, 1.25fr) minmax(270px, 1fr); align-items: center; gap: .7rem 1.2rem; border: 1px solid var(--line); padding: .85rem; border-radius: 10px; }
.count-card.flagged { border-color: #d3838e; background: var(--danger-soft); }
.count-title { display: flex; align-items: baseline; min-width: 0; gap: .4rem; }
.count-name { min-width: 0; overflow: hidden; white-space: nowrap; text-overflow: ellipsis; }
.count-title.has-truncated-name .count-name { text-overflow: clip; }
.count-name.status-open { color: var(--warning); }
.count-name.status-counted { color: var(--accent-hover); }
.count-name.status-confirm { color: var(--danger); }
.count-name-reveal {
  display: inline-grid;
  position: relative;
  place-items: center;
  flex: none;
  align-self: center;
  width: 1.875rem;
  min-height: 1.875rem;
  padding: 0;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: var(--paper);
  color: var(--accent-hover);
  font-weight: 750;
  line-height: 1;
  cursor: pointer;
}
.count-name-reveal::before { content: ""; position: absolute; inset: -7px; }
.count-name-reveal:hover, .count-name-reveal:active { background: var(--accent-soft); }
.count-full-name {
  position: fixed;
  z-index: 9;
  inset: var(--count-name-popover-y, .5rem) auto auto var(--count-name-popover-x, .5rem);
  width: max-content;
  max-width: min(24rem, calc(100% - 1rem));
  max-height: min(14rem, calc(100dvh - 1rem));
  margin: 0;
  padding: .65rem .85rem;
  overflow: auto;
  overflow-wrap: anywhere;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--paper);
  color: var(--ink);
  box-shadow: 0 4px 20px #19273026;
  font-weight: 680;
}
.count-code, .count-supplier { flex: none; color: var(--muted); font-size: .8125rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.count-code { max-width: 35%; font-variant-numeric: tabular-nums; }
.count-supplier { max-width: 40%; }
.count-form { display: grid; grid-template-columns: repeat(auto-fit, minmax(min(95px, 100%), 1fr)); align-items: end; gap: .5rem; }
.count-form .actions { grid-column: 1 / -1; }
.group-heading { margin-top: .55rem; color: var(--muted); }
.report-total { border-top: 2px solid var(--line); font-weight: 750; }
.progress { width: 100%; height: .55rem; accent-color: var(--accent); }

@media (max-width: 850px) {
  .shell { grid-template-columns: 185px minmax(0, 1fr); }
  .side { padding-inline: .65rem; }
  .view { padding: 1.2rem; }
  .count-card { grid-template-columns: 1fr; }
}
@media (max-width: 660px) {
  .shell { display: flex; flex-direction: column; }
  .side { position: sticky; z-index: 3; top: 0; height: auto; padding: .35rem .5rem; gap: 0; }
  .side .brand, .user-badge { display: none; }
  .navigation { display: flex; overflow-x: auto; gap: .2rem; scrollbar-width: thin; }
  .nav-item { width: auto; flex: 0 0 auto; gap: .25rem; padding: .5rem .55rem; white-space: nowrap; font-size: .86rem; }
  .nav-symbol { font-size: 1rem; }
  .side-foot { display: none; }
  .topbar { min-height: 58px; justify-content: space-between; gap: .5rem; padding: .4rem .85rem; }
  .branch-control select { min-width: 0; width: min(140px, 36vw); }
  .mobile-brand { display: block; font-weight: 760; letter-spacing: -.03em; }
  .view { padding: .85rem; }
  .panel { padding: .85rem; }
  .page-head { margin-bottom: 1rem; gap: .75rem; }
  .page-actions { width: 100%; }
  .count-form .actions .button { min-height: 45px; }
  .mobile-logout { display: inline-flex; margin-left: auto; }
}
@media (min-width: 661px) {
  .mobile-logout { display: none; }
}

.section-gap { margin-top: 1rem; }
.issue-list { margin: .55rem 0 0; padding-left: 1.35rem; }
.issue-list li + li { margin-top: .35rem; }
.count-card .count-info { min-width: 0; grid-template-columns: minmax(0, 1fr); gap: .35rem; }
.count-form { grid-template-columns: minmax(0, 1fr); }
.entry-fields { display: grid; grid-template-columns: minmax(0, 1fr); gap: .5rem; }
.count-form .fields {
  grid-template-columns: none;
  grid-auto-flow: column;
  grid-auto-columns: minmax(0, 4rem);
  justify-content: start;
  gap: .4rem;
}
.count-form .fields label { min-width: 0; font-size: .8rem; line-height: 1.2; overflow-wrap: anywhere; }
.count-form .fields input[data-count-amount],
.count-form .fields select[name=quarter] {
  min-height: 44px;
  padding: .35rem;
  font-size: max(1rem, 16px);
  font-variant-numeric: tabular-nums;
}
.count-form .fields input[data-count-amount] { max-width: 3rem; text-align: center; }
.count-form .fields input:disabled { background: var(--bg); color: var(--muted); }
.count-unit-note { grid-column: 1 / -1; }
.count-form .fields select[name=quarter] {
  padding-right: 1.15rem;
  background-position: calc(100% - 10px) 50%, calc(100% - 5px) 50%;
}
.side-foot .logout { color: #bcd0d5; }

body.count-keypad-open {
  --count-keypad-space: min(calc(260px + env(safe-area-inset-bottom, 0px)), 58dvh);
}

body.count-keypad-open .view { padding-bottom: calc(var(--count-keypad-space) + 1rem); }
body.count-keypad-open .notice { bottom: calc(var(--count-keypad-space) + .5rem); }
body.count-keypad-open .count-form .count-save,
body.count-keypad-open .count-form[data-status=open] > .actions { display: none; }
body.count-keypad-open .count-form input:not([type=hidden]),
body.count-keypad-open .count-form select { scroll-margin-bottom: calc(var(--count-keypad-space) + .75rem); }

.count-keypad {
  position: fixed;
  z-index: 8;
  inset: auto 0 0;
  width: min(100%, 440px);
  max-height: var(--count-keypad-space, 260px);
  margin-inline: auto;
  padding: .4rem .55rem calc(.4rem + env(safe-area-inset-bottom, 0px));
  overflow-y: auto;
  overscroll-behavior: contain;
  background: var(--bg);
  border: 1px solid var(--line);
  border-bottom: 0;
  border-radius: 12px 12px 0 0;
  box-shadow: 0 -4px 24px #19273026;
}

.count-keypad-inner { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: .25rem; }
.count-keypad button {
  min-height: 46px;
  min-width: 0;
  padding: .2rem .35rem;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--paper);
  color: var(--ink);
  cursor: pointer;
}
.count-keypad .count-key { font-size: 1.13rem; font-weight: 700; }
.count-keypad button:active { background: var(--accent-soft); }
.count-keypad button:focus-visible { outline-offset: -3px; }
.count-keypad .count-keypad-action {
  grid-column: span 2;
  color: var(--paper);
  background: var(--accent);
  border-color: var(--accent);
  font-size: 1rem;
  font-weight: 720;
}
.count-keypad .count-keypad-action:active { background: var(--accent-hover); }

.mobile-count-toolbar { display: none; }

@media (max-width: 660px) {
  .count-list { gap: .45rem; }
  .count-card { padding: .55rem .65rem; gap: .4rem; border-radius: 8px; background: var(--paper); }
  .count-card:focus-within { border-color: var(--accent); }
  .count-card.flagged { background: var(--danger-soft); }
  .count-card.flagged:focus-within { border-color: var(--danger); }
  .count-card .count-info { gap: .2rem; }
  .count-title { gap: .32rem; }
  .count-name { font-size: 1rem; }
  .count-form, .entry-fields { gap: .4rem; }
  .count-form .fields { gap: .35rem; }
  .count-form .fields label { gap: .18rem; }
  .count-form .fields input[data-count-amount]:focus {
    border-color: var(--accent);
    background: var(--accent-soft);
  }
  .count-form .actions { gap: .35rem; }
  .count-form .actions .button { padding-inline: .65rem; }
  .count-keypad { background: #e9f0f1; }
  .count-keypad .count-key { font-size: 1.25rem; }
  .count-keypad .count-keypad-action { font-size: 1rem; }
}

@media (max-width: 660px), (max-height: 500px) and (pointer: coarse) {
  body.inventory-counting .shell { display: block; }
  body.inventory-counting :is(.side, .topbar, .inventory-overview, .count-list-heading, .location-count-heading, .desktop-count-filter) {
    display: none;
  }
  body.inventory-counting .view { padding-inline: .55rem; padding-block-start: 0; }
  body.inventory-counting .page-body { gap: .45rem; }
  body.inventory-counting .mobile-count-toolbar {
    display: grid;
    position: sticky;
    top: env(safe-area-inset-top, 0px);
    z-index: 4;
    gap: .15rem;
    margin-inline: -.55rem;
    margin-block-end: .45rem;
    padding: .4rem .65rem .25rem;
    background: var(--paper);
    border-bottom: 1px solid var(--line);
  }
  .mobile-count-primary {
    display: grid;
    grid-template-columns: minmax(0, 1fr) auto;
    align-items: center;
    gap: .5rem;
  }
  .mobile-count-identity h1 { font-size: 1.2rem; line-height: 1.2; letter-spacing: -.025em; overflow: hidden; white-space: nowrap; text-overflow: ellipsis; }
  .mobile-count-meta { display: flex; gap: .35rem; color: var(--muted); font-size: .8rem; white-space: nowrap; }
  .mobile-count-branch { overflow: hidden; text-overflow: ellipsis; }
  #mobile-count-progress { font-variant-numeric: tabular-nums; }
  #mobile-count-progress::before { content: '·'; margin-right: .35rem; }
  .mobile-count-controls { display: flex; align-items: center; justify-content: space-between; gap: .4rem; }
  .mobile-count-controls .check, .mobile-count-filter-toggle { flex: none; white-space: nowrap; }
  .mobile-count-controls .inventory-name-search { flex: 1 1 0; min-height: 44px; padding-inline: .5rem; font-size: 1rem; }
  .mobile-count-toolbar :is(.button, .quiet, .check) { min-height: 44px; }
  .mobile-count-toolbar .button { padding-inline: .65rem; }
  .mobile-count-filter-toggle[aria-expanded=true] {
    background: var(--accent-soft);
    color: var(--accent-hover);
  }
  body.inventory-counting .count-list-panel { border: 0; background: transparent; padding: 0; }
  body.inventory-counting .count-list { margin-top: .45rem; }
  body.inventory-counting .count-filter-options { display: none; scroll-margin-top: 6.75rem; }
  body.inventory-counting .count-filter-options.filters-open {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: .4rem;
    padding: .55rem;
    background: var(--paper);
    border: 1px solid var(--line);
    border-radius: 8px;
  }
  body.inventory-counting .count-filter-options label { min-width: 0; }
  body.inventory-counting:not(.count-keypad-open) .count-form[data-status=open] { grid-template-columns: minmax(0, 1fr) auto; }
  body.inventory-counting:not(.count-keypad-open) .count-form[data-status=open] .actions { grid-column: auto; }
  body.inventory-counting .count-save { min-height: 44px; }
  body.inventory-counting .count-form :is(input[data-count-amount], select[name=quarter]) { scroll-margin-top: 6.75rem; }
}

@media (max-height: 500px) and (orientation: landscape) {
  body.count-keypad-open {
    --count-keypad-space: min(calc(170px + env(safe-area-inset-bottom, 0px)), calc(100dvh - 5rem));
  }
  .count-keypad { width: min(100%, 600px); }
  .count-keypad-inner { grid-template-columns: repeat(6, minmax(0, 1fr)); }
  body.inventory-counting .mobile-count-toolbar { grid-template-columns: minmax(0, 1fr) auto; align-items: center; gap: .65rem; }
  body.inventory-counting .count-filter-options,
  body.inventory-counting .count-form :is(input[data-count-amount], select[name=quarter]) { scroll-margin-top: 4.25rem; }
}
