:root {
  --bg: #0b0b12;
  --surface: #13131e;
  --surface2: #1b1b2a;
  --border: #272738;
  --accent: #7c3aed;
  --accent-glow: rgba(124, 58, 237, 0.35);
  --cyan: #06b6d4;
  --text: #e2e2f0;
  --muted: #6b6b85;
  --danger: #ef4444;
  --radius: 12px;
}

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

body {
  background: var(--bg);
  color: var(--text);
  font-family: system-ui, -apple-system, 'Segoe UI', sans-serif;
  font-size: 15px;
  line-height: 1.5;
  min-height: 100vh;
}

/* ── Layout ── */
#app {
  max-width: 1060px;
  margin: 0 auto;
  padding: 2.5rem 1.5rem 3rem;
  display: flex;
  flex-direction: column;
  gap: 1.25rem;
}

/* ── Header ── */
header { text-align: center; margin-bottom: 0.5rem; }

.logo {
  display: inline-flex;
  align-items: center;
  gap: 0.6rem;
  margin-bottom: 0.35rem;
}

.logo-icon { font-size: 1.9rem; }

h1 {
  font-size: 1.9rem;
  font-weight: 700;
  letter-spacing: -0.02em;
  background: linear-gradient(130deg, #a78bfa 10%, var(--cyan) 90%);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}

.subtitle {
  color: var(--muted);
  font-size: 0.95rem;
}

/* ── Prompt section ── */
.prompt-section {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1.25rem 1.5rem;
}

.examples {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.4rem;
  margin-bottom: 1rem;
}

.examples-label {
  font-size: 0.8rem;
  color: var(--muted);
  margin-right: 0.1rem;
}

.chip {
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: 999px;
  color: var(--muted);
  font-size: 0.78rem;
  padding: 0.28rem 0.75rem;
  cursor: pointer;
  transition: color 0.15s, border-color 0.15s, background 0.15s;
}

.chip:hover {
  color: var(--text);
  border-color: var(--accent);
  background: rgba(124, 58, 237, 0.08);
}

.input-row {
  display: flex;
  gap: 0.875rem;
  align-items: stretch;
}

.input-right {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
  min-width: 130px;
}

.model-toggle {
  display: flex;
  align-items: center;
  gap: 0.35rem;
  padding: 0.45rem 0.6rem;
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: 8px;
}

.model-label {
  font-size: 0.7rem;
  font-weight: 600;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.05em;
  margin-right: 0.15rem;
  flex-shrink: 0;
}

.model-btn {
  display: flex;
  flex-direction: row;
  align-items: center;
  gap: 0.25rem;
  background: transparent;
  border: 1px solid transparent;
  border-radius: 6px;
  color: var(--muted);
  padding: 0.2rem 0.5rem;
  font-size: 0.78rem;
  cursor: pointer;
  transition: border-color 0.15s, color 0.15s, background 0.15s;
  white-space: nowrap;
}

.model-btn:hover {
  border-color: var(--border);
  color: var(--text);
}

.model-btn.active {
  border-color: var(--accent);
  background: rgba(124, 58, 237, 0.15);
  color: var(--text);
}

.model-icon {
  font-size: 0.8rem;
}

.model-name {
  font-size: 0.78rem;
  font-weight: 600;
}

textarea {
  flex: 1;
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: 8px;
  color: var(--text);
  font-size: 0.93rem;
  font-family: inherit;
  padding: 0.8rem 1rem;
  min-height: 82px;
  resize: vertical;
  transition: border-color 0.15s, box-shadow 0.15s;
  line-height: 1.55;
}

textarea:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-glow);
}

textarea::placeholder { color: var(--muted); }

.generate-btn {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 0.2rem;
  background: linear-gradient(145deg, var(--accent), #5b21b6);
  border: none;
  border-radius: 8px;
  color: #fff;
  font-size: 0.93rem;
  font-weight: 600;
  padding: 0 1.4rem;
  cursor: pointer;
  transition: opacity 0.15s, transform 0.12s, box-shadow 0.15s;
}

.generate-btn:hover:not(:disabled) {
  transform: translateY(-1px);
  box-shadow: 0 6px 20px var(--accent-glow);
}

.generate-btn:active:not(:disabled) { transform: translateY(0); }

.generate-btn:disabled {
  opacity: 0.55;
  cursor: not-allowed;
}

.btn-hint {
  font-size: 0.7rem;
  opacity: 0.55;
  letter-spacing: 0.03em;
}

/* ── Game section ── */
.game-section {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
}

.game-toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0.7rem 1.25rem;
  border-bottom: 1px solid var(--border);
  background: var(--surface2);
  min-height: 48px;
}

