/* ===== VARIABLES ===== */
:root {
  --bg: #0d1117;
  --bg-card: #161b22;
  --bg-sidebar: #010409;
  --bg-hover: #1f2937;
  --border: #30363d;
  --border-light: #21262d;
  --text: #e6edf3;
  --text-muted: #8b949e;
  --text-dim: #6e7681;
  --blue: #58a6ff;
  --blue-dark: #1f6feb;
  --green: #3fb950;
  --green-dark: #238636;
  --red: #f85149;
  --red-dark: #da3633;
  --yellow: #d29922;
  --purple: #a5d6ff;
  --sidebar-w: 220px;
  --radius: 8px;
  --shadow: 0 4px 24px rgba(0,0,0,.4);
}

/* ===== RESET ===== */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
html { font-size: 14px; }
body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', system-ui, sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.6;
  min-height: 100vh;
}
a { color: var(--blue); text-decoration: none; cursor: pointer; }
button { cursor: pointer; font-family: inherit; font-size: inherit; }
input, select, textarea { font-family: inherit; font-size: inherit; }

/* ===== UTILITIES ===== */
.hidden { display: none !important; }
.text-muted { color: var(--text-muted); font-size: .85rem; }
.text-green { color: var(--green); }
.text-red { color: var(--red); }
.text-yellow { color: var(--yellow); }
.text-blue { color: var(--blue); }

/* ===== LOGIN ===== */
.login-page {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, #0d1117 0%, #161b22 50%, #0d1117 100%);
}
.login-container { width: 100%; max-width: 380px; padding: 1rem; }
.login-card {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 2.5rem 2rem;
  box-shadow: var(--shadow);
}
.login-logo { text-align: center; margin-bottom: 2rem; }
.logo-icon-big { font-size: 3rem; line-height: 1; }
.login-logo h1 { font-size: 1.5rem; font-weight: 700; color: var(--text); margin: .5rem 0 .2rem; }
.login-logo p { color: var(--text-muted); font-size: .9rem; }
.login-footer { text-align: center; color: var(--text-dim); font-size: .75rem; margin-top: 1.5rem; }

/* ===== FORMS ===== */
.form-group { margin-bottom: 1rem; }
.form-group label { display: block; color: var(--text-muted); font-size: .85rem; margin-bottom: .3rem; }
.form-row { display: grid; grid-template-columns: 1fr 1fr; gap: 1rem; }
input[type="text"], input[type="email"], input[type="password"], input[type="date"],
input[type="number"], select, textarea {
  width: 100%;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text);
  padding: .6rem .9rem;
  outline: none;
  transition: border-color .15s;
}
input:focus, select:focus, textarea:focus { border-color: var(--blue); }
textarea { resize: vertical; min-height: 80px; }
.search-input { max-width: 200px; }
.select-input { max-width: 180px; }

/* ===== BUTTONS ===== */
.btn {
  display: inline-flex;
  align-items: center;
  gap: .4rem;
  padding: .55rem 1.1rem;
  border-radius: var(--radius);
  border: 1px solid transparent;
  font-weight: 500;
  font-size: .875rem;
  transition: all .15s;
  white-space: nowrap;
}
.btn-primary { background: var(--blue-dark); color: #fff; border-color: var(--blue); }
.btn-primary:hover { background: var(--blue); }
.btn-secondary { background: var(--bg-card); color: var(--text); border-color: var(--border); }
.btn-secondary:hover { border-color: var(--blue); color: var(--blue); }
.btn-danger { background: transparent; color: var(--red); border-color: var(--red-dark); }
.btn-danger:hover { background: var(--red-dark); color: #fff; }
.btn-success { background: var(--green-dark); color: #fff; }
.btn-success:hover { background: var(--green); }
.btn-sm { padding: .3rem .7rem; font-size: .8rem; }
.btn-full { width: 100%; justify-content: center; padding: .75rem; }
.btn-icon {
  background: none;
  border: none;
  color: var(--text-muted);
  font-size: 1rem;
  padding: .3rem .5rem;
  border-radius: 4px;
  transition: color .15s, background .15s;
}
.btn-icon:hover { color: var(--text); background: var(--bg-hover); }
.btn.copied { background: var(--green-dark) !important; border-color: var(--green) !important; color: #fff !important; }

/* ===== ALERTS ===== */
.alert { padding: .7rem 1rem; border-radius: var(--radius); font-size: .875rem; margin-top: .75rem; }
.alert-error { background: rgba(248,81,73,.1); border: 1px solid var(--red-dark); color: var(--red); }
.alert-success { background: rgba(63,185,80,.1); border: 1px solid var(--green-dark); color: var(--green); }

/* ===== APP LAYOUT ===== */
.app { display: flex; min-height: 100vh; }

/* ===== SIDEBAR ===== */
.sidebar {
  width: var(--sidebar-w);
  min-width: var(--sidebar-w);
  background: var(--bg-sidebar);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  position: fixed;
  top: 0; left: 0; bottom: 0;
  z-index: 100;
}
.sidebar-header {
  display: flex;
  align-items: center;
  gap: .6rem;
  padding: 1.1rem 1rem;
  border-bottom: 1px solid var(--border-light);
  font-weight: 700;
  font-size: .9rem;
  color: var(--text);
}
.logo-icon { font-size: 2rem; }
.sidebar-brand { font-size: 1.05rem; font-weight: 700; line-height: 1.2; }
.sidebar-nav { flex: 1; padding: .5rem 0; overflow-y: auto; }
.nav-item {
  display: flex;
  align-items: center;
  gap: .75rem;
  padding: .65rem 1rem;
  color: var(--text-muted);
  transition: all .15s;
  border-left: 3px solid transparent;
  cursor: pointer;
}
.nav-item:hover { color: var(--text); background: var(--bg-hover); }
.nav-item.active { color: var(--blue); background: rgba(88,166,255,.08); border-left-color: var(--blue); }
.nav-icon { font-size: 1.1rem; flex-shrink: 0; }
.nav-label { font-size: .875rem; }
.sidebar-footer {
  display: flex;
  align-items: center;
  gap: .5rem;
  padding: .75rem 1rem;
  border-top: 1px solid var(--border-light);
}
.user-pill { display: flex; align-items: center; gap: .5rem; flex: 1; overflow: hidden; }
.user-avatar {
  width: 30px; height: 30px;
  border-radius: 50%;
  background: var(--blue-dark);
  color: #fff;
  display: flex; align-items: center; justify-content: center;
  font-size: .8rem; font-weight: 700;
  flex-shrink: 0;
}
.user-info { overflow: hidden; }
.user-name { font-size: .8rem; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.user-role { font-size: .7rem; color: var(--text-muted); text-transform: capitalize; }

/* ===== MAIN ===== */
.main {
  margin-left: var(--sidebar-w);
  flex: 1;
  padding: 1.5rem;
  max-width: calc(100vw - var(--sidebar-w));
  overflow-x: hidden;
}

/* ===== PAGE ===== */
.page { display: block; }
.page.hidden { display: none; }
.page-header {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: .75rem;
  margin-bottom: 1.5rem;
}
.page-header h2 { font-size: 1.3rem; font-weight: 700; flex: 1; }
.page-actions { display: flex; align-items: center; gap: .6rem; flex-wrap: wrap; }

/* ===== STATS ===== */
.stats-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(160px, 1fr));
  gap: 1rem;
  margin-bottom: 1.5rem;
}
.stat-card {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1.25rem 1rem;
  text-align: center;
  transition: transform .15s;
}
.stat-card:hover { transform: translateY(-2px); }
.stat-card.blue { border-top: 3px solid var(--blue); }
.stat-card.green { border-top: 3px solid var(--green); }
.stat-card.yellow { border-top: 3px solid var(--yellow); }
.stat-card.purple { border-top: 3px solid #bc8cff; }
.stat-icon { font-size: 1.8rem; margin-bottom: .4rem; }
.stat-num { font-size: 2rem; font-weight: 700; line-height: 1; }
.stat-lbl { color: var(--text-muted); font-size: .8rem; margin-top: .3rem; }

/* ===== CARD ===== */
.card {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
}
.card-head {
  padding: .9rem 1rem;
  border-bottom: 1px solid var(--border);
  font-weight: 600;
  font-size: .9rem;
}

/* ===== ROW-2 ===== */
.row-2 {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 1rem;
}

/* ===== SIMPLE LIST ===== */
.simple-list { padding: .5rem 0; }
.simple-list-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: .65rem 1rem;
  border-bottom: 1px solid var(--border-light);
  font-size: 1rem;
}
.simple-list-item:last-child { border-bottom: none; }
.simple-list-item .item-title { color: var(--text); font-size: 1.05rem; font-weight: 600; }
.simple-list-item .item-title a { font-size: 1.05rem !important; }
.simple-list-item .item-meta { color: var(--text); font-size: .9rem; }

/* ===== TABLE ===== */
.table-wrap { overflow-x: auto; }
.table {
  width: 100%;
  border-collapse: collapse;
  font-size: .875rem;
}
.table th {
  background: rgba(30,38,50,.6);
  padding: .65rem 1rem;
  text-align: left;
  font-weight: 600;
  color: var(--text-muted);
  border-bottom: 1px solid var(--border);
  white-space: nowrap;
}
.table td {
  padding: .65rem 1rem;
  border-bottom: 1px solid var(--border-light);
  vertical-align: middle;
}
.table tbody tr:hover { background: var(--bg-hover); }
.table tbody tr:last-child td { border-bottom: none; }
.td-loading { text-align: center; padding: 2rem; color: var(--text-muted); }
.td-actions { display: flex; gap: .3rem; white-space: nowrap; }

/* ===== BADGES ===== */
.badge {
  display: inline-block;
  padding: .15rem .55rem;
  border-radius: 20px;
  font-size: .75rem;
  font-weight: 600;
}
.badge-green { background: rgba(63,185,80,.15); color: var(--green); }
.badge-blue { background: rgba(88,166,255,.15); color: var(--blue); }
.badge-yellow { background: rgba(210,153,34,.15); color: var(--yellow); }
.badge-red { background: rgba(248,81,73,.15); color: var(--red); }
.badge-gray { background: rgba(139,148,158,.15); color: var(--text-muted); }
.badge-purple { background: rgba(188,140,255,.15); color: #bc8cff; }

/* ===== CARDS GRID (Tickets) ===== */
.cards-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(320px, 1fr));
  gap: 1rem;
}
.ticket-card {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1rem;
  display: flex;
  flex-direction: column;
  gap: .75rem;
}
.ticket-card-head {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: .5rem;
}
.ticket-question {
  font-weight: 600;
  font-size: .875rem;
  flex: 1;
}
.ticket-response {
  color: var(--text-muted);
  font-size: .8rem;
  line-height: 1.5;
  max-height: 80px;
  overflow: hidden;
  display: -webkit-box;
  -webkit-line-clamp: 4;
  -webkit-box-orient: vertical;
}
.ticket-meta {
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-size: .75rem;
  color: var(--text-dim);
}
.ticket-actions { display: flex; gap: .4rem; }

/* ===== AI ASSISTANT ===== */
.ai-wrap {
  display: flex;
  flex-direction: column;
  height: calc(100vh - 160px);
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
}
.ai-chat {
  flex: 1;
  overflow-y: auto;
  padding: 1.5rem;
  display: flex;
  flex-direction: column;
  gap: 1rem;
}
.ai-welcome {
  text-align: center;
  padding: 2rem 1rem;
  color: var(--text-muted);
}
.ai-bot-icon { font-size: 3rem; margin-bottom: 1rem; }
.ai-welcome h3 { color: var(--text); margin-bottom: .5rem; }
.ai-bubble {
  display: flex;
  gap: .75rem;
  max-width: 80%;
}
.ai-bubble.user { align-self: flex-end; flex-direction: row-reverse; }
.bubble-avatar {
  width: 32px; height: 32px;
  border-radius: 50%;
  display: flex; align-items: center; justify-content: center;
  font-size: 1rem;
  flex-shrink: 0;
}
.bubble-avatar.bot { background: rgba(88,166,255,.15); }
.bubble-avatar.usr { background: var(--blue-dark); color: #fff; font-size: .8rem; font-weight: 700; }
.bubble-content { flex: 1; }
.bubble-text {
  background: var(--bg-hover);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: .75rem 1rem;
  font-size: .875rem;
  line-height: 1.6;
  white-space: pre-wrap;
}
.ai-bubble.user .bubble-text { background: rgba(31,111,235,.2); border-color: var(--blue-dark); }
.bubble-meta {
  font-size: .72rem;
  color: var(--text-dim);
  margin-top: .3rem;
  display: flex;
  align-items: center;
  gap: .5rem;
}
.ai-bubble.user .bubble-meta { justify-content: flex-end; }
.bubble-actions { display: flex; gap: .3rem; margin-top: .4rem; }
.ai-thinking { color: var(--text-muted); font-style: italic; font-size: .875rem; }

/* Robot thinking animation */
.ai-thinking-anim { display: flex; align-items: center; gap: .7rem; }
.ai-thinking-robot {
  font-size: 2rem;
  display: inline-block;
  animation: robot-bounce 0.6s ease-in-out infinite alternate;
  transform-origin: bottom center;
}
.ai-thinking-dots { display: flex; gap: .35rem; align-items: center; flex-direction: column; }
.ai-thinking-dots span {
  width: 8px; height: 8px; border-radius: 50%;
  background: var(--primary, #6366f1); display: inline-block;
  animation: dot-pulse 1.2s ease-in-out infinite;
}
.ai-thinking-dots span:nth-child(2) { animation-delay: .2s; }
.ai-thinking-dots span:nth-child(3) { animation-delay: .4s; }
.ai-thinking-label { font-size: .84rem; color: var(--text-muted); font-style: italic; }
@keyframes robot-bounce {
  from { transform: translateY(0) rotate(-5deg); }
  to   { transform: translateY(-7px) rotate(5deg); }
}
@keyframes dot-pulse {
  0%, 80%, 100% { transform: scale(0.5); opacity: .3; }
  40%           { transform: scale(1.3); opacity: 1; }
}

.ai-input-bar {
  display: flex;
  gap: .6rem;
  padding: .9rem 1rem;
  border-top: 1px solid var(--border);
  background: var(--bg);
}
.ai-text-input { flex: 1; background: var(--bg-card); }

/* ===== MODAL ===== */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,.7);
  z-index: 1000;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 1rem;
}
.modal-box {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: 12px;
  width: 100%;
  max-width: 560px;
  max-height: 90vh;
  display: flex;
  flex-direction: column;
  box-shadow: var(--shadow);
}
.modal-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 1rem 1.25rem;
  border-bottom: 1px solid var(--border);
}
.modal-head h3 { font-size: 1rem; font-weight: 600; }
.modal-body { padding: 1.25rem; overflow-y: auto; flex: 1; }
.modal-foot {
  display: flex;
  justify-content: flex-end;
  gap: .6rem;
  padding: .9rem 1.25rem;
  border-top: 1px solid var(--border);
}

/* ===== TOAST ===== */
#toasts {
  position: fixed;
  bottom: 1.5rem;
  right: 1.5rem;
  z-index: 9999;
  display: flex;
  flex-direction: column;
  gap: .5rem;
  pointer-events: none;
}
.toast {
  padding: .75rem 1.1rem;
  border-radius: var(--radius);
  font-size: .875rem;
  color: #fff;
  box-shadow: var(--shadow);
  animation: slideIn .25s ease;
  max-width: 320px;
}
.toast-success { background: var(--green-dark); }
.toast-error { background: var(--red-dark); }
.toast-info { background: var(--blue-dark); }
@keyframes slideIn { from { transform: translateX(100%); opacity: 0; } to { transform: translateX(0); opacity: 1; } }

/* ===== STATUS BADGES ===== */
.stato-programmato { color: var(--blue); }
.stato-in_corso { color: var(--yellow); }
.stato-completato { color: var(--green); }
.stato-annullato { color: var(--red); }

/* ===== SCROLLBAR ===== */
::-webkit-scrollbar { width: 6px; height: 6px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--border); border-radius: 3px; }
::-webkit-scrollbar-thumb:hover { background: var(--text-dim); }

/* ===== RESPONSIVE ===== */
@media (max-width: 900px) {
  .row-2 { grid-template-columns: 1fr; }
  .sidebar { width: 60px; min-width: 60px; }
  .sidebar-brand, .nav-label, .user-info, .sidebar-footer .btn-icon { display: none; }
  .sidebar-header { justify-content: center; }
  .nav-item { justify-content: center; padding: .7rem; }
  .sidebar-footer { justify-content: center; }
  .main { margin-left: 60px; max-width: calc(100vw - 60px); }
}
@media (max-width: 600px) {
  .stats-grid { grid-template-columns: 1fr 1fr; }
  .page-header { flex-direction: column; align-items: flex-start; }
  .page-actions { width: 100%; }
  .search-input, .select-input { max-width: 100%; width: 100%; }
}

/* ===== KNOWLEDGE BASE - TABS & CARDS ===== */
.kb-tabs {
  display: flex;
  flex-wrap: wrap;
  gap: .35rem;
  margin-bottom: .75rem;
  border-bottom: 1px solid var(--border);
  padding-bottom: .6rem;
}
.kb-tab {
  padding: .4rem .9rem;
  border: 1px solid var(--border);
  border-radius: 20px;
  background: var(--bg-card);
  color: var(--text-muted);
  font-size: .82rem;
  cursor: pointer;
  transition: all .15s;
}
.kb-tab:hover { border-color: var(--blue); color: var(--blue); }
.kb-tab.active { background: var(--blue); border-color: var(--blue); color: #fff; font-weight: 600; }
.kb-tab-count { font-size: .72rem; opacity: .8; margin-left: .2rem; }

.kb-subfilters {
  display: flex;
  align-items: center;
  gap: .75rem;
  flex-wrap: wrap;
  margin-bottom: .6rem;
}
.kb-type-pills { display: flex; flex-wrap: wrap; gap: .3rem; flex: 1; }
.kb-pill {
  padding: .3rem .75rem;
  border: 1px solid var(--border);
  border-radius: 14px;
  background: transparent;
  color: var(--text-muted);
  font-size: .78rem;
  cursor: pointer;
  transition: all .15s;
}
.kb-pill:hover { border-color: var(--primary); color: var(--text); }
.kb-pill.active { background: var(--primary,#6366f1); border-color: var(--primary,#6366f1); color: #fff; font-weight: 600; }

/* Cards grid */
.kb-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(340px, 1fr));
  gap: .85rem;
}
.kb-card {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: .9rem 1rem;
  display: flex;
  flex-direction: column;
  gap: .55rem;
  transition: border-color .15s, box-shadow .15s;
}
.kb-card:hover { border-color: var(--blue); box-shadow: 0 2px 12px rgba(0,0,0,.18); }
.kb-card-draft { border-left: 3px solid var(--yellow); }

.kb-card-header { display: flex; flex-direction: column; gap: .35rem; }
.kb-card-title { font-weight: 700; font-size: .95rem; line-height: 1.3; }
.kb-card-title mark.kb-hl { background: rgba(88,166,255,.25); color: var(--blue); border-radius: 3px; padding: 0 2px; }
.kb-attach-icon { font-size: .8rem; }
.kb-card-badges { display: flex; flex-wrap: wrap; gap: .3rem; }

.kb-card-preview {
  font-size: .82rem;
  color: var(--text-muted);
  line-height: 1.45;
  overflow: hidden;
  display: -webkit-box;
  -webkit-line-clamp: 3;
  -webkit-box-orient: vertical;
}
.kb-card-preview mark.kb-hl { background: rgba(88,166,255,.2); color: var(--blue); border-radius: 2px; padding: 0 1px; }

.kb-card-expand { margin-top: .2rem; }
.kb-card-full {
  white-space: pre-wrap;
  font-size: .82rem;
  color: var(--text);
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 7px;
  padding: .65rem .85rem;
  max-height: 320px;
  overflow-y: auto;
  line-height: 1.5;
}

.kb-card-footer {
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: .4rem;
  margin-top: .1rem;
}
.kb-usage { font-size: .75rem; color: var(--text-dim); }
.kb-card-actions { display: flex; flex-wrap: wrap; gap: .3rem; }

.kb-empty, .kb-card-loading {
  grid-column: 1 / -1;
  text-align: center;
  color: var(--text-muted);
  padding: 2.5rem;
  font-size: .9rem;
}

@media (max-width: 700px) {
  .kb-grid { grid-template-columns: 1fr; }
  .kb-tabs { gap: .25rem; }
}
