:root {
  --bg: #f4f6f3;
  --surface: #ffffff;
  --surface-alt: #f9faf8;
  --border: #dfe5dc;
  --text: #1c241e;
  --text-muted: #5c6b60;
  --primary: #1b4332;
  --primary-hover: #2d6a4f;
  --accent: #d97706;
  --danger: #b91c1c;
  --danger-bg: #fee2e2;
  --ok: #15803d;
  --ok-bg: #dcfce7;
  --warn: #b45309;
  --warn-bg: #fef3c7;
  --radius: 12px;
  --shadow: 0 2px 10px rgba(27, 67, 50, 0.06);
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: "Noto Sans TC", "Microsoft JhengHei", system-ui, -apple-system, sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.5;
  word-break: keep-all;
}

.hidden { display: none !important; }

/* Login */
.login-wrap {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
  background: linear-gradient(135deg, #1b4332 0%, #2d6a4f 100%);
}
.login-card {
  width: min(420px, 100%);
  background: var(--surface);
  border-radius: 16px;
  padding: 32px 28px;
  box-shadow: 0 18px 40px rgba(0, 0, 0, 0.22);
}
.login-card h1 {
  margin: 0 0 6px;
  font-size: clamp(1.35rem, 2.2vw, 1.65rem);
  color: var(--primary);
}
.login-card p {
  margin: 0 0 22px;
  color: var(--text-muted);
  font-size: 0.95rem;
}

/* Header & Nav */
.topbar {
  background: var(--primary);
  color: #fff;
  padding: 12px 20px;
  position: sticky;
  top: 0;
  z-index: 20;
  box-shadow: 0 2px 12px rgba(0,0,0,0.12);
}
.topbar-inner {
  max-width: min(1320px, 96vw);
  margin: 0 auto;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}
.brand {
  font-weight: 700;
  font-size: clamp(1.05rem, 1.6vw, 1.25rem);
  letter-spacing: 0.02em;
}
.nav-tabs {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}
.nav-btn {
  background: rgba(255,255,255,0.12);
  color: #fff;
  border: none;
  padding: 8px 14px;
  border-radius: 8px;
  cursor: pointer;
  font-size: clamp(0.88rem, 1.1vw, 0.96rem);
  font-weight: 500;
  transition: background 0.15s;
}
.nav-btn:hover { background: rgba(255,255,255,0.24); }
.nav-btn.active { background: #fff; color: var(--primary); font-weight: 700; }

/* Main Container */
.container {
  max-width: min(1320px, 96vw);
  margin: 22px auto 48px;
  padding: 0 4px;
}

.section-header {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 16px;
}
.section-header h2 {
  margin: 0;
  font-size: clamp(1.15rem, 1.8vw, 1.45rem);
  color: var(--primary);
}
.btn-row {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

/* Cards & Grids */
.kpi-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(210px, 1fr));
  gap: 14px;
  margin-bottom: 20px;
}
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 18px;
  box-shadow: var(--shadow);
}
.kpi-label {
  font-size: 0.9rem;
  color: var(--text-muted);
  margin-bottom: 6px;
}
.kpi-value {
  font-size: clamp(1.5rem, 2.5vw, 2rem);
  font-weight: 700;
  color: var(--primary);
}
.kpi-sub {
  font-size: 0.84rem;
  color: var(--text-muted);
  margin-top: 4px;
}

/* Forms & Inputs */
.form-group { margin-bottom: 14px; }
.form-group label {
  display: block;
  font-size: 0.9rem;
  font-weight: 600;
  margin-bottom: 5px;
}
input[type="text"], input[type="number"], input[type="password"], select, textarea {
  width: 100%;
  padding: 9px 12px;
  border: 1px solid var(--border);
  border-radius: 8px;
  font-size: 0.95rem;
  font-family: inherit;
  background: #fff;
}
input:focus, select:focus, textarea:focus {
  outline: 2px solid var(--primary-hover);
  border-color: transparent;
}
.form-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
  gap: 12px;
}

/* Buttons */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 9px 16px;
  border-radius: 8px;
  border: 1px solid transparent;
  font-size: clamp(0.88rem, 1.1vw, 0.95rem);
  font-weight: 600;
  cursor: pointer;
  font-family: inherit;
  max-width: 100%;
}
.btn-primary { background: var(--primary); color: #fff; }
.btn-primary:hover { background: var(--primary-hover); }
.btn-secondary { background: var(--surface); color: var(--text); border-color: var(--border); }
.btn-secondary:hover { background: var(--surface-alt); }
.btn-accent { background: var(--accent); color: #fff; }
.btn-danger { background: var(--danger); color: #fff; }
.btn-sm { padding: 6px 10px; font-size: 0.84rem; }

/* Tables */
.table-wrap {
  width: 100%;
  overflow-x: auto;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}
table {
  width: 100%;
  border-collapse: collapse;
  min-width: 760px;
}
th, td {
  padding: 12px 14px;
  text-align: left;
  border-bottom: 1px solid var(--border);
  font-size: 0.92rem;
  vertical-align: middle;
}
th {
  background: var(--surface-alt);
  font-weight: 700;
  color: var(--primary);
}
tr:last-child td { border-bottom: none; }

/* Badges */
.badge {
  display: inline-flex;
  align-items: center;
  padding: 3px 9px;
  border-radius: 999px;
  font-size: 0.8rem;
  font-weight: 600;
  margin: 2px;
}
.badge-ok { background: var(--ok-bg); color: var(--ok); }
.badge-warn { background: var(--warn-bg); color: var(--warn); }
.badge-danger { background: var(--danger-bg); color: var(--danger); }
.badge-muted { background: #e5e7eb; color: #4b5563; }

/* Modals */
.modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.45);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
  z-index: 50;
}
.modal {
  background: var(--surface);
  border-radius: 14px;
  width: min(740px, 100%);
  max-height: 90vh;
  overflow-y: auto;
  padding: 24px;
  box-shadow: 0 20px 50px rgba(0,0,0,0.25);
}
.modal-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 16px;
}
.modal-header h3 { margin: 0; color: var(--primary); }

/* Toast */
.toast {
  position: fixed;
  bottom: 20px;
  right: 20px;
  background: #1c241e;
  color: #fff;
  padding: 12px 18px;
  border-radius: 10px;
  z-index: 90;
  box-shadow: 0 8px 24px rgba(0,0,0,0.25);
  max-width: min(420px, 90vw);
}

/* Print Slip Area */
.print-only { display: none; }

@media print {
  body { background: #fff; }
  .topbar, .container, .modal-backdrop, .toast { display: none !important; }
  .print-only { display: block !important; }
  .slip-page {
    page-break-after: always;
    padding: 12mm;
    border: 1px solid #000;
    margin-bottom: 10mm;
  }
  .slip-page:last-child { page-break-after: auto; }
  .slip-header {
    display: flex;
    justify-content: space-between;
    border-bottom: 2px solid #000;
    padding-bottom: 8px;
    margin-bottom: 12px;
  }
  .slip-table { width: 100%; border-collapse: collapse; margin-top: 10px; }
  .slip-table th, .slip-table td {
    border: 1px solid #333;
    padding: 6px 8px;
    font-size: 12pt;
  }
}