.game-label {
  font-size: 0.82rem;
  color: var(--muted);
  max-width: 480px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.toolbar-actions { display: flex; gap: 0.5rem; }

.toolbar-btn {
  background: transparent;
  border: 1px solid var(--border);
  border-radius: 6px;
  color: var(--muted);
  font-size: 0.78rem;
  padding: 0.35rem 0.8rem;
  cursor: pointer;
  transition: color 0.15s, border-color 0.15s, background 0.15s;
  white-space: nowrap;
}

.toolbar-btn:hover:not(:disabled) {
  color: var(--text);
  border-color: var(--accent);
}

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

.toolbar-btn.accent {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}

.toolbar-btn.accent:hover:not(:disabled) {
  background: #6d28d9;
  border-color: #6d28d9;
}

.toolbar-btn.small { font-size: 0.73rem; padding: 0.25rem 0.6rem; }

/* ── Game viewport ── */
.game-viewport {
  position: relative;
  background: #050508;
  min-height: 560px;
  display: flex;
  align-items: center;
  justify-content: center;
}

/* ── State screens ── */
.state-screen {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 3rem;
  text-align: center;
}

.state-icon { font-size: 3.5rem; margin-bottom: 1rem; opacity: 0.45; }

.state-title {
  font-size: 1rem;
  font-weight: 500;
  margin-bottom: 0.4rem;
}

.loading-timer {
  font-size: 2rem;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  color: var(--accent);
  letter-spacing: 0.05em;
  margin-bottom: 0.25rem;
}

.state-sub {
  font-size: 0.85rem;
  color: var(--muted);
}

/* ── Loader ── */
.loader-gfx {
  position: relative;
  width: 56px;
  height: 56px;
  margin-bottom: 1.5rem;
}

.loader-ring {
  position: absolute;
  inset: 0;
  border-radius: 50%;
  border: 3px solid var(--border);
  border-top-color: var(--accent);
  animation: spin 0.9s linear infinite;
}

.loader-dot {
  position: absolute;
  inset: 14px;
  border-radius: 50%;
  background: var(--accent);
  opacity: 0.25;
  animation: pulse 0.9s ease-in-out infinite alternate;
}

@keyframes spin { to { transform: rotate(360deg); } }
@keyframes pulse { from { opacity: 0.15; transform: scale(0.85); } to { opacity: 0.5; transform: scale(1.1); } }

.progress-track {
  width: 220px;
  height: 3px;
  background: var(--surface2);
  border-radius: 999px;
  overflow: hidden;
  margin-top: 1.25rem;
}

.progress-bar {
  height: 100%;
  width: 0%;
  background: linear-gradient(90deg, var(--accent), var(--cyan));
  border-radius: 999px;
  transition: width 0.4s ease;
}

.progress-bar.indeterminate {
  width: 40% !important;
  animation: indeterminate 1.4s ease-in-out infinite;
}

@keyframes indeterminate {
  0%   { transform: translateX(-100%); }
  100% { transform: translateX(350%); }
}

/* ── Game iframe ── */
#game-frame {
  width: 100%;
  height: 560px;
  border: none;
  display: block;
}

/* ── Code panel ── */
.code-panel {
  border-top: 1px solid var(--border);
}

.code-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0.6rem 1.25rem;
  border-bottom: 1px solid var(--border);
  background: var(--surface2);
  font-size: 0.8rem;
  color: var(--muted);
}

#code-view {
  overflow: auto;
  max-height: 380px;
  padding: 1.25rem 1.5rem;
  background: #07070e;
}

#code-content {
  font-family: 'JetBrains Mono', 'Fira Code', 'Consolas', monospace;
  font-size: 0.78rem;
  line-height: 1.65;
  color: #9ba9d4;
  white-space: pre;
  display: block;
}

