@import url('https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700;800;900&family=JetBrains+Mono:wght@400;500&display=swap');

/* ── Tokens ─────────────────────────────────────── */
:root {
  --accent:        #6366f1;
  --accent-2:      #8b5cf6;
  --accent-3:      #ec4899;
  --accent-glow:   rgba(99,102,241,0.22);

  --bg:            #f0f2f8;
  --surface:       rgba(255,255,255,0.78);
  --surface-s:     #ffffff;
  --input-bg:      rgba(0,0,0,0.04);
  --border:        rgba(0,0,0,0.07);
  --text-h:        #0d0f1a;
  --text-p:        #4a5069;
  --text-muted:    #8892b0;
  --shadow:        0 4px 24px rgba(0,0,0,0.07);
  --shadow-lg:     0 16px 48px rgba(0,0,0,0.13);
  --header-bg:     rgba(240,242,248,0.92);

  --r-xl:  24px;
  --r-lg:  18px;
  --r-md:  14px;
  --r-sm:  10px;

  --font: 'Inter', -apple-system, BlinkMacSystemFont, sans-serif;
  --font-mono: 'JetBrains Mono', 'Fira Code', monospace;

  /* kept for JS-driven modal overlap bg */
  --primary: #6366f1;
  --primary-gradient: linear-gradient(135deg, #6366f1 0%, #8b5cf6 100%);
  --primary-glow: rgba(99,102,241,0.4);

  /* legacy compat aliases used throughout */
  --bg-color:     #f0f2f8;
  --bg-gradient:  linear-gradient(160deg,#f0f2f8 0%,#e6e8f4 100%);
  --surface-solid: #ffffff;
  --text-main:    #0d0f1a;
  --glass-shadow: var(--shadow);
  --radius-lg:    var(--r-xl);
  --radius-md:    var(--r-lg);
  --radius-sm:    var(--r-md);
}

[data-theme="dark"],
body.dark-mode {
  --bg:            #0a0a0f;
  --surface:       rgba(22,22,34,0.82);
  --surface-s:     #16161f;
  --input-bg:      rgba(255,255,255,0.05);
  --border:        rgba(255,255,255,0.08);
  --text-h:        #f0f2ff;
  --text-p:        #8892b0;
  --text-muted:    #4a5069;
  --shadow:        0 4px 24px rgba(0,0,0,0.5);
  --shadow-lg:     0 16px 48px rgba(0,0,0,0.7);
  --header-bg:     rgba(10,10,15,0.92);

  --bg-color:       #0a0a0f;
  --bg-gradient:    linear-gradient(160deg,#0a0a0f 0%,#06060d 100%);
  --surface-solid:  #16161f;
  --text-main:      #f0f2ff;
  --glass-shadow:   var(--shadow);
}

/* ── Reset ──────────────────────────────────────── */
*, *::before, *::after {
  box-sizing: border-box; margin: 0; padding: 0;
  -webkit-tap-highlight-color: transparent;
}

html { -webkit-font-smoothing: antialiased; }

body {
  font-family: var(--font);
  background: var(--bg-gradient);
  color: var(--text-h);
  height: 100dvh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  transition: background 0.4s, color 0.4s;
}

::selection { background: rgba(99,102,241,0.18); }

/* ── Mesh Background ────────────────────────────── */
.bg-mesh {
  position: fixed; inset: 0; z-index: 0;
  overflow: hidden; pointer-events: none;
}
.bg-mesh::before, .bg-mesh::after {
  content: ''; position: absolute; border-radius: 50%;
  filter: blur(80px); opacity: 0.22;
}
[data-theme="dark"] .bg-mesh::before,
[data-theme="dark"] .bg-mesh::after,
body.dark-mode .bg-mesh::before,
body.dark-mode .bg-mesh::after { opacity: 0.11; }

.bg-mesh::before {
  width: 65vw; height: 65vw; top: -20vw; left: -20vw;
  background: radial-gradient(circle, #7c6fee, #ec4899);
  animation: floatA 18s ease-in-out infinite alternate;
}
.bg-mesh::after {
  width: 50vw; height: 50vw; bottom: -14vw; right: -14vw;
  background: radial-gradient(circle, #06b6d4, #6366f1);
  animation: floatB 22s ease-in-out infinite alternate;
}
@keyframes floatA { from{transform:translate(0,0) scale(1)} to{transform:translate(5vw,4vw) scale(1.1)} }
@keyframes floatB { from{transform:translate(0,0) scale(1)} to{transform:translate(-4vw,-5vw) scale(1.08)} }

/* ── Header ─────────────────────────────────────── */
header {
  position: sticky; top: 0; z-index: 100;
  height: 56px;
  padding: 0 16px;
  display: flex;
  align-items: center;
  gap: 10px;
  background: var(--header-bg);
  backdrop-filter: saturate(1.8) blur(24px);
  -webkit-backdrop-filter: saturate(1.8) blur(24px);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
  animation: slideDown 0.55s cubic-bezier(0.16,1,0.3,1);
  width: 100%;
}
@keyframes slideDown {
  from{opacity:0;transform:translateY(-16px)} to{opacity:1;transform:translateY(0)}
}

.header-title-block {
  display: flex; flex-direction: column; gap: 0;
}
.header-eyebrow {
  font-size: 10px; font-weight: 600; letter-spacing: 1px;
  text-transform: uppercase; color: var(--accent); line-height: 1;
}
.brand {
  font-weight: 800; font-size: 15px; letter-spacing: -0.3px;
  background: linear-gradient(135deg, var(--text-h) 40%, var(--text-p));
  -webkit-background-clip: text; background-clip: text;
  -webkit-text-fill-color: transparent;
  white-space: nowrap; line-height: 1.2;
}

/* Back button */
.back-btn-absolute {
  width: 34px; height: 34px; border-radius: 50%;
  flex-shrink: 0;
  display: flex; align-items: center; justify-content: center;
  background: var(--surface);
  border: 1px solid var(--border);
  color: var(--text-h);
  text-decoration: none;
  backdrop-filter: blur(10px);
  box-shadow: var(--shadow);
  transition: all 0.2s ease;
}
.back-btn-absolute:hover {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-glow);
}
.back-btn-absolute:active { transform: scale(0.9); }

/* Header right-side extras */
.header-extras {
  display: flex; align-items: center; gap: 8px;
  flex-shrink: 0;
  margin-left: auto;
  z-index: 102;
}

/* Menu / icon buttons */
.menu-btn,
.icon-btn {
  width: 36px; height: 36px; border-radius: 50%;
  display: flex; align-items: center; justify-content: center;
  background: var(--surface);
  border: 1px solid var(--border);
  color: var(--text-p);
  cursor: pointer;
  backdrop-filter: blur(10px);
  box-shadow: var(--shadow);
  transition: all 0.2s ease;
  flex-shrink: 0; font-size: 0;
}
.menu-btn { position: static; transform: none; width: 36px; height: 36px; }
.menu-btn:hover,
.icon-btn:hover {
  color: var(--text-h);
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-glow);
}
.menu-btn:active,
.icon-btn:active { transform: scale(0.93); }

/* ── Click-outside overlay ──────────────────────── */
.click-outside {
  position: fixed; inset: 0; z-index: 101; display: none;
}

/* ── Popover Menu ───────────────────────────────── */
.popover-menu {
  position: fixed;
  top: 64px; right: 16px;          /* overridden by JS on desktop */
  background: var(--surface-s);
  border: 1px solid var(--border);
  border-radius: var(--r-xl);
  padding: 8px; width: 220px;
  display: none; flex-direction: column; gap: 2px;
  z-index: 2000; box-shadow: var(--shadow-lg);
  transform-origin: top right;
  animation: menuIn 0.2s cubic-bezier(0.16,1,0.3,1);
  backdrop-filter: blur(20px);
  -webkit-backdrop-filter: blur(20px);
}
@keyframes menuIn { from{opacity:0;transform:scale(0.9)} to{opacity:1;transform:scale(1)} }
.popover-menu.active { display: flex; }

.menu-item {
  display: flex; align-items: center; justify-content: space-between;
  padding: 11px 14px; border-radius: 12px; cursor: pointer;
  color: var(--text-h); font-size: 14px; font-weight: 600;
  background: transparent; border: none; width: 100%; text-align: left;
  transition: background 0.15s; font-family: var(--font); text-decoration: none;
}
.menu-item:hover { background: color-mix(in srgb,var(--text-h) 6%,transparent); }

/* Toggle switch */
.switch { position: relative; width: 38px; height: 22px; flex-shrink: 0; }
.switch input { opacity: 0; width: 0; height: 0; }
.slider {
  position: absolute; cursor: pointer; inset: 0;
  background: var(--text-muted); transition: .3s; border-radius: 34px;
}
.slider:before {
  position: absolute; content: ""; height: 16px; width: 16px;
  left: 3px; bottom: 3px; background: white; transition: .3s;
  border-radius: 50%; box-shadow: 0 2px 4px rgba(0,0,0,0.2);
}
input:checked + .slider { background: var(--accent); }
input:checked + .slider:before { transform: translateX(16px); }

/* ── Main Editor Layout ─────────────────────────── */
main {
  flex: 1;
  position: relative;
  padding: 10px 20px 0 20px;
  display: flex;
  justify-content: center;
  z-index: 1;
}

.editor-wrapper {
  width: 100%; max-width: 640px; height: 100%;
  background: var(--surface);
  backdrop-filter: blur(16px);
  -webkit-backdrop-filter: blur(16px);
  border-radius: 0;
  border: 1px solid var(--border);
  border-bottom: none;
  box-shadow: var(--shadow-lg);
  display: flex; flex-direction: column;
  position: relative; overflow: hidden;
  animation: fadeInUp 0.5s cubic-bezier(0.16,1,0.3,1) 0.1s backwards;
}
@keyframes fadeInUp {
  from{opacity:0;transform:translateY(20px)} to{opacity:1;transform:translateY(0)}
}

.status-bar {
  height: 36px; border-bottom: 1px solid var(--border);
  display: flex; align-items: center; justify-content: space-between;
  padding: 0 16px;
  font-size: 11px; font-weight: 600; letter-spacing: 0.3px;
  color: var(--text-muted);
  background: rgba(255,255,255,0.03);
}

.status-badge { display: inline-flex; align-items: center; gap: 6px; }

.status-dot {
  width: 6px; height: 6px; border-radius: 50%;
  background: #10b981;
  box-shadow: 0 0 6px rgba(16,185,129,0.6);
}

textarea#mainEditor {
  flex: 1; width: 100%;
  border: none; resize: none;
  padding: 20px; padding-bottom: 120px;
  background: transparent;
  color: var(--text-h);
  font-family: var(--font-mono);
  font-size: 14px; line-height: 1.7;
  outline: none;
}
textarea#mainEditor::placeholder { color: var(--text-muted); }

/* Custom scrollbar */
textarea::-webkit-scrollbar { width: 4px; }
textarea::-webkit-scrollbar-track { background: transparent; }
textarea::-webkit-scrollbar-thumb {
  background: var(--border); border-radius: 4px;
}
textarea::-webkit-scrollbar-thumb:hover { background: var(--text-muted); }

/* ── Action Pills ───────────────────────────────── */
.action-pills {
  position: absolute;
  bottom: 100px; right: 20px;
  display: flex; flex-direction: column;
  gap: 10px; align-items: flex-end; z-index: 50;
  animation: slideInRight 0.5s cubic-bezier(0.16,1,0.3,1) 0.3s backwards;
}
@keyframes slideInRight {
  from{opacity:0;transform:translateX(20px)} to{opacity:1;transform:translateX(0)}
}

.pill-btn {
  background: var(--surface);
  backdrop-filter: blur(12px); -webkit-backdrop-filter: blur(12px);
  padding: 9px 18px;
  border-radius: 99px;
  border: 1px solid var(--border);
  color: var(--text-p);
  font-size: 13px; font-weight: 600;
  cursor: pointer;
  box-shadow: var(--shadow);
  transition: all 0.2s ease;
  display: flex; align-items: center; gap: 6px;
  font-family: var(--font);
}
.pill-btn:hover {
  background: var(--surface-s);
  color: var(--accent);
  transform: translateX(-4px);
  border-color: color-mix(in srgb,var(--accent) 30%,transparent);
  box-shadow: var(--shadow-lg);
}
.pill-btn.danger:hover { color: #ef4444; border-color: rgba(239,68,68,0.25); }

.pill-btn.primary {
  background: var(--primary-gradient);
  color: white; border: none;
  box-shadow: 0 6px 18px var(--accent-glow);
}
.pill-btn.primary:hover {
  opacity: 0.9; color: white; transform: translateX(0) translateY(-1px);
  box-shadow: 0 8px 24px var(--accent-glow);
}
.full-width { width: 100%; justify-content: center; }

/* ── Bottom Control Bar ─────────────────────────── */
.bottom-bar {
  position: absolute; bottom: 24px; left: 50%;
  transform: translateX(-50%);
  width: calc(100% - 40px); max-width: 500px;
  display: flex; gap: 12px;
  z-index: 50; pointer-events: none;
  animation: slideUpCenter 0.5s cubic-bezier(0.16,1,0.3,1) 0.4s backwards;
}
@keyframes slideUpCenter {
  from{opacity:0;transform:translateX(-50%) translateY(40px)}
  to{opacity:1;transform:translateX(-50%) translateY(0)}
}

.control-btn {
  pointer-events: auto; border: none;
  border-radius: var(--r-lg); height: 56px;
  display: flex; align-items: center; justify-content: center;
  gap: 8px; font-weight: 700; font-family: var(--font);
  cursor: pointer;
  box-shadow: var(--shadow-lg);
  transition: transform 0.2s, box-shadow 0.2s, opacity 0.2s;
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
}
.control-btn:active { transform: scale(0.96); }

.control-btn.secondary {
  width: 56px; flex: 0 0 56px;
  background: var(--surface);
  color: var(--text-h);
  border: 1px solid var(--border);
}
.control-btn.secondary:hover {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-glow);
}

.control-btn.primary {
  flex: 1;
  background: var(--primary-gradient);
  color: white; font-size: 15px;
  box-shadow: 0 8px 24px var(--accent-glow);
}
.control-btn.primary:hover { opacity: 0.9; }

/* ── Profile / Info Modal ───────────────────────── */
.modal-overlay {
  position: fixed; inset: 0;
  background: rgba(0,0,0,0.55);
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
  z-index: 400;
  display: flex; align-items: center; justify-content: center;
  padding: 24px;
  opacity: 0; pointer-events: none;
  transition: opacity 0.3s;
}
.modal-overlay.show { opacity: 1; pointer-events: auto; }

.modal {
  width: 90%; max-width: 400px;
  background: var(--surface-s);
  border: 1px solid var(--border);
  border-radius: 28px;
  box-shadow: var(--shadow-lg);
  transform: translateY(24px) scale(0.96);
  transition: transform 0.35s cubic-bezier(0.34,1.56,0.64,1);
  overflow: hidden;
}
.modal-overlay.show .modal { transform: translateY(0) scale(1); }

.modal-header {
  padding: 20px 20px 16px;
  border-bottom: 1px solid var(--border);
  display: flex; justify-content: space-between; align-items: center;
}
.modal-header h2 {
  font-size: 18px; font-weight: 800; letter-spacing: -0.4px;
  color: var(--text-h); margin: 0;
}

.close-modal {
  width: 30px; height: 30px; border-radius: 50%;
  background: var(--input-bg); border: 1px solid var(--border);
  color: var(--text-p); font-size: 14px;
  cursor: pointer; display: flex; align-items: center; justify-content: center;
  transition: all 0.2s;
}
.close-modal:hover {
  background: color-mix(in srgb,var(--accent) 10%,transparent);
  border-color: var(--accent); color: var(--text-h);
}

.modal-body { padding: 20px; }
.modal-desc {
  font-size: 13px; color: var(--text-p);
  margin-bottom: 20px; line-height: 1.55;
}

.form-group { margin-bottom: 16px; }
.form-group label {
  display: block; font-size: 12px; font-weight: 600;
  letter-spacing: 0.3px; margin-bottom: 6px; color: var(--text-muted);
  text-transform: uppercase;
}
.form-group input {
  width: 100%; padding: 12px 14px;
  border-radius: var(--r-md);
  border: 1px solid var(--border);
  background: var(--input-bg);
  color: var(--text-h);
  font-size: 14px; font-weight: 500; font-family: var(--font);
  transition: all 0.2s ease;
}
.form-group input::placeholder { color: var(--text-muted); }
.form-group input:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-glow);
  background: var(--surface-s);
}
.form-group.row { display: flex; gap: 12px; }
.form-group .col { flex: 1; }

