/* ============================================================
   Modern posture UI — CSS
   ============================================================ */

:root {
  --bg: #f6f7fb;
  --bg-2: #ffffff;
  --surface: #ffffff;
  --border: rgba(15, 23, 42, 0.08);
  --border-strong: rgba(15, 23, 42, 0.16);
  --text: #0f172a;
  --text-soft: #475569;
  --muted: #94a3b8;
  --primary: #2563eb;
  --primary-2: #1d4ed8;
  --primary-soft: #eff6ff;
  --danger: #dc2626;
  --good: #16a34a;
  --warn: #d97706;
  --shadow: 0 10px 30px -10px rgba(15, 23, 42, 0.12);
  --radius: 16px;
  --radius-sm: 10px;
  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Noto Sans KR", sans-serif;
}

* {
  box-sizing: border-box;
}

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
}

body {
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  font-size: 15px;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
}

button {
  font: inherit;
  color: inherit;
  border: none;
  background: none;
}

/* ---------- Top bar ---------- */

.topbar {
  position: sticky;
  top: 0;
  z-index: 20;
  background: rgba(255, 255, 255, 0.82);
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
  border-bottom: 1px solid var(--border);
}

.topbar-inner {
  max-width: 1280px;
  margin: 0 auto;
  padding: 14px 20px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
}

.brand {
  display: flex;
  align-items: center;
  gap: 10px;
}

.brand-mark {
  font-size: 12px;
  font-weight: 700;
  letter-spacing: 0.08em;
  color: var(--text-soft);
  background: var(--border);
  padding: 4px 8px;
  border-radius: 6px;
}

.brand-name {
  font-weight: 600;
}

.topbar-status {
  color: var(--text-soft);
  font-size: 13px;
}

/* ---------- Layout ---------- */

.main {
  max-width: 1280px;
  margin: 0 auto;
  padding: 20px;
  display: grid;
  grid-template-columns: 1fr 320px;
  gap: 18px;
  align-items: start;
}

.stage {
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.sidebar {
  display: flex;
  flex-direction: column;
  gap: 14px;
}

/* ---------- Cards ---------- */

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

.canvas-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 16px;
  border-bottom: 1px solid var(--border);
}

.canvas-title,
.panel-title {
  font-weight: 600;
  font-size: 14px;
}

.canvas-actions {
  display: flex;
  gap: 8px;
}

.panel-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 16px;
  border-bottom: 1px solid var(--border);
}

.panel-body {
  padding: 14px 16px;
}

/* ---------- Buttons ---------- */

.btn-primary {
  width: 100%;
  padding: 10px 14px;
  background: var(--primary);
  color: #fff;
  border-radius: var(--radius-sm);
  font-weight: 600;
  font-size: 14px;
  cursor: pointer;
  transition: background 0.15s ease, transform 0.05s ease;
}

.btn-primary:hover:not(:disabled) {
  background: var(--primary-2);
}

.btn-primary:active:not(:disabled) {
  transform: scale(0.985);
}

.btn-primary:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}

.btn-ghost {
  padding: 6px 10px;
  color: var(--text-soft);
  border: 1px solid var(--border-strong);
  border-radius: 8px;
  font-size: 13px;
  font-weight: 500;
  cursor: pointer;
  transition: background 0.15s ease, color 0.15s ease;
}

.btn-ghost:hover {
  background: var(--primary-soft);
  color: var(--primary);
  border-color: var(--primary);
}

.btn-ghost-small {
  padding: 4px 8px;
  color: var(--text-soft);
  font-size: 12px;
  border-radius: 6px;
  cursor: pointer;
}

.btn-ghost-small:hover {
  background: var(--border);
}

/* ---------- Dropzone ---------- */

.dropzone {
  position: relative;
  min-height: 340px;
  display: flex;
  align-items: center;
  justify-content: center;
  border: 2px dashed var(--border-strong);
  border-radius: var(--radius-sm);
  background: var(--primary-soft);
  cursor: pointer;
  transition: border-color 0.15s ease, background 0.15s ease;
}

.dropzone:hover {
  border-color: var(--primary);
  background: #eef4ff;
}

.dropzone-inner {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  text-align: center;
  pointer-events: none;
}

.dropzone-icon {
  color: var(--primary);
  opacity: 0.9;
}

.dropzone-text {
  margin: 0;
  font-weight: 500;
}

.dropzone-sub {
  margin: 0;
  color: var(--text-soft);
  font-size: 13px;
}

