:root {
  color-scheme: light;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  background: #f6f7f8;
  color: #17191c;
}

* {
  box-sizing: border-box;
}

html,
body,
.app-shell {
  width: 100%;
  height: 100%;
  margin: 0;
}

body {
  overflow: hidden;
}

button,
input,
textarea {
  font: inherit;
}

.login-view,
.contacts-view,
.chat-view {
  height: 100%;
  background: #f6f7f8;
}

.login-view {
  display: grid;
  place-items: center;
  padding: 24px;
}

.login-panel {
  width: min(360px, 100%);
  display: grid;
  gap: 14px;
}

.login-panel h1 {
  margin: 0 0 10px;
  font-size: 30px;
  font-weight: 700;
}

.login-panel input,
.composer textarea {
  width: 100%;
  border: 1px solid #d9dde3;
  border-radius: 8px;
  background: #fff;
  color: #17191c;
  outline: none;
}

.login-panel input {
  height: 46px;
  padding: 0 12px;
}

.login-panel button,
.composer button {
  border: 0;
  border-radius: 8px;
  background: #19b45b;
  color: #fff;
  font-weight: 600;
}

.login-panel button {
  height: 46px;
}

.topbar,
.chat-topbar {
  height: 54px;
  padding: max(env(safe-area-inset-top), 0px) 16px 0;
  border-bottom: 1px solid #e4e7eb;
  background: #f6f7f8;
  display: flex;
  align-items: center;
}

.topbar h1 {
  margin: 0;
  font-size: 22px;
  flex: 1;
}

.icon-button,
.back-button {
  border: 0;
  background: transparent;
  color: #333940;
  text-decoration: none;
}

.icon-button {
  min-height: 34px;
  padding: 6px 8px;
  border-radius: 6px;
}

.icon-button:disabled {
  color: #8b949e;
}

.contact-list {
  height: calc(100% - 54px);
  overflow-y: auto;
  background: #fff;
}

.contact-row {
  min-height: 72px;
  display: grid;
  grid-template-columns: 48px 1fr auto;
  gap: 12px;
  align-items: center;
  padding: 10px 14px;
  color: inherit;
  text-decoration: none;
  border-bottom: 1px solid #eef0f3;
}

.avatar {
  width: 48px;
  height: 48px;
  border-radius: 6px;
  object-fit: cover;
  background: #dfe4ea;
}

.avatar-placeholder {
  display: grid;
  place-items: center;
  color: #5f6873;
  font-weight: 700;
}

.contact-main {
  min-width: 0;
  display: grid;
  gap: 5px;
}

.contact-name {
  font-size: 16px;
  font-weight: 600;
}

.contact-scene-badge {
  display: inline-block;
  margin-left: 6px;
  padding: 1px 6px;
  border-radius: 999px;
  font-size: 11px;
  font-weight: 650;
  color: #b42318;
  background: #fee4e2;
  vertical-align: middle;
}

.contact-pi-badge {
  display: inline-block;
  margin-left: 6px;
  padding: 1px 6px;
  border-radius: 999px;
  font-size: 11px;
  font-weight: 650;
  color: #155eef;
  background: #e0edff;
  vertical-align: middle;
}

.contact-preview,
.contact-row time {
  color: #7b838d;
  font-size: 13px;
}

.contact-preview {
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
}

.chat-view {
  display: grid;
  grid-template-rows: 64px 1fr auto;
}

.chat-topbar {
  justify-content: center;
  position: relative;
  height: 64px;
}

.back-button {
  position: absolute;
  left: 12px;
  font-size: 34px;
  line-height: 1;
}

.retry-button {
  border: 0;
  border-radius: 6px;
  background: #e7eaee;
  color: #333940;
  padding: 6px 9px;
}

.retry-button:disabled {
  opacity: 0.6;
}

/* Title centered in the bar; round sits left of title via 1fr | auto | 1fr grid (not centered as a pair). */
.chat-heading-main {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto minmax(0, 1fr);
  align-items: baseline;
  width: 100%;
  min-width: 0;
}

.chat-round,
.chat-offline-round {
  grid-column: 1;
  justify-self: end;
  padding-right: 6px;
  font-size: 11px;
  font-weight: 650;
  color: #8b949e;
  font-variant-numeric: tabular-nums;
  min-width: 7ch;
  text-align: right;
  line-height: 1;
  white-space: nowrap;
}

.chat-offline-round {
  grid-column: 3;
  justify-self: start;
  padding-right: 0;
  padding-left: 6px;
  color: #1d8cf8;
  text-align: left;
  visibility: hidden;
}

.offline-mode .chat-offline-round {
  visibility: visible;
}

