*,*::before,*::after { box-sizing: border-box; }
:root {
  --bg: #f7f8fa;
  --surface: #ffffff;
  --surface-2: #fafbfc;
  --border: #e4e6eb;
  --border-strong: #d0d4dc;
  --text: #0f172a;
  --text-2: #334155;
  --muted: #64748b;
  --muted-2: #94a3b8;
  --accent: #5b9bff;
  --accent-hover: #4789ee;
  --accent-soft: #eaf2ff;
  --accent-2: #b35bff;
  --danger: #ef4444;
  --good: #10b981;
  --warn: #f59e0b;
  --shadow-sm: 0 1px 2px rgba(15, 23, 42, 0.06);
  --shadow: 0 4px 12px rgba(15, 23, 42, 0.08);
  --shadow-lg: 0 12px 32px rgba(15, 23, 42, 0.12);
  --radius: 10px;
  --radius-lg: 14px;
}
html, body { margin: 0; padding: 0; height: 100%; }
body { font-family: -apple-system, "Segoe UI", "Inter", Roboto, sans-serif; background: var(--bg); color: var(--text); font-size: 14px; -webkit-font-smoothing: antialiased; }
a { color: var(--accent); text-decoration: none; }
a:hover { color: var(--accent-hover); }
h1, h2, h3 { color: var(--text); }

