:root {
  --bg: #0d0d16;
  --panel: #151528;
  --panel-2: #1c1c36;
  --text: #f4f4f8;
  --muted: #9aa0b8;
  --accent: #ff4d6d;
  --live: #35d07f;
  --wait: #c9a227;
  --radius: 14px;
  --video-w: 16;
  --video-h: 9;
  --vv-height: 100dvh;
  --vv-offset-top: 0px;
  color-scheme: dark;
}

* { box-sizing: border-box; margin: 0; padding: 0; }

html, body {
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: system-ui, -apple-system, "Segoe UI", sans-serif;
}

.app {
  min-height: 100%;
  min-height: 100dvh;
  height: 100dvh;
  max-height: 100dvh;
  display: flex;
  flex-direction: column;
  padding: calc(0.6rem + env(safe-area-inset-top)) 0.8rem calc(0.8rem + env(safe-area-inset-bottom));
  max-width: min(2600px, 100%);
  margin: 0 auto;
  overflow: hidden;
}

/* スマホ縦: ページはスクロールせず、映像を上に固定。キーボード分は --vv-* で合わせる */
@media (max-width: 879px) {
  html, body {
    height: 100%;
    overflow: hidden;
    overscroll-behavior: none;
  }
  .app {
    position: fixed;
    left: 0;
    width: 100%;
    top: var(--vv-offset-top, 0px);
    height: var(--vv-height, 100dvh);
    max-height: var(--vv-height, 100dvh);
    min-height: 0;
    overflow: hidden;
  }
}

.top {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 0.6rem;
  flex-wrap: wrap;
  margin-bottom: 0.7rem;
}

h1 { font-size: 1.15rem; font-weight: 700; }
h2 { font-size: 0.95rem; font-weight: 600; margin-bottom: 0.4rem; }

.meta {
  display: flex;
  align-items: center;
  gap: 0.7rem;
  color: var(--muted);
  font-size: 0.9rem;
}

.badge {
  display: inline-block;
  padding: 0.15rem 0.55rem;
  border-radius: 999px;
  font-weight: 700;
  font-size: 0.8rem;
  letter-spacing: 0.04em;
}
.badge.live { background: rgba(53, 208, 127, 0.18); color: var(--live); }
.badge.wait { background: rgba(201, 162, 39, 0.18); color: var(--wait); }
.badge.err { background: rgba(255, 77, 109, 0.18); color: var(--accent); }

.stage {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 0.8rem;
  min-height: 0;
}

.video-col {
  width: 100%;
  flex: 0 0 auto;
}

.video-wrap {
  position: relative;
  background: #000;
  border-radius: var(--radius);
  overflow: hidden;
  /* 幅を高さ上限に合わせて縮め、映像の縦横比を保つ（16:9でも9:16でも切れない） */
  width: min(100%, calc(min(70vh, var(--vv-height, 100dvh) - 12rem) * var(--video-w) / var(--video-h)));
  aspect-ratio: var(--video-w) / var(--video-h);
  max-width: 100%;
  margin-inline: auto;
}

video#video {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  display: block;
  object-fit: contain;
  object-position: center;
  background: #000;
}

.overlay {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 0.8rem;
  background: rgba(0, 0, 0, 0.35);
  pointer-events: none;
  text-align: center;
  padding: 1rem;
  z-index: 2;
}

.overlay.idle { background: transparent; }

.overlay p {
  text-shadow: 0 1px 4px #000;
  font-weight: 600;
}

.unmute, .fs-btn {
  pointer-events: auto;
  border: 0;
  color: #fff;
  font-weight: 700;
  cursor: pointer;
}

.unmute {
  background: var(--accent);
  font-size: 1rem;
  padding: 0.75rem 1.2rem;
  min-height: 44px;
  border-radius: 999px;
}

.fs-btn {
  position: absolute;
  right: 0.6rem;
  bottom: 0.6rem;
  z-index: 3;
  background: rgba(0, 0, 0, 0.55);
  border-radius: 8px;
  padding: 0.45rem 0.65rem;
  min-height: 44px;
  min-width: 44px;
  font-size: 0.9rem;
}

.hidden { display: none !important; }

.chat {
  flex: 1 1 auto;
  display: flex;
  flex-direction: column;
  background: var(--panel);
  border-radius: var(--radius);
  padding: 0.8rem;
  min-height: 0;
  /* 狭い画面では残りの高さに収め、入力欄は常に見える */
  overflow: hidden;
}

.messages {
  flex: 1;
  overflow-y: auto;
  background: rgba(0, 0, 0, 0.25);
  border-radius: 10px;
  padding: 0.6rem;
  min-height: 0;
  -webkit-overflow-scrolling: touch;
}

