:root {
  --bg: #0e0c18;
  --panel: #161328;
  --panel-2: #1d1934;
  --input: #110f20;
  --border: #2c2750;
  --border-strong: #3d3670;
  --text: #ebe8f7;
  --muted: #a29dc2;
  --accent: #9b8cff;
  --accent-strong: #7f6cff;
  --accent-text: #120f24;
  --danger: #f2545b;
  --ok: #4fd1a1;
  --radius: 10px;
  --font: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --mono: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;

  /* Discord preview (always Discord's dark theme) */
  --dc-bg: #313338;
  --dc-embed: #2b2d31;
  --dc-text: #dbdee1;
  --dc-head: #f2f3f5;
  --dc-muted: #949ba4;
  --dc-link: #00a8fc;
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background:
    radial-gradient(1200px 600px at 85% -10%, #2a2160 0%, transparent 60%),
    radial-gradient(900px 500px at -10% 110%, #1b2a55 0%, transparent 55%),
    var(--bg);
  background-attachment: fixed;
  color: var(--text);
  font: 15px/1.45 var(--font);
  min-height: 100vh;
}
[hidden] { display: none !important; }
a { color: var(--accent); }
h1, h2, h3 { margin: 0; line-height: 1.2; }
.muted { color: var(--muted); }
.small { font-size: 13px; }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); }
.error-text { color: var(--danger); }

