:root {
  color-scheme: light;
  --bg: #f6f7f9;
  --surface: #ffffff;
  --surface-alt: #eef1f5;
  --text: #1a1d21;
  --text-muted: #565d68;
  --border: #d4d9e1;
  --accent: #1f5fa9;
  --accent-strong: #164a86;
  --accent-contrast: #ffffff;
  --danger: #b3261e;
  --danger-soft: #fdecea;
  --success-bg: #e6f4ea;
  --success-text: #1b5e2a;
  --error-bg: #fdecea;
  --error-text: #8c1d18;
  --focus: #0b5bd3;
  --radius: 12px;
  --shadow: 0 1px 2px rgba(15, 23, 42, 0.08);
  --space: 1rem;
  --max-width: 960px;
}

[data-theme='dark'] {
  color-scheme: dark;
  --bg: #12151a;
  --surface: #1a1f26;
  --surface-alt: #232a33;
  --text: #eef1f5;
  --text-muted: #b3bcc8;
  --border: #38414d;
  --accent: #8ab8f5;
  --accent-strong: #a9cbf8;
  --accent-contrast: #0b1220;
  --danger: #ff9d95;
  --danger-soft: #40201d;
  --success-bg: #16321f;
  --success-text: #b7e7c4;
  --error-bg: #3d1b18;
  --error-text: #ffc7c1;
  --focus: #8ab8f5;
  --shadow: 0 1px 2px rgba(0, 0, 0, 0.5);
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme='light']) {
    color-scheme: dark;
    --bg: #12151a;
    --surface: #1a1f26;
    --surface-alt: #232a33;
    --text: #eef1f5;
    --text-muted: #b3bcc8;
    --border: #38414d;
    --accent: #8ab8f5;
    --accent-strong: #a9cbf8;
    --accent-contrast: #0b1220;
    --danger: #ff9d95;
    --danger-soft: #40201d;
    --success-bg: #16321f;
    --success-text: #b7e7c4;
    --error-bg: #3d1b18;
    --error-text: #ffc7c1;
    --focus: #8ab8f5;
    --shadow: 0 1px 2px rgba(0, 0, 0, 0.5);
  }
}

*,
*::before,
*::after {
  box-sizing: border-box;
}

html {
  -webkit-text-size-adjust: 100%;
}

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: system-ui, -apple-system, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
  font-size: 1rem;
  line-height: 1.55;
  overflow-wrap: break-word;
}

h1,
h2,
h3 {
  line-height: 1.25;
  margin: 0 0 0.5rem;
}

h1 {
  font-size: 1.5rem;
}

h2 {
  font-size: 1.2rem;
}

h3 {
  font-size: 1.05rem;
}

p {
  margin: 0 0 0.75rem;
}

a {
  color: var(--accent);
  text-decoration-thickness: 1px;
  text-underline-offset: 2px;
}

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

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

.skip-link {
  position: absolute;
  left: -9999px;
  top: 0;
  background: var(--surface);
  color: var(--text);
  padding: 0.75rem 1rem;
  z-index: 10;
  border-radius: 0 0 var(--radius) 0;
}

.skip-link:focus {
  left: 0;
}

/* ---------------------------------- Layout --------------------------------- */

.site-header {
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}

.header-top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.5rem;
  max-width: var(--max-width);
  margin: 0 auto;
  padding: 0.75rem var(--space);
}

.brand {
  margin: 0;
  font-weight: 700;
  letter-spacing: 0.01em;
}

.container {
  max-width: var(--max-width);
  margin: 0 auto;
  padding: var(--space);
}

.site-footer {
  max-width: var(--max-width);
  margin: 0 auto;
  padding: 1.5rem var(--space) 2.5rem;
  color: var(--text-muted);
  font-size: 0.875rem;
}

.site-footer p {
  margin: 0;
}

.nav {
  max-width: var(--max-width);
  margin: 0 auto;
  padding: 0 var(--space) 0.75rem;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.5rem;
}

.nav-list {
  display: flex;
  flex-wrap: wrap;
  gap: 0.25rem;
  list-style: none;
  margin: 0;
  padding: 0;
  flex: 1 1 auto;
}

.nav-link {
  display: inline-flex;
  align-items: center;
  min-height: 2.75rem;
  padding: 0 0.75rem;
  border-radius: 999px;
  color: var(--text-muted);
  text-decoration: none;
  font-weight: 600;
}

.nav-link:hover {
  background: var(--surface-alt);
  color: var(--text);
}

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

.nav-exit {
  margin: 0;
}

/* ---------------------------------- Blocchi -------------------------------- */

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 1rem;
  margin-bottom: 1.25rem;
}

.auth-card {
  max-width: 30rem;
  margin: 1.5rem auto;
}

.page-head {
  margin-bottom: 1rem;
}

.page-head h1 {
  margin-bottom: 0.25rem;
}

.muted {
  color: var(--text-muted);
  font-size: 0.925rem;
}

.help {
  color: var(--text-muted);
  font-size: 0.85rem;
  margin: 0.25rem 0 0;
}

.breadcrumb {
  font-size: 0.875rem;
  color: var(--text-muted);
  margin-bottom: 0.75rem;
}

.breadcrumb a {
  color: var(--text-muted);
}

.breadcrumb span[aria-current='page'] {
  color: var(--text);
}

.notice {
  background: var(--surface-alt);
  border-left: 4px solid var(--accent);
  padding: 0.75rem;
  border-radius: 8px;
}

