/* ---------------------------------------------------------------
   Two zones. The chrome is cool paper, like a plan set. The stage is a
   dark instrument well, because that is where a scanner does its work.
   The structured-light fringe is the only saturated colour in the build.
   --------------------------------------------------------------- */

:root {
  --paper: #e4e7ea;
  --paper-raised: #f3f5f7;
  --ink: #14181f;
  --ink-soft: #5a6472;
  --rule: #c6ccd3;
  --fringe-a: #ff2e88;
  --fringe-b: #00e0ff;

  --display: "Bricolage Grotesque", system-ui, sans-serif;
  --body: "Inter Tight", system-ui, sans-serif;
  --mono: "JetBrains Mono", ui-monospace, monospace;

  --gap: 1rem;
  --radius: 3px; /* nearly square: this is an instrument, not a card */
}

* {
  box-sizing: border-box;
}

/* Author styles outrank the browser's [hidden] rule, and several elements here
   set display, so hiding them needs to be stated explicitly. */
[hidden] {
  display: none !important;
}

html,
body {
  margin: 0;
  max-width: 100%;
  overflow-x: hidden;
}

body {
  background: var(--paper);
  color: var(--ink);
  font-family: var(--body);
  font-size: 16px;
  line-height: 1.45;
  -webkit-font-smoothing: antialiased;
  padding-bottom: env(safe-area-inset-bottom);
}

/* --- top bar --------------------------------------------------- */

.bar {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: var(--gap);
  padding: 0.9rem 1.1rem;
  border-bottom: 1px solid var(--rule);
}

.mark {
  font-family: var(--display);
  font-weight: 700;
  font-size: 1.05rem;
  letter-spacing: -0.02em;
}

.bar-meta {
  font-family: var(--mono);
  font-size: 0.7rem;
  color: var(--ink-soft);
  letter-spacing: 0.02em;
}

main {
  max-width: 720px;
  margin: 0 auto;
  padding: 1.1rem;
  display: grid;
  /* A bare grid column defaults to auto-sized, which — like a flex item's
     default min-width — refuses to shrink below the min-content width of
     its widest child. One overly-wide child (e.g. two unshrinkable buttons)
     then drags every sibling in the column wide enough to overflow with it. */
  grid-template-columns: minmax(0, 1fr);
  gap: 1.1rem;
}

/* --- the well -------------------------------------------------- */

.stage {
  border: 1px solid var(--rule);
  border-radius: var(--radius);
  overflow: hidden;
  background: var(--ink);
}

