/* ==========================================================================
   JANCOK.AI — CHATBOT APPLICATION STYLESHEET (MOBILE-PERFECTED)
   Strictly conforming to DESIGN.md
   ========================================================================== */

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

:root {
  --color-midnight-carbon: #12130f;
  --color-bone-glow: #e4dfda;
  --color-charcoal-vein: #3c3c38;
  --color-rose-quartz-bloom: #f5c2c8;

  --font-display: 'Syne Mono', monospace;
  --font-mono: 'JetBrains Mono', monospace;
  --font-sans: 'Space Grotesk', -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;

  --radius-full: 9999px;
  --radius-sm: 2px;
  --radius-card: 0px;

  --sidebar-width: 280px;
}

*, *::before, *::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

/* Fixed viewport container to prevent mobile elastic bounce */
html, body {
  width: 100%;
  height: 100%;
  height: 100dvh;
  position: fixed;
  inset: 0;
  overflow: hidden;
  background-color: var(--color-midnight-carbon);
  color: var(--color-bone-glow);
  font-family: var(--font-mono);
  font-size: 15px;
  line-height: 1.45;
  -webkit-text-size-adjust: 100%;
}

::selection {
  background-color: var(--color-bone-glow);
  color: var(--color-midnight-carbon);
}

/* App Layout Grid */
.chat-app-layout {
  display: flex;
  width: 100%;
  height: 100%;
  height: 100dvh;
  position: relative;
  overflow: hidden;
}

/* Backdrop for Mobile Sidebar */
.sidebar-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.75);
  backdrop-filter: blur(2px);
  -webkit-backdrop-filter: blur(2px);
  z-index: 80;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.25s ease;
}

.sidebar-backdrop.active {
  opacity: 1;
  pointer-events: auto;
}

@media (min-width: 769px) {
  .sidebar-backdrop {
    display: none !important;
  }
}

/* ==========================================================================
   SIDEBAR
   ========================================================================== */
.chat-sidebar {
  width: var(--sidebar-width);
  height: 100%;
  height: 100dvh;
  background: #0d0e0a;
  border-right: 1px solid var(--color-charcoal-vein);
  display: flex;
  flex-direction: column;
  flex-shrink: 0;
  z-index: 90;
  transition: transform 0.25s cubic-bezier(0.16, 1, 0.3, 1);
}

.sidebar-header {
  padding: 16px;
  border-bottom: 1px solid var(--color-charcoal-vein);
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.brand-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.sidebar-brand {
  font-family: var(--font-display);
  font-size: 20px;
  letter-spacing: -0.04em;
  color: var(--color-bone-glow);
  text-decoration: none;
}

.home-btn {
  font-size: 11px;
  opacity: 0.7;
  text-decoration: none;
  padding: 4px 8px;
  border: 1px solid var(--color-charcoal-vein);
  border-radius: var(--radius-full);
  color: var(--color-bone-glow);
  background: transparent;
  cursor: pointer;
}

.home-btn:hover {
  opacity: 1;
  border-color: var(--color-bone-glow);
}

.sidebar-close-btn {
  display: none;
  background: transparent;
  border: 1px solid var(--color-charcoal-vein);
  color: var(--color-bone-glow);
  border-radius: var(--radius-full);
  width: 28px;
  height: 28px;
  font-size: 18px;
  line-height: 1;
  cursor: pointer;
  align-items: center;
  justify-content: center;
}

.new-chat-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  width: 100%;
  padding: 10px 16px;
  border-radius: var(--radius-full);
  background: transparent;
  color: var(--color-bone-glow);
  border: 1px solid var(--color-charcoal-vein);
  font-family: var(--font-mono);
  font-size: 12px;
  letter-spacing: -0.04em;
  cursor: pointer;
  transition: all 0.2s ease;
}

.new-chat-btn:hover {
  background: var(--color-bone-glow);
  color: var(--color-midnight-carbon);
  border-color: var(--color-bone-glow);
}

.chat-history-list {
  flex: 1;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding: 12px 8px;
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.history-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 12px;
  border-radius: var(--radius-sm);
  background: transparent;
  color: var(--color-bone-glow);
  cursor: pointer;
  font-size: 13px;
  transition: background 0.15s ease;
  user-select: none;
}

.history-item:hover {
  background: rgba(228, 223, 218, 0.06);
}

