:root {
  --bg: #f3fafa;
  --surface: #ffffff;
  --ink: #14343a;
  --muted: #5a7c82;
  --line: #d4e8eb;
  --accent: #12b5c4;
  --accent-deep: #0b7f8a;
  --danger: #b42318;
  --radius: 8px;
}

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  min-height: 100%;
  background: var(--bg);
  color: var(--ink);
  font-family: Figtree, system-ui, sans-serif;
}

body {
  min-height: 100vh;
  display: grid;
  grid-template-rows: auto 1fr;
}

.top {
  display: flex;
  gap: 16px;
  align-items: center;
  padding: 16px 20px;
  border-bottom: 1px solid var(--line);
  background: var(--surface);
}

.source {
  display: flex;
  gap: 8px;
  flex: 1;
}

input,
button,
select {
  font: inherit;
  color: inherit;
}

input,
select {
  border: 1px solid var(--line);
  background: var(--surface);
  border-radius: var(--radius);
  padding: 10px 12px;
}

.source input {
  flex: 1;
}

button {
  border: 0;
  background: var(--accent);
  color: #f4ffff;
  border-radius: var(--radius);
  padding: 10px 16px;
  font-weight: 600;
  cursor: pointer;
}

button:hover {
  background: var(--accent-deep);
}

button.ghost {
  background: transparent;
  color: var(--muted);
  padding: 6px 8px;
}

button.ghost:hover {
  background: #e7f6f8;
  color: var(--ink);
}

.toolbar {
  position: relative;
  display: flex;
  align-items: center;
  gap: 8px;
}

.status {
  display: flex;
  align-items: center;
  gap: 8px;
  color: var(--muted);
  white-space: nowrap;
  font-size: 14px;
}

.text-btn {
  height: 36px;
  padding: 0 12px;
  background: transparent;
  color: var(--accent-deep);
  border: 1px solid var(--line);
  font-weight: 600;
  white-space: nowrap;
}

.text-btn:hover,
.text-btn[aria-pressed='true'] {
  background: #e7f7f9;
  color: var(--accent-deep);
}

.icon-btn {
  display: grid;
  place-items: center;
  width: 36px;
  height: 36px;
  padding: 0;
  background: transparent;
  color: var(--accent-deep);
  border: 1px solid var(--line);
}

.icon-btn:hover,
.icon-btn[aria-expanded='true'] {
  background: #e7f7f9;
  color: var(--accent);
}

.settings {
  position: absolute;
  top: calc(100% + 8px);
  right: 0;
  z-index: 8;
  width: 280px;
  margin: 0;
}

.settings-panel {
  display: grid;
  gap: 10px;
  padding: 12px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
}

.settings label {
  display: grid;
  gap: 4px;
  color: var(--muted);
  font-size: 12px;
  font-weight: 600;
}

.settings label[hidden] {
  display: none;
}

.settings button[type='submit'] {
  justify-self: start;
}

.ask {
  position: absolute;
  top: calc(100% + 8px);
  right: 44px;
  z-index: 8;
  width: 380px;
  margin: 0;
}

.ask-sheet {
  display: grid;
  gap: 10px;
  padding: 12px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
}

.dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: #c5d5d8;
}

.dot.on {
  background: var(--accent);
}

.dot.err {
  background: var(--danger);
}

main {
  padding: 20px;
}

.banner {
  margin: 0 0 16px;
  padding: 10px 12px;
  border: 1px solid var(--line);
  background: #eef8f9;
  border-radius: var(--radius);
  color: var(--accent-deep);
}

.banner.error {
  background: #fff4f2;
  color: var(--danger);
  border-color: #f0c7c2;
}

.charts {
  position: relative;
  display: block;
  min-height: 240px;
}

.card {
  position: absolute;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 12px 12px 18px;
  min-width: 0;
  min-height: 0;
  display: flex;
  flex-direction: column;
  gap: 10px;
  scroll-margin: 24px;
}

