:root {
  --bg: #ffffff;
  --surface: #f3f4f6;
  --border: #9ca3af;
  --text: #111827;
  --text-muted: #374151;
  --primary: #004e89;
  --primary-text: #ffffff;
  --primary-hover: #003a66;
  --bot-bg: #e5e7eb;
  --bot-text: #111827;
  --user-bg: #004e89;
  --user-text: #ffffff;
  --focus-ring: #b45309;
  --danger: #991b1b;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f172a;
    --surface: #1e293b;
    --border: #64748b;
    --text: #f8fafc;
    --text-muted: #cbd5e1;
    --primary: #38bdf8;
    --primary-text: #0f172a;
    --primary-hover: #7dd3fc;
    --bot-bg: #1e293b;
    --bot-text: #f8fafc;
    --user-bg: #38bdf8;
    --user-text: #0f172a;
    --focus-ring: #fbbf24;
    --danger: #f87171;
  }
}

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

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

/* Utilità solo per screen reader */
.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 visibile al focus da tastiera */
.skip-link {
  position: absolute;
  top: -100px;
  left: 1rem;
  z-index: 1000;
  padding: 0.75rem 1rem;
  background: var(--primary);
  color: var(--primary-text);
  font-weight: 700;
  text-decoration: none;
  border-radius: 0.5rem;
}

.skip-link:focus {
  top: 1rem;
}

/* Focus ben visibile per navigazione da tastiera */
:focus-visible {
  outline: 3px solid var(--focus-ring);
  outline-offset: 2px;
}

/* Layout principale */
.app-shell {
  display: flex;
  flex-direction: column;
  height: 100dvh;
  max-width: 52rem;
  margin: 0 auto;
  border-inline: 1px solid var(--border);
}

.app-header {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
  padding: 0.85rem 1rem;
  background-color: var(--surface);
  border-bottom: 2px solid var(--border);
}

.brand h1 {
  margin: 0;
  font-size: 1.35rem;
  line-height: 1.2;
}

.subtitle {
  margin: 0;
  font-size: 0.9rem;
  color: var(--text-muted);
}

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

/* Area chat */
.chat-main {
  display: flex;
  flex-direction: column;
  flex: 1;
  min-height: 0;
}

.chat-log {
  flex: 1;
  overflow-y: auto;
  padding: 1rem;
  display: flex;
  flex-direction: column;
  gap: 1rem;
}

.msg {
  display: flex;
  flex-direction: column;
  max-width: 85%;
}

.msg-bot {
  align-self: flex-start;
}

.msg-user {
  align-self: flex-end;
  align-items: flex-end;
}

.msg-author {
  font-size: 0.85rem;
  font-weight: 700;
  margin-bottom: 0.25rem;
  color: var(--text-muted);
}

.msg-bubble {
  padding: 0.75rem 1rem;
  border-radius: 0.75rem;
  white-space: pre-wrap;
  word-wrap: break-word;
}

.msg-bot .msg-bubble {
  background-color: var(--bot-bg);
  color: var(--bot-text);
  border: 1px solid var(--border);
}

.msg-user .msg-bubble {
  background-color: var(--user-bg);
  color: var(--user-text);
}

.status-bar {
  min-height: 1.5rem;
  padding: 0.2rem 1rem;
  font-size: 0.9rem;
  font-weight: 600;
  color: var(--text-muted);
}

/* Suggerimenti rapidi */
.quick-prompts {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  padding: 0.5rem 1rem;
  border-top: 1px solid var(--border);
  background-color: var(--surface);
}

.chip {
  min-height: 44px;
  padding: 0.4rem 0.85rem;
  border: 2px solid var(--primary);
  border-radius: 999px;
  background-color: var(--bg);
  color: var(--text);
  font-size: 0.95rem;
  font-weight: 600;
  cursor: pointer;
}

.chip:hover {
  background-color: var(--primary);
  color: var(--primary-text);
}

/* Form di invio */
.chat-form {
  display: flex;
  gap: 0.5rem;
  padding: 0.75rem 1rem;
  background-color: var(--surface);
  border-top: 2px solid var(--border);
}

.chat-input {
  flex: 1;
  min-height: 44px;
  padding: 0.6rem 0.85rem;
  font-size: 1rem;
  color: var(--text);
  background-color: var(--bg);
  border: 2px solid var(--border);
  border-radius: 0.5rem;
}

/* Pulsanti (minimo 44x44px per WCAG) */
button {
  font-family: inherit;
}

.btn-primary,
.btn-secondary,
.btn-icon {
  min-height: 44px;
  min-width: 44px;
  padding: 0.5rem 1rem;
  border-radius: 0.5rem;
  font-size: 1rem;
  font-weight: 700;
  cursor: pointer;
}

.btn-primary {
  background-color: var(--primary);
  color: var(--primary-text);
  border: 2px solid var(--primary);
}

.btn-primary:hover:not(:disabled) {
  background-color: var(--primary-hover);
}

.btn-secondary,
.btn-icon {
  background-color: var(--bg);
  color: var(--text);
  border: 2px solid var(--border);
}

.btn-secondary[aria-pressed="true"],
.btn-icon[aria-pressed="true"] {
  background-color: var(--primary);
  color: var(--primary-text);
  border-color: var(--primary);
}

button:disabled {
  opacity: 0.6;
  cursor: not-allowed;
}

/* Modale Privacy */
.modal {
  max-width: 34rem;
  width: calc(100% - 2rem);
  padding: 1.5rem;
  border: 2px solid var(--border);
  border-radius: 0.75rem;
  background-color: var(--bg);
  color: var(--text);
}

.modal::backdrop {
  background-color: rgba(0, 0, 0, 0.7);
}

.modal-actions {
  display: flex;
  justify-content: flex-end;
  margin-top: 1.25rem;
}

@media (prefers-reduced-motion: reduce) {
  * {
    animation-duration: 0.01ms !important;
    transition-duration: 0.01ms !important;
    scroll-behavior: auto !important;
  }
}