*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
/* タブレットのタッチ操作向け：長押しでの選択/コールアウトやタップ遅延を抑制 */
button, .class-name, .class-del, .exp-item, .btn-cam-switch {
  touch-action: manipulation;
  -webkit-user-select: none; user-select: none;
  -webkit-touch-callout: none;
}
:root {
  --bg:     #06060f;
  --card:   #0e0e22;
  --card2:  #14143a;
  --border: #222260;
  --accent: #00d4ff;
  --purple: #7c3aed;
  --text:   #e2e8f0;
  --dim:    #7a82a8;
  --r: 12px;
  --green:  #4ade80;
  --yellow: #fbbf24;
  --red:    #f87171;
  --blue:   #60a5fa;
}
body {
  background: var(--bg); color: var(--text);
  font-family: 'Segoe UI','Hiragino Sans','Meiryo',sans-serif;
  min-height: 100vh; display: flex; flex-direction: column;
  overflow-x: hidden;
}

/* ── HEADER ── */
header {
  background: #0a0a20; border-bottom: 1px solid var(--border);
  padding: 10px 20px; text-align: center;
}
header h1 {
  font-size: 22px; font-weight: 900;
  background: linear-gradient(90deg, var(--accent), #a78bfa, var(--green));
  background-size: 200%; -webkit-background-clip: text;
  -webkit-text-fill-color: transparent; animation: shimmer 4s linear infinite;
}
@keyframes shimmer { 0%{background-position:0%} 100%{background-position:200%} }
header p { font-size: 11px; color: var(--dim); margin-top: 2px; }

/* Phase steps */
.phase-bar {
  display: flex; align-items: center; justify-content: center;
  gap: 6px; margin-top: 8px;
}
.ph-step {
  display: flex; align-items: center; gap: 5px;
  padding: 4px 10px; border-radius: 20px;
  font-size: 12px; font-weight: 600;
  border: 1px solid var(--border); color: var(--dim);
  transition: all .4s;
}
.ph-step.active { border-color: var(--accent); color: var(--accent); background: rgba(0,212,255,.08); }
.ph-step.done   { border-color: var(--purple); color: var(--purple); background: rgba(124,58,237,.08); }
.ph-arrow { color: var(--border); font-size: 12px; }

/* ── LOADING SCREEN ── */
#loadScreen {
  flex: 1; display: flex; flex-direction: column;
  align-items: center; justify-content: center; gap: 20px; padding: 30px;
}
.load-icon { font-size: 60px; animation: spin 2s linear infinite; }
@keyframes spin { 0%{transform:rotate(0deg)} 100%{transform:rotate(360deg)} }
.load-title { font-size: 22px; font-weight: 700; }
.load-sub { font-size: 14px; color: var(--dim); text-align: center; line-height: 1.7; }
.load-bar-wrap { width: 300px; height: 8px; background: var(--border); border-radius: 4px; overflow: hidden; }
.load-bar { height: 100%; border-radius: 4px;
  background: linear-gradient(90deg, var(--purple), var(--accent));
  transition: width .5s ease; box-shadow: 0 0 8px var(--accent); }
.load-status { font-size: 13px; color: var(--dim); }
.load-note {
  max-width: 360px; background: var(--card); border: 1px solid var(--border);
  border-radius: var(--r); padding: 12px 16px;
  font-size: 12px; color: var(--dim); line-height: 1.7; text-align: center;
}

/* ── CAMERA ERROR ── */
#errScreen {
  flex: 1; display: none; flex-direction: column;
  align-items: center; justify-content: center; gap: 14px; padding: 30px; text-align: center;
}
.err-icon { font-size: 50px; }
.err-title { font-size: 20px; font-weight: 700; color: var(--red); }
.err-msg { font-size: 13px; color: var(--dim); max-width: 440px; line-height: 1.7; }
.err-fix {
  background: var(--card); border: 1px solid var(--border); border-radius: var(--r);
  padding: 14px 18px; font-size: 12px; color: var(--dim);
  max-width: 440px; text-align: left; line-height: 1.8;
}
.err-fix strong { color: var(--yellow); }

