/* ─── Reset & Base ─────────────────────────────────────── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --primary: #1e40af;
  --primary-hover: #1e3a8a;
  --primary-light: #dbeafe;
  --success: #059669;
  --success-bg: #ecfdf5;
  --error: #dc2626;
  --error-bg: #fef2f2;
  --bg: #f1f5f9;
  --card: #ffffff;
  --border: #e2e8f0;
  --text: #1e293b;
  --text-secondary: #64748b;
  --text-light: #94a3b8;
  --radius: 12px;
  --shadow: 0 1px 3px rgba(0,0,0,0.08), 0 1px 2px rgba(0,0,0,0.06);
  --shadow-lg: 0 10px 25px rgba(0,0,0,0.1);
  --sidebar-width: 280px;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.6;
  min-height: 100vh;
}

/* ─── Screens ──────────────────────────────────────────── */
.screen { display: none; }
.screen.active { display: flex; }

/* ─── Auth Screen ──────────────────────────────────────── */
#auth-screen {
  min-height: 100vh;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, #0f172a 0%, #1e3a8a 50%, #1e40af 100%);
}

.auth-card {
  background: var(--card);
  border-radius: var(--radius);
  padding: 2.5rem 2rem;
  width: 100%;
  max-width: 400px;
  margin: 1rem;
  box-shadow: var(--shadow-lg);
}

.logo {
  text-align: center;
  margin-bottom: 2rem;
}

.logo-icon {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 56px;
  height: 56px;
  background: var(--primary);
  color: white;
  border-radius: 14px;
  font-size: 1.25rem;
  font-weight: 700;
  margin-bottom: 1rem;
}
.logo-icon svg { width: 70%; height: 70%; display: block; }

.logo h1 {
  font-size: 1.5rem;
  font-weight: 700;
  color: var(--text);
}

.subtitle {
  color: var(--text-secondary);
  font-size: 0.9rem;
  margin-top: 0.25rem;
}

.error-text {
  color: var(--error);
  font-size: 0.85rem;
  text-align: center;
  margin-top: 1rem;
}

.hidden { display: none !important; }

/* ─── Buttons ──────────────────────────────────────────── */
.btn-primary {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.5rem;
  padding: 0.7rem 1.5rem;
  background: var(--primary);
  color: white;
  border: none;
  border-radius: 8px;
  font-size: 0.95rem;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.2s, transform 0.1s;
  text-decoration: none;
}
.btn-primary:hover { background: var(--primary-hover); }
.btn-primary:active { transform: scale(0.98); }
.btn-primary:disabled { background: var(--text-light); cursor: not-allowed; }

.btn-full { width: 100%; }

.btn-secondary {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 0.6rem 1.2rem;
  background: transparent;
  color: var(--primary);
  border: 1.5px solid var(--primary);
  border-radius: 8px;
  font-size: 0.9rem;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.2s;
}
.btn-secondary:hover { background: var(--primary-light); }

.btn-text {
  background: none;
  border: none;
  color: var(--text-secondary);
  font-size: 0.85rem;
  cursor: pointer;
  padding: 0.4rem 0.8rem;
  border-radius: 6px;
  transition: color 0.2s, background 0.2s;
}
.btn-text:hover { color: var(--text); background: rgba(0,0,0,0.05); }

.btn-icon {
  background: none;
  border: none;
  color: var(--text-light);
  font-size: 1.2rem;
  cursor: pointer;
  padding: 0.2rem 0.4rem;
  border-radius: 4px;
  line-height: 1;
}
.btn-icon:hover { background: rgba(0,0,0,0.05); color: var(--error); }

.google-signin-container {
  display: flex;
  justify-content: center;
  margin-top: 1.5rem;
  flex-direction: column;
  align-items: center;
  gap: 0.75rem;
}

.btn-google {
  display: inline-flex;
  align-items: center;
  gap: 0.75rem;
  padding: 0.6rem 1.5rem;
  font-size: 0.9rem;
  font-weight: 500;
  color: var(--text);
  background: white;
  border: 1px solid var(--border);
  border-radius: 8px;
  cursor: pointer;
  transition: all 0.15s;
  box-shadow: var(--shadow);
  min-width: 240px;
  justify-content: center;
}
.btn-google:hover {
  background: #f8fafc;
  box-shadow: var(--shadow-lg);
  border-color: #cbd5e1;
}
.btn-google svg { flex-shrink: 0; }

/* ─── Password login form ───────────────────────────────── */
.password-login-form {
  display: flex;
  flex-direction: column;
  gap: 0.4rem;
  margin-top: 1.5rem;
}
.password-login-form .input-label {
  font-size: 0.8rem;
  font-weight: 500;
  color: var(--text-muted);
  margin-top: 0.3rem;
}
.password-login-form input {
  padding: 0.55rem 0.75rem;
  font-size: 0.95rem;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: white;
  color: var(--text);
}
.password-login-form input:focus {
  outline: none;
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(59,130,246,0.15);
}
.password-login-form button {
  margin-top: 0.75rem;
}
.auth-divider {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  margin: 1.25rem 0 0.25rem;
  color: var(--text-muted);
  font-size: 0.8rem;
}
.auth-divider::before,
.auth-divider::after {
  content: '';
  flex: 1;
  height: 1px;
  background: var(--border);
}

/* ─── Header ───────────────────────────────────────────── */
header {
  background: var(--card);
  border-bottom: 1px solid var(--border);
  padding: 0 1rem;
  position: sticky;
  top: 0;
  z-index: 100;
  flex-shrink: 0;
}

.header-content {
  display: flex;
  align-items: center;
  justify-content: space-between;
  height: 52px;
}

.header-left {
  display: flex;
  align-items: center;
  gap: 0.75rem;
}

