/* ===== Reset & Base ===== */
*, *::before, *::after {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
  scrollbar-width: none;
}

*::-webkit-scrollbar {
  display: none;
}

:root {
  --bg: #0f0f1a;
  --bg-card: #1a1a2e;
  --bg-card-hover: #22223a;
  --primary: #6c5ce7;
  --primary-light: #a29bfe;
  --accent: #00cec9;
  --text: #e8e8f0;
  --text-secondary: #8888a8;
  --border: #2d2d4a;
  --danger: #ff6b6b;
  --radius: 16px;
  --radius-sm: 10px;
  --shadow: 0 4px 24px rgba(0, 0, 0, 0.3);
}

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC",
    "Hiragino Sans GB", "Microsoft YaHei", sans-serif;
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
  -webkit-font-smoothing: antialiased;
  overflow-x: hidden;
}

/* ===== Header ===== */
.header {
  position: sticky;
  top: 0;
  z-index: 100;
  padding: 24px 20px 20px;
  background: linear-gradient(180deg, var(--bg) 60%, transparent);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  text-align: center;
}

.header-title {
  font-size: 24px;
  font-weight: 700;
  background: linear-gradient(135deg, var(--primary-light), var(--accent));
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
  letter-spacing: 1px;
}

.header-subtitle {
  margin-top: 6px;
  font-size: 13px;
  color: var(--text-secondary);
}

/* ===== Loading ===== */
.loading {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding-top: 120px;
  gap: 16px;
  color: var(--text-secondary);
  font-size: 14px;
}

.spinner {
  width: 36px;
  height: 36px;
  border: 3px solid var(--border);
  border-top-color: var(--primary-light);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}

@keyframes spin {
  to { transform: rotate(360deg); }
}

/* ===== Error State ===== */
.error-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding-top: 100px;
  gap: 12px;
}

.error-icon {
  font-size: 48px;
  opacity: 0.7;
}

.error-msg {
  color: var(--text-secondary);
  font-size: 14px;
}

/* ===== Empty State ===== */
.empty-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding-top: 100px;
  gap: 12px;
  color: var(--text-secondary);
  font-size: 14px;
}

.empty-icon {
  font-size: 56px;
  opacity: 0.6;
}

/* ===== Buttons ===== */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border: none;
  cursor: pointer;
  font-size: 14px;
  font-weight: 600;
  border-radius: var(--radius-sm);
  transition: all 0.2s ease;
}

.btn-retry {
  margin-top: 8px;
  padding: 10px 28px;
  background: var(--primary);
  color: #fff;
}

.btn-retry:active {
  transform: scale(0.96);
  background: var(--primary-light);
}

