/* Aurora — main stylesheet
   Palette: smeraldo + ambra su sfondo notte
*/

:root {
  --inset-top: env(safe-area-inset-top, 0px);
  --inset-bottom: env(safe-area-inset-bottom, 0px);
  --inset-left: env(safe-area-inset-left, 0px);
  --inset-right: env(safe-area-inset-right, 0px);

  --bg: #0A1816;
  --bg-elev: #10211D;
  --bg-card: #162B27;
  --bg-input: #0E1F1B;

  --smeraldo: #1FA888;
  --smeraldo-scuro: #0E5C4D;
  --smeraldo-chiaro: #34D6B0;

  --oro: #D4A75B;
  --ambra: #E5C07B;
  --ciano: #5BC0EB;
  --error: #E5604A;

  --text: #E8F0EE;
  --text-dim: #A0B5B0;
  --text-faint: #6B7F7B;
}

* { box-sizing: border-box; }

html, body {
  margin: 0; padding: 0; height: 100%; width: 100%;
  background: var(--bg); color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', system-ui, sans-serif;
  font-size: 16px; line-height: 1.5;
  overflow: hidden;
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior: none;
}
body { display: flex; flex-direction: column; }
#app-root { flex: 1; display: flex; flex-direction: column; overflow: hidden; position: relative; }

/* ============ LOADING ============ */
#loading-screen {
  position: absolute; inset: 0;
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  background: var(--bg); z-index: 100; gap: 24px;
}
.loading-ring {
  width: 56px; height: 56px;
  border: 4px solid var(--bg-card); border-top-color: var(--smeraldo);
  border-radius: 50%; animation: spin 0.8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }
.loading-text { font-size: 18px; color: var(--smeraldo-chiaro); letter-spacing: 1px; }

/* ============ VIEW ============ */
.view { flex: 1; display: flex; flex-direction: column; height: 100%; overflow: hidden; background: var(--bg); }

/* ============ TOPBAR ============ */
.topbar {
  display: flex; align-items: center; justify-content: space-between;
  padding: 8px 12px; padding-top: calc(8px + var(--inset-top));
  border-bottom: 1px solid rgba(31,168,136,0.15);
  background: linear-gradient(180deg, rgba(16,33,29,0.95), rgba(10,24,22,0.85));
  flex-shrink: 0; min-height: calc(54px + var(--inset-top));
}
.topbar-title {
  font-size: 17px; font-weight: 500; letter-spacing: 0.3px;
  color: var(--smeraldo-chiaro); display: flex; align-items: center; gap: 8px; padding-left: 4px;
}
.topbar-title-text {
  font-size: 17px; font-weight: 500; color: var(--smeraldo-chiaro); flex: 1; text-align: center;
}
.topbar-dot {
  width: 8px; height: 8px; border-radius: 50%;
  background: var(--smeraldo); box-shadow: 0 0 8px var(--smeraldo);
}
.topbar-actions { display: flex; gap: 2px; min-width: 38px; }
.topbar-btn {
  width: 38px; height: 38px; border-radius: 10px;
  background: transparent; border: none; color: var(--text-dim);
  cursor: pointer; display: flex; align-items: center; justify-content: center;
  transition: background 120ms; padding: 0;
}
.topbar-btn:active { background: rgba(31,168,136,0.18); }
.topbar-btn svg { width: 22px; height: 22px; }
.topbar-btn[data-active="true"] { color: var(--smeraldo-chiaro); }

