:root {
  --bg: #f6f7f8;
  --surface: #ffffff;
  --text: #1b1f23;
  --muted: #5a6470;
  --border: #d9dde2;
  --primary: #bedc3c;
  --primary-fg: #1b1f23;
  --ok: #1b7f3a;
  --bad: #b3261e;
  --warn: #b86f00;
  --warn-bg: #fff7e6;
  --shadow: 0 1px 2px rgba(0, 0, 0, .05), 0 1px 1px rgba(0, 0, 0, .04);
  --radius: 12px;
  --pad: 14px;
  --gap: 12px;
  --tap: 44px;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #14171a;
    --surface: #1c2024;
    --text: #e8eaed;
    --muted: #98a1ac;
    --border: #2c333a;
    --primary: #bedc3c;
    --primary-fg: #1b1f23;
    --ok: #4cc079;
    --bad: #f47269;
    --warn: #ffb454;
    --warn-bg: #2c2516;
    --shadow: 0 1px 2px rgba(0, 0, 0, .4);
  }
}

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

html, body { margin: 0; padding: 0; height: 100%; }

body {
  background: var(--bg);
  color: var(--text);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  font-size: 16px;
  line-height: 1.4;
  padding-bottom: env(safe-area-inset-bottom);
  overflow-x: hidden;
}

main {
  max-width: 720px;
  margin: 0 auto;
  padding: var(--pad);
}

h1 { font-size: 1.4rem; margin: 0 0 var(--gap); }

.mov-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}
.mov-head h1 { margin-bottom: 0; }
h2 { font-size: 1.1rem; margin: 0 0 6px; }

p { margin: 6px 0; }

code {
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: .95em;
  background: var(--bg);
  padding: 1px 6px;
  border-radius: 6px;
  border: 1px solid var(--border);
}

/* --- Header --- */

#appHeader {
  background: #000000;
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 10;
  padding-top: env(safe-area-inset-top);
}

.header-row {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px var(--pad);
}

.header-row .logo {
  height: 44px;
  width: auto;
  display: block;
  flex: 0 0 auto;
  filter: brightness(0) invert(1);
}
.header-row .spacer { flex: 1 1 auto; min-width: 0; }
.header-row #userLabel {
  color: #ffffff;
  font-size: .85rem;
  opacity: .9;
  flex: 0 1 auto;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.header-row #logoutBtn {
  background: rgba(255, 255, 255, .18);
  color: #ffffff;
  box-shadow: none;
  flex: 0 0 auto;
  padding: 6px 12px;
}

.conn {
  font-size: 1rem;
  line-height: 1;
}
.conn.online    { color: var(--ok); }
.conn.connecting{ color: var(--warn); }
.conn.offline   { color: var(--bad); }

.tabs {
  display: flex;
  background: var(--surface);
  border-top: 1px solid var(--border);
}
.tabs .tab {
  flex: 1 1 0;
  min-width: 0;
  min-height: var(--tap);
  padding: 10px 4px;
  background: transparent;
  border: none;
  border-bottom: 2px solid transparent;
  color: var(--text);
  font: inherit;
  font-size: .8rem;
  line-height: 1.15;
  text-align: center;
  cursor: pointer;
}
.tabs .tab.active {
  color: var(--primary);
  border-bottom-color: var(--primary);
  font-weight: 600;
}

/* --- Forms --- */

label {
  display: block;
  margin-bottom: var(--gap);
  color: var(--muted);
  font-size: .9rem;
}

input, select, textarea, button {
  font: inherit;
  color: var(--text);
}

input, select, textarea {
  display: block;
  width: 100%;
  min-height: var(--tap);
  padding: 8px 12px;
  margin-top: 4px;
  background: var(--surface);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: var(--radius);
}

input:focus, select:focus, textarea:focus {
  outline: 2px solid var(--primary);
  outline-offset: 1px;
}

