:root {
  --primary: #1a73e8;
  --bg: #f5f7fa;
  --pane-bg: #ffffff;
  --text: #202124;
  --muted: #80868b;
  --src-color: #202124;
  --dst-color: #0b57d0;
  --border: #dadce0;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  font-family: "Hiragino Sans", "Yu Gothic UI", "Segoe UI", system-ui, sans-serif;
  background: var(--bg);
  color: var(--text);
}

body {
  display: flex;
  flex-direction: column;
  padding-top: env(safe-area-inset-top);
  padding-bottom: env(safe-area-inset-bottom);
}

header {
  background: var(--pane-bg);
  border-bottom: 1px solid var(--border);
  padding: 8px 12px;
}

.title-row {
  display: flex;
  align-items: center;
  gap: 10px;
}

h1 {
  font-size: 18px;
  margin: 0;
  color: var(--primary);
}

.status {
  font-size: 12px;
  padding: 2px 10px;
  border-radius: 10px;
  background: #e8eaed;
  color: var(--muted);
}
.status.connected { background: #e6f4ea; color: #137333; }
.status.connecting { background: #fef7e0; color: #b06000; }
.status.error { background: #fce8e6; color: #c5221f; }

/* ===== マイク入力レベルメーター（レインボー） ===== */
/* メーター本体＋しきい値つまみのラッパ。上下に三角を出すぶんの余白を確保 */
.mic-meter-wrap {
  position: relative;
  margin-top: 8px;
  padding: 5px 0;
  /* バー全体を送信感度つまみの操作面にする（ドラッグ/タップ）。
     touch-action:none で押下中の縦スクロール/長押し選択を抑止する。 */
  touch-action: none;
  cursor: ew-resize;
}
.mic-meter {
  position: relative;
  height: 14px;
  border-radius: 7px;
  overflow: hidden;
  border: 1px solid var(--border);
  /* 静音(左)→大音量(右) のレインボー。マスクで右側を隠して入力量を表現 */
  background: linear-gradient(90deg,
    #1e88e5 0%, #00e5ff 22%, #00e676 44%, #ffeb3b 64%, #ff9100 82%, #ff1744 100%);
}
.mic-meter-mask {
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
  width: 100%;
  background: #e8eaed;
  transition: width 80ms linear;
}
/* 送信しきい値(VAD)つまみ: この位置より右（大きい音）だけ送信する。
   縦の細線＋上下端の三角(▼▲)で示し、バーをドラッグ/タップして直接動かす。
   操作自体はラッパ(.mic-meter-wrap)が受けるため、つまみは pointer-events:none。 */
.vad-caret {
  position: absolute;
  top: 0;
  bottom: 0;
  left: 28%;
  width: 2px;
  transform: translateX(-50%); /* 細線の中心をしきい値位置に合わせる */
  background: rgba(32, 33, 36, 0.5);
  transition: left 80ms linear;
  pointer-events: none;
  filter: drop-shadow(0 0 1px rgba(255, 255, 255, 0.9)); /* 白フチで背景に埋もれない */
}
.vad-caret::before, .vad-caret::after {
  content: "";
  position: absolute;
  left: 50%;
  transform: translateX(-50%);
  width: 0;
  height: 0;
  border-left: 5px solid transparent;
  border-right: 5px solid transparent;
}
.vad-caret::before { top: 0; border-top: 5px solid #202124; }       /* 上端の▼（下向き） */
.vad-caret::after { bottom: 0; border-bottom: 5px solid #202124; }  /* 下端の▲（上向き） */
/* PTTモード時はしきい値を使わないのでつまみを隠す */
.vad-caret.disabled { display: none; }
.mic-meter-label {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 10px;
  font-weight: bold;
  letter-spacing: 0.04em;
  color: var(--muted);
  pointer-events: none;
  text-shadow: 0 0 2px #fff, 0 0 2px #fff, 0 0 2px #fff;
}
/* マイク無効時: 灰色一色＋減光で「使われていない」ことを明示 */
.mic-meter.off { opacity: 0.55; }
.mic-meter.off .mic-meter-mask { width: 100% !important; background: #d4d7dc; }
/* マイク有効時: 未点灯部はうっすらレインボーが透けて「生きている」ことを示す */
.mic-meter.active .mic-meter-mask { background: rgba(255, 255, 255, 0.4); }
.mic-meter.active .mic-meter-label { color: #263238; }

.controls {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-top: 8px;
  flex-wrap: wrap;
}
/* 操作群（1グループに統合）。400x800縦画面でも2行に収まるようコンパクトに */
.ctl-row {
  margin-top: 6px;
  gap: 5px 8px;
  font-size: 12px;
}
.ctl-row .lang-label { gap: 5px; }
.ctl-row .check-label { gap: 4px; }
.ctl-row select { font-size: 12px; padding: 3px 4px; }
.ctl-row #stateBtn,
.ctl-row #settingsBtn { margin-left: 0; font-size: 13px; padding: 4px 8px; } /* 右寄せ解除＝自然に折り返す */

/* リフレッシュ: アイコン付きの塗りボタン。実行中は色を変える */
#refreshBtn {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  font-size: 12px;
  font-weight: bold;
  padding: 4px 10px;
  border: 1px solid var(--primary);
  border-radius: 16px;
  background: var(--primary);
  color: #fff;
  cursor: pointer;
}
#refreshBtn .refresh-icon { font-size: 14px; line-height: 1; }
#refreshBtn:active { filter: brightness(0.92); }
/* 実行中（セッション再接続中）: 橙色＋アイコン回転で「処理中」を示す */
#refreshBtn.refreshing {
  background: #b06000;
  border-color: #b06000;
  cursor: progress;
}
#refreshBtn.refreshing .refresh-icon { animation: refresh-spin 0.8s linear infinite; }
@keyframes refresh-spin { to { transform: rotate(360deg); } }

.lang-label, .check-label {
  font-size: 13px;
  display: flex;
  align-items: center;
  gap: 6px;
}

select {
  font-size: 14px;
  padding: 4px 6px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: #fff;
}

#stateBtn {
  margin-left: auto;
  font-size: 12px;
  padding: 4px 10px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: #fff;
  cursor: pointer;
}
#stateBtn.active {
  background: var(--primary);
  color: #fff;
  border-color: var(--primary);
}

#settingsBtn {
  font-size: 18px;
  background: none;
  border: none;
  cursor: pointer;
  padding: 2px 6px;
}

.state-list {
  display: grid;
  grid-template-columns: auto 1fr;
  gap: 2px 14px;
  margin: 0;
  font-size: 12px;
  font-family: Consolas, "Courier New", monospace;
}
.state-list dt { color: var(--muted); }
.state-list dd { margin: 0; }

.settings {
  margin-top: 8px;
  padding: 8px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: #fafbfc;
  font-size: 13px;
}
.settings.hidden { display: none; }
.settings label { display: flex; flex-direction: column; gap: 4px; }
.settings input {
  font-size: 14px;
  padding: 6px;
  border: 1px solid var(--border);
  border-radius: 6px;
}
.note { margin: 6px 0 0; font-size: 11px; color: var(--muted); }

.auth-row { display: flex; gap: 6px; }
.auth-row input { flex: 1; min-width: 0; }
/* type=text のまま伏せ字表示（iOS Safari / Android Chrome / Firefox 132+ 対応） */
#authInput.masked { -webkit-text-security: disc; }
#authShowBtn {
  font-size: 16px;
  padding: 2px 10px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: #fff;
  cursor: pointer;
}
#authShowBtn.active {
  background: var(--primary);
  border-color: var(--primary);
}

main {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 0; /* 原文／翻訳の境界を最小化（間隔は境界バー自体だけにする） */
  padding: 8px 12px;
  min-height: 0;
}

.pane {
  flex: 1;
  display: flex;
  flex-direction: column;
  background: var(--pane-bg);
  border: 1px solid var(--border);
  border-radius: 10px;
  min-height: 0;
  overflow: hidden;
}

/* 原文／翻訳の境界（ドラッグで比率を変える）。縦並び=横棒、横並び=縦棒。
   見た目は可能な限り細く（2px）。ただし指で掴めるよう操作判定だけは ::after で
   上下に広げて確保する（レイアウト上の高さは2pxのまま）。 */
.pane-splitter {
  flex: none;
  position: relative;
  background: var(--border);
  height: 2px;
  cursor: row-resize;
  touch-action: none;
}
/* ドラッグ操作のヒット領域（不可視）。見た目の細さを保ったまま掴みやすくする */
.pane-splitter::after {
  content: "";
  position: absolute;
  left: 0;
  right: 0;
  top: -8px;
  bottom: -8px;
}
.pane-splitter:active { background: var(--muted); }

.pane h2 {
  font-size: 12px;
  margin: 0;
  padding: 6px 12px;
  color: var(--muted);
  border-bottom: 1px solid var(--border);
  background: #fafbfc;
}

.text-area {
  flex: 1;
  overflow-y: auto;
  padding: 10px 12px;
  font-size: 16px;
  line-height: 1.3;
  -webkit-overflow-scrolling: touch;
}

.text-area p { margin: 0 0 3px; }
#srcText p { color: var(--src-color); }
/* 入力言語の見出し（言語が変化した時のみ原文の行頭に挿入） */
#srcText p.lang-header {
  color: var(--primary);
  font-weight: bold;
  font-size: 12px;
  margin: 8px 0 2px;
  padding-top: 4px;
  border-top: 1px dashed var(--border);
}
#srcText p.lang-header:first-child { border-top: none; padding-top: 0; margin-top: 0; }
/* 言語ブロックの色（同じ言語は常に同じ色）。原文・見出しに加え、対応する翻訳文にも
   同じ色を適用して、原文と翻訳の言語対応を色で揃える。 */
