/* I-Perceive HTTP demo styles. Mobile-first, clean modern light theme. */

/* Ensure the HTML `hidden` attribute always wins over class-level
   `display: flex/grid/block` rules. Without this, e.g. `.point-section`
   stays visible despite `<div ... hidden>`, which lets users place a
   point without ever toggling `state.pointEnabled` -> the point is
   silently dropped on submit. */
[hidden] { display: none !important; }

:root {
  --bg: #f5f7fb;
  --surface: #ffffff;
  --surface-2: #f8fafc;
  --border: #e5e7eb;
  --text: #0f172a;
  --muted: #64748b;
  --primary: #2563eb;
  --primary-hover: #1d4ed8;
  --primary-soft: #dbeafe;
  --accent: #6366f1;
  --danger: #dc2626;
  --success: #16a34a;
  --shadow-sm: 0 1px 2px rgba(15, 23, 42, 0.06);
  --shadow-md: 0 8px 24px rgba(15, 23, 42, 0.08);
  --radius-sm: 8px;
  --radius-md: 12px;
  --radius-lg: 16px;
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC", "Hiragino Sans GB",
               "Microsoft YaHei", Roboto, Helvetica, Arial, sans-serif;
  color: var(--text);
  background: var(--bg);
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
  padding-bottom: env(safe-area-inset-bottom);
  min-height: 100dvh;
}

/* ---------------- Header ---------------- */
.site-header {
  position: sticky;
  top: 0;
  z-index: 50;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 12px 16px;
  padding-top: calc(12px + env(safe-area-inset-top));
  background: rgba(255, 255, 255, 0.92);
  backdrop-filter: saturate(180%) blur(12px);
  -webkit-backdrop-filter: saturate(180%) blur(12px);
  border-bottom: 1px solid var(--border);
}
.brand { display: flex; align-items: center; gap: 10px; min-width: 0; }
.brand-mark {
  display: inline-flex; align-items: center; justify-content: center;
  width: 36px; height: 36px;
  border-radius: 10px;
  background: linear-gradient(135deg, var(--primary), var(--accent));
  color: #fff;
  flex-shrink: 0;
}
.brand-text { display: flex; align-items: baseline; gap: 8px; min-width: 0; }
.brand-text h1 { margin: 0; font-size: 1.05rem; font-weight: 700; letter-spacing: -0.01em; }
.version {
  font-size: 0.72rem;
  color: var(--muted);
  background: var(--surface-2);
  border: 1px solid var(--border);
  padding: 2px 6px;
  border-radius: 999px;
  font-variant-numeric: tabular-nums;
}

.header-actions { display: flex; align-items: center; gap: 4px; }
.icon-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  min-width: 36px;
  height: 36px;
  padding: 0 8px;
  background: transparent;
  color: var(--text);
  border: 1px solid transparent;
  border-radius: 10px;
  cursor: pointer;
  text-decoration: none;
  font-size: 0.85rem;
  font-weight: 500;
  transition: background 0.15s, border-color 0.15s, color 0.15s;
}
.icon-btn:hover { background: var(--surface-2); border-color: var(--border); }
.icon-btn:focus-visible { outline: 2px solid var(--primary); outline-offset: 2px; }
.lang-btn { font-weight: 600; min-width: 44px; }

/* ---------------- Main ---------------- */
main {
  max-width: 960px;
  margin: 16px auto;
  padding: 0 14px 32px;
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-sm);
  padding: 18px;
}
.card-head { margin-bottom: 14px; }
.card-head h2 { margin: 0 0 4px; font-size: 1.1rem; font-weight: 700; letter-spacing: -0.01em; }
.muted { color: var(--muted); font-size: 0.9rem; margin: 0; }

.field { margin-top: 14px; }
.field:first-of-type { margin-top: 0; }
.field-label {
  display: block;
  font-size: 0.85rem;
  font-weight: 600;
  color: var(--text);
  margin-bottom: 6px;
}
.hint { color: var(--muted); font-size: 0.85rem; margin: 6px 0 0; }

