:root {
  color-scheme: light dark;
  --ptt-green: #19a874;
  --ptt-green-2: #0f8b5f;
  --ptt-red: #d64242;
  --ptt-red-2: #b63636;
  --btn: #2b6;
  --btn-2: #249a58;
  --tap-min: 44px;
  --ctrl-h: 40px;
}

/* ===== Base ===== */
* {
  box-sizing: border-box;
}
body {
  font-family: ui-sans-serif, -apple-system, 'Segoe UI', Roboto, 'Noto Sans JP',
    Arial, sans-serif;
  margin: 16px;
  line-height: 1.6;
}
header h1 {
  margin: 0 0 12px;
  font-size: 20px;
}

/* ===== Layout (pane) ===== */
.pane {
  display: grid;
  grid-template-columns: 1fr;
  gap: 12px;
  width: 100%;
}
@media (min-width: 769px) {
  .pane {
    grid-template-columns: 1fr minmax(320px, 40%);
    align-items: start;
    column-gap: 12px;
  }
}
.pane-col {
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.pane-title {
  margin: 6px 2px;
  font-size: 14px;
  opacity: 0.85;
}
.pane-title.with-toggle {
  display: flex;
  gap: 8px;
  align-items: baseline;
}

/* ===== Controls / Chat 行のベース ===== */
.controls,
.chat {
  display: flex;
  gap: 12px;
  align-items: center;
  flex-wrap: wrap;
  margin-bottom: 12px;
}

/* ===== Buttons & Forms ===== */
.btn {
  padding: 8px 14px;
  font-size: 14px;
  border-radius: 10px;
  border: 1px solid #8888;
  background: #f1f1f1;
  cursor: pointer;
  transition: transform 0.04s ease, filter 0.15s ease, box-shadow 0.15s ease,
    background 0.15s ease;
}
.btn:hover {
  filter: brightness(1.03);
}
.btn:active {
  transform: translateY(1px);
}
.primary {
  background: var(--btn);
  color: #fff;
  border: 0;
}
.primary:hover {
  filter: brightness(1.06);
}
.danger {
  background: #b33636;
  color: #fff;
  border: 0;
}

/* 宛先（ラベル+セレクト） */
.callsign-wrap {
  display: grid;
  grid-template-columns: auto minmax(200px, 260px);
  gap: 10px;
  align-items: center;
}
.label {
  font-size: 14px;
}
#callsignSelect {
  height: 40px;
  line-height: 40px;
  padding: 0 10px;
  font-size: 14px;
  border-radius: 8px;
}

/* ===== PTT 大型ボタン ===== */
.ptt-large {
  min-width: clamp(160px, 32vw, 260px);
  height: clamp(56px, 8vh, 72px);
  padding: 8px 18px;
  font-size: clamp(18px, 3.6vw, 24px);
  font-weight: 800;
  letter-spacing: 0.5px;
  border-radius: 16px;
  border: 2px solid #777;
  background: #f2f2f2;
  cursor: pointer;
  user-select: none;
  transition: transform 0.04s, filter 0.15s, box-shadow 0.15s, background 0.15s,
    border-color 0.15s;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.06);
}
.ptt-large:hover {
  filter: brightness(1.03);
}
.ptt-large:active {
  transform: translateY(1px);
}
.ptt-large:focus-visible {
  outline: none;
  box-shadow: 0 0 0 4px rgba(60, 130, 240, 0.25);
}
.ptt-large.ptt-on {
  color: #fff;
  border-color: var(--ptt-green-2);
  background: linear-gradient(180deg, var(--ptt-green), var(--ptt-green-2));
}
.ptt-large.ptt-off {
  color: #fff;
  border-color: var(--ptt-red-2);
  background: linear-gradient(180deg, var(--ptt-red), var(--ptt-red-2));
}
.ptt-large.ptt-on:hover,
.ptt-large.ptt-off:hover {
  filter: brightness(1.06);
}

/* ===== Chat 入力（親 .chat をグリッド化） ===== */
section.chat {
  display: grid !important;
  grid-template-columns: 1fr auto auto auto;
  gap: 10px;
  align-items: start;
}
section.chat > .chat-form {
  display: contents;
}
section.chat #chatInput {
  grid-column: 1;
}
section.chat .file-btn {
  grid-column: 2;
}
section.chat .send-btn {
  grid-column: 3;
}
section.chat #resetBtn {
  grid-column: 4;
}
section.chat > .hint {
  grid-column: 2 / -1;
  justify-self: start;
  margin-top: 2px;
}

