/* =====================================================================
   CSS Element Studio — Style Sheet (ปรับปรุง UI ใหม่ทั้งหมด)
   Zero-dependency, Vanilla CSS3 with CSS Custom Properties
   ===================================================================== */

/* ---------- รีเซ็ต ---------- */
*,
*::before,
*::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  /* Theme: Light */
  --bg: #f6f7fb;
  --surface: #ffffff;
  --surface-2: #f9fafc;
  --border: #e6e9f0;
  --border-strong: #d6dbe5;
  --text: #0f172a;
  --text-muted: #64748b;
  --text-faint: #94a3b8;
  --accent: #4f46e5;
  --accent-hover: #4338ca;
  --accent-soft: #eef2ff;
  --accent-ring: rgba(79, 70, 229, 0.18);
  --code-bg: #111827;
  --code-text: #e5e7eb;
  --success: #16a34a;
  --shadow-sm: 0 1px 2px 0 rgba(15, 23, 42, 0.05);
  --shadow-md: 0 4px 16px -4px rgba(15, 23, 42, 0.08), 0 2px 6px -2px rgba(15, 23, 42, 0.05);
  --radius: 14px;
  --radius-sm: 9px;
  --font-sans: "Inter", system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --font-mono: ui-monospace, SFMono-Regular, "SF Mono", Menlo, Monaco, Consolas, monospace;
}

[data-theme="dark"] {
  --bg: #0b1120;
  --surface: #151d31;
  --surface-2: #1b2539;
  --border: #283349;
  --border-strong: #36425b;
  --text: #f1f5f9;
  --text-muted: #9aa8bd;
  --text-faint: #6b7a92;
  --accent: #818cf8;
  --accent-hover: #a5b4fc;
  --accent-soft: #232b4d;
  --accent-ring: rgba(129, 140, 248, 0.22);
  --code-bg: #080d19;
  --code-text: #dbe4f0;
  --shadow-sm: 0 1px 2px 0 rgba(0, 0, 0, 0.3);
  --shadow-md: 0 8px 24px -8px rgba(0, 0, 0, 0.55);
}

html { font-family: var(--font-sans); font-size: 15px; line-height: 1.5; color: var(--text); background: var(--bg); -webkit-font-smoothing: antialiased; }
body { min-height: 100vh; }

/* ---------- Top Bar ---------- */
.topbar {
  position: sticky;
  top: 0;
  z-index: 20;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 14px 28px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  box-shadow: var(--shadow-sm);
}
.brand { display: flex; align-items: center; gap: 12px; min-width: 0; }
.brand-mark {
  display: grid;
  place-items: center;
  width: 38px; height: 38px;
  flex-shrink: 0;
  border-radius: 11px;
  background: linear-gradient(135deg, #6366f1, #a855f7);
  color: #fff;
  font-size: 0.85rem;
  font-weight: 800;
  letter-spacing: -0.03em;
  box-shadow: 0 4px 10px -3px rgba(99, 102, 241, 0.6);
}
.brand-copy h1 { font-size: 1.125rem; font-weight: 750; letter-spacing: -0.02em; }
.brand-copy p { font-size: 0.8125rem; color: var(--text-muted); margin-top: 2px; }
.topbar-actions { display: flex; align-items: center; gap: 8px; flex-shrink: 0; }

.btn-ghost, .icon-btn {
  font-family: inherit;
  font-size: 0.85rem;
  font-weight: 600;
  color: var(--text-muted);
  background: transparent;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  cursor: pointer;
  transition: all 0.18s ease;
}
.btn-ghost { padding: 8px 14px; }
.icon-btn { width: 36px; height: 36px; display: grid; place-items: center; font-size: 1rem; }
.btn-ghost:hover, .icon-btn:hover { color: var(--accent); border-color: var(--accent); background: var(--accent-soft); }

/* ---------- Workspace ---------- */
.workspace {
  display: grid;
  grid-template-columns: 356px minmax(0, 1fr);
  gap: 20px;
  max-width: 1500px;
  margin: 0 auto;
  padding: 22px 28px 40px;
  align-items: start;
}

.sidebar {
  display: flex;
  flex-direction: column;
  gap: 14px;
  position: sticky;
  top: 82px;
  max-height: calc(100vh - 104px);
  overflow-y: auto;
  padding-right: 4px;
  scrollbar-width: thin;
  scrollbar-color: var(--border-strong) transparent;
}
.sidebar::-webkit-scrollbar { width: 6px; }
.sidebar::-webkit-scrollbar-thumb { background: var(--border-strong); border-radius: 3px; }
/* กันไม่ให้การ์ดถูก flex บีบให้สั้นกว่าข้อมูลข้างใน */
.sidebar .card { flex-shrink: 0; }
.content { display: flex; flex-direction: column; gap: 20px; min-width: 0; }

/* ---------- Card ---------- */
.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow-sm); overflow: hidden; }
.card-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  width: 100%;
  gap: 12px;
  padding: 13px 16px;
  font-family: inherit;
  background: transparent;
  border: 0;
  color: var(--text);
}
.card-toggle { cursor: pointer; }
.card-toggle:hover { background: var(--surface-2); }
.card-title { display: inline-flex; align-items: center; gap: 9px; font-size: 0.9rem; font-weight: 700; }
.card-icon {
  display: grid; place-items: center;
  width: 22px; height: 22px;
  border-radius: 7px;
  background: var(--accent-soft);
  color: var(--accent);
  font-size: 0.8rem;
}
.chevron { color: var(--text-faint); font-size: 1rem; transition: transform 0.2s ease; }
.card.collapsed .chevron { transform: rotate(-90deg); }
.card.collapsed .card-body { display: none; }
.card-body { padding: 4px 16px 16px; display: flex; flex-direction: column; gap: 13px; }
.card-header.flat { border-bottom: 1px solid var(--border); }

