:root {
  --bg: #14100f;
  --bg-alt: #1e1815;
  --card: #251d19;
  --ember: #d98a4a;
  --ember-bright: #f2a35e;
  --text: #f0e6dd;
  --text-dim: #b8a99c;
  --border: #3a2e28;
}

* { box-sizing: border-box; }

html, body {
  height: 100%;
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "PingFang TC", "Noto Sans TC", sans-serif;
}

#app {
  display: flex;
  flex-direction: column;
  height: 100dvh;
  max-width: 720px;
  margin: 0 auto;
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 16px;
  border-bottom: 1px solid var(--border);
  background: var(--bg-alt);
}

.topbar h1 {
  font-size: 18px;
  margin: 0;
  color: var(--ember-bright);
  letter-spacing: 2px;
}

.topbar-actions {
  display: flex;
  align-items: center;
  gap: 8px;
}

.whoami {
  font-size: 13px;
  color: var(--text-dim);
}

.auth-card {
  max-width: 360px;
  margin: 60px auto 0;
  padding: 24px;
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 16px;
}

.auth-card h2 {
  margin: 0 0 16px;
  color: var(--ember-bright);
  text-align: center;
}

.auth-card .field input {
  width: 100%;
  background: var(--bg);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 10px 12px;
  font-size: 14.5px;
}

.auth-error {
  color: #e07a5f;
  font-size: 13px;
  text-align: center;
  margin: 4px 0 12px;
}

.auth-card .ghost-btn {
  width: 100%;
  margin-top: 8px;
}

.hidden { display: none !important; }

.view {
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

#savesView, #gameSelectView { padding: 16px; overflow-y: auto; }

.section-hint {
  color: var(--text-dim);
  font-size: 14px;
  margin: 0 0 12px;
}

.primary-btn {
  width: 100%;
  padding: 14px;
  border: none;
  border-radius: 10px;
  background: linear-gradient(135deg, var(--ember), var(--ember-bright));
  color: #201512;
  font-size: 15px;
  font-weight: 600;
  cursor: pointer;
  margin-bottom: 16px;
}

.ghost-btn {
  background: none;
  border: 1px solid var(--border);
  color: var(--text-dim);
  padding: 8px 12px;
  border-radius: 8px;
  cursor: pointer;
  font-size: 13px;
}

.saves-list { list-style: none; margin: 0; padding: 0; }

.save-card {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 12px 14px;
  margin-bottom: 10px;
  cursor: pointer;
  transition: border-color 0.15s ease, transform 0.15s ease;
}

.save-card:hover { border-color: var(--ember); }
.save-card:active { opacity: 0.8; transform: scale(0.99); }

.home-hero {
  text-align: center;
  padding: 12px 0 24px;
}

.home-hero-title {
  margin: 0 0 4px;
  font-size: 22px;
  color: var(--ember-bright);
}

.home-hero-subtitle {
  margin: 0;
  font-size: 14px;
  color: var(--text-dim);
}

.game-grid {
  list-style: none;
  margin: 0;
  padding: 0;
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
  gap: 14px;
}

.game-card {
  display: flex;
  align-items: center;
  gap: 14px;
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 16px;
  cursor: pointer;
  transition: border-color 0.15s ease, transform 0.15s ease, box-shadow 0.15s ease;
}

.game-card:hover {
  border-color: var(--ember);
  transform: translateY(-2px);
  box-shadow: 0 6px 20px rgba(0, 0, 0, 0.25);
}

.game-card:active { transform: translateY(0) scale(0.99); }

.game-card-icon {
  font-size: 32px;
  line-height: 1;
  flex-shrink: 0;
  width: 48px;
  height: 48px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--bg-alt);
  border-radius: 12px;
}

.game-card-body .title {
  font-weight: 600;
  color: var(--ember-bright);
  margin-bottom: 4px;
}

.game-card-body .preview {
  font-size: 13px;
  color: var(--text-dim);
  line-height: 1.4;
}

