/* ------------------------------------------------------------------ tokens */
:root {
  --bg: #f7f8fa;
  --surface: #ffffff;
  --surface-2: #f0f2f6;
  --border: #dfe3ea;
  --text: #1b2030;
  --text-soft: #5a6478;
  --accent: #1f6feb;
  --accent-soft: #e7effd;
  --danger: #c0392b;
  --warn-bg: #fff6e0;
  --warn-border: #e8c979;
  --code-bg: #f2f4f8;
  --radius: 10px;
  --sans: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  --mono: ui-monospace, SFMono-Regular, "SF Mono", Menlo, Consolas, monospace;
  --shadow: 0 1px 2px rgba(20, 25, 40, 0.06), 0 8px 24px rgba(20, 25, 40, 0.06);
}

html[data-theme="dark"] {
  --bg: #0f1218;
  --surface: #161b23;
  --surface-2: #1c222c;
  --border: #2a323f;
  --text: #e6e9ef;
  --text-soft: #9aa5b8;
  --accent: #6aa6ff;
  --accent-soft: #1b2740;
  --danger: #ef6a5c;
  --warn-bg: #2c2617;
  --warn-border: #6b5a2a;
  --code-bg: #11161d;
  --shadow: 0 1px 2px rgba(0, 0, 0, 0.4), 0 10px 30px rgba(0, 0, 0, 0.35);
}

@media (prefers-color-scheme: dark) {
  html:not([data-theme="light"]) {
    --bg: #0f1218;
    --surface: #161b23;
    --surface-2: #1c222c;
    --border: #2a323f;
    --text: #e6e9ef;
    --text-soft: #9aa5b8;
    --accent: #6aa6ff;
    --accent-soft: #1b2740;
    --danger: #ef6a5c;
    --warn-bg: #2c2617;
    --warn-border: #6b5a2a;
    --code-bg: #11161d;
    --shadow: 0 1px 2px rgba(0, 0, 0, 0.4), 0 10px 30px rgba(0, 0, 0, 0.35);
  }
}

* { box-sizing: border-box; }

/* L'attributo hidden deve vincere sui display espliciti (flex, grid, block). */
[hidden] { display: none !important; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: var(--sans);
  font-size: 15.5px;
  line-height: 1.65;
  -webkit-font-smoothing: antialiased;
}

a { color: var(--accent); }

.skip-link {
  position: absolute;
  left: -9999px;
}

/* ------------------------------------------------------------------ topbar */
.topbar {
  position: sticky;
  top: 0;
  z-index: 30;
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 10px 18px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}

.brand { display: flex; align-items: center; gap: 10px; min-width: 0; }

.brand-mark {
  display: grid;
  place-items: center;
  width: 32px;
  height: 32px;
  border-radius: 8px;
  background: var(--accent);
  color: #fff;
  font-weight: 700;
  font-size: 13px;
  letter-spacing: 0.4px;
}

.brand-text { display: flex; flex-direction: column; line-height: 1.2; min-width: 0; }
.brand-text strong { font-size: 14.5px; }
.brand-text small { color: var(--text-soft); font-size: 12px; }

.search { position: relative; flex: 1; max-width: 460px; margin-left: auto; }

.search input {
  width: 100%;
  padding: 8px 12px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface-2);
  color: var(--text);
  font: inherit;
  font-size: 14px;
}

.search input:focus { outline: 2px solid var(--accent); outline-offset: -1px; }

.search-results {
  position: absolute;
  top: calc(100% + 6px);
  left: 0;
  right: 0;
  max-height: 60vh;
  overflow-y: auto;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 6px;
}

.search-hit {
  display: block;
  width: 100%;
  text-align: left;
  border: 0;
  background: none;
  color: inherit;
  font: inherit;
  padding: 8px 10px;
  border-radius: 8px;
  cursor: pointer;
}

.search-hit:hover { background: var(--surface-2); }
.search-hit strong { display: block; font-size: 14px; }
.search-hit span { color: var(--text-soft); font-size: 12.5px; }
.search-empty { padding: 10px; color: var(--text-soft); font-size: 13px; }

.topbar-actions { display: flex; align-items: center; gap: 8px; }

.lang-switch {
  display: inline-flex;
  border: 1px solid var(--border);
  border-radius: 8px;
  overflow: hidden;
}

.lang-button {
  border: 0;
  background: var(--surface);
  color: var(--text-soft);
  font: inherit;
  font-size: 12.5px;
  font-weight: 600;
  padding: 6px 10px;
  cursor: pointer;
}

