:root {
  color-scheme: light;
  --bg: #f5f7f7;
  --surface: #ffffff;
  --surface-soft: #eef5f2;
  --ink: #182326;
  --muted: #66767b;
  --line: #d9e2e1;
  --primary: #16655f;
  --primary-strong: #0d4f49;
  --accent: #b56a19;
  --danger: #b73535;
  --warning: #d99a22;
  --ok: #2f7b45;
  --shadow: 0 18px 60px rgba(20, 39, 43, 0.1);
  --radius: 8px;
  --sidebar-width: 248px;
}

* {
  box-sizing: border-box;
}

html {
  min-height: 100%;
  background: var(--bg);
}

body {
  min-height: 100vh;
  margin: 0;
  color: var(--ink);
  background: var(--bg);
  font-family:
    "Noto Sans TC",
    "Microsoft JhengHei",
    system-ui,
    -apple-system,
    BlinkMacSystemFont,
    "Segoe UI",
    sans-serif;
  letter-spacing: 0;
}

button,
input,
select,
textarea {
  font: inherit;
  letter-spacing: 0;
}

button {
  cursor: pointer;
}

.mobile-appbar,
.drawer-backdrop {
  display: none;
}

.mobile-brand {
  display: flex;
  min-width: 0;
  align-items: center;
  gap: 10px;
}

.mobile-brand strong,
.mobile-brand span {
  display: block;
}

.mobile-brand strong {
  line-height: 1.15;
}

.mobile-brand span {
  margin-top: 2px;
  color: var(--muted);
  font-size: 0.78rem;
}

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

.sidebar {
  position: sticky;
  top: 0;
  display: flex;
  height: 100vh;
  flex-direction: column;
  gap: 28px;
  padding: 22px 16px;
  border-right: 1px solid var(--line);
  background: #fbfcfc;
}

.drawer-top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.drawer-close {
  display: none;
}

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

.brand img {
  flex: 0 0 auto;
}

.brand strong,
.brand span {
  display: block;
}

.brand strong {
  font-size: 1.05rem;
  line-height: 1.2;
}

.brand span {
  margin-top: 2px;
  color: var(--muted);
  font-size: 0.78rem;
}

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

.nav-item {
  display: grid;
  width: 100%;
  min-height: 42px;
  grid-template-columns: 28px 1fr;
  align-items: center;
  gap: 8px;
  border: 1px solid transparent;
  border-radius: var(--radius);
  background: transparent;
  color: #314044;
  text-align: left;
}

.nav-item:hover,
.nav-item:focus-visible {
  border-color: var(--line);
  background: #f2f6f5;
}

.nav-item.is-active {
  border-color: rgba(22, 101, 95, 0.2);
  background: #e7f2ef;
  color: var(--primary-strong);
  font-weight: 700;
}

.nav-icon {
  display: grid;
  width: 28px;
  height: 28px;
  place-items: center;
  color: currentColor;
  font-size: 1rem;
}

.sidebar-footer {
  display: grid;
  gap: 10px;
  margin-top: auto;
}

.connection-pill {
  display: inline-flex;
  min-height: 32px;
  align-items: center;
  justify-content: center;
  border: 1px solid var(--line);
  border-radius: 999px;
  color: var(--muted);
  font-size: 0.82rem;
}

.connection-pill.online {
  border-color: rgba(47, 123, 69, 0.25);
  background: rgba(47, 123, 69, 0.08);
  color: var(--ok);
}

.connection-pill.error {
  border-color: rgba(183, 53, 53, 0.3);
  background: rgba(183, 53, 53, 0.08);
  color: var(--danger);
}

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

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

.topbar h1,
.panel h2,
.modal h2 {
  margin: 0;
  line-height: 1.18;
}

.topbar h1 {
  font-size: clamp(1.55rem, 1.2rem + 1vw, 2.2rem);
}

.topbar p,
.panel p,
.modal p {
  margin: 6px 0 0;
  color: var(--muted);
}

.top-actions,
.button-row,
.toolbar,
.modal-actions,
.tool-list {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 10px;
}

.primary-button,
.secondary-button,
.ghost-button,
.icon-button {
  display: inline-flex;
  min-height: 38px;
  align-items: center;
  justify-content: center;
  gap: 8px;
  border: 1px solid transparent;
  border-radius: var(--radius);
  padding: 0 14px;
  transition:
    background-color 0.15s ease,
    border-color 0.15s ease,
    transform 0.15s ease;
}

.primary-button {
  background: var(--primary);
  color: #fff;
  font-weight: 700;
}

.primary-button:hover,
.primary-button:focus-visible {
  background: var(--primary-strong);
}

.secondary-button {
  border-color: rgba(22, 101, 95, 0.22);
  background: #eef7f4;
  color: var(--primary-strong);
  font-weight: 700;
}

