:root {
  --bg: #f6f7f9;
  --surface: #ffffff;
  --surface-alt: #eef4f2;
  --ink: #1f2328;
  --muted: #667085;
  --line: #d8dee7;
  --nav: #222425;
  --teal: #007c72;
  --teal-dark: #005f57;
  --green: #2f855a;
  --amber: #b7791f;
  --red: #c2410c;
  --violet: #6b46c1;
  --blue: #2563eb;
  --shadow: 0 16px 40px rgba(31, 35, 40, 0.08);
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-height: 100vh;
  background: var(--bg);
  color: var(--ink);
  font-family: "Noto Sans TC", "Microsoft JhengHei", Arial, sans-serif;
  font-size: 15px;
  line-height: 1.5;
}

button,
input,
select,
textarea {
  font: inherit;
}

button {
  cursor: pointer;
}

.app-shell {
  display: grid;
  grid-template-columns: 280px minmax(0, 1fr);
  min-height: 100vh;
}

.is-hidden {
  display: none !important;
}

.login-view {
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: 24px;
  background: var(--bg);
}

.login-panel {
  width: min(420px, 100%);
  display: grid;
  gap: 16px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface);
  box-shadow: var(--shadow);
  padding: 24px;
}

.sidebar {
  position: sticky;
  top: 0;
  height: 100vh;
  display: flex;
  flex-direction: column;
  gap: 24px;
  padding: 24px;
  background: var(--nav);
  color: #f6f7f9;
}

.brand {
  display: flex;
  align-items: center;
  gap: 12px;
}

.brand-mark {
  display: grid;
  width: 44px;
  height: 44px;
  place-items: center;
  border: 1px solid rgba(255, 255, 255, 0.22);
  border-radius: 8px;
  background: var(--teal);
  font-weight: 800;
}

.brand-title,
.brand-subtitle,
.eyebrow {
  margin: 0;
}

.brand-title {
  font-size: 18px;
  font-weight: 800;
}

.brand-subtitle {
  color: rgba(255, 255, 255, 0.66);
  font-size: 13px;
}

.nav-list {
  display: grid;
  gap: 8px;
}

.nav-item {
  width: 100%;
  min-height: 42px;
  border: 1px solid transparent;
  border-radius: 8px;
  background: transparent;
  color: rgba(255, 255, 255, 0.78);
  text-align: left;
  padding: 10px 12px;
}

.nav-item:hover,
.nav-item.is-active {
  border-color: rgba(255, 255, 255, 0.16);
  background: rgba(255, 255, 255, 0.1);
  color: #ffffff;
}

.role-switcher {
  display: grid;
  gap: 8px;
  margin-top: auto;
  color: rgba(255, 255, 255, 0.74);
  font-size: 13px;
}

.role-switcher select {
  width: 100%;
  min-height: 40px;
  border: 1px solid rgba(255, 255, 255, 0.18);
  border-radius: 8px;
  background: #ffffff;
  color: var(--ink);
  padding: 8px 10px;
}

.workspace {
  min-width: 0;
  padding: 28px;
}

.topbar {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 20px;
  margin-bottom: 18px;
}

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

h1,
h2,
h3,
p {
  margin-top: 0;
}

h1 {
  margin-bottom: 0;
  font-size: 30px;
  line-height: 1.2;
}

h2 {
  margin-bottom: 0;
  font-size: 18px;
  line-height: 1.3;
}

h3 {
  margin-bottom: 6px;
  font-size: 16px;
}

.topbar-meta {
  display: flex;
  flex-wrap: wrap;
  justify-content: flex-end;
  gap: 8px;
  color: var(--muted);
}

.role-badge,
.status-pill,
.priority-pill {
  display: inline-flex;
  min-height: 26px;
  align-items: center;
  border-radius: 999px;
  padding: 3px 10px;
  font-size: 13px;
  font-weight: 700;
  white-space: nowrap;
}

.role-badge {
  background: var(--surface-alt);
  color: var(--teal-dark);
}

.toast {
  min-height: 0;
  margin-bottom: 14px;
  color: var(--teal-dark);
  font-weight: 700;
}

.toast.is-visible {
  min-height: 24px;
}

.view {
  display: none;
}

.view.is-active {
  display: grid;
  gap: 18px;
}

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

.metric-card,
.panel,
.stack-card,
.job-card,
.machine-card {
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface);
  box-shadow: var(--shadow);
}

.metric-card {
  display: grid;
  gap: 8px;
  min-height: 118px;
  padding: 18px;
}

.metric-label {
  color: var(--muted);
  font-weight: 700;
}

.metric-value {
  font-size: 34px;
  font-weight: 800;
  line-height: 1;
}

.metric-note {
  margin: 0;
  color: var(--muted);
  font-size: 13px;
}

.dashboard-grid,
.split-layout {
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(360px, 0.85fr);
  gap: 18px;
  align-items: start;
}

.workflow-column {
  display: grid;
  gap: 18px;
}

.panel {
  min-width: 0;
  padding: 18px;
}

.panel-heading {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 14px;
  margin-bottom: 14px;
}