.lang-button.is-active { background: var(--accent); color: #fff; }

.button {
  border: 1px solid transparent;
  background: var(--accent);
  color: #fff;
  font: inherit;
  font-size: 14px;
  font-weight: 550;
  padding: 7px 14px;
  border-radius: 8px;
  cursor: pointer;
}

.button:hover { filter: brightness(1.06); }
.button.ghost { background: transparent; color: var(--text); border-color: var(--border); }
.button.ghost:hover { background: var(--surface-2); filter: none; }
.button.small { font-size: 13px; padding: 5px 10px; }
.button.danger { color: var(--danger); border-color: var(--border); }

.icon-button {
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  border-radius: 8px;
  width: 32px;
  height: 32px;
  font-size: 15px;
  cursor: pointer;
}

.nav-toggle { display: none; }

/* ------------------------------------------------------------------ layout */
.layout {
  display: grid;
  grid-template-columns: 264px minmax(0, 1fr);
  align-items: start;
  max-width: 1440px;
  margin: 0 auto;
}

.sidebar {
  position: sticky;
  top: 53px;
  height: calc(100vh - 53px);
  overflow-y: auto;
  padding: 20px 12px 24px 18px;
  border-right: 1px solid var(--border);
}

.nav-group { margin-bottom: 18px; }

.nav-group h3 {
  margin: 0 0 6px 8px;
  font-size: 11px;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--text-soft);
}

.nav-item { display: flex; align-items: center; gap: 2px; }

.nav-link {
  flex: 1;
  display: block;
  padding: 6px 10px;
  border-radius: 8px;
  color: var(--text);
  text-decoration: none;
  font-size: 14px;
  border-left: 2px solid transparent;
}

.nav-link:hover { background: var(--surface-2); }

.nav-link.is-active {
  background: var(--accent-soft);
  color: var(--accent);
  font-weight: 600;
}

.nav-move {
  border: 0;
  background: none;
  color: var(--text-soft);
  cursor: pointer;
  font-size: 11px;
  padding: 2px 3px;
  border-radius: 5px;
}

.nav-move:hover { background: var(--surface-2); color: var(--text); }

.sidebar-admin { margin: 10px 0 14px; padding-left: 8px; }

.sidebar-foot {
  margin: 8px 8px 0;
  font-size: 12px;
  color: var(--text-soft);
}

/* ----------------------------------------------------------------- content */
.content {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 210px;
  grid-template-areas:
    "head toc"
    "banner toc"
    "doc toc"
    "editor editor";
  gap: 0 32px;
  padding: 28px 32px 80px;
  min-width: 0;
}

.page-head {
  grid-area: head;
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 16px;
  flex-wrap: wrap;
}

.crumb {
  margin: 0;
  font-size: 12px;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--text-soft);
}

.page-head h1 { margin: 4px 0 4px; font-size: 30px; line-height: 1.2; }
.meta { margin: 0; color: var(--text-soft); font-size: 12.5px; }
.page-actions { display: flex; gap: 6px; flex-wrap: wrap; }

.banner {
  grid-area: banner;
  margin: 18px 0 0;
  padding: 10px 14px;
  border: 1px solid var(--warn-border);
  background: var(--warn-bg);
  border-radius: var(--radius);
  font-size: 13.5px;
}

.banner.error { border-color: var(--danger); background: transparent; color: var(--danger); }

.doc { grid-area: doc; min-width: 0; padding-top: 8px; }
.doc.preview { grid-area: editor; }

.doc h2 {
  margin: 34px 0 12px;
  padding-bottom: 6px;
  border-bottom: 1px solid var(--border);
  font-size: 21px;
}

.doc h3 { margin: 26px 0 8px; font-size: 17px; }
.doc h4 { margin: 20px 0 6px; font-size: 15px; color: var(--text-soft); }
.doc p, .doc li { font-size: 15.5px; }
.doc ul, .doc ol { padding-left: 22px; }
.doc li + li { margin-top: 4px; }

.doc code {
  font-family: var(--mono);
  font-size: 0.88em;
  background: var(--code-bg);
  border: 1px solid var(--border);
  border-radius: 5px;
  padding: 1px 5px;
}

.doc pre {
  background: var(--code-bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px 16px;
  overflow-x: auto;
}

.doc pre code { background: none; border: 0; padding: 0; font-size: 13px; line-height: 1.55; }

.doc blockquote {
  margin: 16px 0;
  padding: 2px 16px;
  border-left: 3px solid var(--accent);
  color: var(--text-soft);
}

.doc table {
  width: 100%;
  border-collapse: collapse;
  margin: 16px 0;
  font-size: 14px;
  display: block;
  overflow-x: auto;
}

.doc th, .doc td { border: 1px solid var(--border); padding: 7px 10px; text-align: left; vertical-align: top; }
.doc th { background: var(--surface-2); font-size: 13px; }
.doc hr { border: 0; border-top: 1px solid var(--border); margin: 28px 0; }

/* --------------------------------------------------------------------- toc */
.toc {
  grid-area: toc;
  position: sticky;
  top: 80px;
  align-self: start;
  font-size: 13px;
  border-left: 1px solid var(--border);
  padding-left: 16px;
  max-height: calc(100vh - 120px);
  overflow-y: auto;
}

.toc h4 {
  margin: 0 0 8px;
  font-size: 11px;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--text-soft);
}

.toc a { display: block; padding: 3px 0; color: var(--text-soft); text-decoration: none; }
.toc a:hover { color: var(--accent); }
.toc a.depth-3 { padding-left: 12px; font-size: 12.5px; }