.history-item.active {
  background: rgba(228, 223, 218, 0.12);
  border-left: 2px solid var(--color-rose-quartz-bloom);
}

.history-title {
  flex: 1;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  margin-right: 8px;
}

.history-delete {
  opacity: 0.3;
  cursor: pointer;
  font-size: 14px;
  padding: 2px 6px;
}

.history-delete:hover {
  opacity: 1;
  color: #ff6b6b;
}

.sidebar-footer {
  padding: 16px;
  border-top: 1px solid var(--color-charcoal-vein);
  display: flex;
  flex-direction: column;
  gap: 12px;
  background: #090a07;
}

.model-selector-wrap {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.model-select {
  width: 100%;
  padding: 8px 10px;
  background: #141511;
  color: var(--color-bone-glow);
  border: 1px solid var(--color-charcoal-vein);
  border-radius: var(--radius-sm);
  font-family: var(--font-mono);
  font-size: 11px;
  outline: none;
}

.sidebar-telemetry-badge {
  font-size: 10px;
  color: var(--color-bone-glow);
  opacity: 0.85;
  text-transform: uppercase;
  letter-spacing: -0.04em;
  display: flex;
  flex-direction: column;
  gap: 3px;
}

.telemetry-vital-row {
  display: flex;
  align-items: center;
  gap: 5px;
  color: var(--color-bone-glow);
  font-weight: 500;
}

.heartbeat-pulse-icon {
  display: inline-block;
  color: #ff5b77;
  font-size: 11px;
  line-height: 1;
  animation: heartPulse 1.25s cubic-bezier(0.215, 0.61, 0.355, 1) infinite;
}

@keyframes heartPulse {
  0% { transform: scale(1); }
  14% { transform: scale(1.35); }
  28% { transform: scale(1); }
  42% { transform: scale(1.2); }
  70% { transform: scale(1); }
}

.telemetry-sub-row {
  font-size: 9px;
  opacity: 0.6;
}

.telemetry-sub-row.secure-tag {
  color: var(--color-rose-quartz-bloom);
  opacity: 0.9;
}

.memory-toggle {
  display: flex;
  align-items: center;
  gap: 8px;
  width: 100%;
  padding: 8px 12px;
  border-radius: var(--radius-full);
  background: transparent;
  border: 1px solid var(--color-charcoal-vein);
  color: var(--color-bone-glow);
  font-family: var(--font-mono);
  font-size: 10px;
  letter-spacing: -0.04em;
  cursor: pointer;
  transition: border-color 0.15s ease, opacity 0.15s ease;
}

.memory-toggle:hover {
  border-color: var(--color-bone-glow);
}

.memory-toggle-dot {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--color-charcoal-vein);
  flex-shrink: 0;
}

.memory-toggle-label {
  flex: 1;
  text-align: left;
}

.memory-toggle-state {
  font-weight: 600;
  opacity: 0.6;
}

.memory-toggle.is-on {
  border-color: var(--color-rose-quartz-bloom);
}

.memory-toggle.is-on .memory-toggle-dot {
  background: #39e58c;
}

.memory-toggle.is-on .memory-toggle-state {
  color: var(--color-rose-quartz-bloom);
  opacity: 1;
}

/* ==========================================================================
   MAIN CHAT WORKSPACE (CRITICAL FOR MOBILE STABILITY)
   ========================================================================== */
.chat-workspace {
  flex: 1;
  display: flex;
  flex-direction: column;
  height: 100%;
  height: 100dvh;
  min-height: 0; /* Prevents overflow push */
  max-height: 100dvh;
  overflow: hidden;
  position: relative;
  background: var(--color-midnight-carbon);
}

/* Chat Topbar */
.chat-topbar {
  height: 52px;
  border-bottom: 1px solid var(--color-charcoal-vein);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 16px;
  background: #11120e;
  flex-shrink: 0;
  z-index: 20;
}

.topbar-left {
  display: flex;
  align-items: center;
  gap: 10px;
  overflow: hidden;
}

.mobile-menu-btn {
  display: none;
  background: transparent;
  border: 1px solid var(--color-charcoal-vein);
  color: var(--color-bone-glow);
  border-radius: var(--radius-full);
  padding: 4px 10px;
  font-family: var(--font-mono);
  font-size: 11px;
  cursor: pointer;
  white-space: nowrap;
}