/* ── MAIN APP ── */
#appMain { display: none; flex: 1; flex-direction: column; }

/* 3-column layout */
.app-grid {
  display: grid;
  grid-template-columns: 300px 1fr 280px;
  gap: 14px; padding: 14px;
  flex: 1; max-width: 1100px; margin: 0 auto; width: 100%;
}

/* ── CAMERA PANEL ── */
.cam-panel {
  display: flex; flex-direction: column; gap: 10px;
}
.cam-wrap {
  position: relative; background: #000;
  border-radius: var(--r); overflow: hidden;
  border: 2px solid var(--border);
  aspect-ratio: 4/3;
}
#video {
  width: 100%; height: 100%;
  object-fit: cover; transform: scaleX(-1); /* mirror */
  display: block;
}
.cam-flash {
  position: absolute; inset: 0;
  background: white; opacity: 0;
  pointer-events: none; border-radius: var(--r);
  transition: opacity .05s;
}
.cam-flash.flash { opacity: 0.8; transition: opacity .05s; }
.cam-overlay-label {
  position: absolute; bottom: 8px; left: 8px;
  background: rgba(0,0,0,.7); border-radius: 6px;
  padding: 4px 10px; font-size: 12px; color: var(--dim);
}
.btn-cam-switch {
  position: absolute; top: 8px; right: 8px;
  background: rgba(0,0,0,.65); border: 1px solid rgba(255,255,255,.25);
  border-radius: 50%; width: 46px; height: 46px;
  color: #fff; font-size: 20px; cursor: pointer;
  display: flex; align-items: center; justify-content: center;
  transition: background .2s;
  -webkit-tap-highlight-color: transparent;
}
.btn-cam-switch:hover { background: rgba(0,212,255,.35); }
/* Predict overlay */
.cam-pred-overlay {
  position: absolute; bottom: 0; left: 0; right: 0;
  background: linear-gradient(transparent, rgba(0,0,0,.85));
  padding: 16px 12px 12px;
  display: none;
}
.cam-pred-overlay.show { display: block; }
.cam-pred-name {
  font-size: 24px; font-weight: 900; margin-bottom: 4px;
  transition: all .2s;
}
.cam-pred-conf { font-size: 12px; color: var(--dim); }

/* ── CLASSES PANEL ── */
.classes-panel { display: flex; flex-direction: column; gap: 10px; }

.class-card {
  background: var(--card); border: 2px solid var(--border);
  border-radius: var(--r); padding: 12px 14px;
  transition: border-color .2s;
}
.class-card:hover { border-color: var(--accent); }

.class-header {
  display: flex; align-items: center; gap: 8px; margin-bottom: 10px;
}
.class-dot {
  width: 12px; height: 12px; border-radius: 50%; flex-shrink: 0;
}
.class-name {
  flex: 1; font-weight: 700; font-size: 14px;
  padding: 8px 2px; min-height: 44px; display: flex; align-items: center;
}
.class-name-input {
  flex: 1; background: var(--card2); border: 1px solid var(--border);
  border-radius: 6px; padding: 8px; min-height: 44px; color: var(--text); font-size: 14px;
  font-weight: 700; outline: none;
}
.class-count {
  font-size: 12px; padding: 2px 8px;
  border-radius: 12px; font-weight: 700;
}
.class-del {
  background: transparent; border: none; color: var(--dim);
  cursor: pointer; font-size: 16px; padding: 10px 12px;
  min-width: 44px; min-height: 44px;
  transition: color .2s;
}
.class-del:hover { color: var(--red); }