.chat-title {
  grid-column: 2;
  justify-self: center;
  text-align: center;
  font-size: 17px;
  font-weight: 650;
  min-width: 0;
  max-width: 100%;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.chat-heading {
  display: grid;
  gap: 2px;
  justify-items: stretch;
  min-width: 0;
  max-width: calc(100% - 110px);
}

.chat-clock {
  justify-self: center;
  color: #747d87;
  font-size: 12px;
  line-height: 1.2;
  white-space: nowrap;
}

.new-chat-button {
  border: 0;
  border-radius: 6px;
  background: #e7eaee;
  color: #333940;
  padding: 6px 9px;
}

.vars-button {
  border: 0;
  border-radius: 6px;
  background: #e7eaee;
  color: #333940;
  padding: 6px 9px;
}

.toolbox {
  position: absolute;
  right: 12px;
  top: 50%;
  transform: translateY(-50%);
}

.toolbox-button {
  border: 0;
  border-radius: 6px;
  background: #e7eaee;
  color: #333940;
  padding: 4px 8px;
  font-size: 20px;
  line-height: 1;
  cursor: pointer;
}

.toolbox-menu {
  position: absolute;
  top: calc(100% + 6px);
  right: 0;
  background: #ffffff;
  border: 1px solid #e2e5e9;
  border-radius: 8px;
  box-shadow: 0 6px 20px rgba(0, 0, 0, 0.12);
  padding: 6px;
  display: flex;
  flex-direction: column;
  gap: 4px;
  min-width: 120px;
  z-index: 100;
}

.toolbox-menu[hidden] {
  display: none;
}

.toolbox-menu .retry-button,
.toolbox-menu .vars-button,
.toolbox-menu .new-chat-button,
.toolbox-menu .offline-scene-button {
  width: 100%;
  text-align: left;
  background: transparent;
  padding: 8px 10px;
  border-radius: 6px;
}

.toolbox-menu .retry-button:hover,
.toolbox-menu .vars-button:hover,
.toolbox-menu .new-chat-button:hover,
.toolbox-menu .offline-scene-button:hover {
  background: #f0f2f5;
}

.toolbox-menu .offline-scene-button:disabled {
  opacity: 0.55;
  cursor: not-allowed;
}

.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.45);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
  padding: 16px;
}

.modal-panel {
  background: #fff;
  border-radius: 10px;
  width: min(640px, 100%);
  max-height: min(80vh, 720px);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 16px;
  border-bottom: 1px solid #eef0f3;
  font-weight: 600;
}

.modal-close {
  border: 0;
  background: transparent;
  font-size: 22px;
  line-height: 1;
  cursor: pointer;
  color: #555;
}

.modal-body {
  flex: 1;
  display: flex;
  flex-direction: column;
  padding: 12px 16px;
  gap: 8px;
  overflow: hidden;
}

.modal-status {
  font-size: 12px;
  color: #666;
}

.vars-textarea {
  flex: 1;
  min-height: 280px;
  width: 100%;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 13px;
  border: 1px solid #d6d9dd;
  border-radius: 6px;
  padding: 10px;
  resize: vertical;
  box-sizing: border-box;
}

.modal-footer {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
  padding: 12px 16px;
  border-top: 1px solid #eef0f3;
}

.modal-btn-primary,
.modal-btn-secondary {
  border: 0;
  border-radius: 6px;
  padding: 8px 14px;
  cursor: pointer;
  font-size: 14px;
}

.modal-btn-primary {
  background: #07c160;
  color: #fff;
}

.modal-btn-primary:disabled {
  opacity: 0.6;
  cursor: not-allowed;
}

.modal-btn-secondary {
  background: #e7eaee;
  color: #333940;
}

.message-list {
  overflow-y: auto;
  padding: 14px 12px;
}

.pi-start-panel {
  min-height: 100%;
  display: grid;
  place-items: center;
  padding: 24px;
}

.pi-start-button {
  min-width: 112px;
  min-height: 42px;
  border: 0;
  border-radius: 8px;
  background: #155eef;
  color: #fff;
  font-weight: 650;
  cursor: pointer;
}

.pi-start-button:disabled {
  opacity: 0.6;
  cursor: default;
}

.message-list.hidden {
  display: none;
}

.scene-header {
  display: none;
  border-bottom: 1px solid #e4e7eb;
  background: #f6f7f8;
  padding: 8px 12px;
  font-size: 13px;
  color: #4b5563;
}

.scene-header.active {
  display: flex;
  justify-content: flex-start;
  align-items: center;
  gap: 8px;
}

.scene-date-button {
  border: 1px solid transparent;
  background: transparent;
  color: #111827;
  font: inherit;
  font-weight: 650;
  padding: 4px 6px;
  border-radius: 6px;
  cursor: pointer;
}

.scene-date-button:hover {
  border-color: #d1d5db;
  background: #fff;
}

