@import url("https://fonts.googleapis.com/css2?family=Rubik:wght@400;500;700&display=swap");

:root {
  --paper: #f9f9fb;       /* Scratch workspace */
  --dots: #dde1ea;
  --ink: #575e75;         /* Scratch's own text colour */
  --ink-strong: #3b4155;
  --line: #d0d4dd;
  --events: #ffbf00;
  --events-edge: #cc9900;
  --events-text: #4a3500;
  --motion: #4c97ff;
  --motion-edge: #3373cc;
  --danger: #e5484d;
  --white: #ffffff;
  --radius-input: 14px;
  font-family: "Rubik", "Helvetica Neue", Arial, sans-serif;
  color: var(--ink);
  background: var(--paper);
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  background-color: var(--paper);
  background-image: radial-gradient(var(--dots) 1.2px, transparent 1.2px);
  background-size: 24px 24px;
}
button, textarea, input { font: inherit; color: inherit; }
:focus-visible { outline: 3px solid var(--motion); outline-offset: 2px; }

#app {
  display: flex;
  flex-direction: column;
  height: 100%;
  max-width: 780px;
  margin: 0 auto;
  padding: env(safe-area-inset-top, 0) 16px env(safe-area-inset-bottom, 0);
}
.is-panel #app { padding: 0 12px; }

/* Header */
.top {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 16px 0 12px;
}
.brand { display: flex; align-items: center; gap: 10px; margin-right: auto; min-width: 0; }
.brand svg { flex: none; }
.brand h1 { margin: 0; font-size: 1.35rem; font-weight: 700; color: var(--ink-strong); letter-spacing: -0.01em; }
.brand p { margin: 2px 0 0; font-size: 0.85rem; }
.is-panel .brand p { display: none; }
.is-panel .top { gap: 6px; }
.is-panel .brand { gap: 8px; }
.is-panel .brand h1 { font-size: 1.1rem; }
.is-panel .ghost { padding: 5px 10px; font-size: 0.8rem; }
.is-panel .icon-btn { padding: 5px 7px; }

.ghost {
  border: 1px solid var(--line);
  background: var(--white);
  border-radius: 999px;
  padding: 6px 12px;
  font-size: 0.85rem;
  cursor: pointer;
  white-space: nowrap;
}
.ghost:hover { border-color: var(--ink); }
.icon-btn { padding: 6px 8px; line-height: 0; }

/* Settings (extension only) */
.settings {
  background: var(--white);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 12px;
  margin-bottom: 12px;
}
.settings[hidden] { display: none; }
.settings label { display: block; font-weight: 500; color: var(--ink-strong); margin-bottom: 6px; }
.settings .row { display: flex; gap: 8px; }
.settings input[type="url"] {
  flex: 1;
  min-width: 0;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 8px 10px;
  background: var(--paper);
}
.settings small { display: block; margin-top: 6px; font-size: 0.8rem; }
.settings .note { color: var(--danger); }

/* Modes */
.modes { display: flex; gap: 6px; padding-bottom: 10px; }
.modes button {
  border: 1px solid var(--line);
  background: var(--white);
  border-radius: 999px;
  padding: 5px 14px;
  cursor: pointer;
  font-size: 0.9rem;
}
.modes button[aria-pressed="true"] {
  background: var(--motion);
  border-color: var(--motion-edge);
  color: var(--white);
  font-weight: 500;
}

/* Thread */
.thread {
  flex: 1;
  overflow-y: auto;
  padding: 8px 2px 24px;
  scroll-behavior: smooth;
}
.empty { padding: 24px 0; }
.empty h2 { margin: 0 0 6px; font-size: 1.1rem; font-weight: 500; color: var(--ink-strong); }
.empty p { margin: 0 0 14px; max-width: 52ch; line-height: 1.5; }
.examples { display: flex; flex-direction: column; align-items: flex-start; gap: 8px; }
.examples button {
  text-align: left;
  border: 1px solid var(--line);
  background: var(--white);
  border-radius: var(--radius-input);
  padding: 8px 14px;
  cursor: pointer;
  line-height: 1.35;
}
.examples button:hover { border-color: var(--motion); }

.msg { margin: 0 0 18px; }
.msg.user { display: flex; justify-content: flex-end; }
.bubble {
  position: relative;
  max-width: 85%;
  background: var(--white);
  border: 2px solid var(--line);
  border-radius: 16px;
  padding: 10px 14px;
  white-space: pre-wrap;
  word-break: break-word;
  line-height: 1.45;
  color: var(--ink-strong);
}
.bubble::after { /* speech-bubble tail, like Scratch's "say" */
  content: "";
  position: absolute;
  right: 22px;
  bottom: -9px;
  width: 14px;
  height: 14px;
  background: var(--white);
  border-right: 2px solid var(--line);
  border-bottom: 2px solid var(--line);
  transform: skewX(-30deg) rotate(45deg);
}

.reply { line-height: 1.55; max-width: 68ch; }
.reply p { margin: 0 0 10px; }
.reply h3 { margin: 14px 0 6px; font-size: 1rem; color: var(--ink-strong); }
.reply ul, .reply ol { margin: 0 0 10px; padding-left: 1.3em; }
.reply li { margin-bottom: 3px; }
.reply strong { color: var(--ink-strong); font-weight: 500; }
.reply code {
  background: var(--white);
  border: 1px solid var(--line);
  border-radius: 4px;
  padding: 0 4px;
  font-size: 0.92em;
}
.reply pre.plain {
  background: var(--white);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 10px;
  overflow-x: auto;
}