.flash {
  padding: 0.75rem 1rem;
  border-radius: var(--radius);
  border: 1px solid transparent;
  margin-bottom: 1rem;
}

.flash-success {
  background: var(--success-bg);
  color: var(--success-text);
  border-color: var(--success-text);
}

.flash-error {
  background: var(--error-bg);
  color: var(--error-text);
  border-color: var(--error-text);
}

.error-summary {
  background: var(--error-bg);
  color: var(--error-text);
  border: 1px solid var(--error-text);
  border-radius: var(--radius);
  padding: 0.75rem 1rem;
  margin-bottom: 1rem;
}

.error-summary-title {
  font-weight: 700;
  margin: 0 0 0.25rem;
}

.error-summary ul {
  margin: 0;
  padding-left: 1.25rem;
}

.field-error {
  color: var(--error-text);
  font-weight: 600;
  font-size: 0.875rem;
  margin: 0.25rem 0 0;
}

.empty-state {
  background: var(--surface);
  border: 1px dashed var(--border);
  border-radius: var(--radius);
  padding: 1.5rem 1rem;
  text-align: center;
}

/* ---------------------------------- Moduli --------------------------------- */

.form {
  display: flex;
  flex-direction: column;
  gap: 1rem;
}

.form-row {
  gap: 0.75rem;
}

.field {
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
}

.field.grow {
  flex: 1 1 auto;
}

label {
  font-weight: 600;
}

input,
select,
textarea {
  font: inherit;
  color: var(--text);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 0.65rem 0.75rem;
  width: 100%;
  min-height: 2.75rem;
}

textarea {
  resize: vertical;
  min-height: 12rem;
  line-height: 1.5;
}

input[aria-invalid='true'],
select[aria-invalid='true'],
textarea[aria-invalid='true'] {
  border-color: var(--error-text);
}

.form-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  align-items: center;
}

.filters {
  gap: 0.75rem;
}

/* --------------------------------- Pulsanti -------------------------------- */

.button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.35rem;
  min-height: 2.75rem;
  padding: 0.55rem 1rem;
  border-radius: 10px;
  border: 1px solid transparent;
  background: var(--surface-alt);
  color: var(--text);
  font: inherit;
  font-weight: 600;
  text-decoration: none;
  cursor: pointer;
}

.button:hover {
  filter: brightness(0.97);
}

.button:disabled {
  opacity: 0.7;
  cursor: progress;
}

.button-primary {
  background: var(--accent);
  color: var(--accent-contrast);
  border-color: var(--accent);
}

.button-primary:hover {
  background: var(--accent-strong);
  color: var(--accent-contrast);
}

.button-quiet {
  background: transparent;
  border-color: var(--border);
  color: var(--text);
}

.button-danger {
  background: var(--danger);
  border-color: var(--danger);
  color: #fff;
}

.button-danger-quiet {
  background: transparent;
  border-color: var(--danger);
  color: var(--danger);
}

[data-theme='dark'] .button-danger {
  color: #1a0f0e;
}

.theme-toggle[aria-pressed='true'] {
  border-color: var(--accent);
}

/* --------------------------------- Elenchi --------------------------------- */

.topic-list,
.note-list,
.stats {
  list-style: none;
  margin: 0;
  padding: 0;
}

.topic-item,
.note-item {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1rem;
  margin-bottom: 0.75rem;
  box-shadow: var(--shadow);
}

.topic-item {
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
}

.topic-main p,
.note-meta {
  margin: 0;
}

.topic-title {
  margin: 0 0 0.15rem;
}

.topic-title a {
  text-decoration: none;
}

.topic-title a:hover {
  text-decoration: underline;
}

.topic-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
}

.note-title {
  margin: 0 0 0.25rem;
}

.note-title a {
  text-decoration: none;
}

.note-title a:hover {
  text-decoration: underline;
}

.note-meta {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.5rem;
  color: var(--text-muted);
  font-size: 0.875rem;
  margin-bottom: 0.5rem;
}

.badge {
  display: inline-block;
  background: var(--surface-alt);
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 0.1rem 0.6rem;
  font-size: 0.8rem;
  font-weight: 600;
  color: var(--text);
}

.note-excerpt {
  margin: 0;
  color: var(--text-muted);
}

.note-body {
  white-space: pre-wrap;
  overflow-wrap: anywhere;
  margin: 1rem 0 1.25rem;
}

.stats {
  display: flex;
  flex-wrap: wrap;
  gap: 0.75rem 1.5rem;
}

.stats li {
  color: var(--text-muted);
}

.stats strong {
  color: var(--text);
}

.pagination {
  margin-top: 1rem;
}

.pagination-links {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
}

/* ------------------------------- Schermi larghi ---------------------------- */

@media (min-width: 640px) {
  h1 {
    font-size: 1.85rem;
  }

  .container {
    padding: 1.5rem;
  }

  .card {
    padding: 1.5rem;
  }

  .form-row {
    flex-direction: row;
    align-items: flex-end;
  }

  .filters {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    align-items: end;
  }

  .filters .grow {
    grid-column: span 2;
  }

  .topic-item {
    flex-direction: row;
    align-items: center;
    justify-content: space-between;
  }
}

@media (min-width: 960px) {
  .header-top,
  .nav {
    padding-left: 0;
    padding-right: 0;
  }

  .filters {
    grid-template-columns: repeat(3, minmax(0, 1fr));
  }

  .filters .grow {
    grid-column: span 1;
  }
}

@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    animation-duration: 0.001ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.001ms !important;
    scroll-behavior: auto !important;
  }
}