/* ---------------- Image source row ---------------- */
.image-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  align-items: stretch;
}
.file-btn {
  position: relative;
  display: inline-flex;
  align-items: center;
  gap: 8px;
  cursor: pointer;
}
.file-btn input[type=file] {
  position: absolute; inset: 0;
  opacity: 0;
  cursor: pointer;
  font-size: 0;
}
.examples-wrap {
  position: relative;
  flex: 1;
  min-width: 200px;
  display: flex;
  align-items: center;
}
.examples-wrap select {
  width: 100%;
  height: 40px;
  padding: 0 32px 0 12px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 10px;
  font: inherit;
  color: var(--text);
  appearance: none;
  -webkit-appearance: none;
  background-image: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 24 24' fill='none' stroke='%2364748b' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'><polyline points='6 9 12 15 18 9'/></svg>");
  background-repeat: no-repeat;
  background-position: right 12px center;
}
.examples-wrap select:focus { outline: 2px solid var(--primary); outline-offset: 2px; }
.select-spinner {
  position: absolute;
  right: 36px;
  width: 16px; height: 16px;
  border: 2px solid var(--border);
  border-top-color: var(--primary);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ---------------- Buttons ---------------- */
.primary-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 0 16px;
  height: 40px;
  background: var(--primary);
  color: #fff;
  border: 0;
  border-radius: 10px;
  font: inherit;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.15s, transform 0.05s;
  box-shadow: 0 1px 2px rgba(37, 99, 235, 0.25);
}
.primary-btn:hover { background: var(--primary-hover); }
.primary-btn:active { transform: translateY(1px); }
.primary-btn:disabled { background: #94a3b8; cursor: not-allowed; box-shadow: none; }
.submit-btn { width: 100%; height: 46px; font-size: 1rem; }

.ghost-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 0 14px;
  height: 40px;
  background: var(--surface-2);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 10px;
  font: inherit;
  font-weight: 500;
  cursor: pointer;
  transition: background 0.15s, border-color 0.15s;
}
.ghost-btn:hover { background: #eef2f7; border-color: #cbd5e1; }
.ghost-btn.small { height: 32px; padding: 0 10px; font-size: 0.85rem; }

/* ---------------- Help icon (tap-to-toast) ---------------- */
.help-icon {
  width: 22px; height: 22px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  background: var(--surface-2);
  color: var(--muted);
  border: 1px solid var(--border);
  border-radius: 50%;
  font-size: 0.8rem;
  font-weight: 700;
  cursor: pointer;
  flex-shrink: 0;
  padding: 0;
}
.help-icon:hover { color: var(--primary); border-color: var(--primary); }

/* ---------------- Preview / thumbs ---------------- */
.preview-field .preview-header {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 0.9rem;
  color: var(--text);
  margin-bottom: 8px;
}
.thumbs {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(96px, 1fr));
  gap: 8px;
  padding: 10px;
  background: var(--surface-2);
  border: 1px dashed var(--border);
  border-radius: var(--radius-md);
  min-height: 110px;
}
.thumbs-empty {
  grid-column: 1 / -1;
  display: flex;
  align-items: center;
  justify-content: center;
  text-align: center;
  color: var(--muted);
  font-size: 0.9rem;
  padding: 22px 8px;
}
.thumb {
  position: relative;
  display: block;
  padding: 0;
  width: 100%;
  aspect-ratio: 1 / 1;
  border: 2px solid transparent;
  border-radius: 10px;
  overflow: hidden;
  background: #fff;
  cursor: pointer;
  transition: border-color 0.15s, transform 0.05s;
}
.thumb:hover { border-color: #cbd5e1; }
.thumb.start { border-color: var(--primary); box-shadow: 0 0 0 2px var(--primary-soft); }
.thumb img { width: 100%; height: 100%; object-fit: cover; display: block; }
.thumb-badge {
  position: absolute; top: 4px; left: 4px;
  background: var(--primary); color: #fff;
  font-size: 0.75rem; line-height: 1;
  padding: 3px 6px;
  border-radius: 999px;
  font-weight: 700;
}

/* ---------------- Instruction ---------------- */
textarea, input[type=text] {
  width: 100%;
  padding: 10px 12px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 10px;
  font: inherit;
  color: var(--text);
  resize: vertical;
}
textarea:focus, input[type=text]:focus, input[type=password]:focus, input[type=number]:focus {
  outline: 2px solid var(--primary); outline-offset: 2px;
  background: #fff;
}

/* ---------------- Action stack ---------------- */
.action-stack { display: flex; flex-direction: column; gap: 12px; }
.action-row { display: flex; align-items: center; gap: 10px; }
.action-row .ghost-btn { flex: 1; }

.point-section {
  display: flex;
  flex-direction: column;
  gap: 8px;
  padding: 12px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
}
.point-header {
  display: flex; align-items: center; justify-content: space-between; gap: 8px;
  flex-wrap: wrap;
}
#start-canvas {
  width: 100%;
  max-width: 480px;
  height: auto;
  aspect-ratio: 1 / 1;
  display: block;
  margin: 0 auto;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: #fff;
  cursor: crosshair;
  touch-action: manipulation;
}

