:root {
  --green: #1f6f5c;
  --green-dark: #17554a;
  --bg: #f2f5f4;
  --card: #ffffff;
  --text: #1c2523;
  --muted: #62706c;
  --border: #d5dedb;
  --danger: #b3261e;
  color-scheme: light;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  font-size: 16px;
}

[hidden] { display: none !important; }

h1, h2, h3 { margin: 0; }

.topbar {
  position: sticky;
  top: 0;
  z-index: 5;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: calc(10px + env(safe-area-inset-top)) 16px 10px;
  background: var(--green);
  color: #fff;
}
.topbar h1 { font-size: 1.2rem; }

main {
  max-width: 720px;
  margin: 0 auto;
  padding: 12px 12px calc(88px + env(safe-area-inset-bottom));
}

.card {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 14px;
  margin-bottom: 12px;
}

.login {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
}
.login .card { width: 100%; max-width: 380px; }
.login h1 { margin-bottom: 8px; }

label { display: block; margin: 10px 0 4px; font-size: 0.9rem; color: var(--muted); }

input, select, textarea, button {
  font: inherit;
  border-radius: 8px;
  border: 1px solid var(--border);
  padding: 10px 12px;
  background: #fff;
  color: var(--text);
}
input, select, textarea { width: 100%; }

button {
  cursor: pointer;
  min-height: 44px;
  font-weight: 600;
}
button.primary { background: var(--green); border-color: var(--green); color: #fff; }
button.primary:active { background: var(--green-dark); }
button.ghost { background: transparent; border-color: rgba(255, 255, 255, 0.6); color: #fff; min-height: 36px; }
button.danger { color: var(--danger); }
button:disabled { opacity: 0.55; cursor: default; }

.row { display: flex; gap: 8px; align-items: center; margin-top: 8px; }
.row > input { flex: 1; min-width: 0; width: auto; }
.row > button { flex-shrink: 0; }
.row > button.grow { flex: 1; }

.muted { color: var(--muted); font-size: 0.9rem; margin: 8px 0 0; }
.error { color: var(--danger); margin: 8px 0 0; font-size: 0.9rem; }

.filters { display: grid; gap: 8px; }

.scanner {
  position: relative;
  border-radius: 10px;
  overflow: hidden;
  background: #000;
  margin-bottom: 8px;
  aspect-ratio: 4 / 3;
}
.scanner video { width: 100%; height: 100%; object-fit: cover; }
.scanner-frame {
  position: absolute;
  inset: 25% 10%;
  border: 3px solid rgba(255, 255, 255, 0.85);
  border-radius: 10px;
  box-shadow: 0 0 0 999px rgba(0, 0, 0, 0.35);
  pointer-events: none;
}

.product-head { display: flex; justify-content: space-between; gap: 8px; align-items: flex-start; }
.product-head h3 { font-size: 1.05rem; }
.badge {
  display: inline-block;
  padding: 2px 8px;
  border-radius: 999px;
  background: #e1efeb;
  color: var(--green-dark);
  font-size: 0.78rem;
  font-weight: 600;
  white-space: nowrap;
}
.total { font-size: 1.4rem; font-weight: 700; color: var(--green-dark); }

.stock-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 8px 0;
  border-top: 1px solid var(--border);
}
.stock-row .name { flex: 1; min-width: 0; overflow-wrap: anywhere; }
.stepper { display: flex; align-items: center; gap: 6px; }
.stepper button { min-width: 44px; padding: 8px; }
.stepper .qty { min-width: 2.4em; text-align: center; font-weight: 700; }

.add-stock { display: grid; grid-template-columns: 90px 1fr; gap: 8px; margin-top: 10px; }
.add-stock select { grid-column: 1 / -1; }

.item { cursor: pointer; }
.item:active { background: #f0f6f4; }

.banner { display: flex; align-items: center; justify-content: space-between; gap: 8px; background: #fff7df; border-color: #e6cf85; }
code { background: #eef2f1; padding: 1px 5px; border-radius: 4px; }
#import-file { margin-top: 10px; }
.catalog-item { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.catalog-item .info { min-width: 0; overflow-wrap: anywhere; }

.tabbar {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  display: flex;
  background: #fff;
  border-top: 1px solid var(--border);
  padding-bottom: env(safe-area-inset-bottom);
  z-index: 6;
}
.tabbar button {
  flex: 1;
  border: 0;
  border-radius: 0;
  min-height: 56px;
  background: transparent;
  color: var(--muted);
}
.tabbar button.active { color: var(--green); border-top: 3px solid var(--green); }

.fab {
  position: fixed;
  right: 18px;
  bottom: calc(76px + env(safe-area-inset-bottom));
  width: 56px;
  height: 56px;
  border-radius: 50%;
  background: var(--green);
  border-color: var(--green);
  color: #fff;
  font-size: 1.8rem;
  line-height: 1;
  padding: 0;
  box-shadow: 0 3px 10px rgba(0, 0, 0, 0.3);
  z-index: 7;
}

dialog {
  width: min(92vw, 460px);
  max-height: 90vh;
  border: 0;
  border-radius: 14px;
  padding: 16px;
}
dialog::backdrop { background: rgba(0, 0, 0, 0.5); }
dialog h2 { margin-bottom: 4px; }

#toast {
  position: fixed;
  left: 50%;
  bottom: calc(80px + env(safe-area-inset-bottom));
  transform: translateX(-50%);
  background: #222;
  color: #fff;
  padding: 10px 16px;
  border-radius: 8px;
  max-width: 90vw;
  z-index: 20;
}
#toast.error-toast { background: var(--danger); }

/* Monthly inventory and reporting screens */
#app-view main { max-width: 1180px; }
.tabbar { overflow-x: auto; }
.tabbar button { min-width: 84px; }
.metric-grid { display:grid; grid-template-columns:repeat(4,minmax(130px,1fr)); gap:10px; }
.metric { margin-bottom:12px; }
.metric span { color:var(--muted); font-size:.82rem; }
.metric strong { display:block; margin-top:7px; color:var(--green-dark); font-size:1.45rem; }
.section-row { display:flex; align-items:center; justify-content:space-between; gap:12px; }
.report-table { overflow:auto; }
.report-table table { width:100%; border-collapse:collapse; white-space:nowrap; }
.report-table th,.report-table td { text-align:left; border-bottom:1px solid var(--border); padding:10px; font-size:.84rem; }
.report-table th { color:var(--muted); font-size:.74rem; text-transform:uppercase; background:#f7faf9; position:sticky; top:0; z-index:1; }
.report-filters { grid-template-columns:repeat(4,minmax(130px,1fr)); align-items:end; }
.report-filters button { align-self:end; }
.count-table { max-height:65vh; }
.count-table input { width:84px; padding:7px; }
.count-table .notes-input { width:180px; }
.count-table tr.dirty { background:#fff8df; }
.status { display:inline-block; border-radius:999px; padding:3px 8px; font-size:.72rem; font-weight:700; text-transform:uppercase; }
.status.open,.status.ok { background:#e1efeb; color:var(--green-dark); }
.status.closed { background:#e7ecea; color:var(--muted); }
.status.review { background:#fce4d6; color:#8a4300; }
.report-list { display:grid; gap:12px; margin-top:14px; }
.report-bar { display:grid; grid-template-columns:100px 1fr 95px; gap:8px; align-items:center; font-size:.82rem; }
.report-bar i { height:11px; background:#e1e9e6; border-radius:99px; overflow:hidden; }
.report-bar b { display:block; height:100%; background:var(--green); border-radius:99px; }
.inline-check { display:flex; align-items:center; gap:8px; margin:0; }
.inline-check input { width:auto; }
.trend-bars { min-height:230px; display:flex; align-items:flex-end; gap:12px; overflow-x:auto; padding:20px 4px 0; }
.trend-column { min-width:88px; height:210px; display:flex; flex-direction:column; align-items:center; justify-content:flex-end; gap:7px; font-size:.76rem; }
.trend-column i { width:48px; background:linear-gradient(var(--green),var(--green-dark)); border-radius:6px 6px 0 0; }
.trend-column span { white-space:nowrap; }
@media(max-width:760px){.metric-grid{grid-template-columns:1fr 1fr}.report-filters{grid-template-columns:1fr 1fr}.count-table{max-height:none}.report-bar{grid-template-columns:82px 1fr 82px}.tabbar button{min-width:76px;font-size:.76rem}}
.scan-count-controls{margin-top:12px;padding-top:10px;border-top:1px solid var(--border)}
.scan-count-options{display:grid;grid-template-columns:1fr 1fr 110px;gap:8px;margin-top:8px;align-items:end}
.scan-count-options label{margin:0}.scan-count-options input{width:100%}.scan-count-success{border-left:4px solid var(--green)}
@media(max-width:600px){.scan-count-options{grid-template-columns:1fr 1fr}.scan-count-options label{grid-column:1/-1}}
.scan-count-prompt label{font-weight:700;margin-top:14px}.scan-count-prompt>input{font-size:1.35rem;text-align:center}.finalize-count{width:100%;margin-top:10px;background:#fff;border:2px solid var(--green);color:var(--green-dark)}.scan-next-actions{flex-wrap:wrap}.count-finalized{border-left:4px solid var(--green)}
.count-actions{display:flex;gap:6px}.count-actions button{min-height:34px;padding:6px 9px}.count-table tr.editing{outline:2px solid rgba(31,111,92,.25);outline-offset:-2px}.count-table input:disabled{background:#f4f7f6;color:var(--text);opacity:1}.count-actions .danger{background:#fff;border-color:#d8aaa6}

/* Start Count dialog: mobile-first, no horizontal scrolling */
#count-workflow-dialog {
  width: min(94vw, 520px);
  max-width: 94vw;
  max-height: min(88vh, 760px);
  margin: auto;
  padding: 0;
  overflow: hidden;
}
#count-workflow-dialog .count-workflow-card {
  width: 100%;
  min-width: 0;
  max-width: 100%;
  max-height: min(88vh, 760px);
  padding: 16px;
  overflow-x: hidden;
  overflow-y: auto;
}
.count-workflow-head { min-width: 0; }
.count-workflow-head > div { min-width: 0; flex: 1; }
.count-workflow-head h2,
.count-workflow-head p,
.count-location-choice span,
.count-location-choice strong { overflow-wrap: anywhere; word-break: normal; }
.count-workflow-locations { width: 100%; min-width: 0; }
.count-location-choice {
  width: 100%;
  min-width: 0;
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 10px;
}
.count-location-choice strong { text-align: right; white-space: normal; }
.count-workflow-actions { width: 100%; flex-wrap: wrap; }
.count-workflow-actions button { max-width: 100%; }

@media (max-width: 600px) {
  #count-workflow-dialog {
    width: calc(100vw - 20px);
    max-width: calc(100vw - 20px);
    max-height: calc(100dvh - 24px);
  }
  #count-workflow-dialog .count-workflow-card {
    max-height: calc(100dvh - 24px);
    padding: 14px;
  }
  .count-workflow-locations { gap: 7px; margin: 14px 0; }
  .count-location-choice {
    grid-template-columns: 1fr;
    gap: 4px;
    padding: 11px 12px;
  }
  .count-location-choice strong { text-align: left; }
  .count-workflow-actions {
    display: grid;
    grid-template-columns: 1fr;
  }
  .count-workflow-actions button { width: 100%; }
}

/* Compact top-left dropdown navigation */
#app-view { overflow-x: hidden; }
.topbar { position: relative; justify-content: flex-start; gap: 10px; }
.topbar h1 { margin: 0; min-width: 0; overflow-wrap: anywhere; }
.top-left-controls { position: relative; flex: 0 0 auto; }
.menu-trigger { min-height: 40px; padding: 8px 12px; background: var(--green); color: #fff; border-color: var(--green); }
.main-menu-panel { position: absolute; top: calc(100% + 7px); left: 0; z-index: 50; width: min(260px, calc(100vw - 24px)); max-height: calc(100dvh - 90px); overflow-y: auto; overflow-x: hidden; padding: 8px; border: 1px solid var(--border); border-radius: 12px; background: var(--card); box-shadow: 0 14px 34px rgba(0,0,0,.16); }
.main-menu-panel button { display: block; width: 100%; min-height: 42px; padding: 9px 11px; text-align: left; background: transparent; color: var(--text); border: 0; border-radius: 8px; }
.main-menu-panel button:hover,.main-menu-panel button.active { background: #e8f1ee; color: var(--green-dark); }
.menu-divider { height: 1px; margin: 6px 4px; background: var(--border); }
.tabbar { display: none !important; }
#app-view main { width: 100%; max-width: 1180px; min-width: 0; padding-bottom: 24px; }
.card,.filters,.row,.report-table,.count-table { min-width: 0; max-width: 100%; }
.report-table { overflow-x: auto; -webkit-overflow-scrolling: touch; }
button,input,select,textarea { max-width: 100%; }
@media(max-width:600px){.topbar{padding-left:10px;padding-right:10px}.main-menu-panel{position:fixed;top:64px;left:10px;width:calc(100vw - 20px);max-height:calc(100dvh - 76px)}#app-view main{padding-left:10px;padding-right:10px}.topbar h1{font-size:1.05rem}}
.location-manage-row{display:grid;grid-template-columns:minmax(0,1fr) auto;gap:12px;align-items:center}.location-manage-row strong{overflow-wrap:anywhere}.location-actions{display:flex;gap:7px}.location-actions button{min-height:38px;padding:7px 11px}.location-actions .danger{background:#fff;color:#a42b22;border-color:#d8aaa6}@media(max-width:520px){.location-manage-row{grid-template-columns:1fr}.location-actions{display:grid;grid-template-columns:1fr 1fr}.location-actions button{width:100%}}
