:root {
  color-scheme: light;
  --bg: #f7f8fa;
  --surface: #ffffff;
  --border: #d7dbe0;
  --text: #1b1f24;
  --text-muted: #5b6472;
  --primary: #1554d6;
  --primary-dark: #0d3ea3;
  --danger: #b3261e;
  --danger-dark: #8c1d17;
  --success: #1a7f37;
  --focus: #1554d6;
  --radius: 8px;
  font-size: 16px;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  line-height: 1.5;
}

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

.skip-link {
  position: absolute;
  left: -999px;
  top: 0;
  background: var(--primary);
  color: #fff;
  padding: 0.6rem 1rem;
  z-index: 1000;
  border-radius: 0 0 var(--radius) 0;
}
.skip-link:focus {
  left: 0;
}

:focus-visible {
  outline: 3px solid var(--focus);
  outline-offset: 2px;
}

button, input, select, textarea {
  font: inherit;
  color: inherit;
}

button {
  cursor: pointer;
  border: 1px solid var(--border);
  background: var(--surface);
  border-radius: var(--radius);
  padding: 0.5rem 0.9rem;
}
button:disabled { opacity: 0.5; cursor: not-allowed; }
button.primary {
  background: var(--primary);
  border-color: var(--primary);
  color: #fff;
  font-weight: 600;
}
button.primary:hover:not(:disabled) { background: var(--primary-dark); }
button.subtle { background: transparent; }
button.danger {
  background: var(--danger);
  border-color: var(--danger);
  color: #fff;
}
button.danger:hover:not(:disabled) { background: var(--danger-dark); }
button.icon-btn { padding: 0.5rem 0.65rem; line-height: 1; }

input[type=text], input[type=search], input[type=password], select {
  width: 100%;
  padding: 0.5rem 0.6rem;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
}

label { display: block; font-weight: 600; font-size: 0.9rem; margin: 0.6rem 0 0.25rem; }
.muted { color: var(--text-muted); font-size: 0.88rem; }

/* Top bar */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0.7rem 1.2rem;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 20;
}
.brand { display: flex; align-items: center; gap: 0.6rem; font-weight: 700; font-size: 1.05rem; }
.mark {
  display: inline-grid;
  place-items: center;
  width: 2rem; height: 2rem;
  background: var(--primary);
  color: #fff;
  border-radius: 6px;
  font-size: 0.8rem;
  font-weight: 800;
}
.topbar-actions { display: flex; align-items: center; gap: 0.8rem; }

/* Layout */
.layout {
  display: grid;
  grid-template-columns: 320px 1fr;
  min-height: calc(100vh - 3.4rem);
}
.sidebar {
  border-right: 1px solid var(--border);
  background: var(--surface);
  padding: 1rem;
  display: flex;
  flex-direction: column;
  gap: 1rem;
  max-height: calc(100vh - 3.4rem);
  overflow-y: auto;
  position: sticky;
  top: 3.4rem;
}
.sidebar-section { display: flex; flex-direction: column; }
.sidebar-grow { flex: 1; min-height: 0; }
.sidebar-title { font-size: 0.85rem; text-transform: uppercase; letter-spacing: 0.03em; color: var(--text-muted); margin: 0 0 0.4rem; }
.sidebar-title-row { display: flex; align-items: center; justify-content: space-between; gap: 0.5rem; }
.sort-controls { display: flex; align-items: center; gap: 0.3rem; }
.sort-controls select { width: auto; padding: 0.3rem 0.4rem; }
.sidebar-new-btn { width: 100%; }

