/* ============================================
   LIBAAI - Design System
   Palette: #000000, #1a1a1a, #ffffff, #8c52ff
   ============================================ */

:root {
  --bg-primary: #000000;
  --bg-secondary: #1a1a1a;
  --text-primary: #ffffff;
  --text-secondary: #a0a0a0;
  --accent: #8c52ff;
  --border: #2a2a2a;
  --shadow: 0 2px 8px rgba(0, 0, 0, 0.5);
  --font-title: 'Sora', sans-serif;
  --font-ui: 'Manrope', sans-serif;
  --font-body: 'Inter', sans-serif;
}

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

html {
  overflow-x: hidden !important;
  width: 100%;
  max-width: 100%;
  position: relative;
}

body {
  margin: 0;
  padding: 0;
  background: var(--bg-primary);
  color: var(--text-primary);
  font-family: var(--font-body);
  font-size: 16px;
  line-height: 1.5;
  min-height: 100vh;
  overflow-x: hidden !important;
  width: 100%;
  max-width: 100%;
  position: relative;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

/* Typography */
h1, h2, h3, h4, h5, h6 {
  font-family: var(--font-title);
}

button, input, select, textarea {
  font-family: var(--font-ui);
}

/* Prevent horizontal overflow for images and media */
img, video, iframe, embed, object {
  max-width: 100%;
  height: auto;
  box-sizing: border-box;
}

/* Prevent horizontal overflow for tables */
table {
  width: 100%;
  max-width: 100%;
  table-layout: auto;
  box-sizing: border-box;
}

/* Prevent horizontal overflow for pre and code blocks */
pre, code {
  max-width: 100%;
  overflow-x: auto;
  word-wrap: break-word;
  box-sizing: border-box;
}

/* ============================================
   HEADER
   ============================================ */

header {
  background: var(--bg-secondary);
  border-bottom: 1px solid var(--border);
  padding: 10px 20px;
  flex-shrink: 0;
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  z-index: 10;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100%;
  max-width: 100%;
  overflow-x: hidden !important;
  box-sizing: border-box;
}

.header-content {
  display: flex;
  align-items: center;
  gap: 8px;
  max-width: 100%;
  width: 100%;
  justify-content: center;
  position: relative;
}

.header-content > div:first-of-type {
  display: flex;
  align-items: center;
  gap: 8px;
}

.avatar {
  width: 72px;
  height: 72px;
  border-radius: 12px;
  object-fit: cover;
  border: 2px solid var(--border);
  flex-shrink: 0;
}

.new-chat-btn {
  background: var(--bg-primary);
  border: 1px solid var(--border);
  border-radius: 8px;
  color: var(--text-primary);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 6px;
  width: 32px;
  height: 32px;
  transition: all 0.2s ease;
  flex-shrink: 0;
  position: absolute;
  right: 20px;
  font-family: var(--font-ui);
}

.new-chat-btn:hover {
  background: var(--bg-secondary);
  border-color: var(--accent);
  transform: scale(1.05);
}

.new-chat-btn:active {
  transform: scale(0.95);
}

.new-chat-btn svg {
  width: 18px;
  height: 18px;
}

header h1 {
  font-family: var(--font-title);
  font-size: 15px;
  font-weight: 600;
  color: var(--text-primary);
  margin: 0;
  letter-spacing: -0.01em;
}

header .subtitle {
  font-size: 13px;
  color: var(--text-secondary);
  margin-top: 2px;
}

/* ============================================
   CHAT AREA
   ============================================ */

#chat {
  flex: 1;
  overflow-y: auto;
  overflow-x: hidden !important;
  padding: 16px 16px;
  display: flex;
  flex-direction: column;
  gap: 6px;
  scroll-behavior: smooth;
  width: 100%;
  max-width: 100%;
  box-sizing: border-box;
  position: relative;
  margin-top: 92px;
  margin-bottom: 80px;
}

#chat::-webkit-scrollbar {
  width: 4px;
}

