:root {
  --bg: #f6f7f9;
  --surface: #ffffff;
  --surface-2: #eef0f4;
  --text: #1b1f27;
  --muted: #667085;
  --border: #dde1e8;
  --accent: #3b5bdb;
  --accent-text: #ffffff;
  --ok: #2b8a3e;
  --ng: #c92a2a;
  --c1: #3b5bdb;
  --c2: #e8590c;
  --c3: #2b8a3e;
  --c4: #ae3ec9;
  --c5: #0c8599;
  --c6: #d6336c;
  --radius: 12px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #111318;
    --surface: #1a1d24;
    --surface-2: #242833;
    --text: #e7e9ee;
    --muted: #98a2b3;
    --border: #2e3340;
    --accent: #748ffc;
    --accent-text: #0d1020;
    --ok: #51cf66;
    --ng: #ff6b6b;
    --c1: #748ffc;
    --c2: #ff922b;
    --c3: #51cf66;
    --c4: #da77f2;
    --c5: #3bc9db;
    --c6: #f06595;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: system-ui, -apple-system, "Hiragino Sans", "Noto Sans JP", sans-serif;
  font-size: 15px;
  line-height: 1.6;
}
h1, h2, h3, h4 { margin: 0; line-height: 1.3; }
h2 { font-size: 17px; margin-bottom: 10px; }
h4 { font-size: 14px; margin: 18px 0 6px; }
small { font-size: 12px; }
.muted { color: var(--muted); }
.small { font-size: 12px; }
b { font-weight: 650; }

/* ---------- ヘッダー ---------- */
.topbar {
  display: flex; flex-wrap: wrap; gap: 12px 20px; align-items: center; justify-content: space-between;
  padding: 14px 20px; background: var(--surface); border-bottom: 1px solid var(--border);
  position: sticky; top: 0; z-index: 10;
}
.brand { display: flex; gap: 12px; align-items: center; }
.logo { font-size: 28px; color: var(--accent); }
.brand h1 { font-size: 19px; }
.sub { margin: 0; font-size: 12px; color: var(--muted); }
.controls { display: flex; flex-wrap: wrap; gap: 10px; align-items: center; }
.status { display: inline-flex; gap: 10px; font-size: 12px; color: var(--muted); font-variant-numeric: tabular-nums; }
body.model-loading .status::before {
  content: ""; width: 12px; height: 12px; border-radius: 50%;
  border: 2px solid var(--border); border-top-color: var(--accent); animation: spin 0.8s linear infinite; align-self: center;
}
@keyframes spin { to { transform: rotate(360deg); } }

.tabs { display: flex; gap: 4px; padding: 0 20px; background: var(--surface); border-bottom: 1px solid var(--border); overflow-x: auto; }
.tab {
  appearance: none; border: 0; background: none; color: var(--muted); font: inherit; font-weight: 600;
  padding: 12px 14px; border-bottom: 3px solid transparent; cursor: pointer; white-space: nowrap;
}
.tab[aria-selected="true"] { color: var(--text); border-bottom-color: var(--accent); }

main { max-width: 1180px; margin: 0 auto; padding: 20px; }
.panel { display: flex; flex-direction: column; gap: 20px; }
.grid2 { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); gap: 20px; align-items: start; }
@media (max-width: 860px) {
  .grid2 { grid-template-columns: minmax(0, 1fr); }
  main { padding: 16px; }
  .topbar { padding: 12px 16px; }
  .tabs { padding: 0 8px; }
}
/* スマホ: ヘッダーを固定せず、映像をできるだけ大きく */
@media (max-width: 600px) {
  .topbar { position: static; }
  .sub, .status { display: none; }
  .controls { width: 100%; }
  .controls .field { flex: 1; }
  .controls select { width: 100%; }
  .card { padding: 14px; }
  .metrics { gap: 6px; }
  .metrics span { font-size: 18px; }
  .slider { grid-template-columns: 76px 1fr 56px; }
}
.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 18px; min-width: 0; }
.card.disabled { opacity: 0.5; pointer-events: none; }
.lead { margin: 0; }
.row { display: flex; flex-wrap: wrap; gap: 10px; align-items: center; margin-top: 12px; }
.row.spread { justify-content: space-between; margin-top: 0; margin-bottom: 10px; }
.row.spread h2 { margin: 0; }
.note { font-size: 13px; color: var(--muted); background: var(--surface-2); padding: 10px 12px; border-radius: 8px; margin: 14px 0 0; }