.card.picked {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

.card.building {
  animation: card-in 280ms ease-out both, card-glow 900ms ease-out;
}

@keyframes card-in {
  from {
    opacity: 0;
    transform: scale(0.97);
  }
  to {
    opacity: 1;
    transform: scale(1);
  }
}

@keyframes card-glow {
  0% {
    box-shadow: 0 0 0 0 rgba(18, 181, 196, 0.45);
  }
  100% {
    box-shadow: 0 0 0 12px rgba(18, 181, 196, 0);
  }
}

.card.revealed {
  z-index: 6;
  border-color: var(--accent);
  animation: card-reveal 1.4s cubic-bezier(0.22, 1, 0.36, 1);
}

@keyframes card-reveal {
  0% {
    box-shadow: 0 0 0 0 rgba(18, 181, 196, 0.55);
  }
  22% {
    box-shadow: 0 0 0 9px rgba(18, 181, 196, 0.32);
  }
  100% {
    box-shadow: 0 0 0 0 rgba(18, 181, 196, 0);
  }
}

@media (prefers-reduced-motion: reduce) {
  .card.revealed {
    animation: none;
  }
}

.card.dragging,
.card.menu-open,
.card.revealed {
  z-index: 6;
  border-color: var(--accent);
}

.card.off {
  opacity: 0.45;
  min-height: 0;
}

.card-head {
  display: flex;
  align-items: center;
  gap: 8px;
}

.drag-handle {
  flex: 0 0 14px;
  width: 14px;
  height: 22px;
  cursor: grab;
  touch-action: none;
  background-image: radial-gradient(circle, var(--muted) 1.1px, transparent 1.3px);
  background-size: 6px 6px;
  background-position: center;
}

.card.dragging .drag-handle {
  cursor: grabbing;
}

.chart-no {
  flex: 0 0 auto;
  color: var(--accent-deep);
  font-size: 12px;
  font-weight: 700;
}

.card-head h2 {
  margin: 0;
  font-size: 14px;
  font-weight: 600;
  line-height: 1.35;
  flex: 1;
  min-width: 0;
  cursor: text;
  outline: none;
}

.card-head h2:focus {
  box-shadow: inset 0 -1px 0 var(--accent);
}

.type-btn {
  font-size: 12px;
  letter-spacing: 0.04em;
}

.card-tools {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.card-tools select {
  min-width: 0;
  width: 100%;
}

.var-row {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 8px;
  align-items: center;
}

.card-menu {
  position: relative;
  flex: 0 0 auto;
}

.menu-btn {
  display: grid;
  place-items: center;
  width: 28px;
  height: 28px;
  padding: 0;
}

.menu-btn[aria-expanded='true'] {
  background: #e7f7f9;
  color: var(--accent-deep);
}

.menu-panel {
  position: absolute;
  top: calc(100% + 4px);
  right: 0;
  z-index: 7;
  width: 196px;
  padding: 6px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: 0 2px 8px rgba(20, 52, 58, 0.12);
}

.menu-action,
.palette-choice {
  display: flex;
  align-items: center;
  width: 100%;
  gap: 8px;
  margin: 0;
  padding: 7px 8px;
  border: 0;
  border-radius: 6px;
  background: transparent;
  color: var(--ink);
  font-size: 13px;
  font-weight: 500;
  text-align: left;
}

.menu-action:hover,
.palette-choice:hover {
  background: #eef8f9;
}

.menu-action.on,
.palette-choice.on {
  background: #e7f7f9;
  color: var(--accent-deep);
}

.menu-palettes,
.menu-formula,
.menu-export {
  display: grid;
  gap: 2px;
  margin-top: 4px;
  padding-top: 4px;
  border-top: 1px solid var(--line);
}

.menu-export {
  gap: 8px;
  padding-bottom: 4px;
}

.menu-export-head {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 4px 8px 0;
  color: var(--muted);
  font-size: 12px;
  font-weight: 600;
}

.menu-export-head svg {
  flex: 0 0 auto;
}

.menu-formats {
  display: flex;
  gap: 4px;
  padding: 0 8px 2px;
}

.menu-format {
  flex: 1;
  margin: 0;
  padding: 6px 0;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: var(--surface);
  color: var(--ink);
  font-size: 11px;
  font-weight: 700;
  letter-spacing: 0.04em;
  cursor: pointer;
}

.menu-format:hover {
  background: #eef8f9;
  border-color: var(--accent);
  color: var(--accent-deep);
}

.color-pop {
  position: fixed;
  z-index: 20;
  width: 36px;
  height: 36px;
  padding: 4px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: 0 2px 8px rgba(20, 52, 58, 0.12);
}

.color-pop input[type='color'] {
  display: block;
  width: 26px;
  height: 26px;
  padding: 0;
  border: 0;
  background: transparent;
  cursor: pointer;
}

.swatches {
  display: flex;
  gap: 3px;
}

.swatch {
  width: 12px;
  height: 12px;
  border-radius: 2px;
}

.add-var {
  align-self: start;
  font-weight: 600;
  color: var(--accent-deep);
}

.canvas-wrap {
  position: relative;
  flex: 1;
  min-height: 80px;
}

.canvas-wrap canvas {
  cursor: pointer;
}

.resize {
  position: absolute;
  right: 3px;
  bottom: 3px;
  width: 14px;
  height: 14px;
  cursor: nwse-resize;
  touch-action: none;
  background: linear-gradient(135deg, transparent 55%, var(--accent) 55%);
}

.add {
  position: absolute;
  border: 1px dashed var(--accent);
  background: transparent;
  color: var(--accent-deep);
  min-height: 0;
  min-width: 0;
  font-size: 42px;
  font-weight: 500;
}

.add:hover {
  background: #e7f7f9;
}

.chat-log {
  max-height: min(320px, 50vh);
  min-height: 72px;
  overflow: auto;
  display: grid;
  gap: 8px;
  font-size: 14px;
  line-height: 1.45;
}

.msg {
  max-width: 72ch;
}

.msg.user {
  color: var(--accent-deep);
  font-weight: 600;
}

.msg.bot {
  color: var(--ink);
}

.msg.bot p {
  margin: 0 0 8px;
}

.msg.bot p:last-child {
  margin-bottom: 0;
}

.msg.bot table {
  border-collapse: collapse;
  font-size: 13px;
}

.msg.bot th,
.msg.bot td {
  border: 1px solid var(--line);
  padding: 4px 8px;
}

.chips {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-top: 8px;
}

.chip {
  border: 1px solid var(--line);
  background: #fff;
  color: var(--accent-deep);
  border-radius: 999px;
  padding: 6px 10px;
  font-size: 13px;
  font-weight: 600;
}

.chip:hover {
  background: #e7f7f9;
  color: var(--accent-deep);
}

.chip:disabled {
  opacity: 0.45;
  cursor: default;
}

.chat-form {
  display: flex;
  gap: 8px;
  min-width: 0;
}

.chat-form input {
  flex: 1;
  min-width: 0;
}

.chat-form button {
  flex: 0 0 auto;
  white-space: nowrap;
}

@media (max-width: 720px) {
  .top {
    flex-direction: column;
    align-items: stretch;
    gap: 10px;
    padding: 12px max(12px, env(safe-area-inset-right)) 12px max(12px, env(safe-area-inset-left));
  }

  .source {
    display: grid;
    grid-template-columns: auto 1fr;
    grid-template-areas:
      'url url'
      'up go';
  }

  #source-url {
    grid-area: url;
    min-width: 0;
    width: 100%;
  }

  #file-btn {
    grid-area: up;
  }

  .source button[type='submit'] {
    grid-area: go;
    white-space: nowrap;
  }

  .toolbar {
    flex-wrap: wrap;
  }

  .status {
    flex: 1 0 100%;
  }

  .ask,
  .settings {
    left: 8px;
    right: 8px;
    width: auto;
    max-width: none;
  }

  .chat-form {
    flex-direction: column;
  }

  .chat-form button {
    width: 100%;
  }

  .chat-log {
    max-height: min(180px, 28vh);
  }

  .var-row {
    grid-template-columns: minmax(0, 1fr);
  }

  .menu-panel {
    max-width: min(196px, calc(100vw - 24px));
  }

  .tour-card {
    width: calc(100vw - 24px);
    max-height: min(42vh, 320px);
    overflow: auto;
  }

  .tour-demo {
    width: calc(100% - 8px);
    height: 220px;
  }

  .tour-actions {
    justify-content: stretch;
  }

  .tour-actions button {
    flex: 1;
  }
}

