:root {
  --bg: #f4f6f8;
  --surface: #ffffff;
  --line: #d7dde3;
  --text: #1b2430;
  --muted: #667085;
  --primary: #176b87;
  --primary-dark: #0f5068;
  --danger: #b42318;
  --success: #157347;
  --warning: #b54708;
  --shadow: 0 10px 28px rgba(23, 36, 48, 0.08);
}

* {
  box-sizing: border-box;
}

.hidden {
  display: none !important;
}

body {
  margin: 0;
  min-height: 100vh;
  display: grid;
  grid-template-columns: 208px minmax(0, 1fr);
  background: var(--bg);
  color: var(--text);
  font-family: "Microsoft JhengHei", "Noto Sans TC", Arial, sans-serif;
}

body.logged-out .sidebar,
body.logged-out .app {
  filter: blur(1px);
  pointer-events: none;
  user-select: none;
}

.login-screen {
  position: fixed;
  inset: 0;
  z-index: 20;
  display: grid;
  place-items: center;
  padding: 20px;
  background: rgba(244, 246, 248, 0.92);
}

.login-card {
  width: min(420px, 100%);
  display: grid;
  gap: 14px;
  padding: 26px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #ffffff;
  box-shadow: var(--shadow);
}

.login-card .brand-mark {
  width: 48px;
  height: 48px;
}

.login-card h2,
.login-card p {
  margin: 0;
}

.login-card h2 {
  font-size: 22px;
}

.login-card p {
  color: var(--muted);
  font-size: 13px;
  font-weight: 700;
}

.login-error {
  min-height: 18px;
  color: var(--danger) !important;
}

.user-pill {
  min-height: 38px;
  display: inline-flex;
  align-items: center;
  padding: 8px 10px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: #fff;
  color: var(--primary-dark);
  font-size: 13px;
  font-weight: 800;
}

button,
input,
select {
  font: inherit;
}

.sidebar {
  min-height: 100vh;
  padding: 16px 10px;
  background: #172430;
  color: #fff;
}

.brand {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 4px 4px 20px;
}

.brand-mark {
  width: 40px;
  height: 40px;
  display: grid;
  place-items: center;
  border-radius: 8px;
  background: #38a3a5;
  font-weight: 800;
}

.brand h1,
.brand p {
  margin: 0;
}

.brand h1 {
  font-size: 18px;
}

.brand p {
  margin-top: 4px;
  color: #b9c2cc;
  font-size: 12px;
}

.nav {
  display: grid;
  gap: 6px;
}

.nav-item {
  width: 100%;
  padding: 11px 12px;
  border: 0;
  border-radius: 6px;
  color: #dfe7ee;
  background: transparent;
  text-align: left;
  cursor: pointer;
}

.nav-item:hover,
.nav-item.active {
  color: #fff;
  background: rgba(255, 255, 255, 0.12);
}

.app {
  min-width: 0;
  padding: 16px;
}

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

.eyebrow {
  margin: 0 0 6px;
  color: var(--primary);
  font-size: 13px;
  font-weight: 700;
}

.topbar h2 {
  margin: 0;
  font-size: 24px;
}

.top-actions,
.form-actions,
.panel-actions,
.filters {
  display: flex;
  align-items: end;
  gap: 10px;
  flex-wrap: wrap;
}

.btn {
  min-height: 38px;
  padding: 8px 14px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: var(--surface);
  color: var(--text);
  cursor: pointer;
  font-weight: 700;
}

.btn:hover {
  border-color: var(--primary);
}

.btn.primary {
  border-color: var(--primary);
  background: var(--primary);
  color: #fff;
}

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

.btn.ghost {
  background: #f9fafb;
}

.btn.danger {
  border-color: #f3b5ae;
  color: var(--danger);
  background: #fff5f3;
}

.btn.export-excel {
  border-color: #b7dfc6;
  background: #e8f6ee;
  color: #12633a;
}

.btn.export-excel:hover {
  border-color: #69b984;
  background: #d8efdf;
}

.tabs {
  display: inline-flex;
  gap: 4px;
  padding: 4px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #f8fafc;
}

.tab {
  min-height: 32px;
  padding: 5px 12px;
  border: 0;
  border-radius: 6px;
  background: transparent;
  color: var(--muted);
  cursor: pointer;
  font-weight: 700;
}

.tab.active {
  background: var(--primary);
  color: #fff;
}

.sheet-tabs {
  display: flex;
  align-items: end;
  gap: 2px;
  margin: 10px 18px 0;
  border-bottom: 1px solid var(--line);
  overflow-x: auto;
}