/* ── Moon ─────────────────────────────────────────────────────────────── */
.moon {
  width: 64px; height: 64px; border-radius: 50%;
  background: radial-gradient(circle at 35% 35%, #fff 0%, #d9d4ff 45%, #9b8cff 100%);
  box-shadow: 0 0 40px 6px rgba(155, 140, 255, .45);
  -webkit-mask: radial-gradient(circle at 72% 30%, transparent 38%, #000 39%);
          mask: radial-gradient(circle at 72% 30%, transparent 38%, #000 39%);
  margin: 0 auto 16px;
}
.moon.small { width: 34px; height: 34px; margin: 0; box-shadow: 0 0 18px 2px rgba(155, 140, 255, .4); }
.moon.dim { opacity: .55; }
.moon.pulse { animation: pulse 1.6s ease-in-out infinite; }
@keyframes pulse { 50% { opacity: .45; transform: scale(.94); } }
@media (prefers-reduced-motion: reduce) { .moon.pulse { animation: none; } }

/* ── Buttons & inputs ─────────────────────────────────────────────────── */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  border: 1px solid var(--border-strong); background: var(--panel-2); color: var(--text);
  border-radius: 8px; padding: 8px 14px; font: inherit; font-weight: 600; cursor: pointer;
  text-decoration: none; white-space: nowrap;
  transition: background .15s, border-color .15s, opacity .15s;
}
.btn:hover { border-color: var(--accent); }
.btn:disabled { opacity: .5; cursor: not-allowed; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
.btn.primary:hover:not(:disabled) { background: var(--accent-strong); border-color: var(--accent-strong); }
.btn.ghost { background: transparent; }
.btn.danger-ghost { background: transparent; color: var(--danger); border-color: color-mix(in srgb, var(--danger) 45%, transparent); }
.btn.danger-ghost:hover { border-color: var(--danger); }
.btn.small { padding: 4px 10px; font-size: 13px; }
.btn.big { padding: 12px 22px; font-size: 16px; }
.btn.block { width: 100%; }
.btn.icon { padding: 4px 8px; min-width: 30px; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

input:not([type=checkbox]):not([type=color]), textarea, select {
  width: 100%; background: var(--input); color: var(--text);
  border: 1px solid var(--border); border-radius: 8px; padding: 8px 10px; font: inherit;
}
textarea { resize: vertical; min-height: 40px; }
input:focus, textarea:focus, select:focus { outline: none; border-color: var(--accent); }
input[type=color] {
  width: 40px; height: 36px; padding: 2px; border: 1px solid var(--border); border-radius: 8px; background: var(--input);
}
input[type=checkbox] { accent-color: var(--accent); width: 16px; height: 16px; }

/* ── Screens ──────────────────────────────────────────────────────────── */
.screen.center { min-height: 100vh; display: grid; place-content: center; text-align: center; padding: 16px; }
.card { background: var(--panel); border: 1px solid var(--border); border-radius: var(--radius); }
.gate { padding: 36px 32px; max-width: 420px; display: grid; gap: 14px; justify-items: center; }
.gate h1 { font-size: 24px; }
.gate p { margin: 0; }

/* ── App shell ────────────────────────────────────────────────────────── */
.topbar {
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: 10px 20px; border-bottom: 1px solid var(--border);
  background: color-mix(in srgb, var(--panel) 85%, transparent);
  backdrop-filter: blur(8px); position: sticky; top: 0; z-index: 10;
}
.brand { display: flex; align-items: center; gap: 12px; }
.brand-name { font-weight: 700; letter-spacing: .2px; }
.brand-sub { color: var(--muted); font-size: 13px; }
.who { display: flex; align-items: center; gap: 10px; }
.avatar { width: 28px; height: 28px; border-radius: 50%; }

.layout { display: grid; grid-template-columns: 260px minmax(0, 1fr); min-height: calc(100vh - 57px); }
.sidebar { border-right: 1px solid var(--border); padding: 16px; display: grid; align-content: start; gap: 12px; }
.side-title { font-size: 12px; text-transform: uppercase; letter-spacing: .08em; color: var(--muted); margin-top: 8px; }
.embed-list { list-style: none; margin: 0; padding: 0; display: grid; gap: 6px; }
.embed-item {
  width: 100%; text-align: left; background: transparent; color: inherit; font: inherit; cursor: pointer;
  border: 1px solid transparent; border-radius: 8px; padding: 8px 10px; display: grid; gap: 2px;
}
.embed-item:hover { background: var(--panel); }
.embed-item.active { background: var(--panel-2); border-color: var(--border-strong); }
.embed-item .ei-name { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; display: flex; gap: 8px; align-items: center; }
.embed-item .ei-dot { width: 8px; height: 8px; border-radius: 50%; flex: none; background: var(--border-strong); }
.embed-item .ei-sub, .embed-item .ei-meta { font-size: 12px; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.badge-live { color: var(--ok); }

.workspace { padding: 20px; min-width: 0; }
.empty-state { text-align: center; padding: 15vh 16px; display: grid; gap: 8px; }
.empty-state p { margin: 0; }

.toolbar { display: flex; flex-wrap: wrap; align-items: center; gap: 10px; margin-bottom: 16px; }
.name-field { flex: 1 1 260px; }
#embed-name { font-size: 18px; font-weight: 700; background: transparent; border-color: transparent; padding-left: 6px; }
#embed-name:hover { border-color: var(--border); }
#embed-name:focus { border-color: var(--accent); background: var(--input); }
.dirty { color: #ffcf6e; font-size: 13px; }
.toolbar-actions { display: flex; gap: 8px; flex-wrap: wrap; }

.cols { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); gap: 20px; align-items: start; }

/* ── Editor ───────────────────────────────────────────────────────────── */
.editor { display: grid; gap: 14px; }
.insert-bar { display: flex; flex-wrap: wrap; gap: 6px 10px; align-items: center; }
.insert-bar select { flex: 1 1 240px; width: auto; }
.insert-bar #time-input { flex: 0 1 230px; width: auto; }
.insert-bar p { margin: 0; flex-basis: 100%; }
fieldset {
  margin: 0; border: 1px solid var(--border); border-radius: var(--radius); background: var(--panel);
  padding: 12px 14px 14px; display: grid; gap: 10px; min-width: 0;
}
legend { padding: 0 6px; font-weight: 700; font-size: 13px; text-transform: uppercase; letter-spacing: .06em; color: var(--accent); }
label { display: grid; gap: 4px; font-size: 13px; color: var(--muted); }
label.check { display: flex; align-items: center; gap: 8px; color: var(--text); }
.label-row { display: flex; justify-content: space-between; gap: 8px; }
.count { font-variant-numeric: tabular-nums; }
.count.over { color: var(--danger); font-weight: 700; }
.two { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }

.swatches { display: flex; flex-wrap: wrap; gap: 6px; }
.swatch {
  width: 28px; height: 28px; border-radius: 50%; border: 2px solid transparent; cursor: pointer; padding: 0;
  box-shadow: inset 0 0 0 1px rgba(0, 0, 0, .35);
}
.swatch[aria-pressed=true] { border-color: var(--text); }
.color-row { display: flex; gap: 8px; align-items: center; }
.color-row #color-hex { width: 110px; font-family: var(--mono); }

.field-list { list-style: none; margin: 0; padding: 0; display: grid; gap: 10px; }
.field-item { border: 1px solid var(--border); border-radius: 8px; padding: 10px; background: var(--panel-2); display: grid; gap: 8px; }
.field-head { display: flex; align-items: center; gap: 6px; }
.field-head .field-num { font-weight: 700; color: var(--muted); font-size: 12px; margin-right: auto; }

/* ── Preview ──────────────────────────────────────────────────────────── */
.preview-col { position: sticky; top: 77px; display: grid; gap: 16px; }
.preview-head { display: flex; justify-content: space-between; align-items: baseline; }
.preview-head h2, .post-card h2 { font-size: 13px; text-transform: uppercase; letter-spacing: .06em; color: var(--accent); }

.discord {
  background: var(--dc-bg); color: var(--dc-text); border-radius: var(--radius); padding: 16px 16px 18px;
  font-size: 15px; line-height: 1.375; overflow-wrap: anywhere; border: 1px solid #1e1f22;
}
.dc-message { display: grid; grid-template-columns: 40px minmax(0, 1fr); gap: 16px; }
.dc-avatar { width: 40px; height: 40px; border-radius: 50%; background: #1e1f22; }
.dc-header { display: flex; align-items: center; gap: 6px; flex-wrap: wrap; }
.dc-name { color: var(--dc-head); font-weight: 600; }
.dc-tag { background: #5865f2; color: #fff; font-size: 10px; font-weight: 700; border-radius: 3px; padding: 1px 4px; line-height: 15px; }
.dc-time { color: var(--dc-muted); font-size: 12px; }
.dc-content { margin-top: 2px; }

.dc-embed {
  margin-top: 6px; max-width: 520px; background: var(--dc-embed); border-radius: 4px;
  border-left: 4px solid #1e1f22; padding: 8px 16px 16px 12px;
}
.dc-grid { display: grid; grid-template-columns: minmax(0, 1fr) auto; }
.dc-grid > * { grid-column: 1; min-width: 0; }
.dc-author { display: flex; align-items: center; gap: 8px; margin-top: 8px; font-size: 14px; font-weight: 600; color: var(--dc-head); }
.dc-author img { width: 24px; height: 24px; border-radius: 50%; }
.dc-author a { color: var(--dc-head); text-decoration: none; }
.dc-title { margin-top: 8px; font-size: 16px; font-weight: 600; color: var(--dc-head); }
.dc-title a { color: var(--dc-link); text-decoration: none; }
.dc-desc { margin-top: 8px; font-size: 14px; white-space: normal; }
.dc-fields { margin-top: 8px; display: grid; grid-template-columns: repeat(12, minmax(0, 1fr)); gap: 8px; }
.dc-field-name { font-size: 14px; font-weight: 600; color: var(--dc-head); margin-bottom: 2px; }
.dc-field-value { font-size: 14px; }
.dc-thumb { grid-column: 2 !important; grid-row: 1 / span 8; margin: 8px 0 0 16px; max-width: 80px; max-height: 80px; border-radius: 4px; object-fit: contain; }
.dc-image { grid-column: 1 / 3 !important; margin-top: 16px; max-width: 100%; max-height: 300px; border-radius: 4px; justify-self: start; }
.dc-footer { grid-column: 1 / 3 !important; display: flex; align-items: center; gap: 8px; margin-top: 8px; font-size: 12px; color: var(--dc-muted); }
.dc-footer img { width: 20px; height: 20px; border-radius: 50%; }
.dc-empty { color: var(--dc-muted); font-style: italic; margin-top: 8px; font-size: 14px; }

/* Discord markdown */
.md a { color: var(--dc-link); text-decoration: none; }
.md a:hover { text-decoration: underline; }
.md-code { font-family: var(--mono); font-size: 85%; background: #1e1f22; border-radius: 3px; padding: 0 3px; }
.md-pre { font-family: var(--mono); font-size: 13px; background: #1e1f22; border: 1px solid #1a1b1e; border-radius: 4px; padding: 8px; margin: 4px 0; white-space: pre-wrap; }
.md-h1 { font-size: 22px; font-weight: 700; color: var(--dc-head); margin: 8px 0 4px; }
.md-h2 { font-size: 19px; font-weight: 700; color: var(--dc-head); margin: 8px 0 4px; }
.md-h3 { font-size: 16px; font-weight: 700; color: var(--dc-head); margin: 8px 0 4px; }
.md-sub { font-size: 12px; color: var(--dc-muted); }
.md-quote { border-left: 4px solid #4e5058; padding-left: 12px; }
.md-li { padding-left: 18px; position: relative; }
.md-li::before { content: "•"; position: absolute; left: 4px; }
.mention { background: rgba(88, 101, 242, .3); color: #c9cdfb; border-radius: 3px; padding: 0 2px; font-weight: 500; }
.mention.role { background: color-mix(in srgb, currentColor 18%, transparent); }
.md-time { background: rgba(255, 255, 255, .06); border-radius: 3px; padding: 0 2px; }
.spoiler { background: #1e1f22; color: transparent; border-radius: 3px; cursor: pointer; }
.spoiler:hover, .spoiler.open { background: rgba(255, 255, 255, .1); color: inherit; }
.emoji { width: 1.375em; height: 1.375em; vertical-align: bottom; object-fit: contain; }

/* Discord components (preview) */
.dc-components { margin-top: 8px; display: grid; gap: 8px; max-width: 520px; }
.dc-select {
  display: flex; align-items: center; justify-content: space-between; gap: 8px; width: 100%;
  background: #1e1f22; border: 1px solid #1e1f22; border-radius: 4px; padding: 8px 8px 8px 12px;
  color: var(--dc-muted); font: inherit; font-size: 15px; text-align: left; cursor: pointer;
}
.dc-select:hover { border-color: #111214; }
.dc-select.open { border-radius: 4px 4px 0 0; }
.dc-chev { font-size: 18px; line-height: 1; color: var(--dc-text); }
.dc-menu { margin-top: -8px; background: #2b2d31; border: 1px solid #1e1f22; border-top: 0; border-radius: 0 0 4px 4px; max-height: 260px; overflow: auto; }
.dc-opt { display: flex; align-items: center; gap: 10px; padding: 8px 12px; }
.dc-opt + .dc-opt { border-top: 1px solid rgba(255, 255, 255, .04); }
.dc-opt-emoji { width: 22px; height: 22px; object-fit: contain; font-size: 18px; line-height: 22px; text-align: center; flex: none; }
.dc-opt-label { color: var(--dc-head); font-size: 15px; }
.dc-opt-desc { color: var(--dc-muted); font-size: 13px; }
.dc-button { display: inline-block; background: #4e5058; color: #fff; border-radius: 4px; padding: 6px 16px; font-size: 14px; font-weight: 500; }

/* ── Role dropdown editor ─────────────────────────────────────────────── */
.menus .hint { margin: 0; }
.dd-list { list-style: none; margin: 0; padding: 0; display: grid; gap: 12px; }
.dd-item { border: 1px solid var(--border-strong); border-radius: 8px; padding: 10px; background: var(--panel-2); display: grid; gap: 10px; }
.dd-head { display: flex; align-items: center; gap: 6px; }
.dd-title { font-weight: 700; margin-right: auto; }
.opt-list { list-style: none; margin: 0; padding: 0; display: grid; gap: 8px; }
.opt {
  display: grid; gap: 6px; align-items: center;
  grid-template-columns: 40px minmax(0, 1fr) minmax(0, 1fr) auto;
  grid-template-areas: "emoji role label actions" "emoji desc link actions";
  background: var(--input); border: 1px solid var(--border); border-radius: 8px; padding: 6px;
}
.opt .emoji-btn { grid-area: emoji; align-self: stretch; }
.opt .opt-role { grid-area: role; }
.opt .opt-label { grid-area: label; }
.opt .opt-desc { grid-area: desc; }
.opt .opt-link { grid-area: link; display: flex; align-items: center; gap: 6px; white-space: nowrap; }
.opt .opt-link select { min-width: 0; }
.opt .opt-actions { grid-area: actions; display: grid; gap: 2px; }
.opt .opt-actions .btn { padding: 2px 6px; min-width: 26px; font-size: 12px; }
.opt input:not([type=checkbox]), .opt select { padding: 6px 8px; font-size: 14px; background: var(--panel); }
.emoji-btn {
  display: grid; place-items: center; background: var(--panel); border: 1px dashed var(--border-strong);
  border-radius: 8px; cursor: pointer; padding: 0; min-height: 36px; color: var(--muted); font-size: 18px;
}
.emoji-btn:hover { border-color: var(--accent); }
.emoji-btn .emoji { width: 24px; height: 24px; }
.emoji-btn .uni { font-size: 20px; width: auto; height: auto; }
.emoji-empty { opacity: .6; }

/* ── Popovers ─────────────────────────────────────────────────────────── */
.new-wrap { position: relative; }
.menu-pop {
  position: absolute; left: 0; right: 0; top: calc(100% + 6px); z-index: 30;
  background: var(--panel-2); border: 1px solid var(--border-strong); border-radius: 10px;
  box-shadow: 0 16px 40px rgba(0, 0, 0, .5); padding: 6px; display: grid; gap: 2px;
}
.menu-pop button {
  display: grid; gap: 1px; text-align: left; background: none; border: 0; color: var(--text); font: inherit;
  padding: 8px 10px; border-radius: 6px; cursor: pointer;
}
.menu-pop button:hover, .menu-pop button:focus-visible { background: var(--panel); }
.menu-pop button span { font-size: 12px; color: var(--muted); }
.menu-sep { font-size: 11px; text-transform: uppercase; letter-spacing: .08em; color: var(--muted); padding: 8px 10px 2px; border-top: 1px solid var(--border); margin-top: 4px; }
#template-items { display: grid; gap: 2px; }

.emoji-pop {
  position: absolute; z-index: 40; width: min(340px, calc(100vw - 16px));
  background: var(--panel-2); border: 1px solid var(--border-strong); border-radius: 10px;
  box-shadow: 0 16px 40px rgba(0, 0, 0, .5); padding: 10px; display: grid; gap: 8px;
}
.ep-head { display: flex; gap: 6px; }
.ep-section { font-size: 11px; text-transform: uppercase; letter-spacing: .08em; color: var(--muted); }
.ep-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(36px, 1fr)); gap: 4px; max-height: 200px; overflow: auto; }
.ep-item { display: grid; place-items: center; height: 36px; background: none; border: 1px solid transparent; border-radius: 6px; cursor: pointer; padding: 0; font-size: 20px; }
.ep-item:hover, .ep-item:focus-visible { background: var(--panel); border-color: var(--border-strong); }
.ep-item img { width: 26px; height: 26px; object-fit: contain; }

.ei-tag { flex: none; font-size: 10px; font-weight: 700; text-transform: uppercase; letter-spacing: .05em; color: var(--accent); border: 1px solid color-mix(in srgb, var(--accent) 45%, transparent); border-radius: 4px; padding: 0 4px; }
.ei-title { overflow: hidden; text-overflow: ellipsis; }

/* ── Post panel ───────────────────────────────────────────────────────── */
.post-card { padding: 14px; display: grid; gap: 10px; }
.post-card h3 { font-size: 13px; color: var(--muted); font-weight: 600; margin-top: 4px; }
.post-card p { margin: 0; }
.post-row { display: flex; gap: 8px; }
.post-row select { flex: 1; }
.post-list { list-style: none; margin: 0; padding: 0; display: grid; gap: 6px; }
.post-list li { display: flex; align-items: center; gap: 8px; padding: 6px 8px; border-radius: 8px; background: var(--panel-2); }
.post-list .pl-info { flex: 1; min-width: 0; }
.post-list .pl-channel { font-weight: 600; }
.post-list .pl-meta { font-size: 12px; color: var(--muted); }

/* ── Toasts ───────────────────────────────────────────────────────────── */
.toasts { position: fixed; right: 16px; bottom: 16px; display: grid; gap: 8px; z-index: 50; max-width: min(420px, calc(100vw - 32px)); }
.toast {
  background: var(--panel-2); border: 1px solid var(--border-strong); border-left: 4px solid var(--accent);
  border-radius: 8px; padding: 10px 12px; box-shadow: 0 10px 30px rgba(0, 0, 0, .4);
  display: grid; grid-template-columns: 1fr auto; gap: 4px 10px; align-items: start;
}
.toast.ok { border-left-color: var(--ok); }
.toast.error { border-left-color: var(--danger); }
.toast ul { margin: 4px 0 0; padding-left: 18px; color: var(--muted); font-size: 13px; grid-column: 1; }
.toast .t-close { background: none; border: 0; color: var(--muted); cursor: pointer; font-size: 18px; line-height: 1; padding: 0 2px; }

/* ── Responsive ───────────────────────────────────────────────────────── */
@media (max-width: 1100px) {
  .cols { grid-template-columns: minmax(0, 1fr); }
  .preview-col { position: static; order: -1; }
}
@media (max-width: 760px) {
  .layout { grid-template-columns: minmax(0, 1fr); }
  .sidebar { border-right: 0; border-bottom: 1px solid var(--border); }
  .embed-list { max-height: 220px; overflow: auto; }
  .workspace { padding: 16px; }
  .topbar { padding: 10px 16px; }
  .who #user-name { display: none; }
  .two { grid-template-columns: minmax(0, 1fr); }
  .opt { grid-template-columns: 40px minmax(0, 1fr) auto; grid-template-areas: "emoji role actions" "emoji label actions" "desc desc desc" "link link link"; }
}
