/* terminal.css — mario0318 "dark instrument"
   Spec: TERMINAL_SPEC.md §1 tokens, §2 dots, §3 layout, §4 motion
   Animate transform/opacity/clip-path only. No other saturated hues than --dot-*. */

:root {
  --bg:          #0a0b0d;
  --bg-pattern:  rgba(255,255,255,0.03);
  --surface:     #111318;
  --surface-hi:  #181b22;

  --text:        #e8e4dc;
  --text-dim:    #8a8f98;
  --text-ghost:  #4a4f58;

  --dot-1:       #ff5c47;
  --dot-2:       #ffc14d;
  --dot-3:       #4dd8c7;

  --ok:          var(--dot-3);
  --warn:        var(--dot-2);
  --err:         var(--dot-1);
  --focus-ring:  var(--dot-3);

  --t-fast: 120ms;
  --t-med:  240ms;
  --t-slow: 420ms;
  --ease:   cubic-bezier(.2,.9,.2,1);

  --mono: ui-monospace, "JetBrains Mono", "SF Mono", Menlo, Consolas, monospace;
  --fs-body: clamp(14px, 1.6vw, 16px);
  --fs-small: 12px;
  --lh: 1.55;

  /* dots-lab writes these live; defaults per spec §2 */
  --dot-size: 10px;
  --dot-gap: 8px;
  --dot-cycle: 4s;
  --dot-hue: 0deg;
}

@media (prefers-reduced-motion: reduce) {
  :root { --t-fast: 0ms; --t-med: 0ms; --t-slow: 0ms; }
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: var(--mono);
  font-size: var(--fs-body);
  line-height: var(--lh);
  -webkit-font-smoothing: antialiased;
}

body {
  background-image: url("assets/dots-tile.svg");
  background-repeat: repeat;
  background-size: 56px 56px;
}

:focus-visible {
  outline: 2px solid var(--focus-ring);
  outline-offset: 2px;
}

/* ---------- fallback nav (no-JS) ---------- */

#fallback-nav {
  padding: 2rem;
  display: flex;
  gap: 1.5rem;
  flex-wrap: wrap;
}
#fallback-nav[hidden] { display: none; }
#fallback-nav a { color: var(--text); text-decoration: none; border-bottom: 1px solid var(--text-ghost); }
#fallback-nav a:hover { border-color: var(--dot-3); }

/* ---------- terminal shell ---------- */

.term {
  display: flex;
  flex-direction: column;
  height: 100dvh;
  max-width: 76ch;
  margin: 0 auto;
  padding: 0 1.25rem;
}
.term[hidden] { display: none; }

.term-head {
  padding: 1.5rem 0 1rem;
  flex: none;
}