.modal-footer {
  padding: 16px 20px 20px;
  border-top: 1px solid var(--border);
}

/* Info modal: special card layout */
#infoModalApp .modal {
  text-align: center;
}
#infoModalApp .modal-header { justify-content: center; position: relative; }
#infoModalApp .close-modal {
  position: absolute; right: 20px; top: 50%; transform: translateY(-50%);
}

/* ── Toast ──────────────────────────────────────── */
#toast {
  position: fixed;
  top: 72px; left: 50%;
  transform: translateX(-50%) translateY(-20px);
  background: rgba(13,15,26,0.9);
  backdrop-filter: blur(12px);
  color: white;
  padding: 10px 24px;
  border-radius: 99px;
  font-size: 13px; font-weight: 600; font-family: var(--font);
  opacity: 0; pointer-events: none;
  transition: all 0.3s cubic-bezier(0.34,1.56,0.64,1);
  box-shadow: 0 10px 30px rgba(0,0,0,0.2);
  z-index: 600;
}
#toast.show {
  opacity: 1;
  transform: translateX(-50%) translateY(0);
}

/* ── Loading Overlay ────────────────────────────── */
.loading-overlay {
  position: fixed; inset: 0;
  background: rgba(0,0,0,0.4);
  backdrop-filter: blur(6px); -webkit-backdrop-filter: blur(6px);
  z-index: 700;
  display: flex; align-items: center; justify-content: center;
  flex-direction: column; gap: 16px;
  opacity: 0; pointer-events: none;
  transition: opacity 0.2s;
}
.loading-overlay.show { opacity: 1; pointer-events: auto; }

