:root {
  --bg: #0f0f10;
  --panel: #1c1c1f;
  --line: #2e2e33;
  --text: #ececf0;
  --muted: #9a9aa3;
  --accent: #ff3b3b;
  --bar-h: 52px;
  --gap: 4px;
  color-scheme: dark;
}

* { box-sizing: border-box; }

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

button {
  font: inherit;
  color: inherit;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 6px 12px;
  cursor: pointer;
}
button:hover { border-color: var(--muted); }
button.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
button.danger:hover { border-color: var(--accent); color: var(--accent); }

/* ---------- ヘッダー ---------- */
.bar {
  height: var(--bar-h);
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 0 16px;
  border-bottom: 1px solid var(--line);
  overflow-x: auto;   /* 画面が狭くて入りきらない分は横スクロール */
}
.bar > * { flex-shrink: 0; }
.bar h1 { font-size: 16px; margin: 0; white-space: nowrap; }
.layouts { display: flex; gap: 4px; }
.layouts button.active { background: var(--text); color: var(--bg); border-color: var(--text); }
.actions { margin-left: auto; display: flex; gap: 8px; }
.live-group { display: flex; align-items: center; gap: 6px; }
.live-setting { display: flex; align-items: center; gap: 6px; font-size: 13px; color: var(--muted); white-space: nowrap; }
.live-setting select {
  font: inherit;
  color: var(--text);
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 5px 6px;
}

/* ---------- グリッド(全体を 16:9 に保ち、画面に収まる最大サイズにする) ---------- */
.stage {
  height: calc(100dvh - var(--bar-h));
  display: grid;
  place-items: center;
  padding: 8px;
}
.grid {
  --cols: 3;
  --rows: 3;
  --ratio: 1.7778;   /* 全体の横÷縦。JS で列数・行数から設定 */
  width: min(100%, calc((100dvh - var(--bar-h) - 16px) * var(--ratio)));
  aspect-ratio: 16 / 9;
  display: grid;
  grid-template-columns: repeat(var(--cols), 1fr);
  grid-template-rows: repeat(var(--rows), 1fr);
  gap: var(--gap);
}

.cell {
  position: relative;
  min-width: 0;
  min-height: 0;
  background: #000;
  border-radius: 4px;
  overflow: hidden;
  container-type: size;
}
.cell iframe { position: absolute; inset: 0; width: 100%; height: 100%; border: 0; }

/* 空の枠 */
.cell .add {
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 4px;
  background: var(--panel);
  border: 1px dashed var(--line);
  border-radius: 4px;
  color: var(--muted);
}
.cell .add:hover { color: var(--text); border-color: var(--muted); }
.cell .add .plus { font-size: 28px; line-height: 1; }
.cell .add small { font-size: 11px; }
/* 枠が小さいときは説明文を省略 */
@container (max-height: 110px) {
  .cell .add small { display: none; }
  .cell .add .plus { font-size: 20px; }
}

/* 動画がある枠: マウスを乗せると右上に操作ボタン */
.cell .tools {
  position: absolute;
  top: 6px;
  right: 6px;
  display: flex;
  flex-wrap: wrap;
  justify-content: flex-end;
  max-width: calc(100% - 12px);
  gap: 4px;
  opacity: 0;
  transition: opacity .15s;
}
.cell:hover .tools { opacity: 1; }
.cell .tools button {
  padding: 2px 8px;
  font-size: 12px;
  background: rgba(0, 0, 0, .75);
}

/* ドラッグ中は iframe ではなく枠がドロップを受け取る */
body.dragging .cell iframe { pointer-events: none; }
body.dragging .cell { outline: 2px dashed var(--muted); outline-offset: -2px; }

/* ---------- ダイアログ ---------- */
dialog {
  width: min(560px, calc(100vw - 32px));
  background: var(--panel);
  color: var(--text);
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 20px;
}
dialog::backdrop { background: rgba(0, 0, 0, .6); }
dialog h2 { margin: 0 0 12px; font-size: 18px; }
.steps { padding-left: 20px; margin: 0 0 12px; line-height: 2; color: var(--muted); }
.steps button { color: var(--text); }
.url-row { display: flex; gap: 8px; }
.url-row input {
  flex: 1;
  min-width: 0;
  font: inherit;
  padding: 8px 10px;
  color: var(--text);
  background: var(--bg);
  border: 1px solid var(--line);
  border-radius: 6px;
}
.url-row input:focus { outline: 2px solid var(--accent); border-color: transparent; }
.error { color: var(--accent); font-size: 13px; margin: 8px 0 0; }
.hint { color: var(--muted); font-size: 12px; margin: 12px 0 0; }
.dialog-buttons { display: flex; justify-content: flex-end; gap: 8px; margin-top: 16px; }

/* 狭い画面ではヘッダーを詰める */
@media (max-width: 1000px) {
  .bar h1 { display: none; }
}
@media (max-width: 640px) {
  .bar { gap: 8px; padding: 0 8px; }
  .bar h1, #fullscreenBtn { display: none; }
  .live-setting { font-size: 12px; }
  .live-label { display: none; }
  .bar button { padding: 6px 8px; font-size: 13px; white-space: nowrap; }
}
