:root {
  color-scheme: light;
  --bg: #f3f5f7;
  --panel: #ffffff;
  --panel-soft: #fbfcfd;
  --panel-tint: #f7faf9;
  --ink: #17202a;
  --muted: #65707d;
  --line: #d9e0e7;
  --accent: #0f8f6b;
  --accent-dark: #087052;
  --accent-soft: #e3f6ef;
  --danger: #b93232;
  --danger-soft: #fff1f1;
  --mine: #dff5ee;
  --theirs: #edf1f5;
  --video: #101820;
  --video-panel: #1b2632;
  --topbar-bg: rgba(255, 255, 255, 0.98);
  --field-bg: #ffffff;
  --chat-bg: linear-gradient(#ffffff, #fbfcfd);
  --secondary-bg: #ffffff;
  --secondary-hover: #f3f6f8;
  --video-glow: #263746;
}

:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #0d1218;
  --panel: #151d26;
  --panel-soft: #101820;
  --panel-tint: #17232d;
  --ink: #edf4f8;
  --muted: #9daab7;
  --line: #2b3948;
  --accent: #2bbf91;
  --accent-dark: #1e9f78;
  --accent-soft: #14392e;
  --danger: #e05252;
  --danger-soft: #3a1e22;
  --mine: #164a3c;
  --theirs: #263341;
  --video: #070b10;
  --video-panel: #101822;
  --topbar-bg: rgba(21, 29, 38, 0.98);
  --field-bg: #0f1720;
  --chat-bg: linear-gradient(#151d26, #101820);
  --secondary-bg: #202b37;
  --secondary-hover: #2a3846;
  --video-glow: #123c45;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-height: 100vh;
  min-height: 100dvh;
  overflow: hidden;
  font-family: Arial, Helvetica, sans-serif;
  background: var(--video);
  color: var(--ink);
}

button,
input,
select {
  font: inherit;
}

[hidden] {
  display: none !important;
}

button,
input,
select {
  transition: border-color 0.15s ease, background 0.15s ease, color 0.15s ease, box-shadow 0.15s ease, transform 0.15s ease;
}

.app {
  width: 100vw;
  height: 100vh;
  height: 100dvh;
  min-height: 100vh;
  min-height: 100dvh;
  padding: 0;
}

.chat-shell {
  width: 100vw;
  height: 100vh;
  height: 100dvh;
  min-height: 0;
  display: grid;
  grid-template-rows: minmax(0, 1fr);
  background: var(--panel);
  border: 0;
  border-radius: 0;
  overflow: hidden;
  box-shadow: none;
}

.chat-shell.is-running {
  grid-template-rows: minmax(0, 1fr);
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 12px 18px;
  border-bottom: 1px solid var(--line);
  background: var(--topbar-bg);
}

h1 {
  margin: 0;
  font-size: 20px;
  letter-spacing: 0;
  line-height: 1.1;
}

.brand {
  min-width: 0;
}

.brand-title {
  display: flex;
  align-items: center;
  gap: 9px;
  min-width: 0;
  margin: 0 0 4px;
  white-space: nowrap;
}

.brand-title img {
  width: 32px;
  height: 32px;
  flex: 0 0 auto;
  border-radius: 8px;
}

p {
  margin: 0;
  color: var(--muted);
}

.top-actions {
  display: flex;
  align-items: center;
  gap: 10px;
  flex: 0 1 auto;
  min-width: 0;
}

.theme-toggle {
  position: relative;
  min-height: 36px;
  min-width: 118px;
  display: inline-grid;
  grid-template-columns: 1fr 1fr;
  align-items: center;
  padding: 3px;
  border: 1px solid var(--line);
  border-radius: 999px;
  color: var(--muted);
  background: var(--secondary-bg);
}

.theme-toggle::before {
  content: "";
  position: absolute;
  top: 3px;
  bottom: 3px;
  left: 3px;
  width: calc(50% - 3px);
  border-radius: 999px;
  background: var(--accent);
  transition: transform 0.18s ease;
}

.theme-toggle[aria-pressed="true"]::before {
  transform: translateX(100%);
}

.theme-toggle span {
  position: relative;
  z-index: 1;
  text-align: center;
  font-size: 12px;
  font-weight: 700;
}

.theme-toggle[aria-pressed="false"] .theme-sun,
.theme-toggle[aria-pressed="true"] .theme-moon {
  color: #ffffff;
}

.theme-toggle:hover:not(:disabled) {
  background: var(--secondary-hover);
  transform: none;
}

.status-pill {
  flex: 0 0 auto;
  border: 1px solid var(--line);
  border-radius: 999px;
  padding: 7px 12px;
  color: var(--muted);
  background: var(--panel-soft);
  min-width: 86px;
  text-align: center;
  font-weight: 700;
  font-size: 13px;
}

.status-pill.active {
  color: #06513c;
  border-color: #9ad8c5;
  background: var(--accent-soft);
}

.status-pill.ended {
  color: var(--danger);
  border-color: #efbbbb;
  background: var(--danger-soft);
}

.support-link {
  display: inline-flex;
  align-items: center;
  min-height: 36px;
  padding: 0 13px;
  border: 1px solid var(--line);
  border-radius: 999px;
  color: #ffffff;
  background: var(--accent);
  font-size: 13px;
  font-weight: 700;
  text-decoration: none;
  white-space: nowrap;
}

.support-link:hover {
  background: var(--accent-dark);
}

.setup {
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(0, 0.78fr) minmax(280px, 0.9fr);
  align-items: center;
  gap: 12px;
  height: 88px;
  min-height: 88px;
  max-height: 88px;
  padding: 12px 18px;
  border-bottom: 1px solid var(--line);
  background: var(--panel-tint);
  overflow: hidden;
}

.setup.is-hidden {
  display: none;
  pointer-events: none;
}

.setup[hidden] {
  display: none !important;
}

label {
  display: grid;
  gap: 6px;
  color: var(--muted);
  font-size: 13px;
}

input,
select {
  width: 100%;
  min-width: 0;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 11px 12px;
  color: var(--ink);
  background: var(--field-bg);
  outline: none;
}

input:focus,
select:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(15, 143, 107, 0.12);
}