/* Capture button */
.btn-capture {
  width: 100%; padding: 14px; min-height: 48px;
  border: none; border-radius: 8px;
  font-size: 15px; font-weight: 700; cursor: pointer;
  transition: transform .1s, opacity .2s;
  display: flex; align-items: center; justify-content: center; gap: 6px;
}
.btn-capture:hover { opacity: .85; }
.btn-capture:active { transform: scale(.97); }

/* Thumbnails */
.thumbs {
  display: flex; gap: 4px; margin-top: 8px; flex-wrap: wrap;
}
.thumbs img {
  width: 46px; height: 36px; object-fit: cover;
  border-radius: 4px; border: 1px solid var(--border);
  opacity: .9;
}

/* Sample count indicator */
.sample-meter {
  margin-top: 8px;
}
.sample-meter-track { height: 5px; background: var(--border); border-radius: 3px; overflow: hidden; margin-bottom: 3px; }
.sample-meter-fill  { height: 100%; border-radius: 3px; transition: width .3s ease; }
.sample-meter-label { font-size: 10px; color: var(--dim); }

/* Add class button */
.btn-add-class {
  background: var(--card2); border: 1px dashed var(--border);
  border-radius: var(--r); padding: 14px; min-height: 44px;
  color: var(--dim); font-size: 13px; cursor: pointer;
  text-align: center; transition: all .2s;
}
.btn-add-class:hover { border-color: var(--accent); color: var(--accent); background: rgba(0,212,255,.05); }

/* Train button */
.btn-train {
  width: 100%; padding: 16px; min-height: 52px;
  background: linear-gradient(135deg, var(--purple), var(--accent));
  border: none; border-radius: var(--r);
  color: white; font-size: 18px; font-weight: 900;
  cursor: pointer; opacity: .4; pointer-events: none;
  transition: opacity .3s; animation: none;
}
.btn-train.ready {
  opacity: 1; pointer-events: all;
  animation: trainPulse 1.5s ease-in-out infinite;
}
@keyframes trainPulse {
  0%,100%{transform:scale(1);box-shadow:0 0 0 0 rgba(124,58,237,.4)}
  50%{transform:scale(1.02);box-shadow:0 0 0 8px rgba(124,58,237,0)}
}

/* ── RIGHT PANEL ── */
.right-panel { display: flex; flex-direction: column; gap: 10px; }

/* card */
.card {
  background: var(--card); border: 1px solid var(--border);
  border-radius: var(--r); padding: 14px;
}
.card h3 {
  font-size: 11px; text-transform: uppercase; letter-spacing: 1px;
  color: var(--dim); margin-bottom: 10px;
}

/* Confidence bars */
.conf-row { margin-bottom: 10px; }
.conf-header { display: flex; justify-content: space-between; align-items: center; margin-bottom: 4px; }
.conf-name { font-size: 13px; font-weight: 700; display: flex; align-items: center; gap: 6px; }
.conf-pct  { font-size: 14px; font-weight: 900; }
.conf-track { height: 14px; background: var(--border); border-radius: 7px; overflow: hidden; }
.conf-fill  { height: 100%; border-radius: 7px; transition: width .25s ease; }

/* AI explanation bubble */
.explain-bubble {
  background: var(--card2); border: 1px solid var(--border);
  border-radius: 10px; padding: 10px 12px;
  font-size: 12px; line-height: 1.7; color: var(--dim);
  min-height: 50px;
}
.explain-bubble strong { color: var(--accent); }

/* Data balance warning */
.balance-warn {
  background: rgba(251,191,36,.1); border: 1px solid rgba(251,191,36,.4);
  border-radius: 8px; padding: 8px 12px;
  font-size: 11px; color: var(--yellow); line-height: 1.6;
  display: none;
}
.balance-warn.show { display: block; }

