:root {
  --primary: #2563eb;
  --primary-dark: #1d4ed8;
  --success: #16a34a;
  --danger: #dc2626;
  --bg: #f8fafc;
  --text: #0f172a;
  --text-muted: #64748b;
  --border: #e2e8f0;
  --white: #ffffff;
  --shadow: 0 1px 3px 0 rgb(0 0 0 / 0.1);
  --radius: 0.75rem;
}

[data-theme="dark"] {
  --bg: #0f172a;
  --text: #f1f5f9;
  --text-muted: #94a3b8;
  --border: #334155;
  --white: #1e293b;
  --shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.3);
}

html { scroll-behavior: smooth; }
* { box-sizing: border-box; margin: 0; padding: 0; }
body { font-family: -apple-system, system-ui, sans-serif; background: var(--bg); color: var(--text); line-height: 1.5; transition: background 0.3s, color 0.3s; }
.container { max-width: 1200px; margin: 0 auto; padding: 0 1.5rem; }

/* Utilities */
.hidden { display: none !important; }
.flex { display: flex; }
.gap-2 { gap: 0.5rem; }
.mt-2 { margin-top: 0.5rem; }
.mt-4 { margin-top: 1rem; }
.w-full { width: 100%; }
.text-xs { font-size: 0.75rem; }
.text-muted { color: var(--text-muted); }
.opacity-50 { opacity: 0.5; }

/* Header */
.main-header { background: var(--white); border-bottom: 1px solid var(--border); height: 64px; position: sticky; top: 0; z-index: 100; box-shadow: var(--shadow); }
.nav-wrapper { display: flex; justify-content: space-between; align-items: center; height: 100%; }
.logo { font-size: 1.5rem; font-weight: 800; color: var(--text); text-decoration: none; }
.logo span { color: var(--primary); }
.nav-links { display: flex; list-style: none; gap: 1.5rem; align-items: center; }
.nav-links a { text-decoration: none; color: var(--text-muted); font-size: 0.875rem; font-weight: 500; transition: color 0.2s; }
.nav-links a.active, .nav-links a:hover { color: var(--primary); }
.header-actions { display: flex; align-items: center; gap: 1.5rem; }
.theme-toggle { background: none; border: none; cursor: pointer; font-size: 1.25rem; color: var(--text-muted); padding: 0.5rem; border-radius: 50%; transition: background 0.2s; }
.theme-toggle:hover { background: var(--bg); color: var(--text); }
.menu-toggle { display: none; background: none; border: none; cursor: pointer; flex-direction: column; gap: 5px; padding: 5px; }
.menu-toggle span { display: block; width: 24px; height: 2px; background: var(--text); }

/* Mobile Nav */
.mobile-nav {
  display: none;
  position: fixed;
  top: 64px;
  left: 0;
  width: 100%;
  background: var(--white);
  border-bottom: 1px solid var(--border);
  padding: 1rem;
  box-shadow: var(--shadow);
  z-index: 99;
  flex-direction: column;
  gap: 1rem;
}
.mobile-nav.open { display: flex; }
.mobile-nav a { text-decoration: none; color: var(--text); font-weight: 500; padding: 0.5rem; border-radius: 0.5rem; }
.mobile-nav a:hover { background: var(--bg); }

/* Hero */
.hero-section { padding: 3rem 0; text-align: center; background: var(--white); border-bottom: 1px solid var(--border); }
.badge { font-size: 0.75rem; background: var(--primary); color: white; padding: 0.25rem 0.5rem; border-radius: 1rem; vertical-align: middle; margin-left: 0.5rem; }
.hero-subtitle { color: var(--text-muted); max-width: 700px; margin: 1rem auto 0; font-size: 1.1rem; }

/* Toolbar */
.sticky-toolbar { background: var(--white); border: 1px solid var(--border); border-radius: var(--radius); padding: 1.25rem; margin-bottom: 1.5rem; position: sticky; top: 80px; z-index: 90; box-shadow: var(--shadow); }
.toolbar-grid { display: grid; grid-template-columns: 2fr auto 1.5fr auto; gap: 1.5rem; align-items: flex-end; }
.label-row { display: flex; justify-content: space-between; align-items: center; margin-bottom: 0.5rem; flex-wrap: wrap; gap: 0.5rem; }
.label-row label { font-size: 0.75rem; font-weight: 700; text-transform: uppercase; color: var(--text-muted); letter-spacing: 0.05em; }
.small-select { font-size: 0.8rem; padding: 0.25rem 0.5rem; border-radius: 0.4rem; border: 1px solid var(--border); background: var(--bg); color: var(--text); outline: none; cursor: pointer; }

.input-with-counter { position: relative; }
input[type="text"] { width: 100%; padding: 0.6rem 0.8rem; border: 1px solid var(--border); border-radius: 0.5rem; background: var(--bg); color: var(--text); font-size: 0.95rem; outline: none; transition: border-color 0.2s; }
input[type="text"]:focus { border-color: var(--primary); box-shadow: 0 0 0 2px rgba(37, 99, 235, 0.1); }

