/* ai chat */
.history-panel {
  width: 264px;
  flex-shrink: 0;
  display: flex;
  flex-direction: column;
  transition: width .24s ease, opacity .18s ease;
  background: rgba(10, 13, 30, .72);
  backdrop-filter: blur(20px);
  border-inline-end: 1px solid rgba(255, 255, 255, .06);
  z-index: 40;
}

.history-panel[hidden] { display: none; }

/* history head */
.history-head {
  height: 65px;
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 0 14px;
  border-bottom: 1px solid rgba(255, 255, 255, .06);
}

.history-head h2 {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 13px;
  font-weight: 600;
  color: #dfe4f5;
}

.history-head .icon {
  width: 15px;
  height: 15px;
  color: var(--primary, #7c5cff);
}

/* history close */
.history-close {
  display: none;
  border: none;
  background: rgba(255, 255, 255, .06);
  color: #cfd6ec;
  width: 30px;
  height: 30px;
  border-radius: 9px;
  cursor: pointer;
  align-items: center;
  justify-content: center;
}

.history-close .icon { width: 13px; height: 13px; }

/* history list */
.history-list {
  flex: 1;
  overflow-y: auto;
  padding: 10px;
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.history-list::-webkit-scrollbar { width: 5px; }
.history-list::-webkit-scrollbar-thumb {
  background: rgba(255, 255, 255, .12);
  border-radius: 3px;
}

/* history empty */
.history-empty {
  padding: 26px 12px;
  text-align: center;
  font-size: 12px;
  color: #8b93a5;
}

/* history item */
.history-item {
  display: flex;
  align-items: stretch;
  gap: 2px;
  border-radius: 12px;
  border: 1px solid transparent;
  transition: background .18s ease, border-color .18s ease;
}

.history-item:hover { background: rgba(255, 255, 255, .05); }

.history-item.active {
  background: rgba(124, 92, 255, .14);
  border-color: rgba(124, 92, 255, .35);
}

/* history open */
.history-open {
  flex: 1;
  min-width: 0;
  border: none;
  background: none;
  cursor: pointer;
  text-align: start;
  padding: 9px 11px;
  display: flex;
  flex-direction: column;
  gap: 3px;
  font-family: inherit;
  color: inherit;
}

.history-open strong {
  font-size: 12.5px;
  font-weight: 600;
  color: #e6ebfb;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  max-width: 100%;
}

.history-open small { font-size: 10.5px; color: #8b93a5; }

/* history delete */
.history-delete {
  border: none;
  background: none;
  cursor: pointer;
  color: #7d8499;
  width: 32px;
  border-radius: 10px;
  display: flex;
  align-items: center;
  justify-content: center;
  opacity: 0;
  transition: opacity .18s ease, color .18s ease;
}

.history-item:hover .history-delete,
.history-item.active .history-delete { opacity: 1; }

.history-delete:hover { color: #ff6b81; background: rgba(255, 107, 129, .12); }
.history-delete .icon { width: 13px; height: 13px; }

/* history scrim */
.history-scrim {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(4, 6, 16, .55);
  backdrop-filter: blur(2px);
  z-index: 35;
}

/* topbar start */
.topbar-start {
  display: flex;
  align-items: center;
  gap: 10px;
  min-width: 0;
}

/* history panel */
.history-panel.collapsed {
  width: 0;
  opacity: 0;
  pointer-events: none;
  overflow: hidden;
  border-inline-end-color: transparent;
}

/* history toggle */
.history-toggle {
  border: 1px solid rgba(255, 255, 255, .1);
  background: rgba(255, 255, 255, .05);
  color: #cfd6ec;
  width: 36px;
  height: 36px;
  border-radius: 11px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}

.history-toggle[hidden] { display: none; }
.history-toggle:hover { background: rgba(255, 255, 255, .1); }
.history-toggle .icon { width: 15px; height: 15px; }

/* topbar actions */
.topbar-actions {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 10px;
}

/* send button */
.send-button {
  width: 46px;
  height: 46px;
  flex-shrink: 0;
  border: none;
  border-radius: 14px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  color: #fff;
  background: linear-gradient(135deg, var(--primary, #7c5cff), var(--secondary, #00d4ff));
  box-shadow: 0 4px 14px rgba(124, 92, 255, .3);
  transition: filter .15s ease, background .15s ease;
}

.send-button:hover { filter: brightness(1.08); }
.send-button:active { filter: brightness(.94); }

.send-button svg {
  width: 21px;
  height: 21px;
  transform: scaleX(-1);
}

.send-button.is-stop {
  background: linear-gradient(135deg, #ff5f6d, #ff2d55);
  box-shadow: 0 4px 14px rgba(255, 45, 85, .3);
}

/* send stop */
.send-stop {
  width: 15px;
  height: 15px;
  border-radius: 3px;
  background: #fff;
  display: block;
}

/* token bar */
.token-bar {
  display: flex;
  align-items: center;
  gap: 9px;
  margin: 0 auto 8px;
  max-width: 820px;
  width: 100%;
  font-size: 11px;
  color: #9aa3b8;
}

.token-bar[hidden] { display: none; }

/* token track */
.token-track {
  flex: 1;
  height: 5px;
  border-radius: 3px;
  background: rgba(255, 255, 255, .08);
  overflow: hidden;
}

/* token fill */
.token-fill {
  height: 100%;
  border-radius: 3px;
  background: linear-gradient(90deg, var(--primary, #7c5cff), var(--secondary, #00d4ff));
  transition: width .3s ease;
}

.token-bar.warn { color: #ffc46b; }
.token-bar.warn .token-fill { background: linear-gradient(90deg, #ffa64d, #ffc46b); }
.token-bar.full { color: #ff8095; }
.token-bar.full .token-fill { background: linear-gradient(90deg, #ff5f6d, #ff2d55); }

.guest-slot:empty { display: none; }

/* chat lock */
.chat-lock {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  flex-wrap: wrap;
  max-width: 820px;
  width: 100%;
  margin: 0 auto 10px;
  padding: 11px 14px;
  border-radius: 14px;
  border: 1px solid rgba(255, 95, 109, .35);
  background: rgba(255, 95, 109, .12);
  font-size: 12px;
  line-height: 1.9;
  color: #ffc2c9;
}

.chat-lock span { flex: 1; min-width: 200px; }

/* chat lock btn */
.chat-lock-btn {
  flex-shrink: 0;
  border: none;
  cursor: pointer;
  font-family: inherit;
  font-size: 12px;
  font-weight: 600;
  color: #fff;
  padding: 8px 16px;
  border-radius: 10px;
  background: linear-gradient(135deg, var(--primary, #7c5cff), var(--secondary, #00d4ff));
}

.chat-lock-btn:hover { filter: brightness(1.08); }

/* locked composer */
body.chat-full #messageInput,
body.chat-full .send-button {
  opacity: .5;
  cursor: not-allowed;
}

body.light-theme .chat-lock {
  border-color: rgba(217, 58, 88, .3);
  background: rgba(217, 58, 88, .08);
  color: #a3283f;
}

/* guest banner */
.guest-banner {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  flex-wrap: wrap;
  max-width: 820px;
  width: 100%;
  margin: 0 auto 10px;
  padding: 10px 14px;
  border-radius: 14px;
  border: 1px solid rgba(124, 92, 255, .28);
  background: rgba(124, 92, 255, .1);
  font-size: 12px;
  color: #cfd6ec;
}

.guest-banner a {
  flex-shrink: 0;
  text-decoration: none;
  font-size: 12px;
  font-weight: 600;
  color: #fff;
  padding: 7px 15px;
  border-radius: 10px;
  background: linear-gradient(135deg, var(--primary, #7c5cff), var(--secondary, #00d4ff));
}

/* message meta */
.message-meta {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  margin-top: 6px;
}

/* message actions */
.message-actions {
  display: flex;
  align-items: center;
  gap: 4px;
  opacity: 0;
  transition: opacity .18s ease;
}

.message-content:hover .message-actions,
.message-actions:focus-within { opacity: 1; }

/* msg action */
.msg-action {
  border: none;
  background: rgba(255, 255, 255, .08);
  color: #cfd6ec;
  width: 26px;
  height: 26px;
  border-radius: 8px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
}

.msg-action:hover { background: rgba(255, 255, 255, .18); }
.msg-action .icon { width: 13px; height: 13px; }

/* msg edit */
.msg-edit {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.msg-edit textarea {
  width: 100%;
  resize: vertical;
  min-height: 62px;
  border-radius: 12px;
  border: 1px solid rgba(255, 255, 255, .18);
  background: rgba(0, 0, 0, .22);
  color: #fff;
  font-family: inherit;
  font-size: 13.5px;
  line-height: 1.7;
  padding: 10px 12px;
}

.msg-edit textarea:focus {
  outline: none;
  border-color: rgba(255, 255, 255, .38);
}

.msg-edit-actions {
  display: flex;
  gap: 8px;
  justify-content: flex-end;
}

.msg-edit-save,
.msg-edit-cancel {
  border: none;
  cursor: pointer;
  font-family: inherit;
  font-size: 12px;
  padding: 7px 14px;
  border-radius: 10px;
}

.msg-edit-save {
  color: #fff;
  background: linear-gradient(135deg, var(--primary, #7c5cff), var(--secondary, #00d4ff));
}

.msg-edit-cancel {
  color: #cfd6ec;
  background: rgba(255, 255, 255, .12);
}

/* narrow 900px */
@media (max-width: 900px) {
  .history-panel {
    position: fixed;
    inset-block: 0;
    inset-inline-start: 0;
    inset-inline-end: auto;
    width: 280px;
    max-width: 84vw;
    transform: translateX(100%);
    transition: transform .26s ease;
    border-inline-end: none;
    box-shadow: -16px 0 40px rgba(0, 0, 0, .4);
    z-index: 60;
  }

  html[dir="ltr"] .history-panel { transform: translateX(-100%); }
  .history-panel.open,
  html[dir="ltr"] .history-panel.open { transform: translateX(0); }
  .history-panel.open ~ .history-scrim { display: block; }
  .history-close { display: flex; }
  .history-panel.collapsed { width: 280px; opacity: 1; pointer-events: auto; }
  .new-chat-btn span { display: none; }
  .new-chat-btn { padding: 0 12px; }
}

/* mobile 620px */
@media (max-width: 620px) {
  .nav-buttons { display: none; }
  .model-info span { display: none; }
  .guest-banner { font-size: 11.5px; }
  .send-button { width: 42px; height: 42px; border-radius: 12px; }
}

html[data-perf-tier="very-low"] .history-panel,
/* history panel */
html[data-perf-tier="low"] .history-panel {
  backdrop-filter: none;
  background: #0b0e20;
}

/* light theme */
body.light-theme .history-panel {
  background: rgba(255, 255, 255, .82);
  border-inline-end-color: rgba(18, 22, 48, .1);
}

body.light-theme .history-head { border-bottom-color: rgba(18, 22, 48, .1); }
body.light-theme .history-head h2 { color: #1c2140; }
body.light-theme .history-close { background: rgba(18, 22, 48, .06); color: #464d70; }
body.light-theme .history-open strong { color: #1c2140; }
body.light-theme .history-open small,
body.light-theme .history-empty { color: #6b7290; }
body.light-theme .history-item:hover { background: rgba(18, 22, 48, .05); }
body.light-theme .history-item.active { background: rgba(124, 92, 255, .12); }
body.light-theme .history-delete { color: #7a80a0; }
/* history toggle */
body.light-theme .history-toggle {
  background: rgba(18, 22, 48, .05);
  border-color: rgba(18, 22, 48, .12);
  color: #464d70;
}
body.light-theme .history-scrim { background: rgba(30, 34, 66, .35); }
body.light-theme .token-bar { color: #6b7290; }
body.light-theme .token-track { background: rgba(18, 22, 48, .1); }
body.light-theme .guest-banner { color: #3a406a; background: rgba(124, 92, 255, .08); }
body.light-theme .msg-action { background: rgba(18, 22, 48, .07); color: #464d70; }
body.light-theme .msg-action:hover { background: rgba(18, 22, 48, .14); }
/* msg edit */
body.light-theme .msg-edit textarea {
  background: #fff;
  border-color: rgba(18, 22, 48, .16);
  color: #1c2140;
}
body.light-theme .msg-edit-cancel { background: rgba(18, 22, 48, .09); color: #464d70; }

/* narrow 900px */
@media (max-width: 900px) {
  .topbar { height: auto; min-height: 58px; padding: 8px 12px; }
  .logo-mini img { width: 32px; height: 32px; }
  .model-info h2 { font-size: 13.5px; }
}

/* mobile 620px */
@media (max-width: 620px) {
  .chat-messages { padding: 10px 10px 4px; }
  .message-content { max-width: 92%; font-size: 13.5px; }
  .input-area { padding: 8px 10px calc(8px + env(safe-area-inset-bottom)); }
  #messageInput { font-size: 16px; min-height: 42px; padding: 10px 14px; }
  .welcome-screen h1 { font-size: 1.35rem; }
  .welcome-screen p { font-size: .85rem; }
  .hero-icon img { width: 84px; height: 84px; }
  .badges { gap: 6px; }
  .badge { font-size: 10.5px; padding: 4px 9px; }
  .new-chat-btn { height: 34px; width: 34px; padding: 0; justify-content: center; border-radius: 11px; }
  .new-chat-btn svg { width: 17px; height: 17px; }
  .token-bar span { font-size: 10.5px; }
  .guest-banner { padding: 8px 11px; }
  .guest-banner a { padding: 6px 12px; font-size: 11.5px; }
  .msg-action { width: 24px; height: 24px; }
  .message-actions { opacity: 1; }
}

/* mobile 400px */
@media (max-width: 400px) {
  .model-info h2 { font-size: 12.5px; }
  .topbar-actions { gap: 6px; }
  .history-toggle { width: 32px; height: 32px; border-radius: 9px; }
}
