:root {
  --bg: #eef1f4;
  --surface: #ffffff;
  --surface-2: #f7f8fa;
  --line: #d7dde4;
  --text: #1d252d;
  --muted: #667382;
  --brand: #0f766e;
  --brand-dark: #0b5f59;
  --accent: #b45309;
  --danger: #c2410c;
  --ok: #15803d;
  --shadow: 0 14px 34px rgba(15, 23, 42, 0.08);
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: "Microsoft JhengHei", "Segoe UI", Arial, sans-serif;
}

button,
input,
select {
  font: inherit;
}

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

.mobile-header,
.mobile-action-bar,
.mobile-menu {
  display: none;
}

.mobile-header-actions {
  display: flex;
  align-items: center;
  gap: 8px;
}

.mobile-menu-btn {
  width: 42px;
  height: 42px;
  display: none;
  place-items: center;
  gap: 4px;
  border: 1px solid rgba(203, 213, 225, 0.35);
  border-radius: 8px;
  background: #263442;
  cursor: pointer;
}

.mobile-menu-btn span {
  width: 18px;
  height: 2px;
  display: block;
  border-radius: 999px;
  background: #fff;
  transition: transform 0.18s ease, opacity 0.18s ease;
}

.mobile-menu-btn.active span:nth-child(1) {
  transform: translateY(6px) rotate(45deg);
}

.mobile-menu-btn.active span:nth-child(2) {
  opacity: 0;
}

.mobile-menu-btn.active span:nth-child(3) {
  transform: translateY(-6px) rotate(-45deg);
}

.sidebar {
  background: #17212b;
  color: #fff;
  padding: 24px;
  display: flex;
  flex-direction: column;
  gap: 28px;
}

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

.brand-mark {
  width: 44px;
  height: 44px;
  display: grid;
  place-items: center;
  background: #f8fafc;
  color: #17212b;
  border-radius: 8px;
  font-weight: 800;
}

.brand h1,
.brand p,
.topbar h2,
.topbar p,
.section-title h3 {
  margin: 0;
}

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

.brand p,
.muted,
.eyebrow,
.detail-list dt,
.drop-zone small,
.metric span {
  color: var(--muted);
}

.sidebar .brand p,
.sidebar .muted {
  color: #a7b3c2;
}

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

.nav-item {
  display: block;
  border: 0;
  border-radius: 8px;
  padding: 12px 14px;
  background: transparent;
  color: #cbd5e1;
  text-align: left;
  cursor: pointer;
  text-decoration: none;
}

.nav-item.active,
.nav-item:hover {
  background: #263442;
  color: #fff;
}

.status-panel,
.upload-panel,
.summary-panel,
.result-panel,
.settings-panel {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 8px;
  box-shadow: var(--shadow);
}

.status-panel {
  margin-top: auto;
  padding: 16px;
  color: var(--text);
}

.status-panel strong,
.status-panel span {
  display: block;
}

.status-label {
  color: var(--muted);
  font-size: 13px;
  margin-bottom: 6px;
}

.badge {
  width: fit-content;
  margin-top: 12px;
  padding: 5px 9px;
  border-radius: 999px;
  font-size: 13px;
}

.badge.pending {
  background: #fef3c7;
  color: #92400e;
}

.badge.allowed {
  background: #dcfce7;
  color: #166534;
}

.workspace {
  padding: 28px;
  display: grid;
  gap: 20px;
}

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

.eyebrow {
  font-size: 13px;
  font-weight: 700;
  text-transform: uppercase;
}

.topbar h2 {
  font-size: 28px;
}

.topbar-actions {
  display: flex;
  gap: 10px;
}

.ghost-btn,
.icon-btn,
.primary-btn {
  border: 1px solid var(--line);
  border-radius: 8px;
  min-height: 40px;
  cursor: pointer;
}

.ghost-btn {
  display: inline-grid;
  place-items: center;
  background: var(--surface);
  color: var(--text);
  padding: 0 14px;
  text-decoration: none;
}

.icon-btn {
  width: 42px;
  background: var(--surface);
}

.primary-btn {
  background: var(--brand);
  border-color: var(--brand);
  color: #fff;
  padding: 0 16px;
  font-weight: 700;
}

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

.primary-btn:disabled {
  background: #a8b3bd;
  border-color: #a8b3bd;
  cursor: not-allowed;
}

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

.settings-panel.hidden {
  display: none;
}

.settings-panel label {
  display: grid;
  gap: 6px;
  font-size: 13px;
  color: var(--muted);
}

