:root {
  color-scheme: dark;
  --bg: #050c18;
  --bar-bg: #07111ecc;
  --border: rgba(154, 171, 204, 0.16);
  --text: #eef4ff;
  --muted: #9aabc8;
  --accent: #69a8ff;
  --shadow: 0 8px 24px rgba(0, 0, 0, 0.24);
}

* {
  box-sizing: border-box;
}

html,
body {
  height: 100%;
}

html {
  /* 삼성 인터넷 등에서 모바일 글자 자동 확대가 켜지면 화면이 110%처럼 넓어 보일 수 있다. */
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
}

body {
  margin: 0;
  overflow: hidden;
  font-family: "Segoe UI", "Malgun Gothic", sans-serif;
  background: var(--bg);
  color: var(--text);
}

button,
a,
iframe {
  font: inherit;
}

.app-shell {
  height: 100%;
  display: grid;
  grid-template-rows: auto 1fr;
}

.topbar {
  display: flex;
  align-items: center;
  gap: 8px;
  min-height: 48px;
  padding: 6px 10px;
  background: var(--bar-bg);
  backdrop-filter: blur(18px);
  border-bottom: 1px solid var(--border);
  box-shadow: var(--shadow);
  z-index: 10;
}

.toolbar {
  display: flex;
  gap: 6px;
  flex-shrink: 0;
}

.icon-button,
.icon-link,
.tab-button {
  border: 1px solid var(--border);
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.05);
  color: var(--text);
  text-decoration: none;
  transition:
    transform 0.16s ease,
    border-color 0.16s ease,
    background 0.16s ease;
}

.icon-button {
  cursor: pointer;
}

.icon-button:hover,
.icon-link:hover,
.tab-button:hover {
  transform: translateY(-1px) scale(1.01);
  border-color: rgba(106, 168, 255, 0.55);
  background: rgba(255, 255, 255, 0.08);
}

.tabbar {
  display: flex;
  flex: 1;
  min-width: 0;
  gap: 6px;
  overflow-x: auto;
  scrollbar-width: none;
}

.tabbar::-webkit-scrollbar {
  display: none;
}

.tab-button {
  padding: 6px 11px;
  min-height: 32px;
  font-size: 0.82rem;
  line-height: 1;
  white-space: nowrap;
  cursor: pointer;
}

.tab-button.active {
  background: linear-gradient(135deg, rgba(79, 142, 247, 0.94), rgba(88, 183, 255, 0.9));
  border-color: transparent;
  color: #031325;
  font-weight: 700;
}

.icon-button,
.icon-link {
  width: 32px;
  height: 32px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 0;
  flex-shrink: 0;
}

.viewer {
  min-height: 0;
  overflow: hidden;
}

.panels {
  height: 100%;
  min-height: 0;
  position: relative;
}

.panel {
  display: none;
  height: 100%;
}

.panel.active {
  display: block;
}

.panel-frame {
  width: 100%;
  height: 100%;
  border: 0;
  background: #ffffff;
}

.chatbot-fab {
  position: fixed;
  right: 18px;
  bottom: 18px;
  z-index: 60;
  min-width: 112px;
  height: 58px;
  padding: 0 18px;
  border: 1px solid rgba(105, 168, 255, 0.34);
  border-radius: 999px;
  background: linear-gradient(135deg, rgba(79, 142, 247, 0.96), rgba(88, 183, 255, 0.92));
  color: #041223;
  font-size: 0.95rem;
  font-weight: 800;
  box-shadow: 0 18px 40px rgba(0, 0, 0, 0.34);
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  transition:
    opacity 0.22s ease,
    transform 0.22s ease,
    box-shadow 0.22s ease;
}

.chatbot-fab:hover {
  transform: translateY(-2px);
  box-shadow: 0 20px 46px rgba(0, 0, 0, 0.38);
}

.chatbot-fab-text {
  letter-spacing: 0.01em;
}

body.chatbot-drawer-open .chatbot-fab {
  opacity: 0;
  pointer-events: none;
  transform: translateY(10px);
}

