/* ── Reset & Variables ───────────────────────── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --blue: #003D79;
  --blue-light: #005AA3;
  --blue-bg: #E8F0F8;
  --gray-50: #F8F9FA;
  --gray-100: #E9ECEF;
  --gray-200: #DEE2E6;
  --gray-400: #868E96;
  --gray-600: #495057;
  --gray-800: #212529;
  --red: #DC3545;
  --green: #198754;
  --radius: 8px;
  --shadow: 0 1px 3px rgba(0,0,0,.1);
  --safe-bottom: env(safe-area-inset-bottom, 12px);
  font-size: 15px;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'PingFang SC', 'Microsoft YaHei', sans-serif;
  background: var(--gray-50);
  color: var(--gray-800);
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
  -webkit-tap-highlight-color: transparent;
}

/* ── Header ──────────────────────────────────── */
#appHeader {
  background: var(--blue);
  color: #fff;
  padding: 12px 16px;
  position: sticky;
  top: 0;
  z-index: 100;
  box-shadow: 0 2px 8px rgba(0,0,0,.15);
}
.header-top {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 12px;
}
.header-titles h1 {
  font-size: 1rem;
  font-weight: 600;
  line-height: 1.3;
}
.header-titles p {
  font-size: .75rem;
  opacity: .75;
  margin-top: 2px;
}
.btn-lang {
  background: rgba(255,255,255,.15);
  color: #fff;
  border: 1px solid rgba(255,255,255,.3);
  padding: 5px 12px;
  border-radius: var(--radius);
  font-size: .75rem;
  cursor: pointer;
  white-space: nowrap;
}
.btn-lang:active { background: rgba(255,255,255,.25); }

/* ── Step indicators ────────────────────────── */
.step-indicators {
  display: flex;
  gap: 4px;
  margin-top: 10px;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
}
.step-dot {
  flex: 1;
  min-width: 48px;
  text-align: center;
  padding: 6px 4px 4px;
  border-radius: var(--radius);
  cursor: pointer;
  opacity: .5;
  transition: opacity .2s;
  background: rgba(255,255,255,.1);
}
.step-dot span {
  display: block;
  width: 24px; height: 24px;
  line-height: 24px;
  border-radius: 50%;
  background: rgba(255,255,255,.2);
  font-size: .7rem;
  font-weight: 700;
  margin: 0 auto;
}
.step-dot small {
  display: block;
  font-size: .6rem;
  margin-top: 2px;
  white-space: nowrap;
}
.step-dot.active { opacity: 1; }
.step-dot.active span { background: #fff; color: var(--blue); }
.step-dot.done { opacity: .8; }
.step-dot.done span { background: var(--green); }

/* ── Recovery banner ───────────────────────── */
#recoverBanner {
  display: flex;
  background: #FFF3CD;
  color: #856404;
  padding: 10px 16px;
  font-size: .8rem;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  flex-wrap: wrap;
  border-bottom: 1px solid #FFE69C;
}
#recoverBanner span { flex: 1; min-width: 0; }
.recover-actions { display: flex; gap: 8px; }

/* ── Main content ────────────────────────────── */
#mainContent {
  flex: 1;
  padding: 16px;
  padding-bottom: calc(80px + var(--safe-bottom));
  max-width: 640px;
  width: 100%;
  margin: 0 auto;
}

/* ── Footer ──────────────────────────────────── */
#stepFooter {
  position: fixed;
  bottom: 0;
  left: 0; right: 0;
  background: #fff;
  border-top: 1px solid var(--gray-200);
  padding: 12px 16px calc(12px + var(--safe-bottom));
  display: flex;
  gap: 8px;
  justify-content: center;
  z-index: 50;
}