.spinner {
  width: 40px; height: 40px;
  border: 3px solid rgba(255,255,255,0.2);
  border-top-color: white;
  border-radius: 50%;
  animation: spin 0.8s ease-in-out infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

.loading-text {
  color: white; font-weight: 700; font-size: 14px;
  font-family: var(--font);
}

/* ── Header action alias (used in some older refs) */
.header-actions { display: flex; gap: 8px; }
.icon-btn.ghost { background: transparent; border: none; }
.icon-btn.ghost:hover { background: var(--surface); }

/* ── Mobile ─────────────────────────────────────── */
@media (max-width: 600px) {
  header { padding: 0 16px; }

  main { padding: 0; }

  .editor-wrapper {
    border-radius: 0; border: none;
  }

  .bottom-bar {
    width: calc(100% - 32px); bottom: 16px;
  }
}

/* ── Desktop ─────────────────────────────────────── */
@media (min-width: 800px) {
  main {
    padding: 0 24px;
  }

  textarea#mainEditor { padding: 20px 0 140px; }

  .popover-menu {
    position: fixed;
    top: 72px;
    right: 20px;
  }
}

/* ── Screen reader util ─────────────────────────── */
.sr-only {
  position:absolute; width:1px; height:1px; padding:0; margin:-1px;
  overflow:hidden; clip:rect(0,0,0,0); white-space:nowrap; border-width:0;
}