.match-indicator { position: absolute; right: 0.75rem; top: 50%; transform: translateY(-50%); font-size: 0.75rem; font-weight: 700; color: var(--primary); background: var(--white); padding: 0.1rem 0.4rem; border-radius: 0.25rem; border: 1px solid var(--border); pointer-events: none; }
.search-options { display: flex; flex-wrap: wrap; gap: 1rem; margin-top: 0.75rem; font-size: 0.8rem; color: var(--text-muted); }
.search-options label { display: flex; align-items: center; cursor: pointer; user-select: none; }
.search-options input { margin-right: 0.4rem; accent-color: var(--primary); }
.error-msg { color: var(--danger); font-weight: 700; margin-left: auto; font-size: 0.8rem; }

/* Buttons */
.btn { padding: 0.6rem 1rem; border-radius: 0.5rem; font-weight: 600; font-size: 0.875rem; border: 1px solid transparent; cursor: pointer; transition: all 0.2s; white-space: nowrap; }
.btn-primary { background: var(--primary); color: white; }
.btn-primary:hover { background: var(--primary-dark); }
.btn-outline { border: 1px solid var(--primary); color: var(--primary); background: transparent; }
.btn-outline:hover { background: rgba(37, 99, 235, 0.05); }
.btn-danger { color: var(--danger); border-color: #fee2e2; background: #fef2f2; }
.btn-danger:hover { background: #fee2e2; }
.btn-success { background: var(--success); color: white; }
.btn-success:hover { filter: brightness(0.95); }
.btn-icon { background: var(--bg); color: var(--text); border: 1px solid var(--border); display: flex; align-items: center; justify-content: center; width: 40px; height: 40px; font-size: 1.25rem; }
.btn-icon:hover { background: var(--white); border-color: var(--primary); color: var(--primary); }
.btn-sm { padding: 0.4rem 0.75rem; font-size: 0.75rem; }

/* Editor Layout */
.editor-layout { display: grid; grid-template-columns: 1fr 300px; gap: 1.5rem; }
#text-editor-container { position: relative; height: 600px; border: 1px solid var(--border); border-radius: var(--radius); background: var(--white); overflow: hidden; box-shadow: inset 0 2px 4px rgba(0,0,0,0.02); }

/* Backdrop & Input - Must have identical metrics */
.backdrop, #main-input { 
  position: absolute; 
  top: 0; 
  left: 0; 
  width: 100%; 
  height: 100%; 
  padding: 1.5rem; 
  padding-right: 2rem; /* Space for heatmap */ 
  font-family: 'JetBrains Mono', 'Fira Code', 'Courier New', monospace; 
  font-size: 14px; 
  line-height: 1.6; 
  letter-spacing: normal;
  white-space: pre-wrap; 
  word-wrap: break-word; 
  overflow-y: auto;
}

#main-input { 
  background: transparent; 
  color: inherit; 
  border: none; 
  outline: none; 
  resize: none; 
  z-index: 2; 
  caret-color: var(--primary); 
}

.backdrop { 
  z-index: 1; 
  color: transparent; /* Hide text, show only marks */
  pointer-events: none; 
}

/* Scrollbar styling for both to match */
#main-input::-webkit-scrollbar, .backdrop::-webkit-scrollbar { width: 10px; }
#main-input::-webkit-scrollbar-track, .backdrop::-webkit-scrollbar-track { background: transparent; }
#main-input::-webkit-scrollbar-thumb, .backdrop::-webkit-scrollbar-thumb { background: #cbd5e1; border-radius: 5px; }
[data-theme="dark"] #main-input::-webkit-scrollbar-thumb { background: #475569; }

