/* ============================================================================
   4iTEC CRM — Stylesheet
   Identidade: tons terra, primário teal, tipografia Cabinet Grotesk + Satoshi
============================================================================ */

/* ----- 1. TOKENS ----- */
:root, [data-theme="light"] {
  --text-xs: clamp(0.75rem, 0.7rem + 0.25vw, 0.875rem);
  --text-sm: clamp(0.875rem, 0.8rem + 0.35vw, 1rem);
  --text-base: clamp(1rem, 0.95rem + 0.25vw, 1.0625rem);
  --text-lg: clamp(1.125rem, 1rem + 0.6vw, 1.375rem);
  --text-xl: clamp(1.5rem, 1.2rem + 1.25vw, 2.25rem);

  --space-1: 0.25rem; --space-2: 0.5rem; --space-3: 0.75rem; --space-4: 1rem;
  --space-5: 1.25rem; --space-6: 1.5rem; --space-8: 2rem; --space-10: 2.5rem;
  --space-12: 3rem; --space-16: 4rem;

  --color-bg: #f7f6f2;
  --color-surface: #fbfaf6;
  --color-surface-2: #ffffff;
  --color-surface-offset: #f1eee8;
  --color-border: #d4d1ca;
  --color-divider: #e3e0d9;
  --color-text: #28251d;
  --color-text-muted: #6f6d68;
  --color-text-faint: #aca9a2;
  --color-primary: #01696f;
  --color-primary-hover: #0c4e54;
  --color-primary-soft: #cedcd8;
  --color-success: #437a22;
  --color-success-soft: #d8e6cb;
  --color-warning: #964219;
  --color-warning-soft: #efd9cb;
  --color-error: #a12c7b;
  --color-error-soft: #ecd2e1;
  --color-blue: #006494;
  --color-blue-soft: #c7dbe7;

  --radius-sm: 0.375rem;
  --radius-md: 0.5rem;
  --radius-lg: 0.75rem;
  --radius-xl: 1rem;
  --radius-full: 9999px;

  --shadow-sm: 0 1px 2px rgba(40,37,29,.06);
  --shadow-md: 0 8px 24px rgba(40,37,29,.08);
  --shadow-lg: 0 24px 60px rgba(40,37,29,.18);

  --font-body: 'Satoshi', -apple-system, BlinkMacSystemFont, sans-serif;
  --font-display: 'Cabinet Grotesk', 'Satoshi', sans-serif;
}

[data-theme="dark"] {
  --color-bg: #161514;
  --color-surface: #1d1c1a;
  --color-surface-2: #232220;
  --color-surface-offset: #1a1917;
  --color-border: #3b3a37;
  --color-divider: #2a2926;
  --color-text: #d3d1cc;
  --color-text-muted: #9b9893;
  --color-text-faint: #6a6864;
  --color-primary: #4f98a3;
  --color-primary-hover: #6dabb5;
  --color-primary-soft: #2b3a3c;
  --color-success: #88be64;
  --color-success-soft: #2c3a23;
  --color-warning: #c97c4d;
  --color-warning-soft: #3d2b1f;
  --color-error: #d163a7;
  --color-error-soft: #3a253a;
  --color-blue: #6ba9d8;
  --color-blue-soft: #1f3140;

  --shadow-sm: 0 1px 2px rgba(0,0,0,.3);
  --shadow-md: 0 8px 24px rgba(0,0,0,.32);
  --shadow-lg: 0 24px 60px rgba(0,0,0,.5);
}

/* ----- 2. RESET ----- */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
html { -webkit-font-smoothing: antialiased; text-rendering: optimizeLegibility; }
body {
  font-family: var(--font-body);
  font-size: var(--text-base);
  line-height: 1.5;
  color: var(--color-text);
  background: var(--color-bg);
  min-height: 100vh;
}
button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; border: none; background: none; }
a { color: var(--color-primary); text-decoration: none; }
a:hover { text-decoration: underline; }

input, select, textarea {
  width: 100%;
  padding: 0.75rem 0.9rem;
  border: 1px solid var(--color-border);
  border-radius: var(--radius-md);
  background: var(--color-surface-2);
  color: var(--color-text);
  transition: border-color .15s, box-shadow .15s;
}
input:focus, select:focus, textarea:focus {
  outline: none;
  border-color: var(--color-primary);
  box-shadow: 0 0 0 3px var(--color-primary-soft);
}
textarea { min-height: 92px; resize: vertical; }
select { cursor: pointer; }

