@import url('https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&family=JetBrains+Mono:wght@400;500&display=swap');

* { box-sizing: border-box; }
:root {
  /* monochrome palette */
  --bg:        #000000;
  --bg-panel:  #0c0c0e;
  --bg-elev:   #141417;
  --fg:        #e7e7ea;
  --muted:     #8a8a92;

  --grey-1:    #1a1a1d;
  --grey-2:    #232327;
  --grey-3:    #2e2e34;
  --grey-4:    #3a3a42;
  --grey-5:    #4a4a54;
  --grey-6:    #66666f;

  --accent:    #c8c8d0;   /* near-white, used sparingly */
  --good:      #9ca3af;
  --warn:      #d1d5db;
  --red:       #b46a72;

  --border:    #1f1f23;
  --border-2:  #2a2a30;

  --bubble-user:    #1a1a1d;
  --bubble-assist:  #0f0f12;
  --bubble-system:  #0a0a0c;

  --radius-sm: 6px;
  --radius:    10px;
  --radius-lg: 14px;
  --pill:      999px;

  /* LCARS notch radii (asymmetric corners) */
  --notch-l:   28px 4px 4px 28px;
  --notch-r:   4px 28px 28px 4px;
}
html, body {
  height: 100%;
  margin: 0;
  background: var(--bg);
  color: var(--fg);
  font: 15px/1.55 "Inter", -apple-system, BlinkMacSystemFont, "Segoe UI", Helvetica, Arial, sans-serif;
  font-feature-settings: "cv11", "ss01", "ss03";
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}
body {
  display: grid;
  grid-template-rows: auto 1fr auto;
  height: 100vh;
}

/* === header === */
header {
  display: grid;
  grid-template-columns: auto 1fr auto auto;
  align-items: center;
  gap: 0.8rem;
  padding: 0.55rem 1.1rem 0.55rem 0;
  background: var(--bg);
  border-bottom: 1px solid var(--border);
}
header::before {
  /* LCARS L-bracket, monochrome */
  content: "";
  display: block;
  width: 110px;
  height: 36px;
  background: var(--grey-5);
  border-radius: 0 var(--pill) var(--pill) 0;
}
header h1 {
  margin: 0;
  font-family: "Inter", sans-serif;
  font-weight: 600;
  font-size: 1.15rem;
  letter-spacing: -0.01em;
  color: var(--fg);
  display: flex;
  align-items: center;
  gap: 0.55rem;
}
header h1 .env {
  font-family: "JetBrains Mono", ui-monospace, monospace;
  font-size: 0.62rem;
  font-weight: 500;
  color: var(--muted);
  background: var(--grey-1);
  border: 1px solid var(--border-2);
  border-radius: var(--radius-sm);
  padding: 2px 7px;
  letter-spacing: 0.08em;
  text-transform: uppercase;
}
.health {
  font-family: "JetBrains Mono", ui-monospace, monospace;
  font-size: 0.7rem;
  color: var(--muted);
  background: var(--grey-1);
  border: 1px solid var(--border-2);
  border-radius: var(--radius-sm);
  padding: 4px 10px;
  letter-spacing: 0.04em;
}
.health.ok  { color: var(--fg); }
.health.bad { color: var(--red); border-color: var(--red); }
.auth-status {
  min-height: 26px;
  display: flex;
  align-items: center;
  color: var(--muted);
  font-family: "JetBrains Mono", ui-monospace, monospace;
  font-size: 0.7rem;
  letter-spacing: 0.04em;
}
.auth-status.ok { text-transform: none; }
.auth-status.dev { color: var(--accent); }
.hidden { display: none !important; }
.auth-gate {
  display: grid;
  place-items: center;
  min-height: 0;
  padding: 2rem;
  background: var(--bg);
}
.auth-card {
  width: min(460px, 100%);
  padding: 1.4rem 1.5rem;
  background: var(--bg-panel);
  border: 1px solid var(--border-2);
  border-left: 6px solid var(--grey-5);
  border-radius: var(--notch-l);
}
.auth-card h2 {
  margin: 0 0 0.4rem;
  font-family: "Inter", sans-serif;
  font-weight: 600;
  font-size: 1.1rem;
  letter-spacing: -0.01em;
  color: var(--fg);
}
.auth-card p {
  margin: 0 0 1rem;
  color: var(--muted);
}