button {
  min-height: 42px;
  border: 0;
  border-radius: 8px;
  padding: 0 18px;
  color: #ffffff;
  background: var(--accent);
  cursor: pointer;
  white-space: nowrap;
}

button:hover:not(:disabled) {
  background: var(--accent-dark);
  transform: translateY(-1px);
}

button:disabled {
  cursor: not-allowed;
  opacity: 0.55;
}

button.secondary {
  color: var(--ink);
  border: 1px solid var(--line);
  background: var(--secondary-bg);
}

#startStopBtn.stop-action {
  color: #ffffff;
  border-color: #596575;
  background: #596575;
}

#startStopBtn.stop-action:hover:not(:disabled) {
  background: #3f4854;
}

#nextBtn {
  color: #ffffff;
  border-color: var(--accent);
  background: var(--accent);
}

#nextBtn:hover:not(:disabled) {
  background: var(--accent-dark);
}

.primary-action {
  min-width: 132px;
  font-weight: 700;
}

button.secondary:hover:not(:disabled) {
  background: var(--secondary-hover);
}

.chat-panel {
  min-height: 0;
  display: grid;
  grid-template-rows: auto minmax(0, 1fr) auto;
  border-left: 1px solid var(--line);
  background: var(--panel);
}

.chat-panel.has-sponsor {
  grid-template-rows: auto auto minmax(0, 1fr) auto;
}

.chat-panel-head {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 10px;
  padding: 12px 14px;
  border-bottom: 1px solid var(--line);
  background: var(--panel-soft);
}

.chat-panel-head span {
  color: var(--muted);
  font-size: 12px;
}

.sponsor-card {
  display: grid;
  grid-template-columns: auto minmax(0, 1fr);
  gap: 10px;
  align-items: center;
  margin: 10px 12px 0;
  padding: 10px;
  border: 1px solid var(--line);
  border-radius: 10px;
  color: var(--ink);
  background: var(--panel-tint);
  text-decoration: none;
}

.sponsor-card:hover {
  border-color: var(--accent);
}

.sponsor-card img {
  width: 54px;
  height: 54px;
  border-radius: 8px;
  object-fit: cover;
}

.sponsor-card span {
  min-width: 0;
  display: grid;
  gap: 2px;
}

.sponsor-card small {
  color: var(--accent);
  font-size: 11px;
  font-weight: 800;
  text-transform: uppercase;
}

.sponsor-card strong {
  overflow: hidden;
  color: var(--ink);
  font-size: 14px;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.sponsor-card em {
  color: var(--muted);
  font-size: 12px;
  font-style: normal;
  line-height: 1.35;
}

.shortcut-help {
  position: relative;
  border: 1px solid var(--line);
  border-radius: 999px;
  color: var(--muted);
  background: var(--panel-soft);
  font-size: 12px;
}

.shortcut-help summary {
  cursor: pointer;
  min-height: 36px;
  display: flex;
  align-items: center;
  padding: 0 12px;
  color: var(--ink);
  font-weight: 700;
  list-style: none;
}

.shortcut-help summary::-webkit-details-marker {
  display: none;
}

.shortcut-help div {
  position: absolute;
  top: calc(100% + 8px);
  right: 0;
  z-index: 20;
  width: 280px;
  display: flex;
  flex-wrap: wrap;
  gap: 6px 8px;
  padding: 10px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--panel);
  box-shadow: 0 16px 40px rgba(0, 0, 0, 0.16);
}