button {
  min-height: var(--tap);
  min-width: var(--tap);
  padding: 8px 16px;
  background: var(--primary);
  color: var(--primary-fg);
  border: none;
  border-radius: var(--radius);
  cursor: pointer;
  font-weight: 500;
  box-shadow: var(--shadow);
}
button:active { transform: translateY(1px); }
button:disabled { opacity: .55; cursor: default; }

button.ok  { background: var(--primary); color: var(--primary-fg); }
button.bad { background: #ba256e; color: #ffffff; }
button.bw {
  background: #000000;
  color: #ffffff;
  border: 1px solid #ffffff;
}
button.bw:hover {
  background: #ffffff;
  color: #000000;
}

button[data-act] {
  background: var(--surface);
  color: var(--text);
  border: 1px solid var(--border);
  box-shadow: none;
  padding: 6px 12px;
  font-weight: 400;
}

.row {
  display: flex;
  flex-wrap: wrap;
  gap: 12px;
  margin-top: 12px;
}

.big-action {
  width: 100%;
  font-size: 1.1rem;
  padding: 18px;
  margin: var(--gap) 0;
}

.filters {
  display: grid;
  grid-template-columns: 1fr 1fr auto;
  gap: 8px;
  align-items: end;
  margin-bottom: var(--gap);
}
.filters label { margin-bottom: 0; }
.filters button { align-self: end; }

/* --- Cards --- */

.cards {
  list-style: none;
  margin: 0;
  padding: 0;
  display: grid;
  gap: var(--gap);
}

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: var(--pad);
  box-shadow: var(--shadow);
}

.card-head {
  display: flex;
  align-items: center;
  gap: 8px;
  justify-content: space-between;
}

.meta {
  color: var(--muted);
  font-size: .9rem;
}

.card.low {
  border-color: var(--warn);
  background: var(--warn-bg);
}

.card .warn {
  background: var(--warn);
  color: #1b1f23;
  font-size: .75rem;
  padding: 2px 8px;
  border-radius: 999px;
  font-weight: 600;
}

.empty {
  text-align: center;
  color: var(--muted);
  padding: var(--pad);
}

.mov .delta {
  font-weight: 700;
  font-variant-numeric: tabular-nums;
}
.mov.ok .delta { color: var(--ok); }
.mov.bad .delta { color: var(--bad); }

/* --- Login --- */

#loginView {
  max-width: 360px;
  margin: 10vh auto 0;
}

.login-logo {
  display: block;
  width: 280px;
  max-width: 90%;
  height: auto;
  margin: 0 auto var(--gap);
}

/* --- Quick: scanner --- */

.scanner-box {
  margin: var(--gap) 0;
  border-radius: var(--radius);
  overflow: hidden;
  background: black;
  position: relative;
}
.scanner-box video {
  display: block;
  width: 100%;
  max-height: 60vh;
  object-fit: cover;
}
.scanner-box button {
  position: absolute;
  bottom: 10px;
  left: 50%;
  transform: translateX(-50%);
}

.scan-kind {
  font-size: .95rem;
  margin: 8px 0;
}

.msg {
  color: var(--muted);
  font-size: .95rem;
  min-height: 1.4em;
}
.error {
  color: var(--bad);
  font-size: .9rem;
}
p.warn {
  color: var(--warn);
  font-weight: 600;
}

/* --- Dialog --- */

dialog {
  border: none;
  border-radius: var(--radius);
  padding: var(--pad);
  background: var(--surface);
  color: var(--text);
  max-width: 92vw;
  width: 420px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, .25);
}
dialog::backdrop { background: rgba(0, 0, 0, .4); }

.dialog-actions {
  display: flex;
  gap: 12px;
  justify-content: flex-end;
  margin: calc(var(--gap) + 6px) 0 0;
  padding: 0;
}

/* --- Responsiveness --- */

@media (max-width: 480px) {
  .filters { grid-template-columns: 1fr 1fr; }
  .filters button { grid-column: span 2; }
}
