/* Template Manager — styled on the 1lessidiot/Chatwoot design tokens
   (design-tokens.css). No raw hex here except the WhatsApp preview, which
   deliberately uses WhatsApp's own colours. */
* { box-sizing: border-box; }
body {
  margin: 0; background: var(--bg); color: var(--text);
  font-family: var(--font-sans); font-size: var(--t-default); line-height: 1.55;
  -webkit-font-smoothing: antialiased;
}
.muted { color: var(--text-muted); font-weight: 400; }
.small { font-size: var(--t-small); }
.hidden { display: none !important; }

header {
  padding: var(--s-normal) var(--s-medium);
  background: var(--surface); border-bottom: 1px solid var(--border);
}
header h1 { margin: 0; font-size: var(--t-large); font-weight: 600; letter-spacing: -.01em; }
.header-row { display: flex; align-items: center; justify-content: space-between; gap: var(--s-normal); flex-wrap: wrap; }
.account-switch { display: flex; align-items: center; gap: var(--s-smaller); margin: 0; font-size: var(--t-small); color: var(--text-muted); }
.account-switch select { width: auto; min-width: 220px; margin-top: 0; }
#config { margin-top: var(--s-smaller); font-family: var(--font-mono); font-size: 12px; color: var(--text-subtle); }

main { max-width: 1120px; margin: var(--s-medium) auto; padding: 0 var(--s-normal); }

/* Composer: form left, preview right (order overrides DOM) */
.composer { display: grid; grid-template-columns: 1fr 320px; gap: var(--s-medium); align-items: start; }
@media (max-width: 820px) { .composer { grid-template-columns: 1fr; } }
.form-col { order: 1; background: var(--surface); border: 1px solid var(--border); border-radius: var(--r-lg); padding: var(--s-medium); }
.preview-col { order: 2; position: sticky; top: var(--s-normal); }
@media (max-width: 820px) { .preview-col { order: 0; } }

/* Form fields */
label { display: block; margin-bottom: var(--s-slab); font-weight: 600; font-size: var(--t-small); }
input, select, textarea {
  width: 100%; margin-top: var(--s-smaller); padding: 9px 11px;
  border: 1px solid var(--border-strong); border-radius: var(--r-md);
  background: var(--surface); color: var(--text); font: inherit; font-weight: 400;
  transition: border-color var(--dur-base) var(--ease-standard), box-shadow var(--dur-fast) var(--ease-standard);
}
input::placeholder, textarea::placeholder { color: var(--text-subtle); }
input:focus, select:focus, textarea:focus { outline: none; border-color: var(--primary); box-shadow: 0 0 0 3px var(--ring); }
textarea { resize: vertical; }
.row { display: grid; grid-template-columns: 1fr 1fr; gap: var(--s-slab); }

fieldset { border: 1px solid var(--border); border-radius: var(--r-lg); padding: var(--s-normal); margin: 0 0 var(--s-normal); }
legend { font-weight: 600; font-size: var(--t-small); padding: 0 var(--s-smaller); }
/* Segmented pill control — native radio kept for form + JS, visually hidden. */
.radios { display: flex; flex-wrap: wrap; gap: var(--s-smaller); }
.radios label {
  position: relative; margin: 0; font-weight: 500; font-size: var(--t-small); line-height: 1;
  display: inline-flex; align-items: center; padding: 8px 15px;
  border: 1px solid var(--border-strong); border-radius: var(--r-full);
  background: var(--surface); color: var(--text-muted); cursor: pointer; user-select: none;
  transition: background var(--dur-base) var(--ease-standard),
              border-color var(--dur-base) var(--ease-standard),
              color var(--dur-base) var(--ease-standard);
}
.radios label:hover { border-color: var(--primary); color: var(--text); }
.radios label:has(input:checked) { background: var(--primary-tint); border-color: var(--primary); color: var(--primary); font-weight: 600; }
.radios label:has(input:focus-visible) { box-shadow: 0 0 0 3px var(--ring); }
.radios input { position: absolute; opacity: 0; width: 1px; height: 1px; margin: 0; pointer-events: none; }
.hint { margin: var(--s-small) 0 0; font-size: 12px; }
.hint:empty { display: none; }
/* Constant slot height for every header type (including None) so switching
   options never shifts the layout — kills the jitter. */
.header-extra { margin-top: var(--s-slab); min-height: 76px; }
input[type=file] { padding: 7px 9px; background: var(--subtle); }