/* ── History button in header ── */
.history-btn {
  margin-top: 0.875rem;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 999px;
  color: var(--muted);
  font-size: 0.82rem;
  padding: 0.35rem 1rem;
  cursor: pointer;
  transition: color 0.15s, border-color 0.15s;
}

.history-btn:hover {
  color: var(--text);
  border-color: var(--accent);
}

.badge {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  background: var(--accent);
  color: #fff;
  font-size: 0.7rem;
  font-weight: 700;
  min-width: 18px;
  height: 18px;
  border-radius: 999px;
  padding: 0 5px;
  margin-left: 0.3rem;
  vertical-align: middle;
}

/* ── History panel ── */
.history-section {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1.1rem 1.5rem;
}

.history-top {
  display: flex;
  align-items: center;
  gap: 0.6rem;
  margin-bottom: 1rem;
}

.history-title {
  font-size: 0.88rem;
  font-weight: 600;
}

.history-count {
  font-size: 0.78rem;
  color: var(--muted);
}

.history-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
  gap: 0.75rem;
}

.game-card {
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 0.875rem 1rem;
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
  transition: border-color 0.15s;
  cursor: default;
}

.game-card:hover {
  border-color: var(--accent);
}

.game-card-prompt {
  font-size: 0.82rem;
  color: var(--text);
  line-height: 1.4;
  display: -webkit-box;
  -webkit-line-clamp: 3;
  -webkit-box-orient: vertical;
  overflow: hidden;
  flex: 1;
}

.game-card-date {
  font-size: 0.72rem;
  color: var(--muted);
}

.game-card-actions {
  display: flex;
  gap: 0.4rem;
  margin-top: 0.25rem;
}

.card-play-btn {
  flex: 1;
  background: var(--accent);
  border: none;
  border-radius: 6px;
  color: #fff;
  font-size: 0.78rem;
  font-weight: 600;
  padding: 0.35rem 0;
  cursor: pointer;
  transition: background 0.15s;
}

.card-play-btn:hover { background: #6d28d9; }

.card-delete-btn {
  background: transparent;
  border: 1px solid var(--border);
  border-radius: 6px;
  color: var(--muted);
  font-size: 0.78rem;
  padding: 0.35rem 0.5rem;
  cursor: pointer;
  transition: color 0.15s, border-color 0.15s;
}

.card-delete-btn:hover {
  color: #ef4444;
  border-color: #ef4444;
}

.history-empty {
  color: var(--muted);
  font-size: 0.85rem;
  padding: 0.5rem 0;
}

/* ── Fix / Improve panel ── */
.fix-section {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1.1rem 1.5rem;
}

.fix-header {
  display: flex;
  align-items: baseline;
  gap: 0.75rem;
  margin-bottom: 0.875rem;
}

.fix-label {
  font-size: 0.88rem;
  font-weight: 600;
  color: var(--text);
}

.fix-hint {
  font-size: 0.8rem;
  color: var(--muted);
}

.fix-row {
  display: flex;
  gap: 0.875rem;
  align-items: flex-start;
}

.fix-row textarea {
  flex: 1;
  min-height: 60px;
}

.fix-btn {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 0.2rem;
  background: linear-gradient(145deg, #0f766e, #0d9488);
  border: none;
  border-radius: 8px;
  color: #fff;
  font-size: 0.93rem;
  font-weight: 600;
  padding: 0 1.2rem;
  min-width: 120px;
  height: 60px;
  cursor: pointer;
  transition: opacity 0.15s, transform 0.12s, box-shadow 0.15s;
  white-space: nowrap;
}

.fix-btn:hover:not(:disabled) {
  transform: translateY(-1px);
  box-shadow: 0 6px 20px rgba(13, 148, 136, 0.35);
}

.fix-btn:disabled {
  opacity: 0.55;
  cursor: not-allowed;
}

/* ── Responsive ── */
@media (max-width: 600px) {
  .input-row { flex-direction: column; }
  .input-right { min-width: unset; }
  .generate-btn { height: 48px; flex-direction: row; gap: 0.5rem; }
  .examples { display: none; }
  #game-frame { height: 400px; }
  .game-viewport { min-height: 400px; }
}