.header-right {
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.header-hamburger {
  font-size: 1.1rem;
  color: var(--text-secondary);
  padding: 0.3rem 0.5rem;
}
.header-hamburger:hover { color: var(--text); background: rgba(0,0,0,0.05); }

.logo-small {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 32px;
  height: 32px;
  background: var(--primary);
  color: white;
  border-radius: 8px;
  font-size: 0.75rem;
  font-weight: 700;
}
.logo-small svg { width: 70%; height: 70%; display: block; }

header h1 {
  font-size: 1rem;
  font-weight: 700;
}

.badge {
  background: var(--primary-light);
  color: var(--primary);
  font-size: 0.7rem;
  font-weight: 600;
  padding: 0.2rem 0.6rem;
  border-radius: 99px;
  text-transform: uppercase;
  letter-spacing: 0.5px;
}

.user-avatar {
  width: 28px;
  height: 28px;
  border-radius: 50%;
  object-fit: cover;
}

.user-name {
  font-size: 0.8rem;
  color: var(--text-secondary);
  max-width: 120px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.user-credits {
  display: inline-flex;
  align-items: center;
  gap: 0.25rem;
  padding: 0.2rem 0.55rem;
  font-size: 0.75rem;
  font-weight: 600;
  color: var(--primary);
  background: var(--primary-light);
  border-radius: 99px;
  white-space: nowrap;
}
.user-credits.low { color: #b45309; background: #fef3c7; }
.user-credits.empty { color: #b91c1c; background: #fee2e2; }
.user-credits::before { content: '◈'; font-size: 0.7rem; opacity: 0.8; }

.bubble-cost {
  margin-left: auto;
  font-size: 0.7rem;
  font-weight: 600;
  color: var(--text-light);
  background: rgba(0,0,0,0.05);
  padding: 0.1rem 0.4rem;
  border-radius: 99px;
  white-space: nowrap;
  flex-shrink: 0;
}
.chat-bubble.user .bubble-accordion summary .bubble-cost {
  background: rgba(255,255,255,0.35);
  color: var(--primary);
}

.bubble-cost-inline {
  display: inline-block;
  margin-top: 0.4rem;
  font-size: 0.7rem;
  font-weight: 600;
  color: var(--text-light);
  background: rgba(0,0,0,0.05);
  padding: 0.15rem 0.5rem;
  border-radius: 99px;
}

.btn-new-chat {
  display: inline-flex;
  align-items: center;
  gap: 0.3rem;
  padding: 0.35rem 0.85rem;
  font-size: 0.8rem;
  font-weight: 600;
  color: white;
  background: var(--primary);
  border: none;
  border-radius: 6px;
  cursor: pointer;
  transition: background 0.15s;
}
.btn-new-chat:hover { background: var(--primary-hover); }

/* ─── Main Layout (full height chat) ───────────────────── */
#main-screen {
  flex-direction: column;
  height: 100vh;
}

.app-body {
  flex: 1;
  display: flex;
  overflow: hidden;
}

/* ─── History Sidebar ──────────────────────────────────── */
.history-sidebar {
  width: var(--sidebar-width);
  min-width: var(--sidebar-width);
  background: var(--card);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  transition: width 0.18s ease, min-width 0.18s ease, opacity 0.18s ease;
}

@media (min-width: 861px) {
  .history-sidebar.collapsed {
    width: 0;
    min-width: 0;
    opacity: 0;
    border-right: none;
  }
}

.history-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0.75rem 1rem;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}

.history-header h3 {
  font-size: 0.9rem;
  font-weight: 600;
  margin: 0;
  color: var(--text);
}

.history-list {
  flex: 1;
  overflow-y: auto;
  padding: 0.5rem;
  display: flex;
  flex-direction: column;
  gap: 0.15rem;
}

.history-empty {
  color: var(--text-light);
  font-size: 0.8rem;
  text-align: center;
  padding: 1rem 0;
}

.history-item {
  display: block;
  padding: 0.6rem 0.5rem;
  border-radius: 6px;
  text-decoration: none;
  color: var(--text);
  transition: background 0.15s;
  border: none;
  background: none;
  text-align: left;
  cursor: pointer;
  width: 100%;
  position: relative;
}
.history-item:hover { background: var(--primary-light); }

.history-item-delete {
  position: absolute;
  top: 0.4rem;
  right: 0.3rem;
  background: none;
  border: none;
  color: var(--text-light);
  font-size: 1rem;
  line-height: 1;
  cursor: pointer;
  padding: 0 0.2rem;
  opacity: 0;
  transition: opacity 0.15s, color 0.15s;
}
.history-item:hover .history-item-delete { opacity: 1; }
.history-item-delete:hover { color: #e53e3e; }

.history-item-star {
  position: absolute;
  top: 0.4rem;
  right: 1.5rem;
  background: none;
  border: none;
  color: var(--text-light);
  font-size: 1rem;
  line-height: 1;
  cursor: pointer;
  padding: 0 0.2rem;
  opacity: 0;
  transition: opacity 0.15s, color 0.15s;
}
.history-item:hover .history-item-star { opacity: 1; }
.history-item-star.on { opacity: 1; color: #f0a500; }
.history-item-star:hover { color: #f0a500; }

.history-item-query {
  font-size: 0.8rem;
  line-height: 1.3;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
  word-break: break-word;
}

.history-item-meta {
  display: flex;
  align-items: center;
  gap: 0.4rem;
  margin-top: 0.25rem;
  font-size: 0.7rem;
  color: var(--text-light);
}

.history-item-link {
  color: var(--primary);
  text-decoration: none;
  font-size: 0.7rem;
  white-space: nowrap;
}
.history-item-link:hover { text-decoration: underline; }

.history-item-file {
  margin-top: 0.2rem;
  overflow: hidden;
}
.history-item-file .history-item-link {
  display: block;
  overflow: hidden;
  text-overflow: ellipsis;
}

.history-chain-badge {
  font-size: 0.6rem;
  background: var(--primary-light);
  color: var(--primary);
  padding: 0.1rem 0.35rem;
  border-radius: 4px;
  font-weight: 600;
}

/* ─── Chat Area ────────────────────────────────────────── */
.chat-area {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  min-width: 0;
}

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

/* ─── Welcome message ──────────────────────────────────── */
.chat-welcome {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  color: var(--text-light);
  text-align: center;
  padding: 2rem;
}

.welcome-icon {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 64px;
  height: 64px;
  background: var(--primary);
  color: white;
  border-radius: 16px;
  font-size: 1.4rem;
  font-weight: 700;
  margin-bottom: 1rem;
}
.welcome-icon svg { width: 70%; height: 70%; display: block; }

.chat-welcome h2 {
  font-size: 1.2rem;
  font-weight: 700;
  color: var(--text);
  margin-bottom: 0.25rem;
}

.chat-welcome p {
  font-size: 0.9rem;
  color: var(--text-secondary);
}

/* ─── Chat Bubbles ─────────────────────────────────────── */
.chat-bubble {
  max-width: 85%;
  animation: fadeIn 0.2s ease;
}

.chat-bubble.user {
  align-self: flex-end;
}

.chat-bubble.ai {
  align-self: flex-start;
}

.chat-bubble .bubble-content {
  padding: 0.75rem 1rem;
  border-radius: var(--radius);
  position: relative;
}

.chat-bubble.user .bubble-content {
  background: var(--primary);
  color: white;
  border-bottom-right-radius: 4px;
}

.chat-bubble.ai .bubble-content {
  background: var(--card);
  border: 1px solid var(--border);
  box-shadow: var(--shadow);
  border-bottom-left-radius: 4px;
}

.bubble-role {
  display: inline-block;
  font-size: 0.6rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  padding: 0.1rem 0.35rem;
  border-radius: 3px;
  margin-bottom: 0.3rem;
}

.chat-bubble.user .bubble-role {
  background: rgba(255,255,255,0.2);
  color: rgba(255,255,255,0.9);
}

.chat-bubble.ai .bubble-role {
  background: var(--success-bg);
  color: var(--success);
}

.bubble-text {
  font-size: 0.9rem;
  line-height: 1.5;
  white-space: pre-wrap;
  word-break: break-word;
}

/* ─── Collapsed bubble accordion (for history) ─────────── */
.bubble-accordion {
  border: none;
  background: none;
}

.bubble-accordion summary {
  cursor: pointer;
  padding: 0.5rem 0.75rem;
  font-size: 0.82rem;
  display: flex;
  align-items: center;
  gap: 0.5rem;
  list-style: none;
  border-radius: 8px;
  transition: background 0.15s;
}

.bubble-accordion summary::-webkit-details-marker { display: none; }

.chat-bubble.user .bubble-accordion summary {
  background: var(--primary-light);
  color: var(--primary);
}

.chat-bubble.user .bubble-accordion summary .bubble-role {
  background: var(--primary);
  color: white;
}

.chat-bubble.ai .bubble-accordion summary {
  background: var(--card);
  border: 1px solid var(--border);
  color: var(--text);
}

.bubble-accordion summary::before {
  content: '\25B6';
  font-size: 0.55rem;
  transition: transform 0.2s;
  color: var(--text-light);
  flex-shrink: 0;
}

.bubble-accordion[open] summary::before {
  transform: rotate(90deg);
}

.bubble-preview {
  flex: 1;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-weight: 500;
}

.bubble-body {
  padding: 0.6rem 0.75rem;
  font-size: 0.85rem;
  line-height: 1.5;
  border-top: 1px solid var(--border);
  margin-top: 0.25rem;
}

/* ─── AI bubble progress sub-accordion ─────────────────── */
.bubble-progress-accordion {
  margin-top: 0.3rem;
  border: 1px solid var(--border);
  border-radius: 8px;
  overflow: hidden;
  background: var(--bg);
}

.bubble-progress-accordion summary {
  cursor: pointer;
  padding: 0.4rem 0.6rem;
  font-size: 0.78rem;
  font-weight: 600;
  color: var(--text-secondary);
  list-style: none;
  display: flex;
  align-items: center;
  gap: 0.4rem;
  user-select: none;
}

.bubble-progress-accordion summary::-webkit-details-marker { display: none; }

.bubble-progress-accordion summary::before {
  content: '\25B6';
  font-size: 0.5rem;
  transition: transform 0.2s;
  color: var(--text-light);
}

.bubble-progress-accordion[open] summary::before {
  transform: rotate(90deg);
}

.bubble-progress-log {
  padding: 0.3rem 0.6rem 0.5rem;
  max-height: 250px;
  overflow-y: auto;
  font-size: 0.82rem;
}

/* ─── AI bubble result ─────────────────────────────────── */
.bubble-result {
  margin-top: 0.5rem;
  font-size: 0.9rem;
  line-height: 1.6;
}

.bubble-error {
  color: var(--error);
  font-weight: 600;
  font-size: 0.85rem;
  display: flex;
  align-items: center;
  gap: 0.4rem;
}

.error-icon-small {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 20px;
  height: 20px;
  border-radius: 50%;
  background: var(--error);
  color: white;
  font-size: 0.75rem;
  font-weight: 700;
  flex-shrink: 0;
}

.btn-sheet-link {
  display: inline-block;
  margin-top: 0.5rem;
  padding: 0.5rem 1rem;
  font-size: 0.85rem;
  font-weight: 600;
  color: white;
  background: var(--success);
  border-radius: 6px;
  text-decoration: none;
  transition: background 0.15s;
}
.btn-sheet-link:visited,
.btn-sheet-link:link,
.btn-sheet-link:active { color: white; text-decoration: none; }
.btn-sheet-link:hover { background: #047857; color: white; }

/* ─── Progress entries ─────────────────────────────────── */
.progress-entry {
  display: flex;
  align-items: flex-start;
  gap: 0.5rem;
  padding: 0.25rem 0;
  color: var(--text-secondary);
  animation: fadeIn 0.3s ease;
  font-size: 0.82rem;
}

.progress-entry .dot {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--primary);
  flex-shrink: 0;
  margin-top: 0.35rem;
  animation: pulse 1.5s ease-in-out infinite;
}

.progress-entry.done .dot {
  background: var(--success);
  animation: none;
}

.progress-entry .msg-text {
  color: var(--text);
  background: rgba(0,0,0,0.03);
  padding: 0.4rem 0.6rem;
  border-radius: 6px;
  font-size: 0.82rem;
  line-height: 1.5;
  word-break: break-word;
}

/* ─── Chat Input Bar ───────────────────────────────────── */
.chat-input-bar {
  flex-shrink: 0;
  padding: 0.5rem 1rem 0.75rem;
  background: var(--card);
  border-top: 1px solid var(--border);
  position: relative;
}

.chat-input-row {
  display: flex;
  align-items: flex-end;
  gap: 0.4rem;
  max-width: 900px;
  margin: 0 auto;
}

.chat-form {
  flex: 1;
  display: flex;
  align-items: flex-end;
  gap: 0.4rem;
  background: var(--bg);
  border: 1.5px solid var(--border);
  border-radius: 12px;
  padding: 0.35rem 0.5rem 0.35rem 0.75rem;
  transition: border-color 0.2s, box-shadow 0.2s;
}

.chat-form:focus-within {
  border-color: var(--primary);
  box-shadow: 0 0 0 3px var(--primary-light);
}

.chat-form textarea {
  flex: 1;
  border: none;
  background: transparent;
  font-size: 0.9rem;
  line-height: 1.5;
  color: var(--text);
  resize: none;
  font-family: inherit;
  padding: 0.2rem 0;
  min-height: 24px;
  max-height: 150px;
}

.chat-form textarea:focus {
  outline: none;
  box-shadow: none;
}

.btn-send {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 32px;
  height: 32px;
  min-height: 32px;
  border-radius: 8px;
  border: none;
  background: var(--primary);
  color: white;
  cursor: pointer;
  flex-shrink: 0;
  transition: background 0.15s;
  font-size: 0.85rem;
  align-self: center;
}
.btn-send:hover { background: var(--primary-hover); }
.btn-send:disabled { background: var(--text-light); cursor: not-allowed; }

.btn-abort {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 34px;
  height: 34px;
  border-radius: 8px;
  border: 1px solid var(--error);
  background: var(--error-bg);
  color: var(--error);
  cursor: pointer;
  flex-shrink: 0;
  font-size: 1rem;
  transition: all 0.15s;
}
.btn-abort:hover:not(:disabled) { background: var(--error); color: white; }
.btn-abort:disabled { opacity: 0.5; cursor: not-allowed; }

.controls-bar {
  flex-shrink: 0;
  display: flex;
  flex-direction: row;
  gap: 0.4rem;
  align-items: stretch;
  width: 100%;
}

.chat-input-row.controls-row {
  margin-top: 0.7rem;
  margin-bottom: 0.2rem;
}

.control-group {
  display: flex;
  flex: 1;
}

.segmented-control {
  display: inline-flex;
  border: 1px solid var(--border);
  border-radius: 6px;
  overflow: hidden;
  background: var(--bg);
  width: 100%;
}

.seg-btn {
  flex: 1;
  padding: 0.25rem 0.5rem;
  font-size: 0.72rem;
  font-weight: 500;
  color: var(--text-secondary);
  background: transparent;
  border: none;
  cursor: pointer;
  transition: all 0.15s;
  white-space: nowrap;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.05rem;
  line-height: 1.2;
}

.seg-label {
  font-weight: 600;
  font-size: 0.72rem;
}

.seg-hint {
  font-size: 0.52rem;
  font-weight: 400;
  opacity: 0.65;
  line-height: 1.1;
}

.seg-btn + .seg-btn {
  border-left: 1px solid var(--border);
}

.seg-btn.active {
  background: var(--primary);
  color: white;
  font-weight: 600;
}

.seg-btn.active .seg-hint {
  opacity: 0.8;
}

.seg-btn:hover:not(.active) {
  background: var(--primary-light);
  color: var(--primary);
}

.chat-input-footer {
  max-width: 900px;
  margin: 0.2rem auto 0;
  text-align: right;
}

.char-count {
  font-size: 0.7rem;
  color: var(--text-light);
}

/* ─── Cost confirmation overlay ────────────────────────── */
.confirm-overlay {
  position: absolute;
  left: 0;
  right: 0;
  bottom: 100%;
  background: rgba(15, 23, 42, 0.9);
  backdrop-filter: blur(4px);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 10;
  border-radius: var(--radius) var(--radius) 0 0;
  padding: 1rem;
}
.confirm-overlay.hidden { display: none; }
.confirm-content {
  background: var(--card);
  padding: 1.25rem 1.5rem;
  border-radius: var(--radius);
  box-shadow: var(--shadow-lg);
  text-align: center;
  max-width: 340px;
}
.confirm-content p { margin-bottom: 0.5rem; font-size: 0.85rem; }
.confirm-actions { display: flex; gap: 0.75rem; justify-content: center; margin-top: 0.75rem; }
.confirm-actions button { min-width: 100px; }
.confirm-actions .btn-secondary {
  padding: 0.6rem 1.2rem;
  border-radius: 8px;
  font-size: 0.85rem;
  font-weight: 600;
  cursor: pointer;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text);
  transition: background 0.2s;
}
.confirm-actions .btn-secondary:hover { background: var(--border); }

/* ─── Spinner ──────────────────────────────────────────── */
.spinner {
  display: inline-block;
  width: 16px;
  height: 16px;
  border: 2px solid rgba(255,255,255,0.3);
  border-top-color: white;
  border-radius: 50%;
  animation: spin 0.6s linear infinite;
}

@keyframes spin { to { transform: rotate(360deg); } }
@keyframes fadeIn { from { opacity: 0; transform: translateY(4px); } to { opacity: 1; transform: translateY(0); } }
@keyframes pulse { 0%, 100% { opacity: 1; } 50% { opacity: 0.4; } }

/* ─── Footer ───────────────────────────────────────────── */
footer {
  text-align: center;
  padding: 0.5rem 1rem;
  color: var(--text-light);
  font-size: 0.7rem;
  flex-shrink: 0;
  border-top: 1px solid var(--border);
  background: var(--card);
}

/* ─── Markdown rendered content ────────────────────────── */
.md-content { white-space: normal; }
.md-content p { margin: 0.4em 0; }
.md-content h1, .md-content h2, .md-content h3 { margin: 0.8em 0 0.3em; font-size: 1em; font-weight: 700; }
.md-content h2 { font-size: 1.05em; }
.md-content h1 { font-size: 1.1em; }
.md-content ul, .md-content ol { margin: 0.3em 0; padding-left: 1.4em; text-align: left; }
.md-content li { margin: 0.1em 0; line-height: 1.45; }
.md-content hr { border: none; border-top: 1px solid var(--border); margin: 0.6em 0; }
.md-content blockquote {
  border-left: 3px solid var(--primary);
  margin: 0.4em 0;
  padding: 0.3em 0.8em;
  background: var(--primary-light);
  border-radius: 0 6px 6px 0;
  font-size: 0.85em;
}
.md-content a {
  color: var(--primary);
  text-decoration: underline;
  word-break: break-all;
}
.md-content a:hover { color: var(--primary-hover); }
.md-content code {
  font-family: 'SF Mono', 'Fira Code', 'Cascadia Code', 'JetBrains Mono', Menlo, Monaco, Consolas, 'Courier New', monospace;
  background: rgba(0,0,0,0.06);
  padding: 0.15em 0.35em;
  border-radius: 4px;
  font-size: 0.9em;
}
.md-content pre {
  background: #1e293b;
  color: #e2e8f0;
  padding: 0.6em 0.8em;
  border-radius: 6px;
  overflow-x: auto;
  margin: 0.4em 0;
  font-size: 0.82em;
  line-height: 1.5;
}
.md-content pre code {
  background: none;
  padding: 0;
  color: inherit;
  font-size: inherit;
}
.md-content table {
  border-collapse: collapse;
  width: 100%;
  margin: 0.5em 0;
  font-family: 'SF Mono', 'Fira Code', Menlo, Monaco, Consolas, 'Courier New', monospace;
  font-size: 0.8em;
  line-height: 1.4;
}
.md-content th, .md-content td {
  border: 1px solid var(--border);
  padding: 0.3em 0.6em;
  text-align: left;
  white-space: nowrap;
}
.md-content th {
  background: var(--bg);
  font-weight: 600;
}
.md-content tr:nth-child(even) { background: rgba(0,0,0,0.02); }
.md-content strong { font-weight: 700; }
.md-content em { font-style: italic; }

/* ─── Debug Panel (floating bottom-right) ──────────────── */
.debug-panel {
  position: fixed;
  bottom: 0.75rem;
  right: 0.75rem;
  z-index: 200;
  background: #fafbfc;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow-lg);
  max-width: 500px;
  width: auto;
  font-size: 0.8rem;
}

.debug-header {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  cursor: pointer;
  user-select: none;
  padding: 0.4rem 0.75rem;
}

.debug-label {
  font-size: 0.75rem;
  font-weight: 600;
  color: var(--text-secondary);
  flex: 1;
}

.debug-badge {
  font-size: 0.65rem;
  font-weight: 600;
  padding: 0.1rem 0.4rem;
  border-radius: 99px;
  background: var(--border);
  color: var(--text-light);
  letter-spacing: 0.3px;
}

.debug-badge.active {
  background: #fef3c7;
  color: #92400e;
}

.debug-arrow {
  font-size: 0.6rem;
  color: var(--text-light);
  transition: transform 0.2s;
}

.debug-arrow.open {
  transform: rotate(90deg);
}

.debug-body {
  padding: 0.5rem 0.75rem 0.75rem;
  border-top: 1px solid var(--border);
  max-height: 350px;
  overflow-y: auto;
}

.debug-body h4 {
  font-size: 0.75rem;
  font-weight: 600;
  color: var(--text-secondary);
  margin: 0.75rem 0 0.4rem;
}

.debug-stats {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(110px, 1fr));
  gap: 0.35rem;
}

.debug-stat {
  display: flex;
  flex-direction: column;
  background: white;
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 0.35rem 0.5rem;
}

.debug-stat.highlight {
  background: #fffbeb;
  border-color: #fbbf24;
}

.debug-stat-label {
  font-size: 0.6rem;
  font-weight: 500;
  color: var(--text-light);
  text-transform: uppercase;
  letter-spacing: 0.3px;
}

.debug-stat-value {
  font-size: 0.85rem;
  font-weight: 700;
  color: var(--text);
  font-variant-numeric: tabular-nums;
}

.debug-stat.highlight .debug-stat-value {
  color: #92400e;
}

.debug-log {
  max-height: 150px;
  overflow-y: auto;
  font-family: 'SF Mono', 'Fira Code', 'Consolas', monospace;
  font-size: 0.7rem;
  line-height: 1.5;
  background: #1e293b;
  color: #e2e8f0;
  border-radius: 6px;
  padding: 0.4rem 0.6rem;
}

.debug-log-entry {
  display: flex;
  gap: 0.5rem;
  padding: 0.1rem 0;
  border-bottom: 1px solid rgba(255,255,255,0.05);
  flex-wrap: wrap;
}
.debug-log-entry.debug-warning {
  background: rgba(250, 204, 21, 0.08);
  border-left: 2px solid #facc15;
  padding-left: 0.4rem;
}
.debug-warn-label { color: #facc15; font-weight: 600; }
.debug-warn-nums { color: #fde68a; font-family: monospace; }

.debug-round {
  color: #60a5fa;
  font-weight: 600;
  min-width: 28px;
}

.debug-tokens {
  color: #a5b4fc;
}

.debug-tools {
  color: #34d399;
  flex: 1;
  min-width: 60px;
}

.debug-stop {
  color: #94a3b8;
}

.debug-code-section h4 {
  margin: 0.5rem 0 0.2rem;
  font-size: 0.75rem;
  color: var(--text-light);
}

.debug-code {
  max-height: 200px;
  overflow: auto;
  font-family: 'SF Mono', 'Fira Code', 'Consolas', monospace;
  font-size: 0.68rem;
  line-height: 1.4;
  background: #1e293b;
  color: #e2e8f0;
  border-radius: 6px;
  padding: 0.5rem;
  white-space: pre-wrap;
  word-break: break-word;
  margin: 0;
}

/* ─── Responsive ───────────────────────────────────────── */
@media (max-width: 860px) {
  .history-sidebar {
    display: none;
    position: fixed;
    top: 52px;
    left: 0;
    bottom: 0;
    z-index: 150;
    border-radius: 0;
    box-shadow: var(--shadow-lg);
    width: var(--sidebar-width);
    min-width: var(--sidebar-width);
  }
  .history-sidebar.open { display: flex; }

  .chat-bubble { max-width: 92%; }

  .debug-panel {
    max-width: calc(100vw - 1.5rem);
    right: 0.5rem;
    bottom: 0.5rem;
  }

  header h1 { font-size: 0.9rem; }
  .badge { display: none; }
  .user-name { display: none; }
}

@media (max-width: 600px) {
  .chat-form textarea { font-size: 16px; /* prevents iOS zoom */ }
}

/* ═══════════════════════════════════════════════════════════
   Tools framework — overlay "Strumenti" + Prima Nota
   ═══════════════════════════════════════════════════════════ */
.tools-overlay {
  position: fixed; inset: 0; z-index: 1500;
  background: var(--bg); display: flex; flex-direction: column;
}
.tools-overlay.hidden { display: none; }
.tools-overlay-header {
  display: flex; align-items: center; gap: .8rem;
  padding: .8rem 1.2rem; background: var(--card);
  border-bottom: 1px solid var(--border); box-shadow: var(--shadow);
}
.tools-overlay-header h2 { font-size: 1.05rem; margin: 0; color: var(--text); }
.tools-overlay-header .btn-icon {
  background: none; border: none; font-size: 1.3rem; cursor: pointer;
  color: var(--text-secondary); padding: .2rem .5rem; border-radius: 6px;
}
.tools-overlay-header .btn-icon:hover { background: var(--bg); }
.tools-overlay-body { flex: 1; overflow: auto; padding: 1.2rem; }

.tools-grid {
  display: grid; grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
  gap: 1rem; max-width: 900px; margin: 0 auto;
}
.tool-card {
  display: flex; flex-direction: column; gap: .4rem; align-items: flex-start;
  background: var(--card); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 1.1rem; cursor: pointer; text-align: left; transition: .15s;
}
.tool-card:hover { border-color: var(--primary); box-shadow: var(--shadow-lg); transform: translateY(-2px); }
.tool-card-icon { font-size: 1.8rem; }
.tool-card-name { font-weight: 600; color: var(--text); }
.tool-card-desc { font-size: .8rem; color: var(--text-secondary); line-height: 1.4; }
.tools-empty, .pn-empty, .pn-muted { color: var(--text-secondary); font-size: .85rem; text-align: center; padding: 1rem; }

/* Prima Nota */
.pn-wrap { max-width: 1000px; margin: 0 auto; }
.pn-loading, .pn-error { text-align: center; padding: 2rem; color: var(--text-secondary); }
.pn-error { color: var(--error); }
.pn-demo-note {
  background: #fffbeb; border: 1px solid #fde68a; color: #92400e;
  padding: .6rem .8rem; border-radius: 8px; font-size: .8rem; margin-bottom: 1rem;
}
.pn-tabs { display: flex; flex-wrap: wrap; gap: .4rem; margin-bottom: 1rem; }
.pn-tab {
  display: inline-flex; align-items: center; gap: .4rem;
  background: var(--card); border: 1px solid var(--border); border-radius: 999px;
  padding: .4rem .9rem; font-size: .82rem; cursor: pointer; color: var(--text-secondary);
}
.pn-tab.active { background: var(--primary); color: #fff; border-color: var(--primary); }
.pn-tab-n {
  background: rgba(0,0,0,.08); border-radius: 999px; padding: 0 .4rem;
  font-size: .72rem; font-weight: 600; min-width: 1.2rem; text-align: center;
}
.pn-tab.active .pn-tab-n { background: rgba(255,255,255,.25); }

/* Prima Nota — barra filtri */
.pn-filters {
  display: flex; flex-wrap: wrap; align-items: center; gap: .5rem;
  margin-bottom: 1rem;
}
.pn-f-input, .pn-f-select {
  background: var(--card); border: 1px solid var(--border); border-radius: 8px;
  padding: .42rem .7rem; font-size: .82rem; color: var(--text); font-family: inherit;
}
.pn-f-input { flex: 1 1 220px; min-width: 180px; }
.pn-f-select { cursor: pointer; }
.pn-f-input:focus, .pn-f-select:focus { outline: none; border-color: var(--primary); }
.pn-f-reset {
  background: transparent; border: 1px solid var(--border); border-radius: 8px;
  padding: .42rem .8rem; font-size: .8rem; cursor: pointer; color: var(--text-secondary);
}
.pn-f-reset:hover { background: var(--card); color: var(--text); }
.pn-count { margin-left: auto; font-size: .78rem; color: var(--text-secondary); white-space: nowrap; }

/* Prima Nota — selettore vista (Movimenti / Anagrafiche) */
.pn-viewswitch { display: inline-flex; gap: .25rem; background: var(--card); border: 1px solid var(--border); border-radius: 999px; padding: .2rem; margin-bottom: 1rem; }
.pn-vbtn { border: none; background: transparent; border-radius: 999px; padding: .35rem 1rem; font-size: .82rem; cursor: pointer; color: var(--text-secondary); font-family: inherit; }
.pn-vbtn.active { background: var(--primary); color: #fff; }

/* Prima Nota — vista anagrafiche */
.pn-anag-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 1.2rem; }
@media (max-width: 720px) { .pn-anag-grid { grid-template-columns: 1fr; } }
.pn-anag-h { font-size: .95rem; margin: 0 0 .2rem; display: flex; align-items: center; gap: .4rem; }
.pn-anag-n { background: rgba(0,0,0,.08); border-radius: 999px; padding: 0 .45rem; font-size: .72rem; font-weight: 600; }
.pn-anag-sub { font-size: .78rem; color: var(--text-secondary); margin: 0 0 .7rem; }
.pn-anag-card { background: var(--card); border: 1px solid var(--border); border-radius: 10px; padding: .6rem .8rem; margin-bottom: .5rem; }
.pn-anag-name { font-weight: 600; font-size: .88rem; }
.pn-anag-meta { display: flex; flex-wrap: wrap; gap: .35rem; margin-top: .3rem; }
.pn-anag-tag { font-size: .72rem; background: rgba(0,0,0,.05); border-radius: 6px; padding: .1rem .4rem; color: var(--text-secondary); }
.pn-anag-iban { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; }
.pn-anag-note { font-size: .76rem; color: var(--text-secondary); margin-top: .3rem; font-style: italic; }

/* Prima Nota — vista Fatture */
.pn-fatt-card { background: var(--card); border: 1px solid var(--border); border-left: 4px solid var(--border); border-radius: 10px; padding: .6rem .8rem; margin-bottom: .55rem; }
.pn-fatt-card.pn-ric-ok { border-left-color: var(--success); }
.pn-fatt-card.pn-ric-partial { border-left-color: #d97706; }
.pn-fatt-card.pn-ric-overdue { border-left-color: var(--error); }
.pn-fatt-card.pn-ric-aperta { border-left-color: var(--text-light); }
.pn-fatt-card.pn-ric-paid-unlinked { border-left-color: #d97706; }
.pn-fatt-card.pn-ric-stornata { border-left-color: var(--text-light); opacity: .7; }
.pn-fatt-head { display: flex; justify-content: space-between; align-items: center; gap: .5rem; }
.pn-fatt-num { font-weight: 600; font-size: .86rem; font-variant-numeric: tabular-nums; }
.pn-fatt-party { font-size: .82rem; color: var(--text-secondary); margin: .15rem 0 .4rem; }
.pn-fatt-amounts { display: flex; flex-wrap: wrap; gap: .9rem; font-size: .78rem; color: var(--text-secondary); font-variant-numeric: tabular-nums; }
.pn-fatt-amounts strong { color: var(--text); }
.pn-fatt-bar { height: 6px; background: rgba(0,0,0,.07); border-radius: 999px; margin: .45rem 0 .35rem; overflow: hidden; }
.pn-fatt-bar > span { display: block; height: 100%; background: var(--success); border-radius: 999px; }
.pn-fatt-foot { font-size: .74rem; color: var(--text-secondary); }
.pn-fatt-rate { display: flex; flex-wrap: wrap; gap: .35rem; margin-top: .45rem; }
.pn-rata { font-size: .72rem; background: rgba(0,0,0,.05); border: 1px solid var(--border); border-radius: 6px; padding: .12rem .45rem; color: var(--text-secondary); font-variant-numeric: tabular-nums; }
.pn-rata-ok { background: rgba(26,127,55,.12); border-color: rgba(26,127,55,.35); color: #1a7f37; }
.pn-ric-tag { font-size: .72rem; font-weight: 600; border-radius: 999px; padding: .1rem .5rem; }
.pn-ric-tag.pn-ric-ok { background: rgba(26,127,55,.12); color: #1a7f37; }
.pn-ric-tag.pn-ric-partial, .pn-ric-tag.pn-ric-paid-unlinked { background: rgba(217,119,6,.14); color: #b45309; }
.pn-ric-tag.pn-ric-overdue { background: rgba(168,7,26,.12); color: #a8071a; }
.pn-ric-tag.pn-ric-aperta, .pn-ric-tag.pn-ric-stornata { background: rgba(0,0,0,.06); color: var(--text-secondary); }

/* Prima Nota — vista Conti c/c */
.pn-conti-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(260px, 1fr)); gap: 1rem; }
.pn-conto-card { background: var(--card); border: 1px solid var(--border); border-radius: 10px; padding: .8rem .9rem; }
.pn-conto-head { display: flex; justify-content: space-between; align-items: center; gap: .5rem; }
.pn-conto-name { font-weight: 600; font-size: .9rem; }
.pn-conto-int { font-size: .78rem; color: var(--text-secondary); margin: .2rem 0; }
.pn-conto-saldo { font-size: .82rem; margin: .5rem 0; color: var(--text-secondary); }
.pn-conto-saldo strong { font-size: .95rem; }
.pn-conto-stats { width: 100%; border-collapse: collapse; font-size: .78rem; }
.pn-conto-stats td { padding: .2rem 0; border-top: 1px solid var(--border); color: var(--text-secondary); }
.pn-conto-stats .pn-num { text-align: right; font-variant-numeric: tabular-nums; color: var(--text); }

.pn-list { display: flex; flex-direction: column; gap: .6rem; }
.pn-row {
  background: var(--card); border: 1px solid var(--border); border-radius: 10px;
  border-left: 4px solid var(--border); padding: .8rem 1rem;
}
.pn-state-flagged { border-left-color: var(--error); }
.pn-state-suggested { border-left-color: #d97706; }
.pn-state-matched { border-left-color: var(--success); }
.pn-state-confirmed { border-left-color: var(--primary); background: #f8fafc; }
.pn-state-manual { border-left-color: var(--text-light); }
.pn-row-main { display: grid; grid-template-columns: 1fr auto auto; gap: 1rem; align-items: center; }
@media (max-width: 720px) { .pn-row-main { grid-template-columns: 1fr; } }

.pn-mov-top { display: flex; align-items: center; gap: .7rem; flex-wrap: wrap; }
.pn-date { font-size: .78rem; color: var(--text-secondary); font-variant-numeric: tabular-nums; }
.pn-amount { font-weight: 700; font-variant-numeric: tabular-nums; }
.pn-amount.pn-in { color: var(--success); }
.pn-amount.pn-out { color: var(--error); }
.pn-in { color: var(--success); }
.pn-out { color: var(--error); }
.pn-desc { font-size: .88rem; color: var(--text); margin-top: .25rem; }
.pn-meta { display: flex; gap: .5rem; flex-wrap: wrap; margin-top: .25rem; font-size: .76rem; color: var(--text-secondary); align-items: center; }
.pn-cat { background: var(--primary-light); color: var(--primary-hover); border-radius: 6px; padding: .05rem .4rem; }
.pn-cat-noinv { background: #ede9fe; color: #6d28d9; }
.pn-anomalia { margin-top: .35rem; font-size: .8rem; color: var(--error); font-weight: 600; }
.pn-state-tag { font-size: .72rem; color: var(--text-secondary); border: 1px solid var(--border); border-radius: 6px; padding: .02rem .4rem; }

.pn-badge { font-size: .72rem; font-weight: 700; border-radius: 6px; padding: .05rem .4rem; font-variant-numeric: tabular-nums; }
.pn-badge-high { background: var(--success-bg); color: var(--success); }
.pn-badge-mid { background: #fef3c7; color: #b45309; }
.pn-badge-low { background: var(--error-bg); color: var(--error); }
.pn-badge-na { background: var(--bg); color: var(--text-light); }

.pn-alloc { display: flex; flex-wrap: wrap; gap: .3rem; justify-content: flex-end; max-width: 280px; }
.pn-chip { font-size: .74rem; background: var(--bg); border: 1px solid var(--border); border-radius: 6px; padding: .1rem .45rem; white-space: nowrap; }
.pn-chip em { color: var(--primary); font-style: normal; font-weight: 600; }
.pn-chip-empty { color: var(--text-light); font-style: italic; }

.pn-actions { display: flex; gap: .35rem; flex-wrap: wrap; justify-content: flex-end; }
.pn-btn { border: none; border-radius: 7px; padding: .4rem .8rem; font-size: .8rem; cursor: pointer; font-weight: 500; }
.pn-btn-ok { background: var(--success); color: #fff; }
.pn-btn-ko { background: var(--error-bg); color: var(--error); }
.pn-btn-mini { background: var(--bg); color: var(--text-secondary); border: 1px solid var(--border); }
.pn-btn:hover { filter: brightness(.96); }

/* Modale Prima Nota */
.pn-modal-bg { position: fixed; inset: 0; z-index: 1600; background: rgba(15,23,42,.5); display: flex; align-items: flex-start; justify-content: center; padding: 3vh 1rem; overflow: auto; }
.pn-modal-bg.hidden { display: none; }
.pn-modal { background: var(--card); border-radius: var(--radius); padding: 1.4rem; max-width: 860px; width: 100%; box-shadow: var(--shadow-lg); }
.pn-modal h3 { margin: 0 0 .8rem; display: flex; align-items: center; gap: .6rem; }
.pn-modal h4 { margin: 1.1rem 0 .4rem; font-size: .9rem; }
.pn-detail-meta { display: grid; grid-template-columns: repeat(2, 1fr); gap: .4rem; font-size: .82rem; }
.pn-detail-meta strong { color: var(--text-secondary); font-weight: 600; margin-right: .3rem; }
.pn-detail-desc { margin-top: .6rem; font-size: .86rem; background: var(--bg); padding: .5rem .7rem; border-radius: 8px; }
.pn-motivi { margin: .3rem 0; padding-left: 1.2rem; font-size: .83rem; color: var(--text); }
.pn-motivi li { margin: .15rem 0; }
.pn-alloc-table { width: 100%; border-collapse: collapse; font-size: .8rem; }
.pn-alloc-table th, .pn-alloc-table td { text-align: left; padding: .4rem .5rem; border-bottom: 1px solid var(--border); }
.pn-alloc-table th { color: var(--text-secondary); font-weight: 600; }
.pn-alloc-table .pn-num { text-align: right; font-variant-numeric: tabular-nums; }
.pn-alloc-input { width: 110px; padding: .25rem .4rem; border: 1px solid var(--border); border-radius: 6px; text-align: right; }
.pn-alloc-summary { margin-top: .8rem; font-size: .85rem; color: var(--text); }
.pn-modal-actions { display: flex; gap: .5rem; margin-top: 1.2rem; justify-content: flex-end; }
.pn-modal-search { width: 100%; margin-bottom: .5rem; box-sizing: border-box; }
.pn-alloc-scroll { max-height: 320px; overflow: auto; border: 1px solid var(--border); border-radius: 8px; }
.pn-alloc-table .pn-tr-sel { background: rgba(26,127,55,.07); }
.pn-cat-ai { background: #e0e7ff; color: #4338ca; font-size: .68rem; }
.pn-bulk { display: flex; align-items: center; gap: .7rem; background: var(--success-bg, rgba(26,127,55,.08)); border: 1px solid rgba(26,127,55,.25); border-radius: 10px; padding: .55rem .8rem; margin-bottom: .8rem; }
.pn-bulk-hint { font-size: .76rem; color: var(--text-secondary); }
.pn-cf-btns { display: flex; gap: .5rem; flex-wrap: wrap; }
.pn-dash-demo-bar { margin-bottom: .8rem; }

/* Prima Nota — paginazione lista */
.pn-more { display: block; width: 100%; margin-top: .4rem; padding: .55rem; text-align: center; }

/* Prima Nota — regole ricorrenti */
.pn-rule-off { opacity: .55; }
.pn-rule-pattern { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: .74rem; background: rgba(0,0,0,.05); border-radius: 4px; padding: .05rem .3rem; word-break: break-all; }
.pn-rule-actions { white-space: nowrap; }
.pn-rule-actions .pn-btn { padding: .25rem .55rem; font-size: .74rem; }
.pn-rule-form { display: flex; flex-direction: column; gap: .6rem; }
.pn-rule-form label { display: flex; flex-direction: column; gap: .25rem; font-size: .78rem; color: var(--text-secondary); }
.pn-rule-form input, .pn-rule-form select { padding: .4rem .55rem; border: 1px solid var(--border); border-radius: 7px; font-size: .84rem; font-family: inherit; color: var(--text); background: var(--card); }
.pn-rule-form input:focus, .pn-rule-form select:focus { outline: none; border-color: var(--primary); }
.pn-rule-form-row { display: grid; grid-template-columns: 1fr 1fr; gap: .6rem; }
.pn-rule-adv summary { font-size: .78rem; color: var(--text-secondary); cursor: pointer; margin-bottom: .4rem; }
.pn-rule-active { flex-direction: row !important; align-items: center; gap: .4rem !important; }

/* Prima Nota — box demo (generatore movimenti) */
.pn-demo-box { background: var(--card); border: 1px dashed var(--border); border-radius: 10px; margin-bottom: 1rem; overflow: hidden; }
.pn-demo-toggle { display: flex; justify-content: space-between; align-items: center; width: 100%; border: none; background: transparent; padding: .6rem .9rem; font-size: .84rem; font-weight: 600; color: var(--text); cursor: pointer; font-family: inherit; }
.pn-demo-toggle:hover { background: var(--bg); }
.pn-demo-hint { font-size: .74rem; font-weight: 400; color: var(--text-secondary); }
.pn-demo-form { display: flex; flex-wrap: wrap; gap: .7rem; align-items: flex-end; padding: .2rem .9rem .8rem; border-top: 1px solid var(--border); }
.pn-demo-form label { display: flex; flex-direction: column; gap: .25rem; font-size: .74rem; color: var(--text-secondary); padding-top: .6rem; }
.pn-demo-form input, .pn-demo-form select { padding: .35rem .5rem; border: 1px solid var(--border); border-radius: 7px; font-size: .8rem; background: var(--card); color: var(--text); font-family: inherit; }
.pn-demo-form input[type="number"] { width: 70px; }
.pn-demo-actions { display: flex; gap: .5rem; margin-left: auto; padding-top: .6rem; }
.pn-cat-demo { background: #fce7f3; color: #be185d; }

/* Prima Nota — dashboard */
.pn-kpi-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: .7rem; margin-bottom: 1.1rem; }
.pn-kpi { background: var(--card); border: 1px solid var(--border); border-left: 4px solid var(--border); border-radius: 10px; padding: .7rem .8rem; }
.pn-kpi[data-goto] { cursor: pointer; }
.pn-kpi[data-goto]:hover { box-shadow: var(--shadow-lg); }
.pn-kpi-v { font-size: 1.15rem; font-weight: 700; font-variant-numeric: tabular-nums; }
.pn-kpi-l { font-size: .72rem; color: var(--text-secondary); margin-top: .15rem; }
.pn-kpi-good { border-left-color: var(--success); }
.pn-kpi-warn { border-left-color: #d97706; }
.pn-kpi-bad { border-left-color: var(--error); }
.pn-dash-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 1rem; }
.pn-dash-card { background: var(--card); border: 1px solid var(--border); border-radius: 10px; padding: .9rem 1rem; }
.pn-dash-card h3 { margin: 0 0 .7rem; font-size: .9rem; }
.pn-todo-row { display: flex; align-items: center; gap: .6rem; width: 100%; border: none; background: transparent; padding: .4rem .3rem; font-size: .8rem; border-top: 1px solid var(--border); text-align: left; font-family: inherit; color: var(--text); }
.pn-todo-row:not(.pn-todo-static) { cursor: pointer; }
.pn-todo-row:not(.pn-todo-static):hover { background: var(--bg); }
.pn-todo-desc { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.pn-todo-row .pn-amount { font-size: .8rem; }

/* Prima Nota — mini-chart flussi */
.pn-flow-chart { display: flex; gap: .6rem; align-items: flex-end; overflow-x: auto; padding: .3rem 0 .1rem; }
.pn-flow-col { display: flex; flex-direction: column; align-items: center; gap: .2rem; min-width: 64px; }
.pn-flow-bars { display: flex; gap: 3px; align-items: flex-end; height: 90px; }
.pn-flow-bars span { width: 14px; border-radius: 3px 3px 0 0; min-height: 2px; display: block; }
.pn-flow-in { background: var(--success); }
.pn-flow-out { background: var(--error); }
.pn-flow-m { font-size: .7rem; color: var(--text-secondary); font-variant-numeric: tabular-nums; }
.pn-flow-n { font-size: .68rem; font-weight: 600; font-variant-numeric: tabular-nums; white-space: nowrap; }
.pn-flow-legend { display: flex; gap: .5rem; align-items: center; font-size: .72rem; color: var(--text-secondary); margin-top: .5rem; }
.pn-flow-dot { width: 10px; height: 10px; border-radius: 3px; display: inline-block; }

/* Prima Nota — flusso di cassa */
.pn-cf-head { display: flex; justify-content: space-between; align-items: flex-start; gap: 1rem; margin-bottom: .9rem; }
.pn-cf-head h3 { margin: 0 0 .2rem; }
.pn-cf-table { width: 100%; border-collapse: collapse; font-size: .8rem; margin-bottom: .6rem; }
.pn-cf-table th, .pn-cf-table td { text-align: left; padding: .4rem .5rem; border-bottom: 1px solid var(--border); }
.pn-cf-table th { color: var(--text-secondary); font-weight: 600; }
.pn-cf-table .pn-num { text-align: right; font-variant-numeric: tabular-nums; }
.pn-cf-table + .pn-btn { margin-top: .3rem; }

@media (max-width: 760px) {
  .pn-dash-grid { grid-template-columns: 1fr; }
}