/* ---------- Canvas ---------- */

.canvas-wrap {
  position: relative;
  min-height: 360px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: #0b0f1a;
  background-image:
    linear-gradient(rgba(255,255,255,0.025) 1px, transparent 1px),
    linear-gradient(90deg, rgba(255,255,255,0.025) 1px, transparent 1px);
  background-size: 24px 24px;
  border-top: 1px solid var(--border);
  border-bottom: 1px solid var(--border);
}

.pose-canvas {
  display: block;
  max-width: 100%;
  max-height: 620px;
  border-radius: 8px;
}

.analysis-preview {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  background: rgba(11, 15, 26, 0.82);
  backdrop-filter: blur(6px);
  -webkit-backdrop-filter: blur(6px);
  padding: 16px;
  border-radius: 8px;
}

.analysis-preview-head {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  margin-bottom: 12px;
}

.analysis-preview-title {
  font-weight: 600;
}

.analysis-preview-hint {
  color: var(--muted);
  font-size: 12px;
}

#analysisImage {
  max-width: 100%;
  max-height: 620px;
  border-radius: 8px;
  box-shadow: var(--shadow);
}

.analysis-preview[hidden],
.dropzone[hidden] {
  display: none;
}

/* ---------- Angle list ---------- */

.angle-list {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.angle-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 8px 12px;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 8px;
}

.angle-label {
  font-size: 13px;
  color: var(--text-soft);
}

.angle-value {
  font-weight: 600;
  font-variant-numeric: tabular-nums;
}

.angle-value.good { color: var(--good); }
.angle-value.warn { color: var(--warn); }
.angle-value.bad { color: var(--danger); }

.angle-empty {
  color: var(--muted);
  font-size: 13px;
  padding: 6px 0;
}

/* ---------- Control panel ---------- */