.sheet-tab {
  min-height: 34px;
  padding: 7px 14px;
  border: 1px solid var(--line);
  border-bottom: 0;
  border-radius: 7px 7px 0 0;
  background: #eef2f6;
  color: var(--muted);
  cursor: pointer;
  font-weight: 800;
  white-space: nowrap;
}

.sheet-tab.active {
  background: var(--primary-dark);
  color: #fff;
  transform: translateY(1px);
}

.file-action {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: 38px;
  padding: 8px 14px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: #f9fafb;
  color: var(--text);
  cursor: pointer;
  font-size: 14px;
}

.file-action input {
  display: none;
}

input[readonly] {
  background: #f8fafc;
  color: var(--muted);
}

.view {
  display: none;
}

.view.active {
  display: grid;
  gap: 14px;
}

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

.kpi-grid.compact {
  margin: 18px 0;
}

.kpi,
.panel {
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface);
  box-shadow: var(--shadow);
}

.kpi {
  padding: 14px;
}

.kpi span {
  display: block;
  color: var(--muted);
  font-size: 14px;
}

.kpi strong {
  display: block;
  margin-top: 10px;
  font-size: 26px;
}

.split {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 14px;
}

.dashboard-layout {
  display: grid;
  grid-template-columns: minmax(0, 1.15fr) minmax(320px, 0.85fr);
  gap: 14px;
}

.dashboard-search {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 10px;
  padding: 14px 16px 8px;
  align-items: end;
}

.compact-date-field {
  width: 190px;
}

.compact-date-field input {
  cursor: pointer;
}

.dashboard-equipment-board {
  margin-top: 14px;
}

.dashboard-equipment-board .table-wrap {
  overflow-x: hidden;
}

.dashboard-equipment-board table {
  min-width: 0;
  table-layout: fixed;
}

.dashboard-equipment-board th,
.dashboard-equipment-board td {
  padding-left: 6px;
  padding-right: 6px;
}

.dashboard-equipment-board th:nth-child(1),
.dashboard-equipment-board td:nth-child(1) {
  width: 38px;
  text-align: center;
}

.dashboard-equipment-board th:nth-child(2),
.dashboard-equipment-board td:nth-child(2) {
  width: 64px;
}

.dashboard-equipment-board th:nth-child(3),
.dashboard-equipment-board td:nth-child(3) {
  width: 126px;
}

.dashboard-equipment-board th:nth-child(4),
.dashboard-equipment-board td:nth-child(4) {
  width: 92px;
}

.dashboard-equipment-board th:nth-child(5),
.dashboard-equipment-board td:nth-child(5) {
  width: 142px;
}

.dashboard-equipment-board th:nth-child(6),
.dashboard-equipment-board td:nth-child(6) {
  width: auto;
}

.dashboard-equipment-board th:nth-child(7),
.dashboard-equipment-board td:nth-child(7) {
  width: 110px;
}

.dashboard-equipment-board th:nth-child(8),
.dashboard-equipment-board td:nth-child(8) {
  width: 94px;
}

.dashboard-equipment-board th:nth-child(9),
.dashboard-equipment-board td:nth-child(9) {
  width: 86px;
}

.equipment-board-summary {
  background: #f3f4f6;
}

.equipment-board-summary span {
  color: #111827;
}

.light-dot {
  display: inline-block;
  width: 10px;
  height: 10px;
  border-radius: 999px;
  vertical-align: middle;
}

.light-dot.green {
  background: var(--success);
}

.light-dot.yellow {
  background: var(--warning);
}

.order-status-card {
  min-height: 245px;
  padding: 8px 16px 16px;
}

.empty-hint {
  margin: 0;
  padding: 18px;
  border: 1px dashed var(--line);
  border-radius: 7px;
  color: var(--muted);
  font-weight: 700;
  text-align: center;
}

.panel-note {
  margin: -4px 18px 12px;
  color: #98a2b3;
  font-size: 10px;
  font-weight: 700;
}

.status-card-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  margin-bottom: 12px;
  padding: 12px;
  border-radius: 7px;
  background: #eaf3f6;
}

.status-card-head strong {
  color: var(--primary-dark);
  font-size: 18px;
}

.status-grid {
  display: grid;
  grid-template-columns: 86px minmax(0, 1fr);
  gap: 8px 12px;
}

.status-grid span {
  color: var(--muted);
  font-size: 13px;
  font-weight: 700;
}

.status-grid strong {
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  font-size: 14px;
}

.panel {
  min-width: 0;
  overflow: hidden;
}

.panel-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 14px 16px;
  border-bottom: 1px solid var(--line);
}

.panel-head h3 {
  margin: 0;
  font-size: 18px;
}

