:root {
  --bg: #0d1117;
  --bg-elev: #161b22;
  --bg-elev-2: #1f242d;
  --bg-elev-3: #2a313c;
  --fg: #e6edf3;
  --fg-dim: #8b949e;
  --fg-faint: #586069;
  --accent: #7c3aed;
  --accent-2: #a78bfa;
  --accent-fg: #ffffff;
  --green: #3fb950;
  --amber: #d29922;
  --red: #f85149;
  --border: #30363d;
}

* { box-sizing: border-box; }
html, body {
  margin: 0; padding: 0;
  height: 100%;
  background: var(--bg);
  color: var(--fg);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  font-size: 14px;
  -webkit-font-smoothing: antialiased;
  overflow: hidden;
}
body {
  display: grid;
  grid-template-rows: auto auto 1fr;
  height: 100vh;
}
.hidden { display: none !important; }

button { font-family: inherit; font-size: inherit; cursor: pointer; }
input, textarea, select {
  font-family: inherit; font-size: inherit;
  background: var(--bg-elev-2); color: var(--fg);
  border: 1px solid var(--border); border-radius: 6px;
  padding: 8px 10px;
}
input:focus, textarea:focus, select:focus {
  outline: none; border-color: var(--accent);
}

/* Topbar */
.topbar {
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 8px 14px;
  background: var(--bg-elev);
  border-bottom: 1px solid var(--border);
}
.brand {
  display: flex; align-items: center; gap: 8px;
  text-decoration: none; color: var(--fg);
  font-weight: 700; font-size: 15px;
}
.brand-mark {
  font-family: ui-monospace, monospace;
  background: var(--accent); color: var(--accent-fg);
  padding: 2px 8px; border-radius: 6px;
  font-weight: 800; font-size: 12px;
}
.doc-title {
  flex: 1;
  display: flex; align-items: center; gap: 10px;
  max-width: 480px;
}
.doc-title input {
  flex: 1;
  background: transparent; border: 1px solid transparent;
  padding: 6px 10px;
  font-weight: 600;
}
.doc-title input:not(:disabled):hover { border-color: var(--border); }
.doc-title input:not(:disabled):focus { background: var(--bg-elev-2); border-color: var(--accent); }
.doc-status { font-size: 12px; color: var(--fg-dim); white-space: nowrap; }
.topbar-right { display: flex; gap: 8px; align-items: center; }

.btn {
  padding: 7px 14px; border-radius: 6px;
  border: 1px solid var(--border); background: var(--bg-elev-2); color: var(--fg);
  font-weight: 500;
  transition: border-color 0.12s, background 0.12s;
}
.btn:hover { border-color: var(--accent); }
.btn.primary { background: var(--accent); color: var(--accent-fg); border-color: var(--accent); }
.btn.primary:hover { filter: brightness(1.1); }
.btn.ghost { background: transparent; }
.btn.block { width: 100%; }
.btn:disabled { opacity: 0.5; cursor: not-allowed; }

.user-menu { position: relative; }
.popover {
  position: absolute; top: calc(100% + 4px); right: 0;
  background: var(--bg-elev-2); border: 1px solid var(--border);
  border-radius: 8px; padding: 4px;
  min-width: 160px; display: flex; flex-direction: column; z-index: 1000;
}
.popover a { padding: 8px 12px; color: var(--fg); text-decoration: none; border-radius: 4px; }
.popover a:hover { background: var(--bg-elev-3); }

/* Toolbar */
.toolbar {
  display: flex; align-items: center; gap: 4px;
  padding: 6px 12px;
  background: var(--bg-elev); border-bottom: 1px solid var(--border);
  overflow-x: auto; white-space: nowrap;
}
.tb {
  background: transparent; border: 1px solid transparent;
  padding: 6px 10px; border-radius: 6px;
  color: var(--fg); font-size: 13px;
}
.tb:hover { background: var(--bg-elev-2); border-color: var(--border); }
.tb:disabled { color: var(--fg-faint); cursor: not-allowed; }
.tb:disabled:hover { background: transparent; border-color: transparent; }
.tb.gated { color: var(--fg-dim); position: relative; }
.tb.gated::after {
  content: ""; position: absolute; top: 4px; right: 4px;
  width: 6px; height: 6px; border-radius: 50%; background: var(--amber);
}
.tb.gated.unlocked { color: var(--fg); }
.tb.gated.unlocked::after { display: none; }
.tb-sep { width: 1px; height: 18px; background: var(--border); margin: 0 6px; }
.tb-grow { flex: 1; }

