:root {
  --bg: #ffffff;
  --panel: #f7f7f8;
  --panel-hover: #ececf1;
  --border: #e5e7eb;
  --text: #111827;
  --muted: #6b7280;
  --user: #eceff3;
  --assistant: #ffffff;
  --accent: #111827;
  --danger: #b42318;
  --shadow: 0 1px 2px rgba(0, 0, 0, 0.04);
  --radius: 16px;
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; height: 100%; }
body {
  font-family: Inter, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  color: var(--text);
  background: var(--bg);
}
button, textarea, input { font: inherit; }
button { -webkit-tap-highlight-color: transparent; }
svg { width: 20px; height: 20px; display: block; fill: currentColor; }

.app-shell {
  display: flex;
  height: 100vh;
  width: 100%;
  overflow: hidden;
}

.sidebar {
  width: 260px;
  min-width: 260px;
  background: var(--panel);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  transition: margin-left 0.18s ease, transform 0.18s ease;
}
.app-shell.sidebar-hidden .sidebar {
  margin-left: -260px;
}

.sidebar-header {
  padding: 10px;
  border-bottom: 1px solid transparent;
}
.sidebar-toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}
.sidebar-toolbar-right {
  display: flex;
  align-items: center;
  gap: 4px;
}
.icon-btn {
  width: 36px;
  height: 36px;
  border: 0;
  background: transparent;
  color: #444654;
  border-radius: 10px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
}
.icon-btn:hover {
  background: var(--panel-hover);
  color: var(--text);
}
.icon-btn:active { transform: translateY(1px); }

.search-panel {
  display: none;
  padding: 10px 0 2px;
}
.search-panel.open { display: block; }
.search-panel input {
  width: 100%;
  height: 38px;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: #fff;
  padding: 0 12px;
  outline: none;
}
.search-panel input:focus {
  border-color: #c7c9d1;
}

.history-wrap {
  padding: 8px;
  overflow: auto;
  flex: 1;
}
.history-section { margin-bottom: 18px; }
.history-section.empty { display: none; }
.history-title {
  font-size: 11px;
  line-height: 1;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--muted);
  padding: 8px 8px 7px;
}
.history-list { display: flex; flex-direction: column; gap: 2px; }