/* ---------- Fields & Inputs ---------- */
.field { display: flex; flex-direction: column; gap: 6px; min-width: 0; }
.field-row { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.field label, .field > label { font-size: 0.78rem; font-weight: 650; color: var(--text-muted); }
.unit { font-weight: 400; color: var(--text-faint); }

input[type="text"], input[type="number"], input[type="search"], select, textarea {
  width: 100%;
  font-family: inherit;
  font-size: 0.875rem;
  color: var(--text);
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 9px 11px;
  outline: none;
  transition: border-color 0.15s ease, box-shadow 0.15s ease, background 0.15s ease;
}
input[type="text"]:focus, input[type="number"]:focus, input[type="search"]:focus, select:focus, textarea:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-ring);
  background: var(--surface);
}
input::placeholder, textarea::placeholder { color: var(--text-faint); }

select {
  appearance: none;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 24 24' fill='none' stroke='%2394a3b8' stroke-width='2.5' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='m6 9 6 6 6-6'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 10px center;
  padding-right: 28px;
  cursor: pointer;
}

textarea { font-family: var(--font-mono); font-size: 0.8125rem; line-height: 1.6; resize: vertical; min-height: 110px; }
.hint { font-size: 0.75rem; color: var(--text-faint); line-height: 1.5; }

.range-row { display: flex; align-items: center; gap: 8px; min-width: 0; }
input[type="range"] { flex: 1 1 auto; min-width: 0; accent-color: var(--accent); height: 5px; cursor: pointer; }
.range-value {
  flex: 0 0 auto;
  min-width: 40px;
  text-align: center;
  font-family: var(--font-mono);
  font-size: 0.72rem;
  font-weight: 700;
  white-space: nowrap;
  color: var(--accent);
  background: var(--accent-soft);
  border-radius: 6px;
  padding: 3px 4px;
}

/* ---------- Buttons ---------- */
.btn-primary {
  font-family: inherit;
  font-size: 0.875rem;
  font-weight: 650;
  color: #fff;
  background: var(--accent);
  border: 1px solid transparent;
  border-radius: var(--radius-sm);
  padding: 10px 14px;
  cursor: pointer;
  transition: background 0.18s ease, transform 0.18s ease, box-shadow 0.18s ease;
  box-shadow: 0 4px 10px -3px var(--accent-ring);
}
.btn-primary:hover { background: var(--accent-hover); transform: translateY(-1px); }
.btn-primary:active { transform: translateY(0); }
.btn-block { width: 100%; }
.btn-small { padding: 7px 14px; font-size: 0.8rem; }