.table-wrap {
  overflow-x: auto;
}

table {
  width: 100%;
  border-collapse: collapse;
}

th,
td {
  border-bottom: 1px solid var(--line);
  padding: 12px 10px;
  text-align: left;
  vertical-align: top;
}

th {
  color: var(--muted);
  font-size: 13px;
  font-weight: 800;
}

tr:last-child td {
  border-bottom: 0;
}

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

.machine-card {
  padding: 14px;
}

.machine-card p,
.stack-card p,
.job-card p {
  margin-bottom: 8px;
  color: var(--muted);
}

.machine-meta {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.form-panel {
  display: grid;
  gap: 14px;
}

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

label {
  display: grid;
  gap: 6px;
  color: var(--muted);
  font-size: 13px;
  font-weight: 800;
}

input,
select,
textarea {
  width: 100%;
  min-height: 42px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #ffffff;
  color: var(--ink);
  padding: 9px 11px;
}

textarea {
  resize: vertical;
}

.wide-field {
  grid-column: 1 / -1;
}

.primary-button,
.ghost-button,
.danger-button,
.success-button,
.warning-button {
  min-height: 38px;
  border-radius: 8px;
  border: 1px solid transparent;
  padding: 8px 12px;
  font-weight: 800;
}

.primary-button {
  background: var(--teal);
  color: #ffffff;
}

.primary-button:hover {
  background: var(--teal-dark);
}

.ghost-button {
  border-color: var(--line);
  background: #ffffff;
  color: var(--ink);
}

.compact-button {
  min-height: 30px;
  padding: 4px 10px;
  font-size: 13px;
}

.success-button {
  background: var(--green);
  color: #ffffff;
}

.danger-button {
  background: var(--red);
  color: #ffffff;
}

.warning-button {
  background: var(--amber);
  color: #ffffff;
}

.primary-button:disabled,
.ghost-button:disabled,
.danger-button:disabled,
.success-button:disabled,
.warning-button:disabled {
  background: #d4d4d4;
  color: #888;
  border-color: transparent;
  cursor: not-allowed;
}

.primary-button:disabled:hover,
.ghost-button:disabled:hover,
.danger-button:disabled:hover,
.success-button:disabled:hover,
.warning-button:disabled:hover {
  background: #d4d4d4;
}

.button-row {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-top: 10px;
}

.form-note {
  min-height: 24px;
  color: var(--muted);
  font-size: 13px;
  font-weight: 700;
}

.split-row-list {
  display: grid;
  gap: 10px;
}

.split-row {
  display: grid;
  grid-template-columns: minmax(82px, 0.35fr) minmax(0, 1fr) auto;
  gap: 10px;
  align-items: end;
  padding: 10px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fbfcfd;
}

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

.stack-list,
.timeline,
.job-grid,
.chart-list {
  display: grid;
  gap: 12px;
}

.stack-card,
.job-card {
  padding: 14px;
}

.stack-card-header,
.job-card-header {
  display: flex;
  justify-content: space-between;
  gap: 12px;
}

.muted {
  color: var(--muted);
}

.detail-list {
  display: grid;
  gap: 4px;
  margin: 10px 0 0;
  padding: 0;
  list-style: none;
  color: var(--muted);
}

.detail-list strong {
  color: var(--ink);
}

.timeline-item {
  border-left: 3px solid var(--teal);
  padding-left: 12px;
}

.timeline-item p {
  margin-bottom: 2px;
}

.job-grid {
  grid-template-columns: repeat(3, minmax(0, 1fr));
}

.chart-item {
  display: grid;
  gap: 7px;
}

.chart-label {
  display: flex;
  justify-content: space-between;
  gap: 10px;
  color: var(--muted);
  font-weight: 800;
}

.chart-track {
  height: 12px;
  overflow: hidden;
  border-radius: 999px;
  background: #e5e7eb;
}

.chart-bar {
  height: 100%;
  border-radius: inherit;
  background: linear-gradient(90deg, var(--teal), var(--amber));
}

.status-pending_approval,
.status-queued {
  background: #fff7ed;
  color: var(--amber);
}

.status-approved,
.status-received,
.status-idle,
.status-completed,
.status-closed {
  background: #eaf7ef;
  color: var(--green);
}

.status-rejected,
.status-alarm,
.status-failed {
  background: #fff1f0;
  color: var(--red);
}

.status-in_progress,
.status-running,
.status-busy,
.status-loaded,
.status-dispatched {
  background: #eff6ff;
  color: var(--blue);
}

.status-maintenance,
.status-split {
  background: #f4f0ff;
  color: var(--violet);
}

.priority-High {
  background: #fff7ed;
  color: var(--amber);
}

.priority-Critical {
  background: #fff1f0;
  color: var(--red);
}

.priority-Normal {
  background: #eef4f2;
  color: var(--teal-dark);
}

.empty-state {
  display: grid;
  min-height: 110px;
  place-items: center;
  border: 1px dashed var(--line);
  border-radius: 8px;
  color: var(--muted);
  text-align: center;
  padding: 20px;
}

.pagination {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 16px;
  margin-top: 12px;
  flex-wrap: wrap;
}

.pagination-info {
  color: var(--muted);
  font-size: 14px;
}

.pagination button[disabled] {
  opacity: 0.4;
  cursor: not-allowed;
}

.split-summary-error {
  color: var(--danger, #c00);
  font-weight: 600;
}

.recipe-deactivated td {
  opacity: 0.6;
}

.recipe-deactivated strong {
  text-decoration: line-through;
  color: var(--muted);
}

/* Dashboard triple grid */
.dashboard-triple-grid {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 18px;
  align-items: start;
}

/* Status distribution stacked bar */
.status-distribution {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.status-bar-container {
  display: flex;
  height: 28px;
  border-radius: 8px;
  overflow: hidden;
  background: #e5e7eb;
}

.status-bar-segment {
  display: flex;
  align-items: center;
  justify-content: center;
  min-width: 28px;
  color: #fff;
  font-size: 12px;
  font-weight: 800;
  transition: width 0.4s ease;
}

.status-bar-segment.seg-pending_approval { background: var(--amber); }
.status-bar-segment.seg-approved { background: var(--green); }
.status-bar-segment.seg-received { background: #38a169; }
.status-bar-segment.seg-split { background: var(--violet); }
.status-bar-segment.seg-in_progress { background: var(--blue); }
.status-bar-segment.seg-completed,
.status-bar-segment.seg-closed { background: var(--teal); }
.status-bar-segment.seg-rejected { background: var(--red); }

.status-legend {
  display: flex;
  flex-wrap: wrap;
  gap: 8px 14px;
}

.legend-item {
  display: flex;
  align-items: center;
  gap: 5px;
  font-size: 12px;
  color: var(--muted);
  font-weight: 700;
}

.legend-dot {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  flex-shrink: 0;
}

/* Dashboard mini timeline */
.mini-timeline {
  display: grid;
  gap: 6px;
  max-height: 280px;
  overflow-y: auto;
}

.mini-timeline-item {
  display: grid;
  gap: 2px;
  padding: 6px 0 6px 12px;
  border-left: 3px solid var(--teal);
  font-size: 13px;
}

.mini-timeline-item .mini-time {
  color: var(--muted);
  font-size: 11px;
}

/* Severity pills */
.severity-pill {
  display: inline-flex;
  min-height: 22px;
  align-items: center;
  border-radius: 999px;
  padding: 2px 8px;
  font-size: 11px;
  font-weight: 800;
  white-space: nowrap;
}

.severity-Critical { background: #fff1f0; color: var(--red); }
.severity-High { background: #fff7ed; color: var(--amber); }
.severity-Medium { background: #eff6ff; color: var(--blue); }
.severity-Low { background: #eef4f2; color: var(--teal-dark); }

/* Alarm summary bar */
.alarm-summary {
  display: flex;
  gap: 16px;
  flex-wrap: wrap;
  margin-bottom: 10px;
  padding: 10px 14px;
  border-radius: 8px;
  background: var(--surface-alt);
  font-size: 13px;
  font-weight: 700;
  color: var(--muted);
}

.alarm-summary .summary-count {
  font-size: 18px;
  font-weight: 800;
  color: var(--ink);
}

/* Result meta code block */
.result-meta-code {
  display: inline-block;
  padding: 3px 8px;
  border-radius: 4px;
  background: #f1f3f5;
  font-family: "Fira Code", "Consolas", monospace;
  font-size: 12px;
  color: var(--teal-dark);
  word-break: break-all;
}

/* Result stat header */
.result-stat-bar {
  display: flex;
  gap: 16px;
  flex-wrap: wrap;
  margin-bottom: 10px;
  padding: 10px 14px;
  border-radius: 8px;
  background: var(--surface-alt);
  font-size: 13px;
  font-weight: 700;
  color: var(--muted);
}

.result-stat-bar .stat-value {
  font-size: 18px;
  font-weight: 800;
  color: var(--ink);
}

h1, h2, h3, p, td {
  word-break: break-word;
}

@media (max-width: 1100px) {
  .metric-grid,
  .job-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .dashboard-grid,
  .split-layout {
    grid-template-columns: 1fr;
  }

  .dashboard-triple-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }
}

@media (max-width: 760px) {
  .app-shell {
    grid-template-columns: 1fr;
  }

  .sidebar {
    position: static;
    height: auto;
    padding: 18px;
  }

  .nav-list {
    grid-auto-flow: column;
    grid-auto-columns: max-content;
    overflow-x: auto;
    padding-bottom: 4px;
  }

  .nav-item {
    white-space: nowrap;
  }

  .workspace {
    padding: 18px;
  }

  .topbar {
    display: grid;
  }

  .topbar-meta {
    justify-content: flex-start;
  }

  .metric-grid,
  .machine-grid,
  .form-grid,
  .split-row,
  .job-grid,
  .dashboard-triple-grid {
    grid-template-columns: 1fr;
  }

  h1 {
    font-size: 26px;
  }
}
