* { margin: 0; padding: 0; box-sizing: border-box; }

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  background: #fdf2f7;
  color: #333;
  height: 100dvh;
  overflow: hidden;
}

html { height: 100dvh; }

/* -- Screens -- */
.screen {
  display: none;
  flex-direction: column;
  height: 100dvh;
  width: 100%;
  max-width: 480px;
  margin: 0 auto;
}
.screen.active { display: flex; }

/* -- Header -- */
.header {
  padding: 16px 20px;
  text-align: center;
  background: #fff;
  border-bottom: 1px solid #f0dce8;
  position: relative;
}
.header h1 {
  font-size: 18px;
  font-weight: 600;
  color: #6a3d5c;
}
.header p {
  font-size: 13px;
  color: #a07090;
  margin-top: 4px;
}

/* -- Back button -- */
.back-btn {
  position: absolute;
  left: 12px;
  top: 50%;
  transform: translateY(-50%);
  background: none;
  border: none;
  font-size: 22px;
  color: #6a3d5c;
  cursor: pointer;
  padding: 4px 8px;
}

/* -- Login Screen -- */
.login-bg {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 30px;
  background: linear-gradient(160deg, #fdf2f7 0%, #f5e6f0 50%, #ece0f5 100%);
  gap: 16px;
}
.login-card {
  background: #fff;
  border-radius: 20px;
  padding: 32px 28px 28px;
  width: 100%;
  max-width: 320px;
  box-shadow: 0 2px 16px rgba(180,100,150,0.1);
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 14px;
  position: relative;
}
.login-title {
  font-size: 26px;
  font-weight: 700;
  background: linear-gradient(135deg, #d4619c, #a855c7);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}
.login-subtitle {
  font-size: 13px;
  color: #a07090;
  margin-top: -8px;
  margin-bottom: 4px;
}
.login-input {
  width: 100%;
  padding: 11px 14px;
  border: 1px solid #ecd8e5;
  border-radius: 10px;
  font-size: 15px;
  background: #fdf8fb;
  color: #333;
  outline: none;
  transition: border-color 0.2s;
}
.login-input:focus { border-color: #d4619c; }
.login-btn {
  width: 44px;
  height: 44px;
  border-radius: 50%;
  background: linear-gradient(135deg, #d4619c, #b855b0);
  color: #fff;
  border: none;
  font-size: 20px;
  cursor: pointer;
  margin-top: 4px;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: opacity 0.2s;
}
.login-btn:active { opacity: 0.7; }
.login-hint {
  font-size: 12px;
  color: #b090a0;
}

/* -- Home Screen -- */
#home-screen { position: relative; }
.home-header {
  padding: 14px 20px;
  background: #fff;
  border-bottom: 1px solid #f0dce8;
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-shrink: 0;
}
.home-user-name {
  font-size: 16px;
  font-weight: 600;
  color: #6a3d5c;
}
.btn-icon {
  background: none;
  border: none;
  font-size: 18px;
  color: #a07090;
  cursor: pointer;
  padding: 4px;
}
.home-body {
  flex: 1;
  overflow-y: auto;
  padding: 20px;
  -webkit-overflow-scrolling: touch;
}
.home-section-title {
  font-size: 18px;
  font-weight: 600;
  color: #6a3d5c;
  margin-bottom: 16px;
}

/* Character grid */
.character-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 14px;
  margin-bottom: 20px;
}
.character-card {
  background: #fff;
  border-radius: 16px;
  padding: 14px;
  text-align: center;
  cursor: pointer;
  transition: transform 0.15s, box-shadow 0.15s;
  box-shadow: 0 2px 10px rgba(180,100,150,0.08);
  position: relative;
}
.character-card:active {
  transform: scale(0.97);
}
.character-card-img-wrap {
  width: 100%;
  aspect-ratio: 1;
  border-radius: 12px;
  overflow: hidden;
  background: #fdf2f7;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-bottom: 10px;
}
.character-card-img-wrap img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}
.character-card-placeholder {
  font-size: 40px;
  color: #d4619c;
  font-weight: 700;
}
.character-card-name {
  font-size: 14px;
  font-weight: 600;
  color: #6a3d5c;
}
.character-card-memory {
  font-size: 11px;
  color: #d4619c;
  margin-top: 4px;
}
.character-delete-btn {
  position: absolute;
  top: 6px;
  right: 6px;
  background: rgba(0,0,0,0.3);
  color: #fff;
  border: none;
  border-radius: 50%;
  width: 24px;
  height: 24px;
  font-size: 14px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  opacity: 0;
  transition: opacity 0.2s;
}
.character-card:hover .character-delete-btn,
.character-card:active .character-delete-btn {
  opacity: 1;
}
.character-empty, .character-loading {
  grid-column: 1 / -1;
  text-align: center;
  color: #a07090;
  padding: 40px 0;
  font-size: 14px;
  line-height: 1.6;
}
.character-add-btn {
  width: 100%;
}
/* -- FAB feedback button -- */
.fab-feedback {
  position: absolute;
  bottom: 24px;
  right: 20px;
  width: 52px;
  height: 52px;
  border-radius: 50%;
  background: linear-gradient(135deg, #d4619c, #b855b0);
  border: none;
  cursor: pointer;
  box-shadow: 0 3px 14px rgba(180,80,150,0.3);
  display: flex;
  align-items: center;
  justify-content: center;
  transition: transform 0.15s;
  z-index: 10;
}
.fab-feedback:active { transform: scale(0.9); }
.fab-feedback img {
  width: 26px;
  height: 26px;
  filter: brightness(0) invert(1);
}

/* -- Modal -- */
.modal-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.4);
  z-index: 150;
  align-items: center;
  justify-content: center;
  padding: 20px;
}
.modal-overlay.active { display: flex; }
.modal-card {
  background: #fff;
  border-radius: 16px;
  padding: 24px;
  width: 100%;
  max-width: 360px;
  box-shadow: 0 8px 30px rgba(0,0,0,0.15);
}
.modal-title {
  font-size: 16px;
  font-weight: 600;
  color: #6a3d5c;
  margin-bottom: 14px;
}
.modal-textarea {
  width: 100%;
  padding: 12px;
  border: 1px solid #ecd8e5;
  border-radius: 10px;
  font-size: 15px;
  resize: vertical;
  min-height: 100px;
  outline: none;
  color: #333;
  background: #fdf8fb;
}
.modal-textarea:focus { border-color: #d4619c; }
.modal-actions {
  display: flex;
  gap: 10px;
  margin-top: 14px;
}

/* -- Upload Screen -- */
#upload-screen .upload-container {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 20px;
  background: linear-gradient(180deg, #fdf2f7, #f5e6f0);
}
.upload-area {
  width: 260px;
  height: 260px;
  border: 3px dashed #ddb8d0;
  border-radius: 20px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: border-color 0.2s, background 0.2s;
  background: #fff;
}
.upload-area:active { background: #fdf2f7; }
.upload-area.hidden { display: none; }
.upload-icon {
  font-size: 48px;
  color: #d4619c;
  line-height: 1;
  margin-bottom: 8px;
}
.upload-text {
  font-size: 14px;
  color: #a07090;
}
.upload-selected {
  display: none;
  flex-direction: column;
  align-items: center;
  gap: 20px;
  width: 100%;
}
.upload-selected.show { display: flex; }
.upload-selected img {
  max-width: 80%;
  max-height: 50dvh;
  border-radius: 16px;
  box-shadow: 0 4px 20px rgba(180,100,150,0.15);
}
.upload-actions {
  display: flex;
  gap: 12px;
  width: 100%;
  padding: 0 20px;
}

/* -- Segmentation Screen -- */
#segment-screen .segment-container {
  flex: 1;
  position: relative;
  overflow: hidden;
  display: flex;
  align-items: center;
  justify-content: center;
  background: #fdf2f7;
}
#segment-screen .segment-container img {
  max-width: 100%;
  max-height: 100%;
  cursor: crosshair;
}
.segment-instruction {
  position: absolute;
  top: 16px;
  left: 16px;
  right: 16px;
  background: rgba(106,61,92,0.85);
  color: #fff;
  padding: 10px 16px;
  border-radius: 10px;
  font-size: 14px;
  text-align: center;
  z-index: 1;
}

/* -- Cutout Confirmation -- */
#cutout-screen .cutout-container {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  background: #fdf2f7;
  padding: 20px;
}
#cutout-screen .cutout-container img {
  max-width: 80%;
  max-height: 60%;
  border-radius: 16px;
  box-shadow: 0 4px 20px rgba(180,100,150,0.15);
}
.cutout-actions {
  padding: 20px;
  display: flex;
  gap: 12px;
}