.shortcut-help span {
  display: inline-flex;
  align-items: center;
  gap: 4px;
}

kbd {
  min-width: 22px;
  display: inline-flex;
  justify-content: center;
  border: 1px solid var(--line);
  border-bottom-width: 2px;
  border-radius: 5px;
  padding: 2px 5px;
  color: var(--ink);
  background: var(--field-bg);
  font-size: 11px;
  font-family: Arial, Helvetica, sans-serif;
  font-weight: 700;
}

.chat-scroll {
  min-height: 0;
  display: grid;
  grid-template-rows: minmax(0, 1fr) auto;
  background: var(--chat-bg);
}

.log {
  min-height: 0;
  max-height: 100%;
  overflow-y: auto;
  overscroll-behavior: contain;
  scrollbar-gutter: stable;
  scrollbar-width: thin;
  scrollbar-color: var(--muted) transparent;
  padding: 14px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  background: transparent;
}

.log::-webkit-scrollbar {
  width: 10px;
}

.log::-webkit-scrollbar-track {
  background: transparent;
}

.log::-webkit-scrollbar-thumb {
  border: 3px solid transparent;
  border-radius: 999px;
  background: var(--muted);
  background-clip: content-box;
}

.log::-webkit-scrollbar-thumb:hover {
  background: var(--accent);
  background-clip: content-box;
}

.stage {
  min-height: 0;
  display: grid;
  grid-template-columns: minmax(0, 1fr) clamp(300px, 26vw, 430px);
  background: var(--video);
  overflow: hidden;
}

.chat-shell.is-running .stage {
  grid-row: 1;
}

.left-stage {
  min-height: 0;
  display: grid;
  grid-template-rows: auto auto minmax(0, 1fr);
  overflow: hidden;
}

.chat-shell.is-running .left-stage {
  grid-template-rows: auto minmax(0, 1fr);
}

.video-grid {
  min-height: 0;
  overflow: hidden;
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
  grid-template-rows: minmax(0, 1fr);
  gap: 12px;
  padding: 12px;
  border-right: 0;
  background: radial-gradient(circle at top left, var(--video-glow), var(--video) 52%);
}

.video-box {
  position: relative;
  min-height: 0;
  overflow: hidden;
  border: 1px solid rgba(255, 255, 255, 0.08);
  border-radius: 10px;
  background: var(--video-panel);
  box-shadow: 0 16px 40px rgba(0, 0, 0, 0.22);
}

.video-box video {
  width: 100%;
  height: 100%;
  display: block;
  object-fit: cover;
  background: var(--video-panel);
}

.video-box.remote::before,
.video-box.local::before {
  content: "";
  position: absolute;
  inset: 0;
  background: linear-gradient(135deg, rgba(255,255,255,0.08), rgba(255,255,255,0));
  pointer-events: none;
}

.video-label {
  position: absolute;
  left: 10px;
  bottom: 10px;
  border-radius: 999px;
  padding: 5px 9px;
  color: #ffffff;
  background: rgba(0, 0, 0, 0.52);
  font-size: 12px;
  font-weight: 700;
  z-index: 2;
}

.preview-controls {
  position: absolute;
  right: 10px;
  bottom: 10px;
  z-index: 3;
  display: flex;
  gap: 8px;
}

.preview-control {
  width: 40px;
  height: 40px;
  min-height: 40px;
  display: grid;
  place-items: center;
  padding: 0;
  border: 1px solid rgba(255, 255, 255, 0.2);
  border-radius: 999px;
  color: #ffffff;
  background: rgba(0, 0, 0, 0.56);
  backdrop-filter: blur(10px);
}

.preview-control:hover:not(:disabled) {
  background: rgba(15, 143, 107, 0.92);
  transform: translateY(-1px);
}

.preview-control:disabled {
  opacity: 0.42;
}

.preview-control[aria-pressed="true"] {
  border-color: rgba(255, 255, 255, 0.3);
  background: var(--danger);
}

.preview-control svg {
  width: 21px;
  height: 21px;
  fill: none;
  stroke: currentColor;
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.local.audio-muted #muteAudioBtn::after,
.local.video-paused #pauseVideoBtn::after {
  content: "";
  position: absolute;
  width: 24px;
  height: 2px;
  border-radius: 999px;
  background: currentColor;
  transform: rotate(-42deg);
}

