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

  :root {
    --bg:       #0c0c0c;
    --bg1:      #111111;
    --bg2:      #181818;
    --bg3:      #222222;
    --border:   rgba(255,255,255,0.07);
    --border2:  rgba(255,255,255,0.13);
    --accent:   #ffffff;
    --accent2:  rgba(255,255,255,0.75);
    --dim:      rgba(255,255,255,0.35);
    --text:     #f0f0f0;
    --text2:    rgba(240,240,240,0.7);
    --text3:    rgba(240,240,240,0.35);
    --green:    #6ee7b7;
    --red:      #fca5a5;
    --teal:     #5eead4;
    --font-mono: 'Saira', sans-serif;
    --font-ui:   'Space Grotesk', sans-serif;
    --radius:   6px;
  }

  html, body { height: 100%; background: var(--bg); color: var(--text); font-family: var(--font-mono); overflow: hidden; }

  /* scanline effect */
  body::after {
    content: '';
    position: fixed; inset: 0; pointer-events: none; z-index: 9999;
    background: repeating-linear-gradient(0deg, transparent, transparent 2px, rgba(0,0,0,0.015) 2px, rgba(0,0,0,0.015) 4px);
  }

  #app {
    display: grid;
    grid-template-rows: 52px 1fr auto;
    height: 100vh; max-width: 860px;
    margin: 0 auto;
  }

  /* ── Header ── */
  header {
    display: flex; align-items: center; justify-content: space-between;
    padding: 0 24px;
    border-bottom: 1px solid var(--border);
  }
  .logo {
    font-family: var(--font-ui); font-weight: 700; font-size: 14px;
    letter-spacing: 0.05em; color: var(--text); text-transform: uppercase;
    display: flex; align-items: center; gap: 8px;
  }
  .logo-dot { width: 6px; height: 6px; background: var(--teal); border-radius: 50%; }
  .logo-sub { font-family: var(--font-mono); font-weight: 300; font-size: 11px; color: var(--text3); letter-spacing: 0.1em; }

  .header-right { display: flex; align-items: center; gap: 14px; }
  .model-select {
    background: transparent; border: 1px solid var(--border2);
    color: var(--text3); font-family: var(--font-mono); font-size: 10px;
    padding: 4px 8px; border-radius: var(--radius); cursor: pointer;
    outline: none; transition: all .2s; letter-spacing: 0.04em;
    -webkit-appearance: none; appearance: none;
    padding-right: 20px;
    background-image: url("data:image/svg+xml,%3Csvg width='10' height='6' viewBox='0 0 10 6' fill='none' xmlns='http://www.w3.org/2000/svg'%3E%3Cpath d='M1 1L5 5L9 1' stroke='rgba(255,255,255,0.3)' stroke-width='1.5' stroke-linecap='round'/%3E%3C/svg%3E");
    background-repeat: no-repeat; background-position: right 7px center;
  }
  .model-select:hover, .model-select:focus { border-color: var(--border2); color: var(--text2); }
  .model-select option { background: #111; color: #f0f0f0; }

  .status {
    display: flex; align-items: center; gap: 5px;
    font-size: 10px; color: var(--text3); letter-spacing: 0.06em;
  }
  .status-dot {
    width: 6px; height: 6px; border-radius: 50%;
    background: var(--green); box-shadow: 0 0 5px var(--green);
    animation: pulse 3s ease-in-out infinite;
  }
  @keyframes pulse { 0%,100%{opacity:1} 50%{opacity:.4} }

  /* ── Messages ── */
  #messages {
  overflow-y: auto; padding: 28px 24px;
  display: flex; flex-direction: column; gap: 0;
  scrollbar-width: thin; scrollbar-color: rgba(255,255,255,0.08) transparent;
  }
  #messages::-webkit-scrollbar { width: 3px; }
  #messages::-webkit-scrollbar-thumb { background: rgba(255,255,255,0.08); }

  .msg { display: flex; gap: 0; animation: fadeUp .2s ease; margin-bottom: 24px; }
  @keyframes fadeUp { from{opacity:0;transform:translateY(4px)} to{opacity:1;transform:translateY(0)} }

  /* AI a sinistra */
  .msg.ai { flex-direction: row; }

  .msg-gutter { width: 56px; flex-shrink: 0; padding-top: 1px; }
  .msg-label { font-size: 9px; letter-spacing: 0.1em; text-transform: uppercase; font-weight: 600; font-family: var(--font-ui); }
  .msg.ai .msg-label { color: var(--teal); }

  /* USER a destra */
  .msg.user { flex-direction: row-reverse; }
  .msg.user .msg-gutter { display: none; }
  .msg.user .msg-body { align-items: flex-end; }

  .msg-body { flex: 1; min-width: 0; display: flex; flex-direction: column; }

  .msg-text { font-size: 13.5px; line-height: 1.8; color: var(--text2); word-break: break-word; }

  .msg.user .msg-text {
    color: var(--text);
    padding: 10px 14px;
    background: var(--bg2);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    display: inline-block;
    max-width: 75%;
  }

  .msg-text pre {
    background: var(--bg1); border: 1px solid var(--border2);
    border-left: 2px solid rgba(255,255,255,0.2);
    border-radius: var(--radius);
    padding: 14px 16px; margin: 10px 0; overflow-x: auto;
    font-size: 12px; line-height: 1.65;
    scrollbar-width: thin; scrollbar-color: var(--border) transparent;
  }
  .msg-text code:not(pre code) {
    background: rgba(255,255,255,0.06);
    color: var(--teal); padding: 1px 5px; border-radius: 3px; font-size: 12px;
  }
  .msg-text p { margin-bottom: 8px; }
  .msg-text p:last-child { margin-bottom: 0; }
  .msg-text strong { color: var(--text); font-weight: 500; }

  /*Gestione Stile per rendering markdown*/
  .msg-text h1, .msg-text h2, .msg-text h3{
    color: var(--text);
    font-family: var(--font-ui);
    margin:12px 0 6px;
    font-weight: 600;
  }

  .msg-text h1{ font-size: 16px; }
  .msg-text h2{ font-size: 14px; }
  .msg-text h3{ font-size: 13px; }
  .msg-text ul, .msg-text ol{
    padding-left: 20px;
    margin: 6px 0;
  }
  .msg-text li {
    margin-bottom: 3px;
    line-height: 1.7;
  }
  .msg-text a{
    color: var(--teal); text-decoration: none;
  }
  .msg-text a:hover{
    text-decoration: underline;
  }
  .msg-text blockquote{
    border-left: 2px solid rgba(255, 255, 255, 0.15);
    padding-left: 12px;
    color: var(--text3);
    margin: 8px 0;
    font-style: italic;
  }
  .msg-text table{
    border-collapse: collapse;
    width: 100%;
    margin: 10px 0;
    font-size: 12px;
  }
  .msg-text th, .msg-text td{
    border: 1px solid var(--border2);
    padding: 6px 10px;
    text-align: left;
  }
  .msg-text th{
    background: var(--bg2);
    color: var(--text);
  }
  .msg-text hr{
    border: none;
    border-top: 1px solid var(--border);
    margin: 12px 0;
  }
  /* streaming cursor */
  .cursor {
    display: inline-block; width: 7px; height: 13px;
    background: var(--teal); animation: blink .8s infinite;
    vertical-align: middle; margin-left: 2px; border-radius: 1px;
  }
  @keyframes blink { 0%,49%{opacity:1} 50%,100%{opacity:0} }

  /* divider line */
  .msg-divider {
    border: none; border-top: 1px solid var(--border);
    margin: 0 0 28px; opacity: 0.5;
  }

  /* empty state */
  .empty-state {
    flex: 1; display: flex; flex-direction: column;
    align-items: center; justify-content: center; gap: 8px;
    color: var(--text3); font-size: 12px; letter-spacing: 0.05em;
    text-align: center;
  }
  .empty-state .big { font-family: var(--font-ui); font-size: 22px; font-weight: 700; color: rgba(255,255,255,0.06); letter-spacing: 0.15em; text-transform: uppercase; }

  /* ── Input ── */
  #input-area {
    padding: 12px 24px 20px;
    border-top: 1px solid var(--border);
  }
  .input-top {
    display: flex; align-items: center; justify-content: space-between;
    margin-bottom: 8px;
  }
  #sys-toggle {
    font-size: 10px; color: var(--text3); background: none; border: none;
    cursor: pointer; font-family: var(--font-mono); letter-spacing: 0.05em;
    transition: color .2s; padding: 0;
  }
  #sys-toggle:hover { color: var(--text2); }
  #clear-btn {
    font-size: 10px; color: var(--text3); background: none; border: none;
    cursor: pointer; font-family: var(--font-mono); letter-spacing: 0.05em;
    transition: color .2s; padding: 0;
  }
  #clear-btn:hover { color: var(--red); }

  #sys-panel { display: none; margin-bottom: 8px; }
  #sys-panel.open { display: block; }
  #sys-input {
    width: 100%; background: var(--bg1);
    border: 1px solid var(--border); border-radius: var(--radius);
    color: var(--text2); font-family: var(--font-mono); font-size: 12px;
    padding: 8px 12px; resize: vertical; min-height: 56px; outline: none;
    transition: border-color .2s; line-height: 1.6;
  }
  #sys-input:focus { border-color: var(--border2); }

  .input-row { display: flex; gap: 8px; align-items: flex-end; }
  #input {
    flex: 1; background: var(--bg1);
    border: 1px solid var(--border); border-radius: var(--radius);
    color: var(--text); font-family: var(--font-mono); font-size: 13px;
    padding: 10px 14px; resize: none; outline: none;
    line-height: 1.65; max-height: 160px; min-height: 42px;
    transition: border-color .2s; scrollbar-width: none;
  }
  #input:focus { border-color: var(--border2); }
  #input::placeholder { color: var(--text3); }

  #send-btn {
    width: 42px; height: 42px; flex-shrink: 0;
    background: var(--text); border: none; border-radius: var(--radius);
    color: #0c0c0c; cursor: pointer;
    display: flex; align-items: center; justify-content: center;
    transition: opacity .2s, transform .1s;
  }
  #send-btn:hover { opacity: 0.85; }
  #send-btn:active { transform: scale(.95); }
  #send-btn:disabled { opacity: 0.2; cursor: not-allowed; }

  /* Bottone immagine */
  #img-btn {
    width: 42px; height: 42px; flex-shrink: 0;
    background: rgba(94,234,212,0.08);
    border: 1px solid rgba(94,234,212,0.2);
    border-radius: var(--radius);
    color: var(--teal);
    cursor: pointer;
    display: flex; align-items: center; justify-content: center;
    transition: all .2s;
    position: relative;
  }
  #img-btn:hover {
    background: rgba(94,234,212,0.14);
    border-color: rgba(94,234,212,0.4);
  }
  #img-btn.has-image {
    background: rgba(94,234,212,0.15);
    border-color: rgba(94,234,212,0.5);
  }
  .img-badge {
    position: absolute; top: -5px; right: -5px;
    width: 14px; height: 14px;
    background: var(--teal); color: #0c0c0c;
    border-radius: 50%; font-size: 9px; font-weight: 700;
    display: flex; align-items: center; justify-content: center;
    font-family: var(--font-mono);
  }

  /* Preview immagine sopra l'input */
  #img-preview {
    display: flex; align-items: center; gap: 8px;
    padding: 6px 0; margin-bottom: 4px;
  }
  #img-preview img {
    height: 40px; border-radius: 4px;
    border: 1px solid var(--border2);
    object-fit: cover;
  }
  #img-preview span {
    font-size: 10px; color: var(--text3);
    flex: 1; overflow: hidden;
    text-overflow: ellipsis; white-space: nowrap;
  }
  #img-preview button {
    background: none; border: none;
    color: var(--text3); cursor: pointer;
    font-size: 12px; padding: 2px 4px;
    transition: color .2s;
  }
  #img-preview button:hover { color: var(--red); }

  .input-hint {
    font-size: 10px; color: var(--text3); margin-top: 6px;
    display: flex; justify-content: space-between; letter-spacing: 0.03em;
  }
  kbd {
    background: var(--bg3); border: 1px solid var(--border2);
    border-radius: 3px; padding: 1px 4px; font-family: var(--font-mono); font-size: 9px;
    color: var(--text3);
  }

  .msg-logo {
    height: 32px; width: auto; object-fit: contain;
    border-radius: 5px;
    opacity: 0.7; margin-top: 3px; display: block;
  }
  .logo-img {
    height: 24px; width: auto; object-fit: contain;
    filter: brightness(0) invert(1); /* bianco — rimuovi se il tuo logo è già chiaro */
  }
  .empty-logo {
    height: 64px; width: auto; object-fit: contain; margin-bottom: 4px;
    filter: brightness(0) invert(1); opacity: 0.08;
  }

  @media(max-width:600px) {
    #app { max-width: 100%; }
    header, #messages, #input-area { padding-left: 14px; padding-right: 14px; }
    .msg-gutter { width: 40px; }
  }