/* 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;
  --rule:        #252a32;

  --text:        #e8e4dc;
  --text-dim:    #8a8f98;
  --text-ghost:  #4a4f58;

  --dot-1:       #ff5c47;
  --dot-2:       #ffc14d;
  --dot-3:       #4dd8c7;

  /* mario0318 logo mark colors — used by the header brand dots */
  --brand-blue:  #2183e6;
  --brand-green: #23c32b;
  --brand-gray:  #8d9297;

  --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.42;

  /* 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,
* {
  scrollbar-width: none;
}
html::-webkit-scrollbar,
body::-webkit-scrollbar,
*::-webkit-scrollbar {
  width: 0;
  height: 0;
  display: none;
}

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:
    linear-gradient(rgba(255,255,255,.012) 1px, transparent 1px),
    linear-gradient(90deg, rgba(255,255,255,.009) 1px, transparent 1px);
  background-size: 4px 4px;
}

: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;
  width: min(1120px, calc(100% - 2rem));
  height: calc(100dvh - 2rem);
  max-width: 1120px;
  margin: 1rem auto;
  padding: 0 1rem;
  overflow: hidden;
  border: 1px solid var(--rule);
  border-radius: 14px;
  background: color-mix(in srgb, var(--bg) 96%, transparent);
  box-shadow: 0 24px 70px rgba(0,0,0,.38), 0 2px 0 rgba(255,255,255,.025) inset;
}
.term[hidden] { display: none; }

.term-head {
  min-height: 46px;
  padding: 0;
  flex: none;
  display: flex;
  align-items: stretch;
  justify-content: space-between;
  border-bottom: 1px solid var(--rule);
  background: linear-gradient(180deg, rgba(255,255,255,.035), transparent 80%);
  color: var(--text-dim);
  font-size: 11px;
  letter-spacing: .055em;
  text-transform: uppercase;
}

.term-id,
.term-channels {
  display: flex;
  align-items: center;
}

.term-id { gap: .75rem; }

.term-logo {
  display: inline-flex;
  align-items: center;
  gap: .75rem;
  height: 100%;
  color: inherit;
  text-decoration: none;
  text-transform: none;
}
.term-logo:hover .term-session,
.term-logo:focus-visible .term-session { color: var(--text); }

.term-session { color: var(--text); }

.term-channels {
  align-self: stretch;
  margin-left: auto;
  overflow-x: auto;
  scrollbar-width: none;
}
.term-channels::-webkit-scrollbar { display: none; }
.channel {
  align-self: stretch;
  display: inline-flex;
  align-items: center;
  gap: .45rem;
  flex: none;
  padding: 0 .8rem;
  border: 0;
  border-left: 1px solid var(--rule);
  background: transparent;
  color: var(--text-dim);
  font: inherit;
  letter-spacing: inherit;
  text-transform: none;
  text-decoration: none;
  cursor: pointer;
}
.channel:hover,
.channel:focus-visible { color: var(--text); background: var(--surface); }
.channel-dot {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--channel-color);
  box-shadow: 0 0 8px color-mix(in srgb, var(--channel-color) 45%, transparent);
}
.channel-raul3 { --channel-color: var(--dot-2); }
.channel-caret { color: var(--text-ghost); font-size: 14px; line-height: 1; }

.term-logo > .dots {
  align-self: stretch;
  align-items: center;
  padding-right: 1rem;
  border-right: 1px solid var(--rule);
}

.term-out {
  flex: 1 1 auto;
  overflow-y: auto;
  overflow-x: hidden;
  padding: 1rem 0 .6rem 3.25rem;
  background-image: linear-gradient(90deg,
    transparent 0,
    transparent 2.25rem,
    var(--rule) 2.25rem,
    var(--rule) calc(2.25rem + 1px),
    transparent calc(2.25rem + 1px));
  scrollbar-width: none;
  -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 { display: none; }

.line {
  white-space: pre-wrap;
  word-break: break-word;
  margin: 0;
  min-height: 1.35em;
  position: relative;
  animation: line-in var(--t-med) var(--ease) both;
}
.line.dim { color: var(--text-dim); }
.line.echo { color: var(--text-dim); }
.line.echo::before {
  content: "›";
  position: absolute;
  right: calc(100% + 1rem);
  color: var(--dot-3);
}
.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: .65rem;
  min-height: 52px;
  margin: 0 0 .75rem;
  padding: .7rem .9rem;
  padding-bottom: calc(.7rem + env(safe-area-inset-bottom, 0px));
  border: 1px solid color-mix(in srgb, var(--dot-3) 34%, var(--rule));
  border-radius: 8px;
  background: var(--surface-hi);
  box-shadow: 0 8px 24px rgba(0,0,0,.18), 0 1px 0 rgba(255,255,255,.035) inset;
}
.ps1 { color: var(--text); user-select: none; white-space: nowrap; font-weight: 600; }
.ps1-user, .ps1-at, .ps1-host, .ps1-path { color: inherit; }
.ps1-char { color: var(--dot-3); margin-left: .35rem; }

#cmd {
  flex: 1;
  min-width: 0;
  background: none;
  border: none;
  color: var(--text);
  font: inherit;
  font-weight: 600;
  padding: 0;
  caret-color: var(--dot-3);
}
#cmd:focus { outline: none; }
#cmd::placeholder { color: var(--text-ghost); }

/* ---------- 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(--brand-blue); }
.d2 { background: var(--brand-green); }
.d3 { background: var(--brand-gray); }

/* 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 {
  position: relative;
  background:
    radial-gradient(circle at 0 0, rgba(117,225,202,.09), transparent 14rem),
    linear-gradient(145deg, #151b20, var(--surface) 48%, #0f1317);
  color: var(--text);
  border: 1px solid color-mix(in srgb, var(--dot-3) 30%, var(--surface-hi));
  border-radius: 18px;
  padding: 1.8rem 1.2rem 1rem;
  width: min(28rem, calc(100vw - 2rem));
  max-height: 84dvh;
  overflow-y: auto;
  font: inherit;
  box-shadow: 0 28px 100px rgba(0,0,0,.5), 0 1px 0 rgba(255,255,255,.05) inset;
}
.portal::backdrop { background: rgba(0,0,0,.78); backdrop-filter: blur(7px); }
.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 .8rem .7rem; color: var(--text); font-size: .86rem; font-weight: 500; letter-spacing: .12em; text-transform: uppercase; }
.portal h2::before { content: "↗  "; color: var(--dot-3); }
.portal-close {
  position: absolute; top: .8rem; right: .9rem;
  background: none; border: none; color: var(--text-dim);
  font: inherit; font-size: 1.35rem; cursor: pointer; line-height: 1;
}
.portal-close:hover { color: var(--text); }

.p-item {
  display: block; width: 100%; text-align: left;
  background: transparent; border: 1px solid transparent; border-radius: 10px;
  color: var(--text); font: inherit; padding: .56rem .8rem; cursor: pointer;
  text-decoration: none;
  transition: background var(--t-fast) var(--ease), border-color var(--t-fast) var(--ease), transform var(--t-fast) var(--ease);
}
.p-item:hover, .p-item:focus-visible { background: rgba(117,225,202,.08); border-color: rgba(117,225,202,.22); text-decoration: none; transform: translateX(3px); }
.p-item small { display: block; margin-top: .16rem; color: var(--text-dim); font-size: var(--fs-small); }
.p-link { position: relative; padding-right: 2rem; }
.p-link::after { content: "↗"; position: absolute; top: .58rem; right: .85rem; color: var(--text-ghost); }
.p-link:hover::after, .p-link:focus-visible::after { color: var(--dot-3); }

/* ---------- 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 { 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 { width: calc(100% - 1rem); height: calc(100dvh - 1rem); margin: .5rem auto; padding: 0 .75rem; }
  .term-head { min-height: 42px; }
  .term-session { display: none; }
  .channel { padding-inline: .6rem; }
  .channel-label { font-size: 0; }
  .channel-label::after { content: attr(data-short); font-size: 11px; }
  .term-out { padding-left: 1.4rem; background-position-x: -1rem; }
  .term-prompt { position: sticky; bottom: 0; margin-inline: 0; padding-inline: .75rem; }
  .ps1-user, .ps1-at, .ps1-host { display: none; }
  .ps1-path { color: var(--dot-3); }
}

@media (prefers-reduced-motion: reduce) {
  .term-prompt::before { box-shadow: none; }
}