#srcText p.lang-c0, #dstText p.lang-c0 { color: #1a73e8; } /* 青 */
#srcText p.lang-c1, #dstText p.lang-c1 { color: #188038; } /* 緑 */
#srcText p.lang-c2, #dstText p.lang-c2 { color: #c5221f; } /* 赤 */
#srcText p.lang-c3, #dstText p.lang-c3 { color: #8430ce; } /* 紫 */
#srcText p.lang-c4, #dstText p.lang-c4 { color: #b06000; } /* 橙 */
#srcText p.lang-c5, #dstText p.lang-c5 { color: #00796b; } /* 青緑 */
#dstText p { color: var(--dst-color); }
.text-area p.partial { color: var(--muted); }

footer {
  display: flex;
  gap: 10px;
  padding: 10px 12px calc(10px + env(safe-area-inset-bottom));
  background: var(--pane-bg);
  border-top: 1px solid var(--border);
}

.main-btn {
  flex: 1;
  font-size: 17px;
  font-weight: bold;
  padding: 12px;
  border: none;
  border-radius: 10px;
  background: var(--primary);
  color: #fff;
  cursor: pointer;
}
.main-btn.running { background: #c5221f; }

#clearBtn {
  font-size: 14px;
  padding: 12px 16px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: #fff;
  cursor: pointer;
}