/* ── Buttons ─────────────────────────────────── */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 10px 20px;
  border: none;
  border-radius: var(--radius);
  font-size: .9rem;
  font-weight: 500;
  cursor: pointer;
  transition: background .15s, opacity .15s;
  min-height: 44px;
  white-space: nowrap;
}
.btn-primary { background: var(--blue); color: #fff; }
.btn-primary:active { background: var(--blue-light); }
.btn-outline { background: #fff; color: var(--blue); border: 1px solid var(--blue); }
.btn-outline:active { background: var(--blue-bg); }
.btn-ghost { background: transparent; color: var(--gray-600); }
.btn-ghost:active { background: var(--gray-100); }
.btn-sm { padding: 6px 12px; font-size: .75rem; min-height: 32px; }
.btn-scan { width: 100%; background: #fff; color: var(--blue); border: 2px dashed var(--blue); font-size: .95rem; }
.btn-scan:active { background: var(--blue-bg); }
.btn-icon { width: 32px; height: 32px; border-radius: 50%; border: none; background: var(--gray-100); cursor: pointer; font-size: .9rem; display: inline-flex; align-items: center; justify-content: center; }
.btn-icon:active { background: var(--gray-200); }

/* ── Forms ───────────────────────────────────── */
.form-group { margin-bottom: 14px; }
.form-group label { display: block; font-size: .8rem; font-weight: 600; color: var(--gray-600); margin-bottom: 4px; }
.form-group .required { color: var(--red); }
.form-group .hint { display: block; font-size: .7rem; color: var(--gray-400); margin-top: 2px; }
.form-group input[type="text"],
.form-group input[type="number"] {
  width: 100%;
  padding: 10px 12px;
  border: 1px solid var(--gray-200);
  border-radius: var(--radius);
  font-size: .9rem;
  background: #fff;
  transition: border-color .15s;
}
.form-group input:focus { outline: none; border-color: var(--blue); box-shadow: 0 0 0 3px rgba(0,61,121,.1); }

.scan-bar { margin-bottom: 16px; }

/* ── Step 2: Accessories ────────────────────── */
.step2-form { }
.section-title { font-size: .95rem; font-weight: 600; color: var(--blue); margin-bottom: 12px; padding-bottom: 6px; border-bottom: 2px solid var(--blue-bg); }

.acc-item {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 12px;
  background: #fff;
  border: 1px solid var(--gray-200);
  border-radius: var(--radius);
  margin-bottom: 8px;
}
.acc-label { flex: 1; display: flex; align-items: center; gap: 8px; font-size: .85rem; cursor: pointer; }
.acc-label input[type="checkbox"] { width: 18px; height: 18px; accent-color: var(--blue); }
.acc-qty { width: 50px; text-align: center; padding: 6px; border: 1px solid var(--gray-200); border-radius: 4px; font-size: .85rem; }
.qty-label { font-size: .7rem; color: var(--gray-400); }

/* ── Stepper ──────────────────────────────── */
.qty-stepper {
  display: flex;
  align-items: center;
  gap: 4px;
}
.qty-stepper .btn-icon {
  width: 28px; height: 28px;
  border-radius: 6px;
  border: 1px solid var(--gray-200);
  background: var(--gray-50);
  font-size: 1rem;
  font-weight: 600;
  color: var(--gray-600);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  -webkit-user-select: none;
  user-select: none;
}
.qty-stepper .btn-icon:active {
  background: var(--blue);
  color: #fff;
  border-color: var(--blue);
}
.qty-val {
  min-width: 24px;
  text-align: center;
  font-size: .9rem;
  font-weight: 600;
  color: var(--gray-800);
}

.others-group { margin-top: 16px; }
.others-group h4 { font-size: .8rem; color: var(--gray-600); margin-bottom: 8px; }
.other-row { display: flex; gap: 6px; align-items: center; margin-bottom: 6px; }
.other-name { flex: 1; padding: 8px; border: 1px solid var(--gray-200); border-radius: 4px; font-size: .85rem; }
.other-qty { width: 50px; padding: 8px; border: 1px solid var(--gray-200); border-radius: 4px; font-size: .85rem; text-align: center; }

.checkbox-row { margin-top: 16px; }
.checkbox-row label { display: flex; align-items: center; gap: 8px; font-size: .85rem; cursor: pointer; }
.checkbox-row input[type="checkbox"] { width: 18px; height: 18px; accent-color: var(--blue); }

/* ── Photo Grid ──────────────────────────────── */
.photo-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 10px;
}
.photo-slot {
  background: #fff;
  border: 1px solid var(--gray-200);
  border-radius: var(--radius);
  overflow: hidden;
  text-align: center;
}
.photo-preview {
  width: 100%;
  aspect-ratio: 3/4;
  background: var(--gray-50);
  overflow: hidden;
  display: flex;
  align-items: center;
  justify-content: center;
}
.photo-slot[data-orientation="portrait"] .photo-preview { aspect-ratio: 3/4; }
.photo-slot[data-orientation="landscape"] .photo-preview { aspect-ratio: 4/3; }
.photo-preview img {
  max-width: 100%;
  max-height: 100%;
  object-fit: contain;
}
.photo-placeholder {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  color: var(--gray-400);
}
.orient-icon { font-size: 1.4rem; }
.photo-placeholder small { font-size: .65rem; }
.photo-label {
  font-size: .7rem;
  font-weight: 600;
  padding: 6px 8px 2px;
  color: var(--gray-600);
}
.photo-actions {
  display: flex;
  gap: 4px;
  flex-wrap: wrap;
  justify-content: center;
  padding: 4px 8px 8px;
}

/* ── Preview ──────────────────────────────────── */
.preview-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
  margin-bottom: 16px;
}
.preview-thumb {
  background: #fff;
  border: 1px solid var(--gray-200);
  border-radius: var(--radius);
  overflow: hidden;
  text-align: center;
}
.preview-thumb img {
  width: 100%;
  aspect-ratio: 3/4;
  object-fit: contain;
}
.preview-thumb small {
  display: block;
  font-size: .55rem;
  padding: 2px 4px 0;
  color: var(--gray-600);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.preview-empty {
  aspect-ratio: 3/4;
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--gray-400);
  font-size: 1.2rem;
  background: var(--gray-50);
}
.preview-retake { margin: 4px; }

.summary-card {
  background: #fff;
  border: 1px solid var(--gray-200);
  border-radius: var(--radius);
  padding: 12px 16px;
  margin-top: 8px;
}
.summary-card div { font-size: .8rem; line-height: 1.8; }
.summary-card strong { color: var(--gray-600); margin-right: 8px; }

/* ── Generate ─────────────────────────────────── */
.generate-card {
  background: #fff;
  border: 1px solid var(--gray-200);
  border-radius: var(--radius);
  padding: 20px;
  text-align: center;
}
.generate-card code {
  display: block;
  background: var(--gray-50);
  padding: 8px 12px;
  border-radius: 4px;
  font-size: .8rem;
  margin: 10px 0;
  word-break: break-all;
}
.gen-actions { display: flex; gap: 10px; justify-content: center; margin-top: 16px; flex-wrap: wrap; }
.gen-status { margin-top: 16px; font-size: .85rem; }

/* ── Modals ──────────────────────────────────── */
.modal {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,.6);
  z-index: 200;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
}
.modal-content {
  background: #fff;
  border-radius: var(--radius);
  padding: 24px;
  max-width: 400px;
  width: 100%;
  box-shadow: 0 8px 30px rgba(0,0,0,.2);
}
.modal-content h3 { font-size: .9rem; margin-bottom: 12px; text-align: center; }