.local.video-paused video {
  filter: grayscale(1) brightness(0.42);
}

.video-empty {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  color: rgba(255, 255, 255, 0.72);
  font-size: 18px;
  font-weight: 700;
  text-align: center;
  pointer-events: none;
}

.video-box.has-video .video-empty {
  display: none;
}

.message {
  max-width: 86%;
  padding: 9px 11px;
  border-radius: 12px;
  line-height: 1.4;
  word-wrap: break-word;
  overflow-wrap: anywhere;
}

.message.mine {
  align-self: flex-end;
  background: var(--mine);
  border-bottom-right-radius: 4px;
}

.message.theirs {
  align-self: flex-start;
  background: var(--theirs);
  border-bottom-left-radius: 4px;
}

.message.system {
  align-self: center;
  max-width: 90%;
  color: var(--muted);
  background: transparent;
  padding: 4px 0;
  font-size: 14px;
  text-align: center;
}

.time {
  display: block;
  margin-top: 4px;
  color: var(--muted);
  font-size: 11px;
}

.composer {
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(0, 0.82fr) minmax(0, 0.82fr);
  grid-template-rows: 42px 42px;
  align-items: center;
  gap: 8px;
  height: auto;
  min-height: 0;
  max-height: none;
  padding: 10px 12px 12px;
  background: var(--panel-soft);
  border-top: 1px solid var(--line);
  overflow: hidden;
}

.composer.is-searching {
  grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
  grid-template-rows: 42px;
}

.composer.is-idle {
  grid-template-columns: 1fr;
  grid-template-rows: 42px;
}

.composer input {
  grid-column: 1 / 3;
  grid-row: 2;
  min-height: 42px;
}

.composer button {
  width: 100%;
  min-width: 0;
  padding: 0 10px;
}

#startStopBtn {
  grid-column: 1;
  grid-row: 1;
}

#nextBtn {
  grid-column: 2;
  grid-row: 1;
}

#reportBtn {
  grid-column: 3;
  grid-row: 1;
}

#sendBtn {
  grid-column: 3;
  grid-row: 2;
}

.composer.is-searching #nextBtn {
  grid-column: 2;
}

.composer.is-idle #startStopBtn {
  grid-column: 1;
}

button.danger {
  color: #ffffff;
  border-color: var(--danger);
  background: var(--danger);
}

button.danger:hover:not(:disabled) {
  color: #ffffff;
  border-color: var(--danger);
  background: #952727;
}

.typing-line {
  height: 26px;
  min-height: 26px;
  max-height: 26px;
  padding: 5px 14px 7px;
  color: var(--muted);
  background: transparent;
  font-size: 13px;
  font-style: italic;
}