.category-list, .document-list { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 0.25rem; }
.category-list button, .document-list button {
  width: 100%;
  text-align: left;
  border: 1px solid transparent;
  background: transparent;
  border-radius: var(--radius);
  padding: 0.45rem 0.6rem;
}
.category-list button:hover, .document-list button:hover { background: var(--bg); }
.category-list button[aria-current="true"], .document-list button[aria-current="true"] {
  background: #e8effc;
  border-color: var(--primary);
  font-weight: 600;
}
.document-list button {
  display: flex;
  flex-direction: column;
  gap: 0.1rem;
}
.document-list .doc-name { font-weight: 600; overflow-wrap: anywhere; }
.document-list .doc-meta { font-size: 0.78rem; color: var(--text-muted); }
.document-list .doc-snippet { font-size: 0.78rem; color: var(--text-muted); font-style: italic; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* Main content */
.content { padding: 1.2rem 1.6rem; min-width: 0; }
.status-bar {
  padding: 0.6rem 0.9rem;
  border-radius: var(--radius);
  background: var(--surface);
  border: 1px solid var(--border);
  margin-bottom: 1rem;
}
.status-bar.error { border-color: var(--danger); color: var(--danger-dark); background: #fdecea; }
.status-bar.success { border-color: var(--success); color: #14532d; background: #eaf7ee; }

.editor-section { display: flex; flex-direction: column; gap: 0.8rem; height: calc(100vh - 3.4rem - 2.4rem - 3rem); }
.editor-header { display: flex; align-items: center; justify-content: space-between; gap: 1rem; flex-wrap: wrap; }
.breadcrumbs ol { list-style: none; display: flex; gap: 0.4rem; margin: 0; padding: 0; font-size: 0.95rem; }
.breadcrumbs li + li::before { content: "/"; margin-right: 0.4rem; color: var(--text-muted); }
.breadcrumbs li:last-child { font-weight: 700; }

.badge { padding: 0.2rem 0.6rem; border-radius: 999px; font-size: 0.78rem; font-weight: 600; border: 1px solid transparent; display: inline-flex; align-items: center; gap: 0.3rem; }
.badge.unsaved { background: #fff4e5; color: #92400e; border-color: #f0c36d; }
.badge.reindex-success { background: #e6f4ea; color: #137333; border-color: #a8dab5; }
.badge.reindex-warning { background: #fef7e0; color: #b06000; border-color: #fdd663; }
.badge.reindex-error { background: #fce8e6; color: #c5221f; border-color: #f5a6a0; }
.badge.reindex-disabled { background: #f1f3f4; color: #5f6368; border-color: #dadce0; }

.toolbar { display: flex; align-items: center; gap: 0.5rem; flex-wrap: wrap; }
.toolbar-sep { width: 1px; align-self: stretch; background: var(--border); margin: 0 0.2rem; }
.tabs { display: flex; gap: 0.25rem; background: var(--bg); padding: 0.2rem; border-radius: var(--radius); }
.tab { border: none; background: transparent; padding: 0.4rem 0.8rem; border-radius: 6px; }
.tab.selected { background: var(--surface); box-shadow: 0 1px 2px rgba(0,0,0,0.12); font-weight: 600; }

.editor-body { flex: 1; min-height: 0; display: flex; }
#content {
  flex: 1;
  width: 100%;
  resize: none;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 0.9rem;
  font-family: "Cascadia Code", "Consolas", "SFMono-Regular", Menlo, monospace;
  font-size: 0.95rem;
  line-height: 1.6;
  background: var(--surface);
}
.preview {
  flex: 1;
  width: 100%;
  overflow-y: auto;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1rem 1.4rem;
  background: var(--surface);
}
.preview :is(h1,h2,h3,h4,h5,h6) { margin-top: 1.2em; }
.preview img { max-width: 100%; }

.editor-footer { display: flex; justify-content: space-between; font-size: 0.85rem; }

/* Fullscreen editor */
.editor-section.fullscreen {
  position: fixed;
  inset: 0;
  background: var(--bg);
  z-index: 100;
  padding: 1rem 1.4rem;
  height: 100vh;
}

/* Estado oculto absoluto para qualquer elemento com o atributo hidden */
[hidden],
.modal-backdrop[hidden],
.editor-section[hidden],
.preview[hidden],
.toast[hidden] {
  display: none !important;
}

/* Modals */
.modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(15, 18, 22, 0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 1rem;
  z-index: 200;
}
.modal-backdrop[hidden] {
  display: none !important;
}
.modal {
  background: var(--surface);
  border-radius: 12px;
  padding: 1.4rem;
  width: 100%;
  max-width: 420px;
  max-height: 85vh;
  overflow-y: auto;
  box-shadow: 0 10px 40px rgba(0,0,0,0.25);
}
.modal-wide { max-width: 560px; }
.modal-large { max-width: 780px; width: 92vw; }
.modal h2 { margin-top: 0; }
.modal-actions { display: flex; justify-content: flex-end; gap: 0.6rem; margin-top: 1.2rem; }
.form-error { color: var(--danger-dark); min-height: 1.2em; font-size: 0.88rem; }

/* Markdown Assisted Toolbar */
.md-toolbar {
  display: flex;
  align-items: center;
  gap: 0.35rem;
  flex-wrap: wrap;
  background: var(--bg);
  padding: 0.35rem 0.6rem;
  border: 1px solid var(--border);
  border-radius: var(--radius);
}
.toolbar-group { display: flex; align-items: center; gap: 0.2rem; }
.fmt-btn {
  background: var(--surface);
  border: 1px solid var(--border);
  color: var(--text);
  padding: 0.3rem 0.55rem;
  border-radius: 6px;
  font-size: 0.82rem;
  font-weight: 600;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 1.8rem;
  height: 1.8rem;
}
.fmt-btn:hover { background: var(--bg); border-color: var(--primary); color: var(--primary); }
.fmt-btn:focus-visible { outline: 2px solid var(--primary); }

/* Split View (Lado a lado) */
.editor-body.split-view {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0.8rem;
}
.editor-body.split-view #content,
.editor-body.split-view #preview {
  display: block !important;
  height: 100%;
}

/* Modal de criação e Formulários Guiados */
.icon-text-btn { font-size: 0.8rem; padding: 0.2rem 0.5rem; }
.input-with-action { display: flex; gap: 0.4rem; align-items: center; }
.input-with-action select, .input-with-action input { flex: 1; }

.mode-selector-bar {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  margin: 0.8rem 0;
  padding: 0.4rem;
  background: var(--bg);
  border-radius: var(--radius);
}
.mode-label { font-size: 0.85rem; font-weight: 600; color: var(--text-muted); margin-right: 0.4rem; }
.mode-btn {
  border: 1px solid transparent;
  background: transparent;
  padding: 0.4rem 0.8rem;
  border-radius: 6px;
  font-size: 0.88rem;
  color: var(--text);
}
.mode-btn.active {
  background: var(--surface);
  border-color: var(--border);
  box-shadow: 0 1px 3px rgba(0,0,0,0.1);
  font-weight: 600;
  color: var(--primary-dark);
}

.template-desc {
  font-size: 0.88rem;
  padding: 0.5rem 0.7rem;
  background: var(--bg);
  border-left: 3px solid var(--primary);
  border-radius: 4px;
  margin-bottom: 1rem;
}

.guided-form-container {
  display: flex;
  flex-direction: column;
  gap: 0.9rem;
  max-height: 52vh;
  overflow-y: auto;
  padding-right: 0.4rem;
  margin-bottom: 1rem;
}
.form-row { display: flex; gap: 0.8rem; }
.form-row.split-2 > * { flex: 1; }
.form-group { display: flex; flex-direction: column; gap: 0.3rem; }
.field-help { font-size: 0.78rem; margin-top: 0.2rem; }

/* Grupos Repetíveis */
.repeatable-group {
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 0.7rem 0.9rem;
  background: var(--bg);
}
.repeatable-group legend {
  font-weight: 600;
  font-size: 0.88rem;
  padding: 0 0.4rem;
}
.repeatable-list { display: flex; flex-direction: column; gap: 0.5rem; margin-top: 0.5rem; }
.repeatable-item { display: flex; gap: 0.4rem; align-items: center; }
.repeatable-item input, .repeatable-item textarea { flex: 1; }
.repeatable-faq-item {
  display: flex;
  flex-direction: column;
  gap: 0.4rem;
  border-left: 2px solid var(--primary);
  padding-left: 0.6rem;
  margin-bottom: 0.6rem;
}
.add-repeatable-btn {
  align-self: flex-start;
  font-size: 0.82rem;
  margin-top: 0.5rem;
}

.history-list { list-style: none; padding: 0; margin: 0; display: flex; flex-direction: column; gap: 0.5rem; max-height: 50vh; overflow-y: auto; }
.history-list li { display: flex; align-items: center; justify-content: space-between; gap: 0.6rem; border: 1px solid var(--border); border-radius: var(--radius); padding: 0.5rem 0.7rem; flex-wrap: wrap; }
.history-list .history-actions { display: flex; gap: 0.4rem; }

/* Toast */
.toast {
  position: fixed;
  bottom: 1.2rem;
  right: 1.2rem;
  background: #1b1f24;
  color: #fff;
  padding: 0.7rem 1.1rem;
  border-radius: var(--radius);
  box-shadow: 0 8px 24px rgba(0,0,0,0.3);
  z-index: 300;
  max-width: 360px;
}
.toast.error { background: var(--danger-dark); }
.toast.success { background: var(--success); }

/* Login page */
.login-body {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 100vh;
  padding: 1rem;
}
.login-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 2rem;
  width: 100%;
  max-width: 380px;
  text-align: center;
  box-shadow: 0 10px 40px rgba(0,0,0,0.08);
}
.login-card h1 { font-size: 1.3rem; margin: 0.6rem 0 0.2rem; }
.login-card form { text-align: left; margin-top: 1rem; }
.login-card .mark { margin: 0 auto; }
.login-card button { width: 100%; margin-top: 1.2rem; }
.login-message {
  margin-top: 0.8rem;
  padding: 0.5rem 0.7rem;
  border-radius: var(--radius);
  background: #fdecea;
  color: var(--danger-dark);
  font-size: 0.88rem;
}

/* Responsive: tablet and below */
@media (max-width: 900px) {
  .layout { grid-template-columns: 1fr; }
  .sidebar {
    position: static;
    max-height: none;
    border-right: none;
    border-bottom: 1px solid var(--border);
  }
  .content { padding: 1rem; }
  .editor-section { height: auto; }
  .editor-body { min-height: 60vh; }
}

@media (max-width: 520px) {
  .toolbar { gap: 0.35rem; }
  .toolbar button { padding: 0.45rem 0.6rem; font-size: 0.9rem; }
  .topbar { padding: 0.6rem 0.8rem; }
}

@media (prefers-color-scheme: dark) {
  :root {
    color-scheme: dark;
    --bg: #14171c;
    --surface: #1c2028;
    --border: #333a45;
    --text: #e7e9ec;
    --text-muted: #9aa4b2;
    --primary: #5b8dfb;
    --primary-dark: #3f6fe0;
    --danger: #f2857b;
    --danger-dark: #e05a4e;
    --success: #4ade80;
  }
  .category-list button[aria-current="true"], .document-list button[aria-current="true"] {
    background: #1f2a44;
  }
  .status-bar.error { background: #3a2020; }
  .status-bar.success { background: #16321f; }
  .login-message { background: #3a2020; }
}