.cam-buttons { display: flex; flex-direction: column; gap: 10px; }
.cam-hint { font-size: .75rem; color: var(--gray-400); text-align: center; margin-bottom: 8px; }

.modal-crop { max-width: 95vw !important; padding: 12px !important; }
.crop-canvas-wrap { width: 100%; margin-bottom: 12px; }
.crop-canvas-wrap canvas { width: 100%; border-radius: 4px; }
.crop-buttons { display: flex; gap: 10px; justify-content: center; }

.scanner-reader {
  min-height: 200px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  text-align: center;
  color: var(--gray-600);
  font-size: .85rem;
}

/* ── Touch / iOS fixes ──────────────────────── */
input, select, button { -webkit-appearance: none; appearance: none; }

/* ── Tablet + ───────────────────────────────── */
@media (min-width: 768px) {
  :root { font-size: 16px; }
  #mainContent { max-width: 720px; }
  .photo-grid { grid-template-columns: repeat(3, 1fr); }
}

@media (min-width: 1024px) {
  .photo-grid { grid-template-columns: repeat(4, 1fr); }
}

/* ── Appearance sub-hints ─────────────────── */
.chk-sub-hint {
  font-size: .7rem;
  color: var(--gray-400);
  margin-left: 4px;
}
.chk-subs {
  padding: 4px 0 4px 28px;
  display: flex;
  gap: 6px;
  flex-wrap: wrap;
}
.chk-sub-tag {
  font-size: .72rem;
  padding: 2px 8px;
  border-radius: 4px;
  background: var(--blue-50);
  color: var(--blue);
  white-space: nowrap;
}

/* ── Checked state for checklist items ────── */
.acc-item.checked {
  border-color: var(--blue);
  background: var(--blue-50);
}
.acc-item.checked .acc-label span {
  color: var(--blue);
  font-weight: 500;
}

.appearance-scope-prompt p {
  color: var(--gray-600);
  font-size: .88rem;
  line-height: 1.6;
  margin-bottom: 18px;
}
.appearance-prompt-actions {
  display: flex;
  gap: 10px;
  justify-content: flex-end;
  flex-wrap: wrap;
}