/* PTT（押して話す）ボタン。押している間だけ送信 */
.ptt-btn {
  flex: 1.4;
  font-size: 17px;
  font-weight: bold;
  padding: 12px;
  border: none;
  border-radius: 10px;
  background: #5f6368;
  color: #fff;
  cursor: pointer;
  user-select: none;
  -webkit-user-select: none;
  touch-action: none; /* 押下中のスクロール/長押し選択を抑止 */
}
.ptt-btn.held {
  background: #137333; /* 送信中は緑 */
  box-shadow: inset 0 0 0 3px rgba(255, 255, 255, 0.4);
}
.ptt-btn.hidden { display: none; }

/* 戻るガードのデバッグ表示（?debug=1 のときだけ JS が生成）。タップは透過して操作を妨げない */
#backDebug {
  position: fixed;
  left: 4px;
  bottom: 4px;
  z-index: 9999;
  max-width: 92vw;
  padding: 4px 7px;
  background: rgba(0, 0, 0, 0.78);
  color: #19ff19;
  font: 11px/1.4 Consolas, "Courier New", monospace;
  border-radius: 6px;
  pointer-events: none;
  white-space: pre-wrap;
}

@media (min-width: 700px) {
  main { flex-direction: row; }
  /* 横並び時は境界を縦棒に（同じく細く2px、ヒット領域は左右に広げる） */
  .pane-splitter { height: auto; width: 2px; cursor: col-resize; }
  .pane-splitter::after { left: -8px; right: -8px; top: 0; bottom: 0; }
}