.history-item {
  position: relative;
  display: grid;
  grid-template-columns: 1fr 32px;
  align-items: center;
  gap: 2px;
  border-radius: 10px;
  color: var(--text);
  border: 1px solid transparent;
}
.history-item:hover,
.history-item.active {
  background: #fff;
  border-color: rgba(0,0,0,.03);
}
.history-main {
  min-width: 0;
  padding: 9px 8px 9px 10px;
  cursor: pointer;
}
.history-label {
  font-size: 14px;
  line-height: 1.35;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.history-menu-btn {
  width: 30px;
  height: 30px;
  opacity: 0;
  border: 0;
  background: transparent;
  color: var(--muted);
  border-radius: 8px;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}
.history-item:hover .history-menu-btn,
.history-item.active .history-menu-btn,
.history-item.menu-open .history-menu-btn {
  opacity: 1;
}
.history-menu-btn:hover {
  background: var(--panel-hover);
  color: var(--text);
}
.chat-menu {
  position: absolute;
  right: 8px;
  top: 34px;
  z-index: 40;
  min-width: 154px;
  background: #fff;
  border: 1px solid var(--border);
  box-shadow: 0 10px 28px rgba(0,0,0,.12);
  border-radius: 12px;
  padding: 6px;
  display: none;
}
.history-item.menu-open .chat-menu { display: block; }
.chat-menu button {
  width: 100%;
  border: 0;
  background: transparent;
  border-radius: 9px;
  padding: 9px 10px;
  color: var(--text);
  cursor: pointer;
  text-align: left;
  font-size: 14px;
}
.chat-menu button:hover { background: var(--panel); }
.chat-menu button.delete { color: var(--danger); }

.sidebar-footer {
  padding: 14px 16px;
  border-top: 1px solid var(--border);
}
.brand-name { font-weight: 600; }
.brand-sub { color: var(--muted); font-size: 13px; margin-top: 4px; }

.main-panel {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-width: 0;
  background: var(--bg);
}
.topbar {
  height: 56px;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0 16px;
  position: relative;
}
.topbar-title {
  font-size: 14px;
  font-weight: 600;
}
.floating-sidebar-btn {
  position: absolute;
  left: 12px;
  top: 10px;
  display: none;
}
.topbar-actions {
  position: absolute;
  right: 16px;
  top: 50%;
  transform: translateY(-50%);
  display: inline-flex;
  align-items: center;
  gap: 8px;
}
.topbar-contact-link {
  width: 34px;
  height: 34px;
  border-radius: 10px;
  color: #111827;
  border: 1px solid transparent;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  text-decoration: none;
  background: transparent;
}
.topbar-contact-link svg {
  width: 18px;
  height: 18px;
  display: block;
  fill: none;
  stroke: currentColor;
}
.topbar-contact-link:hover {
  background: var(--panel);
  border-color: var(--border);
}
.app-shell.sidebar-hidden .floating-sidebar-btn {
  display: inline-flex;
}

.chat-stage {
  flex: 1;
  overflow: auto;
  padding: 0 20px 28px;
}
.welcome-screen {
  min-height: calc(100vh - 220px);
  display: flex;
  align-items: center;
  justify-content: center;
}
.welcome-title {
  font-size: 28px;
  font-weight: 500;
  text-align: center;
}
.messages {
  max-width: 860px;
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  gap: 18px;
  padding-top: 16px;
}
.message-row { display: flex; }
.message-row.user { justify-content: flex-end; }
.message-row.assistant { justify-content: flex-start; }
.message-bubble {
  max-width: 80%;
  padding: 16px 18px;
  border-radius: 18px;
  line-height: 1.6;
  box-shadow: var(--shadow);
  border: 1px solid var(--border);
  white-space: pre-wrap;
  word-wrap: break-word;
}
.message-row.user .message-bubble { background: var(--user); }
.message-row.assistant .message-bubble { background: var(--assistant); }
.message-bubble a {
  color: #0f5cc0;
  text-decoration: none;
}
.message-bubble a:hover { text-decoration: underline; }
.loading-dots::after {
  content: '...';
  animation: dots 1.2s infinite;
}

.message-bubble.context-thinking {
  color: #8a8f98;
  background: transparent;
  border-color: transparent;
  box-shadow: none;
  padding-left: 4px;
  font-size: 14px;
}
@keyframes dots {
  0% { content: '.'; }
  33% { content: '..'; }
  66% { content: '...'; }
}

.composer-wrap {
  padding: 0 20px 18px;
}
.composer-form {
  max-width: 860px;
  margin: 0 auto;
}
.composer-box {
  min-height: 58px;
  border: 1px solid var(--border);
  border-radius: 26px;
  background: #fff;
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 10px 10px 18px;
  box-shadow: var(--shadow);
}
.composer-box textarea {
  flex: 1;
  border: 0;
  outline: none;
  resize: none;
  min-height: 24px;
  max-height: 180px;
  height: 24px;
  line-height: 24px;
  padding: 0;
  margin: 0;
  background: transparent;
  color: var(--text);
  display: block;
}
.composer-box textarea::placeholder {
  color: #8a8f98;
}
.composer-box button {
  width: 40px;
  height: 40px;
  border: 0;
  background: var(--accent);
  color: #fff;
  border-radius: 14px;
  padding: 0;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  flex: 0 0 auto;
}
.composer-box button svg {
  width: 21px;
  height: 21px;
}
.composer-box button:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}
.composer-note {
  max-width: 860px;
  margin: 8px auto 0;
  color: var(--muted);
  font-size: 12px;
  text-align: center;
}

@media (max-width: 900px) {
  .sidebar {
    position: fixed;
    left: 0;
    top: 0;
    bottom: 0;
    z-index: 50;
    transform: translateX(-100%);
    margin-left: 0 !important;
  }
  .sidebar.open { transform: translateX(0); }
  .app-shell.sidebar-hidden .floating-sidebar-btn,
  .floating-sidebar-btn { display: inline-flex; }
  .message-bubble { max-width: 92%; }
}