/* Experiments card */
.exp-item {
  display: flex; gap: 10px; align-items: flex-start;
  padding: 12px 8px; border-radius: 8px; margin-bottom: 6px;
  background: var(--card2); font-size: 12px;
  border: 1px solid var(--border);
  cursor: pointer; transition: border-color .2s;
}
.exp-item:hover { border-color: var(--accent); }
.exp-item.done { border-color: var(--green); opacity: .7; }
.exp-num { width: 22px; height: 22px; border-radius: 50%; background: var(--purple);
  display: flex; align-items: center; justify-content: center; font-size: 10px;
  font-weight: 700; flex-shrink: 0; margin-top: 1px; }
.exp-text { flex: 1; line-height: 1.5; }
.exp-text strong { color: var(--text); }

/* Stats */
.stat-row { display: flex; justify-content: space-between; margin-bottom: 6px; font-size: 13px; }
.stat-val  { font-weight: 700; color: var(--accent); }

/* ── TRAINING OVERLAY ── */
.train-overlay {
  display: none; position: fixed; inset: 0;
  background: rgba(0,0,0,.95); z-index: 200;
  flex-direction: column; align-items: center;
  justify-content: center; text-align: center; padding: 20px;
}
.train-overlay.show { display: flex; }
.train-anim { font-size: 64px; margin-bottom: 16px; animation: trainSpin 1s linear infinite; }
@keyframes trainSpin { 0%{transform:rotate(0deg)scale(1)} 50%{transform:rotate(180deg)scale(1.1)} 100%{transform:rotate(360deg)scale(1)} }
.train-title { font-size: 28px; font-weight: 900; margin-bottom: 8px;
  background: linear-gradient(90deg,var(--accent),#a78bfa);
  -webkit-background-clip:text; -webkit-text-fill-color:transparent; }
.train-step { font-size: 16px; color: var(--text); margin-bottom: 16px; min-height: 24px; }
.train-bar-wrap { width: 360px; height: 10px; background: var(--border); border-radius: 5px; overflow:hidden; margin-bottom: 8px; }
.train-bar-fill { height: 100%; border-radius: 5px;
  background: linear-gradient(90deg, var(--purple), var(--accent));
  transition: width .4s ease; box-shadow: 0 0 8px var(--accent); }
.train-explain {
  max-width: 440px; background: var(--card); border: 1px solid var(--border);
  border-radius: var(--r); padding: 14px 18px; margin-top: 16px;
  font-size: 13px; line-height: 1.75; color: var(--text); text-align: left;
}
.train-explain h4 { color: var(--accent); margin-bottom: 6px; font-size: 14px; }

/* ── PREDICT PHASE UI ── */
.retrain-bar {
  display: flex; gap: 8px; align-items: center;
  background: var(--card); border: 1px solid var(--border);
  border-radius: var(--r); padding: 10px 14px; margin-bottom: 10px;
}
.retrain-bar span { flex: 1; font-size: 12px; color: var(--dim); }
.btn-retrain {
  background: var(--purple); color: white; border: none;
  border-radius: 8px; padding: 11px 16px; min-height: 44px;
  font-size: 12px; font-weight: 700; cursor: pointer;
  transition: opacity .2s;
}
.btn-retrain:hover { opacity: .85; }

/* particle canvas */
#ptc { position: fixed; inset: 0; pointer-events: none; z-index: 190; }

/* ── VISUALIZATION MODAL ── */
.viz-modal {
  display: none; position: fixed; inset: 0; z-index: 300;
  background: rgba(0,0,0,.92); backdrop-filter: blur(6px);
  align-items: center; justify-content: center; padding: 16px;
}
.viz-modal.show { display: flex; }
.viz-inner {
  background: var(--card); border: 1px solid var(--border);
  border-radius: 16px; width: 100%; max-width: 860px;
  max-height: 90vh; overflow-y: auto;
  animation: vizIn .3s ease;
}
@keyframes vizIn { from{opacity:0;transform:scale(.94)} to{opacity:1;transform:scale(1)} }
.viz-header {
  display: flex; align-items: center; justify-content: space-between;
  padding: 14px 18px; border-bottom: 1px solid var(--border);
}
.viz-header h2 { font-size: 17px; font-weight: 700; }
.viz-close {
  background: var(--card2); border: 1px solid var(--border);
  border-radius: 8px; padding: 10px 16px; min-height: 44px; color: var(--dim);
  font-size: 13px; cursor: pointer; transition: all .2s;
}
.viz-close:hover { border-color: var(--red); color: var(--red); }
.viz-body {
  display: flex; gap: 16px; padding: 16px; flex-wrap: wrap;
}
.viz-canvas-wrap {
  position: relative; flex-shrink: 0;
}
#scatterCanvas {
  border: 1px solid var(--border); border-radius: 10px;
  display: block; background: #08081a;
}
.viz-canvas-label-x {
  text-align: center; font-size: 11px; color: var(--dim); margin-top: 4px;
}
.viz-canvas-label-y {
  position: absolute; left: -22px; top: 50%;
  transform: translateY(-50%) rotate(-90deg);
  font-size: 11px; color: var(--dim); white-space: nowrap;
}
.viz-legend {
  display: flex; flex-wrap: wrap; gap: 8px; margin-top: 10px;
}
.viz-legend-item {
  display: flex; align-items: center; gap: 5px;
  font-size: 12px; color: var(--text);
}
.viz-legend-dot {
  width: 12px; height: 12px; border-radius: 50%;
}
.viz-info {
  flex: 1; min-width: 220px; display: flex; flex-direction: column; gap: 12px;
}
.viz-quality-card {
  background: var(--card2); border: 1px solid var(--border);
  border-radius: 10px; padding: 14px; text-align: center;
}
.viz-quality-label { font-size: 11px; color: var(--dim); margin-bottom: 6px; }
.viz-quality-score { font-size: 32px; font-weight: 900; margin-bottom: 4px; }
.viz-quality-desc  { font-size: 12px; color: var(--dim); line-height: 1.5; }
.viz-explain {
  background: var(--card2); border: 1px solid var(--border);
  border-radius: 10px; padding: 14px; font-size: 12px; line-height: 1.7;
  color: var(--dim);
}
.viz-explain h4 { color: var(--accent); margin-bottom: 8px; font-size: 13px; }
.viz-explain p  { margin-bottom: 6px; }
.viz-explain hr { border: none; border-top: 1px solid var(--border); margin: 8px 0; }
.viz-explain strong { color: var(--text); }
.viz-distances { display: flex; flex-direction: column; gap: 6px; }
.viz-dist-row {
  background: var(--card2); border-radius: 8px; padding: 8px 12px;
  font-size: 11px; display: flex; justify-content: space-between; align-items: center;
}
.viz-dist-val { font-weight: 700; color: var(--accent); }

/* Show viz button */
.btn-show-viz {
  width: 100%; padding: 13px; min-height: 44px;
  background: var(--card2); border: 1px solid var(--accent);
  border-radius: 10px; color: var(--accent);
  font-size: 14px; font-weight: 700; cursor: pointer;
  transition: all .2s; display: none;
}
.btn-show-viz.show { display: block; }
.btn-show-viz:hover { background: rgba(0,212,255,.1); }

/* scrollbar */
::-webkit-scrollbar { width: 4px; }
::-webkit-scrollbar-thumb { background: var(--border); border-radius: 2px; }

@media (max-width: 800px) {
  .app-grid { grid-template-columns: 1fr; }
}

/* Staff-only full reset button: small & low-contrast so kids don't tap it by accident */
#btnFullReset {
  position: fixed; top: 6px; right: 6px; z-index: 250;
  background: rgba(255,255,255,.05); border: 1px solid var(--border);
  color: var(--dim); font-size: 10px; padding: 5px 9px;
  border-radius: 6px; cursor: pointer; opacity: .5;
  transition: opacity .2s;
}
#btnFullReset:hover { opacity: 1; border-color: var(--red); color: var(--red); }