.secondary-button:hover,
.secondary-button:focus-visible {
  background: #dceee9;
}

.ghost-button {
  border-color: var(--line);
  background: #fff;
  color: #34484d;
}

.ghost-button:hover,
.ghost-button:focus-visible {
  background: #f1f5f5;
}

.ghost-button.danger {
  color: var(--danger);
}

.icon-button {
  width: 40px;
  min-width: 40px;
  padding: 0;
  border-color: var(--line);
  background: #fff;
  color: #314044;
  font-size: 1.15rem;
}

.icon-button:hover,
.icon-button:focus-visible {
  background: #f0f5f4;
}

.view {
  display: none;
}

.view.is-active {
  display: block;
}

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

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

.summary-card {
  display: grid;
  min-height: 118px;
  align-content: space-between;
  padding: 18px;
}

.summary-card span {
  color: var(--muted);
  font-size: 0.88rem;
}

.summary-card strong {
  margin-top: 10px;
  font-size: 2rem;
  line-height: 1;
}

.summary-card em {
  color: var(--muted);
  font-size: 0.82rem;
  font-style: normal;
}

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

.settings-grid {
  align-items: start;
}

.locations-panel {
  grid-column: 1 / -1;
}

.panel {
  overflow: hidden;
}

.panel-heading {
  display: flex;
  min-height: 82px;
  align-items: flex-start;
  justify-content: space-between;
  gap: 18px;
  padding: 18px;
  border-bottom: 1px solid var(--line);
}

.panel-heading h2 {
  font-size: 1.08rem;
}

.panel-heading p {
  font-size: 0.9rem;
}

.panel-heading.with-tools {
  align-items: center;
}

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

.table-wrap.compact {
  overflow: hidden;
}

table {
  width: 100%;
  min-width: 760px;
  border-collapse: collapse;
}

.compact table {
  min-width: 0;
}

th,
td {
  height: 48px;
  padding: 10px 14px;
  border-bottom: 1px solid var(--line);
  text-align: left;
  vertical-align: middle;
}

th {
  background: #f7faf9;
  color: #405257;
  font-size: 0.78rem;
  font-weight: 800;
  text-transform: uppercase;
}

td {
  color: #203034;
  font-size: 0.92rem;
}

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

.matrix-table table {
  min-width: 980px;
}

.number-cell {
  font-variant-numeric: tabular-nums;
  text-align: right;
}

.item-main {
  display: grid;
  gap: 2px;
}

.item-main strong {
  font-size: 0.95rem;
}

.item-main span {
  color: var(--muted);
  font-size: 0.8rem;
}

.badge {
  display: inline-flex;
  min-height: 26px;
  align-items: center;
  border-radius: 999px;
  padding: 0 9px;
  border: 1px solid var(--line);
  background: #fff;
  color: #405257;
  font-size: 0.78rem;
  font-weight: 700;
  white-space: nowrap;
}

.badge.raw {
  border-color: rgba(181, 106, 25, 0.22);
  background: rgba(181, 106, 25, 0.08);
  color: #7a450d;
}

.badge.semi {
  border-color: rgba(22, 101, 95, 0.22);
  background: rgba(22, 101, 95, 0.08);
  color: var(--primary-strong);
}

.badge.finished {
  border-color: rgba(58, 82, 133, 0.22);
  background: rgba(58, 82, 133, 0.08);
  color: #304675;
}

.badge.warning {
  border-color: rgba(217, 154, 34, 0.35);
  background: rgba(217, 154, 34, 0.12);
  color: #80530a;
}

.badge.ok {
  border-color: rgba(47, 123, 69, 0.25);
  background: rgba(47, 123, 69, 0.1);
  color: var(--ok);
}

.actions-column {
  width: 126px;
}

.row-actions {
  display: flex;
  width: max-content;
  align-items: center;
  gap: 6px;
}

.row-actions .icon-button {
  width: 34px;
  min-width: 34px;
  min-height: 34px;
  font-size: 1rem;
}

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

.field span {
  color: #47595e;
  font-size: 0.8rem;
  font-weight: 800;
}

input,
select,
textarea {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: #fff;
  color: var(--ink);
  outline: none;
}

input,
select {
  min-height: 40px;
  padding: 0 11px;
}

textarea {
  padding: 10px 11px;
  resize: vertical;
}

input:focus,
select:focus,
textarea:focus {
  border-color: rgba(22, 101, 95, 0.55);
  box-shadow: 0 0 0 3px rgba(22, 101, 95, 0.12);
}

.search-field {
  min-width: min(280px, 100%);
}

.compact-field {
  min-width: 150px;
}

.stack-form {
  display: grid;
  gap: 14px;
  padding: 18px;
}