.form-grid {
  display: grid;
  grid-template-columns: repeat(4, minmax(145px, 1fr));
  gap: 12px;
  padding: 16px;
}

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

input,
select {
  width: 100%;
  min-height: 40px;
  padding: 8px 10px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: #fff;
  color: var(--text);
}

input::placeholder {
  color: rgba(102, 112, 133, 0.6);
  opacity: 1;
}

input:focus,
select:focus {
  outline: 2px solid rgba(23, 107, 135, 0.18);
  border-color: var(--primary);
}

.wide {
  grid-column: span 2;
}

.form-actions {
  grid-column: 1 / -1;
}

.filters {
  padding: 18px 18px 0;
}

.filters.compact {
  padding-top: 8px;
}

.daily-report-date-filter {
  min-width: 145px;
}

.report-section {
  padding-top: 8px;
}

.report-section + .report-section {
  margin-top: 20px;
  border-top: 1px solid var(--line);
}

.report-section h4 {
  display: inline-block;
  margin: 14px 18px 10px;
  padding: 9px 12px;
  border-radius: 6px;
  background: #f2f4f7;
  color: var(--text);
  font-size: 17px;
  font-weight: 900;
}

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

.export-options {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  align-self: flex-end;
  min-height: 38px;
  gap: 5px 8px;
  margin: 0;
  padding: 5px 9px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: #f8fafc;
}

.export-options span {
  color: var(--primary-dark);
  font-size: 12px;
  font-weight: 800;
}

.export-options label {
  display: inline-flex;
  align-items: center;
  gap: 3px;
  color: var(--text);
  font-size: 12px;
  line-height: 1;
  font-weight: 700;
}

.export-options input {
  width: auto;
  min-height: auto;
}

.production-summary-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 14px;
  padding: 0 18px 16px;
}

.summary-block {
  min-width: 0;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #ffffff;
  overflow: hidden;
}

.summary-title {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 10px;
  padding: 12px 14px;
  border-bottom: 1px solid var(--line);
  background: #eaf3f6;
}

.summary-title strong {
  color: var(--primary-dark);
  font-size: 16px;
}

.summary-title span {
  color: var(--muted);
  font-size: 12px;
  font-weight: 700;
}

.summary-block .filters {
  padding-left: 14px;
  padding-right: 14px;
}

.summary-block .section-note {
  margin-left: 14px;
  margin-right: 14px;
}

.summary-block .table-wrap {
  overflow-x: hidden;
}

.summary-block table {
  table-layout: fixed;
}

.summary-block th:first-child,
.summary-block td:first-child {
  width: 38%;
}

.summary-block th:last-child,
.summary-block td:last-child {
  text-align: right;
}

.summary-block th:not(:first-child),
.summary-block td:not(:first-child) {
  text-align: right;
}

.production-total {
  font-size: 17px;
  font-weight: 900;
}

.department-target-input {
  min-height: 30px;
  padding: 5px 7px;
  text-align: right;
  font-size: 14px;
  font-weight: 800;
}

.target-value {
  font-size: 12px;
  font-weight: 800;
  color: var(--text);
  opacity: 0.5;
}

.inline-filter {
  min-width: 240px;
}

.table-wrap {
  width: 100%;
  overflow-x: auto;
}

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