/* ============ CHAT ============ */
.chat-area {
  flex: 1; overflow-y: auto; padding: 16px 16px 8px;
  -webkit-overflow-scrolling: touch; overscroll-behavior: contain;
  padding-left: max(16px, var(--inset-left)); padding-right: max(16px, var(--inset-right));
}
.msg {
  margin-bottom: 14px; display: flex; flex-direction: column; max-width: 100%;
  animation: msgIn 200ms ease-out;
}
@keyframes msgIn {
  from { opacity: 0; transform: translateY(8px); }
  to { opacity: 1; transform: translateY(0); }
}
.msg-user { align-items: flex-end; }
.msg-user .bubble {
  background: var(--smeraldo-scuro); color: var(--text);
  border-radius: 18px 4px 18px 18px; padding: 10px 14px;
  max-width: 85%; word-wrap: break-word; white-space: pre-wrap;
}
.msg-andrew { align-items: flex-start; }
.msg-andrew .bubble {
  background: transparent; color: var(--text); padding: 0; max-width: 100%; line-height: 1.65;
}
.msg-andrew .bubble p { margin: 0 0 0.7em; }
.msg-andrew .bubble p:last-child { margin-bottom: 0; }
.msg-andrew .bubble strong { color: var(--smeraldo-chiaro); font-weight: 600; }
.msg-andrew .bubble em { color: var(--ambra); font-style: italic; }
.msg-andrew .bubble .quote { color: var(--ambra); }
.msg img.attached { max-width: 240px; max-height: 240px; border-radius: 10px; margin-top: 6px; }
.msg-error .bubble {
  background: rgba(229,96,74,0.12); border: 1px solid rgba(229,96,74,0.4);
  color: var(--error); border-radius: 12px; padding: 10px 14px; max-width: 100%;
}

/* TTS per-message button */
.msg-tts-btn {
  margin-top: 6px;
  width: 32px; height: 32px;
  border-radius: 8px;
  background: var(--bg-card);
  border: 1px solid rgba(31,168,136,0.2);
  color: var(--text-faint);
  cursor: pointer;
  display: flex; align-items: center; justify-content: center;
  transition: all 120ms;
  padding: 0;
}
.msg-tts-btn:hover, .msg-tts-btn:active {
  color: var(--smeraldo-chiaro);
  border-color: var(--smeraldo);
  background: rgba(31,168,136,0.1);
}

.typing { display: inline-flex; gap: 4px; align-items: center; }
.typing span {
  width: 6px; height: 6px; background: var(--smeraldo);
  border-radius: 50%; animation: typing 1.2s infinite;
}
.typing span:nth-child(2) { animation-delay: 0.15s; }
.typing span:nth-child(3) { animation-delay: 0.3s; }
@keyframes typing {
  0%, 60%, 100% { opacity: 0.3; transform: translateY(0); }
  30% { opacity: 1; transform: translateY(-3px); }
}

/* ============ WELCOME ============ */
.welcome { text-align: center; padding: 48px 32px 24px; color: var(--text-dim); }
.welcome h1 {
  color: var(--smeraldo-chiaro); font-weight: 400; font-size: 22px;
  margin: 0 0 12px; letter-spacing: 0.5px;
}
.welcome p { margin: 0; line-height: 1.7; font-size: 14px; color: var(--text-faint); }
.welcome-warn {
  margin-top: 18px; padding: 10px 14px;
  background: rgba(229,96,74,0.12); border: 1px solid rgba(229,96,74,0.3);
  border-radius: 10px; color: var(--error); font-size: 13px;
}

/* ============ INPUT BAR ============ */
.input-bar {
  flex-shrink: 0; padding: 8px 10px;
  padding-bottom: calc(8px + var(--inset-bottom));
  padding-left: max(10px, var(--inset-left)); padding-right: max(10px, var(--inset-right));
  background: linear-gradient(0deg, rgba(10,24,22,0.98), rgba(16,33,29,0.92));
  border-top: 1px solid rgba(31,168,136,0.15);
}
.attached-images { display: flex; gap: 8px; flex-wrap: wrap; padding: 0 4px 8px; max-height: 70px; overflow-x: auto; }
.attached-images:empty { display: none; }
.attached-images img { height: 56px; border-radius: 8px; border: 1px solid rgba(31,168,136,0.4); cursor: pointer; }
.input-row { display: flex; align-items: flex-end; gap: 6px; }
.icon-btn {
  width: 42px; height: 42px; border-radius: 12px;
  background: var(--bg-input); border: 1px solid rgba(31,168,136,0.25);
  color: var(--text-dim); flex-shrink: 0;
  display: flex; align-items: center; justify-content: center;
  cursor: pointer; transition: all 120ms; padding: 0;
}
.icon-btn:active { background: var(--smeraldo-scuro); color: var(--text); }
.icon-btn svg { width: 22px; height: 22px; }
.icon-btn[data-active="true"] { background: var(--smeraldo); color: var(--text); border-color: var(--smeraldo-chiaro); }
.icon-btn.recording { background: var(--error); color: white; animation: pulse 1.2s infinite; }
@keyframes pulse { 0%, 100% { transform: scale(1); } 50% { transform: scale(1.04); } }
#input-text {
  flex: 1; min-height: 42px; max-height: 160px; padding: 11px 14px;
  background: var(--bg-input); border: 1px solid rgba(31,168,136,0.25);
  border-radius: 12px; color: var(--text); font: inherit; resize: none; outline: none;
}
#input-text:focus { border-color: var(--smeraldo); }
.send-btn {
  width: 46px; height: 42px; border-radius: 12px;
  background: var(--smeraldo); border: none; color: white; flex-shrink: 0;
  display: flex; align-items: center; justify-content: center;
  cursor: pointer; transition: all 150ms; padding: 0;
}
.send-btn:disabled { opacity: 0.4; }
.send-btn:active:not(:disabled) { transform: scale(0.94); }
.send-btn svg { width: 22px; height: 22px; }
.send-btn.stopping { background: var(--error); }

