*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Noto Sans SC", "PingFang SC", "Microsoft YaHei", sans-serif;
  background: #f0f2f5;
  color: #1d1d1f;
  min-height: 100vh;
}

/* ── Header ── */
header {
  background: #fff;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 24px;
  height: 56px;
  box-shadow: 0 1px 4px rgba(0,0,0,0.08);
  position: sticky;
  top: 0;
  z-index: 100;
}
.header-left { display: flex; align-items: center; gap: 12px; }
.header-left h1 { font-size: 1.1rem; font-weight: 700; color: #1a1a2e; }
.header-right { display: flex; align-items: center; gap: 12px; }

.btn-back {
  background: none; border: none; cursor: pointer;
  font-size: 0.9rem; color: #667eea; padding: 4px 8px; border-radius: 6px;
}
.btn-back:hover { background: #f0f2f5; }

.save-indicator {
  font-size: 0.8rem; padding: 2px 10px; border-radius: 10px;
}
.save-indicator.saved { color: #34c759; background: #f0fdf4; }
.save-indicator.unsaved { color: #ff9500; background: #fff7ed; }
.save-indicator.saving { color: #007aff; background: #eff6ff; }

/* ── Main ── */
main {
  max-width: 900px;
  width: 94%;
  margin: 24px auto;
  padding-bottom: 40px;
}

/* ── Buttons ── */
.btn {
  padding: 8px 20px; border-radius: 8px; border: none;
  font-size: 0.9rem; font-weight: 600; cursor: pointer;
  transition: all 0.15s; font-family: inherit;
}
.btn:hover { opacity: 0.88; }
.btn:active { transform: scale(0.97); }
.btn-primary { background: linear-gradient(135deg, #667eea, #764ba2); color: #fff; }
.btn-outline { background: #fff; color: #667eea; border: 1px solid #667eea; }
.btn-outline:hover { background: #f8f7ff; }
.btn-cancel { background: #f0f2f5; color: #666; }
.btn-danger { background: #ff3b30; color: #fff; }
.btn-sm { padding: 4px 12px; font-size: 0.8rem; border-radius: 6px; border: none; cursor: pointer; transition: all 0.15s; font-family: inherit; }
.btn-edit { background: #e8f0fe; color: #1a73e8; }
.btn-edit:hover { background: #d2e3fc; }
.btn-delete { background: #fce8e6; color: #d93025; }
.btn-delete:hover { background: #f8d7da; }

/* ── List page ── */
.list-header {
  display: flex; gap: 12px; margin-bottom: 20px;
}
.search-box { flex: 1; }
.search-box input {
  width: 100%; padding: 10px 16px; border: 1px solid #d2d2d7;
  border-radius: 10px; font-size: 0.95rem; outline: none; font-family: inherit;
  transition: border-color 0.15s;
}
.search-box input:focus { border-color: #667eea; box-shadow: 0 0 0 3px rgba(102,126,234,0.12); }

.loading { text-align: center; color: #999; padding: 40px 0; font-size: 0.95rem; }

.card-grid { display: grid; gap: 14px; }
.card {
  background: #fff; border-radius: 12px; padding: 14px;
  cursor: pointer; transition: box-shadow 0.15s, transform 0.15s;
  box-shadow: 0 1px 3px rgba(0,0,0,0.05);
  display: flex; gap: 14px; align-items: flex-start;
}
.card:hover { box-shadow: 0 4px 12px rgba(0,0,0,0.1); transform: translateY(-1px); }
.card-img {
  width: 72px; height: 72px; flex-shrink: 0;
  border-radius: 8px; overflow: hidden; background: #f5f5f7;
}
.card-img img { width: 100%; height: 100%; object-fit: cover; }
.card-img-placeholder {
  width: 100%; height: 100%; display: flex; align-items: center;
  justify-content: center; font-size: 0.7rem; color: #b0b0b8;
}
.card-body { flex: 1; min-width: 0; display: flex; flex-direction: column; justify-content: center; gap: 2px; }
.card-title { font-size: 1rem; font-weight: 600; color: #1d1d1f; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.card-cost { font-size: 1.15rem; font-weight: 700; color: #667eea; white-space: nowrap; flex-shrink: 0; align-self: center; }
.card-meta { font-size: 0.78rem; color: #999; }
.card-actions { display: flex; gap: 4px; flex-shrink: 0; flex-direction: column; align-items: center; justify-content: center; }

/* ── Form page ── */
.form-page {
  background: #fff; border-radius: 12px; padding: 28px;
  box-shadow: 0 1px 3px rgba(0,0,0,0.05);
}
.form-page h2 { font-size: 1.2rem; margin-bottom: 20px; }
.form-group { margin-bottom: 16px; }
.form-group label { display: block; font-size: 0.88rem; font-weight: 600; margin-bottom: 6px; color: #444; }
.required { color: #ff3b30; }
.form-group input, .form-group textarea, .form-group select {
  width: 100%; padding: 10px 14px; border: 1px solid #d2d2d7;
  border-radius: 8px; font-size: 0.95rem; font-family: inherit; outline: none;
  transition: border-color 0.15s; background: #fff;
}
.form-group input:focus, .form-group textarea:focus, .form-group select:focus {
  border-color: #667eea; box-shadow: 0 0 0 3px rgba(102,126,234,0.12);
}
.form-actions { display: flex; gap: 10px; margin-top: 20px; }

/* ── Detail page ── */
.detail-page { display: flex; flex-direction: column; gap: 20px; }

.detail-header {
  background: #fff; border-radius: 12px; padding: 24px;
  box-shadow: 0 1px 3px rgba(0,0,0,0.05);
}
.detail-header-top {
  display: flex; gap: 24px; align-items: flex-start;
}
.detail-header-info { flex: 1; min-width: 0; }
.detail-header .info-group { margin-bottom: 14px; }
.detail-header label { display: block; font-size: 0.85rem; font-weight: 600; color: #666; margin-bottom: 4px; }
.detail-header input, .detail-header textarea {
  width: 100%; padding: 8px 12px; border: 1px solid #e5e5ea; border-radius: 8px;
  font-size: 0.95rem; font-family: inherit; outline: none;
}
.detail-header input:focus, .detail-header textarea:focus { border-color: #667eea; }
.detail-header input { font-weight: 600; font-size: 1.1rem; }

.image-upload {
  width: 200px; height: 200px; flex-shrink: 0;
  border: 2px dashed #d2d2d7; border-radius: 12px;
  overflow: hidden; cursor: pointer;
  transition: border-color 0.15s, box-shadow 0.15s;
  position: relative;
}
.image-upload:hover { border-color: #667eea; box-shadow: 0 0 0 3px rgba(102,126,234,0.1); }
.image-upload img {
  width: 100%; height: 100%; object-fit: cover;
}
.image-placeholder {
  width: 100%; height: 100%; display: flex; flex-direction: column;
  align-items: center; justify-content: center; gap: 6px;
  color: #b0b0b8; font-size: 0.9rem;
}
.image-placeholder span:first-child { font-size: 2rem; line-height: 1; }

/* ── Material section ── */
.material-section {
  background: #fff; border-radius: 12px; padding: 20px 24px;
  box-shadow: 0 1px 3px rgba(0,0,0,0.05);
}
.section-header {
  display: flex; justify-content: space-between; align-items: center; margin-bottom: 16px;
}
.section-header h3 { font-size: 1.05rem; }

.material-table { border: 1px solid #e5e5ea; border-radius: 10px; overflow: hidden; }

.material-header {
  display: grid;
  grid-template-columns: 36px 2fr 1fr 0.8fr 0.7fr 1.2fr 1fr 100px;
  background: #f8f9fa; padding: 10px 12px; font-size: 0.82rem; font-weight: 600; color: #666;
  align-items: center; gap: 4px;
}
.material-row {
  display: grid;
  grid-template-columns: 36px 2fr 1fr 0.8fr 0.7fr 1.2fr 1fr 100px;
  padding: 12px 12px; font-size: 0.9rem; align-items: center; gap: 4px;
  border-top: 1px solid #f0f0f5;
}
.material-row:hover { background: #fafbfc; }
.col-index { text-align: center; color: #999; font-size: 0.82rem; }
.col-subtotal { font-weight: 600; color: #667eea; }
.col-name { font-weight: 500; }

/* ── Material form ── */
.material-form {
  background: #fafbfc; border: 1px solid #e5e5ea; border-radius: 10px;
  padding: 20px;
}
.material-form h4 { font-size: 0.95rem; margin-bottom: 16px; }
.form-row { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; }

/* ── Cost section ── */
.cost-section {
  background: #fff; border-radius: 12px; padding: 20px 24px;
  box-shadow: 0 1px 3px rgba(0,0,0,0.05);
}
.cost-section h3 { font-size: 1.05rem; margin-bottom: 14px; }
.cost-card { border: 1px solid #e5e5ea; border-radius: 10px; overflow: hidden; }
.cost-row {
  display: flex; justify-content: space-between; align-items: center;
  padding: 12px 16px; border-bottom: 1px solid #f0f0f5;
}
.cost-row:last-child { border-bottom: none; }
.cost-row.cost-total {
  background: linear-gradient(135deg, #f8f7ff, #fef7ff);
  font-size: 1.1rem; font-weight: 700;
}
.cost-value { font-weight: 600; color: #667eea; }
.cost-extra input {
  width: 80px; padding: 6px 10px; border: 1px solid #d2d2d7; border-radius: 6px;
  font-size: 0.88rem; text-align: right; font-family: inherit; outline: none;
}
.cost-extra input:focus { border-color: #667eea; }

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

/* ── Empty state ── */
.empty { text-align: center; color: #b0b0b8; padding: 40px 0; font-size: 0.95rem; }

/* ── Toast ── */
.toast {
  position: fixed; bottom: 30px; left: 50%; transform: translateX(-50%);
  background: #1d1d1f; color: #fff; padding: 10px 28px; border-radius: 20px;
  font-size: 0.9rem; z-index: 1000; opacity: 0; transition: opacity 0.3s;
}
.toast.show { opacity: 1; }

/* ── Modal ── */
.modal-overlay {
  position: fixed; inset: 0; background: rgba(0,0,0,0.35);
  display: none; align-items: center; justify-content: center; z-index: 200;
}
.modal-overlay.show { display: flex; }
.modal-box {
  background: #fff; border-radius: 14px; padding: 28px; width: 360px; max-width: 90%;
  text-align: center; box-shadow: 0 8px 30px rgba(0,0,0,0.15);
}
.modal-box p { font-size: 1rem; margin-bottom: 20px; color: #333; }
.modal-actions { display: flex; gap: 12px; justify-content: center; }

/* ── Print / PDF Export ── */
@media print {
  header, .btn, .btn-sm, .card-actions, .card-hint, .save-indicator,
  .detail-actions, .section-header .btn, .material-section .btn,
  .detail-header .btn, .toast, .modal-overlay, .cost-extra input,
  #btnSaveInfo, #btnAddMaterial, #btnExportAll, #btnExportPdf, #btnExportSingle,
  #btnExportPdfDetail { display: none !important; }

  body { background: #fff; font-size: 13px; }
  main { max-width: 100%; width: 100%; margin: 0; padding: 0; }

  .card {
    break-inside: avoid; box-shadow: none; border: 1px solid #ddd;
    margin-bottom: 8px; padding: 10px 14px;
  }

  .detail-page { gap: 12px; }
  .detail-header, .material-section, .cost-section {
    box-shadow: none; border: 1px solid #ddd; padding: 14px;
  }

  .print-title {
    display: block !important; font-size: 1.3rem; font-weight: 700;
    text-align: center; margin-bottom: 16px;
  }

  .cost-row.cost-total { background: #f5f5f5 !important; }

  .material-header { background: #eee; }
  .material-row { border-color: #ddd; }
}

.print-title { display: none; }

/* ── Responsive ── */
@media (max-width: 640px) {
  main { width: 96%; margin: 12px auto; }
  .card { flex-direction: column; }
  .card-img { width: 100%; height: 100px; }
  .card-actions { flex-direction: row; }
  .list-header { flex-direction: column; }

  .material-header, .material-row {
    grid-template-columns: 28px 1.5fr 1fr 0.7fr 0.5fr 1fr;
  }
  .col-supplier, .col-actions { display: none; }

  .form-row { grid-template-columns: 1fr; }

  .detail-header, .material-section, .cost-section { padding: 16px; }
  .detail-header-top { flex-direction: column; align-items: center; }
  .image-upload { width: 160px; height: 160px; }
}