.auth-status.clickable { cursor: pointer; }

.btn-google {
  display: flex; align-items: center; gap: 0.6rem;
  width: 100%;
  padding: 0.55rem 0.9rem;
  border: 1px solid var(--border-2);
  border-radius: var(--notch-s, 4px);
  background: var(--bg);
  color: var(--fg);
  font-family: inherit;
  font-size: 0.95rem;
  cursor: pointer;
}
.btn-google:hover { border-color: var(--grey-6); }
.btn-google .g-icon {
  display: inline-grid; place-items: center;
  width: 22px; height: 22px;
  border-radius: 50%;
  background: linear-gradient(135deg, #4285f4, #34a853 33%, #fbbc04 66%, #ea4335);
  color: #fff;
  font-weight: 700;
  font-size: 0.78rem;
}

.auth-divider {
  display: flex; align-items: center; gap: 0.6rem;
  margin: 0.9rem 0;
  color: var(--muted);
  font-size: 0.8rem;
}
.auth-divider::before,
.auth-divider::after {
  content: ""; flex: 1; height: 1px; background: var(--border);
}

.auth-tabs {
  display: flex; gap: 0.4rem;
  margin-bottom: 0.6rem;
}
.auth-tab {
  flex: 1;
  padding: 0.4rem 0.6rem;
  background: transparent;
  border: 1px solid transparent;
  border-bottom: 2px solid var(--border);
  color: var(--muted);
  cursor: pointer;
  font-family: inherit;
}
.auth-tab.active {
  color: var(--fg);
  border-bottom-color: var(--accent);
}

.auth-form { display: flex; flex-direction: column; gap: 0.65rem; }
.auth-form label {
  display: flex; flex-direction: column; gap: 0.25rem;
  font-size: 0.85rem;
  color: var(--muted);
}
.auth-form input,
.auth-form select {
  padding: 0.5rem 0.65rem;
  background: var(--bg);
  border: 1px solid var(--border-2);
  border-radius: var(--notch-s, 4px);
  color: var(--fg);
  font-family: inherit;
  font-size: 0.95rem;
}
.auth-form input:focus,
.auth-form select:focus {
  outline: none;
  border-color: var(--accent);
}
.auth-form button[type="submit"] {
  margin-top: 0.4rem;
  padding: 0.55rem 0.9rem;
  background: var(--accent);
  color: #fff;
  border: 0;
  border-radius: var(--notch-s, 4px);
  font-family: inherit;
  font-size: 0.95rem;
  cursor: pointer;
}
.auth-form button[type="submit"]:hover { filter: brightness(1.08); }
.auth-form small.hint {
  color: var(--muted);
  font-size: 0.75rem;
  margin-top: 0.15rem;
}

.auth-link {
  margin-top: 0.8rem;
  background: transparent;
  border: 0;
  color: var(--muted);
  font-family: inherit;
  font-size: 0.85rem;
  cursor: pointer;
  text-decoration: underline;
}

.auth-error {
  margin-top: 0.7rem;
  padding: 0.5rem 0.7rem;
  background: rgba(220, 40, 40, 0.08);
  border: 1px solid rgba(220, 40, 40, 0.35);
  border-radius: var(--notch-s, 4px);
  color: var(--red, #c44);
  font-size: 0.85rem;
}

/* === main grid === */
main {
  display: grid;
  grid-template-columns: minmax(360px, 0.68fr) minmax(700px, 1.32fr);
  grid-template-rows: 1fr;
  min-height: 0;
  overflow: hidden;
  gap: 0.7rem;
  padding: 0.7rem;
  background: var(--bg);
}

/* === thread (left) === */
#thread {
  min-height: 0;
  overflow-y: auto;
  padding: 1rem 1.1rem 0.5rem;
  background: var(--bg-panel);
  border: 1px solid var(--border);
  border-left: 6px solid var(--grey-5);
  border-radius: var(--notch-l);
}
.msg { display: flex; flex-direction: column; margin-bottom: 1rem; }
.msg.user { align-items: flex-end; }
.msg.debug {
  align-items: stretch;
  margin: -0.35rem 0 0.75rem;
}
.debug-block {
  max-width: 82%;
  background: var(--grey-1);
  border: 1px solid var(--border-2);
  border-left: 3px solid var(--grey-4);
  border-radius: 4px 12px 12px 4px;
  color: var(--muted);
  font-family: "JetBrains Mono", ui-monospace, monospace;
  font-size: 0.72rem;
}
.debug-block summary {
  cursor: pointer;
  padding: 0.4rem 0.7rem;
  color: var(--muted);
  letter-spacing: 0.02em;
}
.debug-block pre {
  max-height: 260px;
  overflow: auto;
  margin: 0;
  padding: 0.55rem 0.7rem 0.75rem;
  white-space: pre-wrap;
  border-top: 1px solid var(--border-2);
}
.msg.system .bubble {
  background: transparent;
  border: 1px dashed var(--border-2);
  color: var(--muted);
  font-size: 0.86rem;
}
.bubble {
  max-width: 82%;
  padding: 0.7rem 1rem;
  border-radius: 6px 18px 18px 18px;
  word-wrap: break-word;
  border: 1px solid var(--border);
}
.msg.user .bubble {
  background: var(--bubble-user);
  color: var(--fg);
  white-space: pre-wrap;
  border-radius: 18px 6px 18px 18px;
  border-color: var(--border-2);
  border-right: 3px solid var(--grey-5);
}
.msg.assistant .bubble {
  background: var(--bubble-assist);
  border-color: var(--border);
  border-left: 3px solid var(--grey-4);
}
.msg.assistant .bubble p { margin: 0 0 0.55rem; }
.msg.assistant .bubble p:last-child { margin-bottom: 0; }
.msg.assistant .bubble ul { margin: 0.3rem 0 0.6rem 1.2rem; padding: 0; }
.msg.assistant .bubble li { margin-bottom: 0.2rem; }
.msg.assistant .bubble strong { color: var(--fg); font-weight: 600; }
.msg.assistant .bubble code {
  background: var(--grey-1); padding: 1px 6px; border-radius: 4px;
  font-size: 0.85em; font-family: "JetBrains Mono", ui-monospace, monospace;
  color: var(--fg);
}
.msg.assistant .bubble sup {
  color: var(--accent);
  margin: 0 1px;
  font-family: "JetBrains Mono", ui-monospace, monospace;
  font-size: 0.72em;
}
.msg.assistant .bubble sup a { color: var(--accent); text-decoration: none; }
.msg.assistant .bubble sup a:hover { color: var(--fg); text-decoration: underline; }
.msg.assistant .bubble sup.fig-ref a {
  color: var(--muted);
  font-style: normal;
  font-size: 0.8em;
}

.bubble h3, .bubble h4, .bubble h5, .bubble h6 {
  margin: 1rem 0 0.4rem;
  line-height: 1.3;
  color: var(--fg);
  font-family: "Inter", sans-serif;
  letter-spacing: -0.005em;
  font-weight: 600;
}
.bubble h3 { font-size: 1.02rem; }
.bubble h4 { font-size: 0.94rem; }
.bubble h5 { font-size: 0.84rem; color: var(--muted); }
.bubble h3:first-child, .bubble h4:first-child { margin-top: 0; }

/* === gallery (right sidebar) === */
#gallery {
  min-height: 0;
  height: 100%;
  background: var(--bg-panel);
  border: 1px solid var(--border);
  border-right: 6px solid var(--grey-4);
  border-radius: var(--notch-r);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  position: relative;
}
#gallery h2 {
  margin: 0;
  padding: 0.9rem 1.1rem 0.7rem;
  font-family: "Inter", sans-serif;
  font-size: 0.85rem;
  font-weight: 600;
  color: var(--muted);
  letter-spacing: 0.04em;
  text-transform: uppercase;
  border-bottom: 1px solid var(--border);
}
#gallery-list {
  flex: 1;
  height: 100%;
  max-height: 100%;
  overflow-y: scroll;
  overscroll-behavior: contain;
  min-height: 0;
  padding: 0.75rem 0.7rem 1.5rem;
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(260px, 1fr));
  grid-auto-rows: min-content;
  align-content: start;
  gap: 0.7rem;
}
#gallery-list .empty {
  grid-column: 1 / -1;
  color: var(--muted);
  font-family: "JetBrains Mono", ui-monospace, monospace;
  font-size: 0.78rem;
  text-align: center;
  margin-top: 2rem;
  letter-spacing: 0.03em;
}
.fig-card {
  display: block;
  margin: 0;
  min-width: 0;
  background: var(--bg-elev);
  border: 1px solid var(--border-2);
  border-left: 3px solid var(--grey-4);
  border-radius: 4px 12px 12px 4px;
  overflow: hidden;
  scroll-margin: 0.5rem;
  transition: border-color 180ms ease, box-shadow 220ms ease, background 220ms ease;
}
.fig-card:hover { border-left-color: var(--grey-6); }
.fig-frame {
  display: grid;
  place-items: center;
  width: 100%;
  height: clamp(160px, 24vh, 300px);
  padding: 0;
  border: 0;
  background: #050507;
  cursor: zoom-in;
  overflow: hidden;
  transition: background 180ms ease;
}
.fig-frame:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: -2px;
}
.fig-card img {
  display: block;
  width: 100%;
  height: 100%;
  background: #050507;
  object-fit: contain;
}
.fig-card .fig-meta {
  padding: 0.55rem 0.8rem 0.7rem;
  font-size: 0.82rem;
  line-height: 1.45;
  color: var(--fg);
}
.fig-card .fig-label {
  font-family: "Inter", sans-serif;
  font-weight: 600;
  letter-spacing: -0.005em;
  color: var(--fg);
  margin-right: 0.5rem;
  font-size: 0.85rem;
}
.fig-card .fig-source-label {
  color: var(--muted);
  font-family: "JetBrains Mono", ui-monospace, monospace;
  font-size: 0.68rem;
  margin-right: 0.4rem;
  letter-spacing: 0.03em;
  text-transform: uppercase;
}
.fig-card .fig-cite {
  color: var(--muted);
  text-decoration: none;
  margin-left: 0.3rem;
  font-family: "JetBrains Mono", ui-monospace, monospace;
  font-size: 0.74rem;
}
.fig-card .fig-cite:hover { color: var(--fg); text-decoration: underline; }
.fig-card .fig-caption {
  color: var(--muted);
  margin-top: 0.45rem;
  font-size: 0.78rem;
  line-height: 1.4;
  display: -webkit-box;
  -webkit-line-clamp: 3;
  -webkit-box-orient: vertical;
  overflow: hidden;
}
.fig-card.fig-error {
  border-color: var(--red);
}
.fig-card.fig-error .fig-label,
.fig-card.fig-error .fig-caption {
  color: var(--red);
}
.gallery-zoom {
  position: fixed;
  inset: 0;
  z-index: 200;
  display: grid;
  grid-template-rows: minmax(0, 1fr);
  grid-template-columns: minmax(0, 1fr);
  place-items: stretch;
  padding: 0.75rem;
  opacity: 0;
  pointer-events: none;
  transition: opacity 140ms ease;
}
.gallery-zoom.active {
  opacity: 1;
  pointer-events: auto;
}
.gallery-zoom-backdrop {
  position: absolute;
  inset: 0;
  border: 0;
  background: rgba(0, 0, 0, 0.9);
  cursor: zoom-out;
}
.gallery-zoom-card {
  position: relative;
  z-index: 1;
  display: grid;
  grid-template-rows: minmax(0, 1fr) auto;
  width: 100%;
  height: 100%;
  min-height: 0;
  min-width: 0;
  margin: 0;
  background: #050507;
  border: 1px solid var(--grey-3);
  border-left: 4px solid var(--grey-5);
  border-radius: 5px 16px 16px 5px;
  box-shadow: 0 22px 60px rgba(0, 0, 0, 0.6);
  overflow: hidden;
  cursor: zoom-out;
}
.gallery-zoom-frame {
  display: grid;
  place-items: center;
  min-height: 0;
  padding: 0.8rem;
}
.gallery-zoom img {
  display: block;
  max-width: 100%;
  max-height: 100%;
  width: auto;
  height: auto;
  object-fit: contain;
  transition: transform 300ms cubic-bezier(.2, .8, .2, 1), opacity 120ms ease;
  will-change: transform;
  pointer-events: none;
}
.gallery-zoom-meta {
  padding: 0.7rem 0.95rem 0.85rem;
  color: var(--fg);
  background: var(--bg-panel);
  border-top: 1px solid var(--border-2);
  font-size: 0.8rem;
  line-height: 1.4;
}

