@import url('https://fonts.googleapis.com/css2?family=Poppins:wght@600;700;800&family=Noto+Sans+JP:wght@400;500;700;800&display=swap');

:root {
  --bg: #0d0f12;
  --panel: #15181d;
  --panel-2: #1d2229;
  --fg: #f7f7f3;
  --muted: #a2a9b3;
  --faint: #6c7480;
  --line: rgba(255,255,255,.12);
  --blue: #2f6df6;
  --green: #28b07c;
  --danger: #e05b5b;
  --shadow: 0 18px 60px rgba(0,0,0,.42);
  --font: "Noto Sans JP", system-ui, sans-serif;
  --display: "Poppins", "Noto Sans JP", sans-serif;
  --duration-fast: 150ms;
  --duration-base: 200ms;
  --duration-slow: 420ms;
  --ease-standard: cubic-bezier(0.2, 0, 0, 1);
  --ease-emphasized: cubic-bezier(0.2, 0.8, 0.2, 1);
  --cp-w: 360px;
}

* { box-sizing: border-box; }
html, body { margin: 0; width: 100%; height: 100%; overflow: hidden; }
body {
  font-family: var(--font);
  color: var(--fg);
  background:
    linear-gradient(135deg, rgba(47,109,246,.08), transparent 32%),
    linear-gradient(180deg, #111418, var(--bg));
}

button, select, textarea, input { font: inherit; }
button {
  height: 32px; border: 1px solid var(--line); border-radius: 6px;
  background: transparent; color: var(--fg); cursor: pointer;
  transition:
    background var(--duration-fast) var(--ease-standard),
    border-color var(--duration-fast) var(--ease-standard),
    color var(--duration-fast) var(--ease-standard),
    opacity var(--duration-fast) var(--ease-standard);
}
button:hover { background: rgba(255,255,255,.08); }
button.primary { background: var(--fg); color: #111418; border-color: var(--fg); font-weight: 800; }
button.primary:hover { background: #fff; }
button.is-active { background: var(--blue); border-color: var(--blue); }
button.ghost.is-active { background: rgba(47,109,246,.18); border-color: rgba(47,109,246,.6); color: #cbdcff; }

/* ── shell / grid (canvas + copilot) ── */
.studio-shell {
  height: 100vh;
  display: grid;
  grid-template-columns: minmax(0, 1fr) var(--cp-w);
  grid-template-rows: 50px 1fr;
  transition: grid-template-columns .18s ease;
}
.studio-shell.copilot-hidden { --cp-w: 0px; }
.studio-shell.copilot-hidden .ai-panel { display: none; }

/* ── topbar ── */
.studio-topbar {
  grid-column: 1 / -1; grid-row: 1;
  display: grid; grid-template-columns: auto minmax(0, 1fr) auto; align-items: center; gap: 14px;
  padding: 0 12px 0 14px;
  border-bottom: 1px solid var(--line);
  background: rgba(13,15,18,.92); backdrop-filter: blur(14px); z-index: 20;
}
.brand { display: flex; align-items: center; gap: 10px; }
.brand a { text-decoration: none; color: inherit; }
.brand-back { font-weight: 800; opacity: .65; }
.brand-back:hover { opacity: 1; }
.brand-mark { display:inline-flex; width:70px; height:18px; flex:0 0 70px; }
.brand-mark img { display:block; width:100%; height:100%; }
.brand-name { color: var(--muted); font-size: 13px; font-weight: 700; }

.deck-bar { display: flex; align-items: center; gap: 8px; min-width: 0; }
.deck-bar label { color: var(--muted); font-size: 12px; font-weight: 700; }
.deck-bar select {
  flex: 0 1 420px; width: auto; min-width: 120px; height: 32px;
  border: 1px solid var(--line); border-radius: 6px;
  background: var(--panel); color: var(--fg); padding: 0 10px;
}
.slide-nav { display: flex; align-items: center; gap: 4px; margin-left: 4px; }
.slide-nav button { width: 28px; padding: 0; font-size: 16px; line-height: 1; }
#slide-count { color: var(--muted); font-size: 12px; font-weight: 700; min-width: 52px; text-align: center; font-variant-numeric: tabular-nums; }

.top-actions { display: flex; align-items: center; gap: 8px; flex-wrap: nowrap; }
.top-actions button { padding: 0 12px; white-space: nowrap; flex: 0 0 auto; }

/* Preview / Edit はモード切替なのでセグメント化して1本のバーに収める */
.mode-group { display: flex; flex: 0 0 auto; }
.mode-group button { border-radius: 0; }
.mode-group button:first-child { border-radius: 6px 0 0 6px; }
.mode-group button + button { border-radius: 0 6px 6px 0; margin-left: -1px; }
.mode-group button.is-active { position: relative; z-index: 1; }

/* ── references dropdown ── */
.ref-menu { position: relative; }
#btn-references[aria-expanded="true"] { background: rgba(47,109,246,.18); border-color: rgba(47,109,246,.6); color: #cbdcff; }
.ref-dropdown {
  position: absolute; top: calc(100% + 8px); right: 0; z-index: 60;
  width: 320px; max-height: min(72vh, 560px); overflow-y: auto;
  padding: 10px; border: 1px solid var(--line); border-radius: 10px;
  background: rgba(21,24,29,.98); backdrop-filter: blur(14px); box-shadow: var(--shadow);
  opacity: 1;
  transform: translateY(0) scale(.999);
  transform-origin: top right;
  transition:
    opacity var(--duration-base) var(--ease-standard),
    transform var(--duration-base) var(--ease-standard),
    display var(--duration-base) allow-discrete;
}
.ref-dropdown[hidden] {
  display: none;
  opacity: 0;
  transform: translateY(-4px) scale(.97);
}
@starting-style {
  .ref-dropdown:not([hidden]) {
    opacity: 0;
    transform: translateY(-4px) scale(.97);
  }
}
.ref-lead { margin: 2px 4px 10px; font-size: 11px; font-weight: 800; color: var(--muted); letter-spacing: .02em; }
.ref-group { display: flex; align-items: baseline; gap: 8px; flex-wrap: wrap; padding: 10px 6px 4px; }
.ref-group:first-of-type { padding-top: 2px; }
.ref-group-title { font-family: var(--display); font-size: 12px; font-weight: 800; color: var(--fg); }
.ref-group-hint { font-size: 10px; color: var(--faint); }
.ref-item {
  display: flex; align-items: baseline; justify-content: space-between; gap: 10px;
  padding: 7px 8px; border-radius: 7px; text-decoration: none; color: var(--fg);
  transition: background .12s;
}
.ref-item:hover { background: rgba(47,109,246,.14); }
.ref-name { font-size: 12px; font-weight: 700; white-space: nowrap; }
.ref-name::after { content: " ↗"; color: var(--faint); font-weight: 400; }
.ref-note { font-size: 10px; color: var(--muted); text-align: right; line-height: 1.4; }

/* ── canvas ── */
.canvas-area {
  grid-column: 1; grid-row: 2; min-width: 0; min-height: 0;
  background: #08090b; position: relative;
}
.canvas-area::before {
  content: ""; position: absolute; inset: 0; pointer-events: none;
  background-image:
    linear-gradient(rgba(255,255,255,.035) 1px, transparent 1px),
    linear-gradient(90deg, rgba(255,255,255,.035) 1px, transparent 1px);
  background-size: 24px 24px;
}
#deck-frame { position: relative; z-index: 1; width: 100%; height: 100%; border: 0; display: block; }

/* Present: デッキ単体の is-presenting と同様に iframe を画面全体へ */
.studio-shell.is-presenting {
  grid-template-columns: 1fr;
  grid-template-rows: 1fr;
  height: 100vh;
  height: 100dvh;
}
.studio-shell.is-presenting .studio-topbar,
.studio-shell.is-presenting .ai-panel { display: none !important; }
.studio-shell.is-presenting .canvas-area {
  grid-column: 1 / -1;
  grid-row: 1 / -1;
  min-height: 0;
  background: #000;
}
.studio-shell.is-presenting .canvas-area::before { display: none; }
body.studio-present-lock {
  overflow: hidden;
  background: #000;
}

/* ── copilot ── */
.ai-panel {
  grid-column: 2; grid-row: 2; min-width: 0; min-height: 0;
  border-left: 1px solid var(--line); background: rgba(21,24,29,.96);
  display: flex; flex-direction: column; gap: 14px; padding: 16px;
}
.ai-head { display: flex; justify-content: space-between; gap: 12px; }
.eyebrow { color: var(--green); font-size: 11px; font-weight: 800; letter-spacing: .14em; text-transform: uppercase; }
.ai-head h1 { margin: 4px 0 0; font-family: var(--display); font-size: 20px; line-height: 1.25; }
.context-pill { flex: 0 0 auto; height: 26px; padding: 4px 9px; border: 1px solid var(--line); border-radius: 100px; color: var(--muted); font-size: 11px; font-weight: 800; }

.quick-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.quick-grid button { height: 36px; color: var(--muted); background: var(--panel-2); }

.chat-log { flex: 1; min-height: 0; overflow-y: auto; display: flex; flex-direction: column; gap: 10px; padding-right: 3px; }
.msg { border: 1px solid var(--line); border-radius: 8px; padding: 10px 11px; line-height: 1.65; font-size: 12px; white-space: pre-wrap; word-break: break-word; }
.msg.user { background: rgba(47,109,246,.12); border-color: rgba(47,109,246,.32); }
.msg.assistant { background: rgba(255,255,255,.05); }
.msg.setup { border-color: rgba(224,162,58,.45); background: rgba(224,162,58,.08); }
.msg.error { border-color: rgba(224,91,91,.5); background: rgba(224,91,91,.1); }
.msg.thinking { color: var(--muted); }
.msg .role { display: block; margin-bottom: 5px; font-size: 10px; font-weight: 800; letter-spacing: .12em; text-transform: uppercase; color: var(--muted); }

.chat-form { display: flex; flex-direction: column; gap: 9px; }
.chat-form textarea {
  width: 100%; min-height: 92px; resize: none;
  border: 1px solid var(--line); border-radius: 8px;
  background: #0f1216; color: var(--fg); padding: 11px 12px; line-height: 1.6; outline: none;
}
.chat-form textarea:focus { border-color: rgba(47,109,246,.72); box-shadow: 0 0 0 2px rgba(47,109,246,.18); }
.form-actions { display: flex; justify-content: flex-end; gap: 8px; }
.form-actions button { padding: 0 14px; }
.form-actions button[disabled] { opacity: .5; cursor: default; }

/* ── toast ── */
.toast {
  position: fixed; left: 50%; bottom: 26px; transform: translateX(-50%) translateY(14px);
  background: #fff; color: #111418; font-weight: 800; font-size: 13px;
  padding: 10px 18px; border-radius: 100px; box-shadow: var(--shadow);
  opacity: 0; pointer-events: none; transition: opacity .16s, transform .16s; z-index: 80;
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }
.toast.ok { background: #1f9d55; color: #fff; }
.toast.err { background: var(--danger); color: #fff; }

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
    scroll-behavior: auto !important;
  }
}

/* 中間幅（〜1366px）: ラベルと余白を絞って Save までを常に見せる */
@media (max-width: 1366px) {
  .studio-topbar { gap: 10px; }
  .top-actions { gap: 6px; }
  .top-actions button { padding: 0 9px; }
  .deck-bar label { display: none; }
}
@media (max-width: 1100px) { :root { --cp-w: 300px; } .brand-name { display: none; } }

@media (max-width: 760px) {
  html, body { height: auto; min-height: 100%; overflow: auto; }
  body { text-wrap: pretty; word-break: auto-phrase; }
  button { min-height: 40px; }
  .studio-shell {
    min-height: 100dvh;
    height: auto;
    grid-template-columns: minmax(0, 1fr);
    grid-template-rows: auto minmax(360px, 62dvh) auto;
  }
  .studio-shell.copilot-hidden { --cp-w: 0px; }
  .studio-topbar {
    grid-column: 1;
    grid-row: 1;
    grid-template-columns: minmax(0, 1fr);
    align-items: stretch;
    gap: 10px;
    padding: 10px 12px 12px;
    position: sticky;
    top: 0;
  }
  .brand { min-width: 0; }
  .brand-mark { width:62px; height:16px; flex-basis:62px; }
  .brand-name { min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
  .deck-bar { flex-wrap: wrap; gap: 8px; }
  .deck-bar label { flex: 0 0 auto; }
  .deck-bar select { flex: 1 1 220px; width: auto; min-width: 0; height: 40px; }
  .slide-nav { margin-left: 0; }
  .slide-nav button { width: 40px; }
  #slide-count { min-width: 58px; }
  .top-actions {
    gap: 6px;
    overflow-x: auto;
    padding-bottom: 2px;
    scrollbar-width: none;
  }
  .top-actions::-webkit-scrollbar { display: none; }
  .top-actions button { flex: 0 0 auto; padding: 0 11px; }
  .ref-dropdown {
    position: fixed;
    left: 12px;
    right: 12px;
    top: 132px;
    width: auto;
    max-height: min(64dvh, 520px);
  }
  .canvas-area {
    grid-column: 1;
    grid-row: 2;
    min-height: 360px;
  }
  .ai-panel {
    grid-column: 1;
    grid-row: 3;
    min-height: 0;
    border-left: 0;
    border-top: 1px solid var(--line);
    padding: 14px 12px 18px;
  }
  .ai-head h1 { font-size: 18px; }
  .quick-grid { grid-template-columns: 1fr; }
  .quick-grid button { height: 40px; }
  .chat-log { flex: 0 1 auto; max-height: 44dvh; }
  .chat-form textarea { min-height: 108px; }
  .form-actions button { flex: 1 1 0; }
}

@media (max-width: 360px) {
  .studio-shell { grid-template-rows: auto minmax(320px, 58dvh) auto; }
  .canvas-area { min-height: 320px; }
  .deck-bar select { flex-basis: 100%; }
  .top-actions button { padding: 0 9px; }
}


/* デッキ iframe 読み込み中（初回の黒画面＋1/1 を「壊れた」に見せない） */
.canvas-area.is-loading {
  position: relative;
}
.canvas-area.is-loading::after {
  content: '読み込み中…';
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(11, 12, 14, 0.72);
  color: rgba(255, 255, 255, 0.78);
  font-weight: 800;
  font-size: 0.95rem;
  letter-spacing: 0.04em;
  pointer-events: none;
  z-index: 5;
}
.canvas-area.is-loading #deck-frame {
  opacity: 0.35;
}