/* Body toolbar */
.toolbar { position: relative; display: flex; flex-wrap: wrap; gap: var(--s-smaller); margin-bottom: var(--s-small); }
.toolbar button {
  background: var(--subtle); color: var(--text); border: 1px solid var(--border);
  border-radius: var(--r-md); padding: 5px 9px; font-size: 13px; font: inherit; font-size: 13px; cursor: pointer;
  transition: background var(--dur-base) var(--ease-standard), border-color var(--dur-base) var(--ease-standard);
}
.toolbar button:hover { border-color: var(--border-strong); background: var(--surface); }
.emoji-pop {
  position: absolute; top: 34px; left: 0; z-index: 5; background: var(--surface);
  border: 1px solid var(--border); border-radius: var(--r-lg); padding: var(--s-small);
  display: grid; grid-template-columns: repeat(8, 1fr); gap: 2px; box-shadow: var(--sh-md);
}
.emoji-pop button { border: 0; background: none; font-size: 18px; padding: 3px; cursor: pointer; border-radius: var(--r-sm); }
.emoji-pop button:hover { background: var(--subtle); }

/* Buttons builder */
.btn-row { border: 1px solid var(--border); border-radius: var(--r-lg); padding: var(--s-slab); margin-bottom: var(--s-small); background: var(--subtle); }
.btn-row-head { display: flex; justify-content: space-between; align-items: center; font-weight: 600; font-size: 13px; margin-bottom: var(--s-small); }
.btn-row-head .rm { background: none; border: 0; cursor: pointer; font-size: 15px; color: var(--text-muted); }
.btn-row-head .rm:hover { color: var(--danger-solid); }
.btn-fields { display: grid; gap: var(--s-small); }
.btn-fields input, .btn-fields select { margin-top: 0; background: var(--surface); }
.add-button-wrap select { background: var(--subtle); font-weight: 600; }

/* Buttons */
button.primary, button.secondary {
  font: inherit; font-size: var(--t-small); font-weight: 600; border-radius: var(--r-md);
  padding: 9px 16px; cursor: pointer; border: 1px solid transparent;
  transition: background var(--dur-base) var(--ease-standard), border-color var(--dur-base) var(--ease-standard), box-shadow var(--dur-fast) var(--ease-standard);
}
button.primary { background: var(--primary); color: var(--primary-fg); }
button.primary:hover { background: var(--primary-hover); }
button.secondary { background: var(--surface); color: var(--text); border-color: var(--border-strong); }
button.secondary:hover { border-color: var(--text-subtle); }
button.primary:focus-visible, button.secondary:focus-visible { outline: none; box-shadow: 0 0 0 3px var(--ring); }
button:disabled { opacity: .5; cursor: default; }
.form-actions { display: flex; align-items: center; gap: var(--s-small); flex-wrap: wrap; }

.msg { display: inline-block; margin-left: var(--s-small); font-size: 13px; min-height: 18px; }
.msg.ok { color: var(--success); } .msg.bad { color: var(--danger); }

/* Status badges — soft semantic */
.badge { display: inline-flex; align-items: center; gap: 5px; font-size: 12px; font-weight: 600; padding: 3px 10px; border-radius: var(--r-full); }
.badge::before { content: ""; width: 6px; height: 6px; border-radius: var(--r-full); background: currentColor; }
.badge.ok { background: var(--success-bg); color: var(--success); }
.badge.pending { background: var(--warning-bg); color: var(--warning); }
.badge.bad { background: var(--danger-bg); color: var(--danger); }
.badge.draft { background: var(--subtle); color: var(--text-muted); }