.chatbot-backdrop {
  position: fixed;
  inset: 0;
  z-index: 58;
  background: rgba(3, 8, 16, 0.62);
  backdrop-filter: blur(10px);
  opacity: 0;
  transition: opacity 0.22s ease;
}

.chatbot-backdrop[hidden] {
  display: none;
}

.chatbot-backdrop.open {
  opacity: 1;
}

.chatbot-panel {
  position: fixed;
  top: 0;
  right: 0;
  bottom: 0;
  z-index: 60;
  /* fixed 요소: % 너비는 뷰포트 기준이라 100vw 보다 가로 잘림이 덜함 */
  width: min(640px, calc(100% - 16px));
  padding: 10px 0 10px 10px;
  opacity: 0;
  pointer-events: none;
  transform: translateX(calc(100% + 24px));
  transition:
    transform 0.24s ease,
    opacity 0.24s ease;
}

.chatbot-panel[hidden] {
  display: none;
}

.chatbot-panel.open {
  opacity: 1;
  pointer-events: auto;
  transform: translateX(0);
}

.chatbot-shell {
  height: 100%;
  min-height: 0;
  display: grid;
  grid-template-rows: minmax(0, 1fr) auto;
  gap: 10px;
  padding: 20px 18px 18px;
  border: 1px solid rgba(154, 171, 204, 0.18);
  border-right: 0;
  border-radius: 28px 0 0 28px;
  background:
    radial-gradient(circle at top left, rgba(111, 211, 255, 0.12), transparent 30%),
    linear-gradient(180deg, rgba(9, 21, 39, 0.98), rgba(4, 11, 24, 0.99)),
    var(--bg);
  box-shadow: -18px 0 42px rgba(0, 0, 0, 0.34);
  backdrop-filter: blur(22px);
  overflow: hidden;
}

.chatbot-meta {
  display: grid;
  gap: 8px;
}

.chatbot-meta-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  flex-wrap: wrap;
}

.chatbot-context-label,
.chatbot-status {
  margin: 0;
  display: inline-flex;
  align-items: center;
  min-height: 34px;
  padding: 0 12px;
  border: 1px solid rgba(154, 171, 204, 0.14);
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.04);
  color: var(--text);
  font-size: 0.82rem;
  line-height: 1.4;
}

.chatbot-context-label {
  font-weight: 700;
}

.chatbot-status {
  color: var(--muted);
}

.chatbot-status.error {
  color: #ffb4b4;
  border-color: rgba(255, 132, 132, 0.3);
  background: rgba(112, 24, 24, 0.28);
}

.chatbot-sources {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.chatbot-source-chip {
  display: inline-flex;
  align-items: center;
  min-height: 30px;
  padding: 0 10px;
  border: 1px solid rgba(154, 171, 204, 0.14);
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.04);
  color: var(--muted);
  font-size: 0.76rem;
  line-height: 1.3;
  white-space: nowrap;
}

.chatbot-stage {
  min-height: 0;
  display: flex;
  flex-direction: column;
  padding: 16px;
  border: 1px solid rgba(154, 171, 204, 0.12);
  border-radius: 22px;
  background: rgba(255, 255, 255, 0.03);
  overflow: hidden;
}

.chatbot-messages {
  flex: 1 1 auto;
  min-height: 0;
  display: flex;
  flex-direction: column;
  gap: 12px;
  overflow-y: auto;
  padding-right: 4px;
  overscroll-behavior: contain;
}