.hl-match { background-color: #fde047; border-radius: 2px; color: transparent; }
.hl-active { background-color: #fb923c; box-shadow: 0 0 0 2px #fb923c; }

[data-theme="dark"] .hl-match { background-color: #854d0e; }
[data-theme="dark"] .hl-active { background-color: #ea580c; box-shadow: 0 0 0 2px #ea580c; }

/* Heatmap */
.heatmap-bar { 
  position: absolute; 
  right: 0; 
  top: 0; 
  bottom: 0;
  width: 12px; 
  background: rgba(0,0,0,0.02); 
  z-index: 20; /* Above editor */
  cursor: pointer; 
  border-left: 1px solid var(--border);
  transition: width 0.2s, background 0.2s;
}
.heatmap-bar:hover {
  width: 16px;
  background: rgba(0,0,0,0.05);
}
.heatmap-node { 
  position: absolute; 
  left: 0; 
  width: 100%; 
  height: 3px; 
  background-color: rgba(251, 146, 60, 0.6); 
  transition: transform 0.1s; 
  pointer-events: none; 
}
[data-theme="dark"] .heatmap-bar { background: rgba(255,255,255,0.05); }
[data-theme="dark"] .heatmap-node { background-color: rgba(251, 146, 60, 0.8); }

/* Stats Bar */
.editor-footer { border-top: 1px solid var(--border); background: var(--white); border-radius: 0 0 var(--radius) var(--radius); padding: 0.75rem 1rem; }
.stats-bar { display: flex; flex-wrap: wrap; gap: 1.5rem; font-size: 0.85rem; color: var(--text-muted); }
.stat strong { color: var(--text); }

/* Context Panel */
.context-panel { background: var(--white); border: 1px solid var(--border); border-radius: var(--radius); margin-top: 1rem; padding: 1.25rem; box-shadow: var(--shadow); }
.panel-header { display: flex; justify-content: space-between; align-items: center; margin-bottom: 1rem; border-bottom: 1px solid var(--border); padding-bottom: 0.5rem; }
.context-list { max-height: 300px; overflow-y: auto; display: flex; flex-direction: column; gap: 0.75rem; }
.context-item { background: var(--bg); border: 1px solid var(--border); padding: 0.75rem; border-radius: 0.5rem; font-family: monospace; font-size: 0.85rem; cursor: pointer; transition: transform 0.1s; white-space: pre-wrap; }
.context-item:hover { transform: translateX(4px); border-color: var(--primary); }
.context-item mark { background: #fde047; color: #000; padding: 0 2px; border-radius: 2px; }
.empty-msg { text-align: center; color: var(--text-muted); padding: 1rem; }

/* Sidebar Tabs */
.sidebar-tabs { display: flex; border-bottom: 1px solid var(--border); margin-bottom: 1.25rem; gap: 0.25rem; }
.tab-btn { flex: 1; padding: 0.6rem 0.25rem; font-size: 0.8rem; font-weight: 700; color: var(--text-muted); background: none; border: none; cursor: pointer; border-bottom: 2px solid transparent; transition: all 0.2s; }
.tab-btn.active { color: var(--primary); border-bottom-color: var(--primary); }
.tab-content { display: none; }
.tab-content.active { display: block; animation: fadeIn 0.3s ease; }

@keyframes fadeIn { from { opacity: 0; } to { opacity: 1; } }

/* Sidebar Tools */
.tool-card { background: var(--white); border: 1px solid var(--border); border-radius: var(--radius); padding: 1.25rem; margin-bottom: 1rem; }
.group-title { font-size: 0.75rem; text-transform: uppercase; color: var(--text-muted); margin-bottom: 1rem; font-weight: 800; letter-spacing: 0.05em; }
.vertical-btn-group { display: flex; flex-direction: column; gap: 0.5rem; }
.btn-tool { padding: 0.7rem; text-align: left; background: var(--bg); border: 1px solid var(--border); border-radius: 0.5rem; font-size: 0.875rem; color: var(--text); cursor: pointer; font-weight: 500; transition: all 0.2s; }
.btn-tool:hover { border-color: var(--primary); background: var(--white); color: var(--primary); transform: translateX(2px); }
.variant-blue { color: var(--primary); }
.variant-red { color: var(--danger); }

/* Chips */
.preset-chips, .builder-controls { display: flex; flex-wrap: wrap; gap: 0.5rem; }
.chip { font-size: 0.75rem; padding: 0.35rem 0.6rem; background: var(--bg); border: 1px solid var(--border); border-radius: 0.4rem; color: var(--text); cursor: pointer; transition: 0.2s; font-weight: 600; }
.chip:hover { background: var(--primary); color: white; border-color: var(--primary); }

/* Prose */
.content-section { padding: 4rem 0; background: var(--white); }
.prose-grid { display: grid; grid-template-columns: 2fr 1fr; gap: 4rem; }
.prose h2 { font-size: 2rem; margin-bottom: 1.5rem; color: var(--text); font-weight: 800; }
.prose h3 { font-size: 1.4rem; margin: 2rem 0 1rem; color: var(--text); font-weight: 700; }
.prose p { margin-bottom: 1.25rem; color: var(--text-muted); }
.pro-tip { background: rgba(37, 99, 235, 0.05); border: 1px solid rgba(37, 99, 235, 0.15); padding: 1.25rem; border-radius: var(--radius); }
.pro-tip h4 { color: var(--primary); margin-bottom: 0.5rem; }
.pro-tip p { color: var(--text-muted); margin-bottom: 0; }
.cheat-list { list-style: none; padding: 0; }
.cheat-list li { padding: 0.5rem 0; border-bottom: 1px solid var(--border); font-size: 0.85rem; font-family: monospace; color: var(--text-muted); }
.cheat-list li code { background: var(--bg); padding: 0.2rem 0.4rem; border-radius: 4px; color: var(--primary); font-weight: 800; }

/* Responsive */
@media (max-width: 992px) {
  .toolbar-grid { grid-template-columns: 1fr; gap: 1rem; }
  .editor-layout { grid-template-columns: 1fr; }
  .prose-grid { grid-template-columns: 1fr; }
}

@media (max-width: 768px) {
  .nav-links { display: none; }
  .menu-toggle { display: flex; }
  .hero-section h1 { font-size: 2rem; }
  .action-row { display: grid; grid-template-columns: 1fr 1fr; gap: 0.5rem; }
}