.hidden { display: none !important; }

/* ----- 3. THEME-SPECIFIC ELEMENTS ----- */
[data-theme="light"] .dark-only { display: none; }
[data-theme="dark"] .light-only { display: none; }

/* ----- 4. LOGIN OVERLAY ----- */
.login-overlay {
  position: fixed; inset: 0; z-index: 200;
  display: grid; place-items: center;
  padding: var(--space-6);
  background:
    radial-gradient(ellipse at 20% 0%, var(--color-primary-soft) 0%, transparent 50%),
    radial-gradient(ellipse at 100% 100%, var(--color-warning-soft) 0%, transparent 50%),
    var(--color-bg);
}
.login-card {
  width: 100%; max-width: 440px;
  background: var(--color-surface);
  border: 1px solid var(--color-divider);
  border-radius: var(--radius-xl);
  padding: var(--space-8);
  box-shadow: var(--shadow-lg);
  display: grid; gap: var(--space-4);
}
.login-brand { display: flex; justify-content: center; margin-bottom: var(--space-2); }
.login-logo { width: 110px; height: auto; object-fit: contain; }
.login-title {
  font-family: var(--font-display); font-size: var(--text-xl);
  font-weight: 800; line-height: 1.05; text-align: center;
}
.login-subtitle { color: var(--color-text-muted); text-align: center; font-size: var(--text-sm); margin-bottom: var(--space-2); }
.login-form { display: grid; gap: var(--space-3); }
.login-field { display: grid; gap: 6px; }
.login-field span { font-size: var(--text-sm); color: var(--color-text-muted); font-weight: 500; }
.login-submit { margin-top: var(--space-2); }
.link-btn {
  font-size: var(--text-sm); color: var(--color-text-muted);
  padding: 0.5rem; text-align: center; border-radius: var(--radius-md);
}
.link-btn:hover { color: var(--color-primary); }
.login-error {
  background: var(--color-error-soft);
  color: var(--color-error);
  padding: 0.7rem 0.9rem;
  border-radius: var(--radius-md);
  font-size: var(--text-sm);
  border: 1px solid var(--color-error);
}
.login-footer { font-size: var(--text-xs); color: var(--color-text-faint); text-align: center; margin-top: var(--space-2); }

/* ----- 5. APP SHELL ----- */
.app { display: grid; grid-template-columns: 280px 1fr; min-height: 100vh; }

.sidebar {
  background: var(--color-surface);
  border-right: 1px solid var(--color-divider);
  padding: var(--space-6);
  position: sticky; top: 0;
  height: 100vh;
  display: flex; flex-direction: column; gap: var(--space-5);
  overflow-y: auto;
}

.brand { display: flex; align-items: center; gap: var(--space-3); padding-bottom: var(--space-3); border-bottom: 1px solid var(--color-divider); }
.brand-logo { width: 56px; height: 56px; object-fit: contain; flex: 0 0 auto; }
.brand-text h1 { font-family: var(--font-display); font-size: 1.5rem; font-weight: 800; line-height: 1; color: var(--color-text); }
.brand-text p { font-size: var(--text-xs); color: var(--color-text-muted); margin-top: 4px; letter-spacing: .02em; }

.nav { display: grid; gap: 4px; }
.nav button {
  padding: 0.75rem 0.9rem;
  border-radius: var(--radius-lg);
  text-align: left;
  background: transparent;
  color: var(--color-text-muted);
  display: flex; align-items: center; justify-content: space-between;
  font-weight: 500;
  transition: background .15s, color .15s;
}
.nav button:hover { background: var(--color-surface-offset); color: var(--color-text); }
.nav button.active { background: var(--color-primary-soft); color: var(--color-primary); font-weight: 600; }
.nav button .nav-count {
  font-size: var(--text-xs);
  background: var(--color-surface-offset);
  padding: 2px 8px; border-radius: var(--radius-full);
  color: var(--color-text-faint);
}
.nav button.active .nav-count { background: var(--color-surface-2); color: var(--color-primary); }