/* フォーム内ボタンは縦中央（高さは伸ばさない） */
.chat-form .file-btn,
.chat-form .send-btn,
.chat-form #resetBtn {
  align-self: center;
  min-height: var(--tap-min);
  width: auto;
}

/* 入力（textarea） */
#chatInput {
  padding: 10px 12px;
  font-size: 14px;
  border-radius: 10px;
  border: 1px solid #ccc;
  display: block;
  min-height: 40px;
  max-height: 160px;
  overflow: auto;
  resize: none;
  line-height: 1.4;
}

/* 添付ボタン/送信ボタン */
.visually-hidden {
  position: absolute !important;
  left: -9999px !important;
  width: 1px;
  height: 1px;
  overflow: hidden;
}
.file-btn {
  display: inline-flex;
  align-items: center;
  gap: 0.4em;
  padding: 10px 12px;
  font-size: 14px;
  border-radius: 10px;
  border: 1px solid #8888;
  background: #f1f1f1;
  cursor: pointer;
  transition: transform 0.04s, filter 0.15s, box-shadow 0.15s;
  white-space: nowrap;
  width: auto;
}
.file-btn:hover {
  filter: brightness(1.03);
}
.file-btn:active {
  transform: translateY(1px);
}
.send-btn {
  background: var(--btn);
  color: #fff;
  border: 0;
  padding: 10px 16px;
  font-weight: 700;
  border-radius: 10px;
  width: auto;
}
.send-btn:hover {
  filter: brightness(1.06);
}
.hint {
  color: #666;
  font-size: 12px;
}

/* ===== Logs ===== */
.log {
  border: 1px solid #ddd;
  padding: 8px;
  height: 800px;
  overflow: auto;
  background: #fafafa;
}
.chatlog {
  height: 800px;
}
.syslog.hidden {
  display: none;
}

/* 既存：チャット・システム・画像の色 */
.log .chat {
  color: #1565c0;
} /* blue-700 */
.log .sys {
  color: #1976d2;
} /* blue-600 */
.log .img {
  color: #1e88e5;
} /* blue-500 */

/* 画像ログの見出しを1行扱い（折り返し可） */
.log .imghead {
  white-space: pre-wrap;
  word-break: break-word;
}

.log .time {
  color: #000;
  opacity: 0.85;
  margin-right: 6px;
}
.log figure {
  margin: 6px 0;
}
.log img {
  max-width: min(480px, 92%);
  height: auto;
  border: 1px solid #ddd;
  border-radius: 6px;
  display: block;
}
.log .chat,
.log .sys,
.log .img {
  white-space: pre-wrap;
  word-break: break-word;
}

/* ★ 追加：新しいテキストチャット行 (.chatline) を青字＋折り返し */
.log .chatline {
  color: #1565c0;
  white-space: pre-wrap;
  word-break: break-word;
}
.log .chatline .head,
.log .chatline .body {
  color: inherit;
}

/* ===== Dialog / Modal ===== */
dialog::backdrop {
  background: rgba(0, 0, 0, 0.3);
}
dialog {
  padding: 0;
}
.modal {
  /* デフォルトはPC想定。各ブレークポイントで上書き */
  min-width: min(520px, 92vw);
  display: grid;
  gap: 10px;
  padding: 12px;
  box-sizing: border-box;
}
.modal * {
  min-width: 0;
}
.list {
  list-style: none;
  padding: 0;
  margin: 0;
  border: 1px solid #ddd;
  max-height: 220px;
  overflow: auto;
}
.list li {
  display: grid;
  grid-template-columns: 20px 1fr;
  gap: 8px;
  align-items: center;
  padding: 6px 8px;
  border-bottom: 1px solid #eee;
}
.list li:last-child {
  border-bottom: 0;
}
.list input[type='checkbox'] {
  margin: 0;
}
.row {
  display: flex;
  gap: 8px;
  align-items: center;
}
.row.end {
  justify-content: flex-end;
}
.note {
  font-size: 12px;
  color: #666;
}
audio {
  display: none;
}
video {
  display: block;
  margin-top: 6px;
}