/* ============ TABS (Memoria) ============ */
.tabs {
  display: flex; border-bottom: 1px solid rgba(31,168,136,0.15);
  background: var(--bg-elev); flex-shrink: 0;
}
.tab {
  flex: 1; padding: 12px 8px; background: transparent; border: none;
  color: var(--text-dim); font: inherit; font-size: 14px; cursor: pointer;
  border-bottom: 2px solid transparent; transition: all 150ms;
}
.tab.active { color: var(--smeraldo-chiaro); border-bottom-color: var(--smeraldo); }
.tab-content { flex: 1; overflow-y: auto; padding: 8px 0; -webkit-overflow-scrolling: touch; }
.entry-list-item {
  padding: 14px 16px; border-bottom: 1px solid rgba(31,168,136,0.08);
  cursor: pointer; transition: background 120ms;
}
.entry-list-item:active { background: rgba(31,168,136,0.1); }
.entry-list-item-date { color: var(--smeraldo-chiaro); font-size: 13px; font-weight: 500; }
.entry-list-item-title { color: var(--text); font-size: 16px; margin-top: 2px; }
.entry-list-item-preview {
  color: var(--text-faint); font-size: 13px; margin-top: 4px; line-height: 1.4;
  display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden;
}
.tab-empty { text-align: center; padding: 48px 24px; color: var(--text-faint); }
.constitution-view {
  padding: 16px; white-space: pre-wrap;
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', system-ui, sans-serif;
  font-size: 13px; line-height: 1.6; color: var(--text-dim);
}

/* ============ EDITOR ============ */
.editor-area { flex: 1; display: flex; overflow: hidden; }
#editor-text {
  flex: 1; width: 100%; background: var(--bg); color: var(--text);
  border: none; outline: none; padding: 16px;
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', system-ui, sans-serif;
  font-size: 15px; line-height: 1.7; resize: none;
}

/* ============ DRAFT ============ */
.draft-area { flex: 1; display: flex; flex-direction: column; padding: 16px; overflow: hidden; }
.draft-status { color: var(--text-dim); font-size: 14px; margin-bottom: 12px; }
.draft-loader { display: flex; justify-content: center; padding: 32px 0; }
#draft-text {
  flex: 1; background: var(--bg-card); color: var(--text);
  border: 1px solid rgba(31,168,136,0.2); border-radius: 12px; padding: 14px;
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', system-ui, sans-serif;
  font-size: 15px; line-height: 1.65; resize: none; outline: none; margin-bottom: 12px;
}
.draft-counters { display: flex; gap: 12px; flex-wrap: wrap; margin-bottom: 12px; font-size: 12px; }
.counter { background: var(--bg-card); padding: 4px 10px; border-radius: 6px; color: var(--text-dim); }
.counter.over-limit { background: rgba(229,96,74,0.18); color: var(--error); }
.draft-actions { display: flex; gap: 10px; }
.btn-primary, .btn-secondary {
  flex: 1; padding: 12px; border-radius: 12px; border: none;
  font: inherit; font-size: 15px; cursor: pointer; transition: all 150ms;
}
.btn-primary { background: var(--smeraldo); color: white; }
.btn-primary:active { transform: scale(0.97); }
.btn-primary:disabled { opacity: 0.4; }
.btn-secondary { background: var(--bg-card); color: var(--smeraldo-chiaro); border: 1px solid rgba(31,168,136,0.3); }

