:root {
  --bg: #0f172a;
  --panel: #1e293b;
  --panel-alt: #334155;
  --text: #f8fafc;
  --text-muted: #94a3b8;
  --primary: #3b82f6;
  --primary-hover: #2563eb;
  --success: #10b981;
  --danger: #ef4444;
  --warning: #f59e0b;
  --border: #334155;
  --radius: 8px;
}

* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Hiragino Sans", sans-serif;
  background-color: var(--bg);
  color: var(--text);
  height: 100vh;
  overflow: hidden;
}

.hidden {
  display: none !important;
}

/* ボタン共通 */
.btn {
  border: none;
  border-radius: 6px;
  padding: 8px 14px;
  font-size: 0.9rem;
  font-weight: 600;
  cursor: pointer;
  color: #fff;
  background: var(--panel-alt);
  transition: opacity 0.15s, background 0.15s;
  display: inline-flex;
  align-items: center;
  gap: 4px;
}
.btn:hover { opacity: 0.9; }
.btn-primary { background: var(--primary); }
.btn-primary:hover { background: var(--primary-hover); }
.btn-secondary { background: var(--panel-alt); }
.btn-success { background: var(--success); }
.btn-danger { background: var(--danger); }
.btn-lg { padding: 14px 24px; font-size: 1.05rem; width: 100%; justify-content: center; }
.btn-sm { padding: 6px 10px; font-size: 0.82rem; }
.btn-xs { padding: 4px 8px; font-size: 0.78rem; }
.btn.active { outline: 2px solid var(--warning); background: var(--warning); color: #000; }

/* ランディング画面 */
.landing-view {
  height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
}
.landing-card {
  background: var(--panel);
  padding: 36px;
  border-radius: 14px;
  max-width: 460px;
  width: 100%;
  text-align: center;
  border: 1px solid var(--border);
  box-shadow: 0 20px 40px rgba(0, 0, 0, 0.4);
}
.landing-card h1 { font-size: 2rem; margin-bottom: 10px; }
.subtitle { color: var(--text-muted); font-size: 0.9rem; margin-bottom: 24px; line-height: 1.5; }
.divider { margin: 20px 0; color: var(--text-muted); font-size: 0.85rem; }
.join-form { display: flex; gap: 8px; }
.join-form input {
  flex: 1;
  padding: 10px 12px;
  border-radius: 6px;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text);
}

/* ルーム画面 */
.room-view {
  display: flex;
  flex-direction: column;
  height: 100vh;
}