.sidebar-footer { margin-top: auto; display: grid; gap: var(--space-3); padding-top: var(--space-3); border-top: 1px solid var(--color-divider); }
.user-pill {
  display: flex; align-items: center; gap: var(--space-3);
  padding: 0.6rem 0.7rem;
  background: var(--color-surface-offset);
  border-radius: var(--radius-md);
}
.user-avatar {
  width: 36px; height: 36px; border-radius: var(--radius-full);
  background: linear-gradient(135deg, var(--color-primary) 0%, var(--color-blue) 100%);
  color: white; font-weight: 700;
  display: grid; place-items: center;
  font-size: var(--text-sm);
  flex-shrink: 0;
}
.user-meta { display: grid; gap: 2px; min-width: 0; }
.user-meta strong { font-size: var(--text-sm); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

.footer-actions { display: flex; gap: 6px; }
.footer-actions .ghost-btn { flex: 1; padding: 0.5rem; }

/* ----- 6. BUTTONS ----- */
.primary-btn, .secondary-btn, .danger-btn, .ghost-btn {
  padding: 0.75rem 1rem;
  border-radius: var(--radius-md);
  font-weight: 600;
  font-size: var(--text-sm);
  transition: background .15s, color .15s, border-color .15s, transform .05s;
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
}
.primary-btn:active, .secondary-btn:active, .danger-btn:active, .ghost-btn:active { transform: translateY(1px); }

.primary-btn { background: var(--color-primary); color: white; border: 1px solid var(--color-primary); }
.primary-btn:hover { background: var(--color-primary-hover); border-color: var(--color-primary-hover); }
.primary-btn:disabled { opacity: 0.6; cursor: not-allowed; }

.secondary-btn { background: var(--color-surface-2); color: var(--color-text); border: 1px solid var(--color-border); }
.secondary-btn:hover { background: var(--color-surface-offset); }

.danger-btn { background: transparent; color: var(--color-error); border: 1px solid transparent; }
.danger-btn:hover { background: var(--color-error-soft); border-color: var(--color-error); }

.ghost-btn { background: transparent; color: var(--color-text-muted); border: 1px solid transparent; }
.ghost-btn:hover { background: var(--color-surface-offset); color: var(--color-text); }

.icon-btn {
  width: 32px; height: 32px; padding: 0;
  border-radius: var(--radius-md);
  display: grid; place-items: center;
}

/* ----- 7. MAIN AREA ----- */
.main { padding: var(--space-6) var(--space-8); display: grid; gap: var(--space-6); align-content: start; }

.topbar {
  display: flex; justify-content: space-between; gap: var(--space-4);
  align-items: flex-start; flex-wrap: wrap;
}
.headline h2 { font-family: var(--font-display); font-size: var(--text-xl); font-weight: 800; line-height: 1.05; }
.headline p { color: var(--color-text-muted); margin-top: 6px; max-width: 70ch; font-size: var(--text-sm); }

.toolbar { display: flex; gap: var(--space-3); flex-wrap: wrap; align-items: center; }
.search-wrap { position: relative; min-width: 280px; }
.search-icon {
  position: absolute; left: 12px; top: 50%; transform: translateY(-50%);
  color: var(--color-text-faint); font-size: 1.1em; pointer-events: none;
}
.search { padding-left: 2.25rem; background: var(--color-surface); }

/* ----- 8. LAYOUT GRIDS ----- */
.grid-4 { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: var(--space-4); }
.grid-3 { display: grid; grid-template-columns: 2fr 1fr 1fr; gap: var(--space-4); }
.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: var(--space-4); }
.split { display: grid; grid-template-columns: 1fr 1.4fr; gap: var(--space-5); align-items: start; }

/* ----- 9. PANELS / SECTIONS ----- */
.panel {
  background: var(--color-surface);
  border: 1px solid var(--color-divider);
  border-radius: var(--radius-xl);
  box-shadow: var(--shadow-sm);
  overflow: hidden;
}
.section { padding: var(--space-5); }
.section-head {
  display: flex; justify-content: space-between; align-items: center;
  gap: var(--space-3); margin-bottom: var(--space-4); flex-wrap: wrap;
}
.section-head h3 { font-size: var(--text-lg); font-family: var(--font-display); font-weight: 700; }
.section-head .actions { display: flex; gap: var(--space-2); flex-wrap: wrap; }

.stat { padding: var(--space-5); display: grid; gap: 6px; }
.stat h3 { font-size: var(--text-sm); color: var(--color-text-muted); font-weight: 500; }
.stat strong { display: block; font-size: 2rem; font-family: var(--font-display); font-weight: 800; line-height: 1.05; }
.stat small { display: block; color: var(--color-text-faint); font-size: var(--text-xs); }
.stat.highlight strong { color: var(--color-primary); }

