:root {
  --bg: #f5f7fa;
  --card: #ffffff;
  --text: #1f2937;
  --muted: #6b7280;
  --primary: #2563eb;
  --primary-hover: #1d4ed8;
  --secondary: #e5e7eb;
  --border: #e5e7eb;
  --success: #16a34a;
  --warning: #d97706;
  --danger: #dc2626;
  --shadow: 0 1px 3px rgba(0, 0, 0, 0.06), 0 1px 2px rgba(0, 0, 0, 0.04);
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Apple SD Gothic Neo", "Malgun Gothic", sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.5;
}

header {
  background: linear-gradient(135deg, #1e40af 0%, #2563eb 100%);
  color: white;
  padding: 32px 24px 0;
  text-align: center;
}
header h1 { margin: 0; font-size: 1.8rem; font-weight: 700; }
.subtitle { margin: 8px 0 16px; opacity: 0.9; font-size: 0.95rem; }

.tabs {
  display: flex;
  justify-content: center;
  gap: 4px;
  margin-top: 16px;
  align-items: center;
}
.tab-action {
  background: rgba(255,255,255,0.15);
  border: 1px solid rgba(255,255,255,0.3);
  color: white;
  padding: 6px 14px;
  border-radius: 6px;
  cursor: pointer;
  font-size: 0.85rem;
  margin-left: 8px;
}
.tab-action[hidden] { display: none !important; }
.tab-action:hover { background: rgba(255,255,255,0.25); }
.tab {
  background: rgba(255,255,255,0.1);
  border: none;
  color: white;
  padding: 12px 24px;
  border-radius: 8px 8px 0 0;
  cursor: pointer;
  font-size: 0.95rem;
  font-weight: 500;
  transition: background 0.15s;
}
.tab:hover { background: rgba(255,255,255,0.2); }
.tab.active { background: var(--bg); color: var(--text); }
.badge {
  display: inline-block;
  background: rgba(255,255,255,0.25);
  color: white;
  border-radius: 10px;
  padding: 1px 8px;
  font-size: 0.8rem;
  margin-left: 4px;
}
.tab.active .badge { background: var(--primary); color: white; }

main {
  max-width: 980px;
  margin: 0 auto 40px;
  padding: 20px 16px 0;
  display: flex;
  flex-direction: column;
  gap: 20px;
}

.tab-panel { display: none; flex-direction: column; gap: 20px; }
.tab-panel.active { display: flex; }

.card {
  background: var(--card);
  border-radius: 12px;
  padding: 24px;
  box-shadow: var(--shadow);
}
.card h2 { margin: 0 0 16px; font-size: 1.25rem; }
.card h3 { margin: 24px 0 8px; font-size: 1.05rem; color: var(--muted); }

/* uploader */
.guide {
  background: #f0f6ff;
  border-left: 3px solid var(--primary);
  border-radius: 6px;
  padding: 12px 16px;
  margin-bottom: 16px;
  font-size: 0.9rem;
}
.guide strong { display: block; margin-bottom: 6px; color: var(--text); }
.guide ul { margin: 0; padding-left: 20px; color: var(--muted); }
.guide li { margin: 2px 0; }

.drop-zone {
  display: block;
  border: 2px dashed var(--border);
  border-radius: 8px;
  padding: 48px 16px;
  text-align: center;
  color: var(--muted);
  cursor: pointer;
  transition: border-color 0.15s, background 0.15s;
}
.visually-hidden {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}
.drop-zone:hover, .drop-zone.dragover {
  border-color: var(--primary);
  background: #f0f6ff;
}

.preview {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 12px;
  margin-top: 16px;
}
.preview[hidden] { display: none !important; }
.tab-panel[hidden] { display: none !important; }
.meta-row[hidden], .summary-row[hidden] { display: none !important; }
.preview img {
  max-width: 100%;
  max-height: 360px;
  border-radius: 8px;
  border: 1px solid var(--border);
}

.btn-primary, .btn-secondary, .btn-danger {
  padding: 12px 20px;
  border: none;
  border-radius: 8px;
  font-size: 0.95rem;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.15s;
  text-decoration: none;
  display: inline-block;
  text-align: center;
}
.btn-primary { background: var(--primary); color: white; width: 100%; margin-top: 16px; padding: 14px 20px; font-size: 1rem; }
.btn-primary:hover:not(:disabled) { background: var(--primary-hover); }
.btn-primary:disabled { background: var(--border); color: var(--muted); cursor: not-allowed; }
.btn-secondary { background: var(--secondary); color: var(--text); }
.btn-secondary:hover { background: #d1d5db; }
.btn-danger { background: var(--danger); color: white; }
.btn-danger:hover { background: #b91c1c; }

.btn-text {
  background: none; border: none;
  color: var(--primary); cursor: pointer;
  font-size: 0.9rem; text-decoration: underline;
  padding: 4px 8px;
}

/* loading */
.spinner {
  width: 36px; height: 36px;
  margin: 0 auto 12px;
  border: 4px solid var(--border);
  border-top-color: var(--primary);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }
#loading { text-align: center; }

/* result */
.stage-box {
  background: #f0f6ff;
  border-left: 4px solid var(--primary);
  padding: 16px 20px;
  border-radius: 6px;
  display: flex;
  flex-direction: column;
  gap: 4px;
  margin-bottom: 20px;
}
.stage-box.stage-1 { background: #f0fdf4; border-color: var(--success); }
.stage-box.stage-2 { background: #fffbeb; border-color: var(--warning); }
.stage-box.stage-3 { background: #fef2f2; border-color: var(--danger); }
#stageLabel { font-size: 1.3rem; font-weight: 700; }

.meta-inputs {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 12px;
  margin-top: 16px;
}
.meta-inputs label { display: flex; flex-direction: column; font-size: 0.85rem; color: var(--muted); gap: 4px; }
.meta-inputs input, .meta-inputs select { padding: 8px 10px; border-radius: 6px; border: 1px solid var(--border); font-size: 0.95rem; }
.meta-hint { font-size: 0.78rem; margin: 8px 0 0; }

.meta-row {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
  gap: 12px;
  margin-bottom: 16px;
}
.meta {
  background: #f9fafb;
  padding: 10px 12px;
  border-radius: 6px;
  display: flex;
  flex-direction: column;
}
.meta-label { font-size: 0.8rem; color: var(--muted); }
.meta-value { font-size: 1.05rem; font-weight: 600; margin-top: 2px; }
.meta-value.pass { color: var(--success); }
.meta-value.fail { color: var(--danger); }

.angle-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.92rem;
}
.angle-table th, .angle-table td {
  padding: 8px 10px;
  text-align: left;
  border-bottom: 1px solid var(--border);
}
.angle-table th { background: #f9fafb; font-weight: 600; color: var(--muted); }
.angle-table td.dev-high { color: var(--danger); font-weight: 600; }
.angle-table td.dev-mid { color: var(--warning); font-weight: 600; }

.overlay-wrap { margin-top: 8px; text-align: center; }
.overlay-wrap img { max-width: 100%; border-radius: 8px; border: 1px solid var(--border); }

.action-row { display: flex; gap: 12px; justify-content: flex-end; align-items: center; margin-top: 16px; flex-wrap: wrap; }
.warning {
  margin-top: 16px;
  padding: 12px 16px;
  background: #fffbeb;
  border-left: 4px solid var(--warning);
  border-radius: 6px;
  font-size: 0.9rem;
}
.warning ul { margin: 6px 0 0; padding-left: 20px; }
.error { border-left: 4px solid var(--danger); }
.error h2 { color: var(--danger); }

/* history */
.history-header { display: flex; justify-content: space-between; align-items: center; flex-wrap: wrap; gap: 12px; }
.history-actions { display: flex; gap: 12px; align-items: center; }
.history-actions select { padding: 8px 12px; border-radius: 6px; border: 1px solid var(--border); font-size: 0.9rem; }

.summary-row {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 12px;
  margin: 16px 0;
}
.summary-item {
  background: #f9fafb;
  padding: 12px;
  border-radius: 8px;
  display: flex;
  flex-direction: column;
  align-items: center;
  border-left: 3px solid var(--border);
}
.summary-item.stage-1 { border-color: var(--success); }
.summary-item.stage-2 { border-color: var(--warning); }
.summary-item.stage-3 { border-color: var(--danger); }
.summary-item span:last-child { font-size: 1.5rem; font-weight: 700; margin-top: 4px; }
.summary-label { font-size: 0.8rem; color: var(--muted); }

.history-list {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
  gap: 12px;
  margin-top: 8px;
}
.history-card {
  background: #f9fafb;
  border: 1px solid var(--border);
  border-radius: 8px;
  overflow: hidden;
  cursor: pointer;
  transition: transform 0.1s, box-shadow 0.15s;
}
.history-card:hover { transform: translateY(-2px); box-shadow: var(--shadow); }
.history-card img { width: 100%; aspect-ratio: 1; object-fit: cover; background: #e5e7eb; display: block; }
.history-card-body { padding: 10px 12px; }
.history-card-stage { display: inline-block; padding: 2px 8px; border-radius: 4px; font-size: 0.75rem; font-weight: 600; }
.history-card-stage.stage-1 { background: #dcfce7; color: #14532d; }
.history-card-stage.stage-2 { background: #fef3c7; color: #78350f; }
.history-card-stage.stage-3 { background: #fee2e2; color: #7f1d1d; }
.history-card-time { font-size: 0.78rem; color: var(--muted); margin-top: 4px; }
.history-card-meta { font-size: 0.8rem; color: var(--muted); margin-top: 4px; }
.history-card-gt {
  display: inline-block; margin-top: 4px;
  font-size: 0.72rem;
  padding: 1px 6px;
  background: #e0e7ff; color: #3730a3;
  border-radius: 4px;
}

.center { text-align: center; }

/* modal */
.modal {
  position: fixed; inset: 0;
  background: rgba(0,0,0,0.5);
  display: flex; align-items: flex-start; justify-content: center;
  padding: 40px 16px;
  overflow-y: auto;
  z-index: 100;
}
.modal[hidden] { display: none !important; }
.modal-content.modal-small { max-width: 420px; }
#authForm { display: flex; flex-direction: column; gap: 12px; margin-top: 12px; }
#authPassword {
  padding: 12px 14px;
  border: 1px solid var(--border);
  border-radius: 6px;
  font-size: 1rem;
}
#authForm .btn-primary { width: 100%; margin-top: 0; }
.modal-content {
  background: var(--card);
  border-radius: 12px;
  max-width: 760px;
  width: 100%;
  box-shadow: 0 10px 30px rgba(0,0,0,0.2);
}
.modal-header {
  display: flex; justify-content: space-between; align-items: center;
  padding: 16px 24px;
  border-bottom: 1px solid var(--border);
}
.modal-header h2 { margin: 0; font-size: 1.2rem; }
.modal-close {
  background: none; border: none; font-size: 1.5rem; cursor: pointer; color: var(--muted);
}
.modal-body { padding: 24px; }

#detailMeta {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(140px, 1fr));
  gap: 10px;
  margin-bottom: 16px;
  font-size: 0.9rem;
}
#detailMeta > div { background: #f9fafb; padding: 8px 10px; border-radius: 6px; }
#detailMeta .label { font-size: 0.75rem; color: var(--muted); display: block; }

.gt-row { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.gt-row label { display: flex; flex-direction: column; gap: 4px; font-size: 0.85rem; color: var(--muted); }
.gt-row select { padding: 8px 12px; border-radius: 6px; border: 1px solid var(--border); font-size: 0.92rem; }

#notes {
  width: 100%;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: 6px;
  font-size: 0.92rem;
  font-family: inherit;
  resize: vertical;
}
.save-msg { margin-top: 8px; padding: 8px 12px; border-radius: 6px; background: #dcfce7; color: #14532d; font-size: 0.9rem; }
.save-msg.error { background: #fee2e2; color: #7f1d1d; }

footer {
  text-align: center;
  padding: 24px 16px;
  color: var(--muted);
  font-size: 0.85rem;
}
footer p { margin: 4px 0; }
.muted { color: var(--muted); font-size: 0.9rem; }