.settings-panel input {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 10px 12px;
  color: var(--text);
}

.hidden {
  display: none !important;
}

.scan-grid {
  display: grid;
  grid-template-columns: minmax(300px, 420px) minmax(320px, 1fr);
  gap: 20px;
}

.upload-panel,
.summary-panel,
.result-panel {
  padding: 18px;
}

.section-title {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-bottom: 16px;
}

.section-title span {
  width: 28px;
  height: 28px;
  display: grid;
  place-items: center;
  border-radius: 6px;
  background: #e0f2f1;
  color: var(--brand-dark);
  font-weight: 800;
  font-size: 13px;
}

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

.drop-zone {
  min-height: 150px;
  border: 1px dashed #9aa8b7;
  border-radius: 8px;
  background: var(--surface-2);
  display: grid;
  place-items: center;
  text-align: center;
  padding: 20px;
  cursor: pointer;
}

.drop-zone.secondary-upload {
  background: #fff;
  border-style: solid;
}

.drop-zone input {
  display: none;
}

.drop-icon {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  display: grid;
  place-items: center;
  background: var(--brand);
  color: #fff;
  font-size: 24px;
  margin-bottom: 8px;
}

.preview-shell {
  margin: 14px 0;
  min-height: 210px;
  border-radius: 8px;
  background: #111827;
  display: grid;
  place-items: center;
  overflow: hidden;
  color: #cbd5e1;
}

.preview-shell img {
  display: none;
  width: 100%;
  height: 100%;
  max-height: 360px;
  object-fit: contain;
}

.preview-shell.has-image img {
  display: block;
}

.preview-shell.has-image span {
  display: none;
}

.metric-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 12px;
}

.metric {
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 14px;
  background: var(--surface-2);
}

.metric strong {
  display: block;
  margin-top: 6px;
  font-size: 30px;
}

.metric.ok strong {
  color: var(--ok);
}

.metric.danger strong {
  color: var(--danger);
}

.detail-list {
  display: grid;
  gap: 10px;
  margin: 18px 0 0;
}

.detail-list div {
  display: grid;
  grid-template-columns: 100px minmax(0, 1fr);
  gap: 10px;
  border-top: 1px solid var(--line);
  padding-top: 10px;
}

.detail-list dt,
.detail-list dd {
  margin: 0;
}

.table-wrap {
  overflow: auto;
  border: 1px solid var(--line);
  border-radius: 8px;
}

table {
  width: 100%;
  border-collapse: collapse;
  min-width: 900px;
  background: var(--surface);
}

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

th {
  background: #eef5f5;
  font-size: 13px;
  color: #31505a;
}

.cell-text {
  display: block;
  max-width: 420px;
  line-height: 1.45;
  overflow-wrap: anywhere;
}

.state {
  display: inline-block;
  padding: 4px 8px;
  border-radius: 999px;
  font-size: 13px;
  font-weight: 700;
}

.state.ok {
  background: #dcfce7;
  color: #166534;
}

.state.bad {
  background: #ffedd5;
  color: #9a3412;
}

.empty-row td {
  text-align: center;
  color: var(--muted);
  padding: 34px;
}

.result-note {
  margin: 14px 0 0;
  color: var(--muted);
}

.processing-modal {
  position: fixed;
  inset: 0;
  z-index: 80;
  display: grid;
  place-items: center;
  padding: 20px;
  background: rgba(15, 23, 42, 0.48);
  backdrop-filter: blur(6px);
}

.processing-card {
  width: min(340px, 100%);
  display: grid;
  justify-items: center;
  gap: 10px;
  padding: 24px;
  border-radius: 8px;
  background: var(--surface);
  border: 1px solid var(--line);
  box-shadow: 0 22px 54px rgba(15, 23, 42, 0.22);
  text-align: center;
}

.processing-card strong {
  font-size: 20px;
}

.processing-card p,
.processing-card small {
  margin: 0;
}

.processing-card p {
  font-weight: 700;
  color: var(--danger);
}

.processing-card small {
  color: var(--muted);
  line-height: 1.5;
}

.processing-spinner {
  width: 46px;
  height: 46px;
  border-radius: 50%;
  border: 4px solid #dbe4ea;
  border-top-color: var(--brand);
  animation: processing-spin 0.9s linear infinite;
}

@keyframes processing-spin {
  to {
    transform: rotate(360deg);
  }
}

.admin-login-panel,
.admin-dashboard .result-panel {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 8px;
  box-shadow: var(--shadow);
  padding: 18px;
}