.source .ghost {
  padding: 10px 16px;
  border: 1px solid var(--line);
  color: var(--ink);
}

.ask.tour-lift {
  z-index: 90;
  animation: tour-ask-in 360ms ease both;
}

@keyframes tour-ask-in {
  from {
    opacity: 0;
    transform: translateY(-8px);
  }
  to {
    opacity: 1;
    transform: none;
  }
}

.tour-demo {
  left: 0;
  top: 0;
  width: min(380px, 100%);
  height: 280px;
  z-index: 1;
  pointer-events: none;
  opacity: 0;
  transform: scale(0.97);
  transition: opacity 360ms ease, transform 360ms ease;
}

.tour-demo.is-in {
  opacity: 1;
  transform: none;
}

.tour-demo .tour-demo-menu {
  display: none;
}

.tour-demo.menu-open .tour-demo-menu {
  display: grid;
}

.tour-demo-chart {
  display: flex;
  align-items: flex-end;
  gap: 12px;
  flex: 1;
  min-height: 140px;
  padding: 8px 16px 4px;
}

.tour-demo-chart span {
  flex: 1;
  border-radius: 4px 4px 0 0;
  transform-origin: bottom;
  transform: scaleY(0);
}

.tour-demo.is-in span {
  animation: tour-bar 520ms cubic-bezier(0.22, 1, 0.36, 1) both;
}