/* ---------- Presets ---------- */
.search-input { font-size: 0.82rem; padding: 8px 11px; }
.filter-chips { display: flex; flex-wrap: wrap; gap: 5px; }
.chip {
  font-family: inherit;
  font-size: 0.72rem;
  font-weight: 600;
  color: var(--text-muted);
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 4px 10px;
  cursor: pointer;
  transition: all 0.15s ease;
}
.chip:hover { border-color: var(--accent); color: var(--accent); }
.chip.active { color: #fff; background: var(--accent); border-color: var(--accent); }
.preset-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(92px, 1fr)); gap: 7px; }
.preset-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
  min-width: 0;
  padding: 9px 5px 7px;
  font-family: inherit;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 10px;
  cursor: pointer;
  transition: all 0.16s ease;
}
.preset-item:hover { border-color: var(--accent); transform: translateY(-1px); }
.preset-item.active { border-color: var(--accent); background: var(--accent-soft); box-shadow: 0 0 0 2px var(--accent-ring) inset; }
.preset-swatch { width: 100%; height: 22px; border-radius: 6px; border: 1px solid rgba(0,0,0,0.06); }
.preset-name { max-width: 100%; font-size: 0.7rem; font-weight: 600; color: var(--text-muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.preset-item.active .preset-name { color: var(--accent); }
/* Game UI preset ให้มีเอกลักษณ์เฉพาะ */
.preset-game { border-color: rgba(167, 139, 250, 0.55); background: linear-gradient(160deg, rgba(139,92,246,0.14), rgba(236,72,153,0.06)); }
.preset-game .preset-swatch { position: relative; }
.preset-game .preset-swatch::after {
  content: "★";
  position: absolute;
  top: -5px;
  right: -4px;
  font-size: 0.62rem;
  color: #fbbf24;
  text-shadow: 0 0 4px rgba(251,191,36,0.8);
}
.preset-meta { font-size: 0.72rem; color: var(--text-faint); text-align: center; }

/* ---------- Colors ---------- */
.color-field { display: flex; align-items: center; gap: 7px; }
input[type="color"] {
  width: 38px; height: 36px;
  flex-shrink: 0;
  padding: 3px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  cursor: pointer;
}
input[type="color"]::-webkit-color-swatch-wrapper { padding: 0; }
input[type="color"]::-webkit-color-swatch { border: none; border-radius: 6px; }
input[type="color"]::-moz-color-swatch { border: none; border-radius: 6px; }
.hex-input { font-family: var(--font-mono); font-size: 0.78rem; text-transform: uppercase; }
.swatches { display: flex; flex-wrap: wrap; gap: 6px; }
.swatch {
  width: 18px; height: 18px;
  border-radius: 50%;
  border: 2px solid var(--surface);
  box-shadow: 0 0 0 1px var(--border-strong);
  cursor: pointer;
  transition: transform 0.15s ease, box-shadow 0.15s ease;
}
.swatch:hover { transform: scale(1.2); box-shadow: 0 0 0 2px var(--accent); }
.gradient-presets { display: flex; flex-wrap: wrap; gap: 6px; }
.gradient-chip { width: 34px; height: 18px; border-radius: 5px; border: 1px solid rgba(0,0,0,0.08); cursor: pointer; transition: transform 0.15s ease; }
.gradient-chip:hover { transform: translateY(-2px); box-shadow: 0 3px 8px rgba(0,0,0,0.15); }
[data-theme="dark"] .swatch { border-color: var(--surface); }

/* ---------- Preview ---------- */
.preview-stage {
  min-height: 300px;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 44px 28px;
  background:
    radial-gradient(circle at 1px 1px, var(--border) 1px, transparent 0) 0 0 / 20px 20px,
    var(--surface);
  transition: background 0.25s ease;
}
.preview-mount { display: flex; align-items: center; justify-content: center; max-width: 100%; }
.context-select { width: auto; min-width: 150px; font-size: 0.78rem; padding: 6px 26px 6px 10px; }

#preview-stage[data-context="card"] { background: linear-gradient(180deg, #f8fafc 0%, #eef2f7 100%); border-style: dashed; }
#preview-stage[data-context="navbar"] { background: #0f172a; }
#preview-stage[data-context="navbar"] .card-title { color: #e2e8f0; }
#preview-stage[data-context="form"] { background: #e2e8f0; }
[data-theme="dark"] #preview-stage[data-context="card"] { background: linear-gradient(180deg, #182135 0%, #111a2b 100%); }
[data-theme="dark"] #preview-stage[data-context="form"] { background: #0d1524; }

/* Element ที่ถูกออกแบบ (ใช้ CSS Variables ที่ script.js ตั้งค่า) */
.cbg-el {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  box-sizing: border-box;
  max-width: 100%;
  text-align: center;
  font-family: var(--font-sans);
  text-decoration: none;
  color: var(--el-text-color, #ffffff);
  background-color: var(--el-bg, #3b82f6);
  background-image: var(--el-bg-gradient, none);
  border: var(--el-border-width, 2px) solid var(--el-border-color, #2563eb);
  border-radius: var(--el-border-radius, 8px);
  padding: var(--el-padding, 12px 24px);
  font-size: var(--el-font-size, 16px);
  font-weight: var(--el-font-weight, 600);
  letter-spacing: var(--el-letter-spacing, 0);
  box-shadow: var(--el-shadow, 0 4px 6px -1px rgba(0,0,0,0.1));
  transition: transform 0.2s ease, box-shadow 0.2s ease, background-color 0.2s ease;
}
.cbg-el:hover { transform: var(--el-hover-transform, scale(1.05)); }
.cbg-el:active {
  transform: var(--el-active-transform, var(--el-hover-transform, scale(1)));
  box-shadow: var(--el-active-shadow, var(--el-shadow, none));
}
.cbg-el[data-el="input"] { display: inline-block; cursor: text; }
.cbg-el[data-el="card"], .cbg-el[data-el="alert"], .cbg-el[data-el="avatar"] { display: flex; min-width: 220px; }
.cbg-el[data-el="avatar"] { width: 96px; height: 96px; min-width: 96px; border-radius: 9999px; padding: 0; }

/* ---------- Code ---------- */
.code-header { display: flex; align-items: center; justify-content: space-between; gap: 12px; flex-wrap: wrap; }
.code-actions { display: flex; align-items: center; gap: 12px; }
.code-tabs { display: inline-flex; padding: 3px; gap: 2px; background: var(--surface-2); border: 1px solid var(--border); border-radius: 9px; }
.code-tab {
  font-family: inherit;
  font-size: 0.75rem;
  font-weight: 650;
  color: var(--text-muted);
  background: transparent;
  border: 0;
  border-radius: 6px;
  padding: 6px 11px;
  cursor: pointer;
  transition: all 0.15s ease;
}
.code-tab.active { color: var(--accent); background: var(--surface); box-shadow: var(--shadow-sm); }
.code-block {
  margin: 0;
  padding: 18px 20px;
  max-height: 460px;
  overflow: auto;
  background: var(--code-bg);
  color: var(--code-text);
  font-family: var(--font-mono);
  font-size: 0.8125rem;
  line-height: 1.65;
  tab-size: 2;
}
.code-block code { white-space: pre; }

/* ---------- Responsive ---------- */
@media (max-width: 1000px) {
  .workspace { grid-template-columns: 1fr; padding: 16px; }
  .sidebar { position: static; max-height: none; overflow: visible; padding-right: 0; }
  .preview-stage { min-height: 240px; padding: 32px 18px; }
}
@media (max-width: 600px) {
  .topbar { padding: 12px 16px; }
  .brand-copy p { display: none; }
  .brand-copy h1 { font-size: 1rem; }
  .btn-ghost { padding: 7px 10px; font-size: 0.78rem; }
  .field-row { grid-template-columns: 1fr; }
  .preset-grid { grid-template-columns: repeat(auto-fill, minmax(80px, 1fr)); }
  .code-actions { width: 100%; justify-content: space-between; }
}