.modal .row {
  display: flex;
  gap: 12px;
  align-items: flex-end;
  flex-wrap: nowrap;
}
.modal .row > div {
  display: flex;
  flex-direction: column;
}
.modal .row .label {
  display: block;
  margin-bottom: 6px;
  line-height: 1.1;
  white-space: nowrap;
}
.modal input[type='text'],
.modal input:not([type]),
.modal input {
  height: var(--ctrl-h);
  padding: 8px 10px;
  border: 1px solid #ccd2d8;
  border-radius: 8px;
  box-sizing: border-box;
}
.modal .row .btn {
  height: var(--ctrl-h);
  line-height: calc(var(--ctrl-h) - 2px);
  padding: 0 14px;
}

/* ===== iOS風トグル ===== */
.toggle {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  user-select: none;
  cursor: pointer;
  position: relative;
  line-height: 1;
}
.toggle > input[type='checkbox'] {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  opacity: 0;
  margin: 0;
  cursor: pointer;
}
.toggle .track {
  display: inline-block;
  width: 48px;
  height: 28px;
  background: #cfd8dc;
  border-radius: 999px;
  position: relative;
  transition: background 0.18s, box-shadow 0.18s;
  box-shadow: inset 0 0 0 0 rgba(0, 0, 0, 0);
}
.toggle .thumb {
  position: absolute;
  left: 2px;
  top: 2px;
  width: 24px;
  height: 24px;
  border-radius: 999px;
  background: #fff;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.18);
  transition: left 0.18s, box-shadow 0.18s;
}
.toggle > input[type='checkbox']:checked + .track {
  background: var(--ptt-green, #19a874);
}
.toggle > input[type='checkbox']:checked + .track .thumb {
  left: 22px;
}
.toggle > input[type='checkbox']:focus-visible + .track {
  box-shadow: 0 0 0 3px rgba(60, 130, 240, 0.35);
}
.toggle > input[type='checkbox']:disabled + .track {
  filter: grayscale(0.3);
  opacity: 0.65;
  cursor: not-allowed;
}
.toggle > input[type='checkbox']:disabled ~ .label {
  opacity: 0.6;
  cursor: not-allowed;
}
.toggle .label {
  font-size: 14px;
}

/* ===== PC（≥769px）で宛先幅を少し広く ===== */
@media (min-width: 769px) {
  .callsign-wrap {
    grid-template-columns: auto minmax(280px, 440px);
  }
  #callsignSelect {
    min-width: 300px;
  }
}

/* ===== Smartphone (≤480px) ===== */
@media (max-width: 480px) {
  body {
    margin: 10px;
  }
  header h1 {
    font-size: 18px;
  }

  /* controls：3段（宛先 / PTT / トグル）を中央寄せ */
  .controls {
    display: flex !important;
    flex-wrap: wrap !important;
    gap: 10px;
    row-gap: 10px;
    justify-content: center;
    align-items: center;
  }
  .controls .callsign-wrap {
    order: 1;
    flex: 1 1 100%;
    min-width: 0;
    grid-template-columns: 1fr;
    gap: 6px;
    align-items: stretch;
  }
  #callsignSelect {
    width: 100%;
    min-width: 0;
  }

  #pttBtn {
    order: 2;
    flex: 1 1 100%;
    max-width: 420px;
    min-height: var(--tap-min);
    margin: 0 auto;
  }
  .controls .toggle {
    order: 3;
    flex: 0 0 auto;
  }
  .controls .toggle + .toggle {
    margin-left: 8px;
  }

  section.chat {
    grid-template-columns: 1fr 1fr 1fr;
  }
  section.chat #chatInput {
    grid-column: 1 / -1;
  }
  section.chat .file-btn {
    grid-column: 1;
  }
  section.chat .send-btn {
    grid-column: 2;
  }
  section.chat #resetBtn {
    grid-column: 3;
  }
  section.chat > .hint {
    grid-column: 1 / -1;
  }

  .log {
    height: 300px;
  }
  .chatlog {
    height: 340px;
  }
  .log img {
    max-width: 100%;
  }

  /* ===== モーダル（スマホ：右欠け根治 & スクロール滑らか） ===== */
  dialog .modal {
    width: min(460px, 100%);
    max-width: none;
    min-width: 0;
    padding-left: max(12px, env(safe-area-inset-left, 0px) + 2px);
    padding-right: max(14px, env(safe-area-inset-right, 0px) + 4px);
    padding-top: 12px;
    padding-bottom: 12px;
    margin: 0 auto;
    box-sizing: border-box;
    max-height: 88svh;
    overflow-y: auto;
    overflow-x: clip;
    -webkit-overflow-scrolling: touch;
    overscroll-behavior: contain;
    touch-action: pan-y;
  }
  @supports not (height: 1svh) {
    dialog .modal {
      max-height: 88vh;
    }
  }
  dialog .modal input,
  dialog .modal select,
  dialog .modal textarea,
  dialog .modal button,
  dialog .modal .btn {
    font-size: 16px;
  }
  dialog .modal .row {
    display: flex;
    flex-wrap: wrap;
    align-items: stretch;
    gap: 10px;
  }
  dialog .modal .row > * {
    flex: 1 1 100%;
    min-width: 0;
  }
  dialog .modal .row .btn {
    width: 100%;
    white-space: nowrap;
  }

  /* ← 画像ボタンのテキストを確実に中央寄せ */
  section.chat .file-btn {
    display: flex;
    justify-content: center;
    align-items: center;
  }
}