.btn-play {
  padding: 10px 0;
  width: 100%;
  background: linear-gradient(135deg, var(--primary), #8b5cf6);
  color: #fff;
  font-size: 15px;
  border-radius: var(--radius-sm);
  letter-spacing: 0.5px;
}

.btn-play:active {
  transform: scale(0.97);
  opacity: 0.9;
}

.card-buttons {
  display: flex;
  flex-direction: column;
  gap: 8px;
  margin-top: 12px;
}
.btn-group-row {
  display: flex;
  gap: 8px;
}
.btn-group-row .btn {
  /* 默认比例，可以在具体的按钮类上覆盖 */
  flex: 1;
}
.btn-group-row .btn-continue {
  flex: 7;
}
.btn-group-row .btn-new {
  flex: 3;
}

.btn-continue {
  flex: 1;
  padding: 10px 0;
  background: linear-gradient(135deg, var(--primary), #8b5cf6);
  color: #fff;
  font-size: 15px;
  border-radius: var(--radius-sm);
  letter-spacing: 0.5px;
}

.btn-continue:active {
  transform: scale(0.97);
  opacity: 0.9;
}

.btn-new {
  padding: 10px 16px;
  background: transparent;
  color: var(--text-secondary);
  font-size: 13px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
}

.btn-new:active {
  background: var(--bg-card-hover);
}

/* ===== Settings Button ===== */
.btn-settings {
  position: absolute;
  right: 0;
  background: transparent;
  border: none;
  color: var(--text-secondary);
  cursor: pointer;
  padding: 4px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 0.2s;
}

.btn-settings:active {
  background: rgba(255, 255, 255, 0.1);
  color: var(--text);
}

/* ===== Modal ===== */
.modal-overlay {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(0, 0, 0, 0.6);
  backdrop-filter: blur(4px);
  -webkit-backdrop-filter: blur(4px);
  z-index: 1000;
  display: flex;
  align-items: flex-end;
  transition: opacity 0.3s ease;
}

.modal-overlay.hidden {
  display: none !important;
}

.modal-content {
  background: var(--bg-card);
  width: 100%;
  border-radius: 24px 24px 0 0;
  padding: 24px 20px 32px;
  animation: slideUp 0.3s ease both;
  border-top: 1px solid var(--border);
}

@keyframes slideUp {
  from { transform: translateY(100%); }
  to { transform: translateY(0); }
}

.modal-title {
  font-size: 18px;
  font-weight: 700;
  margin-bottom: 20px;
  text-align: center;
  color: var(--text);
}

.form-group {
  margin-bottom: 16px;
  text-align: left;
}

.form-group-switch {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding-bottom: 16px;
  border-bottom: 1px solid var(--border);
  margin-bottom: 20px;
}

.form-group-switch .form-label {
  margin-bottom: 0;
}

/* ===== Toggle Switch ===== */
.switch {
  position: relative;
  display: inline-block;
  width: 52px;
  height: 28px;
  flex-shrink: 0;
}

.switch input {
  opacity: 0;
  width: 0;
  height: 0;
}

.switch-slider {
  position: absolute;
  cursor: pointer;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(255, 255, 255, 0.15);
  border-radius: 28px;
  transition: background 0.3s ease;
}

.switch-slider::before {
  content: '';
  position: absolute;
  height: 22px;
  width: 22px;
  left: 3px;
  bottom: 3px;
  background: #fff;
  border-radius: 50%;
  transition: transform 0.3s ease;
}

.switch input:checked + .switch-slider {
  background: var(--primary);
}

.switch input:checked + .switch-slider::before {
  transform: translateX(24px);
}

#own-api-fields.hidden {
  display: none !important;
}

.form-label {
  display: block;
  font-size: 14px;
  color: var(--text-secondary);
  margin-bottom: 8px;
}

.form-input, .form-select {
  width: 100%;
  background: var(--bg);
  border: 1px solid var(--border);
  color: var(--text);
  font-size: 15px;
  padding: 12px 16px;
  border-radius: 12px;
  outline: none;
  transition: border-color 0.2s;
  font-family: inherit;
}

.form-input:focus, .form-select:focus {
  border-color: var(--primary);
}

.form-select {
  appearance: none;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='16' height='16' viewBox='0 0 24 24' fill='none' stroke='%238888a8' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='M6 9l6 6 6-6'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 16px center;
  padding-right: 40px;
}

.form-select option {
  background: var(--bg-card);
  color: var(--text);
}

/* ===== Custom Select ===== */
.custom-select-wrap {
  position: relative;
}

.custom-select-input {
  cursor: pointer;
  padding-right: 40px;
}

.model-input-group {
  display: flex;
  gap: 8px;
  align-items: flex-start;
}

.model-input-group .custom-select-wrap {
  flex: 1;
}

.btn-fetch-models {
  background: var(--primary);
  border: none;
  color: #fff;
  padding: 12px 20px;
  border-radius: 12px;
  font-size: 14px;
  font-weight: 500;
  cursor: pointer;
  transition: background 0.2s;
  white-space: nowrap;
  flex-shrink: 0;
  height: 46px;
}

.btn-fetch-models:hover {
  background: var(--primary-light);
}

.btn-fetch-models:active {
  transform: scale(0.98);
}

.btn-fetch-models:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.btn-fetch-models.loading {
  position: relative;
  color: transparent;
}

.btn-fetch-models.loading::after {
  content: '';
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  width: 16px;
  height: 16px;
  border: 2px solid rgba(255, 255, 255, 0.3);
  border-top-color: #fff;
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}

.custom-select-input[readonly] {
  background: var(--bg);
  opacity: 0.8;
}

.custom-dropdown {
  position: absolute;
  left: 0;
  right: 0;
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: 12px;
  max-height: 240px;
  overflow-y: auto;
  z-index: 1001;
  box-shadow: var(--shadow);
  scrollbar-width: thin;
  scrollbar-color: var(--border) transparent;
}

.custom-dropdown.dropdown-bottom {
  top: calc(100% + 4px);
}

.custom-dropdown.dropdown-top {
  bottom: calc(100% + 4px);
}

.custom-dropdown::-webkit-scrollbar {
  width: 6px;
}

.custom-dropdown::-webkit-scrollbar-track {
  background: transparent;
}

.custom-dropdown::-webkit-scrollbar-thumb {
  background: var(--border);
  border-radius: 3px;
}

.custom-dropdown.hidden {
  display: none !important;
}

.dropdown-item {
  padding: 12px 16px;
  cursor: pointer;
  transition: background 0.15s;
  font-size: 15px;
  color: var(--text);
}

.dropdown-item:first-child {
  border-radius: 12px 12px 0 0;
}

.dropdown-item:last-child {
  border-radius: 0 0 12px 12px;
}

.dropdown-item:only-child {
  border-radius: 12px;
}

.dropdown-item:hover,
.dropdown-item.active {
  background: var(--primary);
  color: #fff;
}

.dropdown-item.empty {
  color: var(--text-secondary);
  cursor: default;
  text-align: center;
  padding: 16px;
}

.dropdown-item.empty:hover {
  background: transparent;
  color: var(--text-secondary);
}

.modal-actions {
  display: flex;
  gap: 12px;
  margin-top: 24px;
}

.btn-cancel {
  flex: 1;
  padding: 12px 0;
  background: transparent;
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  font-size: 15px;
}

.btn-cancel:active {
  background: var(--bg-card-hover);
}

.btn-save {
  flex: 1;
  padding: 12px 0;
  background: linear-gradient(135deg, var(--primary), #8b5cf6);
  color: #fff;
  border: none;
  border-radius: var(--radius-sm);
  font-size: 15px;
}

.btn-save:active {
  transform: scale(0.97);
  opacity: 0.9;
}

.btn-test {
  flex: 1;
  padding: 12px 0;
  background: linear-gradient(135deg, var(--accent), #00b4d8);
  color: #fff;
  border: none;
  border-radius: var(--radius-sm);
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  transition: transform 0.15s, opacity 0.15s;
}

.btn-test:active {
  transform: scale(0.97);
  opacity: 0.9;
}

/* ===== Search Bar ===== */
.search-bar {
  padding: 0 16px 12px;
}

.search-input-wrap {
  display: flex;
  align-items: center;
  gap: 8px;
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 6px 6px 6px 14px;
  transition: border-color 0.2s;
}

.search-input-wrap:focus-within {
  border-color: var(--primary);
}

.search-input {
  flex: 1;
  border: none;
  background: transparent;
  color: var(--text);
  font-size: 14px;
  outline: none;
  font-family: inherit;
  line-height: 1.5;
  padding: 4px 0;
}

.search-input::placeholder {
  color: var(--text-secondary);
}

.btn-clear-search {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 28px;
  height: 28px;
  border: none;
  background: rgba(255, 255, 255, 0.08);
  color: var(--text-secondary);
  border-radius: 50%;
  cursor: pointer;
  flex-shrink: 0;
  transition: all 0.2s;
}

.btn-clear-search:active {
  background: rgba(255, 255, 255, 0.15);
  color: var(--text);
}

.btn-search-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 36px;
  height: 36px;
  border: none;
  background: linear-gradient(135deg, var(--primary), #8b5cf6);
  color: #fff;
  border-radius: 10px;
  cursor: pointer;
  flex-shrink: 0;
  transition: opacity 0.2s, transform 0.2s;
}

.btn-search-btn:active {
  transform: scale(0.92);
  opacity: 0.9;
}

/* ===== Tag List ===== */
.tag-list {
  display: flex;
  gap: 10px;
  padding: 0 16px 16px;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  scrollbar-width: none;
}

.tag-list::-webkit-scrollbar {
  display: none;
}

.tag-item {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 6px 16px;
  background: var(--bg-card);
  color: var(--text-secondary);
  border-radius: 20px;
  font-size: 13px;
  white-space: nowrap;
  border: 1px solid var(--border);
  transition: all 0.2s;
  cursor: pointer;
}

.tag-item:active {
  transform: scale(0.97);
}

.tag-item.active {
  background: linear-gradient(135deg, var(--primary), var(--primary-light));
  color: #fff;
  border-color: transparent;
}

/* ===== Game List ===== */
.game-list {
  padding: 0 16px 32px;
  display: flex;
  flex-direction: column;
  gap: 16px;
}

/* ===== Game Card ===== */
.game-card {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 20px;
  transition: all 0.25s ease;
  animation: fadeUp 0.4s ease both;
}

.game-card:active {
  background: var(--bg-card-hover);
  transform: scale(0.98);
}

@keyframes fadeUp {
  from {
    opacity: 0;
    transform: translateY(16px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.game-card:nth-child(1) { animation-delay: 0s; }
.game-card:nth-child(2) { animation-delay: 0.08s; }
.game-card:nth-child(3) { animation-delay: 0.16s; }
.game-card:nth-child(4) { animation-delay: 0.24s; }
.game-card:nth-child(5) { animation-delay: 0.32s; }

.card-top {
  display: flex;
  align-items: flex-start;
  gap: 14px;
  margin-bottom: 14px;
}

.card-icon {
  flex-shrink: 0;
  width: 52px;
  height: 52px;
  border-radius: 14px;
  background: linear-gradient(135deg, var(--primary), var(--accent));
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 24px;
  color: #fff;
  font-weight: 700;
}

.card-cover {
  flex-shrink: 0;
  width: 52px;
  height: 52px;
  max-width: 52px;
  max-height: 52px;
  min-width: 52px;
  min-height: 52px;
  border-radius: 14px;
  object-fit: cover;
}

.card-info {
  flex: 1;
  min-width: 0;
}

.card-name {
  font-size: 17px;
  font-weight: 700;
  color: var(--text);
  line-height: 1.3;
  margin-bottom: 4px;
}

.card-desc {
  font-size: 13px;
  color: var(--text-secondary);
  line-height: 1.5;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.card-tags {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-bottom: 16px;
}

.tag {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 4px 10px;
  font-size: 11px;
  color: var(--text-secondary);
  background: rgba(108, 92, 231, 0.1);
  border-radius: 6px;
  border: 1px solid rgba(108, 92, 231, 0.15);
}

.tag-accent {
  color: var(--accent);
  background: rgba(0, 206, 201, 0.08);
  border-color: rgba(0, 206, 201, 0.15);
}

.tag-free {
  color: #22c55e;
  background: rgba(34, 197, 94, 0.1);
  border-color: rgba(34, 197, 94, 0.18);
}

/* ===== Utility ===== */
.hidden {
  display: none !important;
}

/* ===== Page Transitions ===== */
.page-enter {
  animation: slideIn 0.35s ease both;
}

@keyframes slideIn {
  from {
    opacity: 0;
    transform: translateX(40px);
  }
  to {
    opacity: 1;
    transform: translateX(0);
  }
}

/* ===== Game Page (Chat) ===== */
.game-page {
  display: flex;
  flex-direction: column;
  height: 100vh;
  height: 100dvh;
  will-change: transform, opacity;
}

/* Chat Header */
.chat-header {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 16px;
  background: var(--bg-card);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}

.btn-back {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 36px;
  height: 36px;
  border: none;
  background: rgba(108, 92, 231, 0.12);
  color: var(--primary-light);
  border-radius: 10px;
  cursor: pointer;
  flex-shrink: 0;
  transition: background 0.2s;
}

.btn-back:active {
  background: rgba(108, 92, 231, 0.25);
}

.chat-header-info {
  display: flex;
  align-items: center;
  gap: 10px;
  min-width: 0;
}

.chat-header-title {
  font-size: 16px;
  font-weight: 700;
  color: var(--text);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.chat-header-badge {
  flex-shrink: 0;
  font-size: 11px;
  padding: 2px 8px;
  border-radius: 6px;
  background: rgba(0, 206, 201, 0.1);
  color: var(--accent);
  border: 1px solid rgba(0, 206, 201, 0.2);
}

/* Chat Messages */
.chat-messages {
  flex: 1;
  overflow-y: auto;
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 16px;
  -webkit-overflow-scrolling: touch;
}

.msg {
  max-width: 88%;
  padding: 14px 16px;
  border-radius: 18px;
  font-size: 14px;
  line-height: 1.7;
  word-break: break-word;
  animation: msgIn 0.3s ease both;
}

@keyframes msgIn {
  from {
    opacity: 0;
    transform: translateY(8px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.msg-ai {
  align-self: flex-start;
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-bottom-left-radius: 4px;
  color: var(--text);
}

.msg-ai strong {
  color: var(--primary-light);
}

.msg-ai hr {
  border: none;
  height: 1px;
  background: var(--border);
  margin: 10px 0;
}

.msg-ai .status-card {
  background: rgba(108, 92, 231, 0.08);
  border: 1px solid rgba(108, 92, 231, 0.2);
  border-radius: 10px;
  padding: 12px 14px;
  margin: 8px 0;
  font-size: 13px;
  line-height: 1.7;
  color: var(--text);
}

.msg-user {
  align-self: flex-end;
  background: linear-gradient(135deg, var(--primary), #8b5cf6);
  color: #fff;
  border-bottom-right-radius: 4px;
}

/* AI Typing Indicator */
.msg-typing {
  align-self: flex-start;
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-bottom-left-radius: 4px;
  padding: 14px 20px;
  display: flex;
  gap: 5px;
  align-items: center;
}

.typing-dot {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--text-secondary);
  animation: typingBounce 1.2s ease-in-out infinite;
}

.typing-dot:nth-child(2) { animation-delay: 0.15s; }
.typing-dot:nth-child(3) { animation-delay: 0.3s; }

@keyframes typingBounce {
  0%, 60%, 100% {
    transform: translateY(0);
    opacity: 0.4;
  }
  30% {
    transform: translateY(-6px);
    opacity: 1;
  }
}

/* Chat Input */
.chat-input-area {
  flex-shrink: 0;
  padding: 12px 16px;
  background: var(--bg);
  border-top: 1px solid var(--border);
}

.chat-input-wrap {
  display: flex;
  align-items: flex-end;
  gap: 10px;
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 6px 6px 6px 16px;
  transition: border-color 0.2s;
}

.chat-input-wrap:focus-within {
  border-color: var(--primary);
}

.chat-input {
  flex: 1;
  border: none;
  background: transparent;
  color: var(--text);
  font-size: 15px;
  line-height: 1.5;
  resize: none;
  outline: none;
  max-height: 120px;
  padding: 6px 0;
  font-family: inherit;
}

.chat-input::placeholder {
  color: var(--text-secondary);
}

.btn-send {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 40px;
  height: 40px;
  border: none;
  background: linear-gradient(135deg, var(--primary), #8b5cf6);
  color: #fff;
  border-radius: 10px;
  cursor: pointer;
  flex-shrink: 0;
  transition: opacity 0.2s, transform 0.2s;
}

.btn-send:disabled {
  opacity: 0.4;
  cursor: not-allowed;
}

.btn-send:not(:disabled):active {
  transform: scale(0.92);
}

/* ===== Choice Buttons (inline in msg bubble) ===== */
.msg-ai .btn-choice {
  display: flex;
  align-items: center;
  gap: 10px;
  width: 100%;
  margin: 4px 0;
  padding: 10px 14px;
  background: rgba(108, 92, 231, 0.08);
  border: 1px solid rgba(108, 92, 231, 0.25);
  border-radius: 10px;
  cursor: pointer;
  transition: all 0.2s ease;
  font-family: inherit;
  font-size: 13px;
  color: var(--text);
  text-align: left;
  line-height: 1.5;
}

.msg-ai .btn-choice:active {
  transform: scale(0.97);
  background: rgba(108, 92, 231, 0.2);
}

.msg-ai .btn-choice:disabled {
  cursor: default;
  pointer-events: none;
}

.msg-ai .btn-choice .choice-key {
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 28px;
  height: 28px;
  border-radius: 8px;
  background: linear-gradient(135deg, var(--primary), #8b5cf6);
  color: #fff;
  font-size: 13px;
  font-weight: 700;
}

.msg-ai .btn-choice .choice-text {
  flex: 1;
  min-width: 0;
}

.msg-ai .btn-choice-selected {
  background: linear-gradient(135deg, var(--primary), #8b5cf6);
  border-color: transparent;
  color: #fff;
}

.msg-ai .btn-choice-selected .choice-key {
  background: rgba(255, 255, 255, 0.25);
}

.msg-ai .btn-choice-dim {
  opacity: 0.3;
}

/* ===== Safe Area for iPhone ===== */
@supports (padding-bottom: env(safe-area-inset-bottom)) {
  .game-list {
    padding-bottom: calc(32px + env(safe-area-inset-bottom));
  }
  .header {
    padding-top: calc(24px + env(safe-area-inset-top));
  }
}