:root {
  --bg: #f1f5f9;
  --panel: #ffffff;
  --ink: #0f172a;
  --muted: #475569;
  --line: #dbe3ef;
  --accent: #0b5fff;
  --danger: #b91c1c;
  --token-meter-h: 18px;
}
* { box-sizing: border-box; }
body {
  margin: 0;
  background: radial-gradient(800px 280px at 20% 0%, #dbeafe, transparent), var(--bg);
  font-family: "Avenir Next", Avenir, "Segoe UI", sans-serif;
  color: var(--ink);
  min-height: 100dvh;
  overflow: hidden;
}
.app {
  max-width: 900px;
  margin: 0 auto;
  padding: 12px 16px calc(12px + env(safe-area-inset-bottom));
  height: 100dvh;
  display: flex;
  flex-direction: column;
  gap: 6px;
}
.hero {
  position: relative;
  background: linear-gradient(135deg, #0b5fff, #0f766e);
  color: #f8fafc;
  border-radius: 14px;
  min-height: 54px;
  padding: 10px 68px 10px 12px;
  display: flex;
  align-items: center;
}
.hero h1 {
  margin: 0;
  font-size: 21px;
  line-height: 1.1;
}
.hero p { margin: 6px 0 0; color: #dbeafe; }
.hint code { background: rgba(255,255,255,0.2); padding: 1px 6px; border-radius: 4px; }
.user-menu {
  position: absolute;
  right: 10px;
  top: 50%;
  transform: translateY(-50%);
  display: inline-flex;
  align-items: center;
  gap: 8px;
  z-index: 1300;
}
.user-fab {
  width: 42px;
  height: 42px;
  border-radius: 999px;
  border: 2px solid rgba(255, 255, 255, 0.6);
  background: rgba(15, 23, 42, 0.28);
  color: #f8fafc;
  font-size: 15px;
  font-weight: 700;
  padding: 0;
}
.user-popover {
  position: absolute;
  right: 0;
  top: 50px;
  width: clamp(620px, 78vw, 860px);
  max-width: calc(100vw - 24px);
  height: min(78vh, 720px);
  max-height: 78vh;
  overflow: auto;
  background: #fff;
  border: 2px solid #cbd5e1;
  border-radius: 14px;
  box-shadow: 0 18px 36px rgba(15, 23, 42, 0.26);
  padding: 12px;
  color: var(--ink);
  z-index: 1301;
}
.popover-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  margin: 0 0 10px;
}
.popover-head h2 {
  margin: 0;
  font-size: 18px;
  line-height: 1.2;
}
.profile-section {
  border: 1px solid var(--line);
  border-radius: 10px;
  background: #f8fafc;
  padding: 10px;
  margin: 0 0 10px;
}
.profile-section h3 {
  margin: 0 0 8px;
  font-size: 12px;
  letter-spacing: 0.03em;
  color: #334155;
  text-transform: uppercase;
}
.user-popover p {
  margin: 0 0 8px;
  font-size: 13px;
  color: var(--muted);
}
.model-info {
  margin: 0 0 8px;
  padding: 8px 10px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #f8fafc;
  color: #334155;
  font-size: 12px;
  line-height: 1.35;
}
.llm-settings {
  margin: 0 0 8px;
  padding: 8px 10px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
}
.llm-list {
  margin: 0 0 8px;
  padding: 8px 10px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
  color: #334155;
  font-size: 12px;
  line-height: 1.35;
  max-height: 220px;
  overflow: auto;
}
.llm-profile-item {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 8px;
  padding: 6px 0;
  border-bottom: 1px solid #e2e8f0;
}
.llm-profile-item:last-child {
  border-bottom: 0;
}
.llm-profile-label {
  display: inline-flex;
  align-items: flex-start;
  gap: 8px;
  flex: 1;
  min-width: 0;
}
.llm-profile-label input {
  width: 15px;
  height: 15px;
  margin-top: 2px;
}
.llm-profile-text {
  min-width: 0;
}
.llm-profile-name {
  font-weight: 600;
  color: #0f172a;
}
.llm-profile-meta {
  color: #475569;
  font-size: 11px;
  margin-top: 2px;
  word-break: break-word;
}
.llm-profile-actions {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  flex-wrap: wrap;
  justify-content: flex-end;
}
.llm-settings input {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 8px 10px;
  font: inherit;
  font-size: 12px;
  margin: 0 0 8px;
}
.llm-settings button {
  margin-top: 0;
}
.llm-settings-actions {
  display: flex;
  gap: 8px;
  justify-content: flex-end;
}
.skills-list {
  margin: 0 0 8px;
  padding: 8px 10px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
  color: #334155;
  font-size: 12px;
  line-height: 1.35;
  max-height: 190px;
  overflow: auto;
}
.skill-upload {
  margin: 0 0 8px;
  padding: 8px 10px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
}
.upload-label {
  display: block;
  margin: 0 0 6px;
  font-size: 12px;
  color: #334155;
}
.skill-upload input[type="file"] {
  width: 100%;
  font-size: 12px;
}
.upload-replace {
  margin-top: 8px;
  font-size: 12px;
}
.skill-upload button {
  margin-top: 8px;
}
.password-change {
  display: grid;
  grid-template-columns: 1fr;
  gap: 8px;
  margin: 0 0 8px;
}
.password-change input {
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 8px 10px;
  font: inherit;
  font-size: 13px;
}
.password-change button {
  width: 100%;
}
.skill-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  margin: 4px 0;
  color: #334155;
}
.skill-item label {
  display: inline-flex;
  align-items: center;
  gap: 8px;
}
.skill-item input {
  width: 15px;
  height: 15px;
}
.skill-item span {
  flex: 1;
}
.skill-item button {
  width: auto;
  margin-top: 0;
}
.user-popover button {
  width: 100%;
  margin-top: 8px;
}
.compact-btn {
  width: auto !important;
  padding: 6px 10px;
  font-size: 12px;
  line-height: 1.2;
}
.uninstall-btn {
  margin-top: 0 !important;
  padding: 4px 8px;
  font-size: 11px;
}
.logout-btn {
  display: block;
  margin-left: auto;
}
.secondary {
  background: #475569;
}
.auth {
  margin-top: 0;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 14px;
  padding: 12px;
}
.auth-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
}
.auth-row input {
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 10px;
  font: inherit;
}
#authStatus {
  margin: 10px 0 0;
  font-size: 13px;
  color: var(--muted);
}
#authStatus.error { color: #b91c1c; }
#authStatus.ok { color: #0f766e; }
.toggle {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  margin-top: 10px;
  color: var(--muted);
  font-size: 14px;
}
.toggle-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
}
.toggle-row .toggle {
  margin-top: 10px;
}
.toggle-right {
  margin-left: auto;
}
.toggle input {
  width: 16px;
  height: 16px;
}
.chat {
  margin-top: 0;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 14px;
  display: flex;
  flex-direction: column;
  flex: 1 1 auto;
  min-height: 0;
  padding: 12px;
  overflow: hidden;
  position: relative;
}
.chat.preview-mode {
  overflow: hidden;
}
.chat-body {
  flex: 1 1 auto;
  min-height: 0;
  height: 100%;
  overflow: auto;
}
.msg {
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 10px;
  margin: 8px 0;
  background: #fff;
}
.msg.user { border-color: #bfdbfe; background: #eff6ff; }
.msg.assistant { border-color: #bbf7d0; background: #f0fdf4; }
.msg.system { border-color: #fcd34d; background: #fffbeb; }
.label { font-size: 12px; color: var(--muted); text-transform: uppercase; margin-bottom: 6px; }
.msg > div:not(.label) { white-space: pre-wrap; }
.chat-preview {
  position: absolute;
  inset: 0;
  z-index: 20;
  border-radius: 14px;
  background: #ffffff;
  border: 1px solid var(--line);
  padding: 10px;
  overflow: auto;
}
.token-meter {
  flex: 0 0 var(--token-meter-h);
  height: var(--token-meter-h);
  display: flex;
  align-items: center;
  justify-content: flex-end;
  color: #475569;
  font-size: 11px;
  line-height: 1.1;
  text-align: right;
  min-height: var(--token-meter-h);
  padding: 0;
  margin: 0;
  opacity: 0.88;
  pointer-events: none;
  white-space: nowrap;
  overflow: hidden;
}
.token-meter.pending {
  color: #1e3a8a;
}
.token-meter.estimated {
  color: #0f766e;
}
.preview-toolbar {
  position: sticky;
  top: 0;
  z-index: 2;
  display: grid;
  grid-template-columns: auto minmax(0, 1fr) auto;
  align-items: center;
  gap: 8px;
  margin: 0 0 8px;
  padding: 6px 8px;
  background: #ffffff;
  border: 1px solid var(--line);
  border-radius: 10px;
}
#previewFileInfo {
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  color: #334155;
  font-size: 12px;
}
.chat-preview p {
  margin: 0;
  color: var(--muted);
  font-size: 14px;
}
.chat-preview iframe,
.chat-preview img {
  width: 100%;
  height: 100%;
  min-height: 240px;
  border: 0;
  border-radius: 10px;
  background: #f8fafc;
}
.chat-preview img {
  object-fit: contain;
}
.composer {
  margin-top: 0;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 14px;
  padding: 8px 10px;
  position: relative;
  z-index: 10;
}
.composer-layout {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 6px;
  align-items: end;
}
.composer-main {
  display: flex;
  flex-direction: column;
  gap: 4px;
  min-width: 0;
}
.composer-actions {
  display: flex;
  flex-direction: column;
  align-items: stretch;
  justify-content: flex-end;
  gap: 8px;
}
.composer-actions button {
  min-width: 120px;
  margin: 0;
}
#viewToggle.preview-processing {
  animation: previewButtonPulse 2.6s ease-in-out infinite;
}
@keyframes previewButtonPulse {
  0%, 100% {
    box-shadow: 0 0 0 0 rgba(30, 64, 175, 0.30);
    filter: brightness(1);
  }
  50% {
    box-shadow: 0 0 0 4px rgba(30, 64, 175, 0.24);
    filter: brightness(1.08);
  }
}
.confirm-inline {
  background: #fff7ed;
  border: 1px solid #fed7aa;
  border-radius: 10px;
  padding: 8px 10px;
}
.confirm-inline p {
  margin: 0;
  color: #9a3412;
  font-size: 13px;
  line-height: 1.35;
}
textarea {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 8px 10px;
  font: inherit;
  line-height: 1.35;
  min-height: calc(1.35em * 3 + 16px);
  max-height: calc(1.35em * 3 + 16px);
  height: calc(1.35em * 3 + 16px);
  resize: none;
  overflow-y: auto;
}
textarea:disabled {
  background: #f8fafc;
  color: #64748b;
}
.row { margin-top: 10px; display: flex; gap: 8px; }
button {
  border: 0;
  border-radius: 10px;
  padding: 10px 14px;
  font-weight: 600;
  background: var(--accent);
  color: #fff;
  cursor: pointer;
}
button.danger { background: var(--danger); }
button:disabled { opacity: 0.55; cursor: not-allowed; }
.hidden { display: none; }
pre {
  background: #0f172a;
  color: #f8fafc;
  border-radius: 8px;
  padding: 10px;
  overflow: auto;
}

@media (max-width: 700px) {
  :root {
    --mobile-composer-h: 98px;
    --mobile-composer-h-confirm: 192px;
    --token-meter-h: 16px;
  }
  body {
    min-height: 100svh;
  }
  body.profile-open {
    overflow: hidden;
  }
  .app {
    margin: 0 auto;
    padding: 10px 10px calc(10px + env(safe-area-inset-bottom));
    height: 100svh;
    min-height: 100svh;
    gap: 5px;
  }
  .hero {
    border-radius: 12px;
    min-height: 52px;
    padding: 10px 60px 10px 10px;
  }
  .hero h1 {
    font-size: 18px;
  }
  .user-menu {
    top: 8px;
    right: 8px;
    transform: none;
    gap: 6px;
  }
  .user-popover {
    position: fixed;
    left: 8px;
    right: 8px;
    top: 8px;
    bottom: 8px;
    width: auto;
    max-width: none;
    max-height: none;
    height: calc(100dvh - 16px);
    overflow: auto;
    z-index: 1200;
    border-radius: 14px;
    padding: 12px;
  }
  .popover-head {
    position: sticky;
    top: 0;
    background: #fff;
    padding-bottom: 8px;
    border-bottom: 1px solid var(--line);
    z-index: 1;
  }
  .profile-section {
    margin-bottom: 12px;
  }
  .user-popover input,
  .user-popover textarea,
  .user-popover button,
  .user-popover .toggle,
  .user-popover .upload-label {
    font-size: 16px;
  }
  .user-popover input[type="checkbox"] {
    width: 18px;
    height: 18px;
  }
  .auth-row {
    grid-template-columns: 1fr;
  }
  .auth,
  .chat,
  .composer {
    margin-top: 0;
  }
  .chat {
    min-height: 0;
    overflow: hidden;
  }
  .composer {
    position: relative;
    left: auto;
    right: auto;
    bottom: auto;
    z-index: 10;
    border-radius: 12px;
    padding: 8px 10px;
    height: var(--mobile-composer-h);
    min-height: var(--mobile-composer-h);
    max-height: var(--mobile-composer-h);
    box-shadow: 0 6px 16px rgba(15, 23, 42, 0.12);
    overflow: hidden;
  }
  .composer.confirm-active {
    height: var(--mobile-composer-h-confirm);
    min-height: var(--mobile-composer-h-confirm);
    max-height: var(--mobile-composer-h-confirm);
    padding: 10px;
  }
  .composer-main,
  .composer-actions {
    height: 100%;
    min-height: 0;
  }
  .composer-main {
    justify-content: flex-end;
  }
  .composer textarea {
    min-height: calc(1.35em * 3 + 16px);
    max-height: calc(1.35em * 3 + 16px);
    height: calc(1.35em * 3 + 16px);
    padding: 8px 10px;
    overflow-y: auto;
  }
  .composer-layout {
    gap: 6px;
    align-items: end;
  }
  .composer-actions {
    gap: 4px;
    justify-content: flex-end;
  }
  .composer-actions button {
    min-width: 62px;
    min-height: 28px;
    padding: 4px 7px;
    font-size: 11px;
    line-height: 1.15;
  }
  .composer.confirm-active .composer-actions {
    gap: 6px;
  }
  .composer.confirm-active .composer-actions button {
    min-width: 70px;
    min-height: 32px;
    padding: 5px 8px;
    font-size: 12px;
  }
  .confirm-inline {
    position: static;
    margin: 0 0 4px;
    min-height: calc(1.35em * 3 + 16px);
    max-height: calc(1.35em * 3 + 16px);
    height: calc(1.35em * 3 + 16px);
    overflow-y: auto;
  }
}
