:root {
  --bg: #f4f6fb;
  --card: #ffffff;
  --text: #111827;
  --muted: #6b7280;
  --border: #dbe1ea;
  --primary: #2563eb;
  --primary-dark: #1d4ed8;
  --success: #16a34a;
  --success-dark: #15803d;
  --danger: #dc2626;
  --shadow: 0 10px 24px rgba(15, 23, 42, 0.08);
  --radius: 18px;
}

* { box-sizing: border-box; }
body {
  margin: 0;
  font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", "Microsoft JhengHei", sans-serif;
  background: var(--bg);
  color: var(--text);
}

.app-header {
  padding: 18px 16px;
  background: linear-gradient(135deg, #0f172a, #1e3a8a);
  color: white;
}
.app-header h1 {
  margin: 0;
  font-size: 24px;
}
.app-header p {
  margin: 6px 0 0;
  color: rgba(255,255,255,0.78);
  font-size: 14px;
}

.container {
  width: min(920px, 100%);
  margin: 0 auto;
  padding: 16px;
}

.tabs {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 8px;
  margin-bottom: 14px;
}
.tab {
  border: 1px solid var(--border);
  background: white;
  padding: 13px 8px;
  border-radius: 999px;
  font-size: 16px;
  font-weight: 700;
  color: var(--muted);
}
.tab.active {
  background: var(--primary);
  color: white;
  border-color: var(--primary);
}

.panel { display: none; }
.panel.active { display: block; }

.card {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px;
  margin-bottom: 14px;
  box-shadow: var(--shadow);
}
.danger-card {
  border-color: #fecaca;
  background: #fff7f7;
}

h2, h3 { margin: 0 0 12px; }
h2 { font-size: 20px; }
h3 { font-size: 17px; }

.section-title-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.field-label {
  display: block;
  margin-bottom: 8px;
  font-weight: 800;
  color: #1f2937;
}
.text-input, .textarea {
  width: 100%;
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 14px 13px;
  font-size: 17px;
  background: white;
  color: var(--text);
}
.text-input:focus, .textarea:focus {
  outline: 3px solid rgba(37, 99, 235, 0.18);
  border-color: var(--primary);
}

.inline-row {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 10px;
  align-items: center;
}

.btn {
  border: 0;
  border-radius: 14px;
  padding: 14px 16px;
  font-size: 17px;
  font-weight: 900;
  cursor: pointer;
  white-space: nowrap;
}
.btn:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}
.btn.primary { background: var(--primary); color: white; }
.btn.primary:hover { background: var(--primary-dark); }
.btn.secondary { background: #e5e7eb; color: #111827; }
.btn.success { background: var(--success); color: white; }
.btn.success:hover { background: var(--success-dark); }
.btn.danger { background: var(--danger); color: white; }
.btn.danger:hover { background: #b91c1c; }
.btn.danger-outline {
  background: white;
  border: 1px solid #fecaca;
  color: var(--danger);
}
.btn.small { padding: 8px 12px; font-size: 14px; }
.btn.wide { width: 100%; }

.scanner-actions {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
}
.scanner-box {
  margin-top: 12px;
  padding: 10px;
  border: 1px dashed var(--border);
  border-radius: 16px;
  background: #f8fafc;
}
#qrReader {
  width: 100%;
  overflow: hidden;
  border-radius: 14px;
}
.manual-add { margin-top: 16px; }
.manual-grid {
  display: grid;
  grid-template-columns: 1fr 150px auto;
  gap: 10px;
  align-items: center;
}
.pcs-input {
  width: 120px;
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 10px 11px;
  font-size: 15px;
}

.lot-list-wrap { margin-top: 18px; }
.lot-list {
  margin: 0;
  padding: 0;
  list-style: none;
}
.lot-list li {
  display: grid;
  grid-template-columns: 42px 1fr 120px auto;
  gap: 8px;
  align-items: center;
  padding: 12px 0;
  border-bottom: 1px solid #eef2f7;
}
.lot-list li:last-child { border-bottom: 0; }
.lot-index {
  width: 34px;
  height: 34px;
  display: grid;
  place-items: center;
  border-radius: 999px;
  background: #eff6ff;
  color: #1d4ed8;
  font-weight: 900;
}
.lot-id {
  font-size: 17px;
  font-weight: 800;
  word-break: break-all;
}
.empty-text {
  color: var(--muted);
  padding: 12px 0;
}
.lot-list.empty li {
  display: block;
  border: 0;
}

.badge {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 58px;
  border-radius: 999px;
  background: #eef2ff;
  color: #3730a3;
  font-weight: 900;
  padding: 6px 10px;
}
.hint {
  margin: 8px 0 0;
  color: var(--muted);
  font-size: 14px;
  line-height: 1.5;
}
.center { text-align: center; }
.hidden { display: none !important; }

.radio-group {
  display: flex;
  flex-wrap: wrap;
  gap: 12px;
  margin-bottom: 12px;
  font-weight: 800;
}
.radio-group label {
  background: #f8fafc;
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 10px 13px;
}
.result-card table {
  width: 100%;
  border-collapse: collapse;
  margin-top: 10px;
}
.result-card th, .result-card td {
  border-bottom: 1px solid #eef2f7;
  padding: 10px 8px;
  text-align: left;
  vertical-align: top;
}
.result-card th {
  background: #f8fafc;
  font-size: 14px;
}
.summary {
  display: grid;
  gap: 8px;
  margin-bottom: 12px;
}
.summary div {
  padding: 10px;
  border-radius: 12px;
  background: #f8fafc;
}

.toast {
  position: fixed;
  left: 50%;
  bottom: 24px;
  transform: translateX(-50%);
  max-width: min(92%, 520px);
  background: #111827;
  color: white;
  padding: 13px 16px;
  border-radius: 999px;
  box-shadow: 0 12px 30px rgba(0,0,0,0.24);
  font-weight: 800;
  z-index: 9999;
}
.toast.error { background: #991b1b; }
.toast.success { background: #166534; }

@media (max-width: 640px) {
  .app-header h1 { font-size: 21px; }
  .container { padding: 12px; }
  .tabs { grid-template-columns: 1fr; }
  .inline-row { grid-template-columns: 1fr; }
  .manual-grid { grid-template-columns: 1fr; }
  .scanner-actions { grid-template-columns: 1fr; }
  .btn { width: 100%; }
  .card { padding: 14px; border-radius: 16px; }
  .lot-list li { grid-template-columns: 34px 1fr; }
  .lot-list li .pcs-input { grid-column: 2 / -1; width: 100%; }
  .lot-list li .btn { grid-column: 2 / -1; }
}

/* 明細計算版 */
.manual-grid.detail-mode {
  grid-template-columns: 1fr 1fr 140px auto;
}

.pcs-detail-input {
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace;
  letter-spacing: 0.5px;
}

.pcs-preview {
  min-height: 48px;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: 14px;
  background: #f8fafc;
  font-weight: 900;
  color: #0f172a;
  white-space: nowrap;
}
.pcs-preview.invalid {
  border-color: #fecaca;
  background: #fef2f2;
  color: #b91c1c;
  white-space: normal;
  text-align: center;
}

.keypad {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 8px;
  margin-top: 10px;
}
.keypad button {
  border: 1px solid var(--border);
  border-radius: 14px;
  background: #f8fafc;
  color: #111827;
  padding: 13px 8px;
  font-size: 20px;
  font-weight: 900;
  cursor: pointer;
}
.keypad button:active {
  transform: translateY(1px);
  background: #e5e7eb;
}

.lot-list li.lot-row {
  grid-template-columns: 42px 1.1fr 1.2fr 130px auto;
}
.lot-edit-wrap {
  display: grid;
  gap: 5px;
}
.lot-edit-wrap label {
  color: var(--muted);
  font-size: 12px;
  font-weight: 800;
}
.lot-edit-wrap .text-input {
  padding: 10px 11px;
  font-size: 15px;
  border-radius: 12px;
}
.lot-id-input {
  font-weight: 800;
}

@media (max-width: 760px) {
  .manual-grid.detail-mode {
    grid-template-columns: 1fr;
  }
  .keypad {
    grid-template-columns: repeat(4, 1fr);
  }
  .lot-list li.lot-row {
    grid-template-columns: 34px 1fr;
  }
  .lot-list li.lot-row .lot-edit-wrap,
  .lot-list li.lot-row .pcs-preview,
  .lot-list li.lot-row .btn {
    grid-column: 2 / -1;
    width: 100%;
  }
}


/* Login overlay */
body.locked {
  overflow: hidden;
}

body.locked .app-header,
body.locked .container {
  filter: blur(2px);
  pointer-events: none;
  user-select: none;
}

.auth-overlay {
  position: fixed;
  inset: 0;
  z-index: 9999;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
  background: rgba(15, 23, 42, 0.38);
  backdrop-filter: blur(4px);
}

.auth-overlay.hidden {
  display: none;
}

.auth-card {
  width: min(420px, 100%);
  background: #ffffff;
  border-radius: 22px;
  padding: 26px;
  box-shadow: 0 18px 50px rgba(15, 23, 42, 0.25);
}

.auth-card h2 {
  margin: 0 0 8px;
  font-size: 24px;
}

.auth-card .text-input {
  margin: 16px 0 14px;
  text-align: center;
  font-size: 24px;
  letter-spacing: 4px;
}

.auth-message {
  margin: 12px 0 0;
  min-height: 22px;
  color: #dc2626;
  font-weight: 700;
  text-align: center;
}
