:root {
  --bg: #0b1020;
  --bg-soft: #121933;
  --surface: #161e3a;
  --surface-2: #1c2648;
  --border: rgba(148, 163, 184, 0.16);
  --text: #e8eefc;
  --muted: #94a3b8;
  --primary: #818cf8;
  --primary-strong: #6366f1;
  --accent: #22d3ee;
  --success: #34d399;
  --danger: #f87171;
  --warning: #fbbf24;
  --radius: 14px;
  --shadow: 0 10px 40px rgba(0, 0, 0, 0.35);
  --font: "Segoe UI", system-ui, -apple-system, sans-serif;
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; height: 100%; }
body {
  font-family: var(--font);
  background: radial-gradient(1200px 600px at 10% -10%, #1e1b4b 0%, transparent 50%),
              radial-gradient(900px 500px at 100% 0%, #164e63 0%, transparent 45%),
              var(--bg);
  color: var(--text);
  min-height: 100vh;
}

a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }

.app-shell { display: flex; min-height: 100vh; }
.sidebar {
  width: 240px;
  background: rgba(15, 23, 42, 0.85);
  border-right: 1px solid var(--border);
  padding: 24px 16px;
  display: flex;
  flex-direction: column;
  backdrop-filter: blur(12px);
  position: sticky;
  top: 0;
  height: 100vh;
}
.brand {
  display: flex; align-items: center; gap: 10px;
  padding: 4px 10px 28px;
  font-weight: 700; font-size: 18px; letter-spacing: -0.02em;
}
.brand-mark {
  width: 34px; height: 34px; border-radius: 10px;
  background: linear-gradient(135deg, var(--primary), var(--accent));
  display: grid; place-items: center; color: #0b1020; font-weight: 800;
}
.nav { display: flex; flex-direction: column; gap: 4px; flex: 1; }
.nav a {
  color: var(--muted); padding: 11px 14px; border-radius: 10px;
  text-decoration: none; font-weight: 500; font-size: 14px;
  display: flex; align-items: center; gap: 10px; transition: .15s ease;
}
.nav a:hover, .nav a.active {
  background: rgba(129, 140, 248, 0.12); color: var(--text); text-decoration: none;
}
.nav a.active { box-shadow: inset 0 0 0 1px rgba(129, 140, 248, 0.35); }
.sidebar-foot {
  padding: 12px; border-radius: 12px; background: var(--surface);
  border: 1px solid var(--border); font-size: 12px; color: var(--muted);
}
.badge {
  display: inline-flex; align-items: center; gap: 6px;
  padding: 3px 8px; border-radius: 999px; font-size: 11px; font-weight: 600;
}
.badge.ok { background: rgba(52, 211, 153, .15); color: var(--success); }
.badge.warn { background: rgba(251, 191, 36, .15); color: var(--warning); }

.main { flex: 1; padding: 28px 32px 48px; min-width: 0; }
.page-header {
  display: flex; align-items: flex-start; justify-content: space-between;
  gap: 16px; margin-bottom: 24px; flex-wrap: wrap;
}
.page-header h1 { margin: 0; font-size: 28px; letter-spacing: -0.03em; }
.page-header p { margin: 6px 0 0; color: var(--muted); font-size: 14px; }
.actions { display: flex; gap: 10px; flex-wrap: wrap; align-items: center; }

.btn {
  appearance: none; border: 1px solid transparent; cursor: pointer;
  background: var(--surface-2); color: var(--text);
  padding: 10px 16px; border-radius: 10px; font-weight: 600; font-size: 13px;
  transition: .15s ease; display: inline-flex; align-items: center; gap: 8px;
}
.btn:hover { filter: brightness(1.1); }
.btn:disabled { opacity: .5; cursor: not-allowed; }
.btn-primary {
  background: linear-gradient(135deg, var(--primary-strong), #7c3aed);
  box-shadow: 0 8px 24px rgba(99, 102, 241, .35);
}
.btn-accent { background: linear-gradient(135deg, #0891b2, #22d3ee); color: #082f49; }
.btn-danger { background: rgba(248, 113, 113, .15); color: var(--danger); border-color: rgba(248, 113, 113, .3); }
.btn-ghost { background: transparent; border-color: var(--border); }
.btn-sm { padding: 7px 12px; font-size: 12px; }

.card {
  background: rgba(22, 30, 58, 0.8); border: 1px solid var(--border);
  border-radius: var(--radius); box-shadow: var(--shadow); overflow: hidden;
}
.card-body { padding: 20px; }
.grid { display: grid; gap: 16px; }
.grid-2 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.grid-3 { grid-template-columns: repeat(3, minmax(0, 1fr)); }
.grid-4 { grid-template-columns: repeat(4, minmax(0, 1fr)); }
@media (max-width: 1100px) {
  .grid-4, .grid-3 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
}
@media (max-width: 720px) {
  .app-shell { flex-direction: column; }
  .sidebar { width: 100%; height: auto; position: static; }
  .grid-4, .grid-3, .grid-2 { grid-template-columns: 1fr; }
  .main { padding: 20px 16px 40px; }
}

.stat {
  padding: 18px 20px; border-radius: var(--radius);
  background: linear-gradient(180deg, rgba(129,140,248,.08), transparent), var(--surface);
  border: 1px solid var(--border);
}
.stat .label { color: var(--muted); font-size: 12px; font-weight: 600; text-transform: uppercase; letter-spacing: .04em; }
.stat .value { font-size: 28px; font-weight: 700; margin-top: 6px; letter-spacing: -0.03em; }

table { width: 100%; border-collapse: collapse; }
th, td { text-align: left; padding: 12px 14px; border-bottom: 1px solid var(--border); font-size: 13px; }
th { color: var(--muted); font-weight: 600; font-size: 11px; text-transform: uppercase; letter-spacing: .04em; }
tr:hover td { background: rgba(255,255,255,.02); }

.form-row { margin-bottom: 14px; }
.form-row label { display: block; font-size: 12px; color: var(--muted); margin-bottom: 6px; font-weight: 600; }
input, select, textarea {
  width: 100%; background: var(--bg-soft); border: 1px solid var(--border);
  color: var(--text); border-radius: 10px; padding: 10px 12px; font: inherit; font-size: 14px;
}
input:focus, select:focus, textarea:focus {
  outline: none; border-color: rgba(129, 140, 248, .6);
  box-shadow: 0 0 0 3px rgba(129, 140, 248, .15);
}
textarea { min-height: 100px; resize: vertical; }

.modal-backdrop {
  position: fixed; inset: 0; background: rgba(2, 6, 23, .7);
  display: flex; align-items: center; justify-content: center; z-index: 1000;
  padding: 20px; backdrop-filter: blur(4px);
}
.modal {
  background: var(--surface); border: 1px solid var(--border); border-radius: 16px;
  width: min(520px, 100%); max-height: 90vh; overflow: auto; box-shadow: var(--shadow);
}
.modal-header { padding: 18px 20px; border-bottom: 1px solid var(--border); display: flex; justify-content: space-between; align-items: center; }
.modal-header h2 { margin: 0; font-size: 18px; }
.modal-body { padding: 20px; }
.modal-footer { padding: 14px 20px; border-top: 1px solid var(--border); display: flex; justify-content: flex-end; gap: 10px; }

.toast-wrap { position: fixed; right: 20px; bottom: 20px; z-index: 1100; display: flex; flex-direction: column; gap: 8px; }
.toast {
  background: var(--surface-2); border: 1px solid var(--border); color: var(--text);
  padding: 12px 16px; border-radius: 12px; box-shadow: var(--shadow); font-size: 13px;
  animation: slideIn .2s ease;
}
.toast.error { border-color: rgba(248,113,113,.4); }
.toast.success { border-color: rgba(52,211,153,.4); }
@keyframes slideIn { from { transform: translateY(8px); opacity: 0; } to { transform: none; opacity: 1; } }

.template-card, .design-card {
  cursor: pointer; transition: .15s ease; height: 100%;
  display: flex; flex-direction: column;
}
.template-card:hover, .design-card:hover { transform: translateY(-2px); border-color: rgba(129,140,248,.4); }
.template-preview {
  height: 160px; background: #fff; color: #111; overflow: hidden;
  border-bottom: 1px solid var(--border); position: relative;
}
.template-preview iframe {
  width: 200%; height: 200%; transform: scale(.5); transform-origin: 0 0;
  border: 0; pointer-events: none; background: #fff;
}
.template-meta { padding: 14px 16px; }
.template-meta h3 { margin: 0 0 4px; font-size: 14px; }
.template-meta p { margin: 0; color: var(--muted); font-size: 12px; }

.empty {
  text-align: center; padding: 48px 20px; color: var(--muted);
}
.empty h3 { color: var(--text); margin: 0 0 8px; }

/* Designer layout */
.designer-page { display: flex; flex-direction: column; height: calc(100vh - 0px); margin: -28px -32px -48px; }
.designer-toolbar {
  display: flex; align-items: center; gap: 10px; padding: 12px 16px;
  background: rgba(15, 23, 42, .95); border-bottom: 1px solid var(--border);
  flex-wrap: wrap;
}
.designer-toolbar input[type="text"] {
  width: 220px; background: var(--bg-soft);
}
#gjs {
  flex: 1; min-height: 0; border: none;
}
.gjs-one-bg { background: #0f172a !important; }
.gjs-two-color { color: #e2e8f0 !important; }
.gjs-three-bg { background: #1e293b !important; }
.gjs-four-color, .gjs-four-color-h:hover { color: #818cf8 !important; }
.gjs-pn-panel { background: #111827 !important; }
.gjs-blocks-c { background: #0f172a; }
.gjs-block {
  background: #1e293b !important; color: #e2e8f0 !important;
  border: 1px solid rgba(148,163,184,.2) !important; border-radius: 8px !important;
  box-shadow: none !important; min-height: 70px;
}
.gjs-block:hover { box-shadow: 0 0 0 1px #818cf8 !important; }
.gjs-cv-canvas { background: #334155 !important; }
.gjs-frame-wrapper { background: #fff; }

.status-pill {
  display: inline-flex; padding: 3px 10px; border-radius: 999px;
  font-size: 11px; font-weight: 700; text-transform: uppercase; letter-spacing: .04em;
}
.status-draft { background: rgba(148,163,184,.15); color: #cbd5e1; }
.status-sending { background: rgba(251,191,36,.15); color: var(--warning); }
.status-sent { background: rgba(52,211,153,.15); color: var(--success); }
.status-failed { background: rgba(248,113,113,.15); color: var(--danger); }

.preview-frame {
  width: 100%; min-height: 420px; border: 1px solid var(--border);
  border-radius: 12px; background: #fff;
}

.tabs { display: flex; gap: 6px; margin-bottom: 16px; flex-wrap: wrap; }
.tab {
  padding: 8px 14px; border-radius: 999px; border: 1px solid var(--border);
  background: transparent; color: var(--muted); cursor: pointer; font-weight: 600; font-size: 13px;
}
.tab.active { background: rgba(129,140,248,.15); color: var(--text); border-color: rgba(129,140,248,.4); }

.help-box {
  background: rgba(34, 211, 238, .08); border: 1px solid rgba(34, 211, 238, .25);
  border-radius: 12px; padding: 14px 16px; font-size: 13px; color: var(--muted); margin-bottom: 16px;
}
.help-box code {
  background: rgba(0,0,0,.35); padding: 1px 6px; border-radius: 4px; color: var(--accent); font-size: 12px;
}

/* Autosave */
.save-status { font-size: 12px; color: var(--muted); margin-right: 8px; white-space: nowrap; }
.save-status.ok { color: var(--success); }
.save-status.pending { color: var(--muted); }
.save-status.error { color: var(--danger); white-space: normal; max-width: 360px; }
.save-status a { color: inherit; text-decoration: underline; }
.restore-banner { background: rgba(251,191,36,.12); color: var(--warning); padding: 8px 14px; font-size: 13px; display: flex; gap: 10px; align-items: center; }
.restore-banner[hidden] { display: none; }

/* Delete */
.template-card, .design-card { position: relative; }
.card-delete { position: absolute; top: 8px; right: 8px; background: transparent; border: 1px solid transparent; border-radius: 6px; cursor: pointer; font-size: 14px; padding: 2px 6px; opacity: .55; }
.card-delete:hover { opacity: 1; border-color: var(--danger); background: rgba(248,113,113,.12); }
.btn[hidden] { display: none; }
.gjs-toolbar-item.ns-tb-link { width: auto; background: #e11d48; border-radius: 3px; margin-right: 4px; padding: 2px 2px; }