.app-header {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 10px 16px;
  background: var(--panel);
  border-bottom: 1px solid var(--border);
}
.header-left, .header-right {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
}
.logo {
  font-weight: 800;
  font-size: 1.1rem;
  color: var(--text);
  text-decoration: none;
}
.room-badge {
  display: flex;
  align-items: center;
  gap: 6px;
  background: var(--bg);
  padding: 4px 8px;
  border-radius: 6px;
  font-family: monospace;
  font-size: 0.85rem;
}
.badge {
  background: var(--panel-alt);
  padding: 4px 8px;
  border-radius: 6px;
  font-size: 0.8rem;
}
.rtt-badge {
  background: var(--bg);
  padding: 4px 10px;
  border-radius: 6px;
  font-size: 0.82rem;
  font-family: monospace;
  border: 1px solid var(--border);
}
.rtt-good { color: #34d399; }
.rtt-mid { color: #fbbf24; }
.rtt-bad { color: #f87171; }

.username-box input {
  background: var(--bg);
  border: 1px solid var(--border);
  color: var(--text);
  padding: 5px 8px;
  border-radius: 6px;
  width: 130px;
  font-size: 0.85rem;
}

/* トグルスイッチ (低遅延モード) */
.toggle-switch {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  cursor: pointer;
  font-size: 0.82rem;
  background: var(--bg);
  padding: 4px 10px;
  border-radius: 20px;
  border: 1px solid var(--border);
}
.toggle-switch input { display: none; }
.toggle-switch .slider {
  width: 32px;
  height: 18px;
  background: var(--panel-alt);
  border-radius: 18px;
  position: relative;
  transition: background 0.2s;
}
.toggle-switch .slider::after {
  content: "";
  position: absolute;
  top: 2px;
  left: 2px;
  width: 14px;
  height: 14px;
  background: #fff;
  border-radius: 50%;
  transition: transform 0.2s;
}
.toggle-switch input:checked + .slider { background: var(--warning); }
.toggle-switch input:checked + .slider::after { transform: translateX(14px); }

/* メインレイアウト */
.main-layout {
  flex: 1;
  display: grid;
  grid-template-columns: 1.2fr 1fr;
  overflow: hidden;
}

/* 左ペイン */
.media-pane {
  display: flex;
  flex-direction: column;
  gap: 12px;
  padding: 12px;
  overflow-y: auto;
  border-right: 1px solid var(--border);
}
.panel {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 12px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}
.panel-header {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}
.panel-header h3 { font-size: 0.95rem; }

/* YouTube */
.youtube-form { display: flex; gap: 6px; flex: 1; max-width: 380px; }
.youtube-form input {
  flex: 1;
  padding: 5px 8px;
  border-radius: 6px;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text);
  font-size: 0.82rem;
}
.youtube-wrapper {
  position: relative;
  width: 100%;
  aspect-ratio: 16 / 9;
  background: #000;
  border-radius: 6px;
  overflow: hidden;
}
.youtube-wrapper iframe {
  width: 100%;
  height: 100%;
}
.sync-status {
  position: absolute;
  bottom: 6px;
  right: 8px;
  background: rgba(0, 0, 0, 0.7);
  color: #fff;
  font-size: 0.72rem;
  padding: 2px 6px;
  border-radius: 4px;
  pointer-events: none;
}

/* WebRTC 通話パネル */
.call-controls { display: flex; flex-wrap: wrap; gap: 6px; }
.device-selectors {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  align-items: center;
  font-size: 0.78rem;
  color: var(--text-muted);
}
.device-selectors select {
  background: var(--bg);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 4px;
  padding: 3px 6px;
  max-width: 180px;
  font-size: 0.78rem;
}
.video-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: 8px;
  min-height: 160px;
}
.video-placeholder {
  grid-column: 1 / -1;
  display: flex;
  align-items: center;
  justify-content: center;
  text-align: center;
  padding: 24px;
  color: var(--text-muted);
  font-size: 0.85rem;
  background: var(--bg);
  border-radius: 6px;
  border: 1px dashed var(--border);
}
.video-tile {
  position: relative;
  background: #000;
  border-radius: 6px;
  overflow: hidden;
  aspect-ratio: 16 / 9;
}
.video-tile video {
  width: 100%;
  height: 100%;
  object-fit: cover;
}
.video-label {
  position: absolute;
  bottom: 6px;
  left: 6px;
  background: rgba(0, 0, 0, 0.65);
  color: #fff;
  font-size: 0.75rem;
  padding: 2px 6px;
  border-radius: 4px;
}

/* 右ペイン：チャット */
.chat-pane {
  display: flex;
  flex-direction: column;
  height: 100%;
  overflow: hidden;
  background: var(--bg);
}
.chat-messages {
  flex: 1;
  overflow-y: auto;
  padding: 14px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}
.msg-item {
  max-width: 85%;
  background: var(--panel);
  padding: 8px 12px;
  border-radius: 10px;
  border: 1px solid var(--border);
  align-self: flex-start;
  word-break: break-word;
}
.msg-item.mine {
  align-self: flex-end;
  background: #1d4ed8;
  border-color: #2563eb;
}
.msg-meta {
  font-size: 0.72rem;
  color: var(--text-muted);
  margin-bottom: 4px;
  display: flex;
  justify-content: space-between;
  gap: 10px;
}
.msg-item.mine .msg-meta { color: #bfdbfe; }
.msg-text { font-size: 0.92rem; line-height: 1.45; white-space: pre-wrap; }
.msg-media img {
  max-width: 100%;
  max-height: 280px;
  border-radius: 6px;
  margin-top: 4px;
  display: block;
}
.msg-media video {
  max-width: 100%;
  max-height: 280px;
  border-radius: 6px;
  margin-top: 4px;
  display: block;
}
.msg-file-link {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  margin-top: 6px;
  color: #93c5fd;
  text-decoration: none;
  font-size: 0.85rem;
  background: rgba(0, 0, 0, 0.25);
  padding: 6px 10px;
  border-radius: 6px;
}
.msg-file-link:hover { text-decoration: underline; }

/* リアルタイムタイピングプレビュー */
.live-typing-area {
  padding: 6px 14px;
  background: rgba(245, 158, 11, 0.12);
  border-top: 1px solid rgba(245, 158, 11, 0.3);
  font-size: 0.82rem;
  color: #fcd34d;
  display: flex;
  flex-direction: column;
  gap: 2px;
}

/* アップロード進捗 */
.upload-progress-wrap {
  position: relative;
  background: var(--panel);
  height: 24px;
  border-top: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 0.75rem;
}
.upload-progress-bar {
  position: absolute;
  left: 0;
  top: 0;
  bottom: 0;
  background: var(--primary);
  width: 0%;
  transition: width 0.1s;
}
.upload-progress-wrap span { position: relative; z-index: 1; }

.chat-toolbar {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  padding: 8px 12px;
  background: var(--panel);
  border-top: 1px solid var(--border);
}
.chat-input-area {
  display: flex;
  gap: 8px;
  padding: 10px 12px;
  background: var(--panel);
  border-top: 1px solid var(--border);
}
.chat-input-area input {
  flex: 1;
  padding: 10px 12px;
  border-radius: 6px;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text);
  font-size: 0.95rem;
}

/* お絵描きモーダル */
.modal {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.75);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
  padding: 16px;
}
.modal-content {
  background: var(--panel);
  border-radius: 12px;
  padding: 16px;
  max-width: 650px;
  width: 100%;
  display: flex;
  flex-direction: column;
  gap: 12px;
  border: 1px solid var(--border);
}
.modal-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.draw-tools {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  align-items: center;
  font-size: 0.85rem;
}
#draw-canvas {
  background: #ffffff;
  border-radius: 6px;
  width: 100%;
  height: auto;
  touch-action: none;
  cursor: crosshair;
}
.modal-footer {
  display: flex;
  justify-content: flex-end;
}

/* モバイルレスポンシブ */
@media (max-width: 860px) {
  body { overflow: auto; height: auto; }
  .room-view { height: auto; min-height: 100vh; }
  .main-layout {
    grid-template-columns: 1fr;
    overflow: visible;
  }
  .media-pane { border-right: none; border-bottom: 1px solid var(--border); }
  .chat-pane { height: 70vh; }
}