/* ---------- Top nav (non-editor pages) ---------- */
.nav {
  display: flex; align-items: center; gap: 24px;
  padding: 12px 24px; background: var(--surface); border-bottom: 1px solid var(--border);
}
.brand { font-weight: 700; font-size: 18px; background: linear-gradient(135deg, var(--accent), var(--accent-2)); -webkit-background-clip: text; background-clip: text; color: transparent; }
.nav-links { display: flex; align-items: center; gap: 4px; margin-left: auto; }
.nav-links a { padding: 8px 12px; border-radius: 8px; color: var(--text-2); font-weight: 500; transition: background 0.15s; }
.nav-links a:hover { background: var(--surface-2); }
.nav-links .user { color: var(--muted); font-size: 13px; padding: 0 8px; }
.role { font-size: 11px; padding: 2px 6px; border-radius: 4px; font-weight: 600; }
.role-admin { background: #fee2e2; color: #b91c1c; }
.role-design { background: #f3e8ff; color: #7c3aed; }
.role-xuongin { background: #d1fae5; color: #065f46; }

.container { max-width: 1200px; margin: 0 auto; padding: 32px 24px; }
.prose { max-width: 720px; }
.prose .lead { color: var(--muted); font-size: 18px; }
.prose h2 { margin-top: 28px; }

/* ---------- Auth pages ---------- */
.auth-box {
  max-width: 420px; margin: 64px auto; padding: 32px;
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius-lg);
  box-shadow: var(--shadow);
}
.auth-box h1 { margin-top: 0; }
.auth-box label, dialog label { display: block; margin: 14px 0; font-size: 13px; color: var(--muted); font-weight: 500; }
.auth-box input, .auth-box select, dialog input, dialog select, .auth-box input[type=tel] {
  width: 100%; margin-top: 4px; padding: 10px 12px; border-radius: 8px;
  border: 1px solid var(--border); background: var(--surface); color: var(--text); font-size: 14px;
  transition: border-color 0.15s, box-shadow 0.15s;
}
.auth-box input:focus, .auth-box select:focus, dialog input:focus, dialog select:focus {
  outline: none; border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft);
}
.role-pick { border: 1px solid var(--border); border-radius: 8px; padding: 12px; margin: 16px 0; }
.role-pick legend { color: var(--muted); padding: 0 6px; font-size: 12px; }
.role-pick .radio { display: block; padding: 8px 0; color: var(--text-2); font-weight: normal; }
.alert { background: #fee2e2; color: #b91c1c; padding: 10px 14px; border-radius: 8px; margin: 12px 0; font-size: 13px; }

/* ---------- Buttons ---------- */
button, .btn-primary, .btn-secondary, .btn-danger, .btn-ghost {
  font-family: inherit; font-size: 14px; padding: 8px 14px; border-radius: 8px;
  border: 1px solid var(--border); background: var(--surface); color: var(--text); cursor: pointer;
  font-weight: 500; transition: background 0.15s, border-color 0.15s, transform 0.05s;
}
button:hover { background: var(--surface-2); border-color: var(--border-strong); }
button:active { transform: scale(0.98); }
.btn-primary { background: var(--accent); border-color: var(--accent); color: white; }
.btn-primary:hover { background: var(--accent-hover); border-color: var(--accent-hover); color: white; }
.btn-secondary { background: var(--surface); }
.btn-danger { background: var(--danger); border-color: var(--danger); color: white; }
.btn-danger:hover { background: #dc2626; border-color: #dc2626; }
.btn-ghost { background: transparent; border-color: transparent; }
.btn-ghost:hover { background: var(--surface-2); }
.btn-link { background: none; border: none; color: var(--accent); padding: 8px 12px; font-weight: 500; }
.btn-link:hover { background: var(--surface-2); }
.block { width: 100%; padding: 12px; font-size: 15px; margin-top: 12px; }
.muted { color: var(--muted); font-size: 14px; }
.inline { display: inline; margin: 0; padding: 0; }

/* ---------- Page heads / grids ---------- */
.page-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 24px; gap: 16px; flex-wrap: wrap; }
.page-head h1 { margin: 0; font-size: 28px; }
.page-head .muted { margin: 4px 0 0; }
.filter select { padding: 8px 12px; background: var(--surface); border: 1px solid var(--border); color: var(--text); border-radius: 8px; }

.cat-bar { display: flex; gap: 8px; flex-wrap: wrap; margin-bottom: 24px; }
.cat { padding: 8px 16px; border-radius: 999px; background: var(--surface); border: 1px solid var(--border); color: var(--text-2); font-size: 13px; font-weight: 500; }
.cat:hover { background: var(--surface-2); color: var(--text); }
.cat.active { background: var(--text); border-color: var(--text); color: white; }

.grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); gap: 16px; }
.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius-lg); padding: 12px; transition: box-shadow 0.15s, transform 0.12s; }
.card:hover { box-shadow: var(--shadow); }
.card .thumb { aspect-ratio: 4/3; background: var(--surface-2); border-radius: 10px; overflow: hidden; display: flex; align-items: center; justify-content: center; border: 1px solid var(--border); }
.card .thumb img { width: 100%; height: 100%; object-fit: contain; }
.card .placeholder { color: var(--muted-2); font-size: 32px; font-weight: 700; }
.card h3 { margin: 12px 4px 4px; font-size: 15px; }
.card .meta { font-size: 12px; color: var(--muted); margin: 4px 4px; padding: 0 4px; }
.tag { background: var(--accent-soft); color: #1d4ed8; padding: 3px 8px; border-radius: 6px; font-size: 11px; font-weight: 500; }
.card .designer { font-size: 13px; color: var(--muted); margin: 8px 0; padding: 10px; background: var(--surface-2); border-radius: 8px; }
.card .designer strong { color: var(--text); }
.card .by { font-size: 12px; color: var(--muted); margin: 4px 8px 0; padding-bottom: 4px; }
.card-actions { display: flex; gap: 6px; margin-top: 10px; }
.card-actions a, .card-actions button { flex: 1; text-align: center; font-size: 13px; padding: 7px; }
.card-link { display: block; color: inherit; }
.card-link:hover { color: inherit; transform: translateY(-2px); }
.card-link:hover .thumb { border-color: var(--accent); }
.empty-state { text-align: center; padding: 64px 24px; color: var(--muted); background: var(--surface); border: 1px dashed var(--border-strong); border-radius: var(--radius-lg); }

/* ---------- Tables ---------- */
.table { width: 100%; border-collapse: collapse; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius-lg); overflow: hidden; }
.table th, .table td { padding: 12px 14px; border-bottom: 1px solid var(--border); text-align: left; font-size: 14px; }
.table th { background: var(--surface-2); color: var(--muted); font-weight: 600; font-size: 12px; text-transform: uppercase; letter-spacing: 0.04em; }
.table tr:last-child td { border-bottom: none; }
.table select { background: var(--surface); border: 1px solid var(--border); color: var(--text); padding: 4px 8px; border-radius: 6px; }