.chatbot-entry {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.chatbot-entry-user {
  align-items: flex-end;
}

.chatbot-entry-assistant {
  align-items: flex-start;
}

.chatbot-entry-assistant .chatbot-message {
  width: min(100%, 92%);
}

.chatbot-message {
  max-width: 100%;
  padding: 14px 16px;
  border-radius: 20px;
  line-height: 1.7;
  font-size: 0.98rem;
  white-space: pre-wrap;
  word-break: keep-all;
}

.chatbot-message.assistant {
  background: linear-gradient(180deg, rgba(255, 255, 255, 0.06), rgba(255, 255, 255, 0.04));
  border: 1px solid rgba(154, 171, 204, 0.14);
}

.chatbot-message.user {
  background: linear-gradient(135deg, rgba(79, 142, 247, 0.28), rgba(88, 183, 255, 0.22));
  border: 1px solid rgba(105, 168, 255, 0.32);
}

.chatbot-advisor-card {
  display: grid;
  gap: 9px;
  padding: 13px 14px;
  border: 1px solid rgba(154, 171, 204, 0.14);
  border-radius: 18px;
  background: linear-gradient(180deg, rgba(255, 255, 255, 0.055), rgba(255, 255, 255, 0.03));
}

.chatbot-advisor-card.has-warning {
  border-color: rgba(255, 204, 96, 0.34);
  background: linear-gradient(180deg, rgba(77, 58, 19, 0.24), rgba(255, 255, 255, 0.03));
}

.chatbot-advisor-top {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 10px;
}

.chatbot-advisor-badges {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}

.chatbot-summary-badge {
  display: inline-flex;
  align-items: center;
  min-height: 28px;
  padding: 0 9px;
  border: 1px solid rgba(154, 171, 204, 0.14);
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.04);
  color: var(--text);
  font-size: 0.74rem;
  font-weight: 700;
  line-height: 1.2;
  white-space: nowrap;
}

.chatbot-summary-badge--warning {
  border-color: rgba(255, 204, 96, 0.34);
  background: rgba(124, 89, 20, 0.28);
  color: #ffd98a;
}

.chatbot-summary-badge--stance.is-bullish {
  border-color: rgba(114, 205, 255, 0.26);
  background: rgba(44, 93, 116, 0.26);
}

.chatbot-summary-badge--stance.is-bearish {
  border-color: rgba(255, 145, 145, 0.28);
  background: rgba(106, 37, 37, 0.24);
}

.chatbot-summary-badge--stance.is-neutral {
  border-color: rgba(190, 197, 214, 0.2);
  background: rgba(255, 255, 255, 0.03);
}

.chatbot-summary-badge--stance.is-no-trade {
  border-color: rgba(255, 220, 130, 0.26);
  background: rgba(110, 91, 32, 0.22);
  color: #ffe0a2;
}

.chatbot-summary-badge--confidence {
  color: var(--muted);
}

.chatbot-advisor-next {
  color: var(--muted);
  font-size: 0.76rem;
  line-height: 1.4;
  text-align: right;
}

.chatbot-advisor-headline,
.chatbot-advisor-avoid,
.chatbot-advisor-state-summary {
  margin: 0;
}

.chatbot-advisor-headline {
  color: var(--text);
  font-size: 0.92rem;
  font-weight: 800;
  line-height: 1.5;
}

.chatbot-advisor-avoid,
.chatbot-advisor-state-summary {
  color: var(--muted);
  font-size: 0.82rem;
  line-height: 1.55;
}

.chatbot-message.streaming::after {
  content: "";
  display: inline-block;
  width: 0.55ch;
  height: 1em;
  margin-left: 2px;
  transform: translateY(2px);
  border-right: 2px solid rgba(141, 194, 255, 0.95);
  animation: chatbot-caret-blink 0.85s step-end infinite;
}

.chatbot-form {
  display: grid;
  gap: 8px;
  padding: 12px;
  border: 1px solid rgba(154, 171, 204, 0.12);
  border-radius: 22px;
  background: rgba(255, 255, 255, 0.03);
  position: relative;
  z-index: 1;
}

.chatbot-input-wrap {
  display: block;
  border: 1px solid rgba(154, 171, 204, 0.12);
  border-radius: 18px;
  background: rgba(255, 255, 255, 0.04);
}

.chatbot-input {
  width: 100%;
  min-height: 68px;
  max-height: 128px;
  resize: none;
  border: 0;
  border-radius: 18px;
  background: transparent;
  color: var(--text);
  padding: 10px 12px;
  line-height: 1.5;
  font-size: 0.96rem;
  overflow-y: auto;
}

.chatbot-input:focus {
  outline: none;
}

.chatbot-form-footer {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 8px;
}

