:root {
  --header: #175e54;       /* YY_system風の濃緑 */
  --header-text: #ffffff;
  --bg: #e9eef1;
  --card: #ffffff;
  --text: #1c2430;
  --muted: #7a8694;
  --line: #d7dee4;
  --green: #1faa59;
  --blue: #3742fa;
  --orange: #e67e22;
  --pink: #e91e8c;
  --red: #e74c3c;
  --font-scale: 1;
}

body[data-theme="dark"] {
  --bg: #1a1f36;
  --card: #232a47;
  --text: #ffffff;
  --muted: #8fa0c4;
  --line: #333c5e;
}

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

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Helvetica Neue", "Hiragino Sans", sans-serif;
  height: 100%;
  overflow: hidden;
}

#app {
  max-width: 560px;
  margin: 0 auto;
  height: 100vh;
  height: 100dvh;
  display: flex;
  flex-direction: column;
}

button { font-family: inherit; cursor: pointer; }

/* ── 上部バー ─────────────────────────────── */
.appbar {
  background: var(--header);
  color: var(--header-text);
  padding: calc(env(safe-area-inset-top) + 10px) 14px 10px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
}
.appbar-title { font-size: 17px; font-weight: 700; }
.phase {
  font-size: 10px;
  background: rgba(255, 255, 255, 0.22);
  padding: 2px 8px;
  border-radius: 10px;
  vertical-align: middle;
  margin-left: 4px;
}
.status { margin-top: 3px; font-size: 11px; color: rgba(255, 255, 255, 0.75); }
.status.ok { color: #7bebae; }
.status.err { color: #ff9c8a; }

.net { text-align: right; line-height: 1.25; white-space: nowrap; }
.net-label { display: block; font-size: 10px; color: rgba(255, 255, 255, 0.7); }
.net-val { font-size: 14px; font-weight: 700; }
.net-val.good { color: #7bebae; }
.net-val.fair { color: #ffd166; }
.net-val.bad { color: #ff9c8a; }

/* ── ツールバー ───────────────────────────── */
.toolbar {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 12px 0;
}
.spacer { flex: 1; }

.chip {
  background: var(--card);
  color: var(--text);
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 6px 14px;
  font-size: 14px;
  display: flex;
  align-items: center;
  gap: 8px;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.06);
}
.chip-label { font-size: 11px; color: var(--muted); }
.chip b { font-size: 15px; }
.chip.danger {
  background: var(--pink);
  border-color: var(--pink);
  color: #fff;
  font-weight: 700;
}

/* ── カメラプレビュー ─────────────────────── */
.stage {
  position: relative;
  margin: 10px 12px 0;
  height: 32vh;
  min-height: 140px;
  background: #000;
  border-radius: 16px;
  overflow: hidden;
  border: 1px solid var(--line);
  flex: none;
  transition: height 0.25s ease, min-height 0.25s ease;
}
.stage.mini { height: 84px; min-height: 84px; }
#video {
  width: 100%;
  height: 100%;
  object-fit: cover;
  transform: scaleX(-1); /* mirror */
}
#video.nomirror { transform: none; }
.overlay { position: absolute; inset: 0; pointer-events: none; border-radius: 16px; }
.overlay.recording { box-shadow: inset 0 0 0 4px var(--orange); }

/* ── 認識ログ ─────────────────────────────── */
.log {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding: 10px 12px 12px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.log-empty {
  margin: auto;
  text-align: center;
  color: var(--muted);
  font-size: 14px;
  line-height: 1.9;
}

.entry {
  background: var(--card);
  border: 1px solid var(--line);
  border-left: 5px solid var(--green);
  border-radius: 14px;
  padding: 10px 14px;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.05);
  animation: pop 0.18s ease;
}
.entry.low { border-left-color: var(--orange); }
.entry.fail { border-left-color: var(--muted); }
.entry-word {
  font-size: calc(26px * var(--font-scale));
  font-weight: 800;
  word-break: break-word;
  line-height: 1.25;
}
.entry.fail .entry-word { font-size: calc(16px * var(--font-scale)); color: var(--muted); font-weight: 600; }
.entry-meta {
  margin-top: 4px;
  font-size: 12px;
  color: var(--muted);
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
}
.tag {
  font-size: 10px;
  font-weight: 700;
  color: #fff;
  background: var(--header);
  border-radius: 8px;
  padding: 1px 7px;
}
.tag.jsl { background: var(--blue); }
.dup {
  font-weight: 700;
  color: var(--orange);
}
@keyframes pop {
  from { transform: scale(0.97); opacity: 0.4; }
  to { transform: scale(1); opacity: 1; }
}
.entry.flash { animation: pop 0.18s ease; }

.autoscroll {
  position: absolute;
  right: 16px;
  bottom: 120px;
  border: 1px solid var(--orange);
  color: var(--orange);
  background: var(--card);
  border-radius: 10px;
  font-size: 11px;
  padding: 5px 10px;
  opacity: 0.92;
}
.autoscroll:not(.on) { color: var(--muted); border-color: var(--line); }

/* ── 下部コントロールバー ─────────────────── */
.controls {
  flex: none;
  background: var(--card);
  border-top: 1px solid var(--line);
  padding: 6px 20px calc(env(safe-area-inset-bottom) + 8px);
  display: flex;
  align-items: center;
  justify-content: space-between;
}
.ctrl {
  background: none;
  border: none;
  color: var(--muted);
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  font-size: 12px;
  min-width: 72px;
  padding: 6px 4px;
}
.ctrl-icon { font-size: 22px; line-height: 1; }
.ctrl-label { font-weight: 600; }

.startbtn {
  position: relative;
  width: 92px;
  height: 92px;
  margin-top: -34px;
  border-radius: 50%;
  border: none;
  background: var(--green);
  color: #fff;
  font-size: 16px;
  font-weight: 800;
  box-shadow: 0 4px 14px rgba(0, 0, 0, 0.25);
  transition: background 0.2s, transform 0.05s;
}
.startbtn:active { transform: scale(0.96); }
.startbtn:disabled { background: #9aa7b3; }
.startbtn.recording { background: var(--orange); }
.startbtn.busy { background: #8a94a8; }
.startbtn.stop { background: var(--red); }
.startlabel { position: relative; z-index: 1; white-space: pre-line; line-height: 1.25; }
.ring {
  position: absolute;
  inset: -6px;
  border-radius: 50%;
  pointer-events: none;
}

/* ── カウントダウン ───────────────────────── */
.countdown {
  position: fixed;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 110px;
  font-weight: 800;
  color: #fff;
  text-shadow: 0 2px 24px rgba(0, 0, 0, 0.7);
  background: rgba(0, 0, 0, 0.25);
  z-index: 40;
  pointer-events: none;
}

/* ── 設定シート ───────────────────────────── */
.backdrop {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.4);
  z-index: 50;
}
.sheet {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  max-width: 560px;
  margin: 0 auto;
  max-height: 86vh;
  background: var(--bg);
  border-radius: 18px 18px 0 0;
  z-index: 51;
  display: flex;
  flex-direction: column;
  animation: slideup 0.22s ease;
}
@keyframes slideup {
  from { transform: translateY(40px); opacity: 0.5; }
  to { transform: translateY(0); opacity: 1; }
}
.sheet-head {
  background: var(--header);
  color: #fff;
  border-radius: 18px 18px 0 0;
  padding: 14px 16px;
  font-size: 17px;
  font-weight: 700;
  text-align: center;
  position: relative;
  flex: none;
}
.sheet-close {
  position: absolute;
  right: 10px;
  top: 50%;
  transform: translateY(-50%);
  background: rgba(255, 255, 255, 0.18);
  color: #fff;
  border: none;
  border-radius: 50%;
  width: 32px;
  height: 32px;
  font-size: 15px;
}
.sheet-body {
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding: 12px 14px calc(env(safe-area-inset-bottom) + 16px);
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.card {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 14px;
  padding: 6px 14px 12px;
}
.card h3 {
  font-size: 15px;
  margin: 8px 0 4px;
  text-align: center;
}
.row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 9px 0;
  border-top: 1px solid var(--line);
  font-size: 14px;
}
.card .row:first-of-type { border-top: none; }
.row .muted { color: var(--muted); font-size: 12px; }
.row input[type="range"] { flex: 1; max-width: 55%; }
.row select {
  max-width: 55%;
  font-size: 13px;
  padding: 6px;
  border-radius: 8px;
  border: 1px solid var(--line);
  background: var(--card);
  color: var(--text);
}

/* セグメントコントロール（YY_system風） */
.seg {
  display: flex;
  background: var(--bg);
  border-radius: 999px;
  padding: 3px;
  gap: 2px;
}
.seg button {
  border: none;
  background: none;
  color: var(--muted);
  font-size: 13px;
  padding: 6px 14px;
  border-radius: 999px;
  font-weight: 600;
}
.seg button.active {
  background: var(--card);
  color: var(--text);
  box-shadow: 0 1px 4px rgba(0, 0, 0, 0.15);
}

/* トグルスイッチ（iOS風） */
.switch { position: relative; display: inline-block; width: 52px; height: 30px; flex: none; }
.switch input { display: none; }
.switch span {
  position: absolute;
  inset: 0;
  background: #c9d2d9;
  border-radius: 999px;
  transition: background 0.2s;
}
.switch span::after {
  content: "";
  position: absolute;
  top: 3px;
  left: 3px;
  width: 24px;
  height: 24px;
  background: #fff;
  border-radius: 50%;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.3);
  transition: left 0.2s;
}
.switch input:checked + span { background: var(--green); }
.switch input:checked + span::after { left: 25px; }

.mini {
  background: #dbeafe;
  color: #1d4ed8;
  border: none;
  border-radius: 999px;
  font-size: 13px;
  font-weight: 700;
  padding: 8px 16px;
  flex: none;
}

.hidden { display: none !important; }

/* ── 学習モード ───────────────────────────── */
.ctrl.square {
  background: var(--card);
  border: 1.5px solid var(--line);
  border-radius: 12px;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.06);
}
.ctrl.square.active {
  border-color: var(--green);
  color: var(--green);
}
.ctrl.square:disabled { opacity: 0.5; }

.learnbar {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 12px 0;
}
.learn-label {
  flex: none;
  font-size: 11px;
  font-weight: 700;
  color: var(--muted);
}
.learnbar input {
  flex: 1;
  min-width: 0;
  font-size: 15px;
  padding: 8px 12px;
  border-radius: 12px;
  border: 1px solid var(--line);
  background: var(--card);
  color: var(--text);
}

.regform {
  margin: 8px 12px 0;
  padding: 10px 12px;
  background: var(--card);
  border: 1px solid var(--blue);
  border-radius: 12px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.regform-row {
  display: flex;
  align-items: center;
  gap: 8px;
}
.regform input {
  flex: 1;
  min-width: 0;
  font-size: 14px;
  padding: 8px 10px;
  border-radius: 10px;
  border: 1px solid var(--line);
  background: var(--bg);
  color: var(--text);
}
.regform-hint {
  margin: 0;
  font-size: 11px;
  color: var(--muted);
}
.mini.gray { background: var(--line); color: var(--muted); }
.mini.danger { background: #fde2e2; color: var(--red); }

.regbar {
  margin: 8px 12px 0;
  padding: 8px 12px;
  background: var(--card);
  border: 1px solid var(--blue);
  border-left: 5px solid var(--blue);
  border-radius: 12px;
  display: flex;
  align-items: center;
  gap: 8px;
}
.regbar b { font-size: 15px; }
.reg-count { font-size: 12px; color: var(--muted); white-space: nowrap; }

.entry.reg { border-left-color: var(--blue); }
.badge.reg { background: var(--blue); }

.skel-canvas {
  display: block;
  width: 160px;
  height: 160px;
  margin-top: 8px;
  border-radius: 12px;
  background: #000;
  cursor: pointer;
}

.learnstats {
  margin: 8px 12px 0;
  padding: 7px 12px;
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 12px;
  font-size: 12px;
  color: var(--muted);
}

.entry.correct { border-left-color: var(--green); }
.entry.wrong { border-left-color: var(--red); }
.entry-judge {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 4px;
}
.badge {
  font-size: 12px;
  font-weight: 800;
  color: #fff;
  border-radius: 8px;
  padding: 2px 10px;
}
.badge.correct { background: var(--green); }
.badge.wrong { background: var(--red); }
.badge.fail { background: var(--muted); }
.judge-target { font-size: 12px; color: var(--muted); }
.savenote { margin-top: 4px; font-size: 11px; color: var(--muted); }
.savenote.err { color: var(--red); }