.save-card .title { font-weight: 600; color: var(--ember-bright); margin-bottom: 4px; }
.save-card .meta { font-size: 12px; color: var(--text-dim); margin-bottom: 6px; }
.save-card .preview {
  font-size: 13px;
  color: var(--text-dim);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.save-card .row { display: flex; justify-content: space-between; align-items: center; gap: 8px; margin-top: 8px; }
.save-card .delete-btn {
  background: none;
  border: none;
  color: #b56a5a;
  font-size: 12px;
  cursor: pointer;
}

.empty-hint { color: var(--text-dim); text-align: center; margin-top: 40px; }

.log {
  flex: 1;
  overflow-y: auto;
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 20px;
}

.msg {
  white-space: pre-wrap;
  word-break: break-word;
  overflow-wrap: break-word;
  line-height: 1.9;
  font-size: 16px;
  letter-spacing: 0.01em;
}

.msg.assistant {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 18px 18px;
}

.msg.user {
  align-self: flex-end;
  max-width: 85%;
  background: #3a2416;
  border-radius: 12px;
  padding: 10px 14px;
  color: var(--ember-bright);
}

.msg .heading {
  display: block;
  color: var(--ember-bright);
  font-weight: 700;
  font-size: 1.05em;
  margin-bottom: 6px;
  letter-spacing: 0.02em;
}

.msg .status-block {
  color: var(--text-dim);
  font-size: 0.9em;
  line-height: 1.75;
  border-top: 1px dashed var(--border);
  margin-top: 14px;
  padding-top: 14px;
}

@media (max-width: 480px) {
  .msg {
    font-size: 17px;
    line-height: 2;
  }

  .msg.assistant {
    padding: 18px 16px;
    border-radius: 16px;
  }

  .log {
    padding: 14px 12px;
    gap: 22px;
  }
}

.msg.note {
  align-self: center;
  font-size: 12px;
  color: var(--text-dim);
  font-style: italic;
  padding: 4px 0;
}

.generating-hint {
  text-align: center;
  margin: 0;
  padding: 8px 12px;
  font-size: 13px;
  color: var(--ember-bright);
  background: var(--bg-alt);
  border-top: 1px solid var(--border);
  animation: pulse-hint 1.4s ease-in-out infinite;
}

@keyframes pulse-hint {
  0%, 100% { opacity: 0.55; }
  50% { opacity: 1; }
}

.toolbar {
  display: flex;
  justify-content: center;
  gap: 8px;
  padding: 6px 12px;
  border-top: 1px solid var(--border);
  background: var(--bg-alt);
}

.toolbar .ghost-btn:disabled {
  opacity: 0.5;
  cursor: default;
}

.input-bar {
  display: flex;
  gap: 8px;
  padding: 10px 12px;
  border-top: 1px solid var(--border);
  background: var(--bg-alt);
  padding-bottom: max(10px, env(safe-area-inset-bottom));
}

#playerInput {
  flex: 1;
  resize: none;
  max-height: 120px;
  background: var(--card);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 10px 12px;
  font-size: 16px;
  font-family: inherit;
}

#sendBtn {
  border: none;
  border-radius: 10px;
  padding: 0 18px;
  background: var(--ember);
  color: #201512;
  font-weight: 600;
  cursor: pointer;
}

#sendBtn:disabled { opacity: 0.5; }

.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.6);
  display: flex;
  align-items: flex-end;
  justify-content: center;
  z-index: 50;
}

@media (min-width: 560px) {
  .modal-overlay { align-items: center; }
}

.modal-card {
  background: var(--bg-alt);
  border: 1px solid var(--border);
  border-radius: 16px 16px 0 0;
  width: 100%;
  max-width: 480px;
  max-height: 88vh;
  overflow-y: auto;
  padding: 20px;
  padding-bottom: max(20px, env(safe-area-inset-bottom));
}

@media (min-width: 560px) {
  .modal-card { border-radius: 16px; }
}

.modal-card h2 {
  margin: 0 0 16px;
  font-size: 17px;
  color: var(--ember-bright);
}

.field { margin-bottom: 14px; }

.field label {
  display: block;
  font-size: 13px;
  color: var(--text-dim);
  margin-bottom: 6px;
}

.checkbox-group {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.checkbox-row {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 14px;
  color: var(--text);
  cursor: pointer;
}

.checkbox-row input[type='checkbox'] {
  width: 16px;
  height: 16px;
  flex-shrink: 0;
}

#dynamicFields textarea[data-custom-for] {
  margin-top: 8px;
}

.field input,
.field select,
.field textarea {
  width: 100%;
  background: var(--card);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 10px 12px;
  font-size: 14.5px;
  font-family: inherit;
  resize: vertical;
}

.inline-row {
  display: flex;
  gap: 8px;
}

.inline-row select,
.inline-row input {
  flex: 1;
}

.inline-row .ghost-btn {
  white-space: nowrap;
}

.modal-actions {
  display: flex;
  gap: 10px;
  margin-top: 8px;
}

.modal-actions .ghost-btn {
  flex: 1;
}

.modal-actions .primary-btn {
  flex: 2;
  margin-bottom: 0;
}