.tool-list {
  padding: 18px;
}

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

.location-tile {
  display: grid;
  gap: 10px;
  min-height: 146px;
  align-content: center;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: #fbfdfc;
  padding: 14px;
}

.location-edit-form {
  display: grid;
  gap: 10px;
}

.location-meta {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
}

.location-meta strong {
  font-size: 1.05rem;
}

.location-meta span {
  color: var(--muted);
  font-size: 0.86rem;
}

.location-edit-form .secondary-button {
  width: 100%;
  min-height: 38px;
}

.empty-or-list {
  display: grid;
  gap: 10px;
  padding: 18px;
}

.list-row {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  align-items: center;
  gap: 12px;
  min-height: 54px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 10px 12px;
  background: #fbfdfc;
}

.list-row strong,
.list-row span {
  display: block;
}

.list-row span {
  margin-top: 2px;
  color: var(--muted);
  font-size: 0.82rem;
}

.empty-state {
  display: grid;
  min-height: 120px;
  place-items: center;
  color: var(--muted);
  text-align: center;
}

.modal {
  width: min(720px, calc(100vw - 28px));
  padding: 0;
  border: 0;
  border-radius: var(--radius);
  background: transparent;
}

.modal::backdrop {
  background: rgba(7, 18, 20, 0.48);
}

.modal-card {
  display: grid;
  overflow: hidden;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: #fff;
  box-shadow: 0 30px 80px rgba(0, 0, 0, 0.2);
}

.modal-header {
  display: flex;
  justify-content: space-between;
  gap: 16px;
  padding: 18px;
  border-bottom: 1px solid var(--line);
}

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

.span-2 {
  grid-column: 1 / -1;
}

.modal-actions {
  justify-content: flex-end;
  padding: 16px 18px;
  border-top: 1px solid var(--line);
  background: #f9fbfb;
}

.toast {
  position: fixed;
  right: 22px;
  bottom: 22px;
  z-index: 10;
  max-width: min(420px, calc(100vw - 44px));
  transform: translateY(24px);
  border: 1px solid rgba(22, 101, 95, 0.25);
  border-radius: var(--radius);
  background: #fff;
  box-shadow: var(--shadow);
  color: #1f3337;
  opacity: 0;
  padding: 13px 15px;
  pointer-events: none;
  transition:
    opacity 0.2s ease,
    transform 0.2s ease;
}

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

.toast.error {
  border-color: rgba(183, 53, 53, 0.3);
  color: var(--danger);
}

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
}