/* === composer === */
#composer {
  display: grid;
  grid-template-columns: auto 1fr auto;
  gap: 0.55rem;
  align-items: stretch;
  padding: 0.55rem 1.1rem 0.75rem 0;
  background: var(--bg);
  border-top: 1px solid var(--border);
}
#composer::before {
  content: "INPUT";
  display: flex;
  align-items: center;
  justify-content: center;
  width: 110px;
  background: var(--grey-4);
  color: var(--bg);
  font-family: "Inter", sans-serif;
  font-size: 0.78rem;
  font-weight: 600;
  letter-spacing: 0.16em;
  border-radius: 0 var(--pill) var(--pill) 0;
}
#input {
  background: var(--bg-panel);
  color: var(--fg);
  border: 1px solid var(--border-2);
  border-left: 3px solid var(--grey-4);
  border-radius: 4px 14px 14px 4px;
  padding: 0.75rem 0.95rem;
  font: inherit;
  resize: vertical;
  transition: border-color 0.15s ease, background 0.15s ease;
}
#input:focus {
  outline: none;
  border-color: var(--grey-5);
  border-left-color: var(--grey-6);
  background: var(--grey-1);
}
#send {
  background: var(--grey-3);
  color: var(--fg);
  border: 1px solid var(--grey-4);
  border-radius: var(--pill);
  padding: 0 1.6rem;
  font-family: "Inter", sans-serif;
  font-size: 0.9rem;
  font-weight: 600;
  letter-spacing: 0.04em;
  cursor: pointer;
  transition: background 0.15s ease, border-color 0.15s ease;
}
#send:hover:not(:disabled) {
  background: var(--grey-4);
  border-color: var(--grey-5);
}
#send:active:not(:disabled) {
  background: var(--grey-5);
}
#send:disabled { opacity: 0.4; cursor: wait; }