/* ---------------- Status ---------------- */
.status {
  margin-top: 14px;
  padding: 10px 12px;
  border-radius: 10px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  font-size: 0.9rem;
  font-family: ui-monospace, "SF Mono", Menlo, Consolas, monospace;
  white-space: pre-wrap;
}
.status.error { background: #fee2e2; border-color: #fecaca; color: #991b1b; }
.status.success { background: #dcfce7; border-color: #bbf7d0; color: #166534; }

/* ---------------- Result viewer ---------------- */
.result-card .card-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  flex-wrap: wrap;
}
.result-card a { color: var(--primary); text-decoration: none; font-size: 0.9rem; }
.result-card a:hover { text-decoration: underline; }
.viewer-wrap {
  width: 100%;
  /* Mobile: portrait-friendly aspect ratio so the page can still scroll. */
  aspect-ratio: 9 / 14;
  max-height: 70vh;
  border-radius: var(--radius-md);
  overflow: hidden;
  background: #000;
}
#viser-frame { width: 100%; height: 100%; border: 0; display: block; }

/* ---------------- Toast ---------------- */
.toast {
  position: fixed;
  bottom: calc(20px + env(safe-area-inset-bottom));
  left: 50%;
  transform: translateX(-50%) translateY(20px);
  background: rgba(15, 23, 42, 0.92);
  color: #fff;
  padding: 10px 16px;
  border-radius: 999px;
  font-size: 0.9rem;
  box-shadow: var(--shadow-md);
  opacity: 0;
  transition: opacity 0.2s, transform 0.2s;
  z-index: 100;
  pointer-events: none;
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }

/* ---------------- Admin tweaks ---------------- */
table { width: 100%; border-collapse: collapse; margin-top: 10px; font-size: 0.9rem; }
th, td { padding: 8px 10px; border-bottom: 1px solid var(--border); text-align: left; vertical-align: top; }
th { background: var(--surface-2); font-weight: 600; }
.action-btn {
  background: #475569; color: #fff;
  padding: 4px 10px; border: 0; border-radius: 6px; cursor: pointer;
  font-size: 0.8rem; margin-right: 4px;
}
.action-btn:hover { background: #334155; }
.action-btn.danger { background: var(--danger); }
.action-btn.danger:hover { background: #b91c1c; }
code {
  background: var(--surface-2); padding: 2px 6px; border-radius: 4px;
  font-family: ui-monospace, "SF Mono", Menlo, Consolas, monospace;
  font-size: 0.85em;
}

/* ---------------- Larger screens ---------------- */
@media (min-width: 720px) {
  .site-header { padding: 14px 28px; padding-top: calc(14px + env(safe-area-inset-top)); }
  .brand-text h1 { font-size: 1.2rem; }
  main { padding: 0 24px 40px; gap: 20px; }
  .card { padding: 24px; }
  .viewer-wrap {
    aspect-ratio: 16 / 9;
    max-height: 70vh;
  }
}

@media (max-width: 480px) {
  .icon-btn span { display: none; }
  .lang-btn span { display: inline; }
  .image-actions { flex-direction: column; }
  .file-btn, .examples-wrap { width: 100%; }
}
