@import url('https://fonts.googleapis.com/css2?family=Plus+Jakarta+Sans:wght@400;500;600;700&family=Inter:wght@400;500;600&family=JetBrains+Mono:wght@400;500&display=swap');

/* ── Light mode (default) ── */
:root {
  --c-bg:         #F8FAFC;
  --c-surface:    #FFFFFF;
  --c-surface-2:  #F1F5F9;
  --c-primary:    #0F172A;
  --c-secondary:  #1E3A8A;
  --c-accent:     #CA8A04;
  --c-accent-bg:  #FFF7ED;
  --c-text:       #020617;
  --c-text-muted: #475569;
  --c-border:     #E2E8F0;
  --c-gain:       #15803D;
  --c-gain-bg:    #DCFCE7;
  --c-loss:       #B91C1C;
  --c-loss-bg:    #FEE2E2;
  --c-gain-vivid: #22C55E;
  --c-loss-vivid: #EF4444;
  --c-pending:    #f59e0b;
  --c-pending-bg: rgba(245,158,11,.12);

  --radius-sm: 6px;
  --radius:    12px;
  --radius-lg: 16px;

  --shadow-sm: 0 1px 2px rgba(0,0,0,.06);
  --shadow:    0 1px 3px rgba(0,0,0,.10), 0 1px 2px rgba(0,0,0,.06);
  --shadow-md: 0 4px 6px rgba(0,0,0,.08), 0 2px 4px rgba(0,0,0,.04);

  --font-head: 'Plus Jakarta Sans', system-ui, sans-serif;
  --font-body: 'Inter', system-ui, sans-serif;
  --font-mono: 'JetBrains Mono', 'Courier New', monospace;

  --space-1: 4px;  --space-2: 8px;   --space-3: 12px;
  --space-4: 16px; --space-5: 20px;  --space-6: 24px;
  --space-8: 32px; --space-10: 40px; --space-12: 48px;

  --ease-out:  cubic-bezier(0.16, 1, 0.3, 1);
  --transition: 160ms var(--ease-out);
  --transition-slow: 320ms var(--ease-out);
}

/* ── Dark mode ── */
[data-theme="dark"], @media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --c-bg:         #020617;
    --c-surface:    #0F172A;
    --c-surface-2:  #1E293B;
    --c-primary:    #F8FAFC;
    --c-secondary:  #3B82F6;
    --c-accent:     #CA8A04;
    --c-accent-bg:  #1C1A08;
    --c-text:       #F8FAFC;
    --c-text-muted: #94A3B8;
    --c-border:     #1E293B;
    --c-gain:       #22C55E;
    --c-gain-bg:    #052E16;
    --c-loss:       #EF4444;
    --c-loss-bg:    #1F0808;
    --c-pending:    #fbbf24;
  }
}
[data-theme="dark"] {
  --c-bg:         #020617;
  --c-surface:    #0F172A;
  --c-surface-2:  #1E293B;
  --c-primary:    #F8FAFC;
  --c-secondary:  #3B82F6;
  --c-text:       #F8FAFC;
  --c-text-muted: #94A3B8;
  --c-border:     #1E293B;
  --c-gain:       #22C55E;
  --c-gain-bg:    #052E16;
  --c-loss:       #EF4444;
  --c-loss-bg:    #1F0808;
  --c-pending:    #fbbf24;
}

/* ── Reset i base ── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
html { font-size: 16px; -webkit-text-size-adjust: 100%; }
body {
  font-family: var(--font-body);
  background: var(--c-bg);
  color: var(--c-text);
  line-height: 1.6;
  min-height: 100dvh;
}

/* ── Tipografia ── */
h1, h2, h3, h4 { font-family: var(--font-head); font-weight: 700; line-height: 1.25; color: var(--c-text); }
h1 { font-size: clamp(1.75rem, 4vw, 2.5rem); }
h2 { font-size: clamp(1.25rem, 3vw, 1.75rem); }
h3 { font-size: 1.125rem; }
p  { line-height: 1.7; max-width: 68ch; }
small { font-size: .875rem; color: var(--c-text-muted); }
a { color: var(--c-secondary); text-decoration: underline; }
a:hover { text-decoration: none; }

