:root {
  --bg: #0f0f12;
  --surface: #17171c;
  --surface-2: #1f1f26;
  --border: #2a2a33;
  --text: #ececf1;
  --muted: #8b8b98;
  --accent: #7c3aed;
  --accent-hover: #6d28d9;
  --danger: #ef4444;
  --ok: #22c55e;
  --radius: 10px;
  /* Discord preview palette */
  --d-bg: #313338;
  --d-embed: #2b2d31;
  --d-text: #dbdee1;
  --d-muted: #949ba4;
  --d-link: #00a8fc;
  --d-header: #f2f3f5;
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: 14px/1.45 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
}
[hidden] { display: none !important; }
h1, h2, h3, h4 { margin: 0; font-weight: 600; }
h2 { font-size: 13px; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); }
h3 { font-size: 15px; margin-bottom: 10px; }
h4 { font-size: 12px; text-transform: uppercase; letter-spacing: .05em; color: var(--muted); margin: 16px 0 6px; }
code { background: var(--surface-2); padding: 1px 5px; border-radius: 4px; font-size: 13px; }
.muted { color: var(--muted); }
a { color: var(--d-link); }

/* ---- Top bar ---------------------------------------------------------- */
.topbar {
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: 10px 20px; border-bottom: 1px solid var(--border); background: var(--surface);
  position: sticky; top: 0; z-index: 10;
}
.brand { display: flex; align-items: baseline; gap: 10px; }
.logo { font-weight: 800; font-size: 20px; letter-spacing: -.02em; color: var(--accent); }
.sub { color: var(--muted); }
.who { display: flex; align-items: center; gap: 8px; min-width: 0; }
.who img { width: 24px; height: 24px; border-radius: 50%; }
.who span { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

/* ---- Layout ----------------------------------------------------------- */
.layout {
  display: grid;
  grid-template-columns: 220px minmax(0, 1fr) minmax(0, 520px);
  gap: 20px; padding: 20px; align-items: start;
}
.sidebar, .editor { min-width: 0; }
.side-head, .editor-head { display: flex; align-items: center; justify-content: space-between; gap: 10px; margin-bottom: 12px; }
.template-list { list-style: none; margin: 0; padding: 0; display: grid; gap: 4px; }
.tpl {
  width: 100%; display: flex; flex-direction: column; align-items: flex-start; gap: 2px;
  background: none; border: 1px solid transparent; color: var(--text); text-align: left;
  padding: 8px 10px; border-radius: 8px; cursor: pointer; font: inherit;
}
.tpl:hover { background: var(--surface); }
.tpl.active { background: var(--surface-2); border-color: var(--border); }
.tpl span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; max-width: 100%; }
.empty { padding: 8px 10px; }

.name-input {
  flex: 1; min-width: 0; font-size: 18px; font-weight: 600; background: transparent;
  border: 1px solid transparent; color: var(--text); padding: 6px 8px; border-radius: 8px;
}
.name-input:hover, .name-input:focus { border-color: var(--border); background: var(--surface); outline: none; }
.actions { display: flex; gap: 8px; flex-shrink: 0; }

.preview-sticky { position: sticky; top: 72px; display: grid; gap: 12px; max-height: calc(100vh - 92px); overflow-y: auto; padding-bottom: 8px; }

