:root {
  --ink: #0f172a;
  --ink-muted: #64748b;
  --paper: #f8fafc;
  --navy: #0f2e42;
  --navy-light: #1e4963;
  --brass: #b45309;
  --rust: #c2410c;
  --ok: #15803d;
  --line: #e2e8f0;
  --card: #ffffff;
  --field: #f1f5f9;
  --danger: #b91c1c;
  --accent: #0284c7;
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  background: var(--paper);
  color: var(--ink);
  line-height: 1.5;
}
.hidden { display: none !important; }
.kicker { letter-spacing: 0.08em; text-transform: uppercase; font-size: 0.72rem; font-weight: 700; color: var(--brass); margin: 0 0 0.4rem; }
h1, h2, h3 { font-weight: 600; margin: 0 0 0.75rem; color: var(--navy); }
button, input, select, textarea {
  font: inherit;
  border-radius: 8px;
  border: 1px solid #cbd5e1;
  padding: 0.65rem 0.85rem;
  background: #ffffff;
  color: var(--ink);
}
button, select { cursor: pointer; }
input:focus, select:focus, textarea:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(2,132,199,0.15);
}
button {
  background: var(--card);
  transition: all 0.15s ease-in-out;
  font-weight: 500;
}
button:hover:not(:disabled) {
  background: #f1f5f9;
  border-color: #94a3b8;
}
button.primary, a.primary {
  background: var(--navy);
  color: #ffffff;
  border-color: var(--navy);
}
a.primary {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  text-decoration: none;
}
button.primary:hover:not(:disabled) {
  background: var(--navy-light);
}
button.success {
  background: var(--ok);
  color: #ffffff;
  border-color: var(--ok);
}
button.danger {
  background: var(--danger);
  color: #ffffff;
  border-color: var(--danger);
}
button.huge {
  min-height: 4rem;
  font-size: 1.15rem;
  font-weight: 700;
  width: 100%;
  letter-spacing: 0.03em;
  border-radius: 10px;
}
label { display: flex; flex-direction: column; gap: 0.35rem; font-size: 0.88rem; font-weight: 500; color: #334155; }
.hint { color: var(--ink-muted); font-size: 0.85rem; }
.err { color: var(--danger); font-size: 0.88rem; font-weight: 500; min-height: 1.2rem; }

/* Login Shell */
.login-shell {
  min-height: 100%;
  display: grid;
  place-items: center;
  background: radial-gradient(circle at 50% 20%, rgba(15,46,66,0.08), transparent 70%), var(--paper);
  padding: 1.5rem;
}
.login-card {
  width: min(440px, 100%);
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 12px;
  box-shadow: 0 10px 25px -5px rgba(0,0,0,0.05), 0 8px 10px -6px rgba(0,0,0,0.01);
  padding: 2rem;
  display: grid;
  gap: 1rem;
}

/* App Layout */
.app { display: grid; grid-template-columns: 240px 1fr; min-height: 100%; }
aside {
  background: var(--navy);
  color: #f8fafc;
  padding: 1.25rem 1rem;
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
}
.brand strong { font-size: 1.15rem; font-weight: 700; letter-spacing: 0.02em; }
.brand small { display: block; color: #94a3b8; margin-top: 0.25rem; font-size: 0.8rem; }
nav { display: grid; gap: 0.25rem; flex: 1; align-content: start; }
nav a {
  color: #cbd5e1;
  text-decoration: none;
  padding: 0.6rem 0.75rem;
  border-radius: 8px;
  font-size: 0.92rem;
  font-weight: 500;
  transition: all 0.15s ease;
}
nav a.active, nav a:hover { background: rgba(255,255,255,0.12); color: #ffffff; }
#logout { background: transparent; color: #e2e8f0; border: 1px solid rgba(255,255,255,0.2); }
#logout:hover { background: rgba(255,255,255,0.1); }

.lang-switch {
  display: inline-flex;
  gap: 0;
  border: 1px solid rgba(255,255,255,0.25);
  border-radius: 8px;
  overflow: hidden;
  align-self: stretch;
}
.login-lang {
  justify-self: end;
  border-color: var(--line);
}
.lang-btn {
  min-width: 2.6rem;
  padding: 0.4rem 0.65rem;
  border: none;
  border-radius: 0;
  background: transparent;
  color: inherit;
  font-weight: 700;
  letter-spacing: 0.04em;
}
.lang-switch .lang-btn:hover:not(:disabled) { background: rgba(255,255,255,0.12); }
.login-lang .lang-btn { color: var(--navy); }
.login-lang .lang-btn:hover:not(:disabled) { background: var(--field); }
.lang-btn.on {
  background: #ffffff;
  color: var(--navy);
}
.login-lang .lang-btn.on {
  background: var(--navy);
  color: #ffffff;
}
main { padding: 1.5rem 2rem 3rem; max-width: 1280px; width: 100%; }

/* Stats & Cards */
.stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(180px, 1fr)); gap: 0.85rem; margin-bottom: 1.25rem; }
.stat, .card {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 10px;
  box-shadow: 0 1px 3px rgba(0,0,0,0.02);
  padding: 1rem 1.25rem;
}
.stat span { font-size: 0.85rem; color: var(--ink-muted); font-weight: 500; }
.stat b { display: block; font-size: 1.85rem; font-weight: 700; color: var(--navy); margin-top: 0.2rem; }
.row { display: flex; gap: 0.6rem; flex-wrap: wrap; align-items: end; }
.grid-form { display: grid; grid-template-columns: repeat(auto-fit, minmax(190px, 1fr)); gap: 0.75rem; }

/* Tables */
table { width: 100%; border-collapse: collapse; font-size: 0.9rem; }
th { text-align: left; color: var(--ink-muted); font-weight: 600; font-size: 0.8rem; text-transform: uppercase; letter-spacing: 0.04em; padding: 0.65rem 0.6rem; border-bottom: 2px solid var(--line); }
td { padding: 0.65rem 0.6rem; border-bottom: 1px solid #f1f5f9; vertical-align: middle; }
tr:hover td { background: #f8fafc; }

/* Status Badges */
.pill { display: inline-flex; align-items: center; padding: 0.2rem 0.65rem; border-radius: 9999px; background: var(--field); font-size: 0.76rem; font-weight: 600; }
.pill.REGISTERED { background: #f1f5f9; color: #475569; }
.pill.GATE_IN { background: #e0f2fe; color: #0369a1; }
.pill.WEIGHED_1 { background: #ede9fe; color: #6d28d9; }
.pill.TALLY_CONFIRMED { background: #fef3c7; color: #b45309; }
.pill.WEIGHED_2 { background: #ccfbf1; color: #0f766e; }
.pill.GATE_OUT { background: #dcfce7; color: #15803d; }
.pill.HOLD { background: #fee2e2; color: #b91c1c; }

/* Berth Workspaces */
.berth-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 0.75rem; margin-bottom: 1rem; }
.berth-card {
  padding: 1rem;
  border-radius: 10px;
  border: 2px solid var(--line);
  background: var(--card);
  text-align: center;
  transition: all 0.15s ease;
}
.berth-card.FREE { border-color: #86efac; background: #f0fdf4; }
.berth-card.LOCKED { border-color: #93c5fd; background: #eff6ff; }
.berth-card.HOLD { border-color: #fca5a5; background: #fef2f2; }
.berth-card h3 { margin: 0 0 0.35rem; font-size: 1.15rem; }
.berth-badge { display: inline-block; font-size: 0.75rem; font-weight: 700; padding: 0.2rem 0.5rem; border-radius: 6px; }
.berth-badge.FREE { background: #22c55e; color: #fff; }
.berth-badge.LOCKED { background: #3b82f6; color: #fff; }
.berth-badge.HOLD { background: #ef4444; color: #fff; }

/* Vehicle Waiting Queue */
.queue-list { display: grid; gap: 0.5rem; max-height: 240px; overflow-y: auto; margin: 0.5rem 0; }
.queue-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0.75rem 1rem;
  background: #f8fafc;
  border: 1px solid var(--line);
  border-radius: 8px;
  cursor: pointer;
  transition: all 0.15s ease;
}
.queue-item:hover { background: #e0f2fe; border-color: #7dd3fc; }
.queue-item.active { background: #0f2e42; color: #fff; border-color: #0f2e42; }
.plate-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(9.5rem, 1fr));
  gap: 0.65rem;
  margin: 0.75rem 0 1.25rem;
}
.plate-btn {
  min-height: 5.2rem;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  gap: 0.25rem;
  font-size: 1.05rem;
  border-width: 2px;
}
.plate-btn.on { background: var(--navy); color: #fff; border-color: var(--navy); }
.plate-btn.on small { color: #e2e8f0; }
.berth-card.on { outline: 2px solid var(--navy); }
.berth-lock.berth-card { width: 100%; cursor: pointer; }
.berth-lock.berth-card:disabled { opacity: 0.55; cursor: not-allowed; }

/* Variance Alerts Banner */
.alert-box {
  background: #fffbeb;
  border: 1px solid #fde68a;
  border-left: 4px solid #f59e0b;
  border-radius: 8px;
  padding: 0.85rem 1rem;
  margin-bottom: 1rem;
}
.alert-box.danger {
  background: #fef2f2;
  border-color: #fecaca;
  border-left-color: #ef4444;
}

/* Field Tally Tablet Mode */
.field-shell { max-width: 680px; margin: 0 auto; }
.field-shell h1 { font-size: 1.85rem; }
.chips { display: flex; flex-wrap: wrap; gap: 0.5rem; margin: 0.6rem 0; }
.chips button { min-width: 4.5rem; min-height: 3.2rem; font-weight: 600; }
.chips button.on { background: var(--navy); color: #ffffff; border-color: var(--navy); }
.fact { font-size: 1.05rem; margin: 0.4rem 0; color: #334155; }
body.tally-mode .app { grid-template-columns: 1fr; }
body.tally-mode aside { display: none; }
body.tally-mode main { background: var(--field); padding: 1rem; }
.topbar-mini {
  display: flex; justify-content: space-between; align-items: center; margin-bottom: 1rem;
}

button, input, select, textarea, a.primary, nav a {
  min-height: 48px;
  min-width: 44px;
}

.aside-top { display: flex; flex-direction: column; gap: 0.35rem; }
.nav-toggle {
  display: none;
  align-items: center;
  justify-content: center;
  min-width: 48px;
  min-height: 48px;
  font-size: 1.35rem;
  background: rgba(255,255,255,0.12);
  color: #fff;
  border-color: rgba(255,255,255,0.25);
}
button.is-busy, a.is-busy {
  opacity: 0.82;
  pointer-events: none;
}
button.is-busy::after {
  content: "";
  display: inline-block;
  width: 0.9rem;
  height: 0.9rem;
  margin-left: 0.45rem;
  border: 2px solid currentColor;
  border-right-color: transparent;
  border-radius: 50%;
  vertical-align: -0.1rem;
  animation: spin 0.55s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

.berth-rail { margin-bottom: 1.25rem; }
.kd-wizard { position: relative; }

@media (min-width: 801px) {
  .berth-rail {
    position: sticky;
    top: 0;
    z-index: 4;
    background: var(--paper);
    padding-bottom: 0.25rem;
  }
  .berth-grid { grid-template-columns: repeat(3, 1fr); }
  main { max-width: 1280px; }
}

@media (max-width: 800px) {
  .app { grid-template-columns: 1fr; }
  aside {
    position: sticky;
    top: 0;
    z-index: 30;
    flex-direction: column;
    flex-wrap: nowrap;
    padding: 0.65rem 0.75rem;
    gap: 0.5rem;
  }
  .aside-top {
    flex-direction: row;
    align-items: center;
    justify-content: space-between;
  }
  .nav-toggle { display: inline-flex; }
  nav.nav-scroll {
    display: flex;
    flex-wrap: nowrap;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    gap: 0.35rem;
    padding-bottom: 0.15rem;
  }
  nav.nav-scroll a {
    flex: 0 0 auto;
    white-space: nowrap;
    display: inline-flex;
    align-items: center;
  }
  aside.nav-open nav.nav-scroll {
    display: grid;
    grid-template-columns: 1fr 1fr;
    overflow: visible;
  }
  .stats { grid-template-columns: 1fr; }
  .berth-grid { grid-template-columns: 1fr; }
  .grid-form { grid-template-columns: 1fr; }
  main { padding: 1rem; padding-bottom: 5.5rem; }
  .card { overflow-x: auto; -webkit-overflow-scrolling: touch; }
  .stack-table thead { display: none; }
  .stack-table tbody tr {
    display: block;
    border: 1px solid var(--line);
    border-radius: 10px;
    background: var(--card);
    margin: 0 0 0.65rem;
    padding: 0.35rem 0.15rem;
    box-shadow: 0 1px 3px rgba(0,0,0,0.04);
  }
  .stack-table tbody td {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 0.75rem;
    border-bottom: 0;
    padding: 0.45rem 0.7rem;
  }
  .stack-table tbody td::before {
    content: attr(data-label);
    font-size: 0.75rem;
    font-weight: 700;
    color: var(--ink-muted);
    text-transform: uppercase;
    letter-spacing: 0.04em;
    flex: 0 0 42%;
  }
  .kd-wizard .kd-slice {
    max-height: calc(100dvh - 16rem);
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
  }
  .kd-nav {
    padding-bottom: calc(0.75rem + env(safe-area-inset-bottom, 0px));
  }
}

.kd-steps {
  display: flex;
  gap: 0.35rem;
  overflow-x: auto;
  padding-bottom: 0.35rem;
  margin-bottom: 0.75rem;
}
.kd-steps button {
  flex: 1 0 auto;
  min-width: 4.4rem;
  min-height: 48px;
  font-size: 0.78rem;
  padding: 0.55rem 0.4rem;
}
.kd-steps button.on { background: var(--navy); color: #fff; border-color: var(--navy); }
.kd-slice { display: grid; gap: 0.75rem; }
.kd-row {
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 0.85rem;
  background: #fff;
  display: grid;
  gap: 0.65rem;
}
.kd-nav {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0.5rem;
  position: sticky;
  bottom: 0;
  background: var(--paper);
  padding: 0.75rem 0 0.25rem;
  z-index: 5;
}
.kd-review dt { color: var(--ink-muted); font-size: 0.8rem; }
.kd-review dd { margin: 0 0 0.55rem; font-weight: 600; }
.checkpoint-split { display: grid; gap: 1rem; margin-bottom: 1.25rem; }
@media (min-width: 800px) {
  .checkpoint-split { grid-template-columns: 1fr 1fr; }
}
.checkpoint-card h2 { font-size: 1.35rem; }
.anpr-strip {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.5rem;
  margin: 0 0 1rem;
}
.anpr-chip {
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 1.35rem;
  font-weight: 800;
  letter-spacing: 0.08em;
  min-height: 48px;
  padding: 0.45rem 0.9rem;
  background: var(--navy);
  color: #fff;
  border-color: var(--navy);
}
.plate-input {
  font-size: 1.25rem;
  font-weight: 700;
  letter-spacing: 0.06em;
  text-transform: uppercase;
}
.plate-input::placeholder {
  text-transform: none;
  font-weight: 500;
  letter-spacing: 0;
  opacity: 0.7;
}
#weigh-kg {
  font-size: 2rem;
  font-weight: 800;
  min-height: 64px;
}
.weigh-actions {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0.75rem;
  margin-top: 1rem;
}
.weigh-actions button.on { outline: 3px solid var(--accent); }
.util-reading {
  font-size: 2rem;
  font-weight: 800;
  min-height: 64px;
}
.settle-steps { overflow-x: auto; }
.settle-card table { width: 100%; }
.settle-card th { text-align: left; }
.pill.LIVE { background: #dcfce7; color: #166534; }
.pill.OFF { background: #fee2e2; color: #991b1b; }
.table-wrap { overflow-x: auto; -webkit-overflow-scrolling: touch; width: 100%; }
.kd-steps button { min-height: 48px; }
@media (max-width: 800px) {
  .card { overflow-x: auto; -webkit-overflow-scrolling: touch; }
}

.modal-back {
  position: fixed;
  inset: 0;
  background: rgba(15, 23, 42, 0.45);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 50;
  padding: 1rem;
}
.modal-card {
  background: #fff;
  padding: 1.25rem;
  border-radius: 12px;
  width: min(480px, 100%);
  box-shadow: 0 16px 40px rgba(15, 23, 42, 0.2);
}
.email-suffix {
  display: flex;
  align-items: center;
  gap: 0.35rem;
}
.email-suffix input { flex: 1; }
.email-suffix em { color: var(--ink-muted); font-style: normal; white-space: nowrap; }
.pill.ACTIVE { background: #dcfce7; color: #166534; }
.pill.LOCKED { background: #fee2e2; color: #991b1b; }