/* ── Components ── */
.card {
  background: var(--c-surface);
  border: 1px solid var(--c-border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: var(--space-6);
}

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: var(--space-2);
  font-family: var(--font-body);
  font-weight: 600;
  font-size: .9375rem;
  line-height: 1;
  padding: 0.75rem 1.5rem;
  border-radius: var(--radius-sm);
  border: none;
  cursor: pointer;
  transition: background var(--transition), opacity var(--transition), transform var(--transition);
  text-decoration: none;
  white-space: nowrap;
  min-height: 44px;
  min-width: 44px;
}
.btn:focus-visible { outline: 3px solid var(--c-secondary); outline-offset: 2px; }
.btn:active { transform: scale(.98); }
.btn[disabled], .btn[aria-busy="true"] { opacity: .6; cursor: not-allowed; pointer-events: none; }

.btn-primary   { background: var(--c-secondary); color: #fff; }
.btn-primary:hover { background: #1e40af; }
.btn-accent    { background: var(--c-accent); color: #fff; }
.btn-accent:hover { background: #a16207; }
.btn-ghost     { background: transparent; color: var(--c-primary); border: 1px solid var(--c-border); }
.btn-ghost:hover { background: var(--c-surface-2); }
.btn-danger    { background: var(--c-loss-vivid); color: #fff; }
.btn-danger:hover { background: #dc2626; }
.btn-buy       { background: var(--c-gain-vivid); color: #fff; }
.btn-buy:hover { background: #16a34a; }
.btn-sell      { background: var(--c-loss-vivid); color: #fff; }
.btn-sell:hover { background: #dc2626; }

.btn-sm { font-size: .8125rem; padding: .5rem 1rem; min-height: 36px; }
.btn-lg { font-size: 1.0625rem; padding: 1rem 2rem; min-height: 52px; }
.btn-full { width: 100%; }

/* ── Form ── */
.field { display: flex; flex-direction: column; gap: var(--space-1); margin-bottom: var(--space-5); }
.field label { font-size: .875rem; font-weight: 600; color: var(--c-text); letter-spacing: 0; }
.field input, .field select {
  font-family: var(--font-body);
  font-size: 1rem;
  padding: .625rem .875rem;
  border: 1.5px solid var(--c-border);
  border-radius: var(--radius-sm);
  background: var(--c-surface);
  color: var(--c-text);
  transition: border-color var(--transition), box-shadow var(--transition);
  min-height: 44px;
}
.field input:focus, .field select:focus {
  outline: none;
  border-color: var(--c-secondary);
  box-shadow: 0 0 0 3px rgba(30,58,138,.28);
}
.field input.error { border-color: var(--c-loss-vivid); }
.field .error-msg { font-size: .8125rem; color: var(--c-loss); display: none; }
.field.has-error .error-msg { display: block; }
.field.has-error input { border-color: var(--c-loss-vivid); }

/* ── Badge gain/loss ── */
.badge {
  display: inline-flex; align-items: center; gap: 4px;
  font-size: .8125rem; font-weight: 600;
  padding: 2px 8px; border-radius: 99px;
}
.badge-gain { color: var(--c-gain); background: var(--c-gain-bg); }
.badge-loss { color: var(--c-loss); background: var(--c-loss-bg); }
.badge-neutral { color: var(--c-text-muted); background: var(--c-surface-2); }

/* ── Taula ── */
.table-wrap { overflow-x: auto; -webkit-overflow-scrolling: touch; }
table { width: 100%; border-collapse: collapse; font-size: .9375rem; }
thead th { background: var(--c-surface-2); font-size: .75rem; text-transform: uppercase; letter-spacing: .06em; font-weight: 700; color: var(--c-text-muted); padding: var(--space-3) var(--space-4); text-align: left; white-space: nowrap; position: sticky; top: 0; z-index: 1; }
tbody tr { border-bottom: 1px solid var(--c-border); cursor: pointer; transition: background var(--transition); }
tbody tr:hover { background: var(--c-surface-2); }
tbody td { padding: var(--space-3) var(--space-4); vertical-align: middle; }
td.num, th.num { text-align: right; font-family: var(--font-mono); font-variant-numeric: tabular-nums; }

/* ── Toast ── */
#toast-container {
  position: fixed; bottom: var(--space-6); right: var(--space-6);
  z-index: 1000; display: flex; flex-direction: column;
  gap: var(--space-2); pointer-events: none;
}
.toast {
  background: var(--c-surface);
  border: 1px solid var(--c-border);
  border-radius: var(--radius-sm);
  box-shadow: var(--shadow-md);
  padding: var(--space-3) var(--space-5);
  font-size: .9375rem;
  font-weight: 500;
  animation: toast-in .22s var(--ease-out);
  pointer-events: all;
  max-width: 320px;
}
.toast-success { border-left: 4px solid var(--c-gain-vivid); }
.toast-error   { border-left: 4px solid var(--c-loss-vivid); }
.toast-info    { border-left: 4px solid var(--c-secondary); }
@keyframes toast-in { from { opacity:0; transform: translateY(10px); } to { opacity:1; transform: translateY(0); } }

/* toast desplaçat per la sticky bar en mòbil */
@media (max-width: 639px) {
  #toast-container { bottom: 88px; right: var(--space-4); left: var(--space-4); }
  .toast { max-width: 100%; }
}

/* ── Top bar ── */
.topbar {
  position: sticky; top: 0; z-index: 100;
  background: var(--c-surface); border-bottom: 1px solid var(--c-border);
  display: flex; align-items: center; gap: var(--space-4);
  padding: 0 var(--space-6); height: 60px;
}
.topbar-brand { font-family: var(--font-head); font-weight: 700; font-size: 1.0625rem; color: var(--c-text); text-decoration: none; display: flex; align-items: center; gap: 6px; }
.topbar-brand span { color: var(--c-accent); }
.topbar-spacer { flex: 1; }

/* Avatar d'usuari */
.user-avatar {
  width: 32px; height: 32px; border-radius: 50%;
  background: var(--c-secondary); color: #fff;
  font-size: .6875rem; font-weight: 700; letter-spacing: .02em;
  display: grid; place-items: center; flex-shrink: 0;
  font-family: var(--font-head);
}

/* ── Layout ── */
.container { max-width: 1200px; margin: 0 auto; padding: 0 var(--space-6); }
.page { padding: var(--space-8) 0; }
.auth-wrap {
  min-height: 100dvh;
  display: flex; align-items: center; justify-content: center;
  padding: var(--space-6);
  background:
    radial-gradient(ellipse 60% 50% at 50% 0%, rgba(30,58,138,.07), transparent 60%),
    var(--c-bg);
}
.auth-card { width: 100%; max-width: 420px; }

/* ── Skeleton ── */
.skeleton { background: linear-gradient(90deg, var(--c-surface-2) 25%, var(--c-border) 50%, var(--c-surface-2) 75%); background-size: 200% 100%; animation: shimmer 1.4s infinite; border-radius: var(--radius-sm); }
@keyframes shimmer { to { background-position: -200% 0; } }

/* ── Animació d'entrada en cascada ── */
@keyframes fade-up {
  from { opacity: 0; transform: translateY(14px); }
  to   { opacity: 1; transform: none; }
}
.animate-in {
  animation: fade-up 0.38s var(--ease-out) both;
}

/* ── Gain/loss color helpers ── */
.gain { color: var(--c-gain); }
.loss { color: var(--c-loss); }
.mono { font-family: var(--font-mono); font-variant-numeric: tabular-nums; }

/* ── Market status banner ── */
.market-banner {
  background: var(--c-accent-bg); border: 1px solid var(--c-accent);
  border-radius: var(--radius-sm); padding: var(--space-3) var(--space-4);
  font-size: .875rem; font-weight: 500; color: var(--c-accent);
  display: flex; align-items: center; gap: var(--space-2);
}

/* ── Market dot live pulse ── */
@keyframes live-pulse {
  0%, 100% { box-shadow: 0 0 0 0 rgba(34,197,94,.55); }
  60%       { box-shadow: 0 0 0 7px rgba(34,197,94,.0); }
}
.market-dot { width: 10px; height: 10px; border-radius: 50%; background: var(--c-loss-vivid); flex-shrink: 0; transition: background var(--transition); }
.market-dot.open { background: var(--c-gain-vivid); animation: live-pulse 2.2s ease-in-out infinite; }

/* ── Dots loader (substitueix spinner circular) ── */
.btn-dots { display: none; gap: 4px; align-items: center; }
.btn-dots span {
  width: 5px; height: 5px; border-radius: 50%;
  background: rgba(255,255,255,.85);
  animation: dot-pulse .9s ease-in-out infinite;
}
.btn-dots span:nth-child(2) { animation-delay: .15s; }
.btn-dots span:nth-child(3) { animation-delay: .30s; }
@keyframes dot-pulse {
  0%,80%,100% { transform: scale(.55); opacity: .4; }
  40%          { transform: scale(1.0);  opacity: 1;   }
}

/* ── Responsive ── */
@media (max-width: 768px) {
  .container { padding: 0 var(--space-4); }
  .topbar { padding: 0 var(--space-4); height: 56px; }
  .card { padding: var(--space-4); }
  thead th, tbody td { padding: var(--space-2) var(--space-3); }
}

/* ── Modal ── */
.modal-backdrop {
  display: none; position: fixed; inset: 0;
  background: rgba(2,6,23,.55);
  backdrop-filter: blur(6px);
  -webkit-backdrop-filter: blur(6px);
  z-index: 200;
  align-items: center; justify-content: center;
  padding: var(--space-4);
}
.modal-backdrop.open { display: flex; animation: modal-fade .18s var(--ease-out); }
@keyframes modal-fade { from { opacity: 0; } to { opacity: 1; } }

.modal {
  background: var(--c-surface);
  border: 1px solid var(--c-border);
  border-radius: var(--radius-lg);
  width: 100%; max-width: 480px;
  box-shadow: 0 24px 64px rgba(0,0,0,.32);
  display: flex; flex-direction: column;
  max-height: 90vh; overflow: hidden;
  animation: modal-slide .22s var(--ease-out);
}
@keyframes modal-slide {
  from { opacity: 0; transform: translateY(16px) scale(.97); }
  to   { opacity: 1; transform: none; }
}

.modal-header {
  display: flex; align-items: center; justify-content: space-between;
  padding: var(--space-5) var(--space-6);
  border-bottom: 1px solid var(--c-border);
  flex-shrink: 0; gap: var(--space-3);
}
.modal-header h3 { margin: 0; font-size: 1.0625rem; font-family: var(--font-head); }

.modal-close {
  flex-shrink: 0; background: none; border: none; cursor: pointer;
  color: var(--c-text-muted); padding: 6px; border-radius: var(--radius-sm);
  display: flex; align-items: center; justify-content: center;
  transition: color var(--transition), background var(--transition);
}
.modal-close:hover { color: var(--c-text); background: var(--c-surface-2); }
.modal-close:focus-visible { outline: 2px solid var(--c-secondary); outline-offset: 1px; }

.modal-body {
  padding: var(--space-6);
  overflow-y: auto; flex: 1;
}
.modal-body .field:last-child { margin-bottom: 0; }

.modal-footer {
  padding: var(--space-4) var(--space-6);
  border-top: 1px solid var(--c-border);
  display: flex; align-items: center; justify-content: flex-end;
  gap: var(--space-3); flex-shrink: 0;
}
.modal-footer-info {
  margin-right: auto; font-size: .8125rem; color: var(--c-text-muted);
}

@media (max-width: 540px) {
  .modal { max-width: 100%; border-radius: var(--radius) var(--radius) 0 0; align-self: flex-end; max-height: 95vh; }
  .modal-backdrop { align-items: flex-end; padding: 0; }
  .modal-footer { flex-wrap: wrap; }
  .modal-footer .btn { flex: 1; }
}

/* ── Reduced motion ── */
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: .01ms !important; transition-duration: .01ms !important; }
}