.well {
  position: relative;
  aspect-ratio: 4 / 3;
  display: grid;
  place-items: center;
  background:
    radial-gradient(120% 90% at 50% 0%, #1e242e 0%, var(--ink) 70%);
  transition: outline-color 0.15s;
  outline: 2px solid transparent;
  outline-offset: -2px;
}

.stage[data-dragging="true"] .well {
  outline-color: var(--fringe-b);
}

/* empty state */

.empty {
  text-align: center;
  padding: 1.5rem;
  color: #8d97a6;
}

.empty-line {
  font-family: var(--display);
  font-weight: 600;
  font-size: clamp(1.3rem, 5vw, 1.75rem);
  letter-spacing: -0.02em;
  color: var(--paper-raised);
  margin: 0 0 0.35rem;
}

.empty-sub {
  font-family: var(--mono);
  font-size: 0.72rem;
  margin: 0;
  letter-spacing: 0.01em;
}

/* viewfinder brackets, sized off the well */
.brackets {
  position: absolute;
  inset: 8%;
  pointer-events: none;
}

.brackets i {
  position: absolute;
  width: 20px;
  height: 20px;
  border: 1px solid #39424f;
}

.brackets i:nth-child(1) { top: 0; left: 0; border-right: 0; border-bottom: 0; }
.brackets i:nth-child(2) { top: 0; right: 0; border-left: 0; border-bottom: 0; }
.brackets i:nth-child(3) { bottom: 0; left: 0; border-right: 0; border-top: 0; }
.brackets i:nth-child(4) { bottom: 0; right: 0; border-left: 0; border-top: 0; }

/* scanning state — the signature */

.scan {
  position: relative;
  margin: 0;
  width: 100%;
  height: 100%;
  overflow: hidden;
}

.scan img,
.scan video {
  width: 100%;
  height: 100%;
  object-fit: contain;
  filter: saturate(0.55) contrast(1.05);
}

.scan video {
  position: absolute;
  inset: 0;
  filter: none;
}

/* Once the turntable is playing it is the better progress signal. */
.stage[data-preview="true"] .scan img,
.stage[data-preview="true"] .fringe {
  display: none;
}

/* AI-generated reference photo, pending a decision */

.ai-preview {
  position: relative;
  margin: 0;
  width: 100%;
  height: 100%;
  overflow: hidden;
}

.ai-preview img {
  width: 100%;
  height: 100%;
  object-fit: contain;
}

.fringe {
  position: absolute;
  inset: 0;
  pointer-events: none;
  background: repeating-linear-gradient(
    to bottom,
    rgba(255, 46, 136, 0.55) 0px,
    rgba(255, 46, 136, 0.55) 2px,
    transparent 2px,
    transparent 7px,
    rgba(0, 224, 255, 0.5) 7px,
    rgba(0, 224, 255, 0.5) 9px,
    transparent 9px,
    transparent 14px
  );
  mask-image: linear-gradient(
    to bottom,
    transparent 0%,
    #000 12%,
    #000 22%,
    transparent 34%
  );
  mask-size: 100% 300%;
  -webkit-mask-image: linear-gradient(
    to bottom,
    transparent 0%,
    #000 12%,
    #000 22%,
    transparent 34%
  );
  -webkit-mask-size: 100% 300%;
  animation: sweep 2.4s linear infinite;
  mix-blend-mode: screen;
}

@keyframes sweep {
  from { mask-position: 0 -100%; -webkit-mask-position: 0 -100%; }
  to   { mask-position: 0 200%;  -webkit-mask-position: 0 200%; }
}

/* result state */

model-viewer {
  width: 100%;
  height: 100%;
  background: transparent;
  --poster-color: transparent;
}

/* readout strip */

.readout {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--gap);
  padding: 0.55rem 0.8rem;
  border-top: 1px solid #2a313c;
  font-family: var(--mono);
  font-size: 0.72rem;
  letter-spacing: 0.02em;
  color: #9aa4b2;
}

.readout-stage {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  color: var(--paper-raised);
}

.stage[data-state="working"] .readout-stage::before {
  content: "";
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--fringe-a);
  animation: pulse 1.1s ease-in-out infinite;
}

@keyframes pulse {
  50% { opacity: 0.25; }
}

/* --- buttons --------------------------------------------------- */

.controls,
.result-actions,
.preview-actions {
  display: flex;
  gap: 0.6rem;
}

.generate-row {
  display: flex;
  gap: 0.6rem;
}

.generate-row input {
  flex: 1;
  min-width: 0;
  min-height: 46px;
  padding: 0 0.8rem;
  border: 1px solid var(--rule);
  border-radius: var(--radius);
  background: var(--paper);
  color: var(--ink);
  font-family: var(--body);
  font-size: 0.95rem;
}

.generate-row input:focus-visible {
  outline: 2px solid var(--fringe-a);
  outline-offset: 2px;
}

.generate-row .btn {
  flex: 0 0 auto;
}

.mic-btn {
  flex: 0 0 auto;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 46px;
  min-height: 46px;
  padding: 0;
  border: 1px solid var(--rule);
  border-radius: var(--radius);
  background: var(--paper);
  font-size: 1.05rem;
  line-height: 1;
  cursor: pointer;
}

.mic-btn:focus-visible {
  outline: 2px solid var(--fringe-a);
  outline-offset: 2px;
}

.mic-btn[aria-disabled="true"] {
  opacity: 0.4;
  pointer-events: none;
}

.mic-btn.listening {
  background: var(--fringe-a);
  border-color: var(--fringe-a);
  animation: pulse 1.1s ease-in-out infinite;
}

.btn {
  flex: 1;
  min-width: 0;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: 46px;
  padding: 0 1rem;
  border: 1px solid var(--ink);
  border-radius: var(--radius);
  background: transparent;
  color: var(--ink);
  font-family: var(--body);
  font-size: 0.95rem;
  font-weight: 500;
  text-decoration: none;
  cursor: pointer;
}

.btn-primary {
  background: var(--ink);
  color: var(--paper-raised);
}

.btn:focus-visible {
  outline: 2px solid var(--fringe-a);
  outline-offset: 2px;
}

.btn[aria-disabled="true"] {
  opacity: 0.4;
  pointer-events: none;
}