.tour-demo-chart span:nth-child(1) {
  height: 42%;
  background: #7dd1e8;
  animation-delay: 80ms;
}

.tour-demo-chart span:nth-child(2) {
  height: 78%;
  background: #4cb7e1;
  animation-delay: 160ms;
}

.tour-demo-chart span:nth-child(3) {
  height: 55%;
  background: #1ab6cb;
  animation-delay: 240ms;
}

.tour-demo-chart span:nth-child(4) {
  height: 28%;
  background: #008a87;
  animation-delay: 320ms;
}

@keyframes tour-bar {
  from {
    transform: scaleY(0);
  }
  to {
    transform: scaleY(1);
  }
}

.tour-root {
  position: fixed;
  inset: 0;
  z-index: 80;
  pointer-events: none;
  opacity: 0;
  transition: opacity 280ms ease;
}

.tour-root.is-on {
  pointer-events: auto;
  opacity: 1;
}

.tour-spot {
  position: absolute;
  border: 2px solid var(--accent);
  border-radius: var(--radius);
  box-shadow: 0 0 0 9999px rgba(20, 52, 58, 0.45);
  transition: top 360ms ease, left 360ms ease, width 360ms ease, height 360ms ease;
}

.tour-spot.pulse {
  animation: tour-pulse 1.6s ease;
}

@keyframes tour-pulse {
  0% {
    box-shadow: 0 0 0 9999px rgba(20, 52, 58, 0.45), 0 0 0 0 rgba(18, 181, 196, 0.45);
  }
  100% {
    box-shadow: 0 0 0 9999px rgba(20, 52, 58, 0.45), 0 0 0 16px rgba(18, 181, 196, 0);
  }
}

.tour-card {
  position: absolute;
  z-index: 91;
  width: min(360px, calc(100vw - 32px));
  background: var(--surface);
  color: var(--ink);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 16px;
  box-shadow: 0 2px 8px rgba(20, 52, 58, 0.12);
  opacity: 0;
  transform: translateY(10px);
  transition: opacity 280ms ease, transform 280ms ease, top 360ms ease, left 360ms ease;
}

.tour-card.is-in {
  opacity: 1;
  transform: none;
}

.tour-card h2 {
  margin: 0 0 8px;
  font-size: 1.05rem;
}

.tour-card p {
  margin: 0 0 16px;
  color: var(--muted);
  line-height: 1.45;
}

.tour-actions {
  display: flex;
  gap: 8px;
  justify-content: flex-end;
  flex-wrap: wrap;
}

.tour-actions .ghost {
  background: transparent;
  color: var(--ink);
  border: 1px solid var(--line);
}

@media (prefers-reduced-motion: reduce) {
  .tour-spot,
  .tour-spot.pulse,
  .tour-card,
  .tour-demo,
  .tour-demo.is-in span,
  .ask.tour-lift {
    animation: none;
    transition: none;
  }
}
