:root {
  --bg: #181411;
  --panel: rgba(255, 255, 255, 0.08);
  --panel-strong: rgba(255, 255, 255, 0.12);
  --line: rgba(255, 255, 255, 0.16);
  --text: #fff8ef;
  --muted: rgba(255, 248, 239, 0.68);
  --tomato: #e34d36;
  --olive: #9bb86f;
  --steel: #8ca7b7;
  --fire: #f0a34a;
  --wine: #7f2d2d;
  --shadow: 0 20px 60px rgba(0, 0, 0, 0.38);
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html,
body {
  height: 100%;
  min-height: 100%;
  overflow: hidden;
}
body {
  margin: 0;
  color: var(--text);
  background:
    linear-gradient(120deg, rgba(140, 167, 183, 0.22), transparent 34%),
    linear-gradient(245deg, rgba(227, 77, 54, 0.16), transparent 36%),
    var(--bg);
  font-family: "Aptos", "Segoe UI", system-ui, sans-serif;
}

button,
input,
select { font: inherit; }
button { cursor: pointer; }
h1,
h2,
h3,
p { margin: 0; }
a {
  color: inherit;
  text-decoration: none;
}

.shell {
  width: 100%;
  max-width: none;
  height: 100vh;
  height: 100dvh;
  min-height: 0;
  margin: 0 auto;
  padding: 14px;
  overflow: hidden;
}

.login-view {
  height: calc(100vh - 28px);
  height: calc(100dvh - 28px);
  min-height: 0;
  display: grid;
  place-items: center;
  overflow: auto;
}

.login-card {
  width: min(470px, 100%);
  display: grid;
  gap: 18px;
  padding: 26px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: linear-gradient(180deg, rgba(24, 20, 17, 0.92), rgba(15, 12, 11, 0.96));
  box-shadow: var(--shadow);
}

.brand-lockup {
  display: inline-flex;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
}

.brand-logo {
  display: block;
  width: min(214px, 50vw);
  height: auto;
  border: 1px solid rgba(255, 255, 255, 0.14);
  border-radius: 8px;
}

.brand-context {
  color: rgba(255, 248, 239, 0.74);
  font-size: 13px;
  font-weight: 900;
}

.login-copy {
  display: grid;
  gap: 8px;
}

.login-copy h1 {
  font-size: clamp(38px, 8vw, 62px);
  line-height: 0.96;
}

.login-copy p,
.auth-message,
.detail-head p,
.section-title span,
.summary-card small {
  color: var(--muted);
}

.auth-message.error { color: #ffd0c2; }

.app-view {
  height: calc(100vh - 28px);
  height: calc(100dvh - 28px);
  min-height: 0;
  display: grid;
  grid-template-rows: auto minmax(88px, auto) minmax(0, 1fr);
  gap: 12px;
  overflow: hidden;
}

.topbar {
  min-height: 70px;
  display: grid;
  grid-template-columns: auto auto minmax(0, 1fr);
  align-items: center;
  gap: 12px;
  padding: 12px 14px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: rgba(24, 20, 17, 0.84);
  backdrop-filter: blur(18px);
}

.station-pill,
.top-actions {
  display: inline-flex;
  align-items: center;
  gap: 10px;
}

.station-pill {
  justify-self: center;
  min-height: 42px;
  padding: 0 14px;
  border-radius: 8px;
  background: rgba(255, 255, 255, 0.08);
}

.status-dot {
  width: 9px;
  height: 9px;
  border-radius: 999px;
  background: var(--olive);
  box-shadow: 0 0 0 5px rgba(155, 184, 111, 0.18);
}

.top-actions {
  justify-self: end;
  flex-wrap: wrap;
}

.eyebrow {
  color: var(--steel);
  font-size: 12px;
  font-weight: 900;
  letter-spacing: 0;
  text-transform: uppercase;
}

label {
  display: grid;
  gap: 8px;
  color: var(--text);
  font-size: 13px;
  font-weight: 900;
}

input,
select {
  width: 100%;
  min-height: 52px;
  padding: 12px 13px;
  border: 1px solid var(--line);
  border-radius: 8px;
  color: var(--text);
  background: rgba(255, 255, 255, 0.08);
  outline: none;
}

input:focus,
select:focus {
  border-color: rgba(140, 167, 183, 0.68);
  box-shadow: 0 0 0 4px rgba(140, 167, 183, 0.16);
}

.primary-button,
.ghost-button {
  min-height: 50px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 0 15px;
  border: 1px solid transparent;
  border-radius: 8px;
  color: #fff;
  font-weight: 900;
}

.primary-button {
  background: linear-gradient(135deg, var(--steel), var(--tomato));
  box-shadow: 0 14px 28px rgba(140, 167, 183, 0.2);
}

.primary-button:disabled {
  pointer-events: none;
  opacity: 0.44;
}

.ghost-button {
  background: rgba(255, 255, 255, 0.08);
  border-color: rgba(255, 255, 255, 0.16);
}

.ghost-button.compact {
  min-height: 40px;
  padding: 0 12px;
  font-size: 12px;
}

.summary-grid {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 12px;
}

.summary-card {
  min-height: 88px;
  display: grid;
  align-content: space-between;
  gap: 6px;
  padding: 12px 14px;
  border-radius: 8px;
  border: 1px solid rgba(255, 255, 255, 0.1);
  box-shadow: var(--shadow);
}

.summary-card span {
  color: rgba(255, 255, 255, 0.86);
  font-size: 12px;
  font-weight: 900;
  text-transform: uppercase;
}

.summary-card strong {
  font-size: 32px;
  line-height: 1.12;
}

.steel { background: linear-gradient(135deg, #376173, #5f8ca1); }
.wine { background: linear-gradient(135deg, #7f2d2d, #b8462f); }
.fire { background: linear-gradient(135deg, #f0a34a, #d85d36); }
.olive { background: linear-gradient(135deg, #657b4e, #86a15c); }

.quality-shell {
  min-height: 0;
  display: grid;
  grid-template-columns: minmax(330px, 0.86fr) minmax(0, 1.74fr);
  gap: 12px;
  overflow: hidden;
}

.lot-panel,
.trace-panel {
  min-height: 0;
  display: grid;
  gap: 12px;
  padding: 14px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: rgba(24, 20, 17, 0.72);
  box-shadow: var(--shadow);
}

.lot-panel {
  grid-template-rows: auto auto auto minmax(0, 1fr);
}

.trace-panel {
  grid-template-rows: auto auto minmax(0, 1fr);
}

.panel-head,
.detail-head,
.section-title {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 12px;
}

.detail-head {
  min-width: 0;
}

.detail-head > div:first-child {
  min-width: 0;
}

.panel-head h2,
.section-title h2 {
  margin-top: 4px;
  font-size: 24px;
  line-height: 1.15;
}

.detail-head h1 {
  margin-top: 4px;
  font-size: clamp(28px, 3vw, 42px);
  line-height: 1.22;
  overflow-wrap: anywhere;
}

.detail-actions {
  flex: 0 0 auto;
  display: grid;
  grid-template-columns: auto auto;
  gap: 8px;
  align-items: start;
}

.status-pill {
  flex: 0 0 auto;
  padding: 8px 10px;
  border: 1px solid rgba(255, 255, 255, 0.16);
  border-radius: 8px;
  color: var(--text);
  background: rgba(255, 255, 255, 0.08);
  font-size: 12px;
  font-weight: 900;
}

.export-button:disabled {
  cursor: not-allowed;
  opacity: 0.42;
}

.filter-row {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 8px;
}

.filter-button {
  min-height: 42px;
  border: 1px solid rgba(255, 255, 255, 0.16);
  border-radius: 8px;
  color: var(--muted);
  background: rgba(255, 255, 255, 0.06);
  font-weight: 900;
}

.filter-button.active {
  color: #fff;
  background: rgba(140, 167, 183, 0.24);
  border-color: rgba(140, 167, 183, 0.48);
}

.lot-search input {
  min-height: 46px;
  background: rgba(255, 255, 255, 0.065);
}

.lot-list,
.trace-result-list,
.trace-tree,
.trace-timeline,
.trace-environment-logs {
  min-height: 0;
  overflow: auto;
  display: grid;
  align-content: start;
  gap: 10px;
  padding-right: 2px;
}

.lot-row,
.trace-card {
  display: grid;
  gap: 8px;
  padding: 13px;
  border: 1px solid rgba(255, 255, 255, 0.14);
  border-radius: 8px;
  background: rgba(255, 255, 255, 0.07);
}

.lot-row {
  text-align: left;
  color: var(--text);
}

.lot-row.selected {
  border-color: rgba(140, 167, 183, 0.62);
  background: rgba(140, 167, 183, 0.16);
}

.lot-row strong,
.trace-card strong {
  font-size: 18px;
  line-height: 1.1;
}

.lot-meta,
.trace-lines {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  color: var(--muted);
  font-size: 12px;
  font-weight: 800;
}

.trace-form {
  display: grid;
  grid-template-columns: minmax(220px, 1fr) minmax(210px, 0.68fr) minmax(160px, auto);
  gap: 10px;
  align-items: end;
}

.trace-result-list {
  grid-template-rows: minmax(0, 1fr);
  align-content: stretch;
}

.trace-result {
  min-height: 0;
  display: grid;
  grid-template-rows: auto minmax(0, 1fr);
  gap: 10px;
  overflow: hidden;
}

.trace-action-button {
  min-width: 170px;
  background: linear-gradient(135deg, #d85d36, #8ca7b7);
}

.trace-explorer-grid {
  height: 100%;
  min-height: 0;
  display: grid;
  grid-template-columns: minmax(260px, 0.9fr) minmax(360px, 1.18fr) minmax(240px, 0.72fr);
  grid-template-rows: minmax(0, 1fr);
  gap: 10px;
  overflow: hidden;
}

.trace-tree-panel,
.trace-timeline-panel,
.trace-environment-panel {
  min-height: 0;
  display: grid;
  grid-template-rows: auto minmax(0, 1fr);
  gap: 10px;
  padding: 12px;
  border: 1px solid rgba(255, 255, 255, 0.13);
  border-radius: 8px;
  background: rgba(255, 255, 255, 0.055);
}

.trace-environment-panel {
  grid-column: auto;
  min-height: 0;
}

.mini-head {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 10px;
}

.mini-head strong {
  color: var(--text);
  font-size: 13px;
}

.trace-tree-node {
  position: relative;
  overflow: hidden;
  border: 1px solid rgba(255, 255, 255, 0.13);
  border-radius: 8px;
  background: rgba(24, 20, 17, 0.48);
}

.trace-tree-node + .trace-tree-node {
  margin-top: 8px;
}

.trace-tree-node summary {
  display: grid;
  grid-template-columns: auto minmax(0, 1fr);
  gap: 10px;
  align-items: center;
  padding: 11px;
  list-style: none;
  cursor: pointer;
}

.trace-tree-node summary::-webkit-details-marker {
  display: none;
}

.trace-node-dot {
  width: 12px;
  height: 12px;
  border-radius: 999px;
  background: var(--steel);
  box-shadow: 0 0 0 5px rgba(140, 167, 183, 0.13);
}

.trace-tree-node.ingredient-lot .trace-node-dot,
.trace-tree-node.material-root .trace-node-dot {
  background: var(--olive);
  box-shadow: 0 0 0 5px rgba(155, 184, 111, 0.14);
}

.trace-tree-node.shipment-lot .trace-node-dot {
  background: var(--fire);
  box-shadow: 0 0 0 5px rgba(240, 163, 74, 0.13);
}

.trace-node-copy {
  min-width: 0;
  display: grid;
  gap: 3px;
}

.trace-node-copy strong {
  overflow-wrap: anywhere;
  font-size: 15px;
  line-height: 1.1;
}

.trace-node-copy em,
.trace-node-lines,
.trace-timeline-event p,
.trace-timeline-event em,
.environment-empty span,
.environment-log span,
.environment-log p {
  color: var(--muted);
  font-size: 12px;
  font-style: normal;
  font-weight: 800;
  line-height: 1.35;
}

.trace-node-lines {
  display: flex;
  flex-wrap: wrap;
  gap: 7px;
  padding: 0 11px 11px 33px;
}

.trace-node-children {
  margin: 0 10px 10px 24px;
  padding-left: 12px;
  border-left: 1px solid rgba(255, 255, 255, 0.16);
}

.trace-timeline {
  gap: 8px;
  scrollbar-gutter: stable;
}

.trace-timeline-event {
  position: relative;
  display: grid;
  gap: 5px;
  padding: 11px 11px 11px 18px;
  border: 1px solid rgba(255, 255, 255, 0.12);
  border-radius: 8px;
  background: rgba(24, 20, 17, 0.42);
}

.trace-timeline-event::before {
  content: "";
  position: absolute;
  left: 7px;
  top: 13px;
  bottom: 13px;
  width: 3px;
  border-radius: 999px;
  background: linear-gradient(var(--steel), var(--tomato));
}

.trace-time {
  color: rgba(255, 248, 239, 0.58);
  font-size: 11px;
  font-weight: 900;
  text-transform: uppercase;
}

.trace-timeline-event strong {
  color: var(--text);
  font-size: 14px;
}

.trace-timeline-event em {
  color: #dbf0c2;
}

.environment-empty,
.environment-log {
  display: grid;
  gap: 6px;
  padding: 12px;
  border: 1px dashed rgba(155, 184, 111, 0.28);
  border-radius: 8px;
  background: rgba(155, 184, 111, 0.07);
}

.environment-empty strong,
.environment-log strong {
  color: #dbf0c2;
  font-size: 14px;
}

.compact-empty {
  min-height: 92px;
}

.empty-state {
  min-height: 160px;
  display: grid;
  place-items: center;
  padding: 18px;
  border: 1px dashed rgba(255, 255, 255, 0.18);
  border-radius: 8px;
  color: var(--muted);
  text-align: center;
  font-weight: 800;
}

.toast {
  position: fixed;
  left: 50%;
  bottom: 18px;
  transform: translateX(-50%);
  z-index: 20;
  max-width: min(520px, calc(100vw - 28px));
  padding: 12px 14px;
  border: 1px solid rgba(255, 255, 255, 0.18);
  border-radius: 8px;
  color: var(--text);
  background: rgba(24, 20, 17, 0.94);
  box-shadow: var(--shadow);
  font-weight: 900;
}

@media (max-width: 1100px) {
  body {
    overflow: auto;
  }

  .shell,
  .app-view {
    height: auto;
    min-height: 100vh;
    overflow: visible;
  }

  .summary-grid,
  .quality-shell,
  .trace-form,
  .trace-explorer-grid {
    grid-template-columns: 1fr;
  }

  .trace-environment-panel {
    grid-column: auto;
  }

  .lot-panel,
  .trace-panel,
  .trace-explorer-grid {
    overflow: visible;
  }

  .topbar {
    grid-template-columns: 1fr;
  }

  .station-pill,
  .top-actions {
    justify-self: start;
  }
}

@media (max-width: 760px) {
  .shell { padding: 10px; }
  .summary-grid { grid-template-columns: 1fr; }
  .filter-row { grid-template-columns: 1fr; }
  .detail-actions { grid-template-columns: 1fr; }
  .panel-head,
  .detail-head,
  .section-title {
    display: grid;
  }
}