/* ===== Tablet (481px–1024px) ===== */
@media (min-width: 481px) and (max-width: 1024px) {
  body {
    margin: 12px;
  }

  .controls {
    display: flex !important;
    flex-wrap: wrap !important;
    gap: 10px;
    row-gap: 10px;
    justify-content: center;
    align-items: center;
    max-width: 720px;
    margin: 0 auto;
  }
  .controls .callsign-wrap {
    order: 1;
    flex: 1 1 100%;
    min-width: 0;
    grid-template-columns: 1fr;
    gap: 6px;
    align-items: stretch;
  }
  #callsignSelect {
    width: 100%;
    min-width: 0;
  }
  #pttBtn {
    order: 2;
    flex: 1 1 100%;
    width: 100%;
    max-width: none;
    margin: 0;
    min-height: var(--tap-min);
  }
  .controls .toggle {
    order: 3;
    flex: 0 0 auto;
  }
  .controls .toggle + .toggle {
    margin-left: 8px;
  }

  section.controls {
    margin-bottom: 18px !important;
  }

  /* ==== Tablet: チャットはスマホと同じ2段構成（入力→ボタン3つ） ==== */
  section.chat {
    margin-top: 10px !important;
    grid-template-columns: 1fr 1fr 1fr; /* 下段の3ボタンを等幅に */
  }

  /* 1行目＝テキストエリアを全幅 */
  section.chat #chatInput {
    grid-column: 1 / -1;
  }

  /* 2行目＝画像／送信／終了 を各カラムに配置して中央寄せ＆幅そろえ */
  section.chat .file-btn {
    grid-column: 1;
    justify-self: center;
    display: flex; /* ← 中の文字も中央寄せ */
    justify-content: center;
    align-items: center;
    width: 100%;
  }
  section.chat .send-btn {
    grid-column: 2;
    justify-self: center;
    width: 100%;
  }
  section.chat #resetBtn {
    grid-column: 3;
    justify-self: center;
    width: 100%;
  }

  /* ヒントは下段の先頭から全幅 */
  section.chat > .hint {
    grid-column: 1 / -1;
  }

  /* モーダル（タブレット：はみ出し/横スクロール防止） */
  dialog .modal {
    width: clamp(520px, 90vw, 800px);
    min-width: 0;
    max-width: 800px;
    margin: 0 auto;
    overflow-x: clip;
  }
  dialog .modal .row {
    flex-wrap: wrap;
  }
  dialog .modal .row > div {
    flex: 1 1 260px;
    min-width: 0;
  }
  dialog .modal input[type='text'],
  dialog .modal input:not([type]) {
    width: 100%;
  }

  /* 「新規 + 追加」を 50/50 に（はみ出し防止） */
  #newCallsign {
    flex: 1 1 50%;
    min-width: 0;
  }
  #addCallsignBtn {
    flex: 1 1 50%;
    min-width: 0;
    width: 100%;
    white-space: nowrap;
  }
  #addGroupBtn {
    width: 100%;
    white-space: nowrap;
  }

  /* ← タブレットでも画像ボタンのテキストを中央寄せ */
  section.chat .file-btn {
    display: flex;
    justify-content: center;
    align-items: center;
  }
}

/* ===== 小さな共通ユーティリティ ===== */
.log .ts,
.ts {
  color: #000 !important;
  opacity: 0.85;
}
