/* App frame: top bar, main grid, chat column shell, pipeline strip,
   composer, memory panel grid, activity log shell, responsive layout. */

#app {
  display: grid;
  grid-template-rows: auto 1fr auto;
  height: 100vh;
  gap: 0;
}

/* ---------- Top bar ---------- */
#topbar {
  position: relative;
  z-index: 650;
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 10px 18px;
  border-bottom: 1px solid var(--line);
  background: var(--ink-2);
  flex-wrap: wrap;
}
#topbar .brand {
  font-family: var(--display);
  font-weight: 700;
  font-size: 19px;
  letter-spacing: 0.2px;
  display: flex;
  align-items: center;
  gap: 9px;
  white-space: nowrap;
}
#topbar .brand .dot-row {
  display: flex;
  gap: 3px;
}
#topbar .brand .dot-row i {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  display: inline-block;
}
#topbar .brand .dot-row i:nth-child(1) {
  background: var(--session);
}
#topbar .brand .dot-row i:nth-child(2) {
  background: var(--episodic);
}
#topbar .brand .dot-row i:nth-child(3) {
  background: var(--semantic);
}
#topbar .brand .dot-row i:nth-child(4) {
  background: var(--contextual);
}
#topbar .brand small {
  font-family: var(--mono);
  font-size: 10.5px;
  color: var(--faint);
  font-weight: 400;
  letter-spacing: 1.2px;
  text-transform: uppercase;
}
#topbar .spacer {
  flex: 1;
}
.ctl {
  display: flex;
  align-items: center;
  gap: 6px;
  font-family: var(--mono);
  font-size: 11.5px;
  color: var(--muted);
}
.ctl select,
.ctl input[type="password"],
.ctl input[type="text"] {
  background: var(--panel);
  border: 1px solid var(--line);
  color: var(--text);
  border-radius: 6px;
  padding: 5px 8px;
  font-family: var(--mono);
  font-size: 11.5px;
  outline: none;
}
.ctl input:focus,
.ctl select:focus {
  border-color: var(--contextual);
}
.mode-pill {
  display: inline-flex;
  border: 1px solid var(--line);
  border-radius: 999px;
  overflow: hidden;
}
.mode-pill button {
  background: transparent;
  color: var(--muted);
  border: 0;
  padding: 5px 13px;
  font-family: var(--mono);
  font-size: 11.5px;
}
.mode-pill button.on {
  background: var(--contextual);
  color: #06251d;
  font-weight: 600;
}
.tbtn {
  background: var(--panel);
  color: var(--muted);
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 5px 11px;
  font-family: var(--mono);
  font-size: 11.5px;
}
.tbtn:hover {
  color: var(--text);
  border-color: var(--faint);
}
.tbtn.on {
  color: #06251d;
  border-color: var(--contextual);
  background: var(--contextual);
  font-weight: 700;
}
.tbtn.danger:hover {
  color: var(--danger);
  border-color: var(--danger);
}

#toastStack {
  position: fixed;
  right: 18px;
  bottom: 18px;
  z-index: 900;
  width: min(360px, calc(100vw - 36px));
  pointer-events: none;
  opacity: 0;
  transform: translateY(12px);
  transition:
    opacity 0.18s ease,
    transform 0.18s ease;
}
#toastStack.show {
  opacity: 1;
  transform: translateY(0);
}
.toast {
  border: 1px solid var(--line);
  background: rgba(12, 20, 29, 0.96);
  box-shadow: 0 18px 40px rgba(0, 0, 0, 0.35);
  border-radius: 8px;
  padding: 10px 12px;
}
.toast b,
.toast span {
  display: block;
}
.toast b {
  color: var(--text);
  font-family: var(--mono);
  font-size: 11px;
  margin-bottom: 3px;
}
.toast span {
  color: var(--muted);
  font-size: 12.5px;
  line-height: 1.4;
}
.toast.success {
  border-color: var(--contextual);
}
.toast.error,
.toast.danger {
  border-color: var(--danger);
}
#keyStatus {
  font-family: var(--mono);
  font-size: 10.5px;
  white-space: nowrap;
}
#keyStatus.ok {
  color: var(--contextual);
}
#keyStatus.bad {
  color: var(--danger);
}
#keyStatus.busy {
  color: var(--muted);
}

/* ---------- Main grid ---------- */
#main {
  display: grid;
  grid-template-columns: minmax(360px, 42fr) minmax(420px, 58fr);
  min-height: 0;
  gap: 14px;
  padding: 14px 18px;
}