.chatbot-form-tip {
  margin: 0;
  color: var(--muted);
  font-size: 0.78rem;
  line-height: 1.4;
}

.chatbot-submit {
  flex-shrink: 0;
  min-width: 96px;
  min-height: 46px;
  padding: 0 18px;
  border: 1px solid transparent;
  border-radius: 999px;
  background: linear-gradient(135deg, rgba(79, 142, 247, 0.96), rgba(88, 183, 255, 0.92));
  color: #041223;
  font-weight: 800;
  cursor: pointer;
}

.chatbot-close-action {
  flex-shrink: 0;
  min-width: 96px;
  min-height: 46px;
  padding: 0 18px;
  border: 1px solid rgba(154, 171, 204, 0.18);
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.06);
  color: var(--text);
  font-weight: 700;
  cursor: pointer;
}

.chatbot-close-action:disabled,
.chatbot-submit:disabled,
.chatbot-input:disabled {
  opacity: 0.6;
  cursor: not-allowed;
}

.widget-shell {
  height: 100%;
  display: flex;
  flex-direction: column;
  background:
    linear-gradient(180deg, rgba(15, 26, 44, 0.85), rgba(8, 17, 31, 0.92)),
    #091320;
}

.widget-note {
  padding: 8px 12px;
  border-bottom: 1px solid var(--border);
}

.widget-note p {
  margin: 0;
  color: var(--muted);
  line-height: 1.4;
  font-size: 0.78rem;
}

.widget-note a {
  color: #8dc2ff;
}

.widget-host {
  flex: 1;
  min-height: 0;
}

.widget-status {
  margin: 0;
  padding: 12px;
  color: var(--muted);
  font-size: 0.8rem;
}

.widget-status.error {
  color: #ffb4b4;
}

@media (max-width: 920px) {
  /*
   * 루트에 max-height: 100dvh를 걸면 일부 모바일(삼성 인터넷 등)에서
   * 핀치 줌·축소가 막히거나 동작이 어색해질 수 있음 → max-height 사용 안 함.
   * overflow-x:hidden 은 확대/축소 후 패닝·줌과 충돌하는 경우가 있어 루트는 auto 유지.
   */
  html {
    overflow: auto;
    touch-action: auto;
  }

  body {
    min-height: 100vh;
    min-height: 100dvh;
    overflow: auto;
    -webkit-overflow-scrolling: touch;
    touch-action: auto;
    padding-left: env(safe-area-inset-left, 0px);
    padding-right: env(safe-area-inset-right, 0px);
  }

  .app-shell {
    max-width: 100%;
  }

  .topbar {
    gap: 6px;
    padding: 5px 8px;
  }

  .tabbar {
    gap: 5px;
  }

  .tab-button {
    padding: 5px 9px;
    font-size: 0.78rem;
  }

  .icon-button,
  .icon-link {
    width: 30px;
    height: 30px;
  }

  .viewer {
    overflow: auto;
    -webkit-overflow-scrolling: touch;
  }
}