/* ----- 10. BADGES ----- */
.badge {
  display: inline-flex; align-items: center; gap: 5px;
  padding: 3px 10px;
  border-radius: var(--radius-full);
  font-size: var(--text-xs);
  font-weight: 700;
  letter-spacing: .01em;
}
.stage-novo, .stage-rascunho, .stage-preparacao { background: var(--color-blue-soft); color: var(--color-blue); }
.stage-contacto, .stage-enviado, .stage-em_analise { background: var(--color-primary-soft); color: var(--color-primary); }
.stage-qualificado, .stage-em_execucao, .stage-execucao { background: var(--color-success-soft); color: var(--color-success); }
.stage-proposta, .stage-pendente { background: var(--color-warning-soft); color: var(--color-warning); }
.stage-negociacao { background: var(--color-error-soft); color: var(--color-error); }
.stage-fechado, .stage-adjudicado, .stage-concluido, .stage-concluida { background: var(--color-success-soft); color: var(--color-success); }
.stage-perdido, .stage-cancelado, .stage-cancelada, .stage-suspenso { background: var(--color-surface-offset); color: var(--color-text-faint); }
.stage-expirado { background: var(--color-warning-soft); color: var(--color-warning); }

.badge.tone-blue { background: var(--color-blue-soft); color: var(--color-blue); }
.badge.tone-success { background: var(--color-success-soft); color: var(--color-success); }
.badge.tone-warning { background: var(--color-warning-soft); color: var(--color-warning); }
.badge.tone-error { background: var(--color-error-soft); color: var(--color-error); }
.badge.tone-muted { background: var(--color-surface-offset); color: var(--color-text-muted); }

/* ----- 11. KANBAN ----- */
.kanban {
  display: grid;
  grid-template-columns: repeat(7, minmax(220px, 1fr));
  gap: var(--space-3);
  overflow-x: auto;
  padding-bottom: 6px;
}
.column {
  background: var(--color-surface-offset);
  border: 1px solid var(--color-divider);
  border-radius: var(--radius-lg);
  padding: var(--space-3);
  min-height: 320px;
}
.column h4 {
  font-size: var(--text-sm); margin-bottom: var(--space-3);
  display: flex; justify-content: space-between; align-items: center;
  color: var(--color-text-muted); font-weight: 600;
}
.column h4 .count {
  font-size: var(--text-xs); background: var(--color-surface-2);
  padding: 2px 8px; border-radius: var(--radius-full); color: var(--color-text);
}
.card {
  background: var(--color-surface-2);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-md);
  padding: var(--space-3);
  margin-bottom: var(--space-2);
  display: grid; gap: 6px;
  cursor: pointer;
  transition: border-color .15s, transform .05s;
}
.card:hover { border-color: var(--color-primary); }
.card:active { transform: translateY(1px); }
.card h5 { font-size: var(--text-sm); font-weight: 700; line-height: 1.3; }
.card p { color: var(--color-text-muted); font-size: var(--text-xs); }
.card strong { font-size: var(--text-sm); }

/* ----- 12. TABLES & LISTS ----- */
.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; }
th, td {
  padding: 0.85rem 0.75rem;
  border-bottom: 1px solid var(--color-divider);
  text-align: left;
  font-size: var(--text-sm);
  vertical-align: middle;
}
th { color: var(--color-text-muted); font-weight: 600; font-size: var(--text-xs); text-transform: uppercase; letter-spacing: .04em; }
tr:hover td { background: var(--color-surface-offset); }
tr.clickable { cursor: pointer; }

.list { display: grid; gap: var(--space-3); }
.item {
  padding: var(--space-4);
  border: 1px solid var(--color-divider);
  border-radius: var(--radius-lg);
  background: var(--color-surface-2);
  display: grid; gap: var(--space-2);
  transition: border-color .15s;
}
.item:hover { border-color: var(--color-primary-soft); }
.item-top {
  display: flex; justify-content: space-between; gap: var(--space-3);
  align-items: flex-start; flex-wrap: wrap;
}
.item-top strong { font-size: var(--text-base); font-weight: 700; }
.item-meta {
  display: flex; gap: var(--space-3); flex-wrap: wrap;
  font-size: var(--text-xs); color: var(--color-text-muted);
}
.item-meta .dot { color: var(--color-text-faint); }