.scene-date-editor {
  display: inline-flex;
  align-items: center;
  gap: 6px;
}

.scene-date-editor[hidden] {
  display: none;
}

.scene-date-input {
  border: 1px solid #d1d5db;
  border-radius: 6px;
  padding: 4px 6px;
  font: inherit;
  color: #111827;
  background: #fff;
}

.scene-date-save,
.scene-date-cancel {
  border: 1px solid #d1d5db;
  border-radius: 6px;
  padding: 4px 8px;
  font: inherit;
  background: #fff;
  color: #111827;
  cursor: pointer;
}

.scene-date-save {
  border-color: #2563eb;
  background: #2563eb;
  color: #fff;
}

.message-row {
  display: flex;
  margin: 8px 0;
  gap: 8px;
  align-items: flex-start;
}

.message-row.from-user {
  justify-content: flex-end;
}

.message-row.from-assistant {
  justify-content: flex-start;
  align-items: flex-start;
}

.message-row.from-system {
  justify-content: center;
}

.message-stack {
  min-width: 0;
  max-width: min(calc(100% - 56px), 640px);
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.from-user .message-stack {
  align-items: flex-end;
}

.from-assistant .message-stack {
  align-items: flex-start;
}

.chat-avatar {
  flex: 0 0 auto;
  display: grid;
  place-items: center;
  background: #dfe4ea;
  color: #5f6873;
  font-weight: 700;
  object-fit: cover;
}

.user-chat-avatar {
  width: 40px;
  height: 40px;
  border-radius: 6px;
}

.assistant-chat-avatar {
  width: 48px;
  height: 72px;
  border-radius: 6px;
}

.time-divider {
  display: flex;
  justify-content: center;
  margin: 12px 0 8px;
}

.time-divider time {
  max-width: 86%;
  padding: 4px 8px;
  border-radius: 6px;
  background: #dfe3e8;
  color: #5f6873;
  font-size: 12px;
  line-height: 1.25;
}

.bubble {
  max-width: min(76%, 640px);
  padding: 9px 11px;
  border-radius: 8px;
  line-height: 1.45;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
  background: #fff;
}

.cm-aside {
  color: #7a3ff2;
  font-style: italic;
}

.message-stack .bubble,
.message-stack .thinking-block,
.message-stack .token-usage {
  max-width: 100%;
}

.bubble.sticker-bubble {
  padding: 4px;
  background: transparent;
}

.bubble.image-bubble {
  padding: 4px;
  background: transparent;
}

.sticker-image {
  display: block;
  max-width: min(34vw, 150px);
  max-height: 150px;
  border-radius: 8px;
  object-fit: contain;
}

.generated-image {
  display: block;
  width: min(58vw, 320px);
  max-height: min(54vh, 460px);
  border-radius: 8px;
  object-fit: contain;
  background: #eef0f3;
  cursor: zoom-in;
}

.generated-image-state,
.generated-image-failed {
  min-width: 132px;
  min-height: 40px;
  border: 1px solid #d9dde3;
  border-radius: 8px;
  background: #fff;
  color: #626b76;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 9px 12px;
  font-size: 13px;
}

.generated-image-failed {
  cursor: pointer;
}

.generated-image-failed:hover {
  background: #f7f8f9;
}

.image-decider-failure {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  max-width: 100%;
  padding: 7px 8px;
  border: 1px solid #ffd0d0;
  border-radius: 8px;
  background: #fff7f7;
  color: #a23a3a;
  font-size: 13px;
  line-height: 1.2;
}

.image-decider-failure button {
  border: 0;
  border-radius: 6px;
  background: #f0e7e7;
  color: #7a2f2f;
  padding: 5px 8px;
  cursor: pointer;
}

.image-decider-failure button:disabled {
  opacity: 0.55;
  cursor: default;
}

.action-options {
  width: min(76%, 640px);
  display: grid;
  gap: 0;
  margin-top: 5px;
  padding: 5px;
  border: 1px solid rgba(25, 180, 91, 0.36);
  border-left-width: 4px;
  border-radius: 10px;
  background: linear-gradient(180deg, rgba(255, 255, 255, 0.94), rgba(247, 252, 249, 0.94));
  box-shadow: 0 2px 8px rgba(20, 35, 28, 0.08);
  overflow: hidden;
}

.message-stack .action-options {
  max-width: 100%;
}

.action-option-button {
  width: 100%;
  min-height: 42px;
  border: 0;
  border-radius: 7px;
  background: transparent;
  color: #26332d;
  display: grid;
  grid-template-columns: 26px 1fr;
  gap: 10px;
  align-items: center;
  padding: 10px 11px;
  text-align: left;
  cursor: pointer;
  box-shadow: none;
  transition: background 0.16s ease, transform 0.16s ease;
}

.action-option-button + .action-option-button {
  margin-top: 2px;
  border-top: 1px solid rgba(32, 132, 86, 0.12);
}

.action-option-button:hover {
  background: rgba(25, 180, 91, 0.08);
  transform: translateY(-1px);
}

.action-option-button:active {
  transform: translateY(0);
}

.action-option-index {
  width: 22px;
  height: 22px;
  border-radius: 50%;
  background: #19b45b;
  color: #fff;
  display: grid;
  place-items: center;
  font-size: 12px;
  font-weight: 700;
  line-height: 1;
}

.action-option-text {
  min-width: 0;
  font-size: 14px;
  line-height: 1.4;
  overflow-wrap: anywhere;
}

.offline-mode .action-options {
  border-color: rgba(226, 83, 135, 0.38);
  background: linear-gradient(180deg, rgba(255, 255, 255, 0.94), rgba(255, 248, 251, 0.94));
}

.offline-mode .action-option-button:hover {
  background: rgba(226, 83, 135, 0.08);
}

.offline-mode .action-option-button + .action-option-button {
  border-top-color: rgba(226, 83, 135, 0.13);
}

.offline-mode .action-option-index {
  background: #e25387;
}

.image-spinner {
  width: 14px;
  height: 14px;
  border: 2px solid #cbd2da;
  border-top-color: #19b45b;
  border-radius: 50%;
  animation: image-spin 0.9s linear infinite;
}

.image-viewer-overlay {
  position: fixed;
  inset: 0;
  z-index: 1100;
  background: rgba(0, 0, 0, 0.82);
  display: grid;
  place-items: center;
  padding: 18px;
}

.image-viewer-img {
  max-width: min(100%, 1100px);
  max-height: 92vh;
  object-fit: contain;
  border-radius: 8px;
  background: #111;
}

.image-viewer-close {
  position: fixed;
  top: max(14px, env(safe-area-inset-top));
  right: 16px;
  width: 38px;
  height: 38px;
  border: 0;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.14);
  color: #fff;
  font-size: 28px;
  line-height: 1;
}