.active-chat-title {
  font-size: 13px;
  font-weight: 500;
  letter-spacing: -0.03em;
  color: var(--color-bone-glow);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.topbar-right {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-shrink: 0;
}

.context-counter-pill {
  font-size: 10px;
  padding: 3px 8px;
  border-radius: var(--radius-full);
  border: 1px solid var(--color-charcoal-vein);
  opacity: 0.8;
  white-space: nowrap;
}

/* Messages Canvas */
.chat-messages-container {
  flex: 1;
  min-height: 0; /* Mandatory for flex scrolling */
  overflow-y: auto;
  overflow-x: hidden;
  -webkit-overflow-scrolling: touch;
  overscroll-behavior-y: contain;
  padding: 16px 16px 24px;
  display: flex;
  flex-direction: column;
  gap: 20px;
}

/* Empty Hero State */
.chat-empty-hero {
  margin: auto;
  max-width: 600px;
  width: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  text-align: center;
  gap: 14px;
  padding: 24px 12px;
}

.empty-brand-title {
  font-family: var(--font-display);
  font-size: clamp(28px, 6vw, 44px);
  color: var(--color-bone-glow);
  letter-spacing: -0.04em;
  line-height: 1;
}

.empty-brand-sub {
  font-family: var(--font-sans);
  font-size: 13px;
  opacity: 0.75;
  line-height: 1.5;
  max-width: 480px;
}

.suggestion-chips {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  justify-content: center;
  margin-top: 12px;
  width: 100%;
}

.suggest-btn {
  padding: 8px 14px;
  border-radius: var(--radius-full);
  background: transparent;
  color: var(--color-bone-glow);
  border: 1px solid var(--color-charcoal-vein);
  font-family: var(--font-mono);
  font-size: 12px;
  cursor: pointer;
  transition: all 0.15s ease;
  text-align: left;
}

.suggest-btn:hover {
  background: var(--color-bone-glow);
  color: var(--color-midnight-carbon);
  border-color: var(--color-bone-glow);
}

/* Message Rows */
.chat-msg-row {
  display: flex;
  flex-direction: column;
  max-width: 840px;
  width: 100%;
  margin: 0 auto;
  gap: 6px;
}

.chat-msg-header {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 11px;
  letter-spacing: -0.04em;
  opacity: 0.6;
}

.chat-msg-header .badge-bot {
  color: var(--color-rose-quartz-bloom);
  font-weight: 600;
  opacity: 1;
}

.chat-msg-body {
  font-family: var(--font-sans);
  font-size: 15px;
  line-height: 1.6;
  color: var(--color-bone-glow);
  word-break: break-word;
}

/* User Message: right side, Bone Glow filled bubble */
.msg-user {
  align-items: flex-end;
}

.msg-user .chat-msg-header {
  padding-right: 4px;
}

.msg-user .chat-msg-body {
  background: var(--color-bone-glow);
  color: var(--color-midnight-carbon);
  border: 1px solid var(--color-bone-glow);
  padding: 10px 14px;
  border-radius: 16px 16px 4px 16px;
  max-width: min(620px, 82%);
  white-space: pre-wrap;
}

.msg-user .chat-msg-body ::selection {
  background: var(--color-midnight-carbon);
  color: var(--color-bone-glow);
}

.msg-user .chat-msg-image {
  border-color: var(--color-midnight-carbon);
}

/* Assistant Message: left side, carbon bubble with Charcoal Vein border */
.msg-assistant {
  align-items: flex-start;
}

.msg-assistant .chat-msg-header {
  padding-left: 4px;
}

.msg-assistant .chat-msg-body {
  background: #191b15;
  border: 1px solid var(--color-charcoal-vein);
  padding: 12px 16px;
  border-radius: 16px 16px 16px 4px;
  max-width: min(720px, 88%);
  min-width: 0;
}

/* Markdown Formatted Elements */
.chat-msg-body p {
  margin-bottom: 12px;
}

.chat-msg-body p:last-child {
  margin-bottom: 0;
}

.chat-msg-body ul, .chat-msg-body ol {
  margin-left: 20px;
  margin-bottom: 12px;
}

.chat-msg-body code:not(pre code) {
  background: #1c1d18;
  border: 1px solid var(--color-charcoal-vein);
  padding: 2px 6px;
  border-radius: 2px;
  font-family: var(--font-mono);
  font-size: 13px;
  color: var(--color-bone-glow);
}

.chat-code-block {
  background: #090a07;
  border: 1px solid var(--color-charcoal-vein);
  border-radius: var(--radius-card);
  margin: 12px 0;
  overflow: hidden;
  max-width: 100%;
}

.code-block-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 6px 12px;
  background: #141511;
  border-bottom: 1px solid var(--color-charcoal-vein);
  font-family: var(--font-mono);
  font-size: 11px;
}