/* WhatsApp phone preview — WhatsApp's own colours */
.phone { background: var(--surface); border: 1px solid var(--border); border-radius: var(--r-xl); overflow: hidden; box-shadow: var(--sh-md); }
.phone-top { background: var(--wa-header); color: #fff; padding: 12px 14px; font-weight: 600; font-size: 14px; display: flex; align-items: center; gap: 6px; }
.phone-top .dot { width: 20px; height: 20px; border-radius: var(--r-full); background: #25d366; }
.phone-screen { background: var(--wa-screen); padding: var(--s-normal) var(--s-slab); min-height: 360px; }
.bubble { background: var(--wa-bubble); border-radius: var(--r-lg); padding: 7px 9px; box-shadow: 0 1px 1px rgba(0,0,0,.08); max-width: 260px; font-size: 14px; color: var(--wa-text); }
.bubble.mini { max-width: 100%; font-size: 13px; }
.b-media { border-radius: var(--r-md); overflow: hidden; margin-bottom: var(--s-small); }
.b-media img, .b-media video { width: 100%; display: block; max-height: 180px; object-fit: cover; }
.b-media.ph { background: #cfe9c8; color: #3a5a40; padding: 22px 8px; text-align: center; font-size: 13px; }
.b-header-text { font-weight: 700; margin-bottom: var(--s-smaller); }
.b-body { white-space: normal; word-wrap: break-word; }
.b-body .var { color: #075e54; background: #c9efb8; border-radius: var(--r-sm); padding: 0 2px; }
.b-body code { background: rgba(0,0,0,.06); padding: 0 3px; border-radius: var(--r-sm); font-size: 12px; font-family: var(--font-mono); }
.b-footer { color: var(--wa-meta); font-size: 12px; margin-top: 5px; }
.b-buttons { margin-top: 5px; display: grid; gap: 3px; }
.b-btn { background: #fff; color: var(--wa-btn); text-align: center; padding: 7px; border-radius: var(--r-md); font-size: 13px; box-shadow: 0 1px 1px rgba(0,0,0,.08); }

/* Templates list — cards */
.list-section { margin-top: var(--s-large); }
.list-head { display: flex; align-items: center; gap: var(--s-slab); margin-bottom: var(--s-normal); }
.list-head h2 { margin: 0; font-size: var(--t-medium); font-weight: 600; }
.cards { display: grid; grid-template-columns: repeat(auto-fill, minmax(260px, 1fr)); gap: var(--s-normal); }
.tcard { position: relative; background: var(--card-bg); border: 1px solid var(--border); border-radius: var(--r-lg); padding: var(--s-slab); }
.tcard-head { display: flex; align-items: center; justify-content: space-between; gap: var(--s-small); }
.tcard-head .tcard-name { min-width: 0; }
@media (prefers-reduced-motion: no-preference) { .tcard { animation: tm-fade-in-up var(--dur-slow) var(--ease-decelerate) both; } }
.tcard-menu { position: absolute; top: 10px; right: 10px; }
.tcard-menu .dots { background: var(--surface); border: 1px solid var(--border); border-radius: var(--r-md); width: 28px; height: 28px; cursor: pointer; font-size: 16px; line-height: 1; color: var(--text-muted); }
.tcard-menu .dots:hover { color: var(--text); border-color: var(--border-strong); }
.tcard-menu .menu { position: absolute; right: 0; top: 32px; background: var(--surface); border: 1px solid var(--border); border-radius: var(--r-lg); box-shadow: var(--sh-md); display: flex; flex-direction: column; min-width: 130px; z-index: 5; overflow: hidden; }
.tcard-menu .menu button { background: none; border: 0; text-align: left; padding: 9px 12px; cursor: pointer; font: inherit; font-size: var(--t-small); color: var(--text); }
.tcard-menu .menu button:hover { background: var(--subtle); }
.tcard-menu .menu button.danger { color: var(--danger); }
.tcard-preview { background: var(--wa-screen); border-radius: var(--r-lg); padding: var(--s-slab); margin-bottom: var(--s-slab); }
.tcard-name { font-weight: 600; font-size: var(--t-small); word-break: break-all; }
.tcard-meta { margin-top: var(--s-smaller); font-size: 12px; color: var(--text-subtle); }
.tcard-submit { width: 100%; margin-top: var(--s-slab); }

/* motion helpers */
@keyframes tm-fade-in-up { from { opacity: 0; transform: translateY(8px); } to { opacity: 1; transform: none; } }
@keyframes tm-spin { to { transform: rotate(360deg); } }
.spinner { display: inline-block; width: 15px; height: 15px; border-radius: var(--r-full); border: 2px solid var(--subtle); border-top-color: var(--primary); animation: tm-spin .9s linear infinite; vertical-align: -3px; }
@media (prefers-reduced-motion: reduce) {
  *, ::before, ::after { animation-duration: .001ms !important; animation-iteration-count: 1 !important; transition-duration: .001ms !important; }
}

/* Edit mode banner */
.edit-banner { background: var(--warning-bg); color: var(--warning); border: 1px solid var(--warning); border-radius: var(--r-md); padding: 8px 12px; margin-bottom: 12px; font-size: 13px; display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.linkbtn { background: none; border: none; color: var(--primary); cursor: pointer; text-decoration: underline; font-size: 13px; padding: 0; }
