/* ===== 色・サイズの定義 ===== */
:root {
  --bg: #0f0f14;
  --panel: #18181f;
  --panel-head: #1f1f28;
  --line: #2a2a35;
  --text: #e6e6ee;
  --muted: #8c8c9c;
  --accent: #fe2c55;      /* TikTokの赤 */
  --accent2: #25f4ee;     /* TikTokの水色 */
  --join: #4fc3f7;
  --follow: #ff6b9a;
  --share: #7ee081;
  --like: #ff5c7a;
  --gift: #ffc94d;
  --font-size: 15px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-left: env(safe-area-inset-left, 0px);
  --safe-right: env(safe-area-inset-right, 0px);
  color-scheme: dark;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Hiragino Sans", "Noto Sans JP", "Segoe UI", sans-serif;
  -webkit-text-size-adjust: 100%;
}

body {
  display: flex;
  flex-direction: column;
  padding: var(--safe-top) var(--safe-right) var(--safe-bottom) var(--safe-left);
  overflow: hidden;           /* 全体はスクロールさせず、各欄の中だけスクロール */
}

button { font: inherit; color: inherit; cursor: pointer; }
input { font: inherit; }

/* ===== 上部バー ===== */
.topbar {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 12px 4px;
}
.brand {
  font-weight: 800;
  font-size: 18px;
  background: linear-gradient(90deg, var(--accent2), var(--accent));
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
}
.status {
  flex: 1;
  display: flex;
  align-items: center;
  gap: 6px;
  min-width: 0;
  font-size: 12px;
  color: var(--muted);
}
.status span:last-child { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.dot {
  flex: none;
  width: 9px; height: 9px;
  border-radius: 50%;
  background: #555;
}
.dot.ok { background: #3ddc84; box-shadow: 0 0 6px #3ddc84; }
.dot.wait { background: #ffc94d; animation: blink 1s infinite; }
.dot.ng { background: var(--accent); }
@keyframes blink { 50% { opacity: .3; } }

.icon-btn {
  background: none;
  border: none;
  font-size: 20px;
  padding: 4px 6px;
  color: var(--muted);
}

/* ===== 接続フォーム ===== */
.connect {
  display: flex;
  gap: 8px;
  padding: 4px 12px;
}
.connect input {
  flex: 1;
  min-width: 0;
  padding: 9px 12px;
  border-radius: 10px;
  border: 1px solid var(--line);
  background: var(--panel);
  color: var(--text);
  font-size: 16px;          /* 16px未満だとiPhoneで自動ズームされる */
}
.primary {
  background: var(--accent);
  border: none;
  border-radius: 10px;
  padding: 9px 16px;
  font-weight: 700;
  color: #fff;
  white-space: nowrap;
}
.primary.disconnect { background: #444452; }

/* ===== 統計 ===== */
.stats {
  display: flex;
  gap: 16px;
  padding: 6px 14px 2px;
  font-size: 13px;
  color: var(--muted);
}
.stats b { color: var(--text); font-variant-numeric: tabular-nums; }

/* ===== 欄の表示切替 ===== */
.toggles {
  display: flex;
  gap: 6px;
  padding: 6px 12px;
  overflow-x: auto;
}
.toggles button {
  border: 1px solid var(--line);
  background: transparent;
  color: var(--muted);
  border-radius: 999px;
  padding: 4px 12px;
  font-size: 12px;
}
.toggles button.on {
  background: var(--panel-head);
  color: var(--text);
  border-color: #4a4a58;
}

/* ===== 欄（パネル） ===== */
.panels {
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
  gap: 8px;
  padding: 4px 12px 10px;
}
.row {
  display: flex;
  gap: 8px;
  min-height: 0;
}
/* スマホ：チャット大きめ、Join/Like は横並び、ギフトは下 */
.row-main { flex: 5; }
.row-sub  { flex: 3; }
.row-gift { flex: 2; }
.row:not(:has(.panel:not(.hidden))) { display: none; }

.panel {
  position: relative;
  flex: 1;
  min-width: 0;
  min-height: 0;
  display: flex;
  flex-direction: column;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 12px;
  overflow: hidden;
}
.panel.hidden { display: none; }

.panel h2 {
  margin: 0;
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 6px 10px;
  font-size: 12px;
  font-weight: 700;
  background: var(--panel-head);
  border-bottom: 1px solid var(--line);
}
#panel-chat h2 { color: var(--text); }
#panel-join h2 { color: var(--join); }
#panel-like h2 { color: var(--like); }
#panel-gift h2 { color: var(--gift); }
.count { color: var(--muted); font-weight: 400; }
.clear {
  margin-left: auto;
  background: none;
  border: none;
  color: var(--muted);
  font-size: 11px;
  padding: 0 2px;
}

.list {
  flex: 1;
  overflow-y: auto;
  overscroll-behavior: contain;
  -webkit-overflow-scrolling: touch;
  padding: 4px 0;
  font-size: var(--font-size);
  line-height: 1.45;
}

/* ===== 1行分の表示 ===== */
.item {
  display: flex;
  align-items: flex-start;
  gap: 8px;
  padding: 3px 10px;
  word-break: break-word;
  overflow-wrap: anywhere;
}
.item + .item { border-top: 1px solid rgba(255,255,255,.03); }
.avatar {
  flex: none;
  width: 1.6em; height: 1.6em;
  border-radius: 50%;
  object-fit: cover;
  background: #333;
  margin-top: .05em;
}
.avatar-fallback {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-size: .8em;
  font-weight: 700;
  color: #fff;
  overflow: hidden;
}
body.no-avatar .avatar { display: none; }
.body { min-width: 0; }
.nick { font-weight: 700; }
.uid { color: var(--muted); font-size: .85em; }
.sep { color: var(--muted); margin-right: .25em; }
.msg { color: var(--text); }

/* 小さい欄（Join / Like）は1行を詰める */
#panel-join .list, #panel-like .list { font-size: calc(var(--font-size) * .88); }
#panel-join .item, #panel-like .item { padding: 2px 8px; }

.tag {
  display: inline-block;
  font-size: .75em;
  font-weight: 700;
  border-radius: 4px;
  padding: 0 .4em;
  margin-right: .35em;
  vertical-align: .08em;
  color: #111;
}
.tag.join { background: var(--join); }
.tag.follow { background: var(--follow); }
.tag.share { background: var(--share); }

.like-count { color: var(--like); font-weight: 700; margin-left: .3em; white-space: nowrap; }

.item.system { color: var(--muted); font-size: .85em; justify-content: center; text-align: center; }

/* ギフト */
.gift-img { flex: none; width: 2em; height: 2em; object-fit: contain; }
.gift-name { color: var(--gift); font-weight: 700; }
.gift-repeat { font-weight: 700; margin-left: .3em; }
.item.pending .gift-repeat { color: var(--accent); }
.gift-dia { color: var(--muted); font-size: .85em; margin-left: .4em; white-space: nowrap; }

/* 新着ボタン */
.new-badge {
  position: absolute;
  right: 10px;
  bottom: 10px;
  background: var(--accent);
  color: #fff;
  border: none;
  border-radius: 999px;
  padding: 4px 12px;
  font-size: 12px;
  font-weight: 700;
  box-shadow: 0 2px 8px rgba(0,0,0,.5);
}

/* ===== 設定ダイアログ ===== */
dialog {
  width: min(420px, calc(100vw - 32px));
  border: 1px solid var(--line);
  border-radius: 14px;
  background: var(--panel);
  color: var(--text);
  padding: 0;
}
dialog::backdrop { background: rgba(0,0,0,.6); }
.settings {
  display: flex;
  flex-direction: column;
  gap: 14px;
  padding: 16px;
  font-size: 14px;
}
.settings h3 { margin: 0; }
.settings label { display: flex; flex-direction: column; gap: 6px; }
.settings label.check { flex-direction: row; align-items: center; gap: 8px; }
.settings input[type=password], .settings input[type=url], .settings input[type=number] {
  padding: 8px 10px;
  border-radius: 8px;
  border: 1px solid var(--line);
  background: var(--bg);
  color: var(--text);
  font-size: 16px;
}
/* アクセスキー入力欄と表示ボタン */
.key-field { display: flex; gap: 6px; }
.key-field input { flex: 1; min-width: 0; }
.settings .key-field input[type=text] {
  padding: 8px 10px;
  border-radius: 8px;
  border: 1px solid var(--line);
  background: var(--bg);
  color: var(--text);
  font-size: 16px;
}
.key-toggle {
  flex: none;
  min-width: 56px;
  background: transparent;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 0 10px;
  font-size: 13px;
}
.settings input[type=checkbox] { width: 18px; height: 18px; accent-color: var(--accent); }
.settings input[type=range] { accent-color: var(--accent); }
.settings details summary { color: var(--muted); cursor: pointer; margin-bottom: 8px; }
.dialog-actions { display: flex; justify-content: flex-end; gap: 8px; }
.dialog-actions button:not(.primary) {
  background: transparent;
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 9px 16px;
}

/* ===== PC・タブレット横向き：4列で並べる ===== */
@media (min-width: 900px) {
  .panels { flex-direction: row; }
  .row { display: contents; }
  #panel-chat { flex: 2; }
  .list { font-size: var(--font-size); }
}

/* iPhone 横向き（高さが低い時）は Join/Like/ギフトを右側にまとめる */
@media (max-width: 899px) and (max-height: 500px) and (orientation: landscape) {
  .stats, .toggles { display: none; }
  .panels { flex-direction: row; }
  .row-main { flex: 3; }
  .row-sub, .row-gift { flex: 2; flex-direction: column; }
}