.kpi-line {
  display: flex; justify-content: space-between; align-items: center;
  padding: 0.75rem 0;
  border-bottom: 1px solid var(--color-divider);
  font-size: var(--text-sm);
}
.kpi-line:last-child { border-bottom: none; }
.kpi-line strong { font-family: var(--font-display); font-size: var(--text-base); }

.muted { color: var(--color-text-muted); font-size: var(--text-sm); }
.mini { font-size: var(--text-xs); color: var(--color-text-faint); }

.empty {
  padding: var(--space-8) var(--space-4);
  text-align: center;
  color: var(--color-text-muted);
  font-size: var(--text-sm);
}

.pill-actions { display: flex; gap: var(--space-2); flex-wrap: wrap; align-items: center; }

.note {
  padding: var(--space-3) var(--space-4);
  border-radius: var(--radius-md);
  background: var(--color-primary-soft);
  color: var(--color-primary);
  font-size: var(--text-sm);
  border-left: 3px solid var(--color-primary);
}
.note.warn { background: var(--color-warning-soft); color: var(--color-warning); border-left-color: var(--color-warning); }

/* ----- 13. FORMS ----- */
.forms { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: var(--space-3); }
.forms .full { grid-column: 1 / -1; }
.field-label { font-size: var(--text-xs); font-weight: 600; color: var(--color-text-muted); margin-bottom: 4px; display: block; }
.field-group { display: grid; gap: 4px; }
.field-group .full { grid-column: 1 / -1; }

/* ----- 14. MODAL ----- */
.modal {
  position: fixed; inset: 0; z-index: 100;
  display: grid; place-items: center;
  padding: var(--space-4);
}
.modal-backdrop {
  position: absolute; inset: 0;
  background: rgba(0,0,0,.45);
  backdrop-filter: blur(6px);
}
.modal-card {
  position: relative;
  width: 100%; max-width: 760px;
  max-height: 90vh;
  background: var(--color-surface);
  border-radius: var(--radius-xl);
  box-shadow: var(--shadow-lg);
  display: flex; flex-direction: column;
  overflow: hidden;
  animation: modalEnter .18s ease-out;
}
@keyframes modalEnter {
  from { opacity: 0; transform: translateY(8px) scale(.98); }
  to { opacity: 1; transform: translateY(0) scale(1); }
}
.modal-head {
  display: flex; justify-content: space-between; align-items: center;
  padding: var(--space-4) var(--space-5);
  border-bottom: 1px solid var(--color-divider);
}
.modal-head h3 { font-family: var(--font-display); font-size: var(--text-lg); font-weight: 700; }
.modal-body {
  padding: var(--space-5);
  overflow-y: auto;
  display: grid; gap: var(--space-4);
}

/* ----- 15. TOAST ----- */
.toast {
  position: fixed; bottom: var(--space-6); right: var(--space-6);
  z-index: 250;
  padding: var(--space-3) var(--space-4);
  background: var(--color-text); color: var(--color-bg);
  border-radius: var(--radius-md);
  box-shadow: var(--shadow-lg);
  font-size: var(--text-sm); font-weight: 500;
  max-width: 380px;
  animation: toastIn .2s ease-out;
}
.toast.success { background: var(--color-success); color: white; }
.toast.error { background: var(--color-error); color: white; }
.toast.info { background: var(--color-primary); color: white; }
@keyframes toastIn {
  from { opacity: 0; transform: translateY(8px); }
  to { opacity: 1; transform: translateY(0); }
}

/* ----- 16. LOADING / SPINNER ----- */
.loading-state { display: grid; place-items: center; gap: var(--space-3); padding: var(--space-12); }
.spinner {
  width: 32px; height: 32px;
  border: 3px solid var(--color-divider);
  border-top-color: var(--color-primary);
  border-radius: var(--radius-full);
  animation: spin .8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ----- 17. RESPONSIVE ----- */
@media (max-width: 1100px) {
  .app { grid-template-columns: 1fr; }
  .sidebar { position: relative; height: auto; }
  .grid-4, .grid-3, .grid-2, .split, .forms { grid-template-columns: 1fr; }
  .kanban { grid-template-columns: 1fr; }
  .main { padding: var(--space-4); }
  .search-wrap { min-width: 0; width: 100%; }
}

@media (max-width: 600px) {
  .modal-card { max-height: 95vh; }
  .section { padding: var(--space-4); }
}

/* ----- 18. ACESSIBILIDADE ----- */
:focus-visible { outline: 2px solid var(--color-primary); outline-offset: 2px; }
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: .01ms !important; transition-duration: .01ms !important; }
}