@media (max-width: 640px) {
  .topbar {
    min-height: 44px;
    padding: 5px max(8px, env(safe-area-inset-right, 0px)) 5px max(8px, env(safe-area-inset-left, 0px));
  }

  .tab-button,
  .icon-button,
  .icon-link {
    min-height: 30px;
    height: 30px;
  }

  .tab-button {
    padding: 5px 9px;
    font-size: 0.78rem;
  }

  .chatbot-fab {
    right: calc(env(safe-area-inset-right, 0px) + 14px);
    bottom: calc(env(safe-area-inset-bottom, 0px) + 82px);
    min-width: 104px;
    height: 52px;
    padding: 0 16px;
    font-size: 0.9rem;
  }

  .chatbot-backdrop {
    backdrop-filter: blur(8px);
  }

  .chatbot-panel {
    left: 0;
    right: 0;
    width: auto;
    max-width: none;
    padding: max(10px, env(safe-area-inset-top, 0px)) env(safe-area-inset-right, 0px) max(12px, env(safe-area-inset-bottom, 0px))
      env(safe-area-inset-left, 0px);
  }

  .chatbot-shell {
    padding: 14px 12px 12px;
    border-right: 0;
    border-left: 0;
    border-radius: 0;
  }

  .chatbot-meta-row {
    align-items: stretch;
  }

  .chatbot-context-label,
  .chatbot-status {
    min-height: 32px;
    font-size: 0.8rem;
  }

  .chatbot-sources {
    gap: 7px;
  }

  .chatbot-source-chip {
    min-height: 28px;
    font-size: 0.74rem;
  }

  .chatbot-stage,
  .chatbot-form {
    border-radius: 18px;
  }

  .chatbot-entry-assistant .chatbot-message {
    width: min(100%, 96%);
  }

  .chatbot-stage {
    padding: 14px;
  }

  .chatbot-input {
    min-height: 62px;
    max-height: 112px;
    font-size: 0.94rem;
  }

  .chatbot-form-footer {
    align-items: center;
    justify-content: stretch;
    gap: 8px;
  }

  .chatbot-submit,
  .chatbot-close-action {
    flex: 1 1 0;
    min-width: 0;
    min-height: 42px;
    padding: 0 12px;
    font-size: 0.92rem;
  }

  .chatbot-advisor-card {
    padding: 12px 13px;
    border-radius: 16px;
  }

  .chatbot-advisor-top {
    flex-direction: column;
    align-items: flex-start;
  }

  .chatbot-advisor-next {
    text-align: left;
  }

  .chatbot-message {
    max-width: 100%;
  }
}

@media (max-width: 430px) {
  .topbar {
    min-height: 42px;
    gap: 4px;
    padding: 4px max(6px, env(safe-area-inset-right, 0px)) 4px max(6px, env(safe-area-inset-left, 0px));
  }

  .toolbar,
  .tabbar {
    gap: 4px;
  }

  .tab-button,
  .icon-button,
  .icon-link {
    min-height: 28px;
    height: 28px;
  }

  .tab-button {
    padding: 4px 8px;
    font-size: 0.74rem;
  }

  .icon-button,
  .icon-link {
    width: 28px;
  }

  .chatbot-fab {
    right: calc(env(safe-area-inset-right, 0px) + 12px);
    bottom: calc(env(safe-area-inset-bottom, 0px) + 92px);
  }

  .chatbot-shell {
    gap: 8px;
    padding: 12px 10px 10px;
  }

  .chatbot-stage {
    padding: 12px;
  }

  .chatbot-context-label,
  .chatbot-status {
    min-height: 30px;
    padding: 0 10px;
    font-size: 0.76rem;
  }

  .chatbot-source-chip {
    min-height: 27px;
    padding: 0 9px;
    font-size: 0.72rem;
  }

  .chatbot-message {
    padding: 12px 13px;
    font-size: 0.94rem;
    line-height: 1.6;
  }

  .chatbot-input {
    min-height: 58px;
    max-height: 104px;
    padding: 9px 11px;
  }

  .chatbot-advisor-card {
    gap: 8px;
    padding: 11px 12px;
  }

  .chatbot-summary-badge {
    min-height: 26px;
    font-size: 0.72rem;
  }

  .chatbot-advisor-headline {
    font-size: 0.88rem;
  }

  .chatbot-advisor-avoid,
  .chatbot-advisor-state-summary {
    font-size: 0.78rem;
  }
}

@media (max-width: 360px) {
  .chatbot-fab {
    min-width: 96px;
    height: 48px;
    bottom: calc(env(safe-area-inset-bottom, 0px) + 96px);
  }

  .chatbot-fab-text {
    font-size: 0.84rem;
  }

  .chatbot-shell {
    padding: 10px 8px 8px;
  }

  .chatbot-stage,
  .chatbot-form {
    border-radius: 16px;
  }

  .chatbot-submit,
  .chatbot-close-action {
    min-height: 40px;
    font-size: 0.88rem;
  }
}

@keyframes chatbot-caret-blink {
  0%,
  45% {
    opacity: 1;
  }
  46%,
  100% {
    opacity: 0;
  }
}