/* ---- Controls --------------------------------------------------------- */
.btn {
  background: var(--surface-2); color: var(--text); border: 1px solid var(--border);
  padding: 8px 14px; border-radius: 8px; font: inherit; font-weight: 500; cursor: pointer;
}
.btn:hover:not(:disabled) { border-color: #3a3a46; background: #26262f; }
.btn:disabled { opacity: .5; cursor: not-allowed; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.primary:hover:not(:disabled) { background: var(--accent-hover); }
.btn.ghost { background: transparent; }
.btn.small { padding: 5px 10px; font-size: 13px; }
.btn.wide { width: 100%; }
.btn.danger { color: var(--danger); }
.icon {
  background: none; border: 1px solid transparent; color: var(--muted); cursor: pointer;
  width: 26px; height: 26px; border-radius: 6px; font-size: 13px; line-height: 1;
}
.icon:hover { color: var(--text); background: var(--surface-2); border-color: var(--border); }
.icon.danger:hover { color: var(--danger); }

input[type=text], input[type=url], input:not([type]), textarea, select {
  width: 100%; background: var(--bg); color: var(--text); border: 1px solid var(--border);
  border-radius: 8px; padding: 8px 10px; font: inherit;
}
textarea { resize: vertical; min-height: 38px; }
input:focus, textarea:focus, select:focus { outline: 2px solid var(--accent); outline-offset: -1px; border-color: transparent; }
input[type=color] { width: 44px; height: 30px; padding: 0; border: 1px solid var(--border); border-radius: 6px; background: none; cursor: pointer; }

.field { display: grid; gap: 4px; margin-bottom: 10px; min-width: 0; }
.lbl { display: flex; justify-content: space-between; gap: 8px; font-size: 12px; font-weight: 600; color: var(--muted); }
.count { font-weight: 400; font-variant-numeric: tabular-nums; }
.count.over { color: var(--danger); font-weight: 600; }
.check { display: flex; align-items: center; gap: 8px; margin: 8px 0; cursor: pointer; }
.check.inline { display: inline-flex; margin: 0 8px 0 0; font-size: 12px; color: var(--muted); }
.row2 { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.row3 { display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 10px; }
.row3:has(> .field:first-child input[placeholder="🔗"]) { grid-template-columns: 70px 1fr 1.4fr; }
.color-row { display: flex; align-items: center; gap: 12px; margin: 4px 0 4px; }

/* ---- Sections --------------------------------------------------------- */
.section, .card {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 14px 16px; margin-bottom: 14px;
}
.section > summary {
  cursor: pointer; font-weight: 600; display: flex; align-items: center; gap: 8px;
  list-style: none; margin: -14px -16px 0; padding: 14px 16px;
}
.section > summary::-webkit-details-marker { display: none; }
.section > summary::before { content: '▸'; color: var(--muted); transition: transform .15s; }
.section[open] > summary::before { transform: rotate(90deg); }
.section[open] > summary { margin-bottom: 8px; }
.summary-actions { margin-left: auto; display: flex; gap: 2px; }
.swatch { width: 12px; height: 12px; border-radius: 3px; border: 1px solid var(--border); flex-shrink: 0; }
.subcard { border: 1px solid var(--border); border-radius: 8px; padding: 10px 12px; margin-bottom: 10px; background: var(--bg); }
.subcard-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: 6px; font-size: 13px; }
.subcard-head > div { display: flex; align-items: center; }

/* ---- Send / errors ---------------------------------------------------- */
.send select { margin-bottom: 10px; }
.radio-row { display: grid; gap: 6px; margin: 4px 0 6px; }
.radio-row label { display: flex; gap: 8px; align-items: center; cursor: pointer; }
.radio-row input:disabled + * , .radio-row label:has(input:disabled) { opacity: .5; cursor: not-allowed; }
.errors { list-style: none; margin: 0; padding: 0; display: grid; gap: 6px; }
.errors li {
  background: rgba(239, 68, 68, .1); border: 1px solid rgba(239, 68, 68, .35); color: #fca5a5;
  padding: 8px 10px; border-radius: 8px; font-size: 13px;
}

/* ---- Toasts / dialog -------------------------------------------------- */
.toasts { position: fixed; bottom: 16px; right: 16px; display: grid; gap: 8px; z-index: 50; max-width: min(420px, calc(100vw - 32px)); }
.toast { background: var(--surface-2); border: 1px solid var(--border); border-left: 4px solid var(--ok); padding: 10px 14px; border-radius: 8px; box-shadow: 0 8px 24px rgba(0,0,0,.4); }
.toast.error { border-left-color: var(--danger); }
dialog {
  background: var(--surface); color: var(--text); border: 1px solid var(--border); border-radius: 12px;
  width: min(640px, calc(100vw - 32px)); padding: 20px;
}
dialog::backdrop { background: rgba(0,0,0,.6); }
dialog textarea { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 12px; margin: 10px 0; }
.dialog-actions { display: flex; justify-content: flex-end; gap: 8px; }

/* ---- Login page ------------------------------------------------------- */
.simple { min-height: calc(100vh - 60px); display: grid; place-items: center; padding: 16px; }
body.simple { min-height: 100vh; }
.simple .card { max-width: 440px; text-align: center; padding: 32px; }
.simple h1 { margin-bottom: 12px; font-size: 22px; }

/* ---- Discord preview -------------------------------------------------- */
.discord { background: var(--d-bg); color: var(--d-text); border-radius: var(--radius); padding: 16px 16px 16px 12px; font-size: 15px; line-height: 1.375; overflow-wrap: anywhere; }
.msg { display: flex; gap: 14px; }
.msg-avatar { width: 40px; height: 40px; border-radius: 50%; flex-shrink: 0; object-fit: cover; }
.msg-body { min-width: 0; flex: 1; }
.msg-meta { display: flex; align-items: center; gap: 6px; margin-bottom: 2px; }
.msg-name { color: var(--d-header); font-weight: 500; }
.app-tag { background: #5865f2; color: #fff; font-size: 10px; font-weight: 600; padding: 1px 4px; border-radius: 3px; line-height: 1.3; }
.msg-time { color: var(--d-muted); font-size: 12px; margin-left: 2px; }

.md h1 { font-size: 24px; margin: 8px 0 4px; color: var(--d-header); }
.md h2 { font-size: 20px; margin: 8px 0 4px; color: var(--d-header); text-transform: none; letter-spacing: 0; }
.md h3 { font-size: 16px; margin: 8px 0 4px; color: var(--d-header); }
.md .subtext { display: block; font-size: 12px; color: var(--d-muted); }
.md blockquote { margin: 2px 0; padding-left: 12px; border-left: 4px solid #4e5058; }
.md ul, .md ol { margin: 2px 0; padding-left: 22px; }
.md code { background: #2b2d31; border: 1px solid #1e1f22; padding: 0 3px; border-radius: 4px; font-size: 85%; color: var(--d-text); }
.md pre { background: #2b2d31; border: 1px solid #1e1f22; border-radius: 4px; padding: 8px; margin: 4px 0; white-space: pre-wrap; }
.md pre code { background: none; border: 0; padding: 0; }
.md a { color: var(--d-link); text-decoration: none; }
.md a:hover { text-decoration: underline; }
.md .mention { background: rgba(88, 101, 242, .3); color: #c9cdfb; border-radius: 3px; padding: 0 2px; font-weight: 500; }
.md .spoiler { background: #1e1f22; color: transparent; border-radius: 3px; cursor: pointer; }
.md .spoiler:hover { color: inherit; background: #3a3c42; }
.md .timestamp { background: rgba(255,255,255,.06); border-radius: 3px; padding: 0 2px; }
.md .emoji { width: 22px; height: 22px; vertical-align: bottom; object-fit: contain; }

.embed {
  display: grid; grid-template-columns: minmax(0, 1fr) auto; gap: 0 16px; max-width: 516px;
  background: var(--d-embed); border-left: 4px solid #1e1f22; border-radius: 4px;
  padding: 8px 16px 16px 12px; margin-top: 8px; font-size: 14px;
}
.embed-inner { min-width: 0; display: grid; gap: 8px; padding-top: 8px; }
.embed-author { display: flex; align-items: center; gap: 8px; font-size: 14px; font-weight: 600; color: var(--d-header); }
.embed-author a { color: var(--d-header); text-decoration: none; }
.embed-author img { width: 24px; height: 24px; border-radius: 50%; object-fit: cover; }
.embed-title { font-weight: 600; font-size: 16px; color: var(--d-header); }
.title-link { text-decoration: none; }
.title-link .embed-title { color: var(--d-link); }
.embed-desc { font-size: 14px; }
.embed-fields { display: grid; grid-template-columns: repeat(12, 1fr); gap: 8px; }
.embed-field { min-width: 0; font-size: 14px; }
.field-name { font-weight: 600; color: var(--d-header); margin-bottom: 2px; }
.embed-image { max-width: 100%; max-height: 300px; border-radius: 4px; object-fit: contain; justify-self: start; margin-top: 8px; }
.embed-thumb { width: 80px; height: 80px; border-radius: 4px; object-fit: cover; margin-top: 8px; }
.embed-footer { display: flex; align-items: center; gap: 8px; font-size: 12px; color: var(--d-muted); margin-top: 8px; }
.embed-footer img { width: 20px; height: 20px; border-radius: 50%; }
.msg-buttons { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 8px; }
.dbtn {
  display: inline-flex; align-items: center; gap: 6px; background: #4e5058; color: #fff; text-decoration: none;
  padding: 2px 16px; min-height: 32px; border-radius: 3px; font-size: 14px; font-weight: 500;
}
.dbtn:hover { background: #6d6f78; }
.dbtn .ext { font-size: 12px; opacity: .8; }

/* ---- Responsive ------------------------------------------------------- */
@media (max-width: 1100px) {
  .layout { grid-template-columns: 200px minmax(0, 1fr); }
  .preview-col { grid-column: 1 / -1; }
  .preview-sticky { position: static; max-height: none; }
}
@media (max-width: 720px) {
  .layout { grid-template-columns: minmax(0, 1fr); padding: 16px; gap: 16px; }
  .topbar { padding: 10px 16px; flex-wrap: wrap; }
  .sub { display: none; }
  .who span:not(:first-of-type) { display: none; }
  .row2, .row3 { grid-template-columns: 1fr; }
  .row3:has(> .field:first-child input[placeholder="🔗"]) { grid-template-columns: 1fr; }
  .editor-head { flex-wrap: wrap; }
  .preview-col { order: 2; }
}

/* OAuth result page */
.simple .avatar { display: block; width: 88px; height: 88px; border-radius: 50%; object-fit: cover; margin: 0 auto 14px; }