.script {
  background: var(--white);
  border: 1px solid var(--line);
  border-radius: 8px;
  margin: 4px 0 14px;
  overflow: hidden;
}
.script-canvas {
  padding: 14px;
  overflow-x: auto;
  background-image: radial-gradient(var(--dots) 1.2px, transparent 1.2px);
  background-size: 24px 24px;
}
.script-canvas pre.blocks { margin: 0; font-size: 0.85rem; white-space: pre; }
.script-canvas svg { display: block; max-width: none; }
.script-bar {
  display: flex;
  justify-content: flex-end;
  gap: 6px;
  padding: 6px 8px;
  border-top: 1px solid var(--line);
}
.script-bar button {
  border: none;
  background: none;
  border-radius: 6px;
  padding: 4px 8px;
  cursor: pointer;
  font-size: 0.82rem;
  color: var(--motion-edge);
  font-weight: 500;
}
.script-bar button:hover { background: #e9f1ff; }
.script-text {
  margin: 0;
  padding: 10px 14px;
  border-top: 1px solid var(--line);
  font-size: 0.82rem;
  overflow-x: auto;
  background: var(--paper);
}
.script-text[hidden] { display: none; }

.pending { display: flex; gap: 5px; padding: 6px 0 20px; }
.pending span {
  width: 26px;
  height: 14px;
  border-radius: 4px;
  background: var(--events);
  opacity: 0.35;
  animation: stack 1.2s ease-in-out infinite;
}
.pending span:nth-child(2) { background: var(--motion); animation-delay: 0.15s; }
.pending span:nth-child(3) { background: #9966ff; animation-delay: 0.3s; }
@keyframes stack { 50% { opacity: 1; transform: translateY(-3px); } }

.error {
  border-left: 4px solid var(--danger);
  background: var(--white);
  padding: 10px 12px;
  margin-bottom: 18px;
  border-radius: 0 8px 8px 0;
  color: var(--ink-strong);
}

/* Composer: an Events hat block */
.composer { padding: 26px 0 18px; }
.hat {
  position: relative;
  background: var(--events);
  border: 1px solid var(--events-edge);
  border-radius: 0 10px 10px 10px;
  padding: 10px 12px 12px;
}
.hat::before { /* the dome on top of a hat block */
  content: "";
  position: absolute;
  left: -1px;
  top: -20px;
  width: 120px;
  height: 36px;
  background: var(--events);
  border: 1px solid var(--events-edge);
  border-bottom: none;
  border-radius: 60px 60px 0 0 / 20px 20px 0 0;
}
.hat::after { /* the connector tab underneath */
  content: "";
  position: absolute;
  left: 18px;
  bottom: -8px;
  width: 34px;
  height: 8px;
  background: var(--events);
  border: 1px solid var(--events-edge);
  border-top: none;
  border-radius: 0 0 5px 5px;
}
.hat > * { position: relative; z-index: 1; }
.hat-label {
  display: block;
  font-weight: 700;
  color: var(--events-text);
  margin: 0 0 8px 2px;
}
.hat-row { display: flex; gap: 8px; align-items: flex-end; }
.hat textarea {
  flex: 1;
  min-width: 0;
  resize: none;
  border: 1px solid var(--events-edge);
  border-radius: var(--radius-input);
  background: var(--white);
  padding: 9px 14px;
  line-height: 1.4;
  max-height: 40vh;
  color: var(--ink-strong);
}
.hat textarea::placeholder { color: #9197a8; }
.send {
  flex: none;
  border: 1px solid var(--motion-edge);
  background: var(--motion);
  color: var(--white);
  font-weight: 500;
  border-radius: var(--radius-input);
  padding: 9px 16px;
  cursor: pointer;
}
.send:disabled { opacity: 0.55; cursor: default; }
.foot { margin: 14px 0 0; font-size: 0.78rem; line-height: 1.4; }

/* Reply actions: Add to Scratch / Download .sb3 */
.reply-actions { display: flex; flex-wrap: wrap; gap: 8px; margin: 4px 0 6px; }
.act {
  border: 1px solid var(--line);
  background: var(--white);
  border-radius: var(--radius-input);
  padding: 7px 14px;
  cursor: pointer;
  font-weight: 500;
  color: var(--ink-strong);
}
.act:hover { border-color: var(--motion); }
.act.primary { background: var(--motion); border-color: var(--motion-edge); color: var(--white); }
.act.primary:hover { background: var(--motion-edge); }
.reply-status { margin: 0 0 10px; font-size: 0.85rem; min-height: 1em; }
.reply-status.bad { color: var(--danger); }
.reply-status:empty { display: none; }

.settings .check { display: flex; gap: 8px; align-items: flex-start; font-weight: 400; margin-bottom: 12px; line-height: 1.35; }
.settings .check input { margin-top: 3px; flex: none; width: 16px; height: 16px; }
a.ghost { text-decoration: none; color: inherit; }

.sr-only {
  position: absolute; width: 1px; height: 1px; overflow: hidden;
  clip: rect(0 0 0 0); white-space: nowrap;
}

@media (max-width: 520px) {
  .brand p { display: none; }
  .hat textarea { min-height: 4.4em; }
}
.is-panel .hat textarea { min-height: 4.4em; }

@media (prefers-reduced-motion: reduce) {
  .thread { scroll-behavior: auto; }
  .pending span { animation: none; opacity: 0.8; }
}