#main { position: relative; min-height: 0; overflow: hidden; }

/* Dropzone */
.dropzone {
  height: 100%;
  display: flex; align-items: center; justify-content: center;
  padding: 32px;
}
.dropzone-inner {
  max-width: 480px; width: 100%;
  text-align: center;
  padding: 48px 32px;
  border: 2px dashed var(--border);
  border-radius: 16px;
  background: var(--bg-elev);
  transition: border-color 0.15s, background 0.15s;
}
.dropzone-inner.drag-active {
  border-color: var(--accent);
  background: var(--bg-elev-2);
}
.dropzone-icon { font-size: 56px; margin-bottom: 16px; }
.dropzone h1 { margin: 0 0 8px; font-size: 22px; }
.dropzone p { margin: 0 0 16px; color: var(--fg-dim); }
.dropzone-meta { font-size: 12px; color: var(--fg-faint); margin-top: 18px !important; }

/* Workspace */
.workspace {
  display: grid;
  grid-template-columns: 1fr 4px 380px;
  height: 100%;
}
.pane {
  display: flex; flex-direction: column; min-width: 0; overflow: hidden;
  background: var(--bg);
}
.pane-header {
  padding: 8px 12px;
  display: flex; justify-content: space-between; align-items: center;
  background: var(--bg-elev); border-bottom: 1px solid var(--border);
  font-size: 12px; color: var(--fg-dim);
  text-transform: uppercase; letter-spacing: 0.04em;
}
.splitter { background: var(--border); cursor: col-resize; transition: background 0.12s; }
.splitter:hover { background: var(--accent); }

/* Preview */
.preview-root {
  flex: 1; overflow: auto;
  padding: 16px;
  display: flex; flex-direction: column; gap: 16px;
  align-items: center;
  background: #1a1d23;
}
.pdf-page {
  box-shadow: 0 4px 16px rgba(0,0,0,0.5);
  border-radius: 4px;
  background: #fff;
  max-width: 100%;
  position: relative;
}
.pdf-page img { display: block; max-width: 100%; height: auto; border-radius: 4px; }
.pdf-page-label {
  position: absolute; top: 6px; right: 8px;
  background: rgba(0,0,0,0.6); color: #fff;
  font-size: 11px; padding: 2px 8px; border-radius: 4px;
  font-family: ui-monospace, monospace;
}
.field-marker {
  position: absolute;
  border: 2px solid var(--accent);
  border-radius: 2px;
  background: rgba(124, 58, 237, 0.12);
  pointer-events: none;
  transition: opacity 0.15s;
  opacity: 0;
}
.field-marker.active { opacity: 1; }
.field-marker.filled { border-color: var(--green); background: rgba(63, 185, 80, 0.12); }

/* Form panel */
.form-root {
  flex: 1; overflow: auto;
  padding: 12px 14px;
}
.form-page-header {
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--fg-dim);
  padding: 8px 0 6px;
  position: sticky; top: 0;
  background: var(--bg);
  z-index: 1;
  border-bottom: 1px solid var(--border);
  margin-bottom: 8px;
}
.form-page-header:first-child { margin-top: 0; }
.field-row {
  display: flex; flex-direction: column; gap: 4px;
  padding: 8px 4px;
  border-bottom: 1px solid rgba(255,255,255,0.04);
}
.field-row:hover { background: var(--bg-elev); }
.field-row.has-suggestion { background: rgba(124, 58, 237, 0.06); }
.field-label {
  display: flex; align-items: center; gap: 6px;
  font-size: 12px;
  color: var(--fg-dim);
}
.field-label .field-name {
  font-family: ui-monospace, monospace;
  font-size: 11px;
  color: var(--fg-faint);
  flex: 1;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.field-label .field-type {
  font-size: 10px;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--fg-faint);
  background: var(--bg-elev-2);
  padding: 1px 6px;
  border-radius: 3px;
}
.field-required { color: var(--red); }
.field-input {
  width: 100%;
  padding: 6px 10px;
}
.field-input.checkbox-input {
  width: auto;
  margin: 0;
}
.checkbox-row {
  display: flex; align-items: center; gap: 8px;
  padding: 4px 0;
}
.suggestion-tag {
  display: inline-flex; align-items: center; gap: 6px;
  font-size: 11px;
  color: var(--accent-2);
  margin-top: 2px;
  cursor: pointer;
}
.suggestion-tag .accept { color: var(--green); }
.suggestion-tag .reject { color: var(--fg-dim); }