/* ---------- Chat column ---------- */
#chatCol {
  display: grid;
  grid-template-rows: 1fr auto auto;
  min-height: 0;
  min-width: 0;
  gap: 10px;
}
#chatScroll {
  overflow-y: auto;
  min-height: 0;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--ink-2);
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 14px;
  scroll-behavior: smooth;
}

/* pipeline */
#pipeline {
  display: flex;
  align-items: stretch;
  gap: 6px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--ink-2);
  padding: 8px 10px;
  overflow-x: auto;
}
.stage {
  flex: 1 0 auto;
  min-width: 92px;
  max-width: 150px;
  display: flex;
  flex-direction: column;
  gap: 2px;
  padding: 6px 8px;
  border-radius: 7px;
  border: 1px solid transparent;
  opacity: 0.45;
  transition:
    opacity 0.25s,
    border-color 0.25s,
    background 0.25s;
}
.stage .s-name {
  font-family: var(--mono);
  font-size: 10px;
  letter-spacing: 0.7px;
  text-transform: uppercase;
  color: var(--muted);
  display: flex;
  align-items: center;
  gap: 5px;
  white-space: nowrap;
}
.stage .s-name i {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--faint);
  display: inline-block;
  flex: none;
}
.stage .s-note {
  font-size: 11px;
  color: var(--faint);
  line-height: 1.3;
  min-height: 14px;
}
.stage.active {
  opacity: 1;
  border-color: var(--contextual);
  background: rgba(87, 230, 196, 0.06);
}
.stage.active .s-name {
  color: var(--contextual);
}
.stage.active .s-name i {
  background: var(--contextual);
  box-shadow: 0 0 8px var(--contextual);
}
.stage.done {
  opacity: 0.85;
}
.stage.done .s-name i {
  background: var(--contextual);
}

/* composer */
#composer {
  display: flex;
  gap: 8px;
}
#composer textarea {
  flex: 1;
  resize: none;
  height: 52px;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  color: var(--text);
  font-size: 14.5px;
  padding: 13px 14px;
  outline: none;
}
#composer textarea:focus {
  border-color: var(--session);
}
#sendBtn {
  width: 92px;
  border: 0;
  border-radius: var(--radius);
  background: var(--contextual);
  color: #06251d;
  font-family: var(--display);
  font-weight: 700;
  font-size: 15px;
}
#sendBtn:disabled {
  background: var(--panel-2);
  color: var(--faint);
  cursor: wait;
}

/* ---------- Memory column ---------- */
#memCol {
  display: grid;
  grid-template-rows: auto 1fr 1fr;
  grid-template-columns: 1fr 1fr;
  grid-template-areas:
    "session session"
    "episodic semantic"
    "contextual contextual";
  gap: 14px;
  min-height: 0;
  min-width: 0;
}
.mem-panel {
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--panel);
  display: flex;
  flex-direction: column;
  min-height: 0;
  min-width: 0;
  position: relative;
  transition:
    box-shadow 0.3s,
    border-color 0.3s;
}
.mem-panel[data-type="session"] {
  grid-area: session;
  max-height: 210px;
  --mem-accent: var(--session);
  --mem-accent-rgb: 111, 211, 255;
}
.mem-panel[data-type="episodic"] {
  grid-area: episodic;
  --mem-accent: var(--episodic);
  --mem-accent-rgb: 255, 180, 84;
}
.mem-panel[data-type="semantic"] {
  grid-area: semantic;
  --mem-accent: var(--semantic);
  --mem-accent-rgb: 167, 139, 250;
}
.mem-panel[data-type="contextual"] {
  grid-area: contextual;
  --mem-accent: var(--contextual);
  --mem-accent-rgb: 87, 230, 196;
}
.mem-panel.flash-session {
  border-color: var(--session);
  box-shadow: 0 0 22px rgba(111, 211, 255, 0.25);
}
.mem-panel.flash-episodic {
  border-color: var(--episodic);
  box-shadow: 0 0 22px rgba(255, 180, 84, 0.25);
}
.mem-panel.flash-semantic {
  border-color: var(--semantic);
  box-shadow: 0 0 22px rgba(167, 139, 250, 0.28);
}
.mem-panel.flash-contextual {
  border-color: var(--contextual);
  box-shadow: 0 0 22px rgba(87, 230, 196, 0.25);
}