.msg { margin-bottom: 0.45rem; font-size: 0.92rem; line-height: 1.4; word-break: break-word; }
.msg .name { font-weight: 700; color: #7ee0c3; margin-right: 0.25rem; }
.msg .time { color: var(--muted); font-size: 0.75rem; margin-right: 0.35rem; }

.composer {
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
  margin-top: 0.5rem;
  min-width: 0;
}

.composer-name-row {
  display: flex;
  align-items: center;
  min-width: 0;
}

.name-chip {
  border: 0;
  background: transparent;
  color: var(--muted);
  font-size: 0.8rem;
  line-height: 1.3;
  padding: 0.15rem 0.1rem;
  min-height: 28px;
  cursor: pointer;
  text-align: left;
  max-width: 100%;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.name-chip-edit {
  color: var(--text);
  text-decoration: underline;
  text-underline-offset: 0.15em;
  margin-left: 0.15rem;
}

.composer input {
  min-width: 0;
  min-height: 44px;
  border-radius: 10px;
  border: 1px solid #3a3a5c;
  background: var(--panel-2);
  color: var(--text);
  padding: 0.5rem 0.7rem;
  /* 16px未満だと iPhone Safari が入力時に拡大する */
  font-size: 16px;
}

.composer input.name-edit {
  flex: 1;
  min-height: 36px;
  font-size: 16px;
}

.composer-msg-row {
  display: flex;
  gap: 0.4rem;
  min-width: 0;
  align-items: stretch;
}

.composer-msg-row input#msg {
  flex: 1 1 auto;
  min-width: 0;
}

.composer-msg-row button {
  flex: 0 0 auto;
  border: 0;
  background: var(--accent);
  color: #fff;
  font-weight: 700;
  border-radius: 10px;
  padding: 0 0.75rem;
  min-height: 44px;
  min-width: 3.6rem;
  font-size: 16px;
  cursor: pointer;
}

.hint { color: var(--accent); font-size: 0.8rem; margin-top: 0.35rem; }

.gate {
  max-width: 26rem;
  margin: 15vh auto;
  background: var(--panel);
  border-radius: var(--radius);
  padding: 1.5rem;
}
.gate h1 { margin-bottom: 0.6rem; }
.gate p { color: var(--muted); margin-bottom: 1rem; line-height: 1.5; }
.gate input {
  width: 100%;
  min-height: 44px;
  border-radius: 10px;
  border: 1px solid #3a3a5c;
  background: var(--panel-2);
  color: var(--text);
  padding: 0.5rem 0.7rem;
  font-size: 16px;
  margin-bottom: 0.7rem;
}
.gate button {
  width: 100%;
  border: 0;
  background: var(--accent);
  color: #fff;
  font-weight: 700;
  border-radius: 10px;
  min-height: 44px;
  cursor: pointer;
}
.gate .err { color: var(--accent); margin-bottom: 0.6rem; }

@media (min-width: 880px) {
  /* コメント欄の高さを映像に合わせ、多いときは中でスクロールする */
  .stage {
    display: grid;
    grid-template-columns: minmax(0, 1fr) 340px;
    flex-direction: row;
    align-items: stretch;
  }
  .video-col { flex: unset; max-width: none; width: 100%; }
  .video-wrap { width: min(100%, calc((100dvh - 5rem) * var(--video-w) / var(--video-h))); }
  .chat {
    flex: unset;
    width: auto;
    max-height: none;
    min-height: 0;
    overflow: hidden;
  }
}

@media (orientation: landscape) and (max-height: 500px) {
  .app {
    padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
  }
  /* タイトルバーを重ねて、映像の高さをできるだけ大きくする */
  .top {
    position: absolute;
    top: env(safe-area-inset-top);
    left: env(safe-area-inset-left);
    right: auto;
    z-index: 4;
    margin: 0;
    padding: 0.3rem 0.55rem;
    width: auto;
    max-width: calc(100% - min(28vw, 170px) - 0.6rem);
    background: linear-gradient(to right, rgba(0, 0, 0, 0.55), transparent);
    pointer-events: none;
  }
  .top .meta { pointer-events: auto; margin-left: 0; }
  .top h1 { display: none; }
  .stage {
    display: grid;
    grid-template-columns: minmax(0, 1fr) min(28vw, 170px);
    flex-direction: row;
    align-items: stretch;
    gap: 0.3rem;
    height: 100%;
  }
  .video-col { flex: unset; max-width: none; width: 100%; }
  .video-wrap {
    width: min(100%, calc(var(--vv-height, 100dvh) * var(--video-w) / var(--video-h)));
    margin-inline: 0;
    border-radius: 8px;
  }
  .chat {
    flex: unset;
    width: auto;
    min-height: 0;
    max-height: none;
    overflow: hidden;
    padding: 0.4rem 0.45rem;
    border-radius: 8px;
  }
  .chat h2 { display: none; }
  h1 { font-size: 1rem; }

  /* 横画面で入力中は、画面の見える範囲の下端いっぱいに入力欄を出す */
  html.composing .composer,
  html.preview-composing .composer,
  .app:has(#msg:focus) .composer {
    position: fixed;
    left: 0;
    right: 0;
    top: calc(var(--vv-offset-top, 0px) + var(--vv-height, 100dvh));
    bottom: auto;
    transform: translateY(-100%);
    z-index: 30;
    margin: 0;
    padding: 0.4rem 0.8rem calc(0.4rem + env(safe-area-inset-bottom));
    background: var(--panel);
    border-top: 1px solid #3a3a5c;
    box-shadow: 0 -8px 24px rgba(0, 0, 0, 0.35);
  }
  html.composing .composer-name-row,
  html.preview-composing .composer-name-row,
  .app:has(#msg:focus) .composer-name-row {
    display: none;
  }
}

/* ---- admin ---- */
.admin {
  max-width: 36rem;
  margin: 8vh auto;
  background: var(--panel);
  border-radius: var(--radius);
  padding: 1.5rem;
}
.admin h1 { margin-bottom: 0.5rem; }
.admin p { color: var(--muted); line-height: 1.55; margin-bottom: 0.8rem; }
.admin .warn {
  color: var(--wait);
  background: rgba(201, 162, 39, 0.12);
  border-radius: 10px;
  padding: 0.7rem 0.8rem;
}
.share-box {
  display: flex;
  gap: 0.4rem;
  flex-wrap: wrap;
  margin: 0.8rem 0 1rem;
}
.share-box input {
  flex: 1;
  min-width: 0;
  min-height: 44px;
  border-radius: 10px;
  border: 1px solid #3a3a5c;
  background: var(--panel-2);
  color: var(--text);
  padding: 0.5rem 0.7rem;
  font-size: 0.95rem;
}
.share-box button, .admin button {
  border: 0;
  background: var(--accent);
  color: #fff;
  font-weight: 700;
  border-radius: 10px;
  min-height: 44px;
  padding: 0 1rem;
  cursor: pointer;
}
.admin button.secondary {
  background: var(--panel-2);
  border: 1px solid #3a3a5c;
}
.admin .ok { color: var(--live); margin-top: 0.5rem; }
.admin h2 { margin: 1.6rem 0 0.5rem; font-size: 1.1rem; }
.admin .hint { font-size: 0.9rem; }
.admin strong { color: var(--text); font-weight: 700; }

/* ---- 満員の表示（視聴ページ） ---- */
.overlay.full { background: rgba(0, 0, 0, 0.82); pointer-events: auto; }
.full-box { display: flex; flex-direction: column; align-items: center; gap: 0.7rem; max-width: 22rem; }
.full-box .full-title { font-size: 1.6rem; font-weight: 800; color: #fff; }
.full-box .full-text { font-size: 1rem; line-height: 1.6; font-weight: 500; color: #e8e8f0; }
.full-retry {
  pointer-events: auto;
  border: 0;
  background: var(--accent);
  color: #fff;
  font-weight: 700;
  font-size: 1rem;
  min-height: 44px;
  padding: 0.6rem 1.2rem;
  border-radius: 999px;
  cursor: pointer;
}

/* ---- 配信者のコメント ---- */
.msg.streamer {
  background: rgba(255, 196, 0, 0.12);
  border-left: 3px solid #ffc400;
  border-radius: 6px;
  padding: 0.3rem 0.5rem;
}
.role-badge {
  display: inline-block;
  background: #ffc400;
  color: #1a1400;
  font-size: 0.72rem;
  font-weight: 800;
  border-radius: 999px;
  padding: 0.05rem 0.5rem;
  margin-right: 0.35rem;
  vertical-align: 0.05rem;
}

/* ---- 配信者用画面（管理ページ） ---- */
.admin-page { max-width: 1200px; margin: 0 auto; padding: 1rem 1rem 3rem; }
.admin-page .admin { max-width: none; margin: 0 0 1rem; }
.studio { display: grid; gap: 1rem; grid-template-columns: 1fr; margin-bottom: 1rem; }
@media (min-width: 960px) { .studio { grid-template-columns: minmax(0, 3fr) minmax(300px, 2fr); } }
.studio .card { background: var(--panel); border-radius: var(--radius); padding: 1rem; display: flex; flex-direction: column; gap: 0.6rem; min-width: 0; }
.studio h2 { font-size: 1.05rem; margin: 0; }
.preview-wrap { position: relative; background: #000; border-radius: 10px; overflow: hidden; aspect-ratio: 16 / 9; }
.preview-wrap video { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: contain; background: #000; }
.preview-msg { position: absolute; inset: 0; display: flex; align-items: center; justify-content: center; color: #fff; font-weight: 600; text-shadow: 0 1px 4px #000; text-align: center; padding: 1rem; pointer-events: none; }
.row { display: flex; gap: 0.5rem; align-items: center; flex-wrap: wrap; }
.studio button, .count-box button {
  border: 0; background: var(--accent); color: #fff; font-weight: 700; border-radius: 10px;
  min-height: 40px; padding: 0 0.9rem; cursor: pointer; font-size: 0.95rem;
}
.studio button.secondary, .count-box button.secondary { background: var(--panel-2); border: 1px solid #3a3a5c; }
.studio .note { color: var(--muted); font-size: 0.88rem; line-height: 1.5; }
.delay { font-size: 0.95rem; }
.delay strong { font-size: 1.15rem; }
.admin-messages { height: 22rem; overflow-y: auto; background: var(--panel-2); border-radius: 10px; padding: 0.6rem; }
@media (min-width: 960px) { .admin-messages { height: auto; flex: 1 1 0; min-height: 16rem; } }
.admin-composer { display: flex; gap: 0.4rem; }
.admin-composer input, .count-box input {
  flex: 1; min-width: 0; min-height: 40px; border-radius: 10px; border: 1px solid #3a3a5c;
  background: var(--panel-2); color: var(--text); padding: 0.4rem 0.7rem; font-size: 1rem;
}
.count-box { background: var(--panel); border-radius: var(--radius); padding: 1rem; margin-bottom: 1rem; }
.count-box h2 { font-size: 1.05rem; margin-bottom: 0.6rem; }
.count-now { font-size: 1.05rem; margin-bottom: 0.7rem; }
.count-now strong { font-size: 1.6rem; color: var(--live); }
.count-box input { max-width: 8rem; flex: 0 0 auto; }
.count-box .ok { color: var(--live); }
.count-box .err { color: var(--accent); }
.count-box .note { color: var(--muted); font-size: 0.88rem; line-height: 1.5; margin-top: 0.5rem; }
.studio-title { font-size: 1.3rem; margin: 0.4rem 0 0.8rem; }
.note-box { color: var(--wait) !important; background: rgba(201, 162, 39, 0.12); border-radius: 10px; padding: 0.7rem 0.8rem; }

/* ---- 見る人の登録 ---- */
.mode-note { border-radius: 10px; padding: 0.6rem 0.8rem; margin-bottom: 0.8rem; font-size: 0.95rem; line-height: 1.5; }
.mode-note.on { background: rgba(53, 208, 127, 0.12); color: var(--live); }
.mode-note.off { background: rgba(201, 162, 39, 0.12); color: var(--wait); }
.people-box .row input { max-width: 16rem; flex: 1 1 10rem; }
.people-wrap { overflow-x: auto; margin: 0.8rem 0 0.4rem; }
.people-table { width: 100%; border-collapse: collapse; font-size: 0.95rem; }
.people-table th, .people-table td { text-align: left; padding: 0.45rem 0.5rem; border-bottom: 1px solid #2a2a48; vertical-align: middle; }
.people-table th { color: var(--muted); font-weight: 600; font-size: 0.85rem; }
.people-table tr.stopped td { opacity: 0.6; }
.people-table code.pw { font-family: ui-monospace, Menlo, Consolas, monospace; font-size: 1.05rem; letter-spacing: 0.06em; background: var(--panel-2); padding: 0.15rem 0.45rem; border-radius: 6px; user-select: all; }
.people-table .ops { display: flex; gap: 0.35rem; flex-wrap: wrap; }
.people-table .ops button { min-height: 34px; padding: 0 0.7rem; font-size: 0.88rem; border: 0; border-radius: 8px; background: var(--accent); color: #fff; font-weight: 700; cursor: pointer; }
.people-table .ops button.secondary { background: var(--panel-2); border: 1px solid #3a3a5c; }
.people-table .ops button.warn-btn { background: #8a5a00; }
.st { display: inline-block; border-radius: 999px; padding: 0.1rem 0.6rem; font-size: 0.82rem; font-weight: 700; }
.st-on { background: rgba(53, 208, 127, 0.18); color: var(--live); }
.st-off { background: rgba(154, 160, 184, 0.15); color: var(--muted); }
.st-stop { background: rgba(255, 77, 109, 0.18); color: var(--accent); }
.msg .assigned { color: #ffc400; font-weight: 700; }