.code-copy-btn {
  background: transparent;
  border: 1px solid var(--color-charcoal-vein);
  color: var(--color-bone-glow);
  border-radius: var(--radius-full);
  padding: 3px 8px;
  font-size: 10px;
  cursor: pointer;
  transition: all 0.15s ease;
}

.code-copy-btn:hover {
  background: var(--color-bone-glow);
  color: var(--color-midnight-carbon);
}

.chat-code-block pre {
  padding: 12px;
  overflow-x: auto;
  font-family: var(--font-mono);
  font-size: 13px;
  line-height: 1.5;
  color: var(--color-bone-glow);
}

/* Streaming Blinking Cursor */
.streaming-cursor {
  display: inline-block;
  width: 7px;
  height: 15px;
  background: var(--color-bone-glow);
  vertical-align: middle;
  margin-left: 4px;
  animation: blink-cursor 0.8s infinite;
}

@keyframes blink-cursor {
  0%, 100% { opacity: 0; }
  50% { opacity: 1; }
}

/* Real-time Thinking Indicator */
.thinking-box {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 6px 12px;
  background: #151612;
  border: 1px solid var(--color-charcoal-vein);
  border-radius: var(--radius-full);
  font-size: 11px;
  color: var(--color-rose-quartz-bloom);
  font-family: var(--font-mono);
  margin-bottom: 8px;
  letter-spacing: -0.03em;
  max-width: 100%;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.thinking-pulse-dot {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--color-rose-quartz-bloom);
  animation: pulse-glow 1.2s infinite ease-in-out;
  flex-shrink: 0;
}

@keyframes pulse-glow {
  0%, 100% { opacity: 0.3; transform: scale(0.9); }
  50% { opacity: 1; transform: scale(1.1); }
}

/* ==========================================================================
   INPUT CONTAINER (ANCHORED & RESILIENT ON MOBILE)
   ========================================================================== */
.chat-input-area {
  flex-shrink: 0;
  position: relative;
  z-index: 40;
  width: 100%;
  background: #0e0f0b;
  border-top: 1px solid var(--color-charcoal-vein);
  padding: 8px 16px max(12px, env(safe-area-inset-bottom));
}

.chat-input-wrapper {
  max-width: 840px;
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.chat-input-box {
  display: flex;
  align-items: flex-end;
  background: #141511;
  border: 1px solid var(--color-charcoal-vein);
  border-radius: var(--radius-card);
  padding: 8px 12px;
  gap: 10px;
  transition: border-color 0.15s ease;
}

.chat-input-box:focus-within {
  border-color: var(--color-bone-glow);
}

/* 16px font-size prevents iOS Safari from automatically zooming into the page */
#chat-textarea {
  flex: 1;
  background: transparent;
  border: none;
  outline: none;
  font-family: var(--font-sans);
  font-size: 16px;
  line-height: 1.4;
  color: var(--color-bone-glow);
  resize: none;
  max-height: 140px;
  min-height: 24px;
}

#chat-textarea::placeholder {
  color: var(--color-bone-glow);
  opacity: 0.35;
}

.input-actions {
  display: flex;
  gap: 6px;
  flex-shrink: 0;
}

.send-btn, .stop-btn {
  padding: 6px 14px;
  border-radius: var(--radius-full);
  background: var(--color-bone-glow);
  color: var(--color-midnight-carbon);
  border: 1px solid var(--color-bone-glow);
  font-family: var(--font-mono);
  font-size: 12px;
  cursor: pointer;
  font-weight: 500;
  transition: opacity 0.15s ease;
}

.send-btn:hover {
  opacity: 0.85;
}

.stop-btn {
  background: #ff6b6b;
  border-color: #ff6b6b;
  color: #fff;
  display: none;
}