@media (max-width: 1040px) {
  .summary-grid,
  .dashboard-grid,
  .settings-grid,
  .location-list {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .locations-panel {
    grid-column: auto;
  }
}

@media (max-width: 800px) {
  body.drawer-open {
    overflow: hidden;
  }

  .mobile-appbar {
    position: sticky;
    top: 0;
    z-index: 25;
    display: grid;
    grid-template-columns: 44px minmax(0, 1fr) 44px;
    align-items: center;
    gap: 10px;
    min-height: 64px;
    padding: 10px 12px;
    border-bottom: 1px solid var(--line);
    background: rgba(251, 252, 252, 0.96);
    backdrop-filter: blur(14px);
  }

  .menu-button {
    width: 44px;
    min-width: 44px;
    min-height: 44px;
    font-size: 1.2rem;
  }

  .drawer-backdrop {
    position: fixed;
    inset: 0;
    z-index: 30;
    display: block;
    background: rgba(7, 18, 20, 0.46);
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.18s ease;
  }

  .drawer-backdrop[hidden] {
    display: none;
  }

  .drawer-backdrop.show {
    opacity: 1;
    pointer-events: auto;
  }

  .app-shell {
    display: block;
    min-height: calc(100vh - 64px);
  }

  .sidebar {
    position: fixed;
    top: 0;
    left: 0;
    z-index: 40;
    width: min(320px, 86vw);
    height: 100dvh;
    gap: 22px;
    padding: 18px 14px;
    transform: translateX(-104%);
    border-right: 1px solid var(--line);
    border-bottom: 0;
    box-shadow: 24px 0 80px rgba(20, 39, 43, 0.18);
    transition:
      transform 0.2s ease,
      visibility 0.2s ease;
    visibility: hidden;
  }

  body.drawer-open .sidebar {
    transform: translateX(0);
    visibility: visible;
  }

  .drawer-close {
    display: inline-flex;
  }

  .brand {
    min-height: 42px;
  }

  .nav-list {
    grid-template-columns: 1fr;
    gap: 8px;
    overflow: visible;
  }

  .nav-item {
    min-height: 48px;
    grid-template-columns: 32px 1fr;
    justify-items: start;
    gap: 10px;
    padding: 0 10px;
    text-align: left;
  }

  .nav-icon {
    width: 32px;
    height: 32px;
  }

  .sidebar-footer {
    display: grid;
  }

  .main-panel {
    padding: 14px 12px 96px;
  }

  .topbar,
  .panel-heading.with-tools {
    display: grid;
  }

  .topbar {
    grid-template-columns: minmax(0, 1fr);
    min-height: auto;
    gap: 14px;
    margin-bottom: 14px;
  }

  .panel-heading.with-tools {
    grid-template-columns: minmax(0, 1fr);
  }

  .topbar h1 {
    font-size: 1.45rem;
  }

  .topbar p {
    font-size: 0.88rem;
    line-height: 1.5;
  }

  .top-actions,
  .toolbar {
    width: 100%;
  }

  .top-actions {
    position: sticky;
    top: 74px;
    z-index: 14;
    display: grid;
    grid-template-columns: minmax(0, 1fr) minmax(0, 1fr) 44px;
    gap: 8px;
    width: 100%;
    max-width: 100%;
    padding: 8px;
    border: 1px solid var(--line);
    border-radius: var(--radius);
    background: rgba(245, 247, 247, 0.94);
    backdrop-filter: blur(14px);
  }

  .top-actions .primary-button,
  .top-actions .secondary-button {
    min-height: 44px;
    padding: 0 10px;
    font-size: 0.92rem;
  }

  .top-actions .icon-button {
    width: 44px;
    min-width: 44px;
    min-height: 44px;
  }

  .summary-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 10px;
    margin-bottom: 14px;
  }

  .summary-card {
    min-height: 96px;
    padding: 14px;
  }

  .summary-card strong {
    font-size: 1.6rem;
  }

  .dashboard-grid,
  .settings-grid,
  .location-list {
    grid-template-columns: 1fr;
    gap: 12px;
  }

  .panel {
    box-shadow: 0 10px 32px rgba(20, 39, 43, 0.08);
  }

  .panel-heading {
    display: grid;
    min-height: auto;
    gap: 12px;
    padding: 14px;
  }

  .panel-heading h2 {
    font-size: 1rem;
  }

  .panel-heading p {
    font-size: 0.84rem;
    line-height: 1.5;
  }

  .toolbar {
    display: grid;
    grid-template-columns: 1fr;
    gap: 10px;
  }

  .search-field,
  .compact-field {
    min-width: 0;
  }

  .table-wrap,
  .table-wrap.compact {
    overflow: visible;
    padding: 12px;
  }

  table,
  .matrix-table table {
    min-width: 0;
  }

  table,
  thead,
  tbody,
  tr,
  td {
    display: block;
    width: 100%;
  }

  thead {
    display: none;
  }

  tbody {
    display: grid;
    gap: 10px;
  }

  tr {
    overflow: hidden;
    border: 1px solid var(--line);
    border-radius: var(--radius);
    background: #fff;
  }

  td {
    display: grid;
    grid-template-columns: minmax(78px, 0.36fr) minmax(0, 1fr);
    align-items: center;
    gap: 12px;
    height: auto;
    min-height: 42px;
    padding: 10px 12px;
    border-bottom: 1px solid var(--line);
    text-align: right;
  }

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

  td::before {
    content: attr(data-label);
    color: var(--muted);
    font-size: 0.78rem;
    font-weight: 800;
    text-align: left;
  }

  td[data-label="品項"] .item-main,
  td[data-label="品名"] .item-main {
    justify-items: end;
    text-align: right;
  }

  td[data-label="操作"] {
    grid-template-columns: 1fr;
  }

  td[data-label="操作"]::before {
    display: none;
  }

  td .empty-state {
    grid-column: 1 / -1;
  }

  .number-cell {
    text-align: right;
  }

  .row-actions {
    width: 100%;
    justify-content: flex-end;
  }

  .row-actions .icon-button {
    width: 42px;
    min-width: 42px;
    min-height: 42px;
  }

  .location-list,
  .empty-or-list,
  .stack-form,
  .tool-list {
    padding: 14px;
  }

  .toast {
    right: 12px;
    bottom: 14px;
    max-width: calc(100vw - 24px);
  }
}

@media (max-width: 620px) {
  .dashboard-grid,
  .settings-grid,
  .location-list,
  .form-grid {
    grid-template-columns: 1fr;
  }

  .span-2 {
    grid-column: auto;
  }

  .modal {
    width: 100vw;
    max-width: 100vw;
    height: 100dvh;
    max-height: 100dvh;
  }

  .modal-card {
    min-height: 100dvh;
    grid-template-rows: auto minmax(0, 1fr) auto;
    border-radius: 0;
  }

  .modal-header,
  .form-grid,
  .modal-actions {
    padding: 14px;
  }

  .form-grid {
    overflow: auto;
  }
}