th,
td {
  padding: 9px 8px;
  border-bottom: 1px solid var(--line);
  text-align: left;
  vertical-align: middle;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

th {
  color: var(--muted);
  background: #f8fafc;
  font-size: 12.5px;
}

td {
  font-size: 13.5px;
}

.order-table {
  min-width: 1460px;
}

.order-table th:nth-child(1),
.order-table td:nth-child(1) {
  width: 9%;
}

.order-table th:nth-child(2),
.order-table td:nth-child(2) {
  width: 6.5%;
}

.order-table th:nth-child(3),
.order-table td:nth-child(3) {
  width: 7%;
}

.order-table th:nth-child(4),
.order-table td:nth-child(4) {
  width: 9%;
}

.order-table th:nth-child(5),
.order-table td:nth-child(5) {
  width: 7.5%;
}

.order-table th:nth-child(6),
.order-table td:nth-child(6),
.order-table th:nth-child(7),
.order-table td:nth-child(7) {
  width: 5.5%;
}

.order-table th:nth-child(8),
.order-table td:nth-child(8) {
  width: 6%;
}

.order-table th:nth-child(9),
.order-table td:nth-child(9) {
  width: 5.5%;
}

.order-table th:nth-child(10),
.order-table td:nth-child(10) {
  width: 7%;
}

.order-table th:nth-child(11),
.order-table td:nth-child(11) {
  width: 7%;
}

.order-table th:nth-child(12),
.order-table td:nth-child(12) {
  width: 6.5%;
}

.order-table th:nth-child(13),
.order-table td:nth-child(13) {
  width: 7%;
}

.order-table th:nth-child(14),
.order-table td:nth-child(14) {
  width: 6.5%;
}

.order-table th:nth-child(15),
.order-table td:nth-child(15) {
  position: sticky;
  right: 0;
  z-index: 1;
  width: 9.5%;
  min-width: 148px;
  background: #ffffff;
  overflow: visible;
  text-overflow: clip;
}

.order-table th:nth-child(15) {
  z-index: 2;
  background: #f8fafc;
}

.user-table th,
.user-table td {
  width: 20%;
}

.locked-row {
  background: #f8fafc;
}

.daily-summary {
  display: flex;
  flex-wrap: wrap;
  gap: 0;
  margin: 12px 18px 14px;
  border: 1px solid var(--line);
  border-radius: 7px 7px 0 0;
  background: #eaf3f6;
  overflow: hidden;
}

.daily-summary span {
  padding: 11px 16px;
  border-right: 1px solid rgba(255, 255, 255, 0.35);
  background: transparent;
  color: var(--primary-dark);
  font-size: 15px;
  font-weight: 800;
}

.daily-summary span:last-child {
  border-right: 0;
}

.metric-good {
  background: #e8f5ee;
  color: var(--success);
  font-weight: 800;
}

.metric-warn {
  background: #fff4e5;
  color: var(--warning);
  font-weight: 800;
}

.metric-progress {
  background: #fff4e5;
  color: var(--warning);
  font-weight: 800;
}

#workOrderDailyTable tr:nth-child(even) td {
  background-color: #f8fafc;
}

#workOrderDailyTable tr:nth-child(even) .metric-good {
  background-color: #dff1e8;
}

#workOrderDailyTable tr:nth-child(even) .metric-progress {
  background-color: #ffedcc;
}

#overdueOrderTable tr:nth-child(even) td {
  background-color: #f8fafc;
}

#departmentDailyTable tr:nth-child(even) td,
#departmentMonthlyTable tr:nth-child(even) td {
  background-color: #f8fafc;
}

#departmentDailyTable tr:nth-child(even) .metric-good,
#departmentMonthlyTable tr:nth-child(even) .metric-good {
  background-color: #dff1e8;
}

#overdueOrderTable tr:nth-child(even) .metric-warn,
#overdueOrderTable tr:nth-child(even) .metric-progress {
  background-color: #ffedcc;
}

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

.badge {
  display: inline-flex;
  align-items: center;
  min-height: 24px;
  padding: 3px 8px;
  border-radius: 999px;
  background: #eef2f6;
  color: var(--text);
  font-size: 12px;
  font-weight: 700;
}

.badge.success {
  background: #e8f5ee;
  color: var(--success);
}

.badge.warning {
  background: #fff4e5;
  color: var(--warning);
}

.badge.danger {
  background: #fff0ee;
  color: var(--danger);
}

.row-actions {
  display: flex;
  gap: 4px;
  flex-wrap: nowrap;
  white-space: nowrap;
}

.row-actions .btn {
  min-height: 30px;
  padding: 4px 6px;
  font-size: 12px;
}

.toast {
  position: fixed;
  right: 20px;
  bottom: 20px;
  max-width: min(360px, calc(100vw - 40px));
  padding: 12px 14px;
  border-radius: 8px;
  background: #172430;
  color: #fff;
  box-shadow: var(--shadow);
  opacity: 0;
  transform: translateY(12px);
  pointer-events: none;
  transition: 0.2s ease;
}

.toast.show {
  opacity: 1;
  transform: translateY(0);
}

@media (max-width: 980px) {
  body {
    grid-template-columns: 1fr;
  }

  .sidebar {
    min-height: auto;
  }

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

  .nav-item {
    text-align: center;
  }

  .kpi-grid,
  .split,
  .dashboard-layout,
  .form-grid,
  .production-summary-grid {
    grid-template-columns: 1fr 1fr;
  }

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

  table {
    min-width: 720px;
  }

  .order-table {
    min-width: 1120px;
  }
}

@media (max-width: 640px) {
  .app {
    padding: 16px;
  }

  .topbar {
    align-items: stretch;
    flex-direction: column;
  }

  .nav,
  .kpi-grid,
  .split,
  .dashboard-layout,
  .form-grid,
  .production-summary-grid {
    grid-template-columns: 1fr;
  }

  .dashboard-search {
    grid-template-columns: 1fr;
  }

  .wide {
    grid-column: span 1;
  }
}