.admin-login-form {
  display: grid;
  grid-template-columns: minmax(220px, 1fr) minmax(160px, 0.55fr) auto;
  gap: 12px;
  align-items: end;
}

.admin-login-form label {
  display: grid;
  gap: 6px;
  color: var(--muted);
  font-size: 13px;
}

.admin-login-form input,
.admin-login-form select {
  width: 100%;
  min-height: 40px;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 9px 11px;
  color: var(--text);
  background: var(--surface);
}

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

.admin-tabs {
  display: flex;
  gap: 8px;
  border-bottom: 1px solid var(--line);
}

.admin-tab {
  min-height: 42px;
  border: 0;
  border-bottom: 3px solid transparent;
  background: transparent;
  color: var(--muted);
  padding: 0 14px;
  cursor: pointer;
  font-weight: 700;
}

.admin-tab.active {
  border-bottom-color: var(--brand);
  color: var(--text);
}

.admin-tab-view {
  display: grid;
  gap: 16px;
}

.admin-metric {
  background: #ecfdf5;
  border-color: #99f6e4;
}

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

.operator-card {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 12px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface-2);
}

.operator-card strong,
.operator-card span {
  display: block;
}

.operator-card span,
.operator-stats span {
  color: var(--muted);
  font-size: 13px;
}

.operator-stats {
  display: grid;
  grid-template-columns: repeat(4, max-content);
  gap: 12px;
  align-items: center;
  text-align: right;
}

.operator-stats strong {
  color: var(--brand-dark);
}

.admin-table {
  min-width: 860px;
}

.split-title {
  justify-content: space-between;
}

.split-title > div {
  display: flex;
  align-items: center;
  gap: 10px;
}

.permission-list {
  display: grid;
  gap: 10px;
  margin-top: 14px;
}

.permission-card {
  display: grid;
  grid-template-columns: minmax(220px, 1fr) minmax(140px, 180px) minmax(120px, max-content) auto;
  gap: 12px;
  align-items: end;
  padding: 12px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface-2);
}

.permission-person {
  display: grid;
  gap: 3px;
}

.permission-person span,
.permission-person small {
  color: var(--muted);
  overflow-wrap: anywhere;
}

.permission-card label {
  display: grid;
  gap: 6px;
  color: var(--muted);
  font-size: 13px;
}

.permission-card select {
  width: 100%;
  min-height: 40px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface);
  padding: 8px 10px;
  color: var(--text);
}

.permission-toggle {
  min-height: 40px;
  grid-template-columns: auto 1fr;
  align-items: center;
  color: var(--text) !important;
  font-weight: 700;
}

.permission-toggle input {
  width: 18px;
  height: 18px;
}

.mobile-link-badge {
  text-decoration: none;
}