#chat::-webkit-scrollbar-track {
  background: transparent;
}

#chat::-webkit-scrollbar-thumb {
  background: var(--border);
  border-radius: 2px;
}

#chat::-webkit-scrollbar-thumb:hover {
  background: #3a3a3a;
}

/* ============================================
   MESSAGES
   ============================================ */

.msg {
  max-width: 75%;
  min-width: 120px;
  width: fit-content;
  padding: 8px 12px;
  border-radius: 8px;
  font-size: 15px;
  line-height: 1.2;
  word-wrap: break-word;
  overflow-wrap: break-word;
  word-break: break-word;
  animation: slideIn 0.2s ease-out;
  position: relative;
  text-align: left;
  display: inline-block;
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.3);
  box-sizing: border-box;
  overflow-x: hidden !important;
  overflow-y: visible;
}

@keyframes slideIn {
  from {
    opacity: 0;
    transform: translateY(4px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.msg.ai {
  background: var(--bg-secondary);
  border: 1px solid var(--border);
  align-self: flex-start;
  color: var(--text-primary);
  text-align: left;
  border-top-left-radius: 4px;
}

/* Messaggio di benvenuto sempre visibile */
#welcomeMsg {
  display: block !important;
}

.msg.ai .msg-author {
  display: block;
  font-family: var(--font-title);
  font-size: 16px;
  font-weight: 700;
  color: var(--accent);
  margin-bottom: 4px;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  text-align: left;
  width: 100%;
}

.msg.user {
  background: var(--accent);
  border: 1px solid var(--accent);
  align-self: flex-end;
  color: var(--text-primary);
  text-align: left;
  border-top-right-radius: 4px;
}

.msg.user .msg-author {
  display: block;
  font-family: var(--font-title);
  font-size: 16px;
  font-weight: 700;
  color: rgba(255, 255, 255, 0.95);
  margin-bottom: 4px;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  text-align: right;
  width: 100%;
}

.msg-timestamp {
  font-size: 11px;
  color: var(--text-secondary);
  margin-top: 6px;
  opacity: 0.7;
}

.msg.user .msg-timestamp {
  text-align: right;
}

.msg.ai .msg-timestamp {
  text-align: left;
}

.msg.loading {
  opacity: 0.6;
  font-style: italic;
}

.msg strong {
  color: var(--accent);
  font-weight: 600;
}

.msg em {
  font-style: italic;
  color: var(--text-secondary);
}

.msg ul,
.msg ol {
  margin: 4px 0;
  padding-left: 20px;
  list-style-position: outside;
  text-align: left;
}

.msg li {
  margin: 2px 0;
  text-align: left;
}

.msg p {
  margin: 2px 0;
}

.msg p:first-child {
  margin-top: 0;
}

.msg p:last-child {
  margin-bottom: 0;
}

/* ============================================
   STATUS BAR
   ============================================ */

.status {
  padding: 10px 20px;
  font-size: 13px;
  color: var(--text-secondary);
  text-align: center;
  background: var(--bg-secondary);
  border-top: 1px solid var(--border);
  flex-shrink: 0;
  transition: color 0.2s ease;
  display: flex;
  align-items: center;
  justify-content: center;
  position: fixed;
  bottom: 80px;
  left: 0;
  right: 0;
  z-index: 9;
}

.status.error {
  color: var(--accent);
}

.status.success {
  color: var(--accent);
}

/* ============================================
   FOOTER / INPUT AREA
   ============================================ */

footer {
  background: var(--bg-secondary);
  border-top: 1px solid var(--border);
  padding: 16px 24px;
  flex-shrink: 0;
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  z-index: 10;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100%;
  max-width: 100%;
  overflow-x: hidden !important;
  box-sizing: border-box;
}

.footer-content {
  display: flex;
  gap: 12px;
  max-width: 100%;
  width: 100%;
  align-items: center;
  box-sizing: border-box;
  overflow-x: hidden !important;
  min-width: 0;
}

footer input {
  flex: 1;
  padding: 14px 18px;
  background: var(--bg-primary);
  border: 1px solid var(--border);
  border-radius: 10px;
  color: var(--text-primary);
  font-size: 16px;
  font-family: var(--font-ui);
  outline: none;
  transition: border-color 0.2s ease;
  min-width: 0;
  max-width: 100%;
  box-sizing: border-box;
  overflow-x: hidden;
}

footer input:focus {
  border-color: var(--accent);
}

footer input::placeholder {
  color: var(--text-secondary);
}

footer input:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

footer button {
  padding: 14px 28px;
  background: var(--accent);
  border: none;
  border-radius: 10px;
  color: var(--text-primary);
  font-size: 15px;
  font-weight: 600;
  font-family: var(--font-ui);
  cursor: pointer;
  transition: opacity 0.2s ease, transform 0.1s ease;
  white-space: nowrap;
  flex-shrink: 0;
}

footer button:hover:not(:disabled) {
  opacity: 0.9;
  transform: scale(0.98);
}

footer button:active:not(:disabled) {
  transform: scale(0.96);
}

footer button:disabled {
  opacity: 0.5;
  cursor: not-allowed;
  transform: none;
}

/* ============================================
   RESPONSIVE - MOBILE FIRST
   ============================================ */

@media (max-width: 768px) {
  header {
    padding: 14px 16px;
  }

  .avatar {
    width: 56px;
    height: 56px;
  }

  header h1 {
    font-size: 16px;
  }

  #chat {
    padding: 16px 12px;
    gap: 8px;
    margin-top: 84px;
    margin-bottom: 80px;
  }

  .msg {
    max-width: 80%;
    min-width: 100px;
    width: fit-content;
    padding: 12px 14px;
    font-size: 14px;
  }

  .msg.ai .msg-author {
    font-size: 14px;
    margin-bottom: 4px;
  }

  .msg.user .msg-author {
    font-size: 14px;
    margin-bottom: 4px;
  }

  footer {
    padding: 14px 16px;
  }

  .footer-content {
    gap: 8px;
  }

  footer input {
    padding: 10px 14px;
    font-size: 16px;
  }

  footer button {
    padding: 10px 20px;
    font-size: 14px;
  }
}

@media (max-width: 480px) {
  header {
    padding: 12px 14px;
  }

  .avatar {
    width: 52px;
    height: 52px;
  }

  #chat {
    margin-top: 76px;
    margin-bottom: 80px;
  }

  .new-chat-btn {
    width: 28px;
    height: 28px;
    padding: 5px;
    right: 14px;
  }

  .new-chat-btn svg {
    width: 16px;
    height: 16px;
  }

  header h1 {
    font-size: 14px;
  }

  #chat {
    padding: 12px 10px;
    gap: 8px;
  }

  .msg {
    max-width: 85%;
    min-width: 80px;
    width: fit-content;
    padding: 10px 12px;
    font-size: 14px;
  }

  .msg.ai .msg-author {
    font-size: 13px;
    margin-bottom: 4px;
  }

  .msg.user .msg-author {
    font-size: 13px;
    margin-bottom: 4px;
  }

  footer {
    padding: 12px 14px;
  }

  footer input {
    padding: 10px 12px;
    font-size: 16px;
  }

  footer button {
    padding: 10px 18px;
  }
}

/* ============================================
   PWA SAFE AREAS
   ============================================ */

@media (display-mode: standalone) {
  body {
    padding-top: env(safe-area-inset-top);
    padding-bottom: env(safe-area-inset-bottom);
  }

  header {
    padding-top: calc(16px + env(safe-area-inset-top));
  }

  footer {
    padding-bottom: calc(16px + env(safe-area-inset-bottom));
  }
}

/* ============================================
   UTILITY
   ============================================ */

@media (prefers-reduced-motion: reduce) {
  * {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
  }
}
