:root {
  --bg: #10131e;
  --bg-raise: #161a29;
  --bg-card: #1c2135;
  --line: #2a3049;
  --text: #f2f3f9;
  --text-dim: #97a0bd;
  --accent: #ff9f3a;
  --accent-2: #f43f6b;
  --violet: #8b5cf6;
  --grad: linear-gradient(135deg, #ffb03a, #f97341 55%, #f43f6b);
  --good: #34d399;
  --warn: #fbbf24;
  --bad: #f87171;
  --stamp: #ffd60a;
  --radius: 14px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

* { box-sizing: border-box; margin: 0; padding: 0; -webkit-tap-highlight-color: transparent; }

html, body {
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", sans-serif;
  overflow: hidden;
}

.hidden { display: none !important; }

/* ---------- Views ---------- */
.view { position: fixed; inset: 0; display: none; flex-direction: column; background: var(--bg); }
.view.active { display: flex; }

/* ---------- Camera stage ---------- */
.camera-stage { position: absolute; inset: 0; background: #000; overflow: hidden; }
#camera-feed { width: 100%; height: 100%; object-fit: cover; }
.detect-overlay { position: absolute; inset: 0; width: 100%; height: 100%; pointer-events: none; }

.camera-placeholder {
  position: absolute; inset: 0; display: flex; flex-direction: column;
  align-items: center; justify-content: center; gap: 14px; text-align: center;
  padding: 24px; background: radial-gradient(ellipse at center, #101a30 0%, #0a0e17 70%);
}
.placeholder-icon { font-size: 52px; opacity: .8; }
.camera-placeholder p { color: var(--text-dim); max-width: 300px; }

/* ---------- Top bar ---------- */
.topbar {
  position: absolute; top: 0; left: 0; right: 0; z-index: 5;
  display: flex; align-items: center; justify-content: space-between;
  padding: calc(10px + var(--safe-top)) 14px 10px;
  background: linear-gradient(to bottom, rgba(4,8,16,.75), transparent);
}
.topbar-right { display: flex; gap: 8px; }
.brand { font-weight: 800; letter-spacing: .5px; font-size: 17px; text-shadow: 0 1px 6px rgba(0,0,0,.6); }
.brand-kai {
  background: var(--grad);
  -webkit-background-clip: text; background-clip: text; color: transparent;
}
.brand-ai {
  background: linear-gradient(90deg, #c084fc, var(--violet));
  -webkit-background-clip: text; background-clip: text; color: transparent;
}

/* ---------- Buttons ---------- */
.icon-btn {
  background: rgba(18,26,43,.72); border: 1px solid rgba(255,255,255,.12); color: var(--text);
  width: 42px; height: 42px; border-radius: 50%; font-size: 18px; cursor: pointer;
  display: flex; align-items: center; justify-content: center;
  backdrop-filter: blur(8px); transition: transform .12s ease, background .12s ease;
}
.icon-btn:active { transform: scale(.92); }
.icon-btn.on { background: var(--grad); color: #fff; border-color: transparent; }

.btn-primary {
  background: var(--grad); color: #fff;
  border: none; border-radius: 999px; padding: 12px 22px; font-size: 15px; font-weight: 700; cursor: pointer;
  box-shadow: 0 2px 14px rgba(244,63,107,.35);
}
.btn-ghost {
  background: transparent; border: 1px solid var(--line); color: var(--text);
  border-radius: 999px; padding: 12px 18px; font-size: 15px; cursor: pointer;
}
.btn-chip {
  background: rgba(255,159,58,.14); border: 1px solid rgba(249,115,65,.55); color: var(--accent);
  border-radius: 999px; padding: 7px 14px; font-size: 13px; font-weight: 600; cursor: pointer; white-space: nowrap;
}

/* ---------- Mode bar ---------- */
.mode-bar {
  position: absolute; left: 0; right: 0; bottom: calc(118px + var(--safe-bottom)); z-index: 5;
  display: flex; gap: 6px; justify-content: center; padding: 0 12px;
  overflow-x: auto; scrollbar-width: none;
}
.mode-bar::-webkit-scrollbar { display: none; }
.mode-btn {
  background: rgba(10,14,23,.6); border: 1px solid rgba(255,255,255,.14); color: var(--text-dim);
  border-radius: 999px; padding: 8px 16px; font-size: 13px; font-weight: 600; cursor: pointer;
  backdrop-filter: blur(8px); flex: 0 0 auto;
}
.mode-btn.active { background: var(--grad); border-color: transparent; color: #fff; }

/* ---------- Zoom ---------- */
.zoom-slider {
  position: absolute; left: 50%; transform: translateX(-50%);
  bottom: calc(160px + var(--safe-bottom)); z-index: 5;
  width: 60%; accent-color: var(--accent);
}

/* ---------- Controls ---------- */
.controls {
  position: absolute; left: 0; right: 0; bottom: 0; z-index: 5;
  display: flex; align-items: center; justify-content: space-around;
  padding: 16px 30px calc(20px + var(--safe-bottom));
  background: linear-gradient(to top, rgba(4,8,16,.8), transparent);
}
.shutter {
  width: 74px; height: 74px; border-radius: 50%; border: 4px solid #fff;
  background: transparent; cursor: pointer; padding: 4px; transition: transform .1s ease;
}
.shutter:active { transform: scale(.9); }
.shutter-inner { display: block; width: 100%; height: 100%; border-radius: 50%; background: #fff; }
.ctrl-side { width: 52px; height: 52px; font-size: 22px; position: relative; overflow: hidden; }
.gallery-thumb { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: cover; }

/* ---------- Timestamp overlay ---------- */
.stamp-overlay {
  position: absolute; left: 10px; bottom: calc(170px + var(--safe-bottom)); z-index: 4;
  max-width: 78%; pointer-events: none;
}
.stamp-line {
  color: var(--stamp); font-size: 12.5px; font-weight: 600; line-height: 1.45;
  text-shadow: 0 0 3px #000, 0 1px 3px #000; font-variant-numeric: tabular-nums;
}
.stamp-primary { font-size: 15px; font-weight: 800; }
.stamp-id { font-size: 10.5px; opacity: .85; }

/* ---------- AI banner ---------- */
.ai-banner {
  position: absolute; top: calc(64px + var(--safe-top)); left: 50%; transform: translateX(-50%); z-index: 6;
  display: flex; align-items: center; gap: 10px; max-width: 88%;
  background: rgba(16,14,30,.85); border: 1px solid rgba(139,92,246,.5);
  border-radius: var(--radius); padding: 10px 14px; backdrop-filter: blur(10px);
}
.ai-dot {
  width: 10px; height: 10px; border-radius: 50%; flex: 0 0 auto;
  background: var(--violet); box-shadow: 0 0 10px var(--violet);
  animation: pulse 1.4s ease-in-out infinite;
}
@keyframes pulse { 50% { opacity: .35; } }
.ai-banner-text { display: flex; flex-direction: column; min-width: 0; }
.ai-banner-text strong { font-size: 14px; }
.ai-banner-text span { font-size: 12px; color: var(--text-dim); overflow: hidden; text-overflow: ellipsis; }

/* ---------- Scan toast ---------- */
.scan-toast {
  position: absolute; bottom: calc(230px + var(--safe-bottom)); left: 50%; transform: translateX(-50%); z-index: 6;
  display: flex; align-items: center; gap: 12px; max-width: 92%;
  background: rgba(10,16,30,.9); border: 1px solid rgba(52,211,153,.5);
  border-radius: var(--radius); padding: 12px 14px; backdrop-filter: blur(10px);
}
#scan-toast-icon { font-size: 22px; color: var(--good); }
.scan-toast div { display: flex; flex-direction: column; min-width: 0; }
.scan-toast strong { font-size: 13px; }
.scan-toast span {
  font-size: 12px; color: var(--text-dim); max-width: 220px;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}

/* ---------- Pages (records/settings/detail) ---------- */
.page-head {
  display: flex; align-items: center; justify-content: space-between; gap: 10px;
  padding: calc(12px + var(--safe-top)) 16px 12px; border-bottom: 1px solid var(--line);
  background: var(--bg-raise);
}
.page-head h1 { font-size: 18px; }

.records-tools { display: flex; gap: 8px; padding: 12px 16px; }
.records-tools input, .records-tools select {
  background: var(--bg-card); border: 1px solid var(--line); color: var(--text);
  border-radius: 10px; padding: 10px 12px; font-size: 14px;
}
.records-tools input { flex: 1; min-width: 0; }

.records-grid {
  display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
  gap: 10px; padding: 4px 16px 30px; overflow-y: auto; flex: 1;
}
.record-card {
  background: var(--bg-card); border: 1px solid var(--line); border-radius: var(--radius);
  overflow: hidden; cursor: pointer; transition: transform .12s ease;
}
.record-card:active { transform: scale(.97); }
.record-card img { width: 100%; aspect-ratio: 4/3; object-fit: cover; display: block; }
.record-card .rc-body { padding: 8px 10px; }
.record-card .rc-title { font-size: 13px; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.record-card .rc-sub { font-size: 11px; color: var(--text-dim); margin-top: 2px; }
.rc-badge {
  display: inline-block; font-size: 10px; font-weight: 700; text-transform: uppercase;
  color: var(--accent); background: rgba(255,159,58,.13); border-radius: 6px; padding: 2px 6px; margin-top: 6px;
}
.empty-note { text-align: center; color: var(--text-dim); padding: 40px 30px; font-size: 14px; }

/* ---------- Detail ---------- */
.detail-body { flex: 1; overflow-y: auto; padding: 16px; }
.detail-body img { width: 100%; border-radius: var(--radius); border: 1px solid var(--line); }
.meta-table { width: 100%; border-collapse: collapse; margin-top: 14px; font-size: 13px; }
.meta-table td { padding: 7px 8px; border-bottom: 1px solid var(--line); vertical-align: top; }
.meta-table td:first-child { color: var(--text-dim); white-space: nowrap; width: 38%; }
.meta-table td:last-child { word-break: break-word; font-variant-numeric: tabular-nums; }
.verify-ok { color: var(--good); font-weight: 700; }
.verify-bad { color: var(--bad); font-weight: 700; }
.detail-actions { display: flex; gap: 10px; margin: 16px 0 30px; flex-wrap: wrap; }

.ai-card {
  background: linear-gradient(135deg, rgba(139,92,246,.12), rgba(244,63,107,.07));
  border: 1px solid rgba(139,92,246,.35); border-radius: var(--radius);
  padding: 14px; margin-top: 14px; font-size: 14px; line-height: 1.55; white-space: pre-wrap;
}
.ai-card h3 { font-size: 13px; text-transform: uppercase; letter-spacing: .8px; color: #c084fc; margin-bottom: 8px; }

/* ---------- Settings ---------- */
.settings-body { flex: 1; overflow-y: auto; padding: 16px; display: flex; flex-direction: column; gap: 18px; }
.setting-group { background: var(--bg-card); border: 1px solid var(--line); border-radius: var(--radius); padding: 14px 16px; }
.setting-group h2 { font-size: 14px; color: var(--accent); margin-bottom: 12px; }
.setting-group label { display: block; font-size: 13px; color: var(--text-dim); margin-bottom: 12px; }
.setting-group label.row { display: flex; align-items: center; gap: 10px; color: var(--text); }
.setting-group input[type="text"], .setting-group input[type="password"] {
  width: 100%; margin-top: 6px; background: var(--bg-raise); border: 1px solid var(--line);
  color: var(--text); border-radius: 10px; padding: 10px 12px; font-size: 14px;
}
.hint { font-size: 12.5px; color: var(--text-dim); line-height: 1.5; margin-bottom: 10px; }
.hint code { color: var(--accent); }

/* ---------- Review sheet ---------- */
.sheet {
  position: fixed; inset: 0; z-index: 20; background: rgba(4,8,16,.82);
  display: flex; align-items: flex-end; justify-content: center; backdrop-filter: blur(6px);
}
.sheet-card {
  width: 100%; max-width: 560px; max-height: 92vh; overflow-y: auto;
  background: var(--bg-raise); border: 1px solid var(--line);
  border-radius: 20px 20px 0 0; padding: 16px 16px calc(16px + var(--safe-bottom));
}
.sheet-card img { width: 100%; border-radius: var(--radius); }
.review-meta { font-size: 12.5px; color: var(--text-dim); margin: 12px 2px; line-height: 1.6; font-variant-numeric: tabular-nums; }
.review-ai {
  background: rgba(139,92,246,.1); border: 1px solid rgba(139,92,246,.35);
  border-radius: var(--radius); padding: 12px; font-size: 13.5px; line-height: 1.55;
  margin-bottom: 12px; white-space: pre-wrap; max-height: 32vh; overflow-y: auto;
}
.sheet-actions { display: flex; gap: 10px; }
.sheet-actions .btn-primary { flex: 1; }

/* ---------- Image editor ---------- */
.editor-card { max-width: 640px; }
.editor-stage {
  background: #000; border-radius: var(--radius); overflow: hidden;
  display: flex; align-items: center; justify-content: center; max-height: 46vh;
}
#editor-canvas { max-width: 100%; max-height: 46vh; touch-action: none; cursor: crosshair; display: block; }
.editor-tabs { display: flex; gap: 6px; margin: 12px 0 4px; }
.etab {
  flex: 1; background: var(--bg-card); border: 1px solid var(--line); color: var(--text-dim);
  border-radius: 10px; padding: 9px 0; font-size: 13px; font-weight: 600; cursor: pointer;
}
.etab.active { background: var(--grad); border-color: transparent; color: #fff; }
.epanel { padding: 10px 2px; }
.epanel label { display: block; font-size: 13px; color: var(--text-dim); margin-bottom: 10px; }
.epanel input[type="range"] { width: 100%; accent-color: var(--accent); margin-top: 4px; }
.filter-chips { display: flex; flex-wrap: wrap; gap: 8px; }
.fchip {
  background: var(--bg-card); border: 1px solid var(--line); color: var(--text);
  border-radius: 999px; padding: 8px 14px; font-size: 13px; cursor: pointer;
}
.fchip.active { background: var(--grad); border-color: transparent; color: #fff; font-weight: 700; }
.crop-actions { display: flex; gap: 8px; flex-wrap: wrap; }
.setting-group label.row.master { align-items: flex-start; padding-bottom: 8px; border-bottom: 1px solid var(--line); margin-bottom: 14px; }
.setting-group label.row small { color: var(--text-dim); }

/* ---------- Busy ---------- */
.busy {
  position: fixed; inset: 0; z-index: 30; background: rgba(4,8,16,.7);
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 14px;
}
.busy span { color: var(--text-dim); font-size: 14px; }
.spinner {
  width: 38px; height: 38px; border-radius: 50%;
  border: 3px solid var(--line); border-top-color: var(--accent);
  animation: spin .8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

@media (min-width: 700px) {
  .stamp-overlay { bottom: calc(150px + var(--safe-bottom)); }
  .records-grid { grid-template-columns: repeat(auto-fill, minmax(190px, 1fr)); }
}