.control-body {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.control-note {
  font-size: 12px;
  color: var(--text-soft);
  line-height: 1.45;
}

/* ---------- Result panel ---------- */

.result-panel {
  border-color: var(--primary);
}

.result-body {
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.result-block {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.result-block-label {
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--muted);
  font-weight: 600;
}

.result-block-content {
  font-size: 14px;
}

.result-block-content ul {
  margin: 0;
  padding-left: 18px;
}

.result-block-content li {
  margin: 2px 0;
}

.result-unsafe-note {
  padding: 10px 12px;
  background: #fff7ed;
  border: 1px solid #fed7aa;
  border-radius: 8px;
  color: #9a3412;
  font-size: 13px;
}

.result-retake {
  padding: 10px 12px;
  background: #eff6ff;
  border: 1px solid #bfdbfe;
  border-radius: 8px;
  color: #1e3a8a;
  font-size: 13px;
}

/* ---------- Meta panel ---------- */

.meta-body {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.meta-row {
  display: flex;
  justify-content: space-between;
  font-size: 13px;
}

.meta-row span:first-child {
  color: var(--text-soft);
}

.meta-row span:last-child {
  font-weight: 500;
}

.muted {
  color: var(--muted);
}

/* ---------- Overlay ---------- */

/* The HTML hidden attribute must override display rules on overlay and its children.
   Without !important the element's own display:flex keeps it visible even when hidden
   is set (hidden 속성이 display:flex와 같은 CSS 규칙에게 무시되지 않게 !important로 강제한다). */
.overlay[hidden],
.overlay-progress[hidden],
.overlay-hint[hidden] {
  display: none !important;
}

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

.overlay-card {
  background: var(--surface);
  border-radius: var(--radius);
  padding: 22px 26px;
  min-width: 220px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 12px;
  box-shadow: var(--shadow);
}

.overlay-spinner {
  width: 32px;
  height: 32px;
  border: 3px solid var(--border-strong);
  border-top-color: var(--primary);
  border-radius: 50%;
  animation: spin 0.7s linear infinite;
}

.overlay-text {
  color: var(--text-soft);
  font-size: 14px;
}

.overlay-progress {
  width: 100%;
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.overlay-progress-track {
  width: 100%;
  height: 6px;
  background: var(--border);
  border-radius: 999px;
  overflow: hidden;
}

.overlay-progress-fill {
  height: 100%;
  width: 0%;
  background: var(--primary);
  border-radius: 999px;
  transition: width 0.15s ease;
}

.overlay-progress-label {
  text-align: center;
  font-size: 12px;
  color: var(--text-soft);
  font-variant-numeric: tabular-nums;
}

.overlay-hint {
  width: 100%;
  font-size: 12px;
  color: var(--muted);
  line-height: 1.5;
}

@keyframes spin {
  to { transform: rotate(360deg); }
}

/* ---------- Camera capture ---------- */

.camera-view {
  display: flex;
  flex-direction: column;
  gap: 12px;
  align-items: center;
  width: 100%;
}

.camera-view[hidden] {
  display: none !important;
}

.camera-video {
  width: 100%;
  max-width: 640px;
  border-radius: var(--radius-sm);
  background: #0b0f1a;
  /* Keep the video's native aspect ratio: forcing 3/4 letterboxeduards the image
     (비율을 강제하지 않아 카메라 화면이 늘어나거나 찌그러지지 않는다). */
  aspect-ratio: auto;
  object-fit: contain;
}

.camera-controls {
  display: flex;
  gap: 10px;
}

.camera-note,
.camera-license-note {
  font-size: 12px;
  color: var(--muted);
  line-height: 1.5;
  text-align: center;
  margin: 6px 0 0;
}

.camera-license-note a {
  color: var(--primary);
  text-decoration: none;
}

/* ---------- Panel footer / license link ---------- */

.panel-footer {
  padding: 10px 16px 14px;
  border-top: 1px solid var(--border);
}

.license-link {
  font-size: 12px;
  color: var(--muted);
  text-decoration: none;
}

.license-link:hover {
  color: var(--primary);
  text-decoration: underline;
}

/* ---------- Licenses page ---------- */

.licenses-main {
  grid-template-columns: 1fr;
}

.license-panel {
  max-width: 860px;
  margin: 0 auto;
  width: 100%;
}

.license-body h2 {
  font-size: 15px;
  margin: 18px 0 8px;
  color: var(--text);
}

.license-body p,
.license-body li {
  font-size: 14px;
  color: var(--text-soft);
  line-height: 1.7;
}

.license-body ul {
  padding-left: 20px;
  margin: 8px 0;
}

.license-body code {
  background: var(--primary-soft);
  padding: 1px 6px;
  border-radius: 5px;
  font-size: 12.5px;
}

.license-body a {
  color: var(--primary);
}

.license-note {
  margin-top: 20px;
  font-size: 12px;
  color: var(--muted);
}

/* ---------- Responsive ---------- */

@media (max-width: 880px) {
  .main {
    grid-template-columns: 1fr;
    padding: 12px;
    gap: 12px;
  }

  .sidebar {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 12px;
  }

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

  .canvas-wrap {
    min-height: 280px;
  }
}


/* Gemini 원문 전체 보기 */
.raw-response-block summary { cursor: pointer; user-select: none; }
.raw-response-text { white-space: pre-wrap; overflow-wrap: anywhere; font: 13px/1.55 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; max-height: 420px; overflow: auto; margin: 0; }


/* Compact posture result styling (간결한 자세 결과 스타일) */
.posture-rating {
  padding: 12px 14px;
  border: 1px solid var(--border, #dbe2ea);
  border-radius: 12px;
  text-align: center;
}
.rating-stars {
  margin: 5px 0 2px;
  font-size: 28px;
  letter-spacing: 4px;
  color: #f59e0b;
  line-height: 1.3;
}
.rating-caption {
  color: var(--muted);
  font-size: 12px;
  line-height: 1.45;
}
.result-dots, .result-middle-dots {
  margin: 0;
  padding-left: 0;
  list-style: none;
}
.result-dots li, .result-middle-dots li {
  position: relative;
  margin: 8px 0;
  padding-left: 17px;
  line-height: 1.55;
}
.result-dots li::before {
  content: "•";
  position: absolute;
  left: 2px;
  top: 0;
  font-weight: 700;
  color: var(--primary);
}
.result-middle-dots li::before {
  content: "·";
  position: absolute;
  left: 3px;
  top: 0;
  font-size: 22px;
  line-height: 1.15;
  color: var(--muted);
}
.result-details summary { cursor: pointer; }

/* Camera stays inside the existing black image frame (기존 검은 이미지 표시 틀 안에 카메라를 표시한다). */
body.camera-active .camera-view {
  position: absolute;
  inset: 0;
  z-index: 20;
  width: 100%;
  height: 100%;
  min-height: 360px;
  padding: 0;
  margin: 0;
  display: flex;
  align-items: stretch;
  justify-content: stretch;
  gap: 0;
  overflow: hidden;
  background: #0b0f1a;
}
body.camera-active .camera-video {
  display: block;
  width: 100%;
  height: 100%;
  max-width: none;
  max-height: none;
  aspect-ratio: auto;
  object-fit: cover;
  border-radius: 0;
}
body.camera-active .camera-controls {
  position: absolute;
  bottom: 16px;
  left: 0;
  right: 0;
  justify-content: center;
  z-index: 2;
}
body.camera-active .camera-note { display: none; }


/* ============================================================
   v12 — metrics panel, clean result card, dialog, PDF report
   (지표 패널, 깔끔한 결과 카드, 대화상자, PDF 리포트)
   ============================================================ */

.btn-block { width: 100%; padding: 10px 14px; font-size: 14px; }
.btn-ghost:disabled { opacity: 0.45; cursor: not-allowed; pointer-events: none; }
.dropzone.is-dragover { border-color: var(--primary); background: #eef4ff; }
.pose-canvas { touch-action: none; } /* lets fingers drag points without scrolling the page (손가락으로 점을 끌 때 페이지가 스크롤되지 않게) */

/* Status chips (상태 칩) */
.chip {
  display: inline-block;
  padding: 2px 8px;
  border-radius: 999px;
  font-size: 11px;
  font-weight: 600;
  line-height: 1.5;
  white-space: nowrap;
}
.chip-good { background: #dcfce7; color: #166534; }
.chip-warn { background: #fef3c7; color: #92400e; }
.chip-bad  { background: #fee2e2; color: #991b1b; }

/* Metrics (지표 목록) */
.view-badge {
  display: inline-block;
  margin-bottom: 10px;
  padding: 3px 10px;
  border-radius: 999px;
  background: var(--primary-soft);
  color: var(--primary-2);
  font-size: 12px;
  font-weight: 600;
}
.metric-list { display: flex; flex-direction: column; }
.metric-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 9px 0;
  border-bottom: 1px solid var(--border);
}
.metric-row:last-child { border-bottom: none; }
.metric-main { display: flex; flex-direction: column; min-width: 0; }
.metric-label { font-size: 13px; font-weight: 600; }
.metric-note { font-size: 11px; color: var(--muted); line-height: 1.4; }
.metric-side { display: flex; flex-direction: column; align-items: flex-end; gap: 3px; flex-shrink: 0; }
.metric-value { font-size: 15px; font-weight: 700; font-variant-numeric: tabular-nums; }
.metric-foot { margin-top: 10px; font-size: 11px; color: var(--text-soft); line-height: 1.5; }

/* Result card (결과 카드) */
.result-body-wrap, #resultBody { display: flex; flex-direction: column; gap: 16px; }

.result-hero {
  --accent: #d97706;
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 14px;
  border-radius: 14px;
  background: var(--bg);
  border: 1px solid var(--border);
}
.result-hero.rating-5, .result-hero.rating-4 { --accent: #16a34a; }
.result-hero.rating-3 { --accent: #d97706; }
.result-hero.rating-2, .result-hero.rating-1 { --accent: #dc2626; }
.result-hero .score {
  flex-shrink: 0;
  width: 64px;
  height: 64px;
  border-radius: 50%;
  display: flex;
  align-items: baseline;
  justify-content: center;
  padding-top: 15px;
  border: 4px solid var(--accent);
  background: var(--surface);
  color: var(--accent);
}
.score-num { font-size: 26px; font-weight: 800; line-height: 1; }
.score-den { font-size: 12px; font-weight: 600; opacity: 0.8; }
.hero-title { font-size: 16px; font-weight: 700; }
.hero-sub { margin-top: 2px; font-size: 13px; color: var(--text-soft); line-height: 1.5; }

.result-sec h4 {
  margin: 0 0 6px;
  font-size: 12px;
  font-weight: 700;
  color: var(--text-soft);
  letter-spacing: 0.02em;
}
.result-list { margin: 0; padding: 0; list-style: none; display: flex; flex-direction: column; gap: 6px; }
.result-list li {
  position: relative;
  padding: 8px 10px 8px 26px;
  background: var(--bg);
  border-radius: 8px;
  font-size: 13.5px;
  line-height: 1.55;
}
.result-list li::before {
  content: "";
  position: absolute;
  left: 11px;
  top: 15px;
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--primary);
}
.result-more summary { cursor: pointer; font-size: 12px; font-weight: 700; color: var(--text-soft); margin-bottom: 6px; }

/* In-app dialog (앱 내 대화상자) */
.dialog-backdrop {
  position: fixed;
  inset: 0;
  z-index: 60;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
  background: rgba(15, 23, 42, 0.5);
}
.dialog-backdrop[hidden] { display: none; }
.dialog-card {
  width: min(380px, 100%);
  padding: 20px;
  border-radius: var(--radius);
  background: var(--surface);
  box-shadow: 0 20px 50px -12px rgba(15, 23, 42, 0.4);
}
.dialog-title { margin: 0 0 8px; font-size: 17px; }
.dialog-message { margin: 0 0 18px; font-size: 14px; color: var(--text-soft); line-height: 1.6; }
.dialog-actions { display: flex; justify-content: flex-end; gap: 8px; }
.dialog-actions .btn-ghost { padding: 9px 14px; }
.dialog-ok { width: auto; padding: 9px 16px; }

/* ---------- PDF report ---------- */
#printReport { display: none; }

@media print {
  @page { size: A4; margin: 14mm; }

  html, body { height: auto; background: #fff !important; }
  body > *:not(#printReport) { display: none !important; }

  #printReport {
    display: block;
    color: #0f172a;
    font-family: var(--font);
    font-size: 11pt;
    line-height: 1.55;
    -webkit-print-color-adjust: exact;
    print-color-adjust: exact;
  }
  #printReport * { box-sizing: border-box; }

  .rp-head { display: flex; justify-content: space-between; align-items: flex-end; padding-bottom: 8pt; margin-bottom: 14pt; border-bottom: 2pt solid #2563eb; }
  .rp-brand { font-size: 9pt; font-weight: 800; letter-spacing: 0.18em; color: #2563eb; }
  .rp-head h1 { margin: 2pt 0 0; font-size: 20pt; }
  .rp-meta { font-size: 9pt; color: #64748b; text-align: right; }

  .rp-grid { display: grid; grid-template-columns: 78mm 1fr; gap: 12pt; align-items: start; margin-bottom: 4pt; }
  .rp-photo { margin: 0; }
  .rp-photo img { width: 100%; max-height: 105mm; object-fit: contain; border-radius: 6pt; border: 1pt solid #e2e8f0; background: #f1f5f9; }

  .rp-section { break-inside: avoid; margin-bottom: 12pt; }
  .rp-section h2 { margin: 0 0 6pt; padding-left: 7pt; border-left: 3pt solid #2563eb; font-size: 13pt; }
  .rp-section h3 { margin: 8pt 0 3pt; font-size: 10.5pt; color: #475569; }
  .rp-section ul { margin: 0; padding-left: 16pt; }
  .rp-section li { margin: 2pt 0; }

  .rp-table { width: 100%; border-collapse: collapse; }
  .rp-table td { padding: 5pt 4pt; border-bottom: 0.5pt solid #e2e8f0; vertical-align: middle; }
  .rp-table tr { break-inside: avoid; }
  .rp-num { text-align: right; font-weight: 700; white-space: nowrap; font-variant-numeric: tabular-nums; }
  .rp-note { font-size: 8.5pt; color: #94a3b8; }
  .rp-muted { font-size: 9pt; color: #64748b; margin: 6pt 0 0; }

  .rp-score { display: flex; align-items: center; gap: 10pt; padding: 8pt 10pt; margin-bottom: 4pt; background: #f1f5f9; border-radius: 8pt; }
  .rp-score-num { font-size: 22pt; font-weight: 800; color: #2563eb; white-space: nowrap; }
  .rp-score-num small { font-size: 10pt; font-weight: 600; }

  .rp-foot { margin-top: 10pt; padding-top: 8pt; border-top: 0.5pt solid #cbd5e1; font-size: 8.5pt; color: #64748b; }
}

/* Header buttons: keep one line, wrap as a group on narrow screens
   (헤더 버튼: 글자가 세로로 꺾이지 않게 하고, 좁은 화면에서는 버튼 단위로 줄바꿈) */
.canvas-header { flex-wrap: wrap; gap: 10px; }
.canvas-actions { flex-wrap: wrap; justify-content: flex-end; }
.canvas-actions .btn-primary { width: auto; padding: 7px 12px; font-size: 13px; }
.canvas-actions .btn-ghost { white-space: nowrap; }
.canvas-actions .btn-primary { white-space: nowrap; }
