:root {
  color-scheme: light;
  --bg: #ffffff;
  --panel: #f6f6f6;
  --panel-2: #f1f1f1;
  --panel-3: #e7e7e7;
  --text: #111111;
  --muted: #686868;
  --line: #dddddd;
  --line-strong: #bdbdbd;
  --sidebar-width: 248px;
  font-family: Inter, ui-sans-serif, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body { margin: 0; overflow: hidden; background: var(--bg); color: var(--text); }
button, textarea, input, select { font: inherit; }
button, a { color: inherit; -webkit-tap-highlight-color: transparent; }
button { cursor: pointer; }

.app-shell { height: 100%; display: grid; grid-template-columns: var(--sidebar-width) minmax(0,1fr); }
.sidebar { min-height: 0; padding: 14px 12px; display: flex; flex-direction: column; background: var(--panel); border-right: 1px solid var(--line); z-index: 30; }
.brand-row { min-height: 42px; padding: 2px 5px 12px; display: flex; align-items: center; gap: 10px; }
.brand-row strong { font-size: 14px; }
.brand-mark, .profile-avatar, .assistant-avatar { display: grid; place-items: center; border-radius: 50%; background: #171717; color: #fff; font-weight: 700; }
.brand-mark, .profile-avatar { width: 30px; height: 30px; border: 0; font-size: 12px; }
.icon-button { width: 36px; height: 36px; display: grid; place-items: center; border: 0; border-radius: 8px; background: transparent; font-size: 19px; }
.icon-button:hover { background: var(--panel-3); }
.sidebar-close { display: none; margin-left: auto; }
.new-story-button { width: 100%; min-height: 42px; padding: 0 12px; display: flex; align-items: center; gap: 9px; border: 1px solid var(--line-strong); border-radius: 8px; background: #fff; font-size: 13px; font-weight: 650; }
.new-story-button:hover { background: #ededed; }
.new-story-button span:first-child { font-size: 20px; font-weight: 300; }
.story-nav { margin-top: 22px; overflow: auto; }
.nav-label, .eyebrow { margin: 0 0 8px; color: var(--muted); font-size: 10px; font-weight: 750; letter-spacing: .08em; text-transform: uppercase; }
.nav-label { padding: 0 9px; }
.story-link { display: block; width: 100%; padding: 10px; border: 0; border-radius: 7px; background: transparent; text-align: left; }
.story-link:hover, .story-link.active { background: #e8e8e8; }
.story-link span { display: block; }
.story-link-title { overflow: hidden; font-size: 13px; text-overflow: ellipsis; white-space: nowrap; }
.story-link-meta, .sidebar-tool small, .profile-row small { margin-top: 3px; color: var(--muted); font-size: 10px; }
.story-list-row { display: flex; align-items: center; gap: 2px; }
.story-list-row .story-link { min-width: 0; flex: 1 1 auto; }
.story-delete-button { width: 27px; height: 27px; flex: 0 0 auto; border: 0; border-radius: 6px; background: transparent; color: var(--muted); font-size: 17px; line-height: 1; }
.story-delete-button:hover { background: #e8e8e8; color: #111; }
.sidebar-spacer { flex: 1; }
.sidebar-tool { padding: 9px; display: flex; align-items: center; gap: 9px; border: 0; border-radius: 7px; background: transparent; text-align: left; text-decoration: none; }
.sidebar-tool:hover { background: #e8e8e8; }
.sidebar-tool span:not(.tool-icon), .profile-row span:nth-child(2) { display: flex; flex-direction: column; }
.sidebar-tool strong, .profile-row strong { font-size: 12px; }
.tool-icon { width: 25px; color: #555; text-align: center; font-size: 17px; }
.profile-row { margin-top: 8px; padding: 13px 8px 3px; display: flex; align-items: center; gap: 9px; border-top: 1px solid var(--line); }
.status-dot { width: 7px; height: 7px; margin-left: auto; border-radius: 50%; background: #555; }
.sidebar-scrim { display: none; }

.main-panel { min-width: 0; min-height: 0; height: 100%; display: flex; flex-direction: column; position: relative; overflow: hidden; }
.topbar { min-height: 58px; padding: 0 22px; display: flex; align-items: center; justify-content: space-between; background: #fff; border-bottom: 1px solid var(--line); z-index: 10; }
.mobile-menu { display: none; }
.story-title-button { padding: 8px 4px; border: 0; background: transparent; font-size: 14px; font-weight: 700; }
.chevron { display: none; }
.topbar-actions { display: flex; align-items: center; gap: 8px; }
.mode-badge { min-height: 28px; padding: 0 10px; display: flex; align-items: center; gap: 7px; border: 1px solid var(--line); border-radius: 999px; color: var(--muted); font-size: 11px; }
.mode-badge span { width: 6px; height: 6px; border-radius: 50%; background: #777 !important; }
.subtle-button { padding: 7px 11px; border: 1px solid var(--line-strong); border-radius: 7px; background: #fff; font-size: 12px; }
.subtle-button:hover { background: #ededed; }

.conversation { flex: 1; min-height: 0; padding: 28px 24px 200px; overflow-y: auto; scroll-behavior: smooth; }
.message { width: min(760px,100%); margin: 0 auto 32px; animation: message-in .2s ease both; }
@keyframes message-in { from { opacity: 0; transform: translateY(4px); } }
.user-message { display: flex; justify-content: flex-end; }
.user-bubble { max-width: 76%; padding: 11px 15px; border-radius: 16px 16px 4px 16px; background: var(--panel-2); font-size: 14px; line-height: 1.5; white-space: pre-wrap; }
.assistant-message { display: grid; grid-template-columns: 32px minmax(0,1fr); gap: 13px; }
.assistant-avatar { width: 30px; height: 30px; font-size: 12px; }
.assistant-content { min-width: 0; }
.assistant-name { margin: 5px 0 9px; font-size: 12px; font-weight: 750; }
.story-copy { color: var(--text); font-size: 16px; line-height: 1.72; }
.story-copy p { margin: 0 0 1em; }
.story-copy p:last-child { margin-bottom: 0; }
.message-actions { margin-top: 13px; display: flex; flex-wrap: wrap; gap: 3px; }
.action-button { min-height: 30px; padding: 0 8px; display: flex; align-items: center; gap: 6px; border: 0; border-radius: 6px; background: transparent; color: var(--muted); font-size: 11px; }
.action-button:hover, .action-button.active { background: var(--panel-2); color: #111; }
.action-button .action-icon { font-size: 14px; }
.cue-chip { margin-top: 13px; padding: 6px 9px; display: inline-flex; align-items: center; gap: 6px; border: 1px solid var(--line); border-radius: 6px; background: #f7f7f7; color: #555; font-size: 10px; }
.cue-chip::before { content: ""; width: 5px; height: 5px; border-radius: 50%; background: #555; }
.typing { height: 31px; display: flex; align-items: center; gap: 5px; }
.typing span { width: 6px; height: 6px; border-radius: 50%; background: #777; animation: blink 1.2s infinite; }
.typing span:nth-child(2) { animation-delay: .16s; }
.typing span:nth-child(3) { animation-delay: .32s; }
@keyframes blink { 0%,70%,100% { opacity: .25; } 35% { opacity: 1; transform: translateY(-2px); } }
.image-card { margin-top: 17px; overflow: hidden; border: 1px solid var(--line); border-radius: 10px; background: #fff; }
.image-placeholder { min-height: 270px; padding: 30px; display: grid; place-items: center; background: #efefef; text-align: center; }
.image-placeholder div { max-width: 390px; }
.image-placeholder strong { display: block; margin-bottom: 8px; font-size: 16px; }
.image-placeholder p, .image-caption { color: var(--muted); font-size: 11px; line-height: 1.5; }
.image-caption { padding: 10px 12px; }

.composer-dock { position: absolute; left: 0; right: 0; bottom: 0; z-index: 8; padding: 20px 22px 10px; background: #fff; border-top: 1px solid #eee; }
.composer { width: min(790px,100%); min-height: 100px; margin: 0 auto; padding: 12px; border: 1px solid var(--line-strong); border-radius: 16px; background: #fff; box-shadow: 0 5px 20px rgba(0,0,0,.05); }
.composer:focus-within { border-color: #777; }
.composer textarea { width: 100%; max-height: 180px; padding: 3px 5px 10px; resize: none; border: 0; outline: 0; background: transparent; color: var(--text); line-height: 1.45; }
.composer textarea::placeholder { color: #777; }
.composer-footer { display: flex; align-items: end; justify-content: space-between; gap: 12px; }
.composer-tools { display: flex; align-items: center; flex-wrap: wrap; gap: 4px; }
.round-tool, .composer-mode, .voice-picker { height: 31px; border: 1px solid var(--line); background: #fff; }
.round-tool { width: 31px; border-radius: 50%; font-size: 18px; }
.composer-mode, .voice-picker { border-radius: 999px; color: #555; font-size: 11px; }
.composer-mode { padding: 0 9px; }
.voice-picker { padding: 0 5px 0 9px; display: flex; align-items: center; gap: 4px; }
.voice-picker select { max-width: 128px; border: 0; outline: 0; background: transparent; color: #444; font-size: 11px; }
.voice-picker select option { background: #fff; color: #111; }
.round-tool:hover, .composer-mode:hover, .voice-picker:hover { background: #ededed; }
.send-button { flex: 0 0 auto; width: 34px; height: 34px; border: 0; border-radius: 50%; background: #171717; color: #fff; font-size: 20px; font-weight: 700; }
.send-button:disabled { opacity: .3; cursor: default; }
.composer-note { width: min(790px,100%); margin: 7px auto 0; color: #777; text-align: center; font-size: 9px; }

.canon-page { height: 100%; padding: 0 24px 88px; overflow-y: auto; background: #fff; }
.canon-page[hidden] { display: none; }
body.canon-view .app-shell { display: none; }
.drawer-header, .drawer-intro, .canon-card, .canon-import, .canon-footer { width: min(960px,100%); margin-left: auto; margin-right: auto; }
.drawer-header { position: sticky; top: 0; z-index: 3; min-height: 78px; padding: 18px 0 12px; display: flex; align-items: flex-start; justify-content: space-between; background: rgba(255,255,255,.96); border-bottom: 1px solid var(--line); }
.drawer-header h2 { margin: 0; font-size: 26px; letter-spacing: -.03em; }
.drawer-header .eyebrow { margin-bottom: 3px; }
.drawer-intro { margin-top: 22px; margin-bottom: 18px; color: var(--muted); font-size: 13px; line-height: 1.55; }
.canon-card { margin-bottom: 12px; padding: 18px; border: 1px solid var(--line); border-radius: 10px; background: #fff; }
.canon-health > div:first-child { display: flex; align-items: center; gap: 12px; }
.canon-health span:not(.canon-score) { display: flex; flex-direction: column; font-size: 13px; }
.canon-health small { margin-top: 2px; color: var(--muted); }
.canon-score { width: 48px; height: 48px; display: grid; place-items: center; border-radius: 50%; background: #e8e8e8; color: #111; font-size: 12px; font-weight: 750; }
.progress-track { height: 4px; margin-top: 13px; overflow: hidden; border-radius: 99px; background: #e5e5e5; }
.progress-track span { display: block; width: 12%; height: 100%; background: #333; transition: width .2s ease; }
.card-heading { margin-bottom: 12px; display: flex; align-items: flex-start; justify-content: space-between; }
.card-heading h3 { margin: 0; font-size: 16px; }
.card-heading .eyebrow { margin-bottom: 3px; }
.fact-status { padding: 4px 7px; border-radius: 999px; background: #ededed; color: #555; font-size: 9px; }
.fact-status.approved { background: #222; color: #fff; }
.canon-gallery { display: grid; grid-template-columns: repeat(3,minmax(0,1fr)); gap: 8px; }
.canon-image { position: relative; min-height: 145px; overflow: hidden; border: 1px solid var(--line); border-radius: 7px; background: #eee; color: #fff; }
.canon-image:first-child { grid-column: span 2; }
.canon-image img { width: 100%; height: 100%; min-height: inherit; display: block; object-fit: cover; }
.canon-image span { position: absolute; inset: auto 0 0; padding: 18px 9px 8px; background: linear-gradient(transparent,rgba(0,0,0,.8)); font-size: 9px; }
.canon-helper, .canon-route-status, .canon-import small { color: var(--muted); font-size: 10px; line-height: 1.45; }
.canon-helper { margin: 9px 1px 0; }
.canon-teach label { margin-top: 0; }
.canon-add-button { width: 100%; margin-top: 9px; }
.canon-route-status { min-height: 28px; margin: 8px 1px 0; }
.canon-route-status.success { color: #111; font-weight: 650; }
.canon-theme { margin: 0 0 10px; color: #444; font-size: 11px; line-height: 1.5; }
.canon-rule-list { margin: 0; padding-left: 17px; color: #555; font-size: 10px; line-height: 1.5; }
.canon-rule-list li + li { margin-top: 5px; }
.canon-card label { display: block; margin-top: 11px; color: #555; font-size: 11px; }
.canon-card textarea { width: 100%; margin-top: 6px; padding: 10px; resize: vertical; border: 1px solid var(--line); border-radius: 7px; outline: none; background: #fafafa; color: var(--text); font-size: 12px; line-height: 1.45; }
.canon-card textarea:focus { border-color: #777; background: #fff; }
.canon-import { padding: 17px; border: 1px dashed var(--line-strong); border-radius: 10px; text-align: center; }
.canon-import small { display: block; margin-top: 9px; }
.primary-button { padding: 10px 14px; border: 1px solid #111; border-radius: 7px; background: #111; color: #fff; font-size: 11px; font-weight: 700; }
.primary-button:hover { background: #333; }
.primary-button.compact { padding: 8px 13px; }
.canon-footer { position: sticky; bottom: 0; z-index: 3; margin-top: 14px; padding: 13px 0; display: flex; align-items: center; justify-content: space-between; background: rgba(255,255,255,.96); border-top: 1px solid var(--line); color: var(--muted); font-size: 10px; }
.toast { position: fixed; left: 50%; bottom: 28px; z-index: 80; padding: 10px 14px; opacity: 0; visibility: hidden; transform: translate(-50%,14px); border-radius: 7px; background: #111; color: #fff; box-shadow: 0 8px 28px rgba(0,0,0,.2); font-size: 11px; transition: .18s ease; }
.toast.show { opacity: 1; visibility: visible; transform: translate(-50%,0); }
.simulator-bridge { position: absolute; width: 1px; height: 1px; border: 0; opacity: 0; pointer-events: none; }
.simulator-state { margin-top: 13px; border: 1px solid var(--line); border-radius: 8px; background: #fafafa; }
.simulator-state summary { padding: 9px 10px; cursor: pointer; color: #333; font-size: 11px; font-weight: 650; }
.simulator-grid { display: grid; grid-template-columns: repeat(3,minmax(0,1fr)); gap: 1px; border-top: 1px solid var(--line); background: var(--line); }
.simulator-grid span { min-width: 0; padding: 7px 8px; background: #fff; color: var(--muted); font-size: 9px; line-height: 1.35; }
.simulator-grid strong { display: block; overflow: hidden; color: var(--text); font-size: 10px; font-weight: 650; text-overflow: ellipsis; white-space: nowrap; }
.operation-card { margin-top: 10px; padding: 10px; display: flex; align-items: center; justify-content: space-between; gap: 10px; border: 1px solid #111; border-radius: 8px; background: #fff; }
.operation-card span { display: block; color: var(--muted); font-size: 10px; }
.operation-card strong { display: block; margin-top: 2px; font-size: 12px; }
.operation-card .primary-button { flex: 0 0 auto; }
.operation-card.running { border-color: var(--line-strong); background: #fafafa; }
.operation-card.running::after { content: ""; width: 7px; height: 7px; flex: 0 0 auto; border-radius: 50%; background: #333; animation: blink 1.2s infinite; }
.diagnostics-dialog { width: min(680px,calc(100vw - 28px)); max-height: min(74vh,720px); padding: 18px; border: 1px solid #111; border-radius: 10px; background: #fff; color: var(--text); box-shadow: 0 18px 60px rgba(0,0,0,.24); }
.diagnostics-dialog::backdrop { background: rgba(0,0,0,.24); }
.diagnostics-header { display: flex; align-items: flex-start; justify-content: space-between; gap: 16px; }
.diagnostics-header h2 { margin: 0; font-size: 19px; }
.diagnostics-header .eyebrow { margin-bottom: 3px; }
.diagnostics-intro { margin: 14px 0 10px; color: var(--muted); font-size: 11px; line-height: 1.5; }
.diagnostics-output { min-height: 180px; max-height: 42vh; margin: 0; padding: 11px; overflow: auto; border: 1px solid var(--line); border-radius: 7px; background: #f7f7f7; color: #222; font: 11px/1.55 ui-monospace, SFMono-Regular, Consolas, monospace; white-space: pre-wrap; }
.diagnostics-actions { margin-top: 12px; display: flex; justify-content: flex-end; gap: 7px; }
.new-story-dialog { width: min(620px,calc(100vw - 28px)); padding: 18px; border: 1px solid #111; border-radius: 10px; background: #fff; color: var(--text); box-shadow: 0 18px 60px rgba(0,0,0,.24); }
.new-story-dialog::backdrop { background: rgba(0,0,0,.24); }
.new-story-input { width: 100%; padding: 10px; resize: vertical; border: 1px solid var(--line-strong); border-radius: 7px; outline: 0; background: #fafafa; color: var(--text); font: inherit; font-size: 13px; line-height: 1.45; }
.new-story-input:focus { border-color: #333; background: #fff; }

body.embedded-story { overflow: hidden; }
body.embedded-story .app-shell { grid-template-columns: minmax(0,1fr); }
body.embedded-story .sidebar { display: none; }
body.embedded-story .sidebar.open { display: flex; }
body.embedded-story .topbar { min-height: 48px; padding-inline: 14px; }
body.embedded-story .conversation { padding: 18px 15px 184px; }
body.embedded-story .composer-dock { padding: 12px 12px 8px; }
body.embedded-story .canon-page { padding-inline: 14px; }

@media (max-width: 820px) {
  .app-shell { display: block; }
  .sidebar { position: fixed; inset: 0 auto 0 0; width: min(300px,88vw); transform: translateX(-103%); transition: transform .2s ease; }
  .sidebar.open { transform: translateX(0); }
  .sidebar-close, .mobile-menu { display: grid; }
  .sidebar-scrim { position: fixed; inset: 0; z-index: 25; display: block; opacity: 0; visibility: hidden; background: rgba(0,0,0,.25); transition: .18s; }
  .sidebar-scrim.open { opacity: 1; visibility: visible; }
  .topbar { padding: 0 11px; }
  .story-title-button { margin-right: auto; }
  .mode-badge { display: none; }
  .conversation { padding-inline: 15px; }
  .canon-page { padding-inline: 15px; }
  .canon-gallery { grid-template-columns: repeat(2,minmax(0,1fr)); }

  /* In the simulator sidebar, the story menu is a self-contained drawer.
     Do not put a grey scrim across the working simulator just to change story. */
  body.embedded-story .sidebar.open { display: flex; transform: translateX(0); z-index: 40; }
  body.embedded-story .sidebar-scrim.open { display: none; }
}

@media (max-width: 520px) {
  .topbar .subtle-button { display: inline-flex; }
  .story-title-button { max-width: 52vw; overflow: hidden; font-size: 13px; text-overflow: ellipsis; white-space: nowrap; }
  .conversation { padding-top: 18px; padding-bottom: 190px; }
  .message { margin-bottom: 25px; }
  .assistant-message { grid-template-columns: 28px minmax(0,1fr); gap: 10px; }
  .assistant-avatar { width: 27px; height: 27px; }
  .story-copy { font-size: 15px; line-height: 1.67; }
  .user-bubble { max-width: 88%; }
  .composer-dock { padding-inline: 10px; }
  .composer-mode:nth-child(2) { display: none; }
  .voice-picker select { max-width: 80px; }
  .image-placeholder { min-height: 210px; }
  .simulator-grid { grid-template-columns: repeat(2,minmax(0,1fr)); }
  .canon-page { padding-inline: 11px; }
  .drawer-header { min-height: 70px; }
  .canon-card { padding: 14px; }
  .canon-gallery { grid-template-columns: 1fr; }
  .canon-image:first-child { grid-column: auto; }
}

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