/* ---------- Detail page ---------- */
.detail { display: grid; grid-template-columns: 1fr 380px; gap: 32px; align-items: start; }
.detail-preview { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius-lg); padding: 32px; display: flex; align-items: center; justify-content: center; min-height: 500px; }
.detail-preview img { max-width: 100%; max-height: 70vh; object-fit: contain; box-shadow: var(--shadow-lg); border-radius: 8px; }
.detail-preview .placeholder.big { font-size: 96px; color: var(--muted-2); }
.detail-info { position: sticky; top: 24px; }
.detail-info .back { font-size: 13px; color: var(--muted); }
.detail-info h1 { margin: 8px 0 12px; }
.info-block { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius-lg); padding: 16px; margin: 16px 0; }
.info-block h3 { margin: 0 0 10px; font-size: 11px; text-transform: uppercase; color: var(--muted); letter-spacing: 0.05em; font-weight: 600; }
.info-block p { margin: 4px 0; font-size: 14px; color: var(--text-2); }
.info-block .big-name { font-size: 18px; font-weight: 600; color: var(--text); margin-bottom: 8px; }
.btn-primary.big { font-size: 16px; padding: 14px; }
.muted.small { font-size: 12px; }
@media (max-width: 800px) { .detail { grid-template-columns: 1fr; } .detail-info { position: static; } }

/* ============================================ */
/* EDITOR — Canva-style                          */
/* ============================================ */
body.editor-body { overflow: hidden; background: #ebecf0; }
body.editor-body .nav { display: none; }

.editor-shell {
  display: grid; grid-template-rows: 56px 1fr;
  height: 100vh;
}

/* --- topbar --- */
.ed-topbar {
  display: flex; align-items: center; gap: 8px;
  padding: 0 16px; background: var(--surface); border-bottom: 1px solid var(--border);
  z-index: 50;
}
.ed-topbar .home { display: flex; align-items: center; gap: 8px; padding: 8px 12px; border-radius: 8px; color: var(--text); font-weight: 600; }
.ed-topbar .home:hover { background: var(--surface-2); }
.ed-topbar .home-brand { background: linear-gradient(135deg, var(--accent), var(--accent-2)); -webkit-background-clip: text; background-clip: text; color: transparent; }
.ed-topbar .divider { width: 1px; height: 28px; background: var(--border); margin: 0 4px; }
.ed-title-input { border: 1px solid transparent; background: transparent; padding: 6px 10px; border-radius: 6px; font-size: 14px; color: var(--text); min-width: 200px; font-weight: 500; }
.ed-title-input:hover { background: var(--surface-2); }
.ed-title-input:focus { outline: none; background: var(--surface); border-color: var(--border-strong); }
.ed-topbar .spacer { flex: 1; }
.ed-icon-btn { width: 34px; height: 34px; padding: 0; display: inline-flex; align-items: center; justify-content: center; background: transparent; border-color: transparent; color: var(--text-2); }
.ed-icon-btn:hover { background: var(--surface-2); }
.ed-icon-btn svg { width: 18px; height: 18px; }
.dropdown { position: relative; }
.dropdown-menu { position: absolute; right: 0; top: calc(100% + 4px); background: var(--surface); border: 1px solid var(--border); border-radius: 10px; box-shadow: var(--shadow-lg); min-width: 180px; padding: 6px; display: none; z-index: 100; }
.dropdown.open .dropdown-menu { display: block; }
.dropdown-menu button { display: block; width: 100%; text-align: left; background: transparent; border: none; padding: 8px 12px; border-radius: 6px; color: var(--text); font-size: 13px; }
.dropdown-menu button:hover { background: var(--surface-2); }

/* --- main workspace --- */
.ed-workspace { display: grid; grid-template-columns: 72px auto 1fr; min-height: 0; }
.ed-workspace.props-open { grid-template-columns: 72px auto 1fr 300px; }

/* icon rail */
.ed-rail { background: var(--surface); border-right: 1px solid var(--border); padding: 12px 8px; display: flex; flex-direction: column; gap: 4px; }
.ed-rail-item {
  background: transparent; border: none; padding: 10px 4px; border-radius: 8px;
  color: var(--muted); font-size: 11px; font-weight: 500;
  display: flex; flex-direction: column; align-items: center; gap: 4px; cursor: pointer;
  transition: background 0.12s, color 0.12s;
}
.ed-rail-item:hover { background: var(--surface-2); color: var(--text); }
.ed-rail-item.active { background: var(--accent-soft); color: var(--accent); }
.ed-rail-item svg { width: 22px; height: 22px; }

/* slide panel */
.ed-panel { width: 320px; background: var(--surface); border-right: 1px solid var(--border); overflow-y: auto; padding: 16px; }
.ed-panel.hidden { display: none; }
.ed-panel h2 { font-size: 16px; margin: 0 0 12px; }
.ed-panel .sub { font-size: 12px; color: var(--muted); margin: 0 0 16px; }
.ed-panel h3 { font-size: 12px; text-transform: uppercase; color: var(--muted); letter-spacing: 0.05em; margin: 16px 0 8px; font-weight: 600; }

.panel-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.panel-grid.three { grid-template-columns: repeat(3, 1fr); }
.panel-grid.four { grid-template-columns: repeat(4, 1fr); }
.tile {
  aspect-ratio: 1; padding: 6px; background: var(--surface-2); border: 1px solid var(--border); border-radius: 8px;
  display: flex; align-items: center; justify-content: center; cursor: pointer; transition: border-color 0.12s, background 0.12s;
  color: var(--text-2);
}
.tile:hover { border-color: var(--accent); background: var(--accent-soft); }
.tile svg { width: 28px; height: 28px; }
.tile.text { aspect-ratio: auto; flex-direction: column; padding: 12px; gap: 4px; }
.tile.text .preview { font-weight: bold; }

.tpl-card {
  cursor: pointer; border: 1px solid var(--border); border-radius: 8px; overflow: hidden;
  background: var(--surface-2); transition: border-color 0.12s, transform 0.12s;
}
.tpl-card:hover { border-color: var(--accent); transform: translateY(-1px); }
.tpl-card .tpl-thumb { aspect-ratio: 1; display: flex; align-items: center; justify-content: center; background: white; }
.tpl-card .tpl-thumb img { width: 100%; height: 100%; object-fit: contain; }
.tpl-card .tpl-mini { width: 100%; height: 100%; }
.tpl-card .tpl-caption { padding: 6px 8px; font-size: 11px; color: var(--text-2); border-top: 1px solid var(--border); background: var(--surface); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.upload-zone {
  border: 2px dashed var(--border-strong); border-radius: 10px; padding: 24px 16px;
  text-align: center; cursor: pointer; color: var(--muted); transition: border-color 0.12s, color 0.12s, background 0.12s;
  display: block;
}
.upload-zone:hover { border-color: var(--accent); color: var(--accent); background: var(--accent-soft); }
.upload-zone .big-icon { font-size: 28px; margin-bottom: 4px; }
.upload-list { margin-top: 12px; display: grid; grid-template-columns: 1fr 1fr; gap: 6px; }
.upload-list img { width: 100%; aspect-ratio: 1; object-fit: cover; border-radius: 6px; cursor: pointer; border: 1px solid var(--border); }
.upload-list img:hover { border-color: var(--accent); }

.colorpalette { display: grid; grid-template-columns: repeat(6, 1fr); gap: 6px; }
.color-swatch { aspect-ratio: 1; border-radius: 6px; cursor: pointer; border: 1px solid var(--border); }
.color-swatch:hover { transform: scale(1.05); }

/* preset sizes */
.preset-list { display: grid; gap: 6px; }
.preset-item {
  display: flex; align-items: center; gap: 10px; padding: 8px 10px; background: var(--surface-2);
  border: 1px solid var(--border); border-radius: 8px; cursor: pointer; text-align: left;
}
.preset-item:hover { border-color: var(--accent); background: var(--accent-soft); }
.preset-item .ico { width: 28px; height: 28px; background: var(--surface); border: 1px solid var(--border-strong); border-radius: 4px; flex-shrink: 0; }
.preset-item .label { flex: 1; }
.preset-item .label strong { display: block; font-size: 13px; color: var(--text); }
.preset-item .label small { font-size: 11px; color: var(--muted); }

/* canvas stage */
.ed-stage {
  position: relative; overflow: auto; background: #ebecf0;
  display: flex; align-items: center; justify-content: center; padding: 32px;
}
.ed-stage canvas { box-shadow: var(--shadow-lg); background: white; }

/* properties panel (right) */
.ed-props { background: var(--surface); border-left: 1px solid var(--border); overflow-y: auto; padding: 16px; }
.ed-props.hidden { display: none; }
.ed-props h2 { font-size: 14px; margin: 0 0 4px; }
.ed-props .pclass { font-size: 11px; color: var(--muted); margin-bottom: 16px; text-transform: uppercase; letter-spacing: 0.05em; }
.ed-props label { display: block; font-size: 12px; color: var(--muted); margin: 10px 0 4px; font-weight: 500; }
.ed-props input[type=text], .ed-props input[type=number], .ed-props select, .ed-props textarea {
  width: 100%; padding: 7px 10px; border: 1px solid var(--border); background: var(--surface);
  color: var(--text); border-radius: 6px; font-size: 13px; font-family: inherit;
}
.ed-props input:focus, .ed-props select:focus, .ed-props textarea:focus {
  outline: none; border-color: var(--accent); box-shadow: 0 0 0 2px var(--accent-soft);
}
.ed-props textarea { resize: vertical; min-height: 60px; }
.ed-props input[type=color] {
  width: 40px; height: 32px; padding: 2px; border: 1px solid var(--border); background: var(--surface);
  border-radius: 6px; cursor: pointer; vertical-align: middle;
}
.ed-props input[type=range] { width: 100%; }
.ed-props .row { display: flex; gap: 6px; align-items: center; }
.ed-props .row > * { flex: 1; min-width: 0; }
.ed-props .row.fixed input[type=color] { flex: 0 0 auto; }
.ed-props .btn-row { display: grid; grid-template-columns: 1fr 1fr; gap: 6px; margin: 12px 0; }
.ed-props .btn-block { display: block; width: 100%; margin: 6px 0; }
.ed-props .sep { height: 1px; background: var(--border); margin: 16px 0; }
.ed-props .empty {
  color: var(--muted); font-size: 13px; text-align: center; padding: 40px 16px;
}
.fontstyle-group { display: grid; grid-template-columns: repeat(4, 1fr); gap: 4px; }
.fontstyle-group button { padding: 8px; font-size: 14px; }
.fontstyle-group button.active { background: var(--accent-soft); color: var(--accent); border-color: var(--accent); }

/* dialog */
dialog { background: var(--surface); color: var(--text); border: 1px solid var(--border); border-radius: var(--radius-lg); padding: 24px; min-width: 380px; box-shadow: var(--shadow-lg); }
dialog::backdrop { background: rgba(15, 23, 42, 0.4); backdrop-filter: blur(2px); }
dialog .actions { display: flex; gap: 8px; margin-top: 16px; justify-content: flex-end; }
dialog h3 { margin: 0 0 16px; }

/* misc helpers */
.tool.link { display: block; text-align: center; text-decoration: none; }