/* Library panel */
.library-panel {
  position: fixed;
  top: 0; right: 0; bottom: 0;
  width: 380px;
  background: var(--bg-elev);
  border-left: 1px solid var(--border);
  z-index: 1500;
  display: flex; flex-direction: column;
  box-shadow: -8px 0 24px rgba(0,0,0,0.4);
}
.library-tabs {
  display: flex; border-bottom: 1px solid var(--border);
}
.library-tab {
  flex: 1; padding: 12px;
  background: transparent; border: none;
  color: var(--fg-dim); font-size: 13px;
  border-bottom: 2px solid transparent;
}
.library-tab.active { color: var(--fg); border-bottom-color: var(--accent); }
.library-tab-pane { flex: 1; overflow: auto; padding: 14px; display: none; }
.library-tab-pane.active { display: block; }
.library-empty {
  padding: 16px;
  background: var(--bg-elev-2);
  border: 1px solid var(--border);
  border-radius: 8px;
  font-size: 13px;
}
.library-list { display: flex; flex-direction: column; gap: 6px; }
.library-item {
  padding: 10px 12px;
  background: var(--bg-elev-2);
  border: 1px solid var(--border);
  border-radius: 8px;
  cursor: pointer;
  display: flex; justify-content: space-between; align-items: center; gap: 8px;
}
.library-item:hover { border-color: var(--accent); }
.library-item-title { font-weight: 600; flex: 1; }
.library-item-meta { font-size: 11px; color: var(--fg-dim); }
.library-item-actions { display: flex; gap: 4px; }
.library-item-actions button {
  background: transparent; border: none;
  color: var(--fg-dim); padding: 2px 6px; border-radius: 4px;
}
.library-item-actions button:hover { color: var(--red); background: var(--bg); }
.library-close {
  position: absolute; top: 8px; right: 8px;
  background: transparent; border: none;
  color: var(--fg-dim); font-size: 22px;
  width: 32px; height: 32px; border-radius: 6px;
}
.library-close:hover { background: var(--bg-elev-3); color: var(--fg); }

/* Modal */
.modal {
  position: fixed; inset: 0;
  background: rgba(0, 0, 0, 0.6);
  display: flex; align-items: center; justify-content: center;
  z-index: 5000;
}
.modal-card {
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 24px;
  width: 100%; max-width: 420px;
  position: relative;
  max-height: 90vh; overflow: auto;
}
.modal-card--wide { max-width: 560px; }
.modal-card h2 { margin: 0 0 8px; font-size: 18px; }
.modal-card p { margin: 0 0 12px; color: var(--fg-dim); }
.modal-card input { width: 100%; margin-bottom: 12px; }
.modal-close {
  position: absolute; top: 8px; right: 8px;
  background: transparent; border: none;
  color: var(--fg-dim); font-size: 22px;
  width: 32px; height: 32px; border-radius: 6px;
}
.modal-close:hover { background: var(--bg-elev-2); color: var(--fg); }
.modal-actions { display: flex; gap: 8px; margin-top: 12px; }
.modal-actions .btn { flex: 1; }

.profile-fields {
  display: flex; flex-direction: column; gap: 6px;
  max-height: 50vh; overflow: auto;
  padding: 6px 0;
}
.profile-field-row {
  display: grid; grid-template-columns: 140px 1fr auto; gap: 6px;
}
.profile-field-row input { width: 100%; margin: 0; }
.profile-field-row .remove { color: var(--fg-dim); cursor: pointer; padding: 0 6px; background: transparent; border: none; }
.profile-field-row .remove:hover { color: var(--red); }

/* Toast */
.toast {
  position: fixed;
  bottom: 24px; left: 50%; transform: translateX(-50%);
  background: var(--bg-elev-3);
  color: var(--fg);
  padding: 10px 16px;
  border-radius: 8px;
  border: 1px solid var(--border);
  z-index: 6000;
  font-size: 13px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.4);
}

.muted { color: var(--fg-dim); }
.small { font-size: 12px; }

@media (max-width: 900px) {
  .workspace { grid-template-columns: 1fr; grid-template-rows: 1fr 1fr; }
  .splitter { display: none; }
  .pane-preview { border-bottom: 1px solid var(--border); }
  .library-panel { width: 90vw; }
  .profile-field-row { grid-template-columns: 1fr; }
}