/* ------------------------------------------------------------------ editor */
.editor {
  grid-area: editor;
  margin-top: 24px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  box-shadow: var(--shadow);
  overflow: hidden;
}

.editor-bar {
  display: flex;
  align-items: center;
  gap: 12px;
  flex-wrap: wrap;
  padding: 10px 12px;
  border-bottom: 1px solid var(--border);
  background: var(--surface-2);
}

.editor-tabs { display: flex; gap: 4px; }

.tab {
  border: 1px solid transparent;
  background: none;
  color: var(--text-soft);
  font: inherit;
  font-size: 13px;
  padding: 4px 10px;
  border-radius: 7px;
  cursor: pointer;
}

.tab.is-active { background: var(--surface); border-color: var(--border); color: var(--text); font-weight: 600; }

.editor-tools { display: flex; gap: 4px; flex-wrap: wrap; }

.chip {
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  border-radius: 6px;
  font: inherit;
  font-size: 12px;
  padding: 3px 8px;
  cursor: pointer;
}

.chip:hover { background: var(--accent-soft); }
.editor-actions { display: flex; gap: 6px; margin-left: auto; }

.editor-titles {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
  gap: 10px;
  padding: 12px;
  border-bottom: 1px solid var(--border);
}

.field { display: flex; flex-direction: column; gap: 4px; font-size: 12.5px; color: var(--text-soft); }
.field-pair { display: flex; gap: 8px; }

.field input, .field textarea {
  border: 1px solid var(--border);
  border-radius: 7px;
  background: var(--bg);
  color: var(--text);
  font: inherit;
  font-size: 14px;
  padding: 7px 9px;
  width: 100%;
}

#markdown {
  display: block;
  width: 100%;
  min-height: 60vh;
  border: 0;
  padding: 16px;
  background: var(--surface);
  color: var(--text);
  font-family: var(--mono);
  font-size: 13.5px;
  line-height: 1.7;
  resize: vertical;
}

#markdown:focus { outline: none; }

.editor-hint {
  margin: 0;
  padding: 8px 12px;
  border-top: 1px solid var(--border);
  background: var(--surface-2);
  color: var(--text-soft);
  font-size: 12px;
}

.preview { padding: 8px 18px 22px; }

/* ----------------------------------------------------- pagine disabilitate */
.page-flag {
  display: inline-block;
  margin: 8px 0 0;
  padding: 3px 9px;
  border: 1px solid var(--warn-border);
  background: var(--warn-bg);
  border-radius: 999px;
  font-size: 12px;
  color: var(--text);
}

.nav-link.is-disabled { color: var(--text-soft); }
.nav-link.is-disabled::after {
  content: "•";
  margin-left: 6px;
  color: var(--warn-border);
  font-weight: 700;
}

.nav-link.is-disabled.is-active { color: var(--accent); }

/* ------------------------------------------------------------------ dialog */
.dialog {
  border: 1px solid var(--border);
  border-radius: 14px;
  background: var(--surface);
  color: var(--text);
  padding: 22px;
  width: min(440px, 92vw);
  box-shadow: var(--shadow);
}

.dialog::backdrop { background: rgba(8, 11, 18, 0.5); }
.dialog h2 { margin: 0 0 6px; font-size: 19px; }
.dialog-sub { margin: 0 0 16px; color: var(--text-soft); font-size: 13px; }
.dialog form { display: flex; flex-direction: column; gap: 12px; }
.dialog-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 6px; }
.dialog-error { margin: 0; color: var(--danger); font-size: 13px; }

.history-list { display: flex; flex-direction: column; gap: 6px; max-height: 50vh; overflow-y: auto; margin: 12px 0; }

.history-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 10px;
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 8px 10px;
  font-size: 13px;
}

.history-row span { color: var(--text-soft); }

/* ------------------------------------------------------------------- toast */
.toast {
  position: fixed;
  right: 20px;
  bottom: 20px;
  z-index: 60;
  background: var(--text);
  color: var(--bg);
  padding: 10px 16px;
  border-radius: 9px;
  font-size: 13.5px;
  box-shadow: var(--shadow);
}

.toast.is-error { background: var(--danger); color: #fff; }

/* -------------------------------------------------------------- responsive */
@media (max-width: 1080px) {
  .content { grid-template-columns: minmax(0, 1fr); grid-template-areas: "head" "banner" "doc" "editor"; }
  .toc { display: none; }
}

@media (max-width: 560px) {
  .topbar { flex-wrap: wrap; row-gap: 8px; }
  .search { order: 10; flex-basis: 100%; max-width: none; margin-left: 0; }
}

@media (max-width: 820px) {
  .nav-toggle { display: block; }
  .layout { grid-template-columns: minmax(0, 1fr); }
  .sidebar {
    display: none;
    position: fixed;
    inset: 53px 0 0 0;
    height: auto;
    z-index: 25;
    background: var(--surface);
    border-right: 0;
  }
  .sidebar.is-open { display: block; }
  .content { padding: 22px 18px 70px; }
  .brand-text { display: none; }
  .search { max-width: none; }
  .page-head h1 { font-size: 25px; }
}