@media (max-width: 980px) {
  body {
    padding-bottom: 76px;
  }

  .mobile-header {
    position: sticky;
    top: 0;
    z-index: 30;
    min-height: 68px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    padding: 10px 14px;
    background: #17212b;
    color: #fff;
    box-shadow: 0 8px 20px rgba(15, 23, 42, 0.18);
  }

  .brand.compact .brand-mark {
    width: 38px;
    height: 38px;
    border-radius: 7px;
  }

  .brand.compact h1 {
    font-size: 17px;
  }

  .brand.compact p {
    font-size: 12px;
    color: #a7b3c2;
  }

  .app-shell {
    grid-template-columns: 1fr;
    min-height: auto;
  }

  .sidebar {
    display: none;
  }

  .scan-grid,
  .settings-panel,
  .admin-login-form,
  .admin-summary-grid {
    grid-template-columns: 1fr;
  }

  .workspace {
    padding: 16px;
    gap: 14px;
  }

  .topbar {
    align-items: flex-start;
  }

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

  .admin-page .topbar-actions {
    width: 100%;
    display: grid;
    grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
  }

  .topbar h2 {
    font-size: 22px;
  }

  .admin-page .eyebrow {
    font-size: 12px;
  }

  .admin-tabs {
    overflow-x: auto;
    border: 1px solid var(--line);
    border-radius: 8px;
    background: var(--surface);
    padding: 4px;
  }

  .admin-tab {
    flex: 1 0 120px;
    border-bottom: 0;
    border-radius: 6px;
  }

  .admin-tab.active {
    background: #e0f2f1;
    color: var(--text);
  }

  .admin-summary-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 8px;
  }

  .upload-panel,
  .summary-panel,
  .result-panel,
  .settings-panel {
    box-shadow: none;
  }

  .drop-zone {
    min-height: 132px;
  }

  .upload-choice-grid {
    gap: 10px;
  }

  .preview-shell {
    min-height: 260px;
    max-height: 42vh;
  }

  .desktop-action {
    display: none;
  }

  .mobile-action-bar {
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    z-index: 40;
    display: grid;
    grid-template-columns: 1fr;
    gap: 10px;
    padding: 10px 14px calc(10px + env(safe-area-inset-bottom));
    background: rgba(255, 255, 255, 0.96);
    border-top: 1px solid var(--line);
    box-shadow: 0 -10px 24px rgba(15, 23, 42, 0.10);
    backdrop-filter: blur(10px);
  }

  .mobile-header-actions .badge {
    margin-top: 0;
  }

  .mobile-menu-btn {
    display: grid;
  }

  .mobile-menu {
    position: sticky;
    top: 68px;
    z-index: 29;
    display: grid;
    gap: 6px;
    padding: 10px 14px;
    background: #17212b;
    border-top: 1px solid rgba(203, 213, 225, 0.18);
    box-shadow: 0 12px 22px rgba(15, 23, 42, 0.20);
  }

  .mobile-menu.hidden {
    display: none;
  }

  .mobile-menu-item {
    min-height: 42px;
    display: flex;
    align-items: center;
    border: 0;
    border-radius: 8px;
    padding: 0 12px;
    background: #263442;
    color: #e5edf5;
    text-align: left;
    text-decoration: none;
    font-weight: 700;
  }

  .mobile-menu-item.active {
    background: var(--brand);
    color: #fff;
  }

  .mobile-menu-item:disabled {
    color: #94a3b8;
    opacity: 0.72;
  }

  .mobile-action-bar .primary-btn {
    min-height: 48px;
    padding: 0 10px;
  }

  .table-wrap {
    border: 0;
    overflow: visible;
  }

  table,
  thead,
  tbody,
  tr,
  th,
  td {
    display: block;
  }

  table {
    min-width: 0;
    background: transparent;
  }

  thead {
    display: none;
  }

  #resultBody {
    display: grid;
    gap: 12px;
  }

  #resultBody tr {
    border: 1px solid var(--line);
    border-radius: 8px;
    background: var(--surface);
    padding: 12px;
  }

  #resultBody td {
    display: grid;
    grid-template-columns: 72px minmax(0, 1fr);
    gap: 8px;
    align-items: center;
    border-bottom: 0;
    padding: 7px 0;
  }

  #resultBody td::before {
    color: var(--muted);
    font-size: 13px;
    font-weight: 700;
  }

  #resultBody td:nth-child(1)::before { content: "狀態"; }
  #resultBody td:nth-child(2)::before { content: "品號"; }
  #resultBody td:nth-child(3)::before { content: "規格"; }
  #resultBody td:nth-child(4)::before { content: "不良"; }
  #resultBody td:nth-child(5)::before { content: "需領"; }
  #resultBody td:nth-child(6)::before { content: "實領"; }
  #resultBody td:nth-child(7)::before { content: "備註"; }

  .cell-text {
    max-width: none;
  }

  .empty-row td {
    display: block;
    padding: 24px;
  }

  .empty-row td::before {
    content: "";
  }

  .admin-page {
    padding-bottom: 0;
  }

  .operator-card,
  .operator-stats {
    align-items: stretch;
    grid-template-columns: 1fr 1fr;
    text-align: left;
  }

  .operator-card {
    display: grid;
  }

  .operator-stats {
    gap: 8px;
  }

  .permission-card {
    grid-template-columns: 1fr;
    align-items: stretch;
  }
}

@media (max-width: 640px) {
  .workspace,
  .sidebar {
    padding: 12px;
  }

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

  .topbar-actions {
    width: 100%;
    display: grid;
    grid-template-columns: 1fr 44px;
  }

  .admin-page .topbar-actions {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .ghost-btn,
  .topbar-actions .primary-btn {
    width: 100%;
  }

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

  .admin-summary-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 8px;
  }

  .admin-page .topbar-actions {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .upload-choice-grid {
    grid-template-columns: 1fr;
  }

  .metric {
    padding: 10px;
  }

  .metric strong {
    font-size: 24px;
  }

  .detail-list div {
    grid-template-columns: 86px minmax(0, 1fr);
    font-size: 14px;
  }

  .section-title {
    margin-bottom: 12px;
  }

  .section-title h3 {
    font-size: 17px;
  }
}