.term-out {
  flex: 1 1 auto;
  overflow-y: auto;
  overflow-x: hidden;
  padding-bottom: .5rem;
  scrollbar-width: thin;
  scrollbar-color: var(--text-ghost) transparent;
  -webkit-mask-image: linear-gradient(to bottom, transparent 0, #000 24px);
          mask-image: linear-gradient(to bottom, transparent 0, #000 24px);
}
.term-out::-webkit-scrollbar { width: 6px; }
.term-out::-webkit-scrollbar-thumb { background: var(--text-ghost); border-radius: 3px; }

.line {
  white-space: pre-wrap;
  word-break: break-word;
  margin: 0;
  animation: line-in var(--t-med) var(--ease) both;
}
.line.dim { color: var(--text-dim); }
.line.echo { color: var(--text-dim); }
.line.shiver { animation: line-in var(--t-med) var(--ease) both, shiver var(--t-fast) var(--ease) 2; }

@keyframes line-in {
  from { clip-path: inset(0 100% 0 0); transform: translateY(4px); }
  to   { clip-path: inset(0 0 0 0);   transform: none; }
}
@keyframes shiver {
  0%,100% { transform: translateX(0); }
  50%     { transform: translateX(3px); }
}

/* stagger cap: 8 lines (spec §4) */
.line.s1 { animation-delay: 40ms }  .line.s2 { animation-delay: 80ms }
.line.s3 { animation-delay: 120ms } .line.s4 { animation-delay: 160ms }
.line.s5 { animation-delay: 200ms } .line.s6 { animation-delay: 240ms }
.line.s7 { animation-delay: 280ms } .line.s8 { animation-delay: 320ms }

/* ---------- prompt ---------- */

.term-prompt {
  flex: none;
  display: flex;
  align-items: baseline;
  gap: .6rem;
  padding: .6rem 0 1.25rem;
  padding-bottom: calc(1.25rem + env(safe-area-inset-bottom, 0px));
  border-top: 1px solid transparent;
}
.ps1 { color: var(--text-dim); user-select: none; }

#cmd {
  flex: 1;
  background: none;
  border: none;
  color: var(--text);
  font: inherit;
  padding: 0;
  caret-color: var(--dot-3);
}
#cmd:focus { outline: none; }
#cmd::placeholder { color: var(--text-ghost); }

/* ---------- chips ---------- */

.chips {
  flex: none;
  display: flex;
  gap: .5rem;
  overflow-x: auto;
  scroll-snap-type: x proximity;
  padding-bottom: .5rem;
  scrollbar-width: none;
}
.chips::-webkit-scrollbar { display: none; }
.chips[hidden] { display: none; }

.chip {
  scroll-snap-align: start;
  flex: none;
  background: var(--surface);
  color: var(--text-dim);
  border: 1px solid transparent;
  border-radius: 999px;
  padding: .3rem .75rem;
  font: inherit;
  font-size: var(--fs-small);
  cursor: pointer;
  transition: transform var(--t-fast) var(--ease),
              color var(--t-fast) var(--ease),
              border-color var(--t-fast) var(--ease);
}
.chip:hover { color: var(--text); border-color: var(--text-ghost); }
.chip:active { transform: scale(.98); }

/* ---------- the three dots (spec §2) ---------- */

.dots {
  display: inline-flex;
  gap: var(--dot-gap);
  filter: hue-rotate(var(--dot-hue));
}
.dot {
  width: var(--dot-size);
  height: var(--dot-size);
  border-radius: 50%;
  opacity: .55;
  transition: opacity var(--t-med) var(--ease),
              transform var(--t-med) var(--ease);
}
.d1 { background: var(--dot-1); }
.d2 { background: var(--dot-2); }
.d3 { background: var(--dot-3); }

/* idle: staggered breathing */
.dots.idle .dot { animation: breathe var(--dot-cycle) var(--ease) infinite; }
.dots.idle .d2 { animation-delay: 400ms; }
.dots.idle .d3 { animation-delay: 800ms; }
@keyframes breathe {
  0%,100% { opacity: .55; }
  50%     { opacity: .9; }
}

/* listening: hold + lift */
.dots.listening .dot { opacity: 1; transform: translateY(-1px); }

/* working: left-to-right chase */
.dots.working .dot { animation: chase 900ms var(--ease) infinite; }
.dots.working .d2 { animation-delay: 150ms; }
.dots.working .d3 { animation-delay: 300ms; }
@keyframes chase {
  0%,70%,100% { opacity: .35; transform: scale(1); }
  20%         { opacity: 1;   transform: scale(1.25); }
}

/* ok: single sync pulse */
.dots.ok .dot { animation: pulse var(--t-slow) var(--ease) 1; }
@keyframes pulse {
  0%   { opacity: .55; }
  40%  { opacity: 1; transform: scale(1.2); }
  100% { opacity: .55; transform: scale(1); }
}

/* err: ember double-blink, others dim */
.dots.err .d1 { animation: blink 300ms steps(1) 2; }
.dots.err .d2, .dots.err .d3 { opacity: .3; }
@keyframes blink {
  0%,49%   { opacity: 1; }
  50%,100% { opacity: .2; }
}

/* panel: shrink + dock tighter */
.dots.panel { gap: calc(var(--dot-gap) * .6); }
.dots.panel .dot { transform: scale(.8); }

.dots.elevated .d3 {
  box-shadow: 0 0 0 2px color-mix(in srgb, var(--dot-3) 40%, transparent);
}

@media (prefers-reduced-motion: reduce) {
  .dots .dot { animation: none !important; opacity: .7; }
  .dots.err .d1 { opacity: 1; }
  .dots.err .d2, .dots.err .d3 { opacity: .3; }
  .line, .line.shiver { animation: none; }
}

/* ---------- portal sheet (<dialog>) ---------- */

.portal {
  background: var(--surface);
  color: var(--text);
  border: 1px solid var(--surface-hi);
  border-radius: 12px;
  padding: 1.5rem;
  width: min(64ch, 92vw);
  max-height: 82dvh;
  overflow-y: auto;
  font: inherit;
}
.portal::backdrop { background: rgba(0,0,0,.72); backdrop-filter: blur(2px); }
.portal[open] { animation: portal-in var(--t-slow) var(--ease); }
@keyframes portal-in {
  from { opacity: 0; transform: translateY(24px); }
  to   { opacity: 1; transform: none; }
}
.portal.closing { animation: portal-out var(--t-med) var(--ease) forwards; }
@keyframes portal-out {
  to { opacity: 0; transform: scale(.98); }
}

.portal h2 { margin: 0 0 1rem; font-size: 1rem; font-weight: 500; color: var(--text-dim); }
.portal-close {
  position: absolute; top: .75rem; right: .9rem;
  background: none; border: none; color: var(--text-dim);
  font: inherit; font-size: 1.2rem; cursor: pointer; line-height: 1;
}
.portal-close:hover { color: var(--text); }

.p-item {
  display: block; width: 100%; text-align: left;
  background: none; border: none; border-radius: 8px;
  color: var(--text); font: inherit; padding: .7rem .8rem; cursor: pointer;
  transition: background var(--t-fast) var(--ease);
}
.p-item:hover { background: var(--surface-hi); }
.p-item small { display: block; color: var(--text-dim); font-size: var(--fs-small); }

/* ---------- applet panel ---------- */

.panel {
  position: fixed;
  inset: 0 0 auto 0;
  height: calc(100dvh - 64px);
  background: var(--surface);
  border-bottom: 1px solid var(--surface-hi);
  overflow: auto;
  animation: panel-in var(--t-slow) var(--ease);
  z-index: 10;
}
.panel[hidden] { display: none; }
@keyframes panel-in {
  from { transform: translateY(100%); }
  to   { transform: none; }
}

.panel-head {
  display: flex; align-items: center; justify-content: space-between;
  padding: 1rem 1.25rem;
  color: var(--text-dim); font-size: var(--fs-small);
  border-bottom: 1px solid var(--surface-hi);
  position: sticky; top: 0; background: var(--surface);
}
.panel-close {
  border: 0;
  background: none;
  color: var(--text-dim);
  font: inherit;
  font-size: 1.25rem;
  cursor: pointer;
}
.panel-close:hover { color: var(--text); }
#panel-mount { padding: 1.25rem; }

/* docked prompt when a panel is open */
body.panel-open .term {
  position: fixed; bottom: 0; left: 0; right: 0;
  height: 64px; max-width: none; z-index: 11;
  background: var(--bg); border-top: 1px solid var(--surface-hi);
  padding: 0 1.25rem;
  transition: transform var(--t-slow) var(--ease);
}
body.panel-open .term-out,
body.panel-open .term-head,
body.panel-open .chips { display: none; }
body.panel-open .term-prompt { padding: 1rem 0; }

/* ---------- applet internals ---------- */

.applet-frame {
  border: 1px solid var(--surface-hi);
  border-radius: 10px;
  overflow: hidden;
  background: var(--bg);
}
.applet-frame iframe { display: block; width: 100%; border: 0; }
.applet-card a { color: var(--dot-3); text-underline-offset: .2em; }

.lab-row { display: flex; align-items: center; gap: .75rem; margin: .4rem 0; }
.lab-row label { width: 10ch; color: var(--text-dim); font-size: var(--fs-small); }
.lab-row input[type="range"] { flex: 1; accent-color: var(--dot-3); }
.lab-actions { margin-top: .75rem; }
.lab-btn {
  background: var(--surface-hi); color: var(--text-dim);
  border: 1px solid transparent; border-radius: 6px;
  font: inherit; font-size: var(--fs-small); padding: .3rem .7rem; cursor: pointer;
}
.lab-btn:hover { color: var(--text); }

@media (max-width: 640px) {
  .term { padding: 0 1rem; }
  .term-head { padding: 1rem 0 .75rem; }
  .term-prompt { position: sticky; bottom: 0; background: var(--bg); }
}