@media (max-width: 720px) {
  body {
    position: fixed;
    inset: 0;
    width: 100%;
  }

  .chat-shell {
    grid-template-rows: minmax(0, 1fr);
  }

  .chat-shell.is-running {
    grid-template-rows: minmax(0, 1fr);
  }

  .topbar {
    display: grid;
    grid-template-columns: minmax(0, 1fr);
    align-items: center;
    gap: 8px;
    padding: max(8px, env(safe-area-inset-top)) 10px 8px;
  }

  .top-actions {
    display: grid;
    grid-template-columns: repeat(3, auto);
    justify-content: start;
    justify-items: start;
    gap: 5px;
  }

  .support-link {
    min-height: 30px;
    padding: 0 9px;
    font-size: 12px;
  }

  .theme-toggle {
    min-width: 92px;
    min-height: 30px;
  }

  h1 {
    font-size: 17px;
  }

  .brand p {
    overflow: hidden;
    max-width: 52vw;
    text-overflow: ellipsis;
    white-space: nowrap;
    font-size: 12px;
  }

  .status-pill {
    grid-column: auto;
    justify-self: start;
    min-width: 70px;
    padding: 5px 8px;
    font-size: 12px;
  }

  .setup {
    grid-template-columns: 1fr;
    align-content: center;
    gap: 8px;
    height: auto;
    min-height: 112px;
    max-height: none;
    padding: 8px 10px;
  }

  .setup label {
    gap: 4px;
    font-size: 12px;
  }

  .setup input,
  .setup select {
    min-height: 38px;
    padding: 8px 10px;
    font-size: 16px;
  }

  .stage {
    grid-template-columns: 1fr;
    grid-template-rows: minmax(360px, 58dvh) minmax(0, 1fr);
  }

  .chat-shell.is-running .stage {
    grid-template-rows: minmax(280px, 48dvh) minmax(0, 1fr);
  }

  .left-stage {
    grid-template-rows: auto auto minmax(0, 1fr);
  }

  .chat-shell.is-running .left-stage {
    grid-template-rows: auto minmax(0, 1fr);
  }

  .video-grid {
    position: relative;
    display: block;
    border-right: 0;
    border-bottom: 1px solid var(--line);
    padding: 8px;
  }

  .video-box.remote {
    width: 100%;
    height: 100%;
    border-radius: 9px;
  }

  .video-box.local {
    position: absolute;
    right: 14px;
    bottom: 14px;
    width: min(34vw, 132px);
    height: min(48vw, 176px);
    min-height: 118px;
    border-radius: 9px;
    box-shadow: 0 14px 34px rgba(0, 0, 0, 0.36);
    z-index: 5;
  }

  .video-empty {
    padding: 0 18px;
    font-size: 15px;
  }

  .video-box.local .video-empty {
    font-size: 12px;
  }

  .video-label {
    left: 8px;
    bottom: 8px;
    padding: 4px 8px;
    font-size: 11px;
  }

  .preview-controls {
    right: 7px;
    bottom: 7px;
    gap: 5px;
  }

  .preview-control {
    width: 32px;
    height: 32px;
    min-height: 32px;
  }

  .preview-control svg {
    width: 17px;
    height: 17px;
  }

  .chat-panel {
    border-left: 0;
    grid-template-rows: auto minmax(0, 1fr) auto;
  }

  .chat-panel.has-sponsor {
    grid-template-rows: auto auto minmax(0, 1fr) auto;
  }

  .chat-panel-head {
    padding: 9px 10px;
  }

  .sponsor-card {
    grid-template-columns: minmax(0, 1fr);
    margin: 8px 10px 0;
    padding: 8px 10px;
  }

  .sponsor-card img {
    display: none;
  }

  .sponsor-card em {
    display: none;
  }

  .shortcut-help summary {
    min-height: 30px;
    padding: 0 9px;
    font-size: 12px;
  }

  .shortcut-help div {
    width: min(280px, calc(100vw - 20px));
  }

  .log {
    padding: 10px;
    gap: 7px;
  }

  .message {
    max-width: 90%;
    padding: 8px 10px;
    font-size: 14px;
  }

  .composer {
    grid-template-columns: minmax(0, 1fr) minmax(0, 0.82fr) minmax(0, 0.82fr);
    grid-template-rows: 42px 42px;
    gap: 6px;
    height: auto;
    min-height: 0;
    max-height: none;
    padding: 8px 8px max(8px, env(safe-area-inset-bottom));
  }

  .composer input {
    grid-column: 1 / 3;
    grid-row: 2;
    min-height: 42px;
    padding: 8px 10px;
    font-size: 16px;
  }

  #startStopBtn {
    grid-column: 1;
    grid-row: 1;
    min-width: 0;
    width: 100%;
  }

  #nextBtn {
    grid-column: 2;
    grid-row: 1;
  }

  #reportBtn {
    grid-column: 3;
    grid-row: 1;
  }

  #sendBtn {
    grid-column: 3;
    grid-row: 2;
  }

  .composer.is-searching {
    grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
    grid-template-rows: 42px;
  }

  .composer.is-searching #startStopBtn {
    grid-column: 1;
  }

  .composer.is-searching #nextBtn {
    grid-column: 2;
    width: 100%;
  }

  .composer.is-idle {
    grid-template-columns: 1fr;
    grid-template-rows: 42px;
  }

  .composer.is-searching #nextBtn {
    min-width: 0;
  }

  .composer button {
    min-height: 40px;
    width: 100%;
    padding: 0 6px;
    font-size: 12px;
  }

  .typing-line {
    height: 22px;
    min-height: 22px;
    max-height: 22px;
    padding: 3px 10px 5px;
    font-size: 12px;
  }
}

@media (max-width: 420px) {
  .top-actions {
    grid-template-columns: auto;
  }

  .support-link {
    display: none;
  }

  .status-pill {
    grid-column: auto;
  }

  .stage {
    grid-template-rows: minmax(340px, 56dvh) minmax(0, 1fr);
  }

  .chat-shell.is-running .stage {
    grid-template-rows: minmax(260px, 46dvh) minmax(0, 1fr);
  }

  .video-box.local {
    width: 112px;
    height: 148px;
  }

  .composer.is-matched {
    grid-template-columns: minmax(0, 1fr) minmax(0, 0.78fr) minmax(0, 0.86fr);
  }
}
