:root {
  --bg: #f7f8fa;
  --card: #ffffff;
  --border: #e3e6ea;
  --text: #1d2530;
  --muted: #667085;
  --primary: #2f5eff;
  --primary-hover: #2449d6;
  --success: #1a7f4e;
  --success-bg: #e7f7ee;
  --warn: #92620a;
  --warn-bg: #fff4de;
  --error: #b3261e;
  --error-bg: #fdecea;
  --locked: #a3a9b3;
  --radius: 10px;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.5;
}

.app-shell { max-width: 920px; margin: 0 auto; padding: 24px 20px 80px; }

header.top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 4px 0 20px;
}

header.top h1 { font-size: 20px; margin: 0; }
header.top .sub { color: var(--muted); font-size: 13px; }

.card {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 20px;
  margin-bottom: 20px;
}

.tabs { display: flex; gap: 6px; margin-bottom: 20px; flex-wrap: wrap; }
.tab-btn {
  border: 1px solid var(--border);
  background: var(--card);
  color: var(--text);
  padding: 8px 14px;
  border-radius: 999px;
  cursor: pointer;
  font-size: 14px;
}
.marketplace-nav { text-decoration: none; display: inline-flex; align-items: center; justify-content: center; background: #eefdf7; border-color: #a7f3d0; color: #047857; }
.marketplace-nav:hover { background: #d1fae5; border-color: #6ee7b7; }
.tab-btn.active { background: var(--primary); color: #fff; border-color: var(--primary); }
.tab-panel { display: none; }
.tab-panel.active { display: block; }

label { font-weight: 600; font-size: 14px; display: block; margin-bottom: 6px; }
textarea, input[type=text], input[type=email], input[type=password], select {
  width: 100%;
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 10px 12px;
  font-size: 14px;
  font-family: inherit;
  margin-bottom: 16px;
}
.billing-grid { display: flex; flex-wrap: wrap; gap: 10px; margin: 16px 0; }
textarea { min-height: 80px; resize: vertical; }

.radio-group { display: flex; flex-direction: column; gap: 8px; margin-bottom: 18px; }
.radio-option {
  display: flex;
  align-items: flex-start;
  gap: 10px;
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 10px 12px;
  cursor: pointer;
}
.radio-option.locked { opacity: 0.55; cursor: not-allowed; }
.radio-option input { margin-top: 3px; }
.radio-option .opt-title { font-weight: 600; font-size: 14px; }
.radio-option .opt-desc { font-size: 13px; color: var(--muted); }
.badge {
  display: inline-block;
  font-size: 11px;
  font-weight: 700;
  padding: 2px 8px;
  border-radius: 999px;
  margin-left: 8px;
}
.badge.upgrade { background: var(--warn-bg); color: var(--warn); }
.badge.owner { background: var(--success-bg); color: var(--success); }

button.primary {
  background: var(--primary);
  color: #fff;
  border: none;
  padding: 11px 18px;
  border-radius: 8px;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
}
button.primary:hover { background: var(--primary-hover); }
button.primary:disabled { background: var(--locked); cursor: not-allowed; }
button.secondary {
  background: #fff;
  color: var(--text);
  border: 1px solid var(--border);
  padding: 9px 14px;
  border-radius: 8px;
  font-size: 13px;
  cursor: pointer;
  margin-right: 8px;
}

.settings-section .sub { margin: 4px 0 18px; }
.provider-settings-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 16px; margin-top: 16px; }
.provider-settings-card { border: 1px solid var(--border); border-radius: 10px; padding: 18px; background: #fafbfc; }
.provider-settings-card h3 { margin: 0; font-size: 16px; }
.provider-settings-card .sub { min-height: 40px; margin-bottom: 14px; }
.provider-settings-card button.secondary { width: 100%; margin: 0; }
.license-section { border-left: 4px solid var(--primary); }
.button-row { display: flex; flex-wrap: wrap; gap: 8px; }
.button-row button.secondary { margin: 0; }
.generation-progress { margin-top: 16px; padding: 14px; border: 1px solid var(--border); border-radius: 12px; background: rgba(49,87,245,.04); }
.generation-progress-head { display: flex; justify-content: space-between; gap: 12px; margin-bottom: 8px; }
.generation-progress progress { width: 100%; height: 14px; accent-color: var(--primary); }
.danger-button { color: var(--error) !important; }
.settings-save-card { position: sticky; bottom: 12px; box-shadow: 0 8px 28px rgba(29, 37, 48, 0.1); }

@media (max-width: 680px) {
  .provider-settings-grid { grid-template-columns: 1fr; }
  .settings-save-card { position: static; }
}

.state-msg { border-radius: 8px; padding: 10px 12px; font-size: 13px; margin-bottom: 16px; }
.state-msg.loading { background: #eef1ff; color: var(--primary); }
.state-msg.success { background: var(--success-bg); color: var(--success); }
.state-msg.error { background: var(--error-bg); color: var(--error); }
.state-msg.warn { background: var(--warn-bg); color: var(--warn); }
.state-msg.empty { background: #f1f2f4; color: var(--muted); }

pre.output {
  white-space: pre-wrap;
  word-break: break-word;
  background: #0f1520;
  color: #dfe6f0;
  padding: 16px;
  border-radius: 8px;
  font-size: 13px;
  max-height: 480px;
  overflow-y: auto;
}

.checklist-step {
  display: flex;
  align-items: flex-start;
  gap: 10px;
  padding: 10px 0;
  border-bottom: 1px solid var(--border);
}
.checklist-step:last-child { border-bottom: none; }
.checklist-step input { margin-top: 4px; }
.checklist-step.done .step-text { text-decoration: line-through; color: var(--muted); }

.history-item {
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 12px 14px;
  margin-bottom: 10px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 10px;
}
.history-item .meta { font-size: 12px; color: var(--muted); }

footer.note { font-size: 12px; color: var(--muted); margin-top: 30px; }
.token-balance-grid { display:grid; grid-template-columns:repeat(4,minmax(0,1fr)); gap:12px; margin:18px 0; }
.token-balance-grid > div { display:grid; gap:4px; padding:16px; border:1px solid var(--border); border-radius:10px; background:#fafbfc; }
.token-balance-grid span { color:var(--muted); font-size:12px; }
.token-balance-grid strong { font-size:22px; }
.usage-meters { display:grid; gap:16px; margin:20px 0; padding:16px; background:#f8fafc; border-radius:12px; }
.token-meter-row { display:flex; justify-content:space-between; gap:16px; align-items:center; }
.usage-meters progress { width:100%; height:12px; accent-color:var(--primary); }
.token-buy-link { display:inline-flex; align-items:center; justify-content:center; text-decoration:none; }
@media (max-width:680px) { .token-balance-grid { grid-template-columns:repeat(2,minmax(0,1fr)); } .token-buy-link { width:100%; } }
@media (max-width:360px) { .token-balance-grid { grid-template-columns:1fr; } }

/* Mobile readiness */
html { overflow-x: hidden; -webkit-text-size-adjust: 100%; }
body { overflow-wrap: anywhere; }
img, picture, video, canvas { max-width: 100%; height: auto; }
picture { display: block; }
img, video { object-fit: contain; }
.tab-btn, button.primary, button.secondary { min-height: 44px; touch-action: manipulation; }
textarea, input[type=text], input[type=email], input[type=password], select { min-height: 44px; }
pre.output { overflow-x: auto; overflow-wrap: anywhere; }
.history-item > div { min-width: 0; overflow-wrap: anywhere; }

@media (max-width: 680px) {
  .app-shell { width: 100%; padding: 16px 14px 56px; }
  header.top { align-items: flex-start; flex-direction: column; gap: 10px; }
  header.top h1 { font-size: 19px; line-height: 1.25; }
  #account-chip { width: 100%; }
  .card { padding: 16px; margin-bottom: 14px; }
  .tabs { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 8px; }
  .tab-btn { width: 100%; padding: 10px 8px; border-radius: 8px; }
  textarea, input[type=text], input[type=email], input[type=password], select { font-size: 16px; padding: 11px 12px; }
  button.primary, button.secondary { width: 100%; margin: 0 0 8px; padding: 11px 14px; font-size: 15px; }
  .billing-grid, .button-row { display: grid; grid-template-columns: 1fr; gap: 8px; }
  .billing-grid button, .button-row button { margin: 0; }
  .radio-option { min-height: 52px; padding: 12px; }
  .radio-option input { width: 20px; height: 20px; }
  .provider-settings-grid { grid-template-columns: minmax(0, 1fr); }
  .provider-settings-card { min-width: 0; padding: 15px; }
  .provider-settings-card .sub { min-height: 0; }
  .settings-save-card { position: static; box-shadow: none; }
  pre.output { max-width: 100%; max-height: 58vh; padding: 14px; font-size: 12px; line-height: 1.55; }
  .history-item { align-items: stretch; flex-direction: column; }
  .history-item .history-download { width: 100%; }
  .checklist-step { gap: 12px; }
  .checklist-step input { flex: 0 0 auto; width: 20px; height: 20px; }
  footer.note { margin-top: 22px; }
  .tab-panel img, .tab-panel picture, .tab-panel video, .tab-panel canvas, .output-card img { display: block; width: 100%; max-width: 100%; height: auto; aspect-ratio: auto; object-fit: contain; }
}

@media (max-width: 360px) {
  .app-shell { padding-inline: 10px; }
  .card { padding: 14px; }
  .tabs { grid-template-columns: 1fr; }
  .badge { display: table; margin: 5px 0 0; }
}