/* -- Form Screen -- */
#form-screen .form-container {
  flex: 1;
  overflow-y: auto;
  padding: 20px;
  -webkit-overflow-scrolling: touch;
}
.form-group {
  margin-bottom: 18px;
}
.form-group label {
  display: block;
  font-size: 14px;
  font-weight: 600;
  color: #6a3d5c;
  margin-bottom: 6px;
}
.form-group input,
.form-group textarea,
.form-group select {
  width: 100%;
  padding: 12px 14px;
  border: 1px solid #ecd8e5;
  border-radius: 10px;
  font-size: 16px;
  background: #fff;
  color: #333;
  outline: none;
  transition: border-color 0.2s;
}
.form-group input:focus,
.form-group textarea:focus,
.form-group select:focus {
  border-color: #d4619c;
}
.form-group textarea { resize: vertical; min-height: 80px; }
.form-group .hint {
  font-size: 12px;
  color: #b090a0;
  margin-top: 4px;
}

/* -- Chat Screen -- */
#chat-screen {
  height: 100%;
  height: 100dvh;
}
#chat-screen .chat-header {
  padding: 12px 20px;
  background: #fff;
  border-bottom: 1px solid #f0dce8;
  display: flex;
  align-items: center;
  gap: 12px;
  flex-shrink: 0;
  position: relative;
}
#chat-screen .chat-header .back-btn {
  position: static;
  transform: none;
}
.chat-input { flex-shrink: 0; }
.chat-header .avatar {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  object-fit: cover;
  background: #f0dce8;
}
.chat-header .name {
  font-size: 16px;
  font-weight: 600;
  color: #6a3d5c;
}
.chat-messages {
  flex: 1;
  overflow-y: auto;
  padding: 16px 20px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  -webkit-overflow-scrolling: touch;
}
.message {
  max-width: 80%;
  padding: 10px 14px;
  border-radius: 16px;
  font-size: 15px;
  line-height: 1.5;
  word-break: break-word;
  white-space: pre-wrap;
}
.message.assistant {
  align-self: flex-start;
  background: #fff;
  color: #333;
  border-bottom-left-radius: 4px;
}
.message.user {
  align-self: flex-end;
  background: linear-gradient(135deg, #d4619c, #b855b0);
  color: #fff;
  border-bottom-right-radius: 4px;
}
.message.typing {
  align-self: flex-start;
  background: #fff;
  color: #c090b0;
  font-style: italic;
}
.chat-input {
  padding: 12px 16px;
  background: #fff;
  border-top: 1px solid #f0dce8;
  display: flex;
  gap: 10px;
  align-items: flex-end;
}
.chat-input textarea {
  flex: 1;
  padding: 10px 14px;
  border: 1px solid #ecd8e5;
  border-radius: 20px;
  font-size: 15px;
  resize: none;
  outline: none;
  max-height: 100px;
  line-height: 1.4;
}
.chat-input textarea:focus { border-color: #d4619c; }

/* -- Buttons -- */
.btn {
  padding: 14px 24px;
  border: none;
  border-radius: 12px;
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
  transition: opacity 0.2s;
  text-align: center;
}
.btn:disabled { opacity: 0.5; cursor: not-allowed; }
.btn-primary {
  background: linear-gradient(135deg, #d4619c, #b855b0);
  color: #fff;
  flex: 1;
}
.btn-secondary {
  background: #f0dce8;
  color: #6a3d5c;
  flex: 1;
}
.btn-send {
  width: 44px;
  height: 44px;
  border-radius: 50%;
  background: linear-gradient(135deg, #d4619c, #b855b0);
  color: #fff;
  border: none;
  font-size: 20px;
  cursor: pointer;
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: center;
}
.btn-send:disabled { opacity: 0.4; }

/* -- Loading overlay -- */
.loading-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(253,242,247,0.92);
  z-index: 100;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 16px;
}
.loading-overlay.active { display: flex; }
.spinner {
  width: 40px;
  height: 40px;
  border: 3px solid #ecd8e5;
  border-top-color: #d4619c;
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }
.loading-text {
  font-size: 15px;
  color: #6a3d5c;
}

/* -- Error toast -- */
.toast {
  display: none;
  position: fixed;
  bottom: 100px;
  left: 50%;
  transform: translateX(-50%);
  background: #d32f2f;
  color: #fff;
  padding: 12px 24px;
  border-radius: 10px;
  font-size: 14px;
  z-index: 200;
  max-width: 90%;
  text-align: center;
}
.toast.show {
  display: block;
  animation: fadeInOut 3s ease;
}
@keyframes fadeInOut {
  0% { opacity: 0; transform: translateX(-50%) translateY(20px); }
  15% { opacity: 1; transform: translateX(-50%) translateY(0); }
  85% { opacity: 1; }
  100% { opacity: 0; }
}