/* scrollbar */
#thread::-webkit-scrollbar,
#gallery-list::-webkit-scrollbar { width: 8px; }
#thread::-webkit-scrollbar-track,
#gallery-list::-webkit-scrollbar-track { background: transparent; }
#thread::-webkit-scrollbar-thumb,
#gallery-list::-webkit-scrollbar-thumb {
  background: var(--grey-3);
  border-radius: var(--pill);
}
#thread::-webkit-scrollbar-thumb:hover,
#gallery-list::-webkit-scrollbar-thumb:hover { background: var(--grey-5); }

@media (max-width: 900px) {
  main {
    grid-template-columns: minmax(0, 1fr);
    grid-template-rows: minmax(0, 1fr) minmax(260px, 42vh);
  }
  #gallery {
    border-right: 1px solid var(--border);
    border-left: 6px solid var(--grey-4);
    border-radius: var(--notch-l);
  }
}

@media (max-width: 760px) {
  header {
    grid-template-columns: 1fr;
    padding: 0.7rem;
  }
  header::before,
  #composer::before {
    display: none;
  }
  main {
    padding: 0.45rem;
  }
  #gallery-list {
    grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
  }
  .bubble {
    max-width: 96%;
  }
  #composer {
    grid-template-columns: 1fr auto;
    padding: 0.55rem 0.7rem 0.7rem;
  }
}