.mem-head {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 9px 12px;
  border-bottom: 1px solid rgba(var(--mem-accent-rgb), 0.38);
  background: linear-gradient(
      90deg,
      rgba(var(--mem-accent-rgb), 0.2),
      rgba(var(--mem-accent-rgb), 0.08) 46%,
      rgba(var(--mem-accent-rgb), 0.025)
    ),
    var(--panel);
  flex: none;
}
.mem-head .swatch {
  width: 9px;
  height: 9px;
  border-radius: 2px;
  flex: none;
  box-shadow: 0 0 12px rgba(var(--mem-accent-rgb), 0.72);
}
.mem-head h2 {
  font-family: var(--display);
  font-size: 13.5px;
  font-weight: 600;
  letter-spacing: 0.3px;
  color: var(--text);
}
.mem-head .sub {
  font-family: var(--mono);
  font-size: 10px;
  color: var(--muted);
  color: color-mix(in srgb, var(--mem-accent) 50%, var(--muted));
  letter-spacing: 0.6px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.mem-head .count {
  margin-left: auto;
  font-family: var(--mono);
  font-size: 10.5px;
  color: var(--mem-accent);
  background: rgba(var(--mem-accent-rgb), 0.1);
  border: 1px solid rgba(var(--mem-accent-rgb), 0.42);
  padding: 1px 7px;
  border-radius: 999px;
  flex: none;
}
.mem-head .whatis {
  flex: none;
  width: 20px;
  height: 20px;
  border-radius: 50%;
  border: 1px solid rgba(var(--mem-accent-rgb), 0.42);
  background: rgba(var(--mem-accent-rgb), 0.08);
  color: var(--mem-accent);
  font-family: var(--mono);
  font-size: 11px;
  line-height: 1;
}
.mem-head .whatis:hover {
  color: var(--text);
  border-color: var(--faint);
}
.mem-body {
  overflow-y: auto;
  padding: 8px;
  display: flex;
  flex-direction: column;
  gap: 6px;
  min-height: 0;
  flex: 1;
}
.empty-note {
  color: var(--faint);
  font-size: 12.5px;
  padding: 10px 8px;
  font-style: italic;
}

/* ---------- Activity log ---------- */
#logbar {
  border-top: 1px solid var(--line);
  background: var(--ink-2);
  height: 118px;
  display: grid;
  grid-template-rows: auto 1fr;
  min-height: 0;
}
#logbar .log-head {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 5px 18px 4px;
  font-family: var(--mono);
  font-size: 10px;
  letter-spacing: 1.4px;
  text-transform: uppercase;
  color: var(--faint);
  flex-wrap: wrap;
}
#logbar .log-head i {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--write);
  display: inline-block;
}
#logbar .log-head .spacer {
  flex: 1;
}
#logbar .credit {
  text-transform: none;
  letter-spacing: 0.4px;
  font-size: 10.5px;
  color: var(--faint);
  white-space: nowrap;
}
#logbar .credit a {
  color: var(--muted);
  text-decoration: underline dotted;
}
#logbar .credit a:hover {
  color: var(--contextual);
}
#dbBtn,
#clapBtn {
  background: transparent;
  border: 1px solid var(--line);
  color: var(--muted);
  border-radius: 999px;
  padding: 1px 10px;
  font-family: var(--mono);
  font-size: 10.5px;
  letter-spacing: 0.4px;
  display: inline-flex;
  align-items: center;
  gap: 5px;
  transition:
    border-color 0.15s,
    color 0.15s;
}
#clapBtn:hover {
  color: var(--danger);
  border-color: var(--danger);
}
#dbBtn:hover {
  color: var(--contextual);
  border-color: var(--contextual);
}
#clapBtn.pop {
  animation: clappop 0.3s ease-out;
}
@keyframes clappop {
  40% {
    transform: scale(1.2);
  }
}
.clap-float {
  position: fixed;
  z-index: 400;
  pointer-events: none;
  font-size: 15px;
}
#logScroll {
  overflow-y: auto;
  padding: 0 18px 8px;
  font-family: var(--mono);
  font-size: 11px;
  line-height: 1.75;
}

/* ---------- Responsive ---------- */
@media (max-width: 1020px) {
  body {
    overflow-x: hidden;
    overflow-y: auto;
  }
  #app {
    height: auto;
  }
  #main {
    grid-template-columns: 1fr;
  }
  #chatCol {
    grid-template-rows: auto auto auto;
  }
  #chatScroll {
    height: 55vh;
  }
  #memCol {
    grid-template-columns: 1fr;
    grid-template-areas: "session" "episodic" "semantic" "contextual";
    grid-template-rows: auto minmax(200px, auto) minmax(200px, auto) minmax(
        200px,
        auto
      );
  }
  .mem-panel {
    max-height: 320px;
  }
}