/* --- error ----------------------------------------------------- */

.error {
  margin: 0;
  padding: 0.7rem 0.85rem;
  border-left: 3px solid var(--fringe-a);
  background: var(--paper-raised);
  font-size: 0.88rem;
}

/* --- blender live viewport --------------------------------------- */

.blender-live {
  border: 1px solid var(--rule);
  border-radius: var(--radius);
  background: var(--paper-raised);
  overflow: hidden;
}

.blender-live-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--gap);
  padding: 0.6rem 0.8rem 0;
}

.blender-live-head .eyebrow {
  margin: 0;
}

.blender-live-actions {
  display: flex;
  gap: 0.9rem;
}

.link-btn {
  border: 0;
  background: none;
  padding: 0;
  color: var(--ink-soft);
  font-family: var(--mono);
  font-size: 0.68rem;
  letter-spacing: 0.04em;
  text-decoration: underline;
  cursor: pointer;
}

.link-btn:focus-visible {
  outline: 2px solid var(--fringe-a);
  outline-offset: 2px;
}

#blenderViewer {
  display: block;
  width: 100%;
  height: 320px;
  background: var(--paper);
}

.blender-raw {
  margin: 0.6rem 0.8rem 0;
}

#blenderFrame {
  display: block;
  width: 100%;
  height: auto;
  border-radius: var(--radius);
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
  cursor: grab;
}

.chat-log {
  list-style: none;
  margin: 0;
  padding: 0.6rem 0.8rem 0;
  max-height: 220px;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 0.4rem;
}

.chat-log:empty {
  padding: 0;
}

.chat-msg {
  max-width: 85%;
  padding: 0.5rem 0.7rem;
  border-radius: var(--radius);
  font-size: 0.88rem;
  line-height: 1.35;
  overflow-wrap: break-word;
  word-break: break-word;
}

.chat-msg-user {
  align-self: flex-end;
  background: var(--ink);
  color: var(--paper-raised);
}

.chat-msg-assistant {
  align-self: flex-start;
  background: var(--paper);
  border: 1px solid var(--rule);
}

.chat-msg-status {
  align-self: flex-start;
  color: var(--ink-soft);
  font-family: var(--mono);
  font-size: 0.72rem;
  letter-spacing: 0.02em;
}

.chat-msg-error {
  align-self: stretch;
  border-left: 3px solid var(--fringe-a);
  background: var(--paper);
}

.chat-input {
  display: flex;
  gap: 0.5rem;
  padding: 0.6rem 0.8rem 0.8rem;
}

.chat-input input {
  flex: 1;
  min-width: 0;
  min-height: 46px;
  padding: 0 0.8rem;
  border: 1px solid var(--rule);
  border-radius: var(--radius);
  background: var(--paper);
  color: var(--ink);
  font-family: var(--body);
  font-size: 0.95rem;
}

.chat-input input:focus-visible {
  outline: 2px solid var(--fringe-a);
  outline-offset: 2px;
}

.chat-input .btn {
  flex: 0 0 auto;
}

.blender-save-row {
  padding: 0 0.8rem 0.8rem;
}

.blender-save-row .btn {
  width: 100%;
}

/* --- library --------------------------------------------------- */

.eyebrow {
  font-family: var(--mono);
  font-size: 0.68rem;
  font-weight: 400;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--ink-soft);
  margin: 0 0 0.55rem;
}

.shelf {
  list-style: none;
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(84px, 1fr));
  gap: 0.5rem;
  margin: 0;
  padding: 0;
}

.shelf button {
  width: 100%;
  aspect-ratio: 1;
  padding: 0;
  border: 1px solid var(--rule);
  border-radius: var(--radius);
  background: var(--paper-raised) center / cover no-repeat;
  cursor: pointer;
}

.shelf button:focus-visible {
  outline: 2px solid var(--fringe-a);
  outline-offset: 2px;
}

/* --- quality floor --------------------------------------------- */

@media (prefers-reduced-motion: reduce) {
  .fringe {
    animation: none;
    mask-position: 0 50%;
    -webkit-mask-position: 0 50%;
  }
  .stage[data-state="working"] .readout-stage::before {
    animation: none;
  }
  model-viewer {
    --auto-rotate: none;
  }
}

@media (min-width: 640px) {
  main {
    padding: 1.5rem;
    gap: 1.25rem;
  }
  .well {
    aspect-ratio: 16 / 10;
  }
}