body.modal-open { overflow: hidden; }
.link-modal {
  position: fixed;
  inset: 0;
  z-index: 99999;
  display: none;
  align-items: center;
  justify-content: center;
  padding: 20px;
  background: rgba(0,0,0,.52);
}
.link-modal.open { display: flex; }
.link-modal-panel {
  width: min(760px, 96vw);
  height: min(860px, 92vh);
  background: #fff;
  border-radius: 16px;
  overflow: hidden;
  box-shadow: 0 22px 70px rgba(0,0,0,.26);
  display: flex;
  flex-direction: column;
}
.link-modal-header {
  min-height: 52px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 10px 12px 10px 16px;
  background: #fafafa;
  border-bottom: 1px solid var(--border);
}
.link-modal-title {
  font-size: 14px;
  font-weight: 600;
  color: var(--text);
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
}
.link-modal-actions { display: flex; align-items: center; gap: 8px; }
.link-modal-open,
.link-modal-close {
  border: 1px solid var(--border);
  background: #fff;
  color: var(--text);
  border-radius: 10px;
  padding: 8px 11px;
  font-size: 13px;
  text-decoration: none;
  cursor: pointer;
  white-space: nowrap;
}
.link-modal-close { background: #111827; color: #fff; border-color: #111827; }
.link-modal-body { position: relative; flex: 1; min-height: 0; background: #fff; }
.link-modal-frame {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  border: 0;
  background: #fff;
}
.link-modal-loading {
  position: absolute;
  inset: 0;
  display: none;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 12px;
  color: var(--muted);
  font-size: 13px;
  background: #fff;
  z-index: 2;
}
.link-modal.loading .link-modal-loading { display: flex; }
.link-modal-spinner {
  width: 26px;
  height: 26px;
  border-radius: 50%;
  border: 3px solid #e5e7eb;
  border-top-color: #111827;
  animation: jgrSpin .7s linear infinite;
}
@keyframes jgrSpin { to { transform: rotate(360deg); } }
.ai-modal-link {
  cursor: pointer;
  text-decoration: underline;
  text-underline-offset: 2px;
}
@media (max-width: 760px) {
  .link-modal { padding: 8px; }
  .link-modal-panel { width: 100%; height: 94vh; border-radius: 12px; }
}


/* V0.9 link buttons: AI links are shown as clean buttons, not iframe modals */
.ai-link-button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  width: fit-content;
  max-width: 100%;
  margin: 6px 6px 6px 0;
  padding: 9px 13px;
  border-radius: 999px;
  border: 1px solid #d1d5db;
  background: #ffffff;
  color: #111827 !important;
  font-size: 13px;
  font-weight: 600;
  line-height: 1.2;
  text-decoration: none !important;
  box-shadow: 0 1px 2px rgba(0,0,0,.04);
  white-space: normal;
}
.ai-link-button:hover {
  background: #f7f7f8;
  border-color: #bfc3ca;
  text-decoration: none !important;
}


/* V1.0 stronger button links */
.ai-link-button {
  display: inline-flex !important;
  margin-top: 8px;
  margin-bottom: 8px;
  cursor: pointer;
  vertical-align: middle;
}

/* V2.8: stronger ChatGPT-like thinking state for every request */
.message-bubble.context-thinking {
  color: #6b7280;
  font-size: 13px;
  background: transparent;
  border-color: transparent;
  box-shadow: none;
}


/* V2.12: Jagarasa Pintar thinking logo */
.message-bubble.context-thinking {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  color: #6b7280;
  font-size: 13px;
  background: transparent;
  border-color: transparent;
  box-shadow: none;
}
.context-thinking .thinking-logo {
  width: 20px;
  height: 20px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  flex: 0 0 20px;
}
.context-thinking .thinking-logo img {
  width: 20px;
  height: 20px;
  object-fit: contain;
  animation: jagarasaThinkingSpin 1.8s linear infinite, jagarasaThinkingPulse 1.2s ease-in-out infinite;
  transform-origin: 50% 50%;
}
.context-thinking .thinking-copy {
  display: inline-flex;
  align-items: center;
}
@keyframes jagarasaThinkingSpin {
  from { transform: rotate(0deg); }
  to { transform: rotate(360deg); }
}
@keyframes jagarasaThinkingPulse {
  0%, 100% { opacity: .45; filter: grayscale(.15); }
  50% { opacity: 1; filter: grayscale(0); }
}



/* V2.20 MOBILE VIEWPORT FIT FIX - CSS ONLY
   Keep mobile layout inside one screen: fixed app viewport, scroll only chat-stage,
   and keep composer at the bottom without shifting with message content. */
@media (max-width: 760px) {
  html,
  body {
    width: 100%;
    height: 100%;
    min-height: 100%;
    overflow: hidden;
    position: fixed;
    inset: 0;
    overscroll-behavior: none;
    touch-action: manipulation;
  }

  .app-shell {
    width: 100%;
    height: 100vh;
    height: 100svh;
    height: 100dvh;
    max-height: 100vh;
    max-height: 100svh;
    max-height: 100dvh;
    overflow: hidden;
  }

  .main-panel {
    width: 100%;
    height: 100%;
    min-height: 0;
    overflow: hidden;
  }

  .topbar {
    height: 50px;
    min-height: 50px;
    flex: 0 0 50px;
    padding: 0 112px 0 54px;
    border-bottom: 1px solid rgba(229, 231, 235, .72);
    background: #fff;
  }

  .floating-sidebar-btn {
    left: 10px;
    top: 7px;
  }

  .topbar-actions {
    right: 10px;
    gap: 4px;
  }

  .topbar-contact-link {
    width: 32px;
    height: 32px;
    border-radius: 9px;
  }

  .topbar-contact-link svg {
    width: 17px;
    height: 17px;
  }

  .chat-stage {
    flex: 1 1 auto;
    min-height: 0;
    overflow-y: auto;
    overflow-x: hidden;
    -webkit-overflow-scrolling: touch;
    overscroll-behavior: contain;
    padding: 0 12px 12px;
  }

  .welcome-screen {
    min-height: 100%;
    height: 100%;
    padding: 20px 10px;
  }

  .welcome-title {
    font-size: 22px;
    line-height: 1.28;
  }

  .messages {
    max-width: 100%;
    gap: 12px;
    padding-top: 12px;
    padding-bottom: 4px;
  }

  .message-bubble {
    max-width: 94%;
    padding: 12px 14px;
    border-radius: 16px;
    font-size: 14px;
    line-height: 1.55;
  }

  .message-row.assistant .message-bubble {
    max-width: 100%;
  }

  .composer-wrap {
    flex: 0 0 auto;
    width: 100%;
    padding: 8px 10px calc(10px + env(safe-area-inset-bottom));
    background: #fff;
    border-top: 1px solid rgba(229, 231, 235, .72);
    position: sticky;
    bottom: 0;
    z-index: 20;
  }

  .composer-form {
    max-width: 100%;
  }

  .composer-box {
    min-height: 48px;
    border-radius: 22px;
    gap: 8px;
    padding: 8px 8px 8px 14px;
  }

  .composer-box textarea {
    min-height: 22px;
    max-height: 96px;
    height: 22px;
    line-height: 22px;
    font-size: 15px;
  }

  .composer-box button {
    width: 36px;
    height: 36px;
    border-radius: 13px;
  }

  .composer-note {
    display: none;
  }

  .sidebar {
    width: min(86vw, 300px);
    min-width: min(86vw, 300px);
    height: 100vh;
    height: 100svh;
    height: 100dvh;
    max-height: 100vh;
    max-height: 100svh;
    max-height: 100dvh;
  }
}

@media (max-width: 380px) {
  .topbar-title {
    font-size: 13px;
  }

  .topbar {
    padding-right: 104px;
  }

  .topbar-actions {
    gap: 3px;
    right: 8px;
  }

  .topbar-contact-link {
    width: 30px;
    height: 30px;
    border-radius: 8px;
  }

  .topbar-contact-link svg {
    width: 16px;
    height: 16px;
  }

  .welcome-title {
    font-size: 20px;
  }

  .message-bubble {
    font-size: 13.5px;
    padding: 11px 12px;
  }

  .composer-wrap {
    padding-left: 8px;
    padding-right: 8px;
  }
}