/* ============ CONFIG ============ */
.config-area {
  flex: 1; overflow-y: auto; padding: 16px;
  padding-bottom: calc(16px + var(--inset-bottom));
}
.config-section { margin-bottom: 28px; }
.config-section-title {
  color: var(--smeraldo); font-size: 12px; letter-spacing: 1.2px;
  text-transform: uppercase; margin: 20px 0 8px; font-weight: 600;
}
.config-section-title:first-child { margin-top: 0; }
.config-label { color: var(--text-dim); font-size: 13px; margin: 10px 0 4px; display: block; }
.config-input, .config-textarea, .config-area select {
  width: 100%; background: var(--bg-card); color: var(--text);
  border: 1px solid rgba(31,168,136,0.2); border-radius: 8px;
  padding: 10px 12px; font: inherit; font-size: 14px; outline: none; display: block;
  -webkit-appearance: none;
}
.config-input:focus, .config-textarea:focus, .config-area select:focus { border-color: var(--smeraldo); }
.config-textarea { resize: vertical; min-height: 80px; font-family: ui-monospace, monospace; font-size: 12px; }
.config-hint { color: var(--text-faint); font-size: 11px; margin-top: 4px; }
.config-row { display: flex; align-items: center; justify-content: space-between; padding: 10px 0; }
.config-row-label { color: var(--text); flex: 1; }
.config-row-value { color: var(--smeraldo-chiaro); font-weight: 500; min-width: 40px; text-align: right; }
.config-slider-row { display: flex; align-items: center; gap: 12px; }
.config-slider-row input[type="range"] { flex: 1; accent-color: var(--smeraldo); }
.config-info {
  font-size: 12px; color: var(--text-faint); margin-top: 8px;
  padding: 8px 10px; background: rgba(31,168,136,0.06); border-radius: 6px;
}
.config-details { margin-top: 20px; }
.config-details summary { list-style: none; }
.config-details summary::-webkit-details-marker { display: none; }

/* ============ SWITCH ============ */
.switch {
  position: relative; width: 44px; height: 26px;
  background: var(--bg-card); border-radius: 13px;
  border: 1px solid rgba(31,168,136,0.3); cursor: pointer;
  transition: all 150ms; flex-shrink: 0;
}
.switch::after {
  content: ''; position: absolute; top: 2px; left: 2px;
  width: 20px; height: 20px; background: var(--text-faint);
  border-radius: 50%; transition: all 150ms;
}
.switch.on { background: var(--smeraldo); border-color: var(--smeraldo-chiaro); }
.switch.on::after { left: 20px; background: white; }

/* ============ TOAST ============ */
.toast {
  position: fixed; bottom: calc(80px + var(--inset-bottom)); left: 50%;
  transform: translateX(-50%);
  background: var(--bg-card); color: var(--text); padding: 12px 20px;
  border-radius: 12px; font-size: 14px; border: 1px solid rgba(31,168,136,0.3);
  z-index: 200; max-width: 90%; text-align: center;
  animation: toastIn 200ms ease-out; box-shadow: 0 8px 24px rgba(0,0,0,0.5);
}
@keyframes toastIn {
  from { opacity: 0; transform: translate(-50%, 20px); }
  to { opacity: 1; transform: translate(-50%, 0); }
}
.toast.error { border-color: var(--error); color: var(--error); }

/* Sovranità Temporale — gesti sui messaggi (stile minimo, rivisto in fase UI) */
.msg-tools {
  display: flex;
  gap: 8px;
  margin-top: 4px;
  justify-content: flex-end;
}
.msg-tool-btn {
  background: transparent;
  border: none;
  color: var(--text-muted, #8a8a8e);
  font-size: 12px;
  padding: 2px 6px;
  cursor: pointer;
  opacity: 0.6;
}
.msg-tool-btn:hover { opacity: 1; }
.msg-error .msg-tool-btn { opacity: 0.9; }