.input-footnote {
  display: flex;
  justify-content: space-between;
  font-size: 10px;
  color: var(--color-bone-glow);
  opacity: 0.4;
  letter-spacing: -0.04em;
  padding: 0 4px;
}

/* ==========================================================================
   IMAGE ATTACHMENTS
   ========================================================================== */
.attach-btn {
  flex-shrink: 0;
  width: 32px;
  height: 32px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border-radius: var(--radius-full);
  background: transparent;
  color: var(--color-bone-glow);
  border: 1px solid var(--color-charcoal-vein);
  cursor: pointer;
  opacity: 0.75;
  transition: opacity 0.15s ease, border-color 0.15s ease;
}

.attach-btn:hover,
.attach-btn.has-file {
  opacity: 1;
  border-color: var(--color-rose-quartz-bloom);
}

.attach-btn:disabled {
  opacity: 0.3;
  cursor: not-allowed;
}

.chat-input-box.drag-over {
  border-color: var(--color-rose-quartz-bloom);
  border-style: dashed;
}

.attach-preview {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 6px 8px;
  border: 1px solid var(--color-charcoal-vein);
  border-radius: var(--radius-sm);
  background: #141511;
  align-self: flex-start;
  max-width: 100%;
}

.attach-preview[hidden] {
  display: none;
}

.attach-preview img {
  width: 52px;
  height: 52px;
  object-fit: cover;
  border-radius: var(--radius-sm);
  border: 1px solid var(--color-charcoal-vein);
}

.attach-preview-meta {
  display: flex;
  flex-direction: column;
  gap: 2px;
  font-family: var(--font-mono);
  font-size: 10px;
  letter-spacing: -0.04em;
  color: var(--color-bone-glow);
}

.attach-preview-label {
  color: var(--color-rose-quartz-bloom);
}

.attach-preview-size {
  opacity: 0.55;
}

.attach-remove-btn {
  width: 26px;
  height: 26px;
  border-radius: var(--radius-full);
  background: transparent;
  border: 1px solid var(--color-charcoal-vein);
  color: var(--color-bone-glow);
  font-size: 16px;
  line-height: 1;
  cursor: pointer;
}

.attach-remove-btn:hover {
  border-color: #ff6b6b;
  color: #ff6b6b;
}

.attach-error {
  font-family: var(--font-mono);
  font-size: 11px;
  color: #ff6b6b;
  padding: 0 4px;
}

.chat-msg-image {
  display: block;
  max-width: min(320px, 70vw);
  max-height: 320px;
  width: auto;
  height: auto;
  border-radius: var(--radius-sm);
  border: 1px solid var(--color-charcoal-vein);
  margin-bottom: 8px;
  cursor: zoom-in;
}

.chat-msg-image.expanded {
  max-width: 100%;
  max-height: none;
  cursor: zoom-out;
}

.chat-msg-image-missing {
  font-family: var(--font-mono);
  font-size: 11px;
  opacity: 0.55;
  margin-bottom: 6px;
}

/* ==========================================================================
   MOBILE RESPONSIVENESS OVERRIDES
   ========================================================================== */
@media (max-width: 768px) {
  .chat-sidebar {
    position: fixed;
    top: 0;
    left: 0;
    bottom: 0;
    width: min(320px, 85vw);
    transform: translateX(-100%);
    box-shadow: none;
  }

  .chat-sidebar.open {
    transform: translateX(0);
    box-shadow: 10px 0 40px rgba(0, 0, 0, 0.9);
  }

  .sidebar-close-btn {
    display: inline-flex;
  }

  .mobile-menu-btn {
    display: block;
  }

  .context-counter-pill {
    display: none; /* Hide turn counter on very small screens to save header space */
  }

  .chat-messages-container {
    padding: 12px 12px 18px;
    gap: 16px;
  }

  .msg-user .chat-msg-body {
    max-width: 85%;
    font-size: 14px;
  }

  .msg-assistant .chat-msg-body {
    max-width: 92%;
    padding: 10px 13px;
    font-size: 14px;
  }

  .chat-input-area {
    padding: 6px 10px max(10px, env(safe-area-inset-bottom));
  }

  .suggest-btn {
    width: 100%;
    font-size: 12px;
    padding: 10px 12px;
  }

  .input-footnote {
    font-size: 9px;
  }
}