@keyframes image-spin {
  to { transform: rotate(360deg); }
}

.from-user .bubble {
  background: #95ec69;
}

.offline-mode .from-user .bubble {
  background: #d6e8ff;
}

.offline-mode .from-assistant .bubble {
  background: #ffd9e6;
}

.from-user .bubble.sticker-bubble {
  background: transparent;
}

.from-user .bubble.image-bubble {
  background: transparent;
}

.from-system .bubble {
  max-width: 86%;
  background: #e7eaee;
  color: #626b76;
  font-size: 13px;
}

.bubble.pending {
  color: #6b737c;
}

.composer {
  min-height: 62px;
  padding: 9px 10px calc(9px + env(safe-area-inset-bottom));
  background: #f4f5f6;
  border-top: 1px solid #e2e5e9;
  display: grid;
  grid-template-columns: 1fr 64px;
  gap: 8px;
  align-items: end;
}

.composer textarea {
  min-height: 42px;
  max-height: 110px;
  resize: none;
  padding: 10px;
}

.composer button {
  height: 42px;
}

.bubble.staged {
  outline: 1px dashed rgba(80, 110, 60, 0.5);
  opacity: 0.85;
}

.empty-state {
  padding: 30px 16px;
  text-align: center;
  color: #747c86;
}

.thinking-block {
  max-width: min(76%, 640px);
  margin: 0;
  padding: 6px 10px;
  background: rgba(180, 180, 180, 0.15);
  border-left: 3px solid rgba(120, 120, 120, 0.4);
  border-radius: 4px;
  font-size: 0.85em;
  color: rgba(80, 80, 80, 0.9);
  cursor: pointer;
}

.thinking-block summary {
  font-weight: 500;
  user-select: none;
}

.thinking-block pre {
  white-space: pre-wrap;
  word-break: break-word;
  margin: 6px 0 0;
  font-family: inherit;
  font-size: 0.95em;
  line-height: 1.4;
}

.from-user .thinking-block {
  display: none;
}

.token-usage {
  color: #8b949e;
  font-size: 11px;
  line-height: 1.2;
  font-variant-numeric: tabular-nums;
  user-select: none;
}

.scene-recap-card {
  width: min(76%, 640px);
  background: #ffffff;
  border: 1px solid #e4e7eb;
  border-radius: 10px;
  padding: 10px 12px;
}

.scene-recap-card summary {
  cursor: pointer;
  font-weight: 650;
  color: #111827;
}

.scene-recap-body {
  margin-top: 8px;
  color: #374151;
  white-space: pre-wrap;
}

.scene-end-status {
  text-align: center;
  color: #6b7280;
  font-size: 13px;
}