/* ---------- 入力部品 ---------- */
.btn {
  display: inline-flex; align-items: center; gap: 6px; border: 1px solid var(--border); background: var(--surface);
  color: var(--text); font: inherit; font-size: 14px; font-weight: 600; padding: 7px 14px; border-radius: 8px; cursor: pointer;
  user-select: none; -webkit-user-select: none;
}
.btn:hover { background: var(--surface-2); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
.btn.primary:hover { filter: brightness(1.08); }
.btn.ghost { border-color: transparent; background: none; color: var(--muted); }
.btn.small { font-size: 12px; padding: 4px 10px; }
.btn:disabled { opacity: 0.5; cursor: default; }
select, input[type="text"], .class-name {
  font: inherit; font-size: 14px; color: var(--text); background: var(--surface); border: 1px solid var(--border);
  border-radius: 8px; padding: 6px 8px; min-width: 0;
}
.field { display: inline-flex; flex-direction: column; gap: 2px; font-size: 12px; color: var(--muted); }
.field.grow { flex: 1; }
.check { display: inline-flex; gap: 6px; align-items: center; font-size: 14px; cursor: pointer; }
.chips { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 2px; }
.chip { display: inline-flex; gap: 4px; align-items: center; padding: 3px 10px; border: 1px solid var(--border); border-radius: 999px; font-size: 13px; cursor: pointer; }
.chip:has(input:checked) { border-color: var(--accent); background: color-mix(in srgb, var(--accent) 12%, transparent); }

/* ---------- 映像ステージ ---------- */
.stage {
  position: relative; aspect-ratio: 1; width: 100%; background: #0b0d12; border-radius: var(--radius); overflow: hidden;
  outline: 2px dashed transparent; outline-offset: -8px; transition: outline-color 0.15s;
}
.stage.dragging { outline-color: var(--accent); }
.stage canvas, .stage video, .still canvas { width: 100%; height: 100%; display: block; object-fit: cover; }
.video-slot, .still { position: absolute; inset: 0; }
.still:empty { display: none; }
.camera-video.mirror { transform: scaleX(-1); }
.placeholder {
  position: absolute; inset: 0; display: grid; place-items: center; text-align: center; color: #aab2c0;
  background: repeating-linear-gradient(45deg, #13161d 0 14px, #171a22 14px 28px); padding: 20px;
}
body:not(.camera-on) .stage:not(.show-still) .overlay { display: none; }
body.camera-on .stage .placeholder, .stage.show-still .placeholder, #lab-stage.loaded .placeholder { display: none; }
.overlay {
  position: absolute; left: 0; right: 0; bottom: 0; padding: 32px 16px 14px; display: flex; align-items: baseline; gap: 10px;
  background: linear-gradient(transparent, rgba(0, 0, 0, 0.75)); color: #fff; pointer-events: none;
}
.overlay-label { font-size: clamp(20px, 4vw, 30px); font-weight: 750; text-shadow: 0 1px 3px rgba(0,0,0,0.6); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.overlay-prob { font-size: 14px; opacity: 0.85; font-variant-numeric: tabular-nums; white-space: nowrap; }
.still .back { position: absolute; top: 10px; right: 10px; }

/* ---------- 棒グラフ ---------- */
.bars { display: flex; flex-direction: column; gap: 6px; }
.bar { position: relative; display: flex; justify-content: space-between; gap: 10px; padding: 6px 10px; border-radius: 6px; background: var(--surface-2); overflow: hidden; font-size: 14px; }
.bar-fill { position: absolute; inset: 0 auto 0 0; background: color-mix(in srgb, var(--c1) 28%, transparent); transition: width 0.15s; }
.bar.hl { box-shadow: inset 0 0 0 2px var(--c2); }
.bar-label, .bar-val { position: relative; }
.bar-label { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.bar-val { font-variant-numeric: tabular-nums; font-weight: 600; }
#teach-pred .bar-fill { opacity: 0.45; }

.metrics { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; margin-top: 16px; }
.metrics div { background: var(--surface-2); border-radius: 8px; padding: 8px; text-align: center; display: flex; flex-direction: column; }
.metrics span { font-size: 22px; font-weight: 700; font-variant-numeric: tabular-nums; }
.metrics small { color: var(--muted); font-size: 11px; }

/* ---------- ベンチマーク ---------- */
.bench-controls { display: flex; flex-wrap: wrap; gap: 14px 20px; align-items: end; margin: 12px 0 16px; }
.bench-controls small { color: var(--muted); }
.table-wrap { overflow-x: auto; }
table.data { width: 100%; border-collapse: collapse; font-size: 13px; margin-top: 12px; }
table.data th, table.data td { padding: 6px 10px; border-bottom: 1px solid var(--border); text-align: left; white-space: nowrap; }
table.data th { color: var(--muted); font-weight: 600; }
td.num { text-align: right; font-variant-numeric: tabular-nums; }
td.strong { font-weight: 700; }
td.err { color: var(--ng); white-space: normal; }
.dot { display: inline-block; width: 10px; height: 10px; border-radius: 50%; margin-right: 6px; vertical-align: -1px; }
.dot.hollow { border: 2px solid var(--c1); background: var(--surface); }
.legend { display: flex; flex-wrap: wrap; gap: 6px 16px; font-size: 12px; margin-top: 6px; }
.mini-bar { display: inline-block; width: 60px; height: 8px; margin-right: 8px; border-radius: 4px; vertical-align: 0;
  background: linear-gradient(90deg, var(--c3) var(--w), var(--surface-2) var(--w)); }

.chart { width: 100%; height: auto; display: block; }
.chart .grid { stroke: var(--border); stroke-width: 1; }
.chart .tick { fill: var(--muted); font-size: 11px; }
.chart .axis { fill: var(--muted); font-size: 12px; }
.chart .pt { fill: var(--text); font-size: 11px; font-weight: 600; }
.chart .series { fill: none; stroke-width: 2; opacity: 0.6; }
.chart .cursor { stroke: var(--c2); stroke-dasharray: 4 3; }
.chart .pt-keep { fill: var(--c1); }
.chart .pt-lost { fill: var(--surface); stroke: var(--c1); stroke-width: 2; }
.chart.pca { max-width: 420px; background: var(--surface-2); border-radius: 8px; }
.pca-pt { stroke: var(--surface); stroke-width: 1; }

/* ---------- 分析ラボ ---------- */
.target-row { display: flex; gap: 12px; align-items: end; }
.thumb canvas { width: 56px; height: 56px; border-radius: 6px; display: block; }
.target-status { display: flex; flex-wrap: wrap; align-items: baseline; gap: 6px; margin: 12px 0 8px; min-height: 36px; }
.big { font-size: 28px; font-weight: 750; font-variant-numeric: tabular-nums; }
.badge { font-size: 12px; font-weight: 700; padding: 2px 8px; border-radius: 999px; margin-left: auto; }
.badge.ok { color: var(--ok); background: color-mix(in srgb, var(--ok) 14%, transparent); }
.badge.ng { color: var(--ng); background: color-mix(in srgb, var(--ng) 14%, transparent); }
.sliders { display: flex; flex-direction: column; gap: 4px; margin-bottom: 4px; }
.slider { display: grid; grid-template-columns: 90px 1fr 64px; gap: 10px; align-items: center; font-size: 13px; }
.slider input { width: 100%; accent-color: var(--accent); }
.slider output { text-align: right; font-variant-numeric: tabular-nums; color: var(--muted); }
#lab-tools .bars { margin-top: 14px; }
.swatch { display: inline-block; width: 12px; height: 12px; border-radius: 3px; vertical-align: -1px; margin: 0 4px 0 2px; }
.swatch.red { background: rgb(255, 40, 40); }
.swatch.blue { background: rgb(40, 110, 255); }

/* ---------- 追加学習 ---------- */
.classes { display: flex; flex-direction: column; gap: 12px; }
.class-card { background: var(--surface); border: 1px solid var(--border); border-left: 5px solid var(--cls); border-radius: var(--radius); padding: 12px 14px; }
.class-card.dragging { outline: 2px dashed var(--cls); }
.class-head { display: flex; gap: 8px; align-items: center; }
.class-name { flex: 1; font-weight: 700; }
.count { font-size: 13px; color: var(--muted); font-variant-numeric: tabular-nums; white-space: nowrap; }
.thumbs { display: flex; flex-wrap: wrap; gap: 3px; margin: 10px 0; min-height: 30px; }
.thumbs canvas { width: 30px; height: 30px; border-radius: 4px; }
.thumbs:empty::before { content: "サンプルなし（撮影するか、画像をここにドロップ）"; font-size: 12px; color: var(--muted); }
.class-actions { display: flex; gap: 8px; flex-wrap: wrap; }
.btn.capture { background: var(--cls); border-color: var(--cls); color: #fff; touch-action: none; }
.btn.capture:active { transform: scale(0.97); filter: brightness(1.15); }
#teach-preview { margin-bottom: 12px; }
.compare { font-size: 13px; color: var(--muted); margin: 10px 0 0; }
.compare b { color: var(--text); }
table.confusion { border-collapse: collapse; font-size: 13px; margin-top: 12px; }
table.confusion th, table.confusion td { border: 1px solid var(--border); padding: 6px 12px; text-align: center; font-variant-numeric: tabular-nums; }
table.confusion th { font-weight: 600; color: var(--muted); }
table.confusion td.hit { background: color-mix(in srgb, var(--ok) calc(var(--a) * 40%), transparent); font-weight: 700; }
table.confusion td.miss { background: color-mix(in srgb, var(--ng) calc(var(--a) * 60% + 10%), transparent); font-weight: 700; }

.toast {
  position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%); background: var(--text); color: var(--bg);
  padding: 10px 16px; border-radius: 10px; font-size: 14px; z-index: 100; max-width: calc(100% - 32px);
  box-shadow: 0 6px 20px rgba(0,0,0,0.25);
}

.site-footer { text-align: center; font-size: 13px; color: var(--muted); padding: 8px 16px 28px; }
.site-footer a { color: var(--muted); }
.site-footer .sep { margin: 0 0.5em; }
