:root {
  color-scheme: dark; /* que los calendarios y desplegables nativos salgan oscuros */

  --bg: #0d0d0f;
  --panel: #16161a;
  --panel-2: #1c1c22;
  --ink: #ece9e2;
  --muted: #8b8792;
  --line: #2a2a31;

  /* Dorado: el color de la marca. Manda en botones, pestañas y resaltes. */
  --gold: #c9a227;
  --gold-bright: #e0b93a;
  --gold-soft: rgba(201, 162, 39, .14);
  --gold-line: rgba(201, 162, 39, .38);

  /* Rojo sangre: SOLO para acciones que destruyen algo. */
  --blood: #8b1a1a;
  --blood-bright: #b52626;
  --blood-soft: rgba(139, 26, 26, .18);

  /* Verde y rojo de resultado. Aquí el color informa, no decora: no tocar. */
  --win: #4ade80;
  --loss: #f87171;
  --amber: #fbbf24;

  --radius: 10px;
  --shadow: 0 1px 2px rgba(0, 0, 0, .4), 0 2px 8px rgba(0, 0, 0, .3);
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font: 14px/1.5 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
}

header {
  background: linear-gradient(180deg, #1a1a20, #131317);
  border-bottom: 1px solid var(--gold-line);
  padding: 0 20px;
  display: flex;
  align-items: center;
  gap: 0 24px;
  flex-wrap: wrap; /* con el usuario a la derecha, en pantallas estrechas baja a otra línea */
  position: sticky;
  top: 0;
  z-index: 20;
}

header h1 {
  font-size: 16px;
  margin: 0;
  padding: 14px 0;
  letter-spacing: -.01em;
  white-space: nowrap;
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  gap: 10px;
}

header h1 .wordmark { color: var(--ink); }
header h1 .wordmark span { color: var(--gold); }

/* El escudo es más alto que ancho: manda la altura y el ancho se ajusta solo. */
#logo { height: 32px; width: auto; display: block; }

nav { display: flex; gap: 4px; }

nav button {
  background: none;
  border: none;
  border-bottom: 2px solid transparent;
  padding: 15px 12px 13px;
  font: inherit;
  font-weight: 500;
  color: var(--muted);
  cursor: pointer;
}

nav button:hover { color: var(--ink); }
nav button.active { color: var(--gold); border-bottom-color: var(--gold); }

main { padding: 20px; max-width: 1800px; margin: 0 auto; }
.view { display: none; }
.view.active { display: block; }

.panel {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 16px;
  margin-bottom: 16px;
}

.panel h2 { margin: 0 0 14px; font-size: 14px; font-weight: 600; }
.panel h2 small { font-weight: 400; color: var(--muted); margin-left: 8px; }

label { display: block; font-size: 12px; color: var(--muted); margin-bottom: 4px; font-weight: 500; }

input, select, textarea {
  width: 100%;
  padding: 7px 9px;
  border: 1px solid var(--line);
  border-radius: 7px;
  font: inherit;
  background: var(--panel-2);
  color: var(--ink);
}

input::placeholder { color: #5f5b68; }

input:focus, select:focus, textarea:focus {
  outline: 2px solid var(--gold-soft);
  border-color: var(--gold);
}

button.btn {
  padding: 8px 14px;
  border-radius: 7px;
  border: 1px solid var(--line);
  background: var(--panel-2);
  color: var(--ink);
  font: inherit;
  font-weight: 500;
  cursor: pointer;
  white-space: nowrap;
}

button.btn:hover { background: #24242c; border-color: #3a3a44; }

button.btn.primary {
  background: var(--gold);
  border-color: var(--gold);
  color: #14140f; /* texto oscuro sobre dorado: es lo que da contraste de verdad */
  font-weight: 600;
}

button.btn.primary:hover { background: var(--gold-bright); border-color: var(--gold-bright); }

/* Rojo sangre = esto borra algo. */
button.btn.danger { color: #ff9b9b; border-color: rgba(139, 26, 26, .5); background: var(--blood-soft); }
button.btn.danger:hover { background: var(--blood); border-color: var(--blood-bright); color: #fff; }

button.btn:disabled { opacity: .45; cursor: not-allowed; }

.grid { display: grid; gap: 12px; }
.row { display: flex; gap: 10px; align-items: flex-end; flex-wrap: wrap; }
.row > * { flex: 1; min-width: 120px; }
.row > .fixed { flex: 0 0 auto; min-width: 0; }
.spacer { flex: 1; min-width: 0; }

/* --- tarjetas de resumen --- */
.stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(160px, 1fr)); gap: 12px; }

.stat {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 14px;
  box-shadow: var(--shadow);
}

.stat .k { font-size: 11px; color: var(--muted); text-transform: uppercase; letter-spacing: .04em; }
.stat .v { font-size: 22px; font-weight: 600; margin-top: 4px; letter-spacing: -.02em; color: var(--gold); }
.stat .s { font-size: 12px; color: var(--muted); }

/* El margen manda sobre el dorado: aquí el color dice si ganas o pierdes. */
.stat .v.pos { color: var(--win); }
.stat .v.neg { color: var(--loss); }

/* --- tablas --- */
.table-wrap { overflow-x: auto; border: 1px solid var(--line); border-radius: var(--radius); background: var(--panel); }
table { border-collapse: collapse; width: 100%; font-size: 13px; }

th {
  background: #1f1f26;
  text-align: left;
  padding: 9px 10px;
  border-bottom: 1px solid var(--line);
  font-weight: 600;
  font-size: 12px;
  white-space: nowrap;
  position: sticky;
  top: 0;
  color: #cfcad9;
}

th.sortable { cursor: pointer; user-select: none; }
th.sortable:hover { color: var(--gold); }
th .arrow { color: var(--gold); }

td { padding: 7px 10px; border-bottom: 1px solid #212128; white-space: nowrap; }
tbody tr:hover { background: #1b1b21; }
.num { text-align: right; font-variant-numeric: tabular-nums; }
.pos { color: var(--win); font-weight: 600; }
.neg { color: var(--loss); font-weight: 600; }

.pill {
  display: inline-block;
  padding: 1px 7px;
  border-radius: 20px;
  font-size: 11px;
  font-weight: 600;
  background: #26262e;
  color: var(--muted);
}

.pill.in_stock { background: rgba(74, 222, 128, .14); color: var(--win); }
.pill.sold { background: var(--gold-soft); color: var(--gold-bright); }
.pill.partial { background: rgba(251, 191, 36, .14); color: var(--amber); }

/* Rarezas: los colores propios de Magic, adaptados al fondo negro. */
.pill.mythic { background: rgba(234, 88, 12, .18); color: #fb923c; }
.pill.rare { background: var(--gold-soft); color: var(--gold-bright); }
.pill.uncommon { background: rgba(148, 163, 184, .16); color: #cbd5e1; }
.pill.common { background: #26262e; color: #9ca3af; }

/* Estado (condición) de la carta. Tonos neutros y ámbar a propósito: el verde y
   el rojo quedan reservados al margen (ganar/perder). Sigla + tooltip completo. */
.pill.cond-top { background: rgba(148, 163, 184, .18); color: #e2e8f0; }
.pill.cond-mid { background: #26262e; color: #9ca3af; }
.pill.cond-low { background: rgba(251, 191, 36, .14); color: var(--amber); }

/* Rarezas especiales que trae TCG Power Tools. */
.pill.land { background: rgba(45, 212, 191, .15); color: #5eead4; }
.pill.token { background: rgba(56, 189, 248, .15); color: #7dd3fc; }
.pill.tip-card { background: rgba(129, 140, 248, .16); color: #a5b4fc; }
.pill.time-shifted { background: rgba(168, 85, 247, .18); color: #d8b4fe; }
.pill.special { background: rgba(236, 72, 153, .16); color: #f9a8d4; }
.pill.masterpiece {
  background: linear-gradient(90deg, rgba(251, 191, 36, .28), rgba(244, 114, 182, .24));
  color: #fde68a;
}
.pill.foil { background: linear-gradient(90deg, rgba(236, 72, 153, .22), rgba(99, 102, 241, .22)); color: #e9d5ff; }

.price-input { width: 82px; padding: 3px 6px; text-align: right; font-variant-numeric: tabular-nums; }
.price-input.saved { border-color: var(--win); background: rgba(74, 222, 128, .12); }

.muted { color: var(--muted); }
.empty { padding: 40px; text-align: center; color: var(--muted); }
.pager { display: flex; gap: 8px; align-items: center; justify-content: center; padding: 12px; }

/* --- modal --- */
.modal-bg {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, .72);
  display: none;
  align-items: center;
  justify-content: center;
  z-index: 50;
  padding: 20px;
}

.modal-bg.open { display: flex; }

.modal {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 20px;
  width: 100%;
  max-width: 480px;
  max-height: 90vh;
  overflow: auto;
  box-shadow: 0 24px 60px rgba(0, 0, 0, .7);
}

.modal h3 { margin: 0 0 4px; font-size: 16px; color: var(--gold); }
.modal .sub { color: var(--muted); font-size: 12px; margin-bottom: 16px; }

/* --- avisos --- */
#toast {
  position: fixed;
  bottom: 20px;
  right: 20px;
  z-index: 100;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.toast {
  background: #26262e;
  border: 1px solid var(--line);
  color: var(--ink);
  padding: 10px 16px;
  border-radius: 8px;
  box-shadow: 0 8px 24px rgba(0, 0, 0, .6);
  font-size: 13px;
  max-width: 380px;
  animation: slide .2s ease;
}

.toast.err { background: var(--blood); border-color: var(--blood-bright); color: #fff; }
.toast.ok { background: rgba(74, 222, 128, .16); border-color: rgba(74, 222, 128, .4); color: var(--win); }

@keyframes slide { from { transform: translateY(8px); opacity: 0; } }

.hint { font-size: 12px; color: var(--muted); margin-top: 6px; line-height: 1.45; }
.checks { display: flex; flex-wrap: wrap; gap: 4px 12px; }
.checks label { display: flex; align-items: center; gap: 5px; color: var(--ink); font-weight: 400; margin: 0; }
.checks input { width: auto; }

/* --- aviso de base abierta en otro equipo --- */
#warn-banner {
  display: flex;
  align-items: center;
  gap: 16px;
  background: rgba(251, 191, 36, .1);
  border-bottom: 1px solid rgba(251, 191, 36, .35);
  color: #f5d68a;
  padding: 10px 20px;
  font-size: 13px;
  line-height: 1.45;
}

#warn-banner[hidden] { display: none; }
#warn-banner strong { color: var(--amber); }
#warn-banner button { flex: 0 0 auto; }

/* --- carrito --- */
.cart-btn { background: var(--gold-soft); border-color: var(--gold-line); color: var(--gold-bright); font-weight: 600; }
.cart-btn:hover { background: rgba(201, 162, 39, .24); border-color: var(--gold); }

.cart-btn #cart-count {
  display: inline-block;
  background: var(--gold);
  color: #14140f;
  border-radius: 10px;
  padding: 0 6px;
  margin-left: 4px;
  font-size: 12px;
  font-weight: 700;
}

#cart-panel {
  position: fixed;
  top: 0;
  right: 0;
  bottom: 0;
  width: 380px;
  max-width: 92vw;
  background: var(--panel);
  border-left: 1px solid var(--gold-line);
  box-shadow: -8px 0 32px rgba(0, 0, 0, .6);
  z-index: 40;
  display: flex;
  flex-direction: column;
  transform: translateX(100%);
  transition: transform .18s ease;
}

#cart-panel.open { transform: translateX(0); }

.cart-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 16px;
  border-bottom: 1px solid var(--line);
}

.cart-head strong { color: var(--gold); }

#cart-lines { flex: 1; overflow-y: auto; padding: 8px 12px; }

.cart-line {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 8px 0;
  border-bottom: 1px solid #212128;
}

.cart-line-info { flex: 1; min-width: 0; font-size: 13px; }
.cart-line-info strong { display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.cart-line-info .muted { font-size: 11px; }
.cart-qty { width: 46px; padding: 4px; text-align: center; }
.cart-price { width: 68px; padding: 4px 6px; text-align: right; }
.cart-del { padding: 4px 8px; line-height: 1; }

.cart-foot { border-top: 1px solid var(--line); padding: 14px 16px; background: var(--panel-2); }
.cart-total { display: flex; justify-content: space-between; align-items: baseline; font-size: 15px; }
.cart-total strong { font-size: 20px; letter-spacing: -.02em; color: var(--gold); }

/* --- entrar / recuperar contraseña --- */
.auth-page { min-height: 100vh; display: flex; align-items: center; justify-content: center; padding: 16px; }
.auth-box { width: 100%; max-width: 380px; }
.auth-brand { display: flex; align-items: center; justify-content: center; gap: 10px; font-size: 18px; margin: 0 0 18px; }
.auth-brand img { height: 40px; }
.auth-brand .wordmark span { color: var(--gold); }
.auth-box .panel input { width: 100%; margin-bottom: 12px; }
.auth-box .btn.primary { width: 100%; justify-content: center; margin-top: 4px; }
.auth-error { color: var(--loss); font-size: 13px; margin: 0 0 10px; }
.auth-ok { color: var(--win); font-size: 13px; margin: 0 0 10px; }
.link-btn { background: none; border: 0; color: var(--muted); font: inherit; font-size: 12px; cursor: pointer;
            display: block; margin: 12px auto 0; text-decoration: underline; }
.link-btn:hover { color: var(--gold); }

/* --- usuario en la cabecera --- */
.user-box { display: flex; align-items: center; gap: 8px; padding: 8px 0; font-size: 12px; color: var(--muted); white-space: nowrap; }
.user-box strong { color: var(--ink); font-weight: 500; }
.pill.role-admin { background: var(--gold-soft); color: var(--gold-bright); }
.pill.role-editor { background: #26262e; color: #9ca3af; }

/* --- timeline (gráficos) ---
   Colores de datos validados contra el fondo del panel (#16161a) con el script de
   la guía de visualización: azul = comprado / en stock, naranja = vendido. El dorado
   de la marca es demasiado claro para marcas de datos en fondo oscuro. */
:root { --viz-1: #3987e5; --viz-2: #d95926; --viz-grid: #2a2a31; --viz-axis: #3a3a42; --viz-band: rgba(201, 162, 39, .16); --viz-band-line: rgba(201, 162, 39, .18); }
.tl-chart { position: relative; width: 100%; min-height: 120px; }
.tl-chart svg { display: block; width: 100%; overflow: visible; }
.tl-chart text { fill: var(--muted); font-size: 11px; }
.tl-chart .lbl { fill: var(--ink); font-size: 11px; }
.tl-tip { position: absolute; pointer-events: none; background: #0f0f12; border: 1px solid var(--line);
          border-radius: 8px; padding: 8px 10px; font-size: 12px; min-width: 180px; box-shadow: var(--shadow);
          z-index: 5; white-space: nowrap; }
.tl-tip .v { font-size: 15px; font-weight: 600; color: var(--ink); }
.tl-tip .n { color: var(--ink); margin: 2px 0 6px; }
.tl-tip .r { display: flex; justify-content: space-between; gap: 14px; color: var(--muted); }
.tl-tip .r b { color: var(--ink); font-weight: 500; }
.tl-sub { font-size: 12px; color: var(--muted); margin-bottom: 4px; }
.tl-legend { display: flex; gap: 16px; font-size: 12px; color: var(--muted); margin-bottom: 6px; }
.tl-legend i { display: inline-block; width: 10px; height: 10px; border-radius: 2px; margin-right: 6px; vertical-align: -1